]> wimlib.net Git - wimlib/blobdiff - src/win32_common.c
Use LGPLv3+ for src/*.c
[wimlib] / src / win32_common.c
index 7f0521e43ab1f1c46f0c131ff2f5840694988848..a1c3378e92ccb04e8384f83098f027c30315f545 100644 (file)
@@ -5,20 +5,18 @@
 /*
  * Copyright (C) 2013, 2014 Eric Biggers
  *
- * This file is part of wimlib, a library for working with WIM files.
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
  *
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
  */
 
 #ifdef __WIN32__
@@ -469,6 +467,12 @@ NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
 
 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
 
+BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
+                 (IN PCWSTR DosName,
+                  OUT PUNICODE_STRING NtName,
+                  OUT PCWSTR *PartName,
+                  OUT PRTL_RELATIVE_NAME_U RelativeName);
+
 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
                (IN PCWSTR DosName,
                 OUT PUNICODE_STRING NtName,
@@ -478,20 +482,8 @@ NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
                (PCUNICODE_STRING VolumeRootPath);
 
-static OSVERSIONINFO windows_version_info = {
-       .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
-};
-
 static bool acquired_privileges = false;
 
-bool
-windows_version_is_at_least(unsigned major, unsigned minor)
-{
-       return windows_version_info.dwMajorVersion > major ||
-               (windows_version_info.dwMajorVersion == major &&
-                windows_version_info.dwMinorVersion >= minor);
-}
-
 struct dll_sym {
        void **func_ptr;
        const char *name;
@@ -526,7 +518,8 @@ struct dll_spec ntdll_spec = {
                DLL_SYM(NtClose, true),
                DLL_SYM(RtlNtStatusToDosError, true),
                DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
-               DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, true),
+               DLL_SYM(RtlDosPathNameToNtPathName_U, true),
+               DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP  */
                {NULL, NULL},
        },
 };
@@ -592,9 +585,6 @@ win32_global_init(int init_flags)
                acquired_privileges = true;
        }
 
-       /* Get Windows version information.  */
-       GetVersionEx(&windows_version_info);
-
        ret = init_dll(&ntdll_spec);
        if (ret)
                goto out_drop_privs;
@@ -615,4 +605,63 @@ win32_global_cleanup(void)
        cleanup_dll(&ntdll_spec);
 }
 
+/*
+ * Translates a Win32-namespace path into an NT-namespace path.
+ *
+ * On success, returns 0.  The NT-namespace path will be stored in the
+ * UNICODE_STRING structure pointed to by nt_path.  nt_path->Buffer will be set
+ * to a new buffer that must later be freed with HeapFree().  (Really
+ * RtlHeapFree(), but HeapFree() seems to be the same thing.)
+ *
+ * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
+ */
+int
+win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
+{
+       NTSTATUS status;
+
+       if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
+               status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
+                                                                        nt_path,
+                                                                        NULL, NULL);
+       } else {
+               if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
+                                                        NULL, NULL))
+                       status = STATUS_SUCCESS;
+               else
+                       status = STATUS_NO_MEMORY;
+       }
+
+       if (likely(NT_SUCCESS(status)))
+               return 0;
+
+       if (status == STATUS_NO_MEMORY)
+               return WIMLIB_ERR_NOMEM;
+
+       ERROR("\"%ls\": invalid path name (status=0x%08"PRIx32")",
+             win32_path, (u32)status);
+       return WIMLIB_ERR_INVALID_PARAM;
+}
+
+int
+win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
+{
+       tchar *file_abspath;
+
+       file_abspath = realpath(file_path, NULL);
+       if (!file_abspath)
+               return WIMLIB_ERR_NOMEM;
+
+       if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
+               ERROR("\"%ls\": Path format not recognized", file_abspath);
+               FREE(file_abspath);
+               return WIMLIB_ERR_UNSUPPORTED;
+       }
+
+       wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
+       FREE(file_abspath);
+       return 0;
+}
+
+
 #endif /* __WIN32__ */