]> wimlib.net Git - wimlib/blob - src/win32_common.c
Use LGPLv3+ for src/*.c
[wimlib] / src / win32_common.c
1 /*
2  * win32_common.c - Windows code common to applying and capturing images.
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef __WIN32__
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <errno.h>
29
30 #include "wimlib/win32_common.h"
31 #include "wimlib/error.h"
32 #include "wimlib/util.h"
33
34 static int
35 win32_error_to_errno(DWORD err_code)
36 {
37         /* This mapping is that used in Cygwin.
38          * Some of these choices are arbitrary. */
39         switch (err_code) {
40         case ERROR_ACCESS_DENIED:
41                 return EACCES;
42         case ERROR_ACTIVE_CONNECTIONS:
43                 return EAGAIN;
44         case ERROR_ALREADY_EXISTS:
45                 return EEXIST;
46         case ERROR_BAD_DEVICE:
47                 return ENODEV;
48         case ERROR_BAD_EXE_FORMAT:
49                 return ENOEXEC;
50         case ERROR_BAD_NETPATH:
51                 return ENOENT;
52         case ERROR_BAD_NET_NAME:
53                 return ENOENT;
54         case ERROR_BAD_NET_RESP:
55                 return ENOSYS;
56         case ERROR_BAD_PATHNAME:
57                 return ENOENT;
58         case ERROR_BAD_PIPE:
59                 return EINVAL;
60         case ERROR_BAD_UNIT:
61                 return ENODEV;
62         case ERROR_BAD_USERNAME:
63                 return EINVAL;
64         case ERROR_BEGINNING_OF_MEDIA:
65                 return EIO;
66         case ERROR_BROKEN_PIPE:
67                 return EPIPE;
68         case ERROR_BUSY:
69                 return EBUSY;
70         case ERROR_BUS_RESET:
71                 return EIO;
72         case ERROR_CALL_NOT_IMPLEMENTED:
73                 return ENOSYS;
74         case ERROR_CANNOT_MAKE:
75                 return EPERM;
76         case ERROR_CHILD_NOT_COMPLETE:
77                 return EBUSY;
78         case ERROR_COMMITMENT_LIMIT:
79                 return EAGAIN;
80         case ERROR_CRC:
81                 return EIO;
82         case ERROR_DEVICE_DOOR_OPEN:
83                 return EIO;
84         case ERROR_DEVICE_IN_USE:
85                 return EAGAIN;
86         case ERROR_DEVICE_REQUIRES_CLEANING:
87                 return EIO;
88         case ERROR_DIRECTORY:
89                 return ENOTDIR;
90         case ERROR_DIR_NOT_EMPTY:
91                 return ENOTEMPTY;
92         case ERROR_DISK_CORRUPT:
93                 return EIO;
94         case ERROR_DISK_FULL:
95                 return ENOSPC;
96 #ifdef ENOTUNIQ
97         case ERROR_DUP_NAME:
98                 return ENOTUNIQ;
99 #endif
100         case ERROR_EAS_DIDNT_FIT:
101                 return ENOSPC;
102 #ifdef ENOTSUP
103         case ERROR_EAS_NOT_SUPPORTED:
104                 return ENOTSUP;
105 #endif
106         case ERROR_EA_LIST_INCONSISTENT:
107                 return EINVAL;
108         case ERROR_EA_TABLE_FULL:
109                 return ENOSPC;
110         case ERROR_END_OF_MEDIA:
111                 return ENOSPC;
112         case ERROR_EOM_OVERFLOW:
113                 return EIO;
114         case ERROR_EXE_MACHINE_TYPE_MISMATCH:
115                 return ENOEXEC;
116         case ERROR_EXE_MARKED_INVALID:
117                 return ENOEXEC;
118         case ERROR_FILEMARK_DETECTED:
119                 return EIO;
120         case ERROR_FILENAME_EXCED_RANGE:
121                 return ENAMETOOLONG;
122         case ERROR_FILE_CORRUPT:
123                 return EEXIST;
124         case ERROR_FILE_EXISTS:
125                 return EEXIST;
126         case ERROR_FILE_INVALID:
127                 return ENXIO;
128         case ERROR_FILE_NOT_FOUND:
129                 return ENOENT;
130         case ERROR_HANDLE_DISK_FULL:
131                 return ENOSPC;
132 #ifdef ENODATA
133         case ERROR_HANDLE_EOF:
134                 return ENODATA;
135 #endif
136         case ERROR_INVALID_ADDRESS:
137                 return EINVAL;
138         case ERROR_INVALID_AT_INTERRUPT_TIME:
139                 return EINTR;
140         case ERROR_INVALID_BLOCK_LENGTH:
141                 return EIO;
142         case ERROR_INVALID_DATA:
143                 return EINVAL;
144         case ERROR_INVALID_DRIVE:
145                 return ENODEV;
146         case ERROR_INVALID_EA_NAME:
147                 return EINVAL;
148         case ERROR_INVALID_EXE_SIGNATURE:
149                 return ENOEXEC;
150 #ifdef EBADRQC
151         case ERROR_INVALID_FUNCTION:
152                 return EBADRQC;
153 #endif
154         case ERROR_INVALID_HANDLE:
155                 return EBADF;
156         case ERROR_INVALID_NAME:
157                 return ENOENT;
158         case ERROR_INVALID_PARAMETER:
159                 return EINVAL;
160         case ERROR_INVALID_SIGNAL_NUMBER:
161                 return EINVAL;
162         case ERROR_IOPL_NOT_ENABLED:
163                 return ENOEXEC;
164         case ERROR_IO_DEVICE:
165                 return EIO;
166         case ERROR_IO_INCOMPLETE:
167                 return EAGAIN;
168         case ERROR_IO_PENDING:
169                 return EAGAIN;
170         case ERROR_LOCK_VIOLATION:
171                 return EBUSY;
172         case ERROR_MAX_THRDS_REACHED:
173                 return EAGAIN;
174         case ERROR_META_EXPANSION_TOO_LONG:
175                 return EINVAL;
176         case ERROR_MOD_NOT_FOUND:
177                 return ENOENT;
178 #ifdef EMSGSIZE
179         case ERROR_MORE_DATA:
180                 return EMSGSIZE;
181 #endif
182         case ERROR_NEGATIVE_SEEK:
183                 return EINVAL;
184         case ERROR_NETNAME_DELETED:
185                 return ENOENT;
186         case ERROR_NOACCESS:
187                 return EFAULT;
188         case ERROR_NONE_MAPPED:
189                 return EINVAL;
190         case ERROR_NONPAGED_SYSTEM_RESOURCES:
191                 return EAGAIN;
192 #ifdef ENOLINK
193         case ERROR_NOT_CONNECTED:
194                 return ENOLINK;
195 #endif
196         case ERROR_NOT_ENOUGH_MEMORY:
197                 return ENOMEM;
198         case ERROR_NOT_OWNER:
199                 return EPERM;
200 #ifdef ENOMEDIUM
201         case ERROR_NOT_READY:
202                 return ENOMEDIUM;
203 #endif
204         case ERROR_NOT_SAME_DEVICE:
205                 return EXDEV;
206         case ERROR_NOT_SUPPORTED:
207                 return ENOSYS;
208         case ERROR_NO_DATA:
209                 return EPIPE;
210         case ERROR_NO_DATA_DETECTED:
211                 return EIO;
212 #ifdef ENOMEDIUM
213         case ERROR_NO_MEDIA_IN_DRIVE:
214                 return ENOMEDIUM;
215 #endif
216 #ifdef ENMFILE
217         case ERROR_NO_MORE_FILES:
218                 return ENMFILE;
219 #endif
220 #ifdef ENMFILE
221         case ERROR_NO_MORE_ITEMS:
222                 return ENMFILE;
223 #endif
224         case ERROR_NO_MORE_SEARCH_HANDLES:
225                 return ENFILE;
226         case ERROR_NO_PROC_SLOTS:
227                 return EAGAIN;
228         case ERROR_NO_SIGNAL_SENT:
229                 return EIO;
230         case ERROR_NO_SYSTEM_RESOURCES:
231                 return EFBIG;
232         case ERROR_NO_TOKEN:
233                 return EINVAL;
234         case ERROR_OPEN_FAILED:
235                 return EIO;
236         case ERROR_OPEN_FILES:
237                 return EAGAIN;
238         case ERROR_OUTOFMEMORY:
239                 return ENOMEM;
240         case ERROR_PAGED_SYSTEM_RESOURCES:
241                 return EAGAIN;
242         case ERROR_PAGEFILE_QUOTA:
243                 return EAGAIN;
244         case ERROR_PATH_NOT_FOUND:
245                 return ENOENT;
246         case ERROR_PIPE_BUSY:
247                 return EBUSY;
248         case ERROR_PIPE_CONNECTED:
249                 return EBUSY;
250 #ifdef ECOMM
251         case ERROR_PIPE_LISTENING:
252                 return ECOMM;
253         case ERROR_PIPE_NOT_CONNECTED:
254                 return ECOMM;
255 #endif
256         case ERROR_POSSIBLE_DEADLOCK:
257                 return EDEADLOCK;
258         case ERROR_PRIVILEGE_NOT_HELD:
259                 return EPERM;
260         case ERROR_PROCESS_ABORTED:
261                 return EFAULT;
262         case ERROR_PROC_NOT_FOUND:
263                 return ESRCH;
264 #ifdef ENONET
265         case ERROR_REM_NOT_LIST:
266                 return ENONET;
267 #endif
268         case ERROR_SECTOR_NOT_FOUND:
269                 return EINVAL;
270         case ERROR_SEEK:
271                 return EINVAL;
272         case ERROR_SETMARK_DETECTED:
273                 return EIO;
274         case ERROR_SHARING_BUFFER_EXCEEDED:
275                 return ENOLCK;
276         case ERROR_SHARING_VIOLATION:
277                 return EBUSY;
278         case ERROR_SIGNAL_PENDING:
279                 return EBUSY;
280         case ERROR_SIGNAL_REFUSED:
281                 return EIO;
282 #ifdef ELIBBAD
283         case ERROR_SXS_CANT_GEN_ACTCTX:
284                 return ELIBBAD;
285 #endif
286         case ERROR_THREAD_1_INACTIVE:
287                 return EINVAL;
288         case ERROR_TOO_MANY_LINKS:
289                 return EMLINK;
290         case ERROR_TOO_MANY_OPEN_FILES:
291                 return EMFILE;
292         case ERROR_WAIT_NO_CHILDREN:
293                 return ECHILD;
294         case ERROR_WORKING_SET_QUOTA:
295                 return EAGAIN;
296         case ERROR_WRITE_PROTECT:
297                 return EROFS;
298         default:
299                 return -1;
300         }
301 }
302
303 void
304 set_errno_from_win32_error(DWORD err)
305 {
306         errno = win32_error_to_errno(err);
307 }
308
309 void
310 set_errno_from_GetLastError(void)
311 {
312         set_errno_from_win32_error(GetLastError());
313 }
314
315 void
316 set_errno_from_nt_status(NTSTATUS status)
317 {
318         set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
319 }
320
321 static bool
322 win32_modify_privilege(const wchar_t *privilege, bool enable)
323 {
324         HANDLE hToken;
325         LUID luid;
326         TOKEN_PRIVILEGES newState;
327         bool ret = FALSE;
328
329         if (!OpenProcessToken(GetCurrentProcess(),
330                               TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
331                               &hToken))
332                 goto out;
333
334         if (!LookupPrivilegeValue(NULL, privilege, &luid))
335                 goto out_close_handle;
336
337         newState.PrivilegeCount = 1;
338         newState.Privileges[0].Luid = luid;
339         newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
340         SetLastError(ERROR_SUCCESS);
341         ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
342         if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
343                 ret = FALSE;
344 out_close_handle:
345         CloseHandle(hToken);
346 out:
347         return ret;
348 }
349
350 static bool
351 win32_modify_capture_privileges(bool enable)
352 {
353         return win32_modify_privilege(SE_BACKUP_NAME, enable)
354             && win32_modify_privilege(SE_SECURITY_NAME, enable);
355 }
356
357 static bool
358 win32_modify_apply_privileges(bool enable)
359 {
360         return win32_modify_privilege(SE_RESTORE_NAME, enable)
361             && win32_modify_privilege(SE_SECURITY_NAME, enable)
362             && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
363 }
364
365 static void
366 win32_release_capture_and_apply_privileges(void)
367 {
368         win32_modify_capture_privileges(false);
369         win32_modify_apply_privileges(false);
370 }
371
372 /* Pointers to dynamically loaded functions  */
373
374 /* ntdll.dll  */
375
376 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
377                                      ACCESS_MASK DesiredAccess,
378                                      POBJECT_ATTRIBUTES ObjectAttributes,
379                                      PIO_STATUS_BLOCK IoStatusBlock,
380                                      PLARGE_INTEGER AllocationSize,
381                                      ULONG FileAttributes,
382                                      ULONG ShareAccess,
383                                      ULONG CreateDisposition,
384                                      ULONG CreateOptions,
385                                      PVOID EaBuffer,
386                                      ULONG EaLength);
387
388 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
389                                     ACCESS_MASK DesiredAccess,
390                                     POBJECT_ATTRIBUTES ObjectAttributes,
391                                     PIO_STATUS_BLOCK IoStatusBlock,
392                                     ULONG ShareAccess,
393                                     ULONG OpenOptions);
394
395 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
396                                     HANDLE Event,
397                                     PIO_APC_ROUTINE ApcRoutine,
398                                     PVOID ApcContext,
399                                     PIO_STATUS_BLOCK IoStatusBlock,
400                                     PVOID Buffer,
401                                     ULONG Length,
402                                     PLARGE_INTEGER ByteOffset,
403                                     PULONG Key);
404
405 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
406                                      HANDLE Event,
407                                      PIO_APC_ROUTINE ApcRoutine,
408                                      PVOID ApcContext,
409                                      PIO_STATUS_BLOCK IoStatusBlock,
410                                      PVOID Buffer,
411                                      ULONG Length,
412                                      PLARGE_INTEGER ByteOffset,
413                                      PULONG Key);
414
415 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
416                                                PIO_STATUS_BLOCK IoStatusBlock,
417                                                PVOID FileInformation,
418                                                ULONG Length,
419                                                FILE_INFORMATION_CLASS FileInformationClass);
420
421 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
422                                               SECURITY_INFORMATION SecurityInformation,
423                                               PSECURITY_DESCRIPTOR SecurityDescriptor,
424                                               ULONG Length,
425                                               PULONG LengthNeeded);
426
427 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
428                                               HANDLE Event,
429                                               PIO_APC_ROUTINE ApcRoutine,
430                                               PVOID ApcContext,
431                                               PIO_STATUS_BLOCK IoStatusBlock,
432                                               PVOID FileInformation,
433                                               ULONG Length,
434                                               FILE_INFORMATION_CLASS FileInformationClass,
435                                               BOOLEAN ReturnSingleEntry,
436                                               PUNICODE_STRING FileName,
437                                               BOOLEAN RestartScan);
438
439 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
440                                                       PIO_STATUS_BLOCK IoStatusBlock,
441                                                       PVOID FsInformation,
442                                                       ULONG Length,
443                                                       FS_INFORMATION_CLASS FsInformationClass);
444
445 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
446                                              PIO_STATUS_BLOCK IoStatusBlock,
447                                              PVOID FileInformation,
448                                              ULONG Length,
449                                              FILE_INFORMATION_CLASS FileInformationClass);
450
451 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
452                                             SECURITY_INFORMATION SecurityInformation,
453                                             PSECURITY_DESCRIPTOR SecurityDescriptor);
454
455 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
456                                          HANDLE Event,
457                                          PIO_APC_ROUTINE ApcRoutine,
458                                          PVOID ApcContext,
459                                          PIO_STATUS_BLOCK IoStatusBlock,
460                                          ULONG FsControlCode,
461                                          PVOID InputBuffer,
462                                          ULONG InputBufferLength,
463                                          PVOID OutputBuffer,
464                                          ULONG OutputBufferLength);
465
466 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
467
468 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
469
470 BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
471                   (IN PCWSTR DosName,
472                    OUT PUNICODE_STRING NtName,
473                    OUT PCWSTR *PartName,
474                    OUT PRTL_RELATIVE_NAME_U RelativeName);
475
476 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
477                 (IN PCWSTR DosName,
478                  OUT PUNICODE_STRING NtName,
479                  OUT PCWSTR *PartName,
480                  OUT PRTL_RELATIVE_NAME_U RelativeName);
481
482 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
483                 (PCUNICODE_STRING VolumeRootPath);
484
485 static bool acquired_privileges = false;
486
487 struct dll_sym {
488         void **func_ptr;
489         const char *name;
490         bool required;
491 };
492
493 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
494
495 #define for_each_sym(sym, spec) \
496         for ((sym) = (spec)->syms; (sym)->name; (sym)++)
497
498 struct dll_spec {
499         const wchar_t *name;
500         HMODULE handle;
501         const struct dll_sym syms[];
502 };
503
504 struct dll_spec ntdll_spec = {
505         .name = L"ntdll.dll",
506         .syms = {
507                 DLL_SYM(NtCreateFile, true),
508                 DLL_SYM(NtOpenFile, true),
509                 DLL_SYM(NtReadFile, true),
510                 DLL_SYM(NtWriteFile, true),
511                 DLL_SYM(NtQueryInformationFile, true),
512                 DLL_SYM(NtQuerySecurityObject, true),
513                 DLL_SYM(NtQueryDirectoryFile, true),
514                 DLL_SYM(NtQueryVolumeInformationFile, true),
515                 DLL_SYM(NtSetInformationFile, true),
516                 DLL_SYM(NtSetSecurityObject, true),
517                 DLL_SYM(NtFsControlFile, true),
518                 DLL_SYM(NtClose, true),
519                 DLL_SYM(RtlNtStatusToDosError, true),
520                 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
521                 DLL_SYM(RtlDosPathNameToNtPathName_U, true),
522                 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP  */
523                 {NULL, NULL},
524         },
525 };
526
527 static int
528 init_dll(struct dll_spec *spec)
529 {
530         const struct dll_sym *sym;
531         void *addr;
532
533         if (!spec->handle)
534                 spec->handle = LoadLibrary(spec->name);
535         if (!spec->handle) {
536                 for_each_sym(sym, spec) {
537                         if (sym->required) {
538                                 ERROR("%ls could not be loaded!", spec->name);
539                                 return WIMLIB_ERR_UNSUPPORTED;
540                         }
541                 }
542                 return 0;
543         }
544         for_each_sym(sym, spec) {
545                 addr = (void *)GetProcAddress(spec->handle, sym->name);
546                 if (addr) {
547                         *(sym->func_ptr) = addr;
548                 } else if (sym->required) {
549                         ERROR("Can't find %s in %ls", sym->name, spec->name);
550                         return WIMLIB_ERR_UNSUPPORTED;
551                 }
552         }
553         return 0;
554 }
555
556 static void
557 cleanup_dll(struct dll_spec *spec)
558 {
559         const struct dll_sym *sym;
560
561         if (spec->handle) {
562                 FreeLibrary(spec->handle);
563                 spec->handle = NULL;
564
565                 for_each_sym(sym, spec)
566                         *(sym->func_ptr) = NULL;
567         }
568 }
569
570 /* One-time initialization for Windows capture/apply code.  */
571 int
572 win32_global_init(int init_flags)
573 {
574         int ret;
575
576         /* Try to acquire useful privileges.  */
577         if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
578                 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
579                 if (!win32_modify_capture_privileges(true))
580                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
581                                 goto out_drop_privs;
582                 if (!win32_modify_apply_privileges(true))
583                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
584                                 goto out_drop_privs;
585                 acquired_privileges = true;
586         }
587
588         ret = init_dll(&ntdll_spec);
589         if (ret)
590                 goto out_drop_privs;
591
592         return 0;
593
594 out_drop_privs:
595         win32_release_capture_and_apply_privileges();
596         return ret;
597 }
598
599 void
600 win32_global_cleanup(void)
601 {
602         if (acquired_privileges)
603                 win32_release_capture_and_apply_privileges();
604
605         cleanup_dll(&ntdll_spec);
606 }
607
608 /*
609  * Translates a Win32-namespace path into an NT-namespace path.
610  *
611  * On success, returns 0.  The NT-namespace path will be stored in the
612  * UNICODE_STRING structure pointed to by nt_path.  nt_path->Buffer will be set
613  * to a new buffer that must later be freed with HeapFree().  (Really
614  * RtlHeapFree(), but HeapFree() seems to be the same thing.)
615  *
616  * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
617  */
618 int
619 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
620 {
621         NTSTATUS status;
622
623         if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
624                 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
625                                                                          nt_path,
626                                                                          NULL, NULL);
627         } else {
628                 if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
629                                                          NULL, NULL))
630                         status = STATUS_SUCCESS;
631                 else
632                         status = STATUS_NO_MEMORY;
633         }
634
635         if (likely(NT_SUCCESS(status)))
636                 return 0;
637
638         if (status == STATUS_NO_MEMORY)
639                 return WIMLIB_ERR_NOMEM;
640
641         ERROR("\"%ls\": invalid path name (status=0x%08"PRIx32")",
642               win32_path, (u32)status);
643         return WIMLIB_ERR_INVALID_PARAM;
644 }
645
646 int
647 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
648 {
649         tchar *file_abspath;
650
651         file_abspath = realpath(file_path, NULL);
652         if (!file_abspath)
653                 return WIMLIB_ERR_NOMEM;
654
655         if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
656                 ERROR("\"%ls\": Path format not recognized", file_abspath);
657                 FREE(file_abspath);
658                 return WIMLIB_ERR_UNSUPPORTED;
659         }
660
661         wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
662         FREE(file_abspath);
663         return 0;
664 }
665
666
667 #endif /* __WIN32__ */