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