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