[KERNEL32]
authorPierre Schweitzer <pierre@reactos.org>
Tue, 26 Sep 2017 06:18:07 +0000 (06:18 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Tue, 26 Sep 2017 06:18:07 +0000 (06:18 +0000)
W2K3ify GetVolumeInformationA().
Also fix a regression brought by GetVolumeInformationW() rewrite (yay, winetests!).

svn path=/trunk/; revision=75969

reactos/dll/win32/kernel32/client/file/volume.c

index 3986f37..847caf1 100644 (file)
@@ -85,124 +85,113 @@ GetVolumeInformationA(IN LPCSTR lpRootPathName,
                       OUT LPSTR lpFileSystemNameBuffer OPTIONAL,
                       IN DWORD nFileSystemNameSize)
 {
-  UNICODE_STRING FileSystemNameU;
-  UNICODE_STRING VolumeNameU = { 0, 0, NULL };
-  ANSI_STRING VolumeName;
-  ANSI_STRING FileSystemName;
-  PWCHAR RootPathNameW;
-  BOOL Result;
-
-  if (!(RootPathNameW = FilenameA2W(lpRootPathName, FALSE)))
-     return FALSE;
-
-  if (lpVolumeNameBuffer)
-    {
-      VolumeNameU.MaximumLength = (USHORT)nVolumeNameSize * sizeof(WCHAR);
-      VolumeNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
-                                           0,
-                                           VolumeNameU.MaximumLength);
-      if (VolumeNameU.Buffer == NULL)
-      {
-          goto FailNoMem;
-      }
-    }
-
-  if (lpFileSystemNameBuffer)
-    {
-      FileSystemNameU.Length = 0;
-      FileSystemNameU.MaximumLength = (USHORT)nFileSystemNameSize * sizeof(WCHAR);
-      FileSystemNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
-                                               0,
-                                               FileSystemNameU.MaximumLength);
-      if (FileSystemNameU.Buffer == NULL)
-      {
-          if (VolumeNameU.Buffer != NULL)
-          {
-              RtlFreeHeap(RtlGetProcessHeap(),
-                          0,
-                          VolumeNameU.Buffer);
-          }
-
-FailNoMem:
-          SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-          return FALSE;
-      }
-    }
-
-  Result = GetVolumeInformationW (RootPathNameW,
-                                 lpVolumeNameBuffer ? VolumeNameU.Buffer : NULL,
-                                 nVolumeNameSize,
-                                 lpVolumeSerialNumber,
-                                 lpMaximumComponentLength,
-                                 lpFileSystemFlags,
-                                 lpFileSystemNameBuffer ? FileSystemNameU.Buffer : NULL,
-                                 nFileSystemNameSize);
-
-  if (Result)
-    {
-      if (lpVolumeNameBuffer)
+    BOOL Ret;
+    NTSTATUS Status;
+    PUNICODE_STRING RootPathNameU;
+    ANSI_STRING VolumeName, FileSystemName;
+    UNICODE_STRING VolumeNameU, FileSystemNameU;
+
+    /* If no root path provided, default to \ */
+    if (lpRootPathName == NULL)
+    {
+        lpRootPathName = "\\";
+    }
+
+    /* Convert root path to unicode */
+    RootPathNameU = Basep8BitStringToStaticUnicodeString(lpRootPathName);
+    if (RootPathNameU == NULL)
+    {
+        return FALSE;
+    }
+
+    /* Init all our STRINGS (U/A) */
+    VolumeNameU.Buffer = NULL;
+    VolumeNameU.MaximumLength = 0;
+    FileSystemNameU.Buffer = NULL;
+    FileSystemNameU.MaximumLength = 0;
+
+    VolumeName.Buffer = lpVolumeNameBuffer;
+    VolumeName.MaximumLength = nVolumeNameSize + 1;
+    FileSystemName.Buffer = lpFileSystemNameBuffer;
+    FileSystemName.MaximumLength = nFileSystemNameSize + 1;
+
+    /* Assume failure for now */
+    Ret = FALSE;
+
+    /* If caller wants volume name, allocate a buffer to receive it */
+    if (lpVolumeNameBuffer != NULL)
+    {
+        VolumeNameU.MaximumLength = sizeof(WCHAR) * (nVolumeNameSize + 1);
+        VolumeNameU.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0,
+                                                VolumeNameU.MaximumLength);
+        if (VolumeNameU.Buffer == NULL)
+        {
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            goto CleanAndQuit;
+        }
+    }
+
+    /* If caller wants file system name, allocate a buffer to receive it */
+    if (lpFileSystemNameBuffer != NULL)
+    {
+        FileSystemNameU.MaximumLength = sizeof(WCHAR) * (nVolumeNameSize + 1);
+        FileSystemNameU.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0,
+                                                    FileSystemNameU.MaximumLength);
+        if (FileSystemNameU.Buffer == NULL)
         {
-          VolumeNameU.Length = wcslen(VolumeNameU.Buffer) * sizeof(WCHAR);
-         VolumeName.Length = 0;
-         VolumeName.MaximumLength = (USHORT)nVolumeNameSize;
-         VolumeName.Buffer = lpVolumeNameBuffer;
-       }
-
-      if (lpFileSystemNameBuffer)
-       {
-         FileSystemNameU.Length = wcslen(FileSystemNameU.Buffer) * sizeof(WCHAR);
-         FileSystemName.Length = 0;
-         FileSystemName.MaximumLength = (USHORT)nFileSystemNameSize;
-         FileSystemName.Buffer = lpFileSystemNameBuffer;
-       }
-
-      /* convert unicode strings to ansi (or oem) */
-      if (bIsFileApiAnsi)
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            goto CleanAndQuit;
+        }
+    }
+
+    /* Call W */
+    Ret = GetVolumeInformationW(RootPathNameU->Buffer,  VolumeNameU.Buffer,
+                                nVolumeNameSize, lpVolumeSerialNumber,
+                                lpMaximumComponentLength, lpFileSystemFlags,
+                                FileSystemNameU.Buffer, nFileSystemNameSize);
+    /* If it succeed, convert back to ANSI */
+    if (Ret)
+    {
+        if (lpVolumeNameBuffer != NULL)
         {
-         if (lpVolumeNameBuffer)
-           {
-             RtlUnicodeStringToAnsiString (&VolumeName,
-                                           &VolumeNameU,
-                                           FALSE);
-           }
-         if (lpFileSystemNameBuffer)
-           {
-             RtlUnicodeStringToAnsiString (&FileSystemName,
-                                           &FileSystemNameU,
-                                           FALSE);
-           }
-       }
-      else
+            RtlInitUnicodeString(&VolumeNameU, VolumeNameU.Buffer);
+            Status = RtlUnicodeStringToAnsiString(&VolumeName, &VolumeNameU, FALSE);
+            if (!NT_SUCCESS(Status))
+            {
+                BaseSetLastNTError(Status);
+                Ret = FALSE;
+
+                goto CleanAndQuit;
+            }
+        }
+
+        if (lpFileSystemNameBuffer != NULL)
         {
-         if (lpVolumeNameBuffer)
-           {
-             RtlUnicodeStringToOemString (&VolumeName,
-                                          &VolumeNameU,
-                                          FALSE);
-           }
-          if (lpFileSystemNameBuffer)
-           {
-             RtlUnicodeStringToOemString (&FileSystemName,
-                                          &FileSystemNameU,
-                                          FALSE);
-           }
-       }
+            RtlInitUnicodeString(&FileSystemNameU, FileSystemNameU.Buffer);
+            Status = RtlUnicodeStringToAnsiString(&FileSystemName, &FileSystemNameU, FALSE);
+            if (!NT_SUCCESS(Status))
+            {
+                BaseSetLastNTError(Status);
+                Ret = FALSE;
+
+                goto CleanAndQuit;
+            }
+        }
     }
 
-  if (lpVolumeNameBuffer)
+    /* Clean and quit */
+CleanAndQuit:
+    if (VolumeNameU.Buffer != NULL)
     {
-      RtlFreeHeap (RtlGetProcessHeap (),
-                  0,
-                  VolumeNameU.Buffer);
+        RtlFreeHeap(RtlGetProcessHeap(), 0, VolumeNameU.Buffer);
     }
-  if (lpFileSystemNameBuffer)
+
+    if (FileSystemNameU.Buffer != NULL)
     {
-      RtlFreeHeap (RtlGetProcessHeap (),
-                  0,
-                  FileSystemNameU.Buffer);
+        RtlFreeHeap(RtlGetProcessHeap(), 0, FileSystemNameU.Buffer);
     }
 
-  return Result;
+    return Ret;
 }
 
 /*