[MOUNTMGR]
[reactos.git] / reactos / drivers / filters / mountmgr / mountmgr.c
index d955453..0a20d4e 100644 (file)
  *                   Alex Ionescu (alex.ionescu@reactos.org)
  */
 
-/* INCLUDES *****************************************************************/
-
 #include "mntmgr.h"
 
 #define NDEBUG
 #include <debug.h>
 
+#if defined(ALLOC_PRAGMA)
+#pragma alloc_text(INIT, MountmgrReadNoAutoMount)
+#pragma alloc_text(INIT, DriverEntry)
+#endif
+
 /* FIXME */
 GUID MountedDevicesGuid = {0x53F5630D, 0xB6BF, 0x11D0, {0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B}};
 
@@ -134,8 +137,8 @@ CreateNewDriveLetterName(OUT PUNICODE_STRING DriveLetter,
     NTSTATUS Status = STATUS_UNSUCCESSFUL;
 
     /* Allocate a big enough buffer to contain the symbolic link */
-    DriveLetter->MaximumLength = sizeof(DosDevices.Buffer) + 3 * sizeof(WCHAR);
-    DriveLetter->Buffer = AllocatePool(sizeof(DosDevices.Buffer) + 3 * sizeof(WCHAR));
+    DriveLetter->MaximumLength = DosDevices.Length + 3 * sizeof(WCHAR);
+    DriveLetter->Buffer = AllocatePool(DriveLetter->MaximumLength);
     if (!DriveLetter->Buffer)
     {
         return STATUS_INSUFFICIENT_RESOURCES;
@@ -145,9 +148,9 @@ CreateNewDriveLetterName(OUT PUNICODE_STRING DriveLetter,
     RtlCopyUnicodeString(DriveLetter, &DosDevices);
 
     /* Update string to reflect real contents */
-    DriveLetter->Length = sizeof(DosDevices.Buffer) + 2 * sizeof(WCHAR);
-    DriveLetter->Buffer[(sizeof(DosDevices.Buffer) + 2 * sizeof(WCHAR)) / sizeof (WCHAR)] = UNICODE_NULL;
-    DriveLetter->Buffer[(sizeof(DosDevices.Buffer) + sizeof(WCHAR)) / sizeof (WCHAR)] = L':';
+    DriveLetter->Length = DosDevices.Length + 2 * sizeof(WCHAR);
+    DriveLetter->Buffer[DosDevices.Length / sizeof(WCHAR) + 2] = UNICODE_NULL;
+    DriveLetter->Buffer[DosDevices.Length / sizeof(WCHAR) + 1] = L':';
 
     /* If caller wants a no drive entry */
     if (Letter == (UCHAR)-1)
@@ -160,7 +163,7 @@ CreateNewDriveLetterName(OUT PUNICODE_STRING DriveLetter,
     else if (Letter)
     {
         /* Use the letter given by the caller */
-        DriveLetter->Buffer[sizeof(DosDevices.Buffer) / sizeof(WCHAR)] = (WCHAR)Letter;
+        DriveLetter->Buffer[DosDevices.Length / sizeof(WCHAR)] = (WCHAR)Letter;
         Status = GlobalCreateSymbolicLink(DriveLetter, DeviceName);
         if (NT_SUCCESS(Status))
         {
@@ -189,16 +192,18 @@ CreateNewDriveLetterName(OUT PUNICODE_STRING DriveLetter,
     /* Try to affect a letter (up to Z, ofc) until it's possible */
     for (; Letter <= 'Z'; Letter++)
     {
-        DriveLetter->Buffer[sizeof(DosDevices.Buffer) / sizeof(WCHAR)] = (WCHAR)Letter;
+        DriveLetter->Buffer[DosDevices.Length / sizeof(WCHAR)] = (WCHAR)Letter;
         Status = GlobalCreateSymbolicLink(DriveLetter, DeviceName);
         if (NT_SUCCESS(Status))
         {
+            DPRINT("Assigned drive %c: to %wZ\n", Letter, DeviceName);
             return Status;
         }
     }
 
     /* We failed to allocate a letter */
     FreePool(DriveLetter->Buffer);
+    DPRINT("Failed to create a drive letter for %wZ\n", DeviceName);
     return Status;
 }
 
@@ -560,7 +565,7 @@ QueryDeviceInformation(IN PUNICODE_STRING SymbolicName,
 
             /* Query unique ID */
             KeInitializeEvent(&Event, NotificationEvent, FALSE);
-            Irp = IoBuildDeviceIoControlRequest(IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
+            Irp = IoBuildDeviceIoControlRequest(IOCTL_MOUNTDEV_QUERY_UNIQUE_ID,
                                                 DeviceObject,
                                                 NULL,
                                                 0,
@@ -912,6 +917,7 @@ MountMgrUnload(IN struct _DRIVER_OBJECT *DriverObject)
 /*
  * @implemented
  */
+INIT_SECTION
 BOOLEAN
 MountmgrReadNoAutoMount(IN PUNICODE_STRING RegistryPath)
 {
@@ -1072,7 +1078,7 @@ MountMgrMountedDeviceArrival(IN PDEVICE_EXTENSION DeviceExtension,
     {
         CurrentDevice = CONTAINING_RECORD(NextEntry, DEVICE_INFORMATION, DeviceListEntry);
 
-        if (RtlEqualUnicodeString(&(DeviceInformation->DeviceName), &TargetDeviceName, TRUE))
+        if (RtlEqualUnicodeString(&(CurrentDevice->DeviceName), &TargetDeviceName, TRUE))
         {
             break;
         }
@@ -1331,9 +1337,9 @@ MountMgrMountedDeviceArrival(IN PDEVICE_EXTENSION DeviceExtension,
         DeviceInformation->SuggestedDriveLetter = 0;
     }
     /* Else, it's time to set up one */
-    else if (!DeviceExtension->NoAutoMount && !DeviceInformation->Removable &&
-             DeviceExtension->AutomaticDriveLetter && HasGptDriveLetter &&
-             DeviceInformation->SuggestedDriveLetter &&
+    else if ((DeviceExtension->NoAutoMount || DeviceInformation->Removable) &&
+             DeviceExtension->AutomaticDriveLetter &&
+             (HasGptDriveLetter || DeviceInformation->SuggestedDriveLetter) &&
              !HasNoDriveLetterEntry(UniqueId))
     {
         /* Create a new drive letter */
@@ -1805,6 +1811,7 @@ MountMgrShutdown(IN PDEVICE_OBJECT DeviceObject,
 
 /* FUNCTIONS ****************************************************************/
 
+INIT_SECTION
 NTSTATUS
 NTAPI
 DriverEntry(IN PDRIVER_OBJECT DriverObject,
@@ -1878,7 +1885,7 @@ DriverEntry(IN PDRIVER_OBJECT DriverObject,
                                             &MountedDevicesGuid,
                                             DriverObject,
                                             MountMgrMountedDeviceNotification,
-                                            DeviceObject,
+                                            DeviceExtension,
                                             &(DeviceExtension->NotificationEntry));
 
     if (!NT_SUCCESS(Status))