]> wimlib.net Git - wimlib/blob - src/win32_common.c
9f5c72f1647b742f9e93f7f806a557a89974b0f4
[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 part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef __WIN32__
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <errno.h>
31
32 #include <winternl.h>
33
34 #include "wimlib/win32_common.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/error.h"
37 #include "wimlib/util.h"
38
39 static int
40 win32_error_to_errno(DWORD err_code)
41 {
42         /* This mapping is that used in Cygwin.
43          * Some of these choices are arbitrary. */
44         switch (err_code) {
45         case ERROR_ACCESS_DENIED:
46                 return EACCES;
47         case ERROR_ACTIVE_CONNECTIONS:
48                 return EAGAIN;
49         case ERROR_ALREADY_EXISTS:
50                 return EEXIST;
51         case ERROR_BAD_DEVICE:
52                 return ENODEV;
53         case ERROR_BAD_EXE_FORMAT:
54                 return ENOEXEC;
55         case ERROR_BAD_NETPATH:
56                 return ENOENT;
57         case ERROR_BAD_NET_NAME:
58                 return ENOENT;
59         case ERROR_BAD_NET_RESP:
60                 return ENOSYS;
61         case ERROR_BAD_PATHNAME:
62                 return ENOENT;
63         case ERROR_BAD_PIPE:
64                 return EINVAL;
65         case ERROR_BAD_UNIT:
66                 return ENODEV;
67         case ERROR_BAD_USERNAME:
68                 return EINVAL;
69         case ERROR_BEGINNING_OF_MEDIA:
70                 return EIO;
71         case ERROR_BROKEN_PIPE:
72                 return EPIPE;
73         case ERROR_BUSY:
74                 return EBUSY;
75         case ERROR_BUS_RESET:
76                 return EIO;
77         case ERROR_CALL_NOT_IMPLEMENTED:
78                 return ENOSYS;
79         case ERROR_CANNOT_MAKE:
80                 return EPERM;
81         case ERROR_CHILD_NOT_COMPLETE:
82                 return EBUSY;
83         case ERROR_COMMITMENT_LIMIT:
84                 return EAGAIN;
85         case ERROR_CRC:
86                 return EIO;
87         case ERROR_DEVICE_DOOR_OPEN:
88                 return EIO;
89         case ERROR_DEVICE_IN_USE:
90                 return EAGAIN;
91         case ERROR_DEVICE_REQUIRES_CLEANING:
92                 return EIO;
93         case ERROR_DIRECTORY:
94                 return ENOTDIR;
95         case ERROR_DIR_NOT_EMPTY:
96                 return ENOTEMPTY;
97         case ERROR_DISK_CORRUPT:
98                 return EIO;
99         case ERROR_DISK_FULL:
100                 return ENOSPC;
101 #ifdef ENOTUNIQ
102         case ERROR_DUP_NAME:
103                 return ENOTUNIQ;
104 #endif
105         case ERROR_EAS_DIDNT_FIT:
106                 return ENOSPC;
107 #ifdef ENOTSUP
108         case ERROR_EAS_NOT_SUPPORTED:
109                 return ENOTSUP;
110 #endif
111         case ERROR_EA_LIST_INCONSISTENT:
112                 return EINVAL;
113         case ERROR_EA_TABLE_FULL:
114                 return ENOSPC;
115         case ERROR_END_OF_MEDIA:
116                 return ENOSPC;
117         case ERROR_EOM_OVERFLOW:
118                 return EIO;
119         case ERROR_EXE_MACHINE_TYPE_MISMATCH:
120                 return ENOEXEC;
121         case ERROR_EXE_MARKED_INVALID:
122                 return ENOEXEC;
123         case ERROR_FILEMARK_DETECTED:
124                 return EIO;
125         case ERROR_FILENAME_EXCED_RANGE:
126                 return ENAMETOOLONG;
127         case ERROR_FILE_CORRUPT:
128                 return EEXIST;
129         case ERROR_FILE_EXISTS:
130                 return EEXIST;
131         case ERROR_FILE_INVALID:
132                 return ENXIO;
133         case ERROR_FILE_NOT_FOUND:
134                 return ENOENT;
135         case ERROR_HANDLE_DISK_FULL:
136                 return ENOSPC;
137 #ifdef ENODATA
138         case ERROR_HANDLE_EOF:
139                 return ENODATA;
140 #endif
141         case ERROR_INVALID_ADDRESS:
142                 return EINVAL;
143         case ERROR_INVALID_AT_INTERRUPT_TIME:
144                 return EINTR;
145         case ERROR_INVALID_BLOCK_LENGTH:
146                 return EIO;
147         case ERROR_INVALID_DATA:
148                 return EINVAL;
149         case ERROR_INVALID_DRIVE:
150                 return ENODEV;
151         case ERROR_INVALID_EA_NAME:
152                 return EINVAL;
153         case ERROR_INVALID_EXE_SIGNATURE:
154                 return ENOEXEC;
155 #ifdef EBADRQC
156         case ERROR_INVALID_FUNCTION:
157                 return EBADRQC;
158 #endif
159         case ERROR_INVALID_HANDLE:
160                 return EBADF;
161         case ERROR_INVALID_NAME:
162                 return ENOENT;
163         case ERROR_INVALID_PARAMETER:
164                 return EINVAL;
165         case ERROR_INVALID_SIGNAL_NUMBER:
166                 return EINVAL;
167         case ERROR_IOPL_NOT_ENABLED:
168                 return ENOEXEC;
169         case ERROR_IO_DEVICE:
170                 return EIO;
171         case ERROR_IO_INCOMPLETE:
172                 return EAGAIN;
173         case ERROR_IO_PENDING:
174                 return EAGAIN;
175         case ERROR_LOCK_VIOLATION:
176                 return EBUSY;
177         case ERROR_MAX_THRDS_REACHED:
178                 return EAGAIN;
179         case ERROR_META_EXPANSION_TOO_LONG:
180                 return EINVAL;
181         case ERROR_MOD_NOT_FOUND:
182                 return ENOENT;
183 #ifdef EMSGSIZE
184         case ERROR_MORE_DATA:
185                 return EMSGSIZE;
186 #endif
187         case ERROR_NEGATIVE_SEEK:
188                 return EINVAL;
189         case ERROR_NETNAME_DELETED:
190                 return ENOENT;
191         case ERROR_NOACCESS:
192                 return EFAULT;
193         case ERROR_NONE_MAPPED:
194                 return EINVAL;
195         case ERROR_NONPAGED_SYSTEM_RESOURCES:
196                 return EAGAIN;
197 #ifdef ENOLINK
198         case ERROR_NOT_CONNECTED:
199                 return ENOLINK;
200 #endif
201         case ERROR_NOT_ENOUGH_MEMORY:
202                 return ENOMEM;
203         case ERROR_NOT_OWNER:
204                 return EPERM;
205 #ifdef ENOMEDIUM
206         case ERROR_NOT_READY:
207                 return ENOMEDIUM;
208 #endif
209         case ERROR_NOT_SAME_DEVICE:
210                 return EXDEV;
211         case ERROR_NOT_SUPPORTED:
212                 return ENOSYS;
213         case ERROR_NO_DATA:
214                 return EPIPE;
215         case ERROR_NO_DATA_DETECTED:
216                 return EIO;
217 #ifdef ENOMEDIUM
218         case ERROR_NO_MEDIA_IN_DRIVE:
219                 return ENOMEDIUM;
220 #endif
221 #ifdef ENMFILE
222         case ERROR_NO_MORE_FILES:
223                 return ENMFILE;
224 #endif
225 #ifdef ENMFILE
226         case ERROR_NO_MORE_ITEMS:
227                 return ENMFILE;
228 #endif
229         case ERROR_NO_MORE_SEARCH_HANDLES:
230                 return ENFILE;
231         case ERROR_NO_PROC_SLOTS:
232                 return EAGAIN;
233         case ERROR_NO_SIGNAL_SENT:
234                 return EIO;
235         case ERROR_NO_SYSTEM_RESOURCES:
236                 return EFBIG;
237         case ERROR_NO_TOKEN:
238                 return EINVAL;
239         case ERROR_OPEN_FAILED:
240                 return EIO;
241         case ERROR_OPEN_FILES:
242                 return EAGAIN;
243         case ERROR_OUTOFMEMORY:
244                 return ENOMEM;
245         case ERROR_PAGED_SYSTEM_RESOURCES:
246                 return EAGAIN;
247         case ERROR_PAGEFILE_QUOTA:
248                 return EAGAIN;
249         case ERROR_PATH_NOT_FOUND:
250                 return ENOENT;
251         case ERROR_PIPE_BUSY:
252                 return EBUSY;
253         case ERROR_PIPE_CONNECTED:
254                 return EBUSY;
255 #ifdef ECOMM
256         case ERROR_PIPE_LISTENING:
257                 return ECOMM;
258         case ERROR_PIPE_NOT_CONNECTED:
259                 return ECOMM;
260 #endif
261         case ERROR_POSSIBLE_DEADLOCK:
262                 return EDEADLOCK;
263         case ERROR_PRIVILEGE_NOT_HELD:
264                 return EPERM;
265         case ERROR_PROCESS_ABORTED:
266                 return EFAULT;
267         case ERROR_PROC_NOT_FOUND:
268                 return ESRCH;
269 #ifdef ENONET
270         case ERROR_REM_NOT_LIST:
271                 return ENONET;
272 #endif
273         case ERROR_SECTOR_NOT_FOUND:
274                 return EINVAL;
275         case ERROR_SEEK:
276                 return EINVAL;
277         case ERROR_SETMARK_DETECTED:
278                 return EIO;
279         case ERROR_SHARING_BUFFER_EXCEEDED:
280                 return ENOLCK;
281         case ERROR_SHARING_VIOLATION:
282                 return EBUSY;
283         case ERROR_SIGNAL_PENDING:
284                 return EBUSY;
285         case ERROR_SIGNAL_REFUSED:
286                 return EIO;
287 #ifdef ELIBBAD
288         case ERROR_SXS_CANT_GEN_ACTCTX:
289                 return ELIBBAD;
290 #endif
291         case ERROR_THREAD_1_INACTIVE:
292                 return EINVAL;
293         case ERROR_TOO_MANY_LINKS:
294                 return EMLINK;
295         case ERROR_TOO_MANY_OPEN_FILES:
296                 return EMFILE;
297         case ERROR_WAIT_NO_CHILDREN:
298                 return ECHILD;
299         case ERROR_WORKING_SET_QUOTA:
300                 return EAGAIN;
301         case ERROR_WRITE_PROTECT:
302                 return EROFS;
303         default:
304                 return -1;
305         }
306 }
307
308
309 void
310 set_errno_from_win32_error(DWORD err)
311 {
312         errno = win32_error_to_errno(err);
313 }
314
315 void
316 set_errno_from_GetLastError(void)
317 {
318         set_errno_from_win32_error(GetLastError());
319 }
320
321 void
322 set_errno_from_nt_status(NTSTATUS status)
323 {
324         set_errno_from_win32_error((*func_RtlNtStatusToDosError)(status));
325 }
326
327 /* Given a Windows-style path, return the number of characters of the prefix
328  * that specify the path to the root directory of a drive, or return 0 if the
329  * drive is relative (or at least on the current drive, in the case of
330  * absolute-but-not-really-absolute paths like \Windows\System32) */
331 static size_t
332 win32_path_drive_spec_len(const wchar_t *path)
333 {
334         size_t n = 0;
335
336         if (!wcsncmp(path, L"\\\\?\\", 4)) {
337                 /* \\?\-prefixed path.  Check for following drive letter and
338                  * path separator. */
339                 if (path[4] != L'\0' && path[5] == L':' &&
340                     is_any_path_separator(path[6]))
341                         n = 7;
342         } else {
343                 /* Not a \\?\-prefixed path.  Check for an initial drive letter
344                  * and path separator. */
345                 if (path[0] != L'\0' && path[1] == L':' &&
346                     is_any_path_separator(path[2]))
347                         n = 3;
348         }
349         /* Include any additional path separators.*/
350         if (n > 0)
351                 while (is_any_path_separator(path[n]))
352                         n++;
353         return n;
354 }
355
356 bool
357 win32_path_is_root_of_drive(const wchar_t *path)
358 {
359         size_t drive_spec_len;
360         wchar_t full_path[32768];
361         DWORD ret;
362
363         ret = GetFullPathName(path, ARRAY_LEN(full_path), full_path, NULL);
364         if (ret > 0 && ret < ARRAY_LEN(full_path))
365                 path = full_path;
366
367         /* Explicit drive letter and path separator? */
368         drive_spec_len = win32_path_drive_spec_len(path);
369         if (drive_spec_len > 0 && path[drive_spec_len] == L'\0')
370                 return true;
371
372         /* All path separators? */
373         for (const wchar_t *p = path; *p != L'\0'; p++)
374                 if (!is_any_path_separator(*p))
375                         return false;
376         return true;
377 }
378
379
380 /* Given a path, which may not yet exist, get a set of flags that describe the
381  * features of the volume the path is on. */
382 int
383 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret,
384                     bool *supports_SetFileShortName_ret)
385 {
386         wchar_t *volume;
387         BOOL bret;
388         DWORD vol_flags;
389         size_t drive_spec_len;
390         wchar_t filesystem_name[MAX_PATH + 1];
391
392         if (supports_SetFileShortName_ret)
393                 *supports_SetFileShortName_ret = false;
394
395         drive_spec_len = win32_path_drive_spec_len(path);
396
397         if (drive_spec_len == 0)
398                 if (path[0] != L'\0' && path[1] == L':') /* Drive-relative path? */
399                         drive_spec_len = 2;
400
401         if (drive_spec_len == 0) {
402                 /* Path does not start with a drive letter; use the volume of
403                  * the current working directory. */
404                 volume = NULL;
405         } else {
406                 /* Path starts with a drive letter (or \\?\ followed by a drive
407                  * letter); use it. */
408                 volume = alloca((drive_spec_len + 2) * sizeof(wchar_t));
409                 wmemcpy(volume, path, drive_spec_len);
410                 /* Add trailing backslash in case this was a drive-relative
411                  * path. */
412                 volume[drive_spec_len] = L'\\';
413                 volume[drive_spec_len + 1] = L'\0';
414         }
415         bret = GetVolumeInformation(
416                         volume,                         /* lpRootPathName */
417                         NULL,                           /* lpVolumeNameBuffer */
418                         0,                              /* nVolumeNameSize */
419                         NULL,                           /* lpVolumeSerialNumber */
420                         NULL,                           /* lpMaximumComponentLength */
421                         &vol_flags,                     /* lpFileSystemFlags */
422                         filesystem_name,                /* lpFileSystemNameBuffer */
423                         ARRAY_LEN(filesystem_name));    /* nFileSystemNameSize */
424         if (!bret) {
425                 set_errno_from_GetLastError();
426                 WARNING_WITH_ERRNO("Failed to get volume information for "
427                                    "path \"%ls\"", path);
428                 vol_flags = 0xffffffff;
429                 goto out;
430         }
431
432         if (wcsstr(filesystem_name, L"NTFS")) {
433                 /* FILE_SUPPORTS_HARD_LINKS is only supported on Windows 7 and later.
434                  * Force it on anyway if filesystem is NTFS.  */
435                 vol_flags |= FILE_SUPPORTS_HARD_LINKS;
436
437                 if (supports_SetFileShortName_ret)
438                         *supports_SetFileShortName_ret = true;
439         }
440
441 out:
442         DEBUG("using vol_flags = %x", vol_flags);
443         *vol_flags_ret = vol_flags;
444         return 0;
445 }
446
447 static bool
448 win32_modify_privilege(const wchar_t *privilege, bool enable)
449 {
450         HANDLE hToken;
451         LUID luid;
452         TOKEN_PRIVILEGES newState;
453         bool ret = FALSE;
454
455         if (!OpenProcessToken(GetCurrentProcess(),
456                               TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
457                               &hToken))
458                 goto out;
459
460         if (!LookupPrivilegeValue(NULL, privilege, &luid))
461                 goto out_close_handle;
462
463         newState.PrivilegeCount = 1;
464         newState.Privileges[0].Luid = luid;
465         newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
466         SetLastError(ERROR_SUCCESS);
467         ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
468         if (ret && GetLastError() == ERROR_NOT_ALL_ASSIGNED)
469                 ret = FALSE;
470 out_close_handle:
471         CloseHandle(hToken);
472 out:
473         return ret;
474 }
475
476 static bool
477 win32_modify_capture_privileges(bool enable)
478 {
479         return win32_modify_privilege(SE_BACKUP_NAME, enable)
480             && win32_modify_privilege(SE_SECURITY_NAME, enable);
481 }
482
483 static bool
484 win32_modify_apply_privileges(bool enable)
485 {
486         return win32_modify_privilege(SE_RESTORE_NAME, enable)
487             && win32_modify_privilege(SE_SECURITY_NAME, enable)
488             && win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
489 }
490
491 static void
492 win32_release_capture_and_apply_privileges(void)
493 {
494         win32_modify_capture_privileges(false);
495         win32_modify_apply_privileges(false);
496 }
497
498 HANDLE
499 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
500 {
501         return CreateFile(path,
502                           dwDesiredAccess,
503                           FILE_SHARE_READ,
504                           NULL, /* lpSecurityAttributes */
505                           OPEN_EXISTING,
506                           FILE_FLAG_BACKUP_SEMANTICS |
507                                 FILE_FLAG_OPEN_REPARSE_POINT,
508                           NULL /* hTemplateFile */);
509 }
510
511 /* Pointers to dynamically loaded functions  */
512
513 /* kernel32.dll:  Vista and later */
514 BOOL (WINAPI *func_CreateSymbolicLinkW)(const wchar_t *lpSymlinkFileName,
515                                         const wchar_t *lpTargetFileName,
516                                         DWORD dwFlags);
517
518 /* ntdll.dll  */
519
520 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
521                                     ACCESS_MASK DesiredAccess,
522                                     POBJECT_ATTRIBUTES ObjectAttributes,
523                                     PIO_STATUS_BLOCK IoStatusBlock,
524                                     ULONG ShareAccess,
525                                     ULONG OpenOptions);
526
527 NTSTATUS (WINAPI *func_NtReadFile) (HANDLE FileHandle,
528                                     HANDLE Event,
529                                     PIO_APC_ROUTINE ApcRoutine,
530                                     PVOID ApcContext,
531                                     PIO_STATUS_BLOCK IoStatusBlock,
532                                     PVOID Buffer,
533                                     ULONG Length,
534                                     PLARGE_INTEGER ByteOffset,
535                                     PULONG Key);
536
537 NTSTATUS (WINAPI *func_NtOpenFile) (PHANDLE FileHandle,
538                                     ACCESS_MASK DesiredAccess,
539                                     POBJECT_ATTRIBUTES ObjectAttributes,
540                                     PIO_STATUS_BLOCK IoStatusBlock,
541                                     ULONG ShareAccess,
542                                     ULONG OpenOptions);
543
544 NTSTATUS (WINAPI *func_NtQueryInformationFile)(HANDLE FileHandle,
545                                                PIO_STATUS_BLOCK IoStatusBlock,
546                                                PVOID FileInformation,
547                                                ULONG Length,
548                                                FILE_INFORMATION_CLASS FileInformationClass);
549
550 NTSTATUS (WINAPI *func_NtQuerySecurityObject)(HANDLE handle,
551                                               SECURITY_INFORMATION SecurityInformation,
552                                               PSECURITY_DESCRIPTOR SecurityDescriptor,
553                                               ULONG Length,
554                                               PULONG LengthNeeded);
555
556 NTSTATUS (WINAPI *func_NtQueryDirectoryFile) (HANDLE FileHandle,
557                                               HANDLE Event,
558                                               PIO_APC_ROUTINE ApcRoutine,
559                                               PVOID ApcContext,
560                                               PIO_STATUS_BLOCK IoStatusBlock,
561                                               PVOID FileInformation,
562                                               ULONG Length,
563                                               FILE_INFORMATION_CLASS FileInformationClass,
564                                               BOOLEAN ReturnSingleEntry,
565                                               PUNICODE_STRING FileName,
566                                               BOOLEAN RestartScan);
567
568 NTSTATUS (WINAPI *func_NtSetSecurityObject)(HANDLE Handle,
569                                             SECURITY_INFORMATION SecurityInformation,
570                                             PSECURITY_DESCRIPTOR SecurityDescriptor);
571
572 NTSTATUS (WINAPI *func_NtClose) (HANDLE Handle);
573
574 DWORD (WINAPI *func_RtlNtStatusToDosError)(NTSTATUS status);
575
576 NTSTATUS (WINAPI *func_RtlCreateSystemVolumeInformationFolder)
577                 (PCUNICODE_STRING VolumeRootPath);
578
579 static OSVERSIONINFO windows_version_info = {
580         .dwOSVersionInfoSize = sizeof(OSVERSIONINFO),
581 };
582
583 static bool acquired_privileges = false;
584
585 bool
586 windows_version_is_at_least(unsigned major, unsigned minor)
587 {
588         return windows_version_info.dwMajorVersion > major ||
589                 (windows_version_info.dwMajorVersion == major &&
590                  windows_version_info.dwMinorVersion >= minor);
591 }
592
593 struct dll_sym {
594         void **func_ptr;
595         const char *name;
596         bool required;
597 };
598
599 #define DLL_SYM(name, required) { (void **)&func_##name, #name, required }
600
601 #define for_each_sym(sym, spec) \
602         for ((sym) = (spec)->syms; (sym)->name; (sym)++)
603
604 struct dll_spec {
605         const wchar_t *name;
606         HMODULE handle;
607         const struct dll_sym syms[];
608 };
609
610 struct dll_spec ntdll_spec = {
611         .name = L"ntdll.dll",
612         .syms = {
613                 DLL_SYM(NtOpenFile, true),
614                 DLL_SYM(NtReadFile, true),
615                 DLL_SYM(NtQueryInformationFile, true),
616                 DLL_SYM(NtQuerySecurityObject, true),
617                 DLL_SYM(NtQueryDirectoryFile, true),
618                 DLL_SYM(NtSetSecurityObject, true),
619                 DLL_SYM(NtClose, true),
620                 DLL_SYM(RtlNtStatusToDosError, true),
621                 DLL_SYM(RtlCreateSystemVolumeInformationFolder, false),
622                 {NULL, NULL},
623         },
624 };
625
626 struct dll_spec kernel32_spec = {
627         .name = L"kernel32.dll",
628         .syms = {
629                 DLL_SYM(CreateSymbolicLinkW, false),
630                 {NULL, NULL},
631         },
632 };
633
634 static int
635 init_dll(struct dll_spec *spec)
636 {
637         const struct dll_sym *sym;
638         void *addr;
639
640         if (!spec->handle)
641                 spec->handle = LoadLibrary(spec->name);
642         if (!spec->handle) {
643                 for_each_sym(sym, spec) {
644                         if (sym->required) {
645                                 ERROR("%ls could not be loaded!", spec->name);
646                                 return WIMLIB_ERR_UNSUPPORTED;
647                         }
648                 }
649                 return 0;
650         }
651         for_each_sym(sym, spec) {
652                 addr = (void *)GetProcAddress(spec->handle, sym->name);
653                 if (addr) {
654                         *(sym->func_ptr) = addr;
655                 } else if (sym->required) {
656                         ERROR("Can't find %s in %ls", sym->name, spec->name);
657                         return WIMLIB_ERR_UNSUPPORTED;
658                 }
659         }
660         return 0;
661 }
662
663 static void
664 cleanup_dll(struct dll_spec *spec)
665 {
666         const struct dll_sym *sym;
667
668         if (spec->handle) {
669                 FreeLibrary(spec->handle);
670                 spec->handle = NULL;
671
672                 for_each_sym(sym, spec)
673                         *(sym->func_ptr) = NULL;
674         }
675 }
676
677 /* One-time initialization for Windows capture/apply code.  */
678 int
679 win32_global_init(int init_flags)
680 {
681         int ret;
682
683         /* Try to acquire useful privileges.  */
684         if (!(init_flags & WIMLIB_INIT_FLAG_DONT_ACQUIRE_PRIVILEGES)) {
685                 ret = WIMLIB_ERR_INSUFFICIENT_PRIVILEGES;
686                 if (!win32_modify_capture_privileges(true))
687                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_CAPTURE_PRIVILEGES)
688                                 goto out_drop_privs;
689                 if (!win32_modify_apply_privileges(true))
690                         if (init_flags & WIMLIB_INIT_FLAG_STRICT_APPLY_PRIVILEGES)
691                                 goto out_drop_privs;
692                 acquired_privileges = true;
693         }
694
695         /* Get Windows version information.  */
696         GetVersionEx(&windows_version_info);
697
698         /* Try to dynamically load some functions.  */
699         ret = init_dll(&kernel32_spec);
700         if (ret)
701                 goto out_drop_privs;
702
703         ret = init_dll(&ntdll_spec);
704         if (ret)
705                 goto out_cleanup_kernel32;
706
707         return 0;
708
709 out_cleanup_kernel32:
710         cleanup_dll(&kernel32_spec);
711 out_drop_privs:
712         win32_release_capture_and_apply_privileges();
713         return ret;
714 }
715
716 void
717 win32_global_cleanup(void)
718 {
719         if (acquired_privileges)
720                 win32_release_capture_and_apply_privileges();
721
722         cleanup_dll(&kernel32_spec);
723         cleanup_dll(&ntdll_spec);
724 }
725
726 #endif /* __WIN32__ */