bc92fe33a51258d170db6cb5d92e9a11f0c70bd9
[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, 2015 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 static bool
317 win32_modify_privilege(const wchar_t *privilege, bool enable)
318 {
319         HANDLE hToken;
320         LUID luid;
321         TOKEN_PRIVILEGES newState;
322         bool ret = FALSE;
323
324         if (!OpenProcessToken(GetCurrentProcess(),
325                               TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
326                               &hToken))
327                 goto out;
328
329         if (!LookupPrivilegeValue(NULL, privilege, &luid))
330                 goto out_close_handle;
331
332         newState.PrivilegeCount = 1;
333         newState.Privileges[0].Luid = luid;
334         newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
335         SetLastError(ERROR_SUCCESS);
336         ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
337         if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
338                 ret = FALSE;
339 out_close_handle:
340         CloseHandle(hToken);
341 out:
342         return ret;
343 }
344
345 static bool
346 win32_modify_capture_privileges(bool enable)
347 {
348         return win32_modify_privilege(SE_BACKUP_NAME, enable)
349             && win32_modify_privilege(SE_SECURITY_NAME, enable);
350 }
351
352 static bool
353 win32_modify_apply_privileges(bool enable)
354 {
355         return win32_modify_privilege(SE_RESTORE_NAME, enable)
356             && win32_modify_privilege(SE_SECURITY_NAME, enable)
357             && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
358 }
359
360 static void
361 win32_release_capture_and_apply_privileges(void)
362 {
363         win32_modify_capture_privileges(false);
364         win32_modify_apply_privileges(false);
365 }
366
367 /* Pointers to dynamically loaded functions  */
368
369 /* ntdll.dll  */
370
371 NTSTATUS (WINAPI *func_NtCreateFile)(PHANDLE FileHandle,
372                                      ACCESS_MASK DesiredAccess,
373                                      POBJECT_ATTRIBUTES ObjectAttributes,
374                                      PIO_STATUS_BLOCK IoStatusBlock,
375                                      PLARGE_INTEGER AllocationSize,
376                                      ULONG FileAttributes,
377                                      ULONG ShareAccess,
378                                      ULONG CreateDisposition,
379                                      ULONG CreateOptions,
380                                      PVOID EaBuffer,
381                                      ULONG EaLength);
382
383 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
384                                     ACCESS_MASK DesiredAccess,
385                                     POBJECT_ATTRIBUTES ObjectAttributes,
386                                     PIO_STATUS_BLOCK IoStatusBlock,
387                                     ULONG ShareAccess,
388                                     ULONG OpenOptions);
389
390 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
391                                     HANDLE Event,
392                                     PIO_APC_ROUTINE ApcRoutine,
393                                     PVOID ApcContext,
394                                     PIO_STATUS_BLOCK IoStatusBlock,
395                                     PVOID Buffer,
396                                     ULONG Length,
397                                     PLARGE_INTEGER ByteOffset,
398                                     PULONG Key);
399
400 NTSTATUS (WINAPI *func_NtWriteFile) (HANDLE FileHandle,
401                                      HANDLE Event,
402                                      PIO_APC_ROUTINE ApcRoutine,
403                                      PVOID ApcContext,
404                                      PIO_STATUS_BLOCK IoStatusBlock,
405                                      PVOID Buffer,
406                                      ULONG Length,
407                                      PLARGE_INTEGER ByteOffset,
408                                      PULONG Key);
409
410 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
411                                                PIO_STATUS_BLOCK IoStatusBlock,
412                                                PVOID FileInformation,
413                                                ULONG Length,
414                                                FILE_INFORMATION_CLASS FileInformationClass);
415
416 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
417                                               SECURITY_INFORMATION SecurityInformation,
418                                               PSECURITY_DESCRIPTOR SecurityDescriptor,
419                                               ULONG Length,
420                                               PULONG LengthNeeded);
421
422 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
423                                               HANDLE Event,
424                                               PIO_APC_ROUTINE ApcRoutine,
425                                               PVOID ApcContext,
426                                               PIO_STATUS_BLOCK IoStatusBlock,
427                                               PVOID FileInformation,
428                                               ULONG Length,
429                                               FILE_INFORMATION_CLASS FileInformationClass,
430                                               BOOLEAN ReturnSingleEntry,
431                                               PUNICODE_STRING FileName,
432                                               BOOLEAN RestartScan);
433
434 NTSTATUS (WINAPI *func_NtQueryVolumeInformationFile) (HANDLE FileHandle,
435                                                       PIO_STATUS_BLOCK IoStatusBlock,
436                                                       PVOID FsInformation,
437                                                       ULONG Length,
438                                                       FS_INFORMATION_CLASS FsInformationClass);
439
440 NTSTATUS (WINAPI *func_NtSetInformationFile)(HANDLE FileHandle,
441                                              PIO_STATUS_BLOCK IoStatusBlock,
442                                              PVOID FileInformation,
443                                              ULONG Length,
444                                              FILE_INFORMATION_CLASS FileInformationClass);
445
446 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
447                                             SECURITY_INFORMATION SecurityInformation,
448                                             PSECURITY_DESCRIPTOR SecurityDescriptor);
449
450 NTSTATUS (WINAPI *func_NtFsControlFile) (HANDLE FileHandle,
451                                          HANDLE Event,
452                                          PIO_APC_ROUTINE ApcRoutine,
453                                          PVOID ApcContext,
454                                          PIO_STATUS_BLOCK IoStatusBlock,
455                                          ULONG FsControlCode,
456                                          PVOID InputBuffer,
457                                          ULONG InputBufferLength,
458                                          PVOID OutputBuffer,
459                                          ULONG OutputBufferLength);
460
461 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
462
463 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
464
465 BOOLEAN (WINAPI *func_RtlDosPathNameToNtPathName_U)
466                   (IN PCWSTR DosName,
467                    OUT PUNICODE_STRING NtName,
468                    OUT PCWSTR *PartName,
469                    OUT PRTL_RELATIVE_NAME_U RelativeName);
470
471 NTSTATUS (WINAPI *func_RtlDosPathNameToNtPathName_U_WithStatus)
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_RtlCreateSystemVolumeInformationFolder)
478                 (PCUNICODE_STRING VolumeRootPath);
479
480 static bool acquired_privileges = false;
481
482 struct dll_sym {
483         void **func_ptr;
484         const char *name;
485         bool required;
486 };
487
488 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
489
490 #define for_each_sym(sym, spec) \
491         for ((sym) = (spec)->syms; (sym)->name; (sym)++)
492
493 struct dll_spec {
494         const wchar_t *name;
495         HMODULE handle;
496         const struct dll_sym syms[];
497 };
498
499 struct dll_spec ntdll_spec = {
500         .name = L"ntdll.dll",
501         .syms = {
502                 DLL_SYM(NtCreateFile, true),
503                 DLL_SYM(NtOpenFile, true),
504                 DLL_SYM(NtReadFile, true),
505                 DLL_SYM(NtWriteFile, true),
506                 DLL_SYM(NtQueryInformationFile, true),
507                 DLL_SYM(NtQuerySecurityObject, true),
508                 DLL_SYM(NtQueryDirectoryFile, true),
509                 DLL_SYM(NtQueryVolumeInformationFile, true),
510                 DLL_SYM(NtSetInformationFile, true),
511                 DLL_SYM(NtSetSecurityObject, true),
512                 DLL_SYM(NtFsControlFile, true),
513                 DLL_SYM(NtClose, true),
514                 DLL_SYM(RtlNtStatusToDosError, true),
515                 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
516                 DLL_SYM(RtlDosPathNameToNtPathName_U, true),
517                 DLL_SYM(RtlDosPathNameToNtPathName_U_WithStatus, false), /* Not present on XP  */
518                 {NULL, NULL},
519         },
520 };
521
522 static int
523 init_dll(struct dll_spec *spec)
524 {
525         const struct dll_sym *sym;
526         void *addr;
527
528         if (!spec->handle)
529                 spec->handle = LoadLibrary(spec->name);
530         if (!spec->handle) {
531                 for_each_sym(sym, spec) {
532                         if (sym->required) {
533                                 ERROR("%ls could not be loaded!", spec->name);
534                                 return WIMLIB_ERR_UNSUPPORTED;
535                         }
536                 }
537                 return 0;
538         }
539         for_each_sym(sym, spec) {
540                 addr = (void *)GetProcAddress(spec->handle, sym->name);
541                 if (addr) {
542                         *(sym->func_ptr) = addr;
543                 } else if (sym->required) {
544                         ERROR("Can't find %s in %ls", sym->name, spec->name);
545                         return WIMLIB_ERR_UNSUPPORTED;
546                 }
547         }
548         return 0;
549 }
550
551 static void
552 cleanup_dll(struct dll_spec *spec)
553 {
554         const struct dll_sym *sym;
555
556         if (spec->handle) {
557                 FreeLibrary(spec->handle);
558                 spec->handle = NULL;
559
560                 for_each_sym(sym, spec)
561                         *(sym->func_ptr) = NULL;
562         }
563 }
564
565 /* One-time initialization for Windows capture/apply code.  */
566 int
567 win32_global_init(int init_flags)
568 {
569         int ret;
570
571         /* Try to acquire useful privileges.  */
572         if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
573                 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
574                 if (!win32_modify_capture_privileges(true))
575                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
576                                 goto out_drop_privs;
577                 if (!win32_modify_apply_privileges(true))
578                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
579                                 goto out_drop_privs;
580                 acquired_privileges = true;
581         }
582
583         ret = init_dll(&ntdll_spec);
584         if (ret)
585                 goto out_drop_privs;
586
587         return 0;
588
589 out_drop_privs:
590         win32_release_capture_and_apply_privileges();
591         return ret;
592 }
593
594 void
595 win32_global_cleanup(void)
596 {
597         if (acquired_privileges)
598                 win32_release_capture_and_apply_privileges();
599
600         cleanup_dll(&ntdll_spec);
601 }
602
603 /*
604  * Translates a Win32-namespace path into an NT-namespace path.
605  *
606  * On success, returns 0.  The NT-namespace path will be stored in the
607  * UNICODE_STRING structure pointed to by nt_path.  nt_path->Buffer will be set
608  * to a new buffer that must later be freed with HeapFree().  (Really
609  * RtlHeapFree(), but HeapFree() seems to be the same thing.)
610  *
611  * On failure, returns WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_PARAM.
612  */
613 int
614 win32_path_to_nt_path(const wchar_t *win32_path, UNICODE_STRING *nt_path)
615 {
616         NTSTATUS status;
617
618         if (func_RtlDosPathNameToNtPathName_U_WithStatus) {
619                 status = (*func_RtlDosPathNameToNtPathName_U_WithStatus)(win32_path,
620                                                                          nt_path,
621                                                                          NULL, NULL);
622         } else {
623                 if ((*func_RtlDosPathNameToNtPathName_U)(win32_path, nt_path,
624                                                          NULL, NULL))
625                         status = STATUS_SUCCESS;
626                 else
627                         status = STATUS_NO_MEMORY;
628         }
629
630         if (likely(NT_SUCCESS(status)))
631                 return 0;
632
633         if (status == STATUS_NO_MEMORY)
634                 return WIMLIB_ERR_NOMEM;
635
636         ERROR("\"%ls\": invalid path name (status=0x%08"PRIx32")",
637               win32_path, (u32)status);
638         return WIMLIB_ERR_INVALID_PARAM;
639 }
640
641 int
642 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
643 {
644         tchar *file_abspath;
645
646         file_abspath = realpath(file_path, NULL);
647         if (!file_abspath)
648                 return WIMLIB_ERR_NOMEM;
649
650         if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
651                 ERROR("\"%ls\": Path format not recognized", file_abspath);
652                 FREE(file_abspath);
653                 return WIMLIB_ERR_UNSUPPORTED;
654         }
655
656         wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
657         FREE(file_abspath);
658         return 0;
659 }
660
661 static void
662 windows_msg(u32 code, const wchar_t *format, va_list va,
663             bool is_ntstatus, bool is_error)
664 {
665         wchar_t _buf[STACK_MAX / 8];
666         wchar_t *buf = _buf;
667         size_t buflen = ARRAY_LEN(_buf);
668         size_t ret;
669         size_t n;
670
671 retry:
672         n = vsnwprintf(buf, buflen, format, va);
673
674         if (n >= buflen)
675                 goto realloc;
676
677         n += snwprintf(&buf[n], buflen - n,
678                        (is_ntstatus ?
679                         L" (status=%08"PRIx32"): " :
680                         L" (err=%"PRIu32"): "),
681                        code);
682
683         if (n >= buflen)
684                 goto realloc;
685
686         ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
687                             NULL,
688                             is_ntstatus ? (*func_RtlNtStatusToDosError)(code) : code,
689                             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
690                             &buf[n],
691                             buflen - n,
692                             NULL);
693         n += ret;
694
695         if (n >= buflen || (ret == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
696                 goto realloc;
697
698         if (buf[n - 1] == L'\n')
699                 buf[--n] = L'\0';
700         if (buf[n - 1] == L'\r')
701                 buf[--n] = L'\0';
702         if (buf[n - 1] == L'.')
703                 buf[--n] = L'\0';
704
705         if (is_error)
706                 ERROR("%ls", buf);
707         else
708                 WARNING("%ls", buf);
709         if (buf != _buf)
710                 FREE(buf);
711         return;
712
713 realloc:
714         if (buf != _buf)
715                 FREE(buf);
716         buflen *= 2;
717         buf = MALLOC(buflen * sizeof(buf[0]));
718         if (buf)
719                 goto retry;
720         ERROR("Ran out of memory while building error message!!!");
721 }
722
723 void
724 win32_warning(DWORD err, const wchar_t *format, ...)
725 {
726         va_list va;
727
728         va_start(va, format);
729         windows_msg(err, format, va, false, false);
730         va_end(va);
731 }
732
733 void
734 win32_error(DWORD err, const wchar_t *format, ...)
735 {
736         va_list va;
737
738         va_start(va, format);
739         windows_msg(err, format, va, false, true);
740         va_end(va);
741 }
742
743 void
744 winnt_warning(NTSTATUS status, const wchar_t *format, ...)
745 {
746         va_list va;
747
748         va_start(va, format);
749         windows_msg(status, format, va, true, false);
750         va_end(va);
751 }
752
753 void
754 winnt_error(NTSTATUS status, const wchar_t *format, ...)
755 {
756         va_list va;
757
758         va_start(va, format);
759         windows_msg(status, format, va, true, true);
760         va_end(va);
761 }
762
763 #endif /* __WIN32__ */