]> wimlib.net Git - wimlib/blob - src/win32.c
Dynamically load Find{Find,Next}StreamW()
[wimlib] / src / win32.c
1 /*
2  * win32.c
3  *
4  * All the library code specific to native Windows builds is in here.
5  */
6
7 /*
8  * Copyright (C) 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #ifndef __WIN32__
27 #  error "This file contains Windows code"
28 #endif
29
30 #include "config.h"
31 #include <windows.h>
32 #include <ntdef.h>
33 #include <wchar.h>
34 #include <shlwapi.h> /* shlwapi.h for PathMatchSpecA() */
35 #ifdef ERROR /* windows.h defines this */
36 #  undef ERROR
37 #endif
38
39 #include "win32.h"
40 #include "dentry.h"
41 #include "lookup_table.h"
42 #include "security.h"
43 #include "endianness.h"
44
45 #include <errno.h>
46
47
48
49 /* Pointers to functions that are not available on all targetted versions of
50  * Windows (XP and later).  NOTE: The WINAPI annotations seem to be important; I
51  * assume it specifies a certain calling convention. */
52
53 /* Vista and later */
54 static HANDLE (WINAPI *win32func_FindFirstStreamW)(LPCWSTR lpFileName,
55                                             STREAM_INFO_LEVELS InfoLevel,
56                                             LPVOID lpFindStreamData,
57                                             DWORD dwFlags) = NULL;
58
59 /* Vista and later */
60 static BOOL (WINAPI *win32func_FindNextStreamW)(HANDLE hFindStream,
61                                          LPVOID lpFindStreamData) = NULL;
62
63 /* Try to dynamically load some functions */
64 void
65 win32_global_init()
66 {
67         DWORD err;
68         bool warned;
69
70         DEBUG("Loading Kernel32.dll");
71
72         HMODULE lib = LoadLibraryA("Kernel32.dll");
73         if (lib == NULL) {
74                 err = GetLastError();
75                 WARNING("Can't load Kernel32.dll");
76                 win32_error(err);
77                 return;
78         }
79
80         DEBUG("Looking for FindFirstStreamW");
81         win32func_FindFirstStreamW = (void*)GetProcAddress(lib, "FindFirstStreamW");
82         if (!win32func_FindFirstStreamW) {
83                 WARNING("Could not find function FindFirstStreamW() in Kernel32.dll!");
84                 WARNING("Capturing alternate data streams will not be supported.");
85                 goto out_free_lib;
86         }
87
88         DEBUG("Looking for FindNextStreamW");
89         win32func_FindNextStreamW = (void*)GetProcAddress(lib, "FindNextStreamW");
90         if (!win32func_FindNextStreamW) {
91                 WARNING("Could not find function FindNextStreamW() in Kernel32.dll!");
92                 WARNING("Capturing alternate data streams will not be supported.");
93                 win32func_FindFirstStreamW = NULL;
94         }
95 out_free_lib:
96         DEBUG("Closing Kernel32.dll");
97         FreeLibrary(lib);
98 }
99
100 static const char *access_denied_msg =
101 "         If you are not running this program as the administrator, you may\n"
102 "         need to do so, so that all data and metadata can be backed up.\n"
103 "         Otherwise, there may be no way to access the desired data or\n"
104 "         metadata without taking ownership of the file or directory.\n";
105
106 #ifdef ENABLE_ERROR_MESSAGES
107 void
108 win32_error(u32 err_code)
109 {
110         char *buffer;
111         DWORD nchars;
112         nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
113                                 NULL, err_code, 0,
114                                 (char*)&buffer, 0, NULL);
115         if (nchars == 0) {
116                 ERROR("Error printing error message! "
117                       "Computer will self-destruct in 3 seconds.");
118         } else {
119                 ERROR("Win32 error: %s", buffer);
120                 LocalFree(buffer);
121         }
122 }
123
124 void
125 win32_error_last()
126 {
127         win32_error(GetLastError());
128 }
129 #endif
130
131 static HANDLE
132 win32_open_existing_file(const wchar_t *path, DWORD dwDesiredAccess)
133 {
134         return CreateFileW(path,
135                            dwDesiredAccess,
136                            FILE_SHARE_READ,
137                            NULL, /* lpSecurityAttributes */
138                            OPEN_EXISTING,
139                            FILE_FLAG_BACKUP_SEMANTICS |
140                                FILE_FLAG_OPEN_REPARSE_POINT,
141                            NULL /* hTemplateFile */);
142 }
143
144 HANDLE
145 win32_open_file_data_only(const wchar_t *path)
146 {
147         return win32_open_existing_file(path, FILE_READ_DATA);
148 }
149
150 int
151 win32_read_file(const mbchar *filename,
152                 void *handle, u64 offset, size_t size, void *buf)
153 {
154         HANDLE h = handle;
155         DWORD err;
156         DWORD bytesRead;
157         LARGE_INTEGER liOffset = {.QuadPart = offset};
158
159         wimlib_assert(size <= 0xffffffff);
160
161         if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
162                 if (ReadFile(h, buf, size, &bytesRead, NULL) && bytesRead == size)
163                         return 0;
164         err = GetLastError();
165         ERROR("Error reading \"%s\"", filename);
166         win32_error(err);
167         return WIMLIB_ERR_READ;
168 }
169
170 void
171 win32_close_file(void *handle)
172 {
173         CloseHandle((HANDLE)handle);
174 }
175
176 static u64
177 FILETIME_to_u64(const FILETIME *ft)
178 {
179         return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
180 }
181
182 static int
183 win32_get_short_name(struct wim_dentry *dentry,
184                      const wchar_t *path_utf16)
185 {
186         WIN32_FIND_DATAW dat;
187         if (FindFirstFileW(path_utf16, &dat) &&
188             dat.cAlternateFileName[0] != L'\0')
189         {
190                 size_t short_name_nbytes = wcslen(dat.cAlternateFileName) * 2;
191                 size_t n = short_name_nbytes + sizeof(wchar_t);
192                 dentry->short_name = MALLOC(n);
193                 if (!dentry->short_name)
194                         return WIMLIB_ERR_NOMEM;
195                 memcpy(dentry->short_name, dat.cAlternateFileName, n);
196                 dentry->short_name_nbytes = short_name_nbytes;
197         }
198         return 0;
199 }
200
201 static int
202 win32_get_security_descriptor(struct wim_dentry *dentry,
203                               struct sd_set *sd_set,
204                               const wchar_t *path_utf16)
205 {
206         SECURITY_INFORMATION requestedInformation;
207         DWORD lenNeeded = 0;
208         BOOL status;
209         DWORD err;
210
211         requestedInformation = DACL_SECURITY_INFORMATION |
212                                SACL_SECURITY_INFORMATION |
213                                OWNER_SECURITY_INFORMATION |
214                                GROUP_SECURITY_INFORMATION;
215         /* Request length of security descriptor */
216         status = GetFileSecurityW(path_utf16, requestedInformation,
217                                   NULL, 0, &lenNeeded);
218         err = GetLastError();
219         if (!status && err == ERROR_INSUFFICIENT_BUFFER) {
220                 DWORD len = lenNeeded;
221                 char buf[len];
222                 if (GetFileSecurityW(path_utf16, requestedInformation,
223                                      (PSECURITY_DESCRIPTOR)buf, len, &lenNeeded))
224                 {
225                         int security_id = sd_set_add_sd(sd_set, buf, len);
226                         if (security_id < 0)
227                                 return WIMLIB_ERR_NOMEM;
228                         else {
229                                 dentry->d_inode->i_security_id = security_id;
230                                 return 0;
231                         }
232                 } else {
233                         err = GetLastError();
234                 }
235         }
236
237         if (err == ERROR_ACCESS_DENIED) {
238                 WARNING("Failed to read security descriptor of \"%ls\": "
239                         "Access denied!\n%s", path_utf16, access_denied_msg);
240                 return 0;
241         } else {
242                 ERROR("Win32 API: Failed to read security descriptor of \"%ls\"",
243                       path_utf16);
244                 win32_error(err);
245                 return WIMLIB_ERR_READ;
246         }
247 }
248
249 /* Reads the directory entries of directory using a Win32 API and recursively
250  * calls win32_build_dentry_tree() on them. */
251 static int
252 win32_recurse_directory(struct wim_dentry *root,
253                         const mbchar *root_disk_path,
254                         struct wim_lookup_table *lookup_table,
255                         struct wim_security_data *sd,
256                         const struct capture_config *config,
257                         int add_image_flags,
258                         wimlib_progress_func_t progress_func,
259                         struct sd_set *sd_set,
260                         const wchar_t *path_utf16,
261                         size_t path_utf16_nchars)
262 {
263         WIN32_FIND_DATAW dat;
264         HANDLE hFind;
265         DWORD err;
266         int ret;
267
268         {
269                 /* Begin reading the directory by calling FindFirstFileW.
270                  * Unlike UNIX opendir(), FindFirstFileW has file globbing built
271                  * into it.  But this isn't what we actually want, so just add a
272                  * dummy glob to get all entries. */
273                 wchar_t pattern_buf[path_utf16_nchars + 3];
274                 memcpy(pattern_buf, path_utf16,
275                        path_utf16_nchars * sizeof(wchar_t));
276                 pattern_buf[path_utf16_nchars] = L'/';
277                 pattern_buf[path_utf16_nchars + 1] = L'*';
278                 pattern_buf[path_utf16_nchars + 2] = L'\0';
279                 hFind = FindFirstFileW(pattern_buf, &dat);
280         }
281         if (hFind == INVALID_HANDLE_VALUE) {
282                 err = GetLastError();
283                 if (err == ERROR_FILE_NOT_FOUND) {
284                         return 0;
285                 } else {
286                         ERROR("Win32 API: Failed to read directory \"%s\"",
287                               root_disk_path);
288                         win32_error(err);
289                         return WIMLIB_ERR_READ;
290                 }
291         }
292         ret = 0;
293         do {
294                 /* Skip . and .. entries */
295                 if (!(dat.cFileName[0] == cpu_to_le16(L'.') &&
296                       (dat.cFileName[1] == cpu_to_le16(L'\0') ||
297                        (dat.cFileName[1] == cpu_to_le16(L'.') &&
298                         dat.cFileName[2] == cpu_to_le16(L'\0')))))
299                 {
300                         struct wim_dentry *child;
301
302                         mbchar *mbs_name;
303                         size_t mbs_name_nbytes;
304                         ret = utf16le_to_mbs(dat.cFileName,
305                                              wcslen(dat.cFileName) * sizeof(wchar_t),
306                                              &mbs_name,
307                                              &mbs_name_nbytes);
308                         if (ret)
309                                 goto out_find_close;
310
311                         mbchar name[strlen(root_disk_path) + 1 + mbs_name_nbytes + 1];
312                         sprintf(name, "%s/%s", root_disk_path, mbs_name);
313                         FREE(mbs_name);
314                         ret = win32_build_dentry_tree(&child, name, lookup_table,
315                                                       sd, config, add_image_flags,
316                                                       progress_func, sd_set);
317                         if (ret)
318                                 goto out_find_close;
319                         if (child)
320                                 dentry_add_child(root, child);
321                 }
322         } while (FindNextFileW(hFind, &dat));
323         err = GetLastError();
324         if (err != ERROR_NO_MORE_FILES) {
325                 ERROR("Win32 API: Failed to read directory \"%s\"", root_disk_path);
326                 win32_error(err);
327                 if (ret == 0)
328                         ret = WIMLIB_ERR_READ;
329         }
330 out_find_close:
331         FindClose(hFind);
332         return ret;
333 }
334
335 /* Load a reparse point into a WIM inode.  It is just stored in memory.
336  *
337  * @hFile:  Open handle to a reparse point, with permission to read the reparse
338  *          data.
339  *
340  * @inode:  WIM inode for the reparse point.
341  *
342  * @lookup_table:  Stream lookup table for the WIM; an entry will be added to it
343  *                 for the reparse point unless an entry already exists for
344  *                 the exact same data stream.
345  *
346  * @path:  External path to the reparse point.  Used for error messages only.
347  *
348  * Returns 0 on success; nonzero on failure. */
349 static int
350 win32_capture_reparse_point(HANDLE hFile,
351                             struct wim_inode *inode,
352                             struct wim_lookup_table *lookup_table,
353                             const mbchar *path)
354 {
355         /* "Reparse point data, including the tag and optional GUID,
356          * cannot exceed 16 kilobytes." - MSDN  */
357         char reparse_point_buf[16 * 1024];
358         DWORD bytesReturned;
359
360         if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
361                              NULL, /* "Not used with this operation; set to NULL" */
362                              0, /* "Not used with this operation; set to 0" */
363                              reparse_point_buf, /* "A pointer to a buffer that
364                                                    receives the reparse point data */
365                              sizeof(reparse_point_buf), /* "The size of the output
366                                                            buffer, in bytes */
367                              &bytesReturned,
368                              NULL))
369         {
370                 DWORD err = GetLastError();
371                 ERROR("Win32 API: Failed to get reparse data of \"%s\"", path);
372                 win32_error(err);
373                 return WIMLIB_ERR_READ;
374         }
375         if (bytesReturned < 8) {
376                 ERROR("Reparse data on \"%s\" is invalid", path);
377                 return WIMLIB_ERR_READ;
378         }
379         inode->i_reparse_tag = le32_to_cpu(*(u32*)reparse_point_buf);
380         return inode_add_ads_with_data(inode, "",
381                                        reparse_point_buf + 8,
382                                        bytesReturned - 8, lookup_table);
383 }
384
385 /* Calculate the SHA1 message digest of a Win32 data stream, which may be either
386  * an unnamed or named data stream.
387  *
388  * @path:       Path to the file, with the stream noted at the end for named
389  *              streams.  UTF-16LE encoding.
390  *
391  * @hash:       On success, the SHA1 message digest of the stream is written to
392  *              this location.
393  *
394  * Returns 0 on success; nonzero on failure.
395  */
396 static int
397 win32_sha1sum(const wchar_t *path, u8 hash[SHA1_HASH_SIZE])
398 {
399         HANDLE hFile;
400         SHA_CTX ctx;
401         u8 buf[32768];
402         DWORD bytesRead;
403         int ret;
404
405         hFile = win32_open_file_data_only(path);
406         if (hFile == INVALID_HANDLE_VALUE)
407                 return WIMLIB_ERR_OPEN;
408
409         sha1_init(&ctx);
410         for (;;) {
411                 if (!ReadFile(hFile, buf, sizeof(buf), &bytesRead, NULL)) {
412                         ret = WIMLIB_ERR_READ;
413                         goto out_close_handle;
414                 }
415                 if (bytesRead == 0)
416                         break;
417                 sha1_update(&ctx, buf, bytesRead);
418         }
419         ret = 0;
420         sha1_final(hash, &ctx);
421 out_close_handle:
422         CloseHandle(hFile);
423         return ret;
424 }
425
426 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
427  * stream); calculates its SHA1 message digest and either creates a `struct
428  * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
429  * wim_lookup_table_entry' for an identical stream.
430  *
431  * @path_utf16:         Path to the file (UTF-16LE).
432  *
433  * @path_utf16_nchars:  Number of 2-byte characters in @path_utf16.
434  *
435  * @inode:              WIM inode to save the stream into.
436  *
437  * @lookup_table:       Stream lookup table for the WIM.
438  *
439  * @dat:                A `WIN32_FIND_STREAM_DATA' structure that specifies the
440  *                      stream name.
441  *
442  * Returns 0 on success; nonzero on failure.
443  */
444 static int
445 win32_capture_stream(const wchar_t *path_utf16,
446                      size_t path_utf16_nchars,
447                      struct wim_inode *inode,
448                      struct wim_lookup_table *lookup_table,
449                      WIN32_FIND_STREAM_DATA *dat)
450 {
451         struct wim_ads_entry *ads_entry;
452         u8 hash[SHA1_HASH_SIZE];
453         struct wim_lookup_table_entry *lte;
454         int ret;
455         wchar_t *stream_name, *colon;
456         size_t stream_name_nchars;
457         bool is_named_stream;
458         wchar_t *spath;
459         size_t spath_nchars;
460         DWORD err;
461         size_t spath_buf_nbytes;
462         const wchar_t *relpath_prefix;
463         const wchar_t *colonchar;
464
465         /* The stream name should be returned as :NAME:TYPE */
466         stream_name = dat->cStreamName;
467         if (*stream_name != L':')
468                 goto out_invalid_stream_name;
469         stream_name += 1;
470         colon = wcschr(stream_name, L':');
471         if (colon == NULL)
472                 goto out_invalid_stream_name;
473
474         if (wcscmp(colon + 1, L"$DATA")) {
475                 /* Not a DATA stream */
476                 ret = 0;
477                 goto out;
478         }
479
480         *colon = '\0';
481
482         stream_name_nchars = colon - stream_name;
483         is_named_stream = (stream_name_nchars != 0);
484
485         if (is_named_stream) {
486                 /* Allocate an ADS entry for the named stream. */
487                 ads_entry = inode_add_ads_utf16le(inode, stream_name,
488                                                   stream_name_nchars * 2);
489                 if (!ads_entry) {
490                         ret = WIMLIB_ERR_NOMEM;
491                         goto out;
492                 }
493         }
494
495         /* Create a UTF-16LE string @spath that gives the filename, then a
496          * colon, then the stream name.  Or, if it's an unnamed stream, just the
497          * filename.  It is MALLOC()'ed so that it can be saved in the
498          * wim_lookup_table_entry if needed.
499          *
500          * As yet another special case, relative paths need to be changed to
501          * begin with an explicit "./" so that, for example, a file t:ads, where
502          * :ads is the part we added, is not interpreted as a file on the t:
503          * drive. */
504         spath_nchars = path_utf16_nchars;
505         relpath_prefix = L"";
506         colonchar = L"";
507         if (is_named_stream) {
508                 spath_nchars += 1 + stream_name_nchars;
509                 colonchar = L":";
510                 if (path_utf16_nchars == 1 &&
511                     path_utf16[0] != cpu_to_le16('/') &&
512                     path_utf16[0] != cpu_to_le16('\\'))
513                 {
514                         spath_nchars += 2;
515                         relpath_prefix = L"./";
516                 }
517         }
518
519         spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
520         spath = MALLOC(spath_buf_nbytes);
521
522         swprintf(spath, spath_buf_nbytes, L"%ls%ls%ls%ls",
523                  relpath_prefix, path_utf16, colonchar, stream_name);
524
525         ret = win32_sha1sum(spath, hash);
526         if (ret) {
527                 err = GetLastError();
528                 ERROR("Win32 API: Failed to read \"%ls\" to calculate SHA1sum",
529                       spath);
530                 win32_error(err);
531                 goto out_free_spath;
532         }
533
534         lte = __lookup_resource(lookup_table, hash);
535         if (lte) {
536                 /* Use existing wim_lookup_table_entry that has the same SHA1
537                  * message digest */
538                 lte->refcnt++;
539         } else {
540                 /* Make a new wim_lookup_table_entry */
541                 lte = new_lookup_table_entry();
542                 if (!lte) {
543                         ret = WIMLIB_ERR_NOMEM;
544                         goto out_free_spath;
545                 }
546                 lte->win32_file_on_disk = spath;
547                 lte->file_on_disk_fp = INVALID_HANDLE_VALUE;
548                 spath = NULL;
549                 lte->resource_location = RESOURCE_WIN32;
550                 lte->resource_entry.original_size = (uint64_t)dat->StreamSize.QuadPart;
551                 lte->resource_entry.size = (uint64_t)dat->StreamSize.QuadPart;
552                 copy_hash(lte->hash, hash);
553                 lookup_table_insert(lookup_table, lte);
554         }
555         if (is_named_stream)
556                 ads_entry->lte = lte;
557         else
558                 inode->i_lte = lte;
559 out_free_spath:
560         FREE(spath);
561 out:
562         return ret;
563 out_invalid_stream_name:
564         ERROR("Invalid stream name: \"%ls:%ls\"", path_utf16, dat->cStreamName);
565         ret = WIMLIB_ERR_READ;
566         goto out;
567 }
568
569 /* Scans a Win32 file for unnamed and named data streams (not reparse point
570  * streams).
571  *
572  * @path_utf16:         Path to the file (UTF-16LE).
573  *
574  * @path_utf16_nchars:  Number of 2-byte characters in @path_utf16.
575  *
576  * @inode:              WIM inode to save the stream into.
577  *
578  * @lookup_table:       Stream lookup table for the WIM.
579  *
580  * @file_size:          Size of unnamed data stream.  (Used only if alternate
581  *                      data streams API appears to be unavailable.)
582  *
583  * Returns 0 on success; nonzero on failure.
584  */
585 static int
586 win32_capture_streams(const wchar_t *path_utf16,
587                       size_t path_utf16_nchars,
588                       struct wim_inode *inode,
589                       struct wim_lookup_table *lookup_table,
590                       u64 file_size)
591 {
592         WIN32_FIND_STREAM_DATA dat;
593         int ret;
594         HANDLE hFind;
595         DWORD err;
596
597         if (win32func_FindFirstStreamW == NULL)
598                 goto unnamed_only;
599
600         hFind = win32func_FindFirstStreamW(path_utf16, FindStreamInfoStandard, &dat, 0);
601         if (hFind == INVALID_HANDLE_VALUE) {
602                 err = GetLastError();
603
604                 if (err == ERROR_CALL_NOT_IMPLEMENTED)
605                         goto unnamed_only;
606
607                 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
608                  * points and directories */
609                 if ((inode->i_attributes &
610                     (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
611                     && err == ERROR_HANDLE_EOF)
612                 {
613                         return 0;
614                 } else {
615                         if (err == ERROR_ACCESS_DENIED) {
616                                 WARNING("Failed to look up data streams of \"%ls\": "
617                                         "Access denied!\n%s", path_utf16,
618                                         access_denied_msg);
619                                 return 0;
620                         } else {
621                                 ERROR("Win32 API: Failed to look up data streams of \"%ls\"",
622                                       path_utf16);
623                                 win32_error(err);
624                                 return WIMLIB_ERR_READ;
625                         }
626                 }
627         }
628         do {
629                 ret = win32_capture_stream(path_utf16,
630                                            path_utf16_nchars,
631                                            inode, lookup_table,
632                                            &dat);
633                 if (ret)
634                         goto out_find_close;
635         } while (win32func_FindNextStreamW(hFind, &dat));
636         err = GetLastError();
637         if (err != ERROR_HANDLE_EOF) {
638                 ERROR("Win32 API: Error reading data streams from \"%ls\"", path_utf16);
639                 win32_error(err);
640                 ret = WIMLIB_ERR_READ;
641         }
642 out_find_close:
643         FindClose(hFind);
644         return ret;
645 unnamed_only:
646         if (inode->i_attributes &
647              (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
648         {
649                 ret = 0;
650         } else {
651                 wcscpy(dat.cStreamName, L"::$DATA");
652                 dat.StreamSize.QuadPart = file_size;
653                 ret = win32_capture_stream(path_utf16,
654                                            path_utf16_nchars,
655                                            inode, lookup_table,
656                                            &dat);
657         }
658         return ret;
659 }
660
661 /* Win32 version of capturing a directory tree */
662 int
663 win32_build_dentry_tree(struct wim_dentry **root_ret,
664                         const mbchar *root_disk_path,
665                         struct wim_lookup_table *lookup_table,
666                         struct wim_security_data *sd,
667                         const struct capture_config *config,
668                         int add_image_flags,
669                         wimlib_progress_func_t progress_func,
670                         void *extra_arg)
671 {
672         struct wim_dentry *root = NULL;
673         int ret = 0;
674         struct wim_inode *inode;
675
676         wchar_t *path_utf16;
677         size_t path_utf16_nbytes;
678         size_t path_utf16_nchars;
679         struct sd_set *sd_set;
680         DWORD err;
681         u64 file_size;
682
683         if (exclude_path(root_disk_path, config, true)) {
684                 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
685                         ERROR("Cannot exclude the root directory from capture");
686                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
687                         goto out;
688                 }
689                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
690                     && progress_func)
691                 {
692                         union wimlib_progress_info info;
693                         info.scan.cur_path = root_disk_path;
694                         info.scan.excluded = true;
695                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
696                 }
697                 goto out;
698         }
699
700         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
701             && progress_func)
702         {
703                 union wimlib_progress_info info;
704                 info.scan.cur_path = root_disk_path;
705                 info.scan.excluded = false;
706                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
707         }
708
709         if (extra_arg == NULL) {
710                 sd_set = alloca(sizeof(struct sd_set));
711                 sd_set->rb_root.rb_node = NULL,
712                 sd_set->sd = sd;
713         } else {
714                 sd_set = extra_arg;
715         }
716
717         ret = mbs_to_utf16le(root_disk_path, strlen(root_disk_path),
718                              &path_utf16, &path_utf16_nbytes);
719         if (ret)
720                 goto out_destroy_sd_set;
721         path_utf16_nchars = path_utf16_nbytes / sizeof(wchar_t);
722
723         HANDLE hFile = win32_open_existing_file(path_utf16,
724                                                 FILE_READ_DATA | FILE_READ_ATTRIBUTES);
725         if (hFile == INVALID_HANDLE_VALUE) {
726                 err = GetLastError();
727                 ERROR("Win32 API: Failed to open \"%s\"", root_disk_path);
728                 win32_error(err);
729                 ret = WIMLIB_ERR_OPEN;
730                 goto out_free_path_utf16;
731         }
732
733         BY_HANDLE_FILE_INFORMATION file_info;
734         if (!GetFileInformationByHandle(hFile, &file_info)) {
735                 err = GetLastError();
736                 ERROR("Win32 API: Failed to get file information for \"%s\"",
737                       root_disk_path);
738                 win32_error(err);
739                 ret = WIMLIB_ERR_STAT;
740                 goto out_close_handle;
741         }
742
743         /* Create a WIM dentry */
744         ret = new_dentry_with_timeless_inode(path_basename(root_disk_path), &root);
745         if (ret)
746                 goto out_close_handle;
747
748         /* Start preparing the associated WIM inode */
749         inode = root->d_inode;
750
751         inode->i_attributes = file_info.dwFileAttributes;
752         inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
753         inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
754         inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
755         inode->i_ino = ((u64)file_info.nFileIndexHigh << 32) |
756                         (u64)file_info.nFileIndexLow;
757
758         inode->i_resolved = 1;
759         add_image_flags &= ~(WIMLIB_ADD_IMAGE_FLAG_ROOT | WIMLIB_ADD_IMAGE_FLAG_SOURCE);
760
761         /* Get DOS name and security descriptor (if any). */
762         ret = win32_get_short_name(root, path_utf16);
763         if (ret)
764                 goto out_close_handle;
765
766         if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
767                 ret = win32_get_security_descriptor(root, sd_set, path_utf16);
768                 if (ret)
769                         goto out_close_handle;
770         }
771
772         file_size = ((u64)file_info.nFileSizeHigh << 32) |
773                      (u64)file_info.nFileSizeLow;
774
775         if (inode_is_directory(inode)) {
776                 /* Directory (not a reparse point) --- recurse to children */
777
778                 /* But first... directories may have alternate data streams that
779                  * need to be captured. */
780                 ret = win32_capture_streams(path_utf16,
781                                             path_utf16_nchars,
782                                             inode,
783                                             lookup_table,
784                                             file_size);
785                 if (ret)
786                         goto out_close_handle;
787                 ret = win32_recurse_directory(root,
788                                               root_disk_path,
789                                               lookup_table,
790                                               sd,
791                                               config,
792                                               add_image_flags,
793                                               progress_func,
794                                               sd_set,
795                                               path_utf16,
796                                               path_utf16_nchars);
797         } else if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
798                 /* Reparse point: save the reparse tag and data */
799                 ret = win32_capture_reparse_point(hFile,
800                                                   inode,
801                                                   lookup_table,
802                                                   root_disk_path);
803         } else {
804                 /* Not a directory, not a reparse point; capture the default
805                  * file contents and any alternate data streams. */
806                 ret = win32_capture_streams(path_utf16,
807                                             path_utf16_nchars,
808                                             inode,
809                                             lookup_table,
810                                             file_size);
811         }
812 out_close_handle:
813         CloseHandle(hFile);
814 out_free_path_utf16:
815         FREE(path_utf16);
816 out_destroy_sd_set:
817         if (extra_arg == NULL)
818                 destroy_sd_set(sd_set);
819 out:
820         if (ret == 0)
821                 *root_ret = root;
822         else
823                 free_dentry_tree(root, lookup_table);
824         return ret;
825 }
826
827 /* Replacement for POSIX fnmatch() (partial functionality only) */
828 int
829 fnmatch(const char *pattern, const char *string, int flags)
830 {
831         if (PathMatchSpecA(string, pattern))
832                 return 0;
833         else
834                 return FNM_NOMATCH;
835 }
836
837 static int
838 win32_set_reparse_data(HANDLE h,
839                        u32 reparse_tag,
840                        const struct wim_lookup_table_entry *lte,
841                        const wchar_t *path)
842 {
843         int ret;
844         u8 *buf;
845         size_t len;
846
847         if (!lte) {
848                 WARNING("\"%ls\" is marked as a reparse point but had no reparse data",
849                         path);
850                 return 0;
851         }
852         len = wim_resource_size(lte);
853         if (len > 16 * 1024 - 8) {
854                 WARNING("\"%ls\": reparse data too long!", path);
855                 return 0;
856         }
857
858         /* The WIM stream omits the ReparseTag and ReparseDataLength fields, so
859          * leave 8 bytes of space for them at the beginning of the buffer, then
860          * set them manually. */
861         buf = alloca(len + 8);
862         ret = read_full_wim_resource(lte, buf + 8, 0);
863         if (ret)
864                 return ret;
865         *(u32*)(buf + 0) = cpu_to_le32(reparse_tag);
866         *(u16*)(buf + 4) = cpu_to_le16(len);
867         *(u16*)(buf + 6) = 0;
868
869         /* Set the reparse data on the open file using the
870          * FSCTL_SET_REPARSE_POINT ioctl.
871          *
872          * There are contradictions in Microsoft's documentation for this:
873          *
874          * "If hDevice was opened without specifying FILE_FLAG_OVERLAPPED,
875          * lpOverlapped is ignored."
876          *
877          * --- So setting lpOverlapped to NULL is okay since it's ignored.
878          *
879          * "If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an
880          * operation returns no output data and lpOutBuffer is NULL,
881          * DeviceIoControl makes use of lpBytesReturned. After such an
882          * operation, the value of lpBytesReturned is meaningless."
883          *
884          * --- So lpOverlapped not really ignored, as it affects another
885          *  parameter.  This is the actual behavior: lpBytesReturned must be
886          *  specified, even though lpBytesReturned is documented as:
887          *
888          *  "Not used with this operation; set to NULL."
889          */
890         DWORD bytesReturned;
891         if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT, buf, len + 8,
892                              NULL, 0,
893                              &bytesReturned /* lpBytesReturned */,
894                              NULL /* lpOverlapped */))
895         {
896                 DWORD err = GetLastError();
897                 ERROR("Failed to set reparse data on \"%ls\"", path);
898                 win32_error(err);
899                 return WIMLIB_ERR_WRITE;
900         }
901         return 0;
902 }
903
904
905 static int
906 win32_extract_chunk(const void *buf, size_t len, u64 offset, void *arg)
907 {
908         HANDLE hStream = arg;
909
910         DWORD nbytes_written;
911         wimlib_assert(len <= 0xffffffff);
912
913         if (!WriteFile(hStream, buf, len, &nbytes_written, NULL) ||
914             nbytes_written != len)
915         {
916                 DWORD err = GetLastError();
917                 ERROR("WriteFile(): write error");
918                 win32_error(err);
919                 return WIMLIB_ERR_WRITE;
920         }
921         return 0;
922 }
923
924 static int
925 do_win32_extract_stream(HANDLE hStream, struct wim_lookup_table_entry *lte)
926 {
927         return extract_wim_resource(lte, wim_resource_size(lte),
928                                     win32_extract_chunk, hStream);
929 }
930
931 static int
932 win32_extract_stream(const struct wim_inode *inode,
933                      const wchar_t *path,
934                      const wchar_t *stream_name_utf16,
935                      struct wim_lookup_table_entry *lte,
936                      const struct wim_security_data *security_data)
937 {
938         wchar_t *stream_path;
939         HANDLE h;
940         int ret;
941         DWORD err;
942         DWORD creationDisposition = CREATE_ALWAYS;
943
944         SECURITY_ATTRIBUTES *secattr;
945
946         if (security_data && inode->i_security_id != -1) {
947                 secattr = alloca(sizeof(*secattr));
948                 secattr->nLength = sizeof(*secattr);
949                 secattr->lpSecurityDescriptor = security_data->descriptors[inode->i_security_id];
950                 secattr->bInheritHandle = FALSE;
951         } else {
952                 secattr = NULL;
953         }
954
955         if (stream_name_utf16) {
956                 /* Named stream.  Create a buffer that contains the UTF-16LE
957                  * string [.\]@path:@stream_name_utf16.  This is needed to
958                  * create and open the stream using CreateFileW().  I'm not
959                  * aware of any other APIs to do this.  Note: the '$DATA' suffix
960                  * seems to be unneeded.  Additional note: a "./" prefix needs
961                  * to be added when the path is not absolute to avoid ambiguity
962                  * with drive letters. */
963                 size_t stream_path_nchars;
964                 size_t path_nchars;
965                 size_t stream_name_nchars;
966                 const wchar_t *prefix;
967
968                 path_nchars = wcslen(path);
969                 stream_name_nchars = wcslen(stream_name_utf16);
970                 stream_path_nchars = path_nchars + 1 + stream_name_nchars;
971                 if (path[0] != cpu_to_le16(L'\0') &&
972                     path[0] != cpu_to_le16(L'/') &&
973                     path[0] != cpu_to_le16(L'\\') &&
974                     path[1] != cpu_to_le16(L':'))
975                 {
976                         prefix = L"./";
977                         stream_path_nchars += 2;
978                 } else {
979                         prefix = L"";
980                 }
981                 stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
982                 swprintf(stream_path, stream_path_nchars + 1, L"%ls%ls:%ls",
983                          prefix, path, stream_name_utf16);
984         } else {
985                 /* Unnamed stream; its path is just the path to the file itself.
986                  * */
987                 stream_path = (wchar_t*)path;
988
989                 /* Directories must be created with CreateDirectoryW().  Then
990                  * the call to CreateFileW() will merely open the directory that
991                  * was already created rather than creating a new file. */
992                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
993                         if (!CreateDirectoryW(stream_path, secattr)) {
994                                 err = GetLastError();
995                                 if (err != ERROR_ALREADY_EXISTS) {
996                                         ERROR("Failed to create directory \"%ls\"",
997                                               stream_path);
998                                         win32_error(err);
999                                         ret = WIMLIB_ERR_MKDIR;
1000                                         goto fail;
1001                                 }
1002                         }
1003                         DEBUG("Created directory \"%ls\"", stream_path);
1004                         if (!(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
1005                                 ret = 0;
1006                                 goto out;
1007                         }
1008                         creationDisposition = OPEN_EXISTING;
1009                 }
1010         }
1011
1012         DEBUG("Opening \"%ls\"", stream_path);
1013         h = CreateFileW(stream_path,
1014                         GENERIC_WRITE,
1015                         0,
1016                         secattr,
1017                         creationDisposition,
1018                         FILE_FLAG_OPEN_REPARSE_POINT |
1019                             FILE_FLAG_BACKUP_SEMANTICS |
1020                             inode->i_attributes,
1021                         NULL);
1022         if (h == INVALID_HANDLE_VALUE) {
1023                 err = GetLastError();
1024                 ERROR("Failed to create \"%ls\"", stream_path);
1025                 win32_error(err);
1026                 ret = WIMLIB_ERR_OPEN;
1027                 goto fail;
1028         }
1029
1030         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
1031             stream_name_utf16 == NULL)
1032         {
1033                 DEBUG("Setting reparse data on \"%ls\"", path);
1034                 ret = win32_set_reparse_data(h, inode->i_reparse_tag, lte, path);
1035                 if (ret)
1036                         goto fail_close_handle;
1037         } else {
1038                 if (lte) {
1039                         DEBUG("Extracting \"%ls\" (len = %"PRIu64")",
1040                               stream_path, wim_resource_size(lte));
1041                         ret = do_win32_extract_stream(h, lte);
1042                         if (ret)
1043                                 goto fail_close_handle;
1044                 }
1045         }
1046
1047         DEBUG("Closing \"%ls\"", stream_path);
1048         if (!CloseHandle(h)) {
1049                 err = GetLastError();
1050                 ERROR("Failed to close \"%ls\"", stream_path);
1051                 win32_error(err);
1052                 ret = WIMLIB_ERR_WRITE;
1053                 goto fail;
1054         }
1055         ret = 0;
1056         goto out;
1057 fail_close_handle:
1058         CloseHandle(h);
1059 fail:
1060         ERROR("Error extracting %ls", stream_path);
1061 out:
1062         return ret;
1063 }
1064
1065 /*
1066  * Creates a file, directory, or reparse point and extracts all streams to it
1067  * (unnamed data stream and/or reparse point stream, plus any alternate data
1068  * streams).  This in Win32-specific code.
1069  *
1070  * @inode:      WIM inode for this file or directory.
1071  * @path:       UTF-16LE external path to extract the inode to.
1072  *
1073  * Returns 0 on success; nonzero on failure.
1074  */
1075 static int
1076 win32_extract_streams(const struct wim_inode *inode,
1077                       const wchar_t *path, u64 *completed_bytes_p,
1078                       const struct wim_security_data *security_data)
1079 {
1080         struct wim_lookup_table_entry *unnamed_lte;
1081         int ret;
1082
1083         unnamed_lte = inode_unnamed_lte_resolved(inode);
1084         ret = win32_extract_stream(inode, path, NULL, unnamed_lte,
1085                                    security_data);
1086         if (ret)
1087                 goto out;
1088         if (unnamed_lte)
1089                 *completed_bytes_p += wim_resource_size(unnamed_lte);
1090         for (u16 i = 0; i < inode->i_num_ads; i++) {
1091                 const struct wim_ads_entry *ads_entry = &inode->i_ads_entries[i];
1092                 if (ads_entry->stream_name_nbytes != 0) {
1093                         /* Skip special UNIX data entries (see documentation for
1094                          * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
1095                         if (ads_entry->stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
1096                             && !memcmp(ads_entry->stream_name,
1097                                        WIMLIB_UNIX_DATA_TAG_UTF16LE,
1098                                        WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
1099                                 continue;
1100                         ret = win32_extract_stream(inode,
1101                                                    path,
1102                                                    ads_entry->stream_name,
1103                                                    ads_entry->lte,
1104                                                    NULL);
1105                         if (ret)
1106                                 break;
1107                         if (ads_entry->lte)
1108                                 *completed_bytes_p += wim_resource_size(ads_entry->lte);
1109                 }
1110         }
1111 out:
1112         return ret;
1113 }
1114
1115 /* Extract a file, directory, reparse point, or hard link to an
1116  * already-extracted file using the Win32 API */
1117 int win32_do_apply_dentry(const mbchar *output_path,
1118                           size_t output_path_nbytes,
1119                           struct wim_dentry *dentry,
1120                           struct apply_args *args)
1121 {
1122         wchar_t *utf16le_path;
1123         size_t utf16le_path_nbytes;
1124         DWORD err;
1125         int ret;
1126         struct wim_inode *inode = dentry->d_inode;
1127
1128         ret = mbs_to_utf16le(output_path, output_path_nbytes,
1129                              &utf16le_path, &utf16le_path_nbytes);
1130         if (ret)
1131                 return ret;
1132
1133         if (inode->i_nlink > 1 && inode->i_extracted_file != NULL) {
1134                 /* Linked file, with another name already extracted.  Create a
1135                  * hard link. */
1136                 DEBUG("Creating hard link \"%ls => %ls\"",
1137                       utf16le_path, inode->i_extracted_file);
1138                 if (!CreateHardLinkW(utf16le_path, inode->i_extracted_file, NULL))
1139                 {
1140                         err = GetLastError();
1141                         ERROR("Can't create hard link \"%ls => %ls\"",
1142                               utf16le_path, inode->i_extracted_file);
1143                         ret = WIMLIB_ERR_LINK;
1144                         win32_error(err);
1145                 }
1146         } else {
1147                 /* Create the file, directory, or reparse point, and extract the
1148                  * data streams. */
1149                 const struct wim_security_data *security_data;
1150                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_NOACLS)
1151                         security_data = NULL;
1152                 else
1153                         security_data = wim_const_security_data(args->w);
1154
1155                 ret = win32_extract_streams(inode, utf16le_path,
1156                                             &args->progress.extract.completed_bytes,
1157                                             security_data);
1158                 if (ret)
1159                         goto out_free_utf16_path;
1160
1161                 if (inode->i_nlink > 1) {
1162                         /* Save extracted path for a later call to
1163                          * CreateHardLinkW() if this inode has multiple links.
1164                          * */
1165                         inode->i_extracted_file = utf16le_path;
1166                         goto out;
1167                 }
1168         }
1169 out_free_utf16_path:
1170         FREE(utf16le_path);
1171 out:
1172         return ret;
1173 }
1174
1175 /* Set timestamps on an extracted file using the Win32 API */
1176 int
1177 win32_do_apply_dentry_timestamps(const mbchar *output_path,
1178                                  size_t output_path_nbytes,
1179                                  const struct wim_dentry *dentry,
1180                                  const struct apply_args *args)
1181 {
1182         /* Win32 */
1183         wchar_t *utf16le_path;
1184         size_t utf16le_path_nbytes;
1185         DWORD err;
1186         HANDLE h;
1187         int ret;
1188         const struct wim_inode *inode = dentry->d_inode;
1189
1190         ret = mbs_to_utf16le(output_path, output_path_nbytes,
1191                             &utf16le_path, &utf16le_path_nbytes);
1192         if (ret)
1193                 return ret;
1194
1195         DEBUG("Opening \"%s\" to set timestamps", output_path);
1196         h = win32_open_existing_file(utf16le_path, FILE_WRITE_ATTRIBUTES);
1197
1198         if (h == INVALID_HANDLE_VALUE)
1199                 err = GetLastError();
1200         FREE(utf16le_path);
1201         if (h == INVALID_HANDLE_VALUE)
1202                 goto fail;
1203
1204         FILETIME creationTime = {.dwLowDateTime = inode->i_creation_time & 0xffffffff,
1205                                  .dwHighDateTime = inode->i_creation_time >> 32};
1206         FILETIME lastAccessTime = {.dwLowDateTime = inode->i_last_access_time & 0xffffffff,
1207                                   .dwHighDateTime = inode->i_last_access_time >> 32};
1208         FILETIME lastWriteTime = {.dwLowDateTime = inode->i_last_write_time & 0xffffffff,
1209                                   .dwHighDateTime = inode->i_last_write_time >> 32};
1210
1211         DEBUG("Calling SetFileTime() on \"%s\"", output_path);
1212         if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime)) {
1213                 err = GetLastError();
1214                 CloseHandle(h);
1215                 goto fail;
1216         }
1217         DEBUG("Closing \"%s\"", output_path);
1218         if (!CloseHandle(h)) {
1219                 err = GetLastError();
1220                 goto fail;
1221         }
1222         goto out;
1223 fail:
1224         /* Only warn if setting timestamps failed. */
1225         WARNING("Can't set timestamps on \"%s\"", output_path);
1226         win32_error(err);
1227 out:
1228         return 0;
1229 }
1230
1231 /* Replacement for POSIX fsync() */
1232 int
1233 fsync(int fd)
1234 {
1235         HANDLE h = (HANDLE)_get_osfhandle(fd);
1236         if (h == INVALID_HANDLE_VALUE) {
1237                 ERROR("Could not get Windows handle for file descriptor");
1238                 win32_error(GetLastError());
1239                 errno = EBADF;
1240                 return -1;
1241         }
1242         if (!FlushFileBuffers(h)) {
1243                 ERROR("Could not flush file buffers to disk");
1244                 win32_error(GetLastError());
1245                 errno = EIO;
1246                 return -1;
1247         }
1248         return 0;
1249 }
1250
1251 /* Use the Win32 API to get the number of processors */
1252 unsigned
1253 win32_get_number_of_processors()
1254 {
1255         SYSTEM_INFO sysinfo;
1256         GetSystemInfo(&sysinfo);
1257         return sysinfo.dwNumberOfProcessors;
1258 }
1259
1260 /* Replacement for POSIX-2008 realpath().  Warning: partial functionality only
1261  * (resolved_path must be NULL).   Also I highly doubt that GetFullPathName
1262  * really does the right thing under all circumstances. */
1263 mbchar *
1264 realpath(const mbchar *path, mbchar *resolved_path)
1265 {
1266         DWORD ret;
1267         wimlib_assert(resolved_path == NULL);
1268
1269         ret = GetFullPathNameA(path, 0, NULL, NULL);
1270         if (!ret)
1271                 goto fail_win32;
1272
1273         resolved_path = MALLOC(ret);
1274         if (!resolved_path)
1275                 goto fail;
1276         ret = GetFullPathNameA(path, ret, resolved_path, NULL);
1277         if (!ret) {
1278                 free(resolved_path);
1279                 goto fail_win32;
1280         }
1281         return resolved_path;
1282 fail_win32:
1283         win32_error(GetLastError());
1284 fail:
1285         return NULL;
1286 }
1287
1288 char *
1289 nl_langinfo(nl_item item)
1290 {
1291         wimlib_assert(item == CODESET);
1292         static char buf[64];
1293         strcpy(buf, "Unknown");
1294         return buf;
1295 }
1296
1297 /* rename() on Windows fails if the destination file exists.  Fix it. */
1298 int
1299 rename_replacement(const char *oldpath, const char *newpath)
1300 {
1301         if (MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
1302                 return 0;
1303         } else {
1304                 /* As usual, the possible error values are not documented */
1305                 DWORD err = GetLastError();
1306                 ERROR("MoveFileExA(): Can't rename \"%s\" to \"%s\"",
1307                       oldpath, newpath);
1308                 win32_error(err);
1309                 errno = 0;
1310                 return -1;
1311         }
1312 }