Minor simplifications.
authorEric Kohl <eric.kohl@reactos.org>
Mon, 11 Oct 2004 12:37:04 +0000 (12:37 +0000)
committerEric Kohl <eric.kohl@reactos.org>
Mon, 11 Oct 2004 12:37:04 +0000 (12:37 +0000)
svn path=/trunk/; revision=11269

reactos/drivers/fs/np/create.c
reactos/drivers/fs/np/fsctrl.c

index 75d7076..54d2e91 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: create.c,v 1.22 2004/07/03 17:40:20 navaraf Exp $
+/* $Id: create.c,v 1.23 2004/10/11 12:37:04 ekohl Exp $
  *
  * COPYRIGHT:  See COPYING in the top level directory
  * PROJECT:    ReactOS kernel
 
 /* FUNCTIONS *****************************************************************/
 
+static PNPFS_PIPE
+NpfsFindPipe(PNPFS_DEVICE_EXTENSION DeviceExt,
+            PUNICODE_STRING PipeName)
+{
+  PLIST_ENTRY CurrentEntry;
+  PNPFS_PIPE Pipe;
+
+  CurrentEntry = DeviceExt->PipeListHead.Flink;
+  while (CurrentEntry != &DeviceExt->PipeListHead)
+    {
+      Pipe = CONTAINING_RECORD(CurrentEntry, NPFS_PIPE, PipeListEntry);
+      if (RtlCompareUnicodeString(PipeName,
+                                 &Pipe->PipeName,
+                                 TRUE) == 0)
+       {
+         DPRINT1("<%wZ> = <%wZ>\n", PipeName, &Pipe->PipeName);
+         return Pipe;
+       }
+
+       CurrentEntry = CurrentEntry->Flink;
+     }
+
+  return NULL;
+}
+
+
+static PNPFS_FCB
+NpfsFindListeningServerInstance(PNPFS_PIPE Pipe)
+{
+  PLIST_ENTRY CurrentEntry;
+  PNPFS_FCB ServerFcb;
+
+  CurrentEntry = Pipe->ServerFcbListHead.Flink;
+  while (CurrentEntry != &Pipe->ServerFcbListHead)
+    {
+      ServerFcb = CONTAINING_RECORD(CurrentEntry, NPFS_FCB, FcbListEntry);
+      if (ServerFcb->PipeState == FILE_PIPE_LISTENING_STATE)
+       {
+         DPRINT("Server found! Fcb %p\n", ServerFcb);
+         return ServerFcb;
+       }
+      CurrentEntry = CurrentEntry->Flink;
+    }
+
+  return NULL;
+}
+
+
 NTSTATUS STDCALL
 NpfsCreate(
    PDEVICE_OBJECT DeviceObject,
    PIRP Irp)
 {
-   PLIST_ENTRY CurrentEntry;
-   PNPFS_PIPE Current;
    PIO_STACK_LOCATION IoStack;
    PFILE_OBJECT FileObject;
    PNPFS_PIPE Pipe;
@@ -43,29 +89,17 @@ NpfsCreate(
    DPRINT("FileName %wZ\n", &FileObject->FileName);
 
    Irp->IoStatus.Information = 0;
-    
+
    /*
     * Step 1. Find the pipe we're trying to open.
     */
 
    KeLockMutex(&DeviceExt->PipeListLock);
-   CurrentEntry = DeviceExt->PipeListHead.Flink;
-   while (CurrentEntry != &DeviceExt->PipeListHead)
-   {
-      Current = CONTAINING_RECORD(CurrentEntry, NPFS_PIPE, PipeListEntry);
-      if (RtlCompareUnicodeString(
-             &FileObject->FileName,
-             &Current->PipeName,
-             TRUE) == 0)
-      {
-         break;
-      }
-      CurrentEntry = CurrentEntry->Flink;
-   }
-
-   /* Not found, bail out with error. */
-   if (CurrentEntry == &DeviceExt->PipeListHead)
+   Pipe = NpfsFindPipe(DeviceExt,
+                      &FileObject->FileName);
+   if (Pipe == NULL)
    {
+      /* Not found, bail out with error. */
       DPRINT("No pipe found!\n");
       KeUnlockMutex(&DeviceExt->PipeListLock);
       Irp->IoStatus.Status = STATUS_OBJECT_NAME_NOT_FOUND;
@@ -75,9 +109,6 @@ NpfsCreate(
 
    KeUnlockMutex(&DeviceExt->PipeListLock);
 
-   /* Save the pipe we found for later use. */
-   Pipe = Current;
-
    /*
     * Step 2. Search for listening server FCB.
     */
@@ -88,21 +119,10 @@ NpfsCreate(
     */
    KeLockMutex(&Pipe->FcbListLock);
 
-   CurrentEntry = Pipe->ServerFcbListHead.Flink;
-   while (CurrentEntry != &Pipe->ServerFcbListHead)
-   {
-      ServerFcb = CONTAINING_RECORD(CurrentEntry, NPFS_FCB, FcbListEntry);
-      if (ServerFcb->PipeState == FILE_PIPE_LISTENING_STATE)
-      {
-         DPRINT("Server found! Fcb %p\n", ServerFcb);
-         break;
-      }
-      CurrentEntry = CurrentEntry->Flink;
-   }
-
-   /* Not found, bail out with error for FILE_OPEN requests. */
-   if (CurrentEntry == &Pipe->ServerFcbListHead)
+   ServerFcb = NpfsFindListeningServerInstance(Pipe);
+   if (ServerFcb == NULL)
    {
+      /* Not found, bail out with error for FILE_OPEN requests. */
       DPRINT("No server fcb found!\n");
       if (Disposition == FILE_OPEN)
       {
@@ -111,7 +131,6 @@ NpfsCreate(
          IoCompleteRequest(Irp, IO_NO_INCREMENT);
          return STATUS_PIPE_BUSY;
       }
-      ServerFcb = NULL;
    }
 
    /*
@@ -188,7 +207,7 @@ NpfsCreate(
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
 
    DPRINT("Success!\n");
-   
+
    return STATUS_SUCCESS;
 }
 
@@ -463,11 +482,12 @@ NpfsClose(
       /* FIXME: Clean up existing connections here ?? */
       DPRINT("Server\n");
       Pipe->CurrentInstances--;
-   } else
+   }
+   else
    {
       DPRINT("Client\n");
    }
-  
+
    if (Fcb->PipeState == FILE_PIPE_CONNECTED_STATE)
    {
       if (Fcb->OtherSide)
@@ -482,6 +502,7 @@ NpfsClose(
           */
          KeSetEvent(&Fcb->OtherSide->Event, IO_NO_INCREMENT, FALSE);
       }
+
 #ifndef FIN_WORKAROUND_READCLOSE
       Fcb->PipeState = 0;
 #endif
@@ -507,9 +528,11 @@ NpfsClose(
             ExFreePool(Fcb->OtherSide->Data);
         ExFreePool(Fcb->OtherSide);
       }
+
       RemoveEntryList(&Fcb->FcbListEntry);
       if (Fcb->Data)
          ExFreePool(Fcb->Data);
+
       ExFreePool(Fcb);
    }
 #endif
index fcffbd9..a65c11e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: fsctrl.c,v 1.15 2004/05/05 18:30:16 navaraf Exp $
+/* $Id: fsctrl.c,v 1.16 2004/10/11 12:37:04 ekohl Exp $
  *
  * COPYRIGHT:  See COPYING in the top level directory
  * PROJECT:    ReactOS kernel
 static NTSTATUS
 NpfsConnectPipe(PNPFS_FCB Fcb)
 {
-   PNPFS_PIPE Pipe;
-   PLIST_ENTRY current_entry;
-   PNPFS_FCB ClientFcb;
-   NTSTATUS Status;
-
-   DPRINT("NpfsConnectPipe()\n");
-
-   if (Fcb->PipeState == FILE_PIPE_CONNECTED_STATE)
-   {
-     KeResetEvent(&Fcb->ConnectEvent);
-     return STATUS_PIPE_CONNECTED;
-   }
-
-   if (Fcb->PipeState == FILE_PIPE_CLOSING_STATE)
-     return STATUS_PIPE_CLOSING;
-
-   DPRINT("Waiting for connection...\n");
-
-   Pipe = Fcb->Pipe;
-
-   /* search for a listening client fcb */
-
-   KeLockMutex(&Pipe->FcbListLock);
-   current_entry = Pipe->ClientFcbListHead.Flink;
-   while (current_entry != &Pipe->ClientFcbListHead)
-     {
-       ClientFcb = CONTAINING_RECORD(current_entry,
-                                     NPFS_FCB,
-                                     FcbListEntry);
-       
-       if (ClientFcb->PipeState == FILE_PIPE_LISTENING_STATE)
-         {
-            break;
-         }
-       
-       current_entry = current_entry->Flink;
-     }
-   
-   if (current_entry != &Pipe->ClientFcbListHead)
-     {
-       /* found a listening client fcb */
-       DPRINT("Listening client fcb found -- connecting\n");
-
-       /* connect client and server fcb's */
-       Fcb->OtherSide = ClientFcb;
-       ClientFcb->OtherSide = Fcb;
-
-       /* set connected state */
-       Fcb->PipeState = FILE_PIPE_CONNECTED_STATE;
-       ClientFcb->PipeState = FILE_PIPE_CONNECTED_STATE;
-
-        KeUnlockMutex(&Pipe->FcbListLock);
-
-       /* FIXME: create and initialize data queues */
-
-       /* signal client's connect event */
-        DPRINT("Setting the ConnectEvent for %x\n", ClientFcb);
-       KeSetEvent(&ClientFcb->ConnectEvent, IO_NO_INCREMENT, FALSE);
-
-       Status = STATUS_PIPE_CONNECTED;
-     }
-   else
-     {
-       /* no listening client fcb found */
-       DPRINT("No listening client fcb found -- waiting for client\n");
-
-        KeUnlockMutex(&Pipe->FcbListLock);
-
-       Fcb->PipeState = FILE_PIPE_LISTENING_STATE;
-
-       Status = KeWaitForSingleObject(&Fcb->ConnectEvent,
-                                      UserRequest,
-                                      KernelMode,
-                                      FALSE,
-                                      NULL);
-
-       if (NT_SUCCESS(Status))
-         {
-           Status = STATUS_PIPE_CONNECTED;
-           Fcb->PipeState = FILE_PIPE_CONNECTED_STATE;
-         }
-
-       DPRINT("Finished waiting! Status: %x\n", Status);
-     }
-
-   DPRINT("Client Fcb: %p\n", Fcb->OtherSide);
-
-   return Status;
+  PNPFS_PIPE Pipe;
+  PLIST_ENTRY current_entry;
+  PNPFS_FCB ClientFcb;
+  NTSTATUS Status;
+
+  DPRINT("NpfsConnectPipe()\n");
+
+  if (Fcb->PipeState == FILE_PIPE_CONNECTED_STATE)
+    {
+      KeResetEvent(&Fcb->ConnectEvent);
+      return STATUS_PIPE_CONNECTED;
+    }
+
+  if (Fcb->PipeState == FILE_PIPE_CLOSING_STATE)
+    return STATUS_PIPE_CLOSING;
+
+  DPRINT("Waiting for connection...\n");
+
+  Pipe = Fcb->Pipe;
+
+  /* search for a listening client fcb */
+  KeLockMutex(&Pipe->FcbListLock);
+
+  current_entry = Pipe->ClientFcbListHead.Flink;
+  while (current_entry != &Pipe->ClientFcbListHead)
+    {
+      ClientFcb = CONTAINING_RECORD(current_entry,
+                                   NPFS_FCB,
+                                   FcbListEntry);
+
+      if (ClientFcb->PipeState == FILE_PIPE_LISTENING_STATE)
+       {
+         /* found a listening client fcb */
+         DPRINT("Listening client fcb found -- connecting\n");
+
+         /* connect client and server fcb's */
+         Fcb->OtherSide = ClientFcb;
+         ClientFcb->OtherSide = Fcb;
+
+         /* set connected state */
+         Fcb->PipeState = FILE_PIPE_CONNECTED_STATE;
+         ClientFcb->PipeState = FILE_PIPE_CONNECTED_STATE;
+
+         KeUnlockMutex(&Pipe->FcbListLock);
+
+         /* FIXME: create and initialize data queues */
+
+         /* signal client's connect event */
+         DPRINT("Setting the ConnectEvent for %x\n", ClientFcb);
+         KeSetEvent(&ClientFcb->ConnectEvent, IO_NO_INCREMENT, FALSE);
+
+         return STATUS_PIPE_CONNECTED;
+       }
+
+      current_entry = current_entry->Flink;
+    }
+
+  KeUnlockMutex(&Pipe->FcbListLock);
+
+  /* no listening client fcb found */
+  DPRINT("No listening client fcb found -- waiting for client\n");
+
+  Fcb->PipeState = FILE_PIPE_LISTENING_STATE;
+
+  Status = KeWaitForSingleObject(&Fcb->ConnectEvent,
+                                UserRequest,
+                                KernelMode,
+                                FALSE,
+                                NULL);
+  if (!NT_SUCCESS(Status))
+    {
+      DPRINT("KeWaitForSingleObject() failed (Status %lx)\n", Status);
+      return Status;
+    }
+
+  Fcb->PipeState = FILE_PIPE_CONNECTED_STATE;
+
+  DPRINT("Client Fcb: %p\n", Fcb->OtherSide);
+
+  return STATUS_PIPE_CONNECTED;
 }
 
 
@@ -174,31 +166,28 @@ NpfsWaitPipe(PIRP Irp,
                                    FcbListEntry);
 
       if (ServerFcb->PipeState == FILE_PIPE_LISTENING_STATE)
-       break;
+       {
+         /* found a listening server fcb */
+         DPRINT("Listening server fcb found -- connecting\n");
+
+         return STATUS_SUCCESS;
+       }
 
       current_entry = current_entry->Flink;
     }
 
-  if (current_entry != &Pipe->ServerFcbListHead)
-    {
-      /* found a listening server fcb */
-      DPRINT("Listening server fcb found -- connecting\n");
+  /* no listening server fcb found -- wait for one */
+  Fcb->PipeState = FILE_PIPE_LISTENING_STATE;
 
-      Status = STATUS_SUCCESS;
-    }
-  else
-    {
-      /* no listening server fcb found -- wait for one */
-      Fcb->PipeState = FILE_PIPE_LISTENING_STATE;
-
-      Status = KeWaitForSingleObject(&Fcb->ConnectEvent,
-                                    UserRequest,
-                                    KernelMode,
-                                    FALSE,
-                                    &WaitPipe->Timeout);
-    }
+  Status = KeWaitForSingleObject(&Fcb->ConnectEvent,
+                                UserRequest,
+                                KernelMode,
+                                FALSE,
+                                &WaitPipe->Timeout);
 
-  return(Status);
+  DPRINT("KeWaitForSingleObject() returned (Status %lx)\n", Status);
+
+  return Status;
 }