]> wimlib.net Git - wimlib/blob - src/win32_apply.c
8b795f458dd2ead757ec0908eae1d648466f74c1
[wimlib] / src / win32_apply.c
1 /*
2  * win32_apply.c - Windows-specific code for applying files from a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013-2018 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef __WIN32__
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/win32_common.h"
29
30 #include "wimlib/apply.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/blob_table.h"
33 #include "wimlib/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/error.h"
36 #include "wimlib/metadata.h"
37 #include "wimlib/object_id.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/pattern.h"
40 #include "wimlib/reparse.h"
41 #include "wimlib/scan.h" /* for mangle_pat() and match_pattern_list()  */
42 #include "wimlib/textfile.h"
43 #include "wimlib/wimboot.h"
44 #include "wimlib/wof.h"
45 #include "wimlib/xattr.h"
46 #include "wimlib/xml.h"
47
48 struct win32_apply_ctx {
49
50         /* Extract flags, the pointer to the WIMStruct, etc.  */
51         struct apply_ctx common;
52
53         /* WIMBoot information, only filled in if WIMLIB_EXTRACT_FLAG_WIMBOOT
54          * was provided  */
55         struct {
56                 /* This array contains the WIM files registered with WOF on the
57                  * target volume for this extraction operation.  All WIMStructs
58                  * in this array are distinct and have ->filename != NULL.  */
59                 struct wimboot_wim {
60                         WIMStruct *wim;
61                         u64 data_source_id;
62                         u8 blob_table_hash[SHA1_HASH_SIZE];
63                 } *wims;
64                 size_t num_wims;
65                 bool wof_running;
66                 bool have_wrong_version_wims;
67                 bool have_uncompressed_wims;
68                 bool have_unsupported_compressed_resources;
69                 bool have_huge_resources;
70         } wimboot;
71
72         /* External backing information  */
73         struct string_list *prepopulate_pats;
74         void *mem_prepopulate_pats;
75         bool tried_to_load_prepopulate_list;
76
77         /* Open handle to the target directory  */
78         HANDLE h_target;
79
80         /* NT namespace path to the target directory (buffer allocated)  */
81         UNICODE_STRING target_ntpath;
82
83         /* Temporary buffer for building paths (buffer allocated)  */
84         UNICODE_STRING pathbuf;
85
86         /* Object attributes to reuse for opening files in the target directory.
87          * (attr.ObjectName == &pathbuf) and (attr.RootDirectory == h_target).
88          */
89         OBJECT_ATTRIBUTES attr;
90
91         /* Temporary I/O status block for system calls  */
92         IO_STATUS_BLOCK iosb;
93
94         /* Allocated buffer for creating "printable" paths from our
95          * target-relative NT paths  */
96         wchar_t *print_buffer;
97
98         /* Allocated buffer for reading blob data when it cannot be extracted
99          * directly  */
100         u8 *data_buffer;
101
102         /* Pointer to the next byte in @data_buffer to fill  */
103         u8 *data_buffer_ptr;
104
105         /* Size allocated in @data_buffer  */
106         size_t data_buffer_size;
107
108         /* Current offset in the raw encrypted file being written  */
109         size_t encrypted_offset;
110
111         /* Current size of the raw encrypted file being written  */
112         size_t encrypted_size;
113
114         /* Temporary buffer for reparse data  */
115         struct reparse_buffer_disk rpbuf;
116
117         /* Temporary buffer for reparse data of "fixed" absolute symbolic links
118          * and junctions  */
119         struct reparse_buffer_disk rpfixbuf;
120
121         /* Array of open handles to filesystem streams currently being written
122          */
123         HANDLE open_handles[MAX_OPEN_FILES];
124
125         /* Number of handles in @open_handles currently open (filled in from the
126          * beginning of the array)  */
127         unsigned num_open_handles;
128
129         /* For each currently open stream, whether we're writing to it in
130          * "sparse" mode or not.  */
131         bool is_sparse_stream[MAX_OPEN_FILES];
132
133         /* Whether is_sparse_stream[] is true for any currently open stream  */
134         bool any_sparse_streams;
135
136         /* List of dentries, joined by @d_tmp_list, that need to have reparse
137          * data extracted as soon as the whole blob has been read into
138          * @data_buffer.  */
139         struct list_head reparse_dentries;
140
141         /* List of dentries, joined by @d_tmp_list, that need to have raw
142          * encrypted data extracted as soon as the whole blob has been read into
143          * @data_buffer.  */
144         struct list_head encrypted_dentries;
145
146         /* Number of files for which we didn't have permission to set the full
147          * security descriptor.  */
148         unsigned long partial_security_descriptors;
149
150         /* Number of files for which we didn't have permission to set any part
151          * of the security descriptor.  */
152         unsigned long no_security_descriptors;
153
154         /* Number of files for which we couldn't set the short name.  */
155         unsigned long num_set_short_name_failures;
156
157         /* Number of files for which we couldn't remove the short name.  */
158         unsigned long num_remove_short_name_failures;
159
160         /* Number of files on which we couldn't set System Compression.  */
161         unsigned long num_system_compression_failures;
162
163         /* The number of files which, for compatibility with the Windows
164          * bootloader, were not compressed using the requested system
165          * compression format.  This includes matches with the hardcoded pattern
166          * list only; it does not include matches with patterns in
167          * [PrepopulateList].  */
168         unsigned long num_system_compression_exclusions;
169
170         /* Number of files for which we couldn't set the object ID.  */
171         unsigned long num_object_id_failures;
172
173         /* Number of files for which we couldn't set extended attributes.  */
174         unsigned long num_xattr_failures;
175
176         /* The Windows build number of the image being applied, or 0 if unknown.
177          */
178         u64 windows_build_number;
179
180         /* Have we tried to enable short name support on the target volume yet?
181          */
182         bool tried_to_enable_short_names;
183 };
184
185 /* Get the drive letter from a Windows path, or return the null character if the
186  * path is relative.  */
187 static wchar_t
188 get_drive_letter(const wchar_t *path)
189 {
190         /* Skip \\?\ prefix  */
191         if (!wcsncmp(path, L"\\\\?\\", 4))
192                 path += 4;
193
194         /* Return drive letter if valid  */
195         if (((path[0] >= L'a' && path[0] <= L'z') ||
196              (path[0] >= L'A' && path[0] <= L'Z')) && path[1] == L':')
197                 return path[0];
198
199         return L'\0';
200 }
201
202 static void
203 get_vol_flags(const wchar_t *target, DWORD *vol_flags_ret,
204               bool *short_names_supported_ret)
205 {
206         wchar_t filesystem_name[MAX_PATH + 1];
207         wchar_t drive[4];
208         wchar_t *volume = NULL;
209
210         *vol_flags_ret = 0;
211         *short_names_supported_ret = false;
212
213         drive[0] = get_drive_letter(target);
214         if (drive[0]) {
215                 drive[1] = L':';
216                 drive[2] = L'\\';
217                 drive[3] = L'\0';
218                 volume = drive;
219         }
220
221         if (!GetVolumeInformation(volume, NULL, 0, NULL, NULL,
222                                   vol_flags_ret, filesystem_name,
223                                   ARRAY_LEN(filesystem_name)))
224         {
225                 win32_warning(GetLastError(),
226                               L"Failed to get volume information for \"%ls\"",
227                               target);
228                 return;
229         }
230
231         if (wcsstr(filesystem_name, L"NTFS")) {
232                 /*
233                  * FILE_SUPPORTS_HARD_LINKS and
234                  * FILE_SUPPORTS_EXTENDED_ATTRIBUTES are only supported on
235                  * Windows 7 and later.  Force them on anyway if the filesystem
236                  * is NTFS.
237                  */
238                 *vol_flags_ret |= FILE_SUPPORTS_HARD_LINKS;
239                 *vol_flags_ret |= FILE_SUPPORTS_EXTENDED_ATTRIBUTES;
240
241                 /* There's no volume flag for short names, but according to the
242                  * MS documentation they are only user-settable on NTFS.  */
243                 *short_names_supported_ret = true;
244         }
245 }
246
247 /* Is the image being extracted an OS image for Windows 10 or later?  */
248 static bool
249 is_image_windows_10_or_later(struct win32_apply_ctx *ctx)
250 {
251         /* Note: if no build number is available, this returns false.  */
252         return ctx->windows_build_number >= 10240;
253 }
254
255 static const wchar_t *
256 current_path(struct win32_apply_ctx *ctx);
257
258 static void
259 build_extraction_path(const struct wim_dentry *dentry,
260                       struct win32_apply_ctx *ctx);
261
262 static int
263 report_dentry_apply_error(const struct wim_dentry *dentry,
264                           struct win32_apply_ctx *ctx, int ret)
265 {
266         build_extraction_path(dentry, ctx);
267         return report_apply_error(&ctx->common, ret, current_path(ctx));
268 }
269
270 static inline int
271 check_apply_error(const struct wim_dentry *dentry,
272                   struct win32_apply_ctx *ctx, int ret)
273 {
274         if (unlikely(ret))
275                 ret = report_dentry_apply_error(dentry, ctx, ret);
276         return ret;
277 }
278
279 static int
280 win32_get_supported_features(const wchar_t *target,
281                              struct wim_features *supported_features)
282 {
283         DWORD vol_flags;
284         bool short_names_supported;
285
286         /* Query the features of the target volume.  */
287
288         get_vol_flags(target, &vol_flags, &short_names_supported);
289
290         supported_features->readonly_files = 1;
291         supported_features->hidden_files = 1;
292         supported_features->system_files = 1;
293         supported_features->archive_files = 1;
294
295         if (vol_flags & FILE_FILE_COMPRESSION)
296                 supported_features->compressed_files = 1;
297
298         if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
299                 supported_features->encrypted_files = 1;
300                 supported_features->encrypted_directories = 1;
301         }
302
303         supported_features->not_context_indexed_files = 1;
304
305         if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
306                 supported_features->sparse_files = 1;
307
308         if (vol_flags & FILE_NAMED_STREAMS)
309                 supported_features->named_data_streams = 1;
310
311         if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
312                 supported_features->hard_links = 1;
313
314         if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS)
315                 supported_features->reparse_points = 1;
316
317         if (vol_flags & FILE_PERSISTENT_ACLS)
318                 supported_features->security_descriptors = 1;
319
320         if (short_names_supported)
321                 supported_features->short_names = 1;
322
323         if (vol_flags & FILE_SUPPORTS_OBJECT_IDS)
324                 supported_features->object_ids = 1;
325
326         supported_features->timestamps = 1;
327
328         if (vol_flags & FILE_CASE_SENSITIVE_SEARCH) {
329                 /*
330                  * The filesystem supports case-sensitive filenames.  But does
331                  * the operating system as well?  This normally requires the
332                  * registry setting ObCaseInsensitive=0.  We can test it
333                  * indirectly by attempting to open the "\SystemRoot" symbolic
334                  * link using a name with the wrong case.  If we get
335                  * STATUS_OBJECT_NAME_NOT_FOUND instead of STATUS_ACCESS_DENIED,
336                  * then case-sensitive names must be enabled.
337                  */
338                 UNICODE_STRING path;
339                 OBJECT_ATTRIBUTES attr;
340                 HANDLE h;
341                 NTSTATUS status;
342
343                 RtlInitUnicodeString(&path, L"\\systemroot");
344                 InitializeObjectAttributes(&attr, &path, 0, NULL, NULL);
345
346                 status = NtOpenSymbolicLinkObject(&h, 0, &attr);
347                 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
348                         supported_features->case_sensitive_filenames = 1;
349         }
350
351         if (vol_flags & FILE_SUPPORTS_EXTENDED_ATTRIBUTES)
352                 supported_features->xattrs = 1;
353
354         return 0;
355 }
356
357 #define COMPACT_FLAGS   (WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K |         \
358                          WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K |         \
359                          WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K |        \
360                          WIMLIB_EXTRACT_FLAG_COMPACT_LZX)
361
362
363
364 /*
365  * If not done already, load the patterns from the [PrepopulateList] section of
366  * WimBootCompress.ini in the WIM image being extracted.
367  *
368  * Note: WimBootCompress.ini applies to both types of "external backing":
369  *
370  *      - WIM backing ("WIMBoot" - Windows 8.1 and later)
371  *      - File backing ("System Compression" - Windows 10 and later)
372  */
373 static int
374 load_prepopulate_pats(struct win32_apply_ctx *ctx)
375 {
376         const wchar_t *path = L"\\Windows\\System32\\WimBootCompress.ini";
377         struct wim_dentry *dentry;
378         const struct blob_descriptor *blob;
379         int ret;
380         void *buf;
381         struct string_list *strings;
382         void *mem;
383         struct text_file_section sec;
384
385         if (ctx->tried_to_load_prepopulate_list)
386                 return 0;
387
388         ctx->tried_to_load_prepopulate_list = true;
389
390         dentry = get_dentry(ctx->common.wim, path, WIMLIB_CASE_INSENSITIVE);
391         if (!dentry ||
392             (dentry->d_inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
393                                               FILE_ATTRIBUTE_REPARSE_POINT |
394                                               FILE_ATTRIBUTE_ENCRYPTED)) ||
395             !(blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
396                                                             ctx->common.wim->blob_table)))
397         {
398                 WARNING("%ls does not exist in the WIM image.\n"
399                         "          The default configuration will be used instead; it assumes that all\n"
400                         "          files are valid for external backing regardless of path, equivalent\n"
401                         "          to an empty [PrepopulateList] section.", path);
402                 return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
403         }
404
405         ret = read_blob_into_alloc_buf(blob, &buf);
406         if (ret)
407                 return ret;
408
409         strings = CALLOC(1, sizeof(struct string_list));
410         if (!strings) {
411                 FREE(buf);
412                 return WIMLIB_ERR_NOMEM;
413         }
414
415         sec.name = T("PrepopulateList");
416         sec.strings = strings;
417
418         ret = load_text_file(path, buf, blob->size, &mem, &sec, 1,
419                              LOAD_TEXT_FILE_REMOVE_QUOTES |
420                              LOAD_TEXT_FILE_NO_WARNINGS,
421                              mangle_pat);
422         STATIC_ASSERT(OS_PREFERRED_PATH_SEPARATOR == WIM_PATH_SEPARATOR);
423         FREE(buf);
424         if (ret) {
425                 FREE(strings);
426                 return ret;
427         }
428         ctx->prepopulate_pats = strings;
429         ctx->mem_prepopulate_pats = mem;
430         return 0;
431 }
432
433 /* Returns %true if the specified absolute path to a file in the WIM image can
434  * be subject to external backing when extracted.  Otherwise returns %false.  */
435 static bool
436 can_externally_back_path(const wchar_t *path, const struct win32_apply_ctx *ctx)
437 {
438         /* Does the path match a pattern given in the [PrepopulateList] section
439          * of WimBootCompress.ini?  */
440         if (ctx->prepopulate_pats && match_pattern_list(path, ctx->prepopulate_pats,
441                                                         MATCH_RECURSIVELY))
442                 return false;
443
444         /* Since we attempt to modify the SYSTEM registry after it's extracted
445          * (see end_wimboot_extraction()), it can't be extracted as externally
446          * backed.  This extends to associated files such as SYSTEM.LOG that
447          * also must be writable in order to write to the registry.  Normally,
448          * SYSTEM is in [PrepopulateList], and the SYSTEM.* files match patterns
449          * in [ExclusionList] and therefore are not captured in the WIM at all.
450          * However, a WIM that wasn't specifically captured in "WIMBoot mode"
451          * may contain SYSTEM.* files.  So to make things "just work", hard-code
452          * the pattern.  */
453         if (match_path(path, L"\\Windows\\System32\\config\\SYSTEM*", 0))
454                 return false;
455
456         return true;
457 }
458
459 /* Can the specified WIM resource be used as the source of an external backing
460  * for the wof.sys WIM provider?  */
461 static bool
462 is_resource_valid_for_external_backing(const struct wim_resource_descriptor *rdesc,
463                                        struct win32_apply_ctx *ctx)
464 {
465         /* Must be the original WIM file format.  This check excludes pipable
466          * resources and solid resources.  It also excludes other resources
467          * contained in such files even if they would be otherwise compatible.
468          */
469         if (rdesc->wim->hdr.magic != WIM_MAGIC ||
470             rdesc->wim->hdr.wim_version != WIM_VERSION_DEFAULT)
471         {
472                 ctx->wimboot.have_wrong_version_wims = true;
473                 return false;
474         }
475
476         /*
477          * Whitelist of compression types and chunk sizes supported by
478          * Microsoft's WOF driver.
479          *
480          * Notes:
481          *    - Uncompressed WIMs result in BSOD.  However, this only applies to
482          *      the WIM file itself, not to uncompressed resources in a WIM file
483          *      that is otherwise compressed.
484          *    - XPRESS 64K sometimes appears to work, but sometimes it causes
485          *      reads to fail with STATUS_UNSUCCESSFUL.
486          */
487         switch (rdesc->compression_type) {
488         case WIMLIB_COMPRESSION_TYPE_NONE:
489                 if (rdesc->wim->compression_type == WIMLIB_COMPRESSION_TYPE_NONE) {
490                         ctx->wimboot.have_uncompressed_wims = true;
491                         return false;
492                 }
493                 break;
494         case WIMLIB_COMPRESSION_TYPE_XPRESS:
495                 switch (rdesc->chunk_size) {
496                 case 4096:
497                 case 8192:
498                 case 16384:
499                 case 32768:
500                         break;
501                 default:
502                         ctx->wimboot.have_unsupported_compressed_resources = true;
503                         return false;
504                 }
505                 break;
506         case WIMLIB_COMPRESSION_TYPE_LZX:
507                 switch (rdesc->chunk_size) {
508                 case 32768:
509                         break;
510                 default:
511                         ctx->wimboot.have_unsupported_compressed_resources = true;
512                         return false;
513                 }
514                 break;
515         default:
516                 ctx->wimboot.have_unsupported_compressed_resources = true;
517                 return false;
518         }
519
520         /* Microsoft's WoF driver errors out if it tries to satisfy a read with
521          * ending offset >= 4 GiB from an externally backed file.  */
522         if (rdesc->uncompressed_size > 4200000000) {
523                 ctx->wimboot.have_huge_resources = true;
524                 return false;
525         }
526
527         return true;
528 }
529
530 #define EXTERNAL_BACKING_NOT_ENABLED            -1
531 #define EXTERNAL_BACKING_NOT_POSSIBLE           -2
532 #define EXTERNAL_BACKING_EXCLUDED               -3
533
534 /*
535  * Determines whether the specified file will be externally backed.  Returns a
536  * negative status code if no, 0 if yes, or a positive wimlib error code on
537  * error.  If the file is excluded from external backing based on its path, then
538  * *excluded_dentry_ret is set to the dentry for the path that matched the
539  * exclusion rule.
540  *
541  * Note that this logic applies to both types of "external backing":
542  *
543  *      - WIM backing ("WIMBoot" - Windows 8.1 and later)
544  *      - File backing ("System Compression" - Windows 10 and later)
545  *
546  * However, in the case of WIM backing we also need to validate that the WIM
547  * resource that would be the source of the backing is supported by the wof.sys
548  * WIM provider.
549  */
550 static int
551 will_externally_back_inode(struct wim_inode *inode, struct win32_apply_ctx *ctx,
552                            const struct wim_dentry **excluded_dentry_ret,
553                            bool wimboot_mode)
554 {
555         struct wim_dentry *dentry;
556         struct blob_descriptor *blob;
557         int ret;
558
559         if (load_prepopulate_pats(ctx) == WIMLIB_ERR_NOMEM)
560                 return WIMLIB_ERR_NOMEM;
561
562         if (inode->i_can_externally_back)
563                 return 0;
564
565         /* This may do redundant checks because the cached value
566          * i_can_externally_back is 2-state (as opposed to 3-state:
567          * unknown/no/yes).  But most files can be externally backed, so this
568          * way is fine.  */
569
570         if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
571                                    FILE_ATTRIBUTE_REPARSE_POINT |
572                                    FILE_ATTRIBUTE_ENCRYPTED))
573                 return EXTERNAL_BACKING_NOT_POSSIBLE;
574
575         blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
576
577         if (!blob)
578                 return EXTERNAL_BACKING_NOT_POSSIBLE;
579
580         if (wimboot_mode &&
581             (blob->blob_location != BLOB_IN_WIM ||
582              !is_resource_valid_for_external_backing(blob->rdesc, ctx)))
583                 return EXTERNAL_BACKING_NOT_POSSIBLE;
584
585         /*
586          * We need to check the patterns in [PrepopulateList] against every name
587          * of the inode, in case any of them match.
588          */
589
590         inode_for_each_extraction_alias(dentry, inode) {
591
592                 ret = calculate_dentry_full_path(dentry);
593                 if (ret)
594                         return ret;
595
596                 if (!can_externally_back_path(dentry->d_full_path, ctx)) {
597                         if (excluded_dentry_ret)
598                                 *excluded_dentry_ret = dentry;
599                         return EXTERNAL_BACKING_EXCLUDED;
600                 }
601         }
602
603         inode->i_can_externally_back = 1;
604         return 0;
605 }
606
607 /*
608  * Determines if the unnamed data stream of a file will be created as a WIM
609  * external backing (a "WIMBoot pointer file"), as opposed to a standard
610  * extraction.
611  */
612 static int
613 win32_will_back_from_wim(struct wim_dentry *dentry, struct apply_ctx *_ctx)
614 {
615         struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
616
617         if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
618                 return EXTERNAL_BACKING_NOT_ENABLED;
619
620         return will_externally_back_inode(dentry->d_inode, ctx, NULL, true);
621 }
622
623 /* Find the WOF registration information for the specified WIM file.  */
624 static struct wimboot_wim *
625 find_wimboot_wim(WIMStruct *wim_to_find, struct win32_apply_ctx *ctx)
626 {
627         for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
628                 if (wim_to_find == ctx->wimboot.wims[i].wim)
629                         return &ctx->wimboot.wims[i];
630
631         wimlib_assert(0);
632         return NULL;
633 }
634
635 static int
636 set_backed_from_wim(HANDLE h, struct wim_inode *inode, struct win32_apply_ctx *ctx)
637 {
638         int ret;
639         const struct wim_dentry *excluded_dentry;
640         const struct blob_descriptor *blob;
641         const struct wimboot_wim *wimboot_wim;
642
643         ret = will_externally_back_inode(inode, ctx, &excluded_dentry, true);
644         if (ret > 0) /* Error.  */
645                 return ret;
646
647         if (ret < 0 && ret != EXTERNAL_BACKING_EXCLUDED)
648                 return 0; /* Not externally backing, other than due to exclusion.  */
649
650         if (unlikely(ret == EXTERNAL_BACKING_EXCLUDED)) {
651                 /* Not externally backing due to exclusion.  */
652                 union wimlib_progress_info info;
653
654                 build_extraction_path(excluded_dentry, ctx);
655
656                 info.wimboot_exclude.path_in_wim = excluded_dentry->d_full_path;
657                 info.wimboot_exclude.extraction_path = current_path(ctx);
658
659                 return call_progress(ctx->common.progfunc,
660                                      WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE,
661                                      &info, ctx->common.progctx);
662         }
663
664         /* Externally backing.  */
665
666         blob = inode_get_blob_for_unnamed_data_stream_resolved(inode);
667         wimboot_wim = find_wimboot_wim(blob->rdesc->wim, ctx);
668
669         if (unlikely(!wimboot_set_pointer(h,
670                                           blob,
671                                           wimboot_wim->data_source_id,
672                                           wimboot_wim->blob_table_hash,
673                                           ctx->wimboot.wof_running)))
674         {
675                 const DWORD err = GetLastError();
676
677                 build_extraction_path(inode_first_extraction_dentry(inode), ctx);
678                 win32_error(err, L"\"%ls\": Couldn't set WIMBoot pointer data",
679                             current_path(ctx));
680                 return WIMLIB_ERR_WIMBOOT;
681         }
682         return 0;
683 }
684
685 /* Calculates the SHA-1 message digest of the WIM's blob table.  */
686 static int
687 hash_blob_table(WIMStruct *wim, u8 hash[SHA1_HASH_SIZE])
688 {
689         return wim_reshdr_to_hash(&wim->hdr.blob_table_reshdr, wim, hash);
690 }
691
692 static int
693 register_wim_with_wof(WIMStruct *wim, struct win32_apply_ctx *ctx)
694 {
695         struct wimboot_wim *p;
696         int ret;
697
698         /* Check if already registered  */
699         for (size_t i = 0; i < ctx->wimboot.num_wims; i++)
700                 if (wim == ctx->wimboot.wims[i].wim)
701                         return 0;
702
703         /* Not yet registered  */
704
705         p = REALLOC(ctx->wimboot.wims,
706                     (ctx->wimboot.num_wims + 1) * sizeof(ctx->wimboot.wims[0]));
707         if (!p)
708                 return WIMLIB_ERR_NOMEM;
709         ctx->wimboot.wims = p;
710
711         ctx->wimboot.wims[ctx->wimboot.num_wims].wim = wim;
712
713         ret = hash_blob_table(wim, ctx->wimboot.wims[ctx->wimboot.num_wims].blob_table_hash);
714         if (ret)
715                 return ret;
716
717         ret = wimboot_alloc_data_source_id(wim->filename,
718                                            wim->hdr.guid,
719                                            ctx->common.wim->current_image,
720                                            ctx->common.target,
721                                            &ctx->wimboot.wims[ctx->wimboot.num_wims].data_source_id,
722                                            &ctx->wimboot.wof_running);
723         if (ret)
724                 return ret;
725
726         ctx->wimboot.num_wims++;
727         return 0;
728 }
729
730 /* Prepare for doing a "WIMBoot" extraction by registering each source WIM file
731  * with WOF on the target volume.  */
732 static int
733 start_wimboot_extraction(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
734 {
735         int ret;
736         struct wim_dentry *dentry;
737
738         if (!xml_get_wimboot(ctx->common.wim->xml_info,
739                              ctx->common.wim->current_image))
740                 WARNING("The WIM image is not marked as WIMBoot compatible.  This usually\n"
741                         "          means it is not intended to be used to back a Windows operating\n"
742                         "          system.  Proceeding anyway.");
743
744         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
745                 struct blob_descriptor *blob;
746
747                 ret = win32_will_back_from_wim(dentry, &ctx->common);
748                 if (ret > 0) /* Error */
749                         return ret;
750                 if (ret < 0) /* Won't externally back */
751                         continue;
752
753                 blob = inode_get_blob_for_unnamed_data_stream_resolved(dentry->d_inode);
754                 ret = register_wim_with_wof(blob->rdesc->wim, ctx);
755                 if (ret)
756                         return ret;
757         }
758
759         if (ctx->wimboot.have_wrong_version_wims) {
760   WARNING("At least one of the source WIM files uses a version of the WIM\n"
761 "          file format that not supported by Microsoft's wof.sys driver.\n"
762 "          Files whose data is contained in one of these WIM files will be\n"
763 "          extracted as full files rather than externally backed.");
764         }
765
766         if (ctx->wimboot.have_uncompressed_wims) {
767   WARNING("At least one of the source WIM files is uncompressed.  Files whose\n"
768 "          data is contained in an uncompressed WIM file will be extracted as\n"
769 "          full files rather than externally backed, since uncompressed WIM\n"
770 "          files are not supported by Microsoft's wof.sys driver.");
771         }
772
773         if (ctx->wimboot.have_unsupported_compressed_resources) {
774   WARNING("At least one of the source WIM files uses a compression format that\n"
775 "          is not supported by Microsoft's wof.sys driver.  Files whose data is\n"
776 "          contained in a compressed resource in one of these WIM files will be\n"
777 "          extracted as full files rather than externally backed.  (The\n"
778 "          compression formats supported by wof.sys are: XPRESS 4K, XPRESS 8K,\n"
779 "          XPRESS 16K, XPRESS 32K, and LZX 32K.)");
780         }
781
782         if (ctx->wimboot.have_huge_resources) {
783   WARNING("Some files exceeded 4.2 GB in size.  Such files will be extracted\n"
784 "          as full files rather than externally backed, since very large files\n"
785 "          are not supported by Microsoft's wof.sys driver.");
786         }
787
788         return 0;
789 }
790
791 static void
792 build_win32_extraction_path(const struct wim_dentry *dentry,
793                             struct win32_apply_ctx *ctx);
794
795 /* Sets WimBoot=1 in the extracted SYSTEM registry hive.
796  *
797  * WIMGAPI does this, and it's possible that it's important.
798  * But I don't know exactly what this value means to Windows.  */
799 static int
800 end_wimboot_extraction(struct win32_apply_ctx *ctx)
801 {
802         struct wim_dentry *dentry;
803         wchar_t subkeyname[32];
804         LONG res;
805         LONG res2;
806         HKEY key;
807         DWORD value;
808
809         dentry = get_dentry(ctx->common.wim, L"\\Windows\\System32\\config\\SYSTEM",
810                             WIMLIB_CASE_INSENSITIVE);
811
812         if (!dentry || !will_extract_dentry(dentry))
813                 goto out;
814
815         if (!will_extract_dentry(wim_get_current_root_dentry(ctx->common.wim)))
816                 goto out;
817
818         /* Not bothering to use the native routines (e.g. NtLoadKey()) for this.
819          * If this doesn't work, you probably also have many other problems.  */
820
821         build_win32_extraction_path(dentry, ctx);
822
823         get_random_alnum_chars(subkeyname, 20);
824         subkeyname[20] = L'\0';
825
826         res = RegLoadKey(HKEY_LOCAL_MACHINE, subkeyname, ctx->pathbuf.Buffer);
827         if (res)
828                 goto out_check_res;
829
830         wcscpy(&subkeyname[20], L"\\Setup");
831
832         res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkeyname, 0, NULL,
833                              REG_OPTION_BACKUP_RESTORE, 0, NULL, &key, NULL);
834         if (res)
835                 goto out_unload_key;
836
837         value = 1;
838
839         res = RegSetValueEx(key, L"WimBoot", 0, REG_DWORD,
840                             (const BYTE *)&value, sizeof(DWORD));
841         if (res)
842                 goto out_close_key;
843
844         res = RegFlushKey(key);
845
846 out_close_key:
847         res2 = RegCloseKey(key);
848         if (!res)
849                 res = res2;
850 out_unload_key:
851         subkeyname[20] = L'\0';
852         RegUnLoadKey(HKEY_LOCAL_MACHINE, subkeyname);
853 out_check_res:
854         if (res) {
855                 /* Warning only.  */
856                 win32_warning(res, L"Failed to set \\Setup: dword \"WimBoot\"=1 "
857                               "value in registry hive \"%ls\"",
858                               ctx->pathbuf.Buffer);
859         }
860 out:
861         return 0;
862 }
863
864 /* Returns the number of wide characters needed to represent the path to the
865  * specified @dentry, relative to the target directory, when extracted.
866  *
867  * Does not include null terminator (not needed for NtCreateFile).  */
868 static size_t
869 dentry_extraction_path_length(const struct wim_dentry *dentry)
870 {
871         size_t len = 0;
872         const struct wim_dentry *d;
873
874         d = dentry;
875         do {
876                 len += d->d_extraction_name_nchars + 1;
877                 d = d->d_parent;
878         } while (!dentry_is_root(d) && will_extract_dentry(d));
879
880         return --len;  /* No leading slash  */
881 }
882
883 /* Returns the length of the longest string that might need to be appended to
884  * the path to an alias of an inode to open or create a named data stream.
885  *
886  * If the inode has no named data streams, this will be 0.  Otherwise, this will
887  * be 1 plus the length of the longest-named data stream, since the data stream
888  * name must be separated from the path by the ':' character.  */
889 static size_t
890 inode_longest_named_data_stream_spec(const struct wim_inode *inode)
891 {
892         size_t max = 0;
893         for (unsigned i = 0; i < inode->i_num_streams; i++) {
894                 const struct wim_inode_stream *strm = &inode->i_streams[i];
895                 if (!stream_is_named_data_stream(strm))
896                         continue;
897                 size_t len = utf16le_len_chars(strm->stream_name);
898                 if (len > max)
899                         max = len;
900         }
901         if (max)
902                 max += 1;
903         return max;
904 }
905
906 /* Find the length, in wide characters, of the longest path needed for
907  * extraction of any file in @dentry_list relative to the target directory.
908  *
909  * Accounts for named data streams, but does not include null terminator (not
910  * needed for NtCreateFile).  */
911 static size_t
912 compute_path_max(struct list_head *dentry_list)
913 {
914         size_t max = 0;
915         const struct wim_dentry *dentry;
916
917         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
918                 size_t len;
919
920                 len = dentry_extraction_path_length(dentry);
921
922                 /* Account for named data streams  */
923                 len += inode_longest_named_data_stream_spec(dentry->d_inode);
924
925                 if (len > max)
926                         max = len;
927         }
928
929         return max;
930 }
931
932 /* Build the path at which to extract the @dentry, relative to the target
933  * directory.
934  *
935  * The path is saved in ctx->pathbuf.  */
936 static void
937 build_extraction_path(const struct wim_dentry *dentry,
938                       struct win32_apply_ctx *ctx)
939 {
940         size_t len;
941         wchar_t *p;
942         const struct wim_dentry *d;
943
944         len = dentry_extraction_path_length(dentry);
945
946         ctx->pathbuf.Length = len * sizeof(wchar_t);
947         p = ctx->pathbuf.Buffer + len;
948         for (d = dentry;
949              !dentry_is_root(d->d_parent) && will_extract_dentry(d->d_parent);
950              d = d->d_parent)
951         {
952                 p -= d->d_extraction_name_nchars;
953                 if (d->d_extraction_name_nchars)
954                         wmemcpy(p, d->d_extraction_name,
955                                 d->d_extraction_name_nchars);
956                 *--p = '\\';
957         }
958         /* No leading slash  */
959         p -= d->d_extraction_name_nchars;
960         wmemcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
961 }
962
963 /* Build the path at which to extract the @dentry, relative to the target
964  * directory, adding the suffix for a named data stream.
965  *
966  * The path is saved in ctx->pathbuf.  */
967 static void
968 build_extraction_path_with_ads(const struct wim_dentry *dentry,
969                                struct win32_apply_ctx *ctx,
970                                const wchar_t *stream_name,
971                                size_t stream_name_nchars)
972 {
973         wchar_t *p;
974
975         build_extraction_path(dentry, ctx);
976
977         /* Add :NAME for named data stream  */
978         p = ctx->pathbuf.Buffer + (ctx->pathbuf.Length / sizeof(wchar_t));
979         *p++ = L':';
980         wmemcpy(p, stream_name, stream_name_nchars);
981         ctx->pathbuf.Length += (1 + stream_name_nchars) * sizeof(wchar_t);
982 }
983
984 /* Build the Win32 namespace path to the specified @dentry when extracted.
985  *
986  * The path is saved in ctx->pathbuf and will be null terminated.
987  *
988  * XXX: We could get rid of this if it wasn't needed for the file encryption
989  * APIs, and the registry manipulation in WIMBoot mode.  */
990 static void
991 build_win32_extraction_path(const struct wim_dentry *dentry,
992                             struct win32_apply_ctx *ctx)
993 {
994         build_extraction_path(dentry, ctx);
995
996         /* Prepend target_ntpath to our relative path, then change \??\ into \\?\  */
997
998         memmove(ctx->pathbuf.Buffer +
999                         (ctx->target_ntpath.Length / sizeof(wchar_t)) + 1,
1000                 ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1001         memcpy(ctx->pathbuf.Buffer, ctx->target_ntpath.Buffer,
1002                 ctx->target_ntpath.Length);
1003         ctx->pathbuf.Buffer[ctx->target_ntpath.Length / sizeof(wchar_t)] = L'\\';
1004         ctx->pathbuf.Length += ctx->target_ntpath.Length + sizeof(wchar_t);
1005         ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)] = L'\0';
1006
1007         wimlib_assert(ctx->pathbuf.Length >= 4 * sizeof(wchar_t) &&
1008                       !wmemcmp(ctx->pathbuf.Buffer, L"\\??\\", 4));
1009
1010         ctx->pathbuf.Buffer[1] = L'\\';
1011
1012 }
1013
1014 /* Returns a "printable" representation of the last relative NT path that was
1015  * constructed with build_extraction_path() or build_extraction_path_with_ads().
1016  *
1017  * This will be overwritten by the next call to this function.  */
1018 static const wchar_t *
1019 current_path(struct win32_apply_ctx *ctx)
1020 {
1021         wchar_t *p = ctx->print_buffer;
1022
1023         p = wmempcpy(p, ctx->common.target, ctx->common.target_nchars);
1024         *p++ = L'\\';
1025         p = wmempcpy(p, ctx->pathbuf.Buffer, ctx->pathbuf.Length / sizeof(wchar_t));
1026         *p = L'\0';
1027         return ctx->print_buffer;
1028 }
1029
1030 /* Open handle to the target directory if it is not already open.  If the target
1031  * directory does not exist, this creates it.  */
1032 static int
1033 open_target_directory(struct win32_apply_ctx *ctx)
1034 {
1035         NTSTATUS status;
1036
1037         if (ctx->h_target)
1038                 return 0;
1039
1040         ctx->attr.Length = sizeof(ctx->attr);
1041         ctx->attr.RootDirectory = NULL;
1042         ctx->attr.ObjectName = &ctx->target_ntpath;
1043
1044         /* Don't use FILE_OPEN_REPARSE_POINT here; we want the extraction to
1045          * happen at the directory "pointed to" by the reparse point. */
1046         status = NtCreateFile(&ctx->h_target,
1047                               FILE_TRAVERSE,
1048                               &ctx->attr,
1049                               &ctx->iosb,
1050                               NULL,
1051                               0,
1052                               FILE_SHARE_VALID_FLAGS,
1053                               FILE_OPEN_IF,
1054                               FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT,
1055                               NULL,
1056                               0);
1057         if (!NT_SUCCESS(status)) {
1058                 winnt_error(status, L"Can't open or create directory \"%ls\"",
1059                             ctx->common.target);
1060                 return WIMLIB_ERR_OPENDIR;
1061         }
1062         ctx->attr.RootDirectory = ctx->h_target;
1063         ctx->attr.ObjectName = &ctx->pathbuf;
1064         return 0;
1065 }
1066
1067 static void
1068 close_target_directory(struct win32_apply_ctx *ctx)
1069 {
1070         if (ctx->h_target) {
1071                 NtClose(ctx->h_target);
1072                 ctx->h_target = NULL;
1073                 ctx->attr.RootDirectory = NULL;
1074         }
1075 }
1076
1077 /*
1078  * Ensures the target directory exists and opens a handle to it, in preparation
1079  * of using paths relative to it.
1080  */
1081 static int
1082 prepare_target(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1083 {
1084         int ret;
1085         size_t path_max;
1086
1087         ret = win32_path_to_nt_path(ctx->common.target, &ctx->target_ntpath);
1088         if (ret)
1089                 return ret;
1090
1091         ret = open_target_directory(ctx);
1092         if (ret)
1093                 return ret;
1094
1095         path_max = compute_path_max(dentry_list);
1096         /* Add some extra for building Win32 paths for the file encryption APIs,
1097          * and ensure we have at least enough to potentially use an 8.3 name for
1098          * the last component.  */
1099         path_max += max(2 + (ctx->target_ntpath.Length / sizeof(wchar_t)),
1100                         8 + 1 + 3);
1101
1102         ctx->pathbuf.MaximumLength = path_max * sizeof(wchar_t);
1103         if (ctx->pathbuf.MaximumLength != path_max * sizeof(wchar_t)) {
1104                 /* Paths are too long for a UNICODE_STRING! */
1105                 ERROR("Some paths are too long to extract (> 32768 characters)!");
1106                 return WIMLIB_ERR_UNSUPPORTED;
1107         }
1108
1109         ctx->pathbuf.Buffer = MALLOC(ctx->pathbuf.MaximumLength);
1110         if (!ctx->pathbuf.Buffer)
1111                 return WIMLIB_ERR_NOMEM;
1112
1113         ctx->print_buffer = MALLOC((ctx->common.target_nchars + 1 + path_max + 1) *
1114                                    sizeof(wchar_t));
1115         if (!ctx->print_buffer)
1116                 return WIMLIB_ERR_NOMEM;
1117
1118         return 0;
1119 }
1120
1121 /* When creating an inode that will have a short (DOS) name, we create it using
1122  * the long name associated with the short name.  This ensures that the short
1123  * name gets associated with the correct long name.  */
1124 static struct wim_dentry *
1125 first_extraction_alias(const struct wim_inode *inode)
1126 {
1127         struct wim_dentry *dentry;
1128
1129         inode_for_each_extraction_alias(dentry, inode)
1130                 if (dentry_has_short_name(dentry))
1131                         return dentry;
1132         return inode_first_extraction_dentry(inode);
1133 }
1134
1135 /*
1136  * Set or clear FILE_ATTRIBUTE_COMPRESSED if the inherited value is different
1137  * from the desired value.
1138  *
1139  * Note that you can NOT override the inherited value of
1140  * FILE_ATTRIBUTE_COMPRESSED directly with NtCreateFile().
1141  */
1142 static int
1143 adjust_compression_attribute(HANDLE h, const struct wim_dentry *dentry,
1144                              struct win32_apply_ctx *ctx)
1145 {
1146         const bool compressed = (dentry->d_inode->i_attributes &
1147                                  FILE_ATTRIBUTE_COMPRESSED);
1148         FILE_BASIC_INFORMATION info;
1149         USHORT compression_state;
1150         NTSTATUS status;
1151
1152         if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)
1153                 return 0;
1154
1155         if (!ctx->common.supported_features.compressed_files)
1156                 return 0;
1157
1158
1159         /* Get current attributes  */
1160         status = NtQueryInformationFile(h, &ctx->iosb, &info, sizeof(info),
1161                                         FileBasicInformation);
1162         if (NT_SUCCESS(status) &&
1163             compressed == !!(info.FileAttributes & FILE_ATTRIBUTE_COMPRESSED))
1164         {
1165                 /* Nothing needs to be done.  */
1166                 return 0;
1167         }
1168
1169         /* Set the new compression state  */
1170
1171         if (compressed)
1172                 compression_state = COMPRESSION_FORMAT_DEFAULT;
1173         else
1174                 compression_state = COMPRESSION_FORMAT_NONE;
1175
1176         status = winnt_fsctl(h, FSCTL_SET_COMPRESSION,
1177                              &compression_state, sizeof(USHORT), NULL, 0, NULL);
1178         if (NT_SUCCESS(status))
1179                 return 0;
1180
1181         winnt_error(status, L"Can't %s compression attribute on \"%ls\"",
1182                     (compressed ? "set" : "clear"), current_path(ctx));
1183         return WIMLIB_ERR_SET_ATTRIBUTES;
1184 }
1185
1186 static bool
1187 need_sparse_flag(const struct wim_inode *inode,
1188                  const struct win32_apply_ctx *ctx)
1189 {
1190         return (inode->i_attributes & FILE_ATTRIBUTE_SPARSE_FILE) &&
1191                 ctx->common.supported_features.sparse_files;
1192 }
1193
1194 static int
1195 set_sparse_flag(HANDLE h, struct win32_apply_ctx *ctx)
1196 {
1197         NTSTATUS status;
1198
1199         status = winnt_fsctl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, NULL);
1200         if (NT_SUCCESS(status))
1201                 return 0;
1202
1203         winnt_error(status, L"Can't set sparse flag on \"%ls\"",
1204                     current_path(ctx));
1205         return WIMLIB_ERR_SET_ATTRIBUTES;
1206 }
1207
1208 /* Try to enable short name support on the target volume.  If successful, return
1209  * true.  If unsuccessful, issue a warning and return false.  */
1210 static bool
1211 try_to_enable_short_names(const wchar_t *volume)
1212 {
1213         HANDLE h;
1214         FILE_FS_PERSISTENT_VOLUME_INFORMATION info;
1215         BOOL bret;
1216         DWORD bytesReturned;
1217
1218         h = CreateFile(volume, GENERIC_WRITE,
1219                        FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
1220                        FILE_FLAG_BACKUP_SEMANTICS, NULL);
1221         if (h == INVALID_HANDLE_VALUE)
1222                 goto fail;
1223
1224         info.VolumeFlags = 0;
1225         info.FlagMask = PERSISTENT_VOLUME_STATE_SHORT_NAME_CREATION_DISABLED;
1226         info.Version = 1;
1227         info.Reserved = 0;
1228
1229         bret = DeviceIoControl(h, FSCTL_SET_PERSISTENT_VOLUME_STATE,
1230                                &info, sizeof(info), NULL, 0,
1231                                &bytesReturned, NULL);
1232
1233         CloseHandle(h);
1234
1235         if (!bret)
1236                 goto fail;
1237         return true;
1238
1239 fail:
1240         win32_warning(GetLastError(),
1241                       L"Failed to enable short name support on %ls",
1242                       volume + 4);
1243         return false;
1244 }
1245
1246 static NTSTATUS
1247 remove_conflicting_short_name(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1248 {
1249         wchar_t *name;
1250         wchar_t *end;
1251         NTSTATUS status;
1252         HANDLE h;
1253         size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1254                          (13 * sizeof(wchar_t));
1255         u8 buf[bufsize] _aligned_attribute(8);
1256         bool retried = false;
1257         FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1258
1259         memset(buf, 0, bufsize);
1260
1261         /* Build the path with the short name.  */
1262         name = &ctx->pathbuf.Buffer[ctx->pathbuf.Length / sizeof(wchar_t)];
1263         while (name != ctx->pathbuf.Buffer && *(name - 1) != L'\\')
1264                 name--;
1265         end = mempcpy(name, dentry->d_short_name, dentry->d_short_name_nbytes);
1266         ctx->pathbuf.Length = ((u8 *)end - (u8 *)ctx->pathbuf.Buffer);
1267
1268         /* Open the conflicting file (by short name).  */
1269         status = NtOpenFile(&h, GENERIC_WRITE | DELETE,
1270                             &ctx->attr, &ctx->iosb,
1271                             FILE_SHARE_VALID_FLAGS,
1272                             FILE_OPEN_REPARSE_POINT | FILE_OPEN_FOR_BACKUP_INTENT);
1273         if (!NT_SUCCESS(status)) {
1274                 winnt_warning(status, L"Can't open \"%ls\"", current_path(ctx));
1275                 goto out;
1276         }
1277
1278 #if 0
1279         WARNING("Overriding conflicting short name; path=\"%ls\"",
1280                 current_path(ctx));
1281 #endif
1282
1283         /* Try to remove the short name on the conflicting file.  */
1284
1285 retry:
1286         status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1287                                       FileShortNameInformation);
1288
1289         if (status == STATUS_INVALID_PARAMETER && !retried) {
1290                 /* Microsoft forgot to make it possible to remove short names
1291                  * until Windows 7.  Oops.  Use a random short name instead.  */
1292                 get_random_alnum_chars(info->FileName, 8);
1293                 wcscpy(&info->FileName[8], L".WLB");
1294                 info->FileNameLength = 12 * sizeof(wchar_t);
1295                 retried = true;
1296                 goto retry;
1297         }
1298         NtClose(h);
1299 out:
1300         build_extraction_path(dentry, ctx);
1301         return status;
1302 }
1303
1304 /* Set the short name on the open file @h which has been created at the location
1305  * indicated by @dentry.
1306  *
1307  * Note that this may add, change, or remove the short name.
1308  *
1309  * @h must be opened with DELETE access.
1310  *
1311  * Returns 0 or WIMLIB_ERR_SET_SHORT_NAME.  The latter only happens in
1312  * STRICT_SHORT_NAMES mode.
1313  */
1314 static int
1315 set_short_name(HANDLE h, const struct wim_dentry *dentry,
1316                struct win32_apply_ctx *ctx)
1317 {
1318
1319         if (!ctx->common.supported_features.short_names)
1320                 return 0;
1321
1322         /*
1323          * Note: The size of the FILE_NAME_INFORMATION buffer must be such that
1324          * FileName contains at least 2 wide characters (4 bytes).  Otherwise,
1325          * NtSetInformationFile() will return STATUS_INFO_LENGTH_MISMATCH.  This
1326          * is despite the fact that FileNameLength can validly be 0 or 2 bytes,
1327          * with the former case being removing the existing short name if
1328          * present, rather than setting one.
1329          *
1330          * The null terminator is seemingly optional, but to be safe we include
1331          * space for it and zero all unused space.
1332          */
1333
1334         size_t bufsize = offsetof(FILE_NAME_INFORMATION, FileName) +
1335                          max(dentry->d_short_name_nbytes, sizeof(wchar_t)) +
1336                          sizeof(wchar_t);
1337         u8 buf[bufsize] _aligned_attribute(8);
1338         FILE_NAME_INFORMATION *info = (FILE_NAME_INFORMATION *)buf;
1339         NTSTATUS status;
1340         bool tried_to_remove_existing = false;
1341
1342         memset(buf, 0, bufsize);
1343
1344         info->FileNameLength = dentry->d_short_name_nbytes;
1345         memcpy(info->FileName, dentry->d_short_name, dentry->d_short_name_nbytes);
1346
1347 retry:
1348         status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1349                                       FileShortNameInformation);
1350         if (NT_SUCCESS(status))
1351                 return 0;
1352
1353         if (status == STATUS_SHORT_NAMES_NOT_ENABLED_ON_VOLUME) {
1354                 if (dentry->d_short_name_nbytes == 0)
1355                         return 0;
1356                 if (!ctx->tried_to_enable_short_names) {
1357                         wchar_t volume[7];
1358                         int ret;
1359
1360                         ctx->tried_to_enable_short_names = true;
1361
1362                         ret = win32_get_drive_path(ctx->common.target,
1363                                                    volume);
1364                         if (ret)
1365                                 return ret;
1366                         if (try_to_enable_short_names(volume))
1367                                 goto retry;
1368                 }
1369         }
1370
1371         /*
1372          * Short names can conflict in several cases:
1373          *
1374          * - a file being extracted has a short name conflicting with an
1375          *   existing file
1376          *
1377          * - a file being extracted has a short name conflicting with another
1378          *   file being extracted (possible, but shouldn't happen)
1379          *
1380          * - a file being extracted has a short name that conflicts with the
1381          *   automatically generated short name of a file we previously
1382          *   extracted, but failed to set the short name for.  Sounds unlikely,
1383          *   but this actually does happen fairly often on versions of Windows
1384          *   prior to Windows 7 because they do not support removing short names
1385          *   from files.
1386          */
1387         if (unlikely(status == STATUS_OBJECT_NAME_COLLISION) &&
1388             dentry->d_short_name_nbytes && !tried_to_remove_existing)
1389         {
1390                 tried_to_remove_existing = true;
1391                 status = remove_conflicting_short_name(dentry, ctx);
1392                 if (NT_SUCCESS(status))
1393                         goto retry;
1394         }
1395
1396         /* By default, failure to set short names is not an error (since short
1397          * names aren't too important anymore...).  */
1398         if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SHORT_NAMES)) {
1399                 if (dentry->d_short_name_nbytes)
1400                         ctx->num_set_short_name_failures++;
1401                 else
1402                         ctx->num_remove_short_name_failures++;
1403                 return 0;
1404         }
1405
1406         winnt_error(status, L"Can't set short name on \"%ls\"", current_path(ctx));
1407         return WIMLIB_ERR_SET_SHORT_NAME;
1408 }
1409
1410 /*
1411  * A wrapper around NtCreateFile() to make it slightly more usable...
1412  * This uses the path currently constructed in ctx->pathbuf.
1413  *
1414  * Also, we always specify SYNCHRONIZE access, FILE_OPEN_FOR_BACKUP_INTENT, and
1415  * FILE_OPEN_REPARSE_POINT.
1416  */
1417 static NTSTATUS
1418 do_create_file(PHANDLE FileHandle,
1419                ACCESS_MASK DesiredAccess,
1420                PLARGE_INTEGER AllocationSize,
1421                ULONG FileAttributes,
1422                ULONG CreateDisposition,
1423                ULONG CreateOptions,
1424                struct win32_apply_ctx *ctx)
1425 {
1426         return NtCreateFile(FileHandle,
1427                             DesiredAccess | SYNCHRONIZE,
1428                             &ctx->attr,
1429                             &ctx->iosb,
1430                             AllocationSize,
1431                             FileAttributes,
1432                             FILE_SHARE_VALID_FLAGS,
1433                             CreateDisposition,
1434                             CreateOptions |
1435                                 FILE_OPEN_FOR_BACKUP_INTENT |
1436                                 FILE_OPEN_REPARSE_POINT,
1437                             NULL,
1438                             0);
1439 }
1440
1441 /* Like do_create_file(), but builds the extraction path of the @dentry first.
1442  */
1443 static NTSTATUS
1444 create_file(PHANDLE FileHandle,
1445             ACCESS_MASK DesiredAccess,
1446             PLARGE_INTEGER AllocationSize,
1447             ULONG FileAttributes,
1448             ULONG CreateDisposition,
1449             ULONG CreateOptions,
1450             const struct wim_dentry *dentry,
1451             struct win32_apply_ctx *ctx)
1452 {
1453         build_extraction_path(dentry, ctx);
1454         return do_create_file(FileHandle,
1455                               DesiredAccess,
1456                               AllocationSize,
1457                               FileAttributes,
1458                               CreateDisposition,
1459                               CreateOptions,
1460                               ctx);
1461 }
1462
1463 static int
1464 delete_file_or_stream(struct win32_apply_ctx *ctx)
1465 {
1466         NTSTATUS status;
1467         HANDLE h;
1468         ULONG perms = DELETE;
1469         ULONG flags = FILE_NON_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE;
1470
1471         /* First try opening the file with FILE_DELETE_ON_CLOSE.  In most cases,
1472          * all we have to do is that plus close the file handle.  */
1473 retry:
1474         status = do_create_file(&h, perms, NULL, 0, FILE_OPEN, flags, ctx);
1475
1476         if (unlikely(status == STATUS_CANNOT_DELETE)) {
1477                 /* This error occurs for files with FILE_ATTRIBUTE_READONLY set.
1478                  * Try an alternate approach: first open the file without
1479                  * FILE_DELETE_ON_CLOSE, then reset the file attributes, then
1480                  * set the "delete" disposition on the handle.  */
1481                 if (flags & FILE_DELETE_ON_CLOSE) {
1482                         flags &= ~FILE_DELETE_ON_CLOSE;
1483                         perms |= FILE_WRITE_ATTRIBUTES;
1484                         goto retry;
1485                 }
1486         }
1487
1488         if (unlikely(!NT_SUCCESS(status))) {
1489                 winnt_error(status, L"Can't open \"%ls\" for deletion "
1490                             "(perms=%x, flags=%x)",
1491                             current_path(ctx), perms, flags);
1492                 return WIMLIB_ERR_OPEN;
1493         }
1494
1495         if (unlikely(!(flags & FILE_DELETE_ON_CLOSE))) {
1496
1497                 FILE_BASIC_INFORMATION basic_info =
1498                         { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1499                 status = NtSetInformationFile(h, &ctx->iosb, &basic_info,
1500                                               sizeof(basic_info),
1501                                               FileBasicInformation);
1502
1503                 if (!NT_SUCCESS(status)) {
1504                         winnt_error(status, L"Can't reset attributes of \"%ls\" "
1505                                     "to prepare for deletion", current_path(ctx));
1506                         NtClose(h);
1507                         return WIMLIB_ERR_SET_ATTRIBUTES;
1508                 }
1509
1510                 FILE_DISPOSITION_INFORMATION disp_info =
1511                         { .DoDeleteFile = TRUE };
1512                 status = NtSetInformationFile(h, &ctx->iosb, &disp_info,
1513                                               sizeof(disp_info),
1514                                               FileDispositionInformation);
1515                 if (!NT_SUCCESS(status)) {
1516                         winnt_error(status, L"Can't set delete-on-close "
1517                                     "disposition on \"%ls\"", current_path(ctx));
1518                         NtClose(h);
1519                         return WIMLIB_ERR_SET_ATTRIBUTES;
1520                 }
1521         }
1522
1523         status = NtClose(h);
1524         if (unlikely(!NT_SUCCESS(status))) {
1525                 winnt_error(status, L"Error closing \"%ls\" after setting "
1526                             "delete-on-close disposition", current_path(ctx));
1527                 return WIMLIB_ERR_OPEN;
1528         }
1529
1530         return 0;
1531 }
1532
1533 /*
1534  * Create a nondirectory file or named data stream at the current path,
1535  * superseding any that already exists at that path.  If successful, return an
1536  * open handle to the file or named data stream with the requested permissions.
1537  */
1538 static int
1539 supersede_file_or_stream(struct win32_apply_ctx *ctx, DWORD perms,
1540                          HANDLE *h_ret)
1541 {
1542         NTSTATUS status;
1543         bool retried = false;
1544
1545         /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1546          * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be.  */
1547 retry:
1548         status = do_create_file(h_ret,
1549                                 perms,
1550                                 NULL,
1551                                 FILE_ATTRIBUTE_SYSTEM,
1552                                 FILE_CREATE,
1553                                 FILE_NON_DIRECTORY_FILE,
1554                                 ctx);
1555         if (likely(NT_SUCCESS(status)))
1556                 return 0;
1557
1558         /* STATUS_OBJECT_NAME_COLLISION means that the file or stream already
1559          * exists.  Delete the existing file or stream, then try again.
1560          *
1561          * Note: we don't use FILE_OVERWRITE_IF or FILE_SUPERSEDE because of
1562          * problems with certain file attributes, especially
1563          * FILE_ATTRIBUTE_ENCRYPTED.  FILE_SUPERSEDE is also broken in the
1564          * Windows PE ramdisk.  */
1565         if (status == STATUS_OBJECT_NAME_COLLISION && !retried) {
1566                 int ret = delete_file_or_stream(ctx);
1567                 if (ret)
1568                         return ret;
1569                 retried = true;
1570                 goto retry;
1571         }
1572         winnt_error(status, L"Can't create \"%ls\"", current_path(ctx));
1573         return WIMLIB_ERR_OPEN;
1574 }
1575
1576 /* Set the reparse point @rpbuf of length @rpbuflen on the extracted file
1577  * corresponding to the WIM dentry @dentry.  */
1578 static int
1579 do_set_reparse_point(const struct wim_dentry *dentry,
1580                      const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
1581                      struct win32_apply_ctx *ctx)
1582 {
1583         NTSTATUS status;
1584         HANDLE h;
1585
1586         status = create_file(&h, GENERIC_WRITE, NULL,
1587                              0, FILE_OPEN, 0, dentry, ctx);
1588         if (!NT_SUCCESS(status))
1589                 goto fail;
1590
1591         status = winnt_fsctl(h, FSCTL_SET_REPARSE_POINT,
1592                              rpbuf, rpbuflen, NULL, 0, NULL);
1593         NtClose(h);
1594
1595         if (NT_SUCCESS(status))
1596                 return 0;
1597
1598         /* On Windows, by default only the Administrator can create symbolic
1599          * links for some reason.  By default we just issue a warning if this
1600          * appears to be the problem.  Use WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS
1601          * to get a hard error.  */
1602         if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_SYMLINKS)
1603             && (status == STATUS_PRIVILEGE_NOT_HELD ||
1604                 status == STATUS_ACCESS_DENIED)
1605             && (dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
1606                 dentry->d_inode->i_reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
1607         {
1608                 WARNING("Can't create symbolic link \"%ls\"!              \n"
1609                         "          (Need Administrator rights, or at least "
1610                         "the\n"
1611                         "          SeCreateSymbolicLink privilege.)",
1612                         current_path(ctx));
1613                 return 0;
1614         }
1615
1616 fail:
1617         winnt_error(status, L"Can't set reparse data on \"%ls\"",
1618                     current_path(ctx));
1619         return WIMLIB_ERR_SET_REPARSE_DATA;
1620 }
1621
1622 /*
1623  * Create empty named data streams and potentially a reparse point for the
1624  * specified file, if any.
1625  *
1626  * Since these won't have blob descriptors, they won't show up in the call to
1627  * extract_blob_list().  Hence the need for the special case.
1628  */
1629 static int
1630 create_empty_streams(const struct wim_dentry *dentry,
1631                      struct win32_apply_ctx *ctx)
1632 {
1633         const struct wim_inode *inode = dentry->d_inode;
1634         int ret;
1635
1636         for (unsigned i = 0; i < inode->i_num_streams; i++) {
1637                 const struct wim_inode_stream *strm = &inode->i_streams[i];
1638
1639                 if (stream_blob_resolved(strm) != NULL)
1640                         continue;
1641
1642                 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT &&
1643                     ctx->common.supported_features.reparse_points)
1644                 {
1645                         u8 buf[REPARSE_DATA_OFFSET] _aligned_attribute(8);
1646                         struct reparse_buffer_disk *rpbuf =
1647                                 (struct reparse_buffer_disk *)buf;
1648                         complete_reparse_point(rpbuf, inode, 0);
1649                         ret = do_set_reparse_point(dentry, rpbuf,
1650                                                    REPARSE_DATA_OFFSET, ctx);
1651                         if (ret)
1652                                 return ret;
1653                 } else if (stream_is_named_data_stream(strm) &&
1654                            ctx->common.supported_features.named_data_streams)
1655                 {
1656                         HANDLE h;
1657
1658                         build_extraction_path_with_ads(dentry, ctx,
1659                                                        strm->stream_name,
1660                                                        utf16le_len_chars(strm->stream_name));
1661                         /*
1662                          * Note: do not request any permissions on the handle.
1663                          * Otherwise, we may encounter a Windows bug where the
1664                          * parent directory DACL denies read access to the new
1665                          * named data stream, even when using backup semantics!
1666                          */
1667                         ret = supersede_file_or_stream(ctx, 0, &h);
1668
1669                         build_extraction_path(dentry, ctx);
1670
1671                         if (ret)
1672                                 return ret;
1673                         NtClose(h);
1674                 }
1675         }
1676
1677         return 0;
1678 }
1679
1680 /*
1681  * Creates the directory named by @dentry, or uses an existing directory at that
1682  * location.  If necessary, sets the short name and/or fixes compression and
1683  * encryption attributes.
1684  *
1685  * Returns 0, WIMLIB_ERR_MKDIR, or WIMLIB_ERR_SET_SHORT_NAME.
1686  */
1687 static int
1688 create_directory(const struct wim_dentry *dentry, struct win32_apply_ctx *ctx)
1689 {
1690         DWORD perms;
1691         NTSTATUS status;
1692         HANDLE h;
1693         int ret;
1694
1695         /* DELETE is needed for set_short_name(); GENERIC_READ and GENERIC_WRITE
1696          * are needed for adjust_compression_attribute().  */
1697         perms = GENERIC_READ | GENERIC_WRITE;
1698         if (!dentry_is_root(dentry))
1699                 perms |= DELETE;
1700
1701         /* FILE_ATTRIBUTE_SYSTEM is needed to ensure that
1702          * FILE_ATTRIBUTE_ENCRYPTED doesn't get set before we want it to be.  */
1703         status = create_file(&h, perms, NULL, FILE_ATTRIBUTE_SYSTEM,
1704                              FILE_OPEN_IF, FILE_DIRECTORY_FILE, dentry, ctx);
1705         if (unlikely(!NT_SUCCESS(status))) {
1706                 const wchar_t *path = current_path(ctx);
1707                 winnt_error(status, L"Can't create directory \"%ls\"", path);
1708
1709                 /* Check for known issue with WindowsApps directory.  */
1710                 if (status == STATUS_ACCESS_DENIED &&
1711                     (wcsstr(path, L"\\WindowsApps\\") ||
1712                      wcsstr(path, L"\\InfusedApps\\"))) {
1713                         ERROR(
1714 "You seem to be trying to extract files to the WindowsApps directory.\n"
1715 "        Windows 8.1 and later use new file permissions in this directory that\n"
1716 "        cannot be overridden, even by backup/restore programs.  To extract your\n"
1717 "        files anyway, you need to choose a different target directory, delete\n"
1718 "        the WindowsApps directory entirely, reformat the volume, do the\n"
1719 "        extraction from a non-broken operating system such as Windows 7 or\n"
1720 "        Linux, or wait for Microsoft to fix the design flaw in their operating\n"
1721 "        system.  This is *not* a bug in wimlib.  See this thread for more\n"
1722 "        information: https://wimlib.net/forums/viewtopic.php?f=1&t=261");
1723                 }
1724                 return WIMLIB_ERR_MKDIR;
1725         }
1726
1727         if (ctx->iosb.Information == FILE_OPENED) {
1728                 /* If we opened an existing directory, try to clear its file
1729                  * attributes.  As far as I know, this only actually makes a
1730                  * difference in the case where a FILE_ATTRIBUTE_READONLY
1731                  * directory has a named data stream which needs to be
1732                  * extracted.  You cannot create a named data stream of such a
1733                  * directory, even though this contradicts Microsoft's
1734                  * documentation for FILE_ATTRIBUTE_READONLY which states it is
1735                  * not honored for directories!  */
1736                 if (!(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
1737                         FILE_BASIC_INFORMATION basic_info =
1738                                 { .FileAttributes = FILE_ATTRIBUTE_NORMAL };
1739                         NtSetInformationFile(h, &ctx->iosb, &basic_info,
1740                                              sizeof(basic_info),
1741                                              FileBasicInformation);
1742                 }
1743         }
1744
1745         if (!dentry_is_root(dentry)) {
1746                 ret = set_short_name(h, dentry, ctx);
1747                 if (ret)
1748                         goto out;
1749         }
1750
1751         ret = adjust_compression_attribute(h, dentry, ctx);
1752 out:
1753         NtClose(h);
1754         return ret;
1755 }
1756
1757 /*
1758  * Create all the directories being extracted, other than the target directory
1759  * itself.
1760  *
1761  * Note: we don't honor directory hard links.  However, we don't allow them to
1762  * exist in WIM images anyway (see inode_fixup.c).
1763  */
1764 static int
1765 create_directories(struct list_head *dentry_list,
1766                    struct win32_apply_ctx *ctx)
1767 {
1768         const struct wim_dentry *dentry;
1769         int ret;
1770
1771         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1772
1773                 if (!(dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
1774                         continue;
1775
1776                 /* Note: Here we include files with
1777                  * FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_REPARSE_POINT, but we
1778                  * wait until later to actually set the reparse data.  */
1779
1780                 ret = create_directory(dentry, ctx);
1781
1782                 if (!ret)
1783                         ret = create_empty_streams(dentry, ctx);
1784
1785                 ret = check_apply_error(dentry, ctx, ret);
1786                 if (ret)
1787                         return ret;
1788
1789                 ret = report_file_created(&ctx->common);
1790                 if (ret)
1791                         return ret;
1792         }
1793         return 0;
1794 }
1795
1796 /*
1797  * Creates the nondirectory file named by @dentry.
1798  *
1799  * On success, returns an open handle to the file in @h_ret, with GENERIC_READ,
1800  * GENERIC_WRITE, and DELETE access.  Also, the path to the file will be saved
1801  * in ctx->pathbuf.  On failure, returns an error code.
1802  */
1803 static int
1804 create_nondirectory_inode(HANDLE *h_ret, const struct wim_dentry *dentry,
1805                           struct win32_apply_ctx *ctx)
1806 {
1807         int ret;
1808         HANDLE h;
1809
1810         build_extraction_path(dentry, ctx);
1811
1812         ret = supersede_file_or_stream(ctx,
1813                                        GENERIC_READ | GENERIC_WRITE | DELETE,
1814                                        &h);
1815         if (ret)
1816                 goto out;
1817
1818         ret = adjust_compression_attribute(h, dentry, ctx);
1819         if (ret)
1820                 goto out_close;
1821
1822         if (need_sparse_flag(dentry->d_inode, ctx)) {
1823                 ret = set_sparse_flag(h, ctx);
1824                 if (ret)
1825                         goto out_close;
1826         }
1827
1828         ret = create_empty_streams(dentry, ctx);
1829         if (ret)
1830                 goto out_close;
1831
1832         *h_ret = h;
1833         return 0;
1834
1835 out_close:
1836         NtClose(h);
1837 out:
1838         return ret;
1839 }
1840
1841 /* Creates a hard link at the location named by @dentry to the file represented
1842  * by the open handle @h.  Or, if the target volume does not support hard links,
1843  * create a separate file instead.  */
1844 static int
1845 create_link(HANDLE h, const struct wim_dentry *dentry,
1846             struct win32_apply_ctx *ctx)
1847 {
1848         if (ctx->common.supported_features.hard_links) {
1849
1850                 build_extraction_path(dentry, ctx);
1851
1852                 size_t bufsize = offsetof(FILE_LINK_INFORMATION, FileName) +
1853                                  ctx->pathbuf.Length + sizeof(wchar_t);
1854                 u8 buf[bufsize] _aligned_attribute(8);
1855                 FILE_LINK_INFORMATION *info = (FILE_LINK_INFORMATION *)buf;
1856                 NTSTATUS status;
1857
1858                 info->ReplaceIfExists = TRUE;
1859                 info->RootDirectory = ctx->attr.RootDirectory;
1860                 info->FileNameLength = ctx->pathbuf.Length;
1861                 memcpy(info->FileName, ctx->pathbuf.Buffer, ctx->pathbuf.Length);
1862                 info->FileName[info->FileNameLength / 2] = L'\0';
1863
1864                 /* Note: the null terminator isn't actually necessary,
1865                  * but if you don't add the extra character, you get
1866                  * STATUS_INFO_LENGTH_MISMATCH when FileNameLength
1867                  * happens to be 2  */
1868
1869                 status = NtSetInformationFile(h, &ctx->iosb, info, bufsize,
1870                                               FileLinkInformation);
1871                 if (NT_SUCCESS(status))
1872                         return 0;
1873                 winnt_error(status, L"Failed to create link \"%ls\"",
1874                             current_path(ctx));
1875                 return WIMLIB_ERR_LINK;
1876         } else {
1877                 HANDLE h2;
1878                 int ret;
1879
1880                 ret = create_nondirectory_inode(&h2, dentry, ctx);
1881                 if (ret)
1882                         return ret;
1883
1884                 NtClose(h2);
1885                 return 0;
1886         }
1887 }
1888
1889 /* Given an inode (represented by the open handle @h) for which one link has
1890  * been created (named by @first_dentry), create the other links.
1891  *
1892  * Or, if the target volume does not support hard links, create separate files.
1893  *
1894  * Note: This uses ctx->pathbuf and does not reset it.
1895  */
1896 static int
1897 create_links(HANDLE h, const struct wim_dentry *first_dentry,
1898              struct win32_apply_ctx *ctx)
1899 {
1900         const struct wim_inode *inode = first_dentry->d_inode;
1901         const struct wim_dentry *dentry;
1902         int ret;
1903
1904         inode_for_each_extraction_alias(dentry, inode) {
1905                 if (dentry != first_dentry) {
1906                         ret = create_link(h, dentry, ctx);
1907                         if (ret)
1908                                 return ret;
1909                 }
1910         }
1911         return 0;
1912 }
1913
1914 /* Create a nondirectory file, including all links.  */
1915 static int
1916 create_nondirectory(struct wim_inode *inode, struct win32_apply_ctx *ctx)
1917 {
1918         struct wim_dentry *first_dentry;
1919         HANDLE h;
1920         int ret;
1921
1922         first_dentry = first_extraction_alias(inode);
1923
1924         /* Create first link.  */
1925         ret = create_nondirectory_inode(&h, first_dentry, ctx);
1926         if (ret)
1927                 return ret;
1928
1929         /* Set short name.  */
1930         ret = set_short_name(h, first_dentry, ctx);
1931
1932         /* Create additional links, OR if hard links are not supported just
1933          * create more files.  */
1934         if (!ret)
1935                 ret = create_links(h, first_dentry, ctx);
1936
1937         /* "WIMBoot" extraction: set external backing by the WIM file if needed.  */
1938         if (!ret && unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT))
1939                 ret = set_backed_from_wim(h, inode, ctx);
1940
1941         NtClose(h);
1942         return ret;
1943 }
1944
1945 /* Create all the nondirectory files being extracted, including all aliases
1946  * (hard links).  */
1947 static int
1948 create_nondirectories(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
1949 {
1950         struct wim_dentry *dentry;
1951         struct wim_inode *inode;
1952         int ret;
1953
1954         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1955                 inode = dentry->d_inode;
1956                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
1957                         continue;
1958                 /* Call create_nondirectory() only once per inode  */
1959                 if (dentry == inode_first_extraction_dentry(inode)) {
1960                         ret = create_nondirectory(inode, ctx);
1961                         ret = check_apply_error(dentry, ctx, ret);
1962                         if (ret)
1963                                 return ret;
1964                 }
1965                 ret = report_file_created(&ctx->common);
1966                 if (ret)
1967                         return ret;
1968         }
1969         return 0;
1970 }
1971
1972 static void
1973 close_handles(struct win32_apply_ctx *ctx)
1974 {
1975         for (unsigned i = 0; i < ctx->num_open_handles; i++)
1976                 NtClose(ctx->open_handles[i]);
1977 }
1978
1979 /* Prepare to read the next blob, which has size @blob_size, into an in-memory
1980  * buffer.  */
1981 static bool
1982 prepare_data_buffer(struct win32_apply_ctx *ctx, u64 blob_size)
1983 {
1984         if (blob_size > ctx->data_buffer_size) {
1985                 /* Larger buffer needed.  */
1986                 void *new_buffer;
1987                 if ((size_t)blob_size != blob_size)
1988                         return false;
1989                 new_buffer = REALLOC(ctx->data_buffer, blob_size);
1990                 if (!new_buffer)
1991                         return false;
1992                 ctx->data_buffer = new_buffer;
1993                 ctx->data_buffer_size = blob_size;
1994         }
1995         /* On the first call this changes data_buffer_ptr from NULL, which tells
1996          * extract_chunk() that the data buffer needs to be filled while reading
1997          * the stream data.  */
1998         ctx->data_buffer_ptr = ctx->data_buffer;
1999         return true;
2000 }
2001
2002 static int
2003 begin_extract_blob_instance(const struct blob_descriptor *blob,
2004                             struct wim_dentry *dentry,
2005                             const struct wim_inode_stream *strm,
2006                             struct win32_apply_ctx *ctx)
2007 {
2008         HANDLE h;
2009         NTSTATUS status;
2010
2011         if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
2012                 /* We can't write the reparse point stream directly; we must set
2013                  * it with FSCTL_SET_REPARSE_POINT, which requires that all the
2014                  * data be available.  So, stage the data in a buffer.  */
2015                 if (!prepare_data_buffer(ctx, blob->size))
2016                         return WIMLIB_ERR_NOMEM;
2017                 list_add_tail(&dentry->d_tmp_list, &ctx->reparse_dentries);
2018                 return 0;
2019         }
2020
2021         if (unlikely(strm->stream_type == STREAM_TYPE_EFSRPC_RAW_DATA)) {
2022                 /* We can't write encrypted files directly; we must use
2023                  * WriteEncryptedFileRaw(), which requires providing the data
2024                  * through a callback function.  This can't easily be combined
2025                  * with our own callback-based approach.
2026                  *
2027                  * The current workaround is to simply read the blob into memory
2028                  * and write the encrypted file from that.
2029                  *
2030                  * TODO: This isn't sufficient for extremely large encrypted
2031                  * files.  Perhaps we should create an extra thread to write
2032                  * such files...  */
2033                 if (!prepare_data_buffer(ctx, blob->size))
2034                         return WIMLIB_ERR_NOMEM;
2035                 list_add_tail(&dentry->d_tmp_list, &ctx->encrypted_dentries);
2036                 return 0;
2037         }
2038
2039         /* It's a data stream (may be unnamed or named).  */
2040         wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
2041
2042         if (ctx->num_open_handles == MAX_OPEN_FILES) {
2043                 /* XXX: Fix this.  But because of the checks in
2044                  * extract_blob_list(), this can now only happen on a filesystem
2045                  * that does not support hard links.  */
2046                 ERROR("Can't extract data: too many open files!");
2047                 return WIMLIB_ERR_UNSUPPORTED;
2048         }
2049
2050
2051         if (unlikely(stream_is_named(strm))) {
2052                 build_extraction_path_with_ads(dentry, ctx,
2053                                                strm->stream_name,
2054                                                utf16le_len_chars(strm->stream_name));
2055         } else {
2056                 build_extraction_path(dentry, ctx);
2057         }
2058
2059
2060         /* Open a new handle  */
2061         status = do_create_file(&h,
2062                                 FILE_WRITE_DATA | SYNCHRONIZE,
2063                                 NULL, 0, FILE_OPEN_IF,
2064                                 FILE_SEQUENTIAL_ONLY |
2065                                         FILE_SYNCHRONOUS_IO_NONALERT,
2066                                 ctx);
2067         if (!NT_SUCCESS(status)) {
2068                 winnt_error(status, L"Can't open \"%ls\" for writing",
2069                             current_path(ctx));
2070                 return WIMLIB_ERR_OPEN;
2071         }
2072
2073         ctx->is_sparse_stream[ctx->num_open_handles] = false;
2074         if (need_sparse_flag(dentry->d_inode, ctx)) {
2075                 /* If the stream is unnamed, then the sparse flag was already
2076                  * set when the file was created.  But if the stream is named,
2077                  * then we need to set the sparse flag here. */
2078                 if (unlikely(stream_is_named(strm))) {
2079                         int ret = set_sparse_flag(h, ctx);
2080                         if (ret) {
2081                                 NtClose(h);
2082                                 return ret;
2083                         }
2084                 }
2085                 ctx->is_sparse_stream[ctx->num_open_handles] = true;
2086                 ctx->any_sparse_streams = true;
2087         } else {
2088                 /* Allocate space for the data.  */
2089                 FILE_ALLOCATION_INFORMATION info =
2090                         { .AllocationSize = { .QuadPart = blob->size }};
2091                 NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
2092                                      FileAllocationInformation);
2093         }
2094         ctx->open_handles[ctx->num_open_handles++] = h;
2095         return 0;
2096 }
2097
2098 /* Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2099  * pointer to the suffix of the path that begins with the device directly, such
2100  * as e:\Windows\System32.  */
2101 static const wchar_t *
2102 skip_nt_toplevel_component(const wchar_t *path, size_t path_nchars)
2103 {
2104         static const wchar_t * const dirs[] = {
2105                 L"\\??\\",
2106                 L"\\DosDevices\\",
2107                 L"\\Device\\",
2108         };
2109         const wchar_t * const end = path + path_nchars;
2110
2111         for (size_t i = 0; i < ARRAY_LEN(dirs); i++) {
2112                 size_t len = wcslen(dirs[i]);
2113                 if (len <= (end - path) && !wmemcmp(path, dirs[i], len)) {
2114                         path += len;
2115                         while (path != end && *path == L'\\')
2116                                 path++;
2117                         return path;
2118                 }
2119         }
2120         return path;
2121 }
2122
2123 /*
2124  * Given a Windows NT namespace path, such as \??\e:\Windows\System32, return a
2125  * pointer to the suffix of the path that is device-relative but possibly with
2126  * leading slashes, such as \Windows\System32.
2127  *
2128  * The path has an explicit length and is not necessarily null terminated.
2129  */
2130 static const wchar_t *
2131 get_device_relative_path(const wchar_t *path, size_t path_nchars)
2132 {
2133         const wchar_t * const orig_path = path;
2134         const wchar_t * const end = path + path_nchars;
2135
2136         path = skip_nt_toplevel_component(path, path_nchars);
2137         if (path == orig_path)
2138                 return orig_path;
2139
2140         while (path != end && *path != L'\\')
2141                 path++;
2142
2143         return path;
2144 }
2145
2146 /*
2147  * Given a reparse point buffer for an inode for which the absolute link target
2148  * was relativized when it was archived, de-relative the link target to be
2149  * consistent with the actual extraction location.
2150  */
2151 static void
2152 try_rpfix(struct reparse_buffer_disk *rpbuf, u16 *rpbuflen_p,
2153           struct win32_apply_ctx *ctx)
2154 {
2155         struct link_reparse_point link;
2156         size_t orig_subst_name_nchars;
2157         const wchar_t *relpath;
2158         size_t relpath_nchars;
2159         size_t target_ntpath_nchars;
2160         size_t fixed_subst_name_nchars;
2161         const wchar_t *fixed_print_name;
2162         size_t fixed_print_name_nchars;
2163
2164         /* Do nothing if the reparse data is invalid.  */
2165         if (parse_link_reparse_point(rpbuf, *rpbuflen_p, &link))
2166                 return;
2167
2168         /* Do nothing if the reparse point is a relative symbolic link.  */
2169         if (link_is_relative_symlink(&link))
2170                 return;
2171
2172         /* Build the new substitute name from the NT namespace path to the
2173          * target directory, then a path separator, then the "device relative"
2174          * part of the old substitute name.  */
2175
2176         orig_subst_name_nchars = link.substitute_name_nbytes / sizeof(wchar_t);
2177
2178         relpath = get_device_relative_path(link.substitute_name,
2179                                            orig_subst_name_nchars);
2180         relpath_nchars = orig_subst_name_nchars -
2181                          (relpath - link.substitute_name);
2182
2183         target_ntpath_nchars = ctx->target_ntpath.Length / sizeof(wchar_t);
2184
2185         /* If the target directory is a filesystem root, such as \??\C:\, then
2186          * it already will have a trailing slash.  Don't include this slash if
2187          * we are already adding slashes via 'relpath'.  This prevents an extra
2188          * slash from being generated each time the link is extracted.  And
2189          * unlike on UNIX, the number of slashes in paths on Windows can be
2190          * significant; Windows won't understand the link target if it contains
2191          * too many slashes.  */
2192         if (target_ntpath_nchars > 0 && relpath_nchars > 0 &&
2193             ctx->target_ntpath.Buffer[target_ntpath_nchars - 1] == L'\\')
2194                 target_ntpath_nchars--;
2195
2196         /* Also remove extra slashes from the beginning of 'relpath'.  Normally
2197          * this isn't needed, but this is here to make the extra slash(es) added
2198          * by wimlib pre-v1.9.1 get removed automatically.  */
2199         while (relpath_nchars >= 2 &&
2200                relpath[0] == L'\\' && relpath[1] == L'\\') {
2201                 relpath++;
2202                 relpath_nchars--;
2203         }
2204
2205         fixed_subst_name_nchars = target_ntpath_nchars + relpath_nchars;
2206
2207         wchar_t fixed_subst_name[fixed_subst_name_nchars];
2208
2209         wmemcpy(fixed_subst_name, ctx->target_ntpath.Buffer, target_ntpath_nchars);
2210         wmemcpy(&fixed_subst_name[target_ntpath_nchars], relpath, relpath_nchars);
2211         /* Doesn't need to be null-terminated.  */
2212
2213         /* Print name should be Win32, but not all NT names can even be
2214          * translated to Win32 names.  But we can at least delete the top-level
2215          * directory, such as \??\, and this will have the expected result in
2216          * the usual case.  */
2217         fixed_print_name = skip_nt_toplevel_component(fixed_subst_name,
2218                                                       fixed_subst_name_nchars);
2219         fixed_print_name_nchars = fixed_subst_name_nchars - (fixed_print_name -
2220                                                              fixed_subst_name);
2221
2222         link.substitute_name = fixed_subst_name;
2223         link.substitute_name_nbytes = fixed_subst_name_nchars * sizeof(wchar_t);
2224         link.print_name = (wchar_t *)fixed_print_name;
2225         link.print_name_nbytes = fixed_print_name_nchars * sizeof(wchar_t);
2226         make_link_reparse_point(&link, rpbuf, rpbuflen_p);
2227 }
2228
2229 /* Sets the reparse point on the specified file.  This handles "fixing" the
2230  * targets of absolute symbolic links and junctions if WIMLIB_EXTRACT_FLAG_RPFIX
2231  * was specified.  */
2232 static int
2233 set_reparse_point(const struct wim_dentry *dentry,
2234                   const struct reparse_buffer_disk *rpbuf, u16 rpbuflen,
2235                   struct win32_apply_ctx *ctx)
2236 {
2237         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
2238             && !(dentry->d_inode->i_rp_flags & WIM_RP_FLAG_NOT_FIXED))
2239         {
2240                 memcpy(&ctx->rpfixbuf, rpbuf, rpbuflen);
2241                 try_rpfix(&ctx->rpfixbuf, &rpbuflen, ctx);
2242                 rpbuf = &ctx->rpfixbuf;
2243         }
2244         return do_set_reparse_point(dentry, rpbuf, rpbuflen, ctx);
2245
2246 }
2247
2248 /* Import the next block of raw encrypted data  */
2249 static DWORD WINAPI
2250 import_encrypted_data(PBYTE pbData, PVOID pvCallbackContext, PULONG Length)
2251 {
2252         struct win32_apply_ctx *ctx = pvCallbackContext;
2253         ULONG copy_len;
2254
2255         copy_len = min(ctx->encrypted_size - ctx->encrypted_offset, *Length);
2256         memcpy(pbData, &ctx->data_buffer[ctx->encrypted_offset], copy_len);
2257         ctx->encrypted_offset += copy_len;
2258         *Length = copy_len;
2259         return ERROR_SUCCESS;
2260 }
2261
2262 /*
2263  * Write the raw encrypted data to the already-created file (or directory)
2264  * corresponding to @dentry.
2265  *
2266  * The raw encrypted data is provided in ctx->data_buffer, and its size is
2267  * ctx->encrypted_size.
2268  *
2269  * This function may close the target directory, in which case the caller needs
2270  * to re-open it if needed.
2271  */
2272 static int
2273 extract_encrypted_file(const struct wim_dentry *dentry,
2274                        struct win32_apply_ctx *ctx)
2275 {
2276         void *rawctx;
2277         DWORD err;
2278         ULONG flags;
2279         bool retried;
2280
2281         /* Temporarily build a Win32 path for OpenEncryptedFileRaw()  */
2282         build_win32_extraction_path(dentry, ctx);
2283
2284         flags = CREATE_FOR_IMPORT | OVERWRITE_HIDDEN;
2285         if (dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
2286                 flags |= CREATE_FOR_DIR;
2287
2288         retried = false;
2289 retry:
2290         err = OpenEncryptedFileRaw(ctx->pathbuf.Buffer, flags, &rawctx);
2291         if (err == ERROR_SHARING_VIOLATION && !retried) {
2292                 /* This can be caused by the handle we have open to the target
2293                  * directory.  Try closing it temporarily.  */
2294                 close_target_directory(ctx);
2295                 retried = true;
2296                 goto retry;
2297         }
2298
2299         /* Restore the NT namespace path  */
2300         build_extraction_path(dentry, ctx);
2301
2302         if (err != ERROR_SUCCESS) {
2303                 win32_error(err, L"Can't open \"%ls\" for encrypted import",
2304                             current_path(ctx));
2305                 return WIMLIB_ERR_OPEN;
2306         }
2307
2308         ctx->encrypted_offset = 0;
2309
2310         err = WriteEncryptedFileRaw(import_encrypted_data, ctx, rawctx);
2311
2312         CloseEncryptedFileRaw(rawctx);
2313
2314         if (err != ERROR_SUCCESS) {
2315                 win32_error(err, L"Can't import encrypted file \"%ls\"",
2316                             current_path(ctx));
2317                 return WIMLIB_ERR_WRITE;
2318         }
2319
2320         return 0;
2321 }
2322
2323 /* Called when starting to read a blob for extraction */
2324 static int
2325 win32_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
2326 {
2327         struct win32_apply_ctx *ctx = _ctx;
2328         const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2329         int ret;
2330
2331         ctx->num_open_handles = 0;
2332         ctx->data_buffer_ptr = NULL;
2333         ctx->any_sparse_streams = false;
2334         INIT_LIST_HEAD(&ctx->reparse_dentries);
2335         INIT_LIST_HEAD(&ctx->encrypted_dentries);
2336
2337         for (u32 i = 0; i < blob->out_refcnt; i++) {
2338                 const struct wim_inode *inode = targets[i].inode;
2339                 const struct wim_inode_stream *strm = targets[i].stream;
2340                 struct wim_dentry *dentry;
2341
2342                 /* A copy of the blob needs to be extracted to @inode.  */
2343
2344                 if (ctx->common.supported_features.hard_links) {
2345                         dentry = inode_first_extraction_dentry(inode);
2346                         ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2347                         ret = check_apply_error(dentry, ctx, ret);
2348                         if (ret)
2349                                 goto fail;
2350                 } else {
2351                         /* Hard links not supported.  Extract the blob
2352                          * separately to each alias of the inode.  */
2353                         inode_for_each_extraction_alias(dentry, inode) {
2354                                 ret = begin_extract_blob_instance(blob, dentry, strm, ctx);
2355                                 ret = check_apply_error(dentry, ctx, ret);
2356                                 if (ret)
2357                                         goto fail;
2358                         }
2359                 }
2360         }
2361
2362         return 0;
2363
2364 fail:
2365         close_handles(ctx);
2366         return ret;
2367 }
2368
2369 static int
2370 pwrite_to_handle(HANDLE h, const void *data, size_t size, u64 offset)
2371 {
2372         const void * const end = data + size;
2373         const void *p;
2374         IO_STATUS_BLOCK iosb;
2375         NTSTATUS status;
2376
2377         for (p = data; p != end; p += iosb.Information,
2378                                  offset += iosb.Information)
2379         {
2380                 LARGE_INTEGER offs = { .QuadPart = offset };
2381
2382                 status = NtWriteFile(h, NULL, NULL, NULL, &iosb,
2383                                      (void *)p, min(INT32_MAX, end - p),
2384                                      &offs, NULL);
2385                 if (!NT_SUCCESS(status)) {
2386                         winnt_error(status,
2387                                     L"Error writing data to target volume");
2388                         return WIMLIB_ERR_WRITE;
2389                 }
2390         }
2391         return 0;
2392 }
2393
2394 /* Called when the next chunk of a blob has been read for extraction */
2395 static int
2396 win32_extract_chunk(const struct blob_descriptor *blob, u64 offset,
2397                     const void *chunk, size_t size, void *_ctx)
2398 {
2399         struct win32_apply_ctx *ctx = _ctx;
2400         const void * const end = chunk + size;
2401         const void *p;
2402         bool zeroes;
2403         size_t len;
2404         unsigned i;
2405         int ret;
2406
2407         /*
2408          * For sparse streams, only write nonzero regions.  This lets the
2409          * filesystem use holes to represent zero regions.
2410          */
2411         for (p = chunk; p != end; p += len, offset += len) {
2412                 zeroes = maybe_detect_sparse_region(p, end - p, &len,
2413                                                     ctx->any_sparse_streams);
2414                 for (i = 0; i < ctx->num_open_handles; i++) {
2415                         if (!zeroes || !ctx->is_sparse_stream[i]) {
2416                                 ret = pwrite_to_handle(ctx->open_handles[i],
2417                                                        p, len, offset);
2418                                 if (ret)
2419                                         return ret;
2420                         }
2421                 }
2422         }
2423
2424         /* Copy the data chunk into the buffer (if needed)  */
2425         if (ctx->data_buffer_ptr)
2426                 ctx->data_buffer_ptr = mempcpy(ctx->data_buffer_ptr,
2427                                                chunk, size);
2428         return 0;
2429 }
2430
2431 static int
2432 get_system_compression_format(int extract_flags)
2433 {
2434         if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS4K)
2435                 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2436
2437         if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS8K)
2438                 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K;
2439
2440         if (extract_flags & WIMLIB_EXTRACT_FLAG_COMPACT_XPRESS16K)
2441                 return FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K;
2442
2443         return FILE_PROVIDER_COMPRESSION_FORMAT_LZX;
2444 }
2445
2446
2447 static const wchar_t *
2448 get_system_compression_format_string(int format)
2449 {
2450         switch (format) {
2451         case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K:
2452                 return L"XPRESS4K";
2453         case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS8K:
2454                 return L"XPRESS8K";
2455         case FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS16K:
2456                 return L"XPRESS16K";
2457         default:
2458                 return L"LZX";
2459         }
2460 }
2461
2462 static NTSTATUS
2463 set_system_compression(HANDLE h, int format)
2464 {
2465         NTSTATUS status;
2466         struct {
2467                 struct wof_external_info wof_info;
2468                 struct file_provider_external_info file_info;
2469         } in = {
2470                 .wof_info = {
2471                         .version = WOF_CURRENT_VERSION,
2472                         .provider = WOF_PROVIDER_FILE,
2473                 },
2474                 .file_info = {
2475                         .version = FILE_PROVIDER_CURRENT_VERSION,
2476                         .compression_format = format,
2477                 },
2478         };
2479
2480         /* We intentionally use NtFsControlFile() rather than DeviceIoControl()
2481          * here because the "compressing this object would not save space"
2482          * status code does not map to a valid Win32 error code on older
2483          * versions of Windows (before Windows 10?).  This can be a problem if
2484          * the WOFADK driver is being used rather than the regular WOF, since
2485          * WOFADK can be used on older versions of Windows.  */
2486         status = winnt_fsctl(h, FSCTL_SET_EXTERNAL_BACKING,
2487                              &in, sizeof(in), NULL, 0, NULL);
2488
2489         if (status == 0xC000046F) /* "Compressing this object would not save space."  */
2490                 return STATUS_SUCCESS;
2491
2492         return status;
2493 }
2494
2495 /* Hard-coded list of files which the Windows bootloader may need to access
2496  * before the WOF driver has been loaded.  */
2497 static const wchar_t * const bootloader_pattern_strings[] = {
2498         L"*winload.*",
2499         L"*winresume.*",
2500         L"\\Windows\\AppPatch\\drvmain.sdb",
2501         L"\\Windows\\Boot\\DVD\\*",
2502         L"\\Windows\\Boot\\EFI\\*",
2503         L"\\Windows\\bootstat.dat",
2504         L"\\Windows\\Fonts\\vgaoem.fon",
2505         L"\\Windows\\Fonts\\vgasys.fon",
2506         L"\\Windows\\INF\\errata.inf",
2507         L"\\Windows\\System32\\config\\*",
2508         L"\\Windows\\System32\\ntkrnlpa.exe",
2509         L"\\Windows\\System32\\ntoskrnl.exe",
2510         L"\\Windows\\System32\\bootvid.dll",
2511         L"\\Windows\\System32\\ci.dll",
2512         L"\\Windows\\System32\\hal*.dll",
2513         L"\\Windows\\System32\\mcupdate_AuthenticAMD.dll",
2514         L"\\Windows\\System32\\mcupdate_GenuineIntel.dll",
2515         L"\\Windows\\System32\\pshed.dll",
2516         L"\\Windows\\System32\\apisetschema.dll",
2517         L"\\Windows\\System32\\api-ms-win*.dll",
2518         L"\\Windows\\System32\\ext-ms-win*.dll",
2519         L"\\Windows\\System32\\KernelBase.dll",
2520         L"\\Windows\\System32\\drivers\\*.sys",
2521         L"\\Windows\\System32\\*.nls",
2522         L"\\Windows\\System32\\kbd*.dll",
2523         L"\\Windows\\System32\\kd*.dll",
2524         L"\\Windows\\System32\\clfs.sys",
2525         L"\\Windows\\System32\\CodeIntegrity\\driver.stl",
2526 };
2527
2528 static const struct string_list bootloader_patterns = {
2529         .strings = (wchar_t **)bootloader_pattern_strings,
2530         .num_strings = ARRAY_LEN(bootloader_pattern_strings),
2531 };
2532
2533 static NTSTATUS
2534 set_system_compression_on_inode(struct wim_inode *inode, int format,
2535                                 struct win32_apply_ctx *ctx)
2536 {
2537         bool retried = false;
2538         NTSTATUS status;
2539         HANDLE h;
2540
2541         /* If it may be needed for compatibility with the Windows bootloader,
2542          * force this file to XPRESS4K or uncompressed format.  The bootloader
2543          * of Windows 10 supports XPRESS4K only; older versions don't support
2544          * system compression at all.  */
2545         if (!is_image_windows_10_or_later(ctx) ||
2546             format != FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K)
2547         {
2548                 /* We need to check the patterns against every name of the
2549                  * inode, in case any of them match.  */
2550                 struct wim_dentry *dentry;
2551                 inode_for_each_extraction_alias(dentry, inode) {
2552                         bool incompatible;
2553                         bool warned;
2554
2555                         if (calculate_dentry_full_path(dentry)) {
2556                                 ERROR("Unable to compute file path!");
2557                                 return STATUS_NO_MEMORY;
2558                         }
2559
2560                         incompatible = match_pattern_list(dentry->d_full_path,
2561                                                           &bootloader_patterns,
2562                                                           MATCH_RECURSIVELY);
2563                         FREE(dentry->d_full_path);
2564                         dentry->d_full_path = NULL;
2565
2566                         if (!incompatible)
2567                                 continue;
2568
2569                         warned = (ctx->num_system_compression_exclusions++ > 0);
2570
2571                         if (is_image_windows_10_or_later(ctx)) {
2572                                 /* Force to XPRESS4K  */
2573                                 if (!warned) {
2574                                         WARNING("For compatibility with the "
2575                                                 "Windows bootloader, some "
2576                                                 "files are being\n"
2577                                                 "          compacted "
2578                                                 "using the XPRESS4K format "
2579                                                 "instead of the %"TS" format\n"
2580                                                 "          you requested.",
2581                                                 get_system_compression_format_string(format));
2582                                 }
2583                                 format = FILE_PROVIDER_COMPRESSION_FORMAT_XPRESS4K;
2584                                 break;
2585                         } else {
2586                                 /* Force to uncompressed  */
2587                                 if (!warned) {
2588                                         WARNING("For compatibility with the "
2589                                                 "Windows bootloader, some "
2590                                                 "files will not\n"
2591                                                 "          be compressed with"
2592                                                 " system compression "
2593                                                 "(\"compacted\").");
2594                                 }
2595                                 return STATUS_SUCCESS;
2596                         }
2597
2598                 }
2599         }
2600
2601         /* Open the extracted file.  */
2602         status = create_file(&h, GENERIC_READ | GENERIC_WRITE, NULL,
2603                              0, FILE_OPEN, 0,
2604                              inode_first_extraction_dentry(inode), ctx);
2605
2606         if (!NT_SUCCESS(status))
2607                 return status;
2608 retry:
2609         /* Compress the file.  If the attempt fails with "invalid device
2610          * request", then attach wof.sys (or wofadk.sys) and retry.  */
2611         status = set_system_compression(h, format);
2612         if (unlikely(status == STATUS_INVALID_DEVICE_REQUEST && !retried)) {
2613                 wchar_t drive_path[7];
2614                 if (!win32_get_drive_path(ctx->common.target, drive_path) &&
2615                     win32_try_to_attach_wof(drive_path + 4)) {
2616                         retried = true;
2617                         goto retry;
2618                 }
2619         }
2620
2621         NtClose(h);
2622         return status;
2623 }
2624
2625 /*
2626  * This function is called when doing a "compact-mode" extraction and we just
2627  * finished extracting a blob to one or more locations.  For each location that
2628  * was the unnamed data stream of a file, this function compresses the
2629  * corresponding file using System Compression, if allowed.
2630  *
2631  * Note: we're doing the compression immediately after extracting the data
2632  * rather than during a separate compression pass.  This way should be faster
2633  * since the operating system should still have the file's data cached.
2634  *
2635  * Note: we're having the operating system do the compression, which is not
2636  * ideal because wimlib could create the compressed data faster and more
2637  * efficiently (the compressed data format is identical to a WIM resource).  But
2638  * we seemingly don't have a choice because WOF prevents applications from
2639  * creating its reparse points.
2640  */
2641 static void
2642 handle_system_compression(struct blob_descriptor *blob, struct win32_apply_ctx *ctx)
2643 {
2644         const struct blob_extraction_target *targets = blob_extraction_targets(blob);
2645
2646         const int format = get_system_compression_format(ctx->common.extract_flags);
2647
2648         for (u32 i = 0; i < blob->out_refcnt; i++) {
2649                 struct wim_inode *inode = targets[i].inode;
2650                 struct wim_inode_stream *strm = targets[i].stream;
2651                 NTSTATUS status;
2652
2653                 if (!stream_is_unnamed_data_stream(strm))
2654                         continue;
2655
2656                 if (will_externally_back_inode(inode, ctx, NULL, false) != 0)
2657                         continue;
2658
2659                 status = set_system_compression_on_inode(inode, format, ctx);
2660                 if (likely(NT_SUCCESS(status)))
2661                         continue;
2662
2663                 if (status == STATUS_INVALID_DEVICE_REQUEST) {
2664                         WARNING(
2665           "The request to compress the extracted files using System Compression\n"
2666 "          will not be honored because the operating system or target volume\n"
2667 "          does not support it.  System Compression is only supported on\n"
2668 "          Windows 10 and later, and only on NTFS volumes.");
2669                         ctx->common.extract_flags &= ~COMPACT_FLAGS;
2670                         return;
2671                 }
2672
2673                 ctx->num_system_compression_failures++;
2674                 if (ctx->num_system_compression_failures < 10) {
2675                         winnt_warning(status, L"\"%ls\": Failed to compress "
2676                                       "extracted file using System Compression",
2677                                       current_path(ctx));
2678                 } else if (ctx->num_system_compression_failures == 10) {
2679                         WARNING("Suppressing further warnings about "
2680                                 "System Compression failures.");
2681                 }
2682         }
2683 }
2684
2685 /* Called when a blob has been fully read for extraction */
2686 static int
2687 win32_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
2688 {
2689         struct win32_apply_ctx *ctx = _ctx;
2690         int ret;
2691         const struct wim_dentry *dentry;
2692
2693         /* Extend sparse streams to their final size. */
2694         if (ctx->any_sparse_streams && !status) {
2695                 for (unsigned i = 0; i < ctx->num_open_handles; i++) {
2696                         FILE_END_OF_FILE_INFORMATION info =
2697                                 { .EndOfFile = { .QuadPart = blob->size } };
2698                         NTSTATUS ntstatus;
2699
2700                         if (!ctx->is_sparse_stream[i])
2701                                 continue;
2702
2703                         ntstatus = NtSetInformationFile(ctx->open_handles[i],
2704                                                         &ctx->iosb,
2705                                                         &info, sizeof(info),
2706                                                         FileEndOfFileInformation);
2707                         if (!NT_SUCCESS(ntstatus)) {
2708                                 winnt_error(ntstatus, L"Error writing data to "
2709                                             "target volume (while extending)");
2710                                 status = WIMLIB_ERR_WRITE;
2711                                 break;
2712                         }
2713                 }
2714         }
2715
2716         close_handles(ctx);
2717
2718         if (status)
2719                 return status;
2720
2721         if (unlikely(ctx->common.extract_flags & COMPACT_FLAGS))
2722                 handle_system_compression(blob, ctx);
2723
2724         if (likely(!ctx->data_buffer_ptr))
2725                 return 0;
2726
2727         if (!list_empty(&ctx->reparse_dentries)) {
2728                 if (blob->size > REPARSE_DATA_MAX_SIZE) {
2729                         dentry = list_first_entry(&ctx->reparse_dentries,
2730                                                   struct wim_dentry, d_tmp_list);
2731                         build_extraction_path(dentry, ctx);
2732                         ERROR("Reparse data of \"%ls\" has size "
2733                               "%"PRIu64" bytes (exceeds %u bytes)",
2734                               current_path(ctx), blob->size,
2735                               REPARSE_DATA_MAX_SIZE);
2736                         ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
2737                         return check_apply_error(dentry, ctx, ret);
2738                 }
2739                 /* Reparse data  */
2740                 memcpy(ctx->rpbuf.rpdata, ctx->data_buffer, blob->size);
2741
2742                 list_for_each_entry(dentry, &ctx->reparse_dentries, d_tmp_list) {
2743
2744                         /* Reparse point header  */
2745                         complete_reparse_point(&ctx->rpbuf, dentry->d_inode,
2746                                                blob->size);
2747
2748                         ret = set_reparse_point(dentry, &ctx->rpbuf,
2749                                                 REPARSE_DATA_OFFSET + blob->size,
2750                                                 ctx);
2751                         ret = check_apply_error(dentry, ctx, ret);
2752                         if (ret)
2753                                 return ret;
2754                 }
2755         }
2756
2757         if (!list_empty(&ctx->encrypted_dentries)) {
2758                 ctx->encrypted_size = blob->size;
2759                 list_for_each_entry(dentry, &ctx->encrypted_dentries, d_tmp_list) {
2760                         ret = extract_encrypted_file(dentry, ctx);
2761                         ret = check_apply_error(dentry, ctx, ret);
2762                         if (ret)
2763                                 return ret;
2764                         /* Re-open the target directory if needed.  */
2765                         ret = open_target_directory(ctx);
2766                         if (ret)
2767                                 return ret;
2768                 }
2769         }
2770
2771         return 0;
2772 }
2773
2774 /* Attributes that can't be set directly  */
2775 #define SPECIAL_ATTRIBUTES                      \
2776         (FILE_ATTRIBUTE_REPARSE_POINT   |       \
2777          FILE_ATTRIBUTE_DIRECTORY       |       \
2778          FILE_ATTRIBUTE_ENCRYPTED       |       \
2779          FILE_ATTRIBUTE_SPARSE_FILE     |       \
2780          FILE_ATTRIBUTE_COMPRESSED)
2781
2782 static void
2783 set_object_id(HANDLE h, const struct wim_inode *inode,
2784               struct win32_apply_ctx *ctx)
2785 {
2786         const void *object_id;
2787         u32 len;
2788         NTSTATUS status;
2789
2790         if (!ctx->common.supported_features.object_ids)
2791                 return;
2792
2793         object_id = inode_get_object_id(inode, &len);
2794         if (likely(object_id == NULL))  /* No object ID?  */
2795                 return;
2796
2797         status = winnt_fsctl(h, FSCTL_SET_OBJECT_ID,
2798                              object_id, len, NULL, 0, NULL);
2799         if (NT_SUCCESS(status))
2800                 return;
2801
2802         /* Object IDs must be unique within the filesystem.  A duplicate might
2803          * occur if an image containing object IDs is applied twice to the same
2804          * filesystem.  Arguably, the user should be warned in this case; but
2805          * the reality seems to be that nothing important cares about object IDs
2806          * except the Distributed Link Tracking Service... so for now these
2807          * failures are just ignored.  */
2808         if (status == STATUS_DUPLICATE_NAME ||
2809             status == STATUS_OBJECT_NAME_COLLISION)
2810                 return;
2811
2812         ctx->num_object_id_failures++;
2813         if (ctx->num_object_id_failures < 10) {
2814                 winnt_warning(status, L"Can't set object ID on \"%ls\"",
2815                               current_path(ctx));
2816         } else if (ctx->num_object_id_failures == 10) {
2817                 WARNING("Suppressing further warnings about failure to set "
2818                         "object IDs.");
2819         }
2820 }
2821
2822 static int
2823 set_xattrs(HANDLE h, const struct wim_inode *inode, struct win32_apply_ctx *ctx)
2824 {
2825         const void *entries, *entries_end;
2826         u32 len;
2827         const struct wim_xattr_entry *entry;
2828         size_t bufsize = 0;
2829         u8 _buf[1024] _aligned_attribute(4);
2830         u8 *buf = _buf;
2831         FILE_FULL_EA_INFORMATION *ea, *ea_prev;
2832         NTSTATUS status;
2833         int ret;
2834
2835         if (!ctx->common.supported_features.xattrs)
2836                 return 0;
2837
2838         entries = inode_get_xattrs(inode, &len);
2839         if (likely(entries == NULL || len == 0))  /* No extended attributes? */
2840                 return 0;
2841         entries_end = entries + len;
2842
2843         entry = entries;
2844         for (entry = entries; (void *)entry < entries_end;
2845              entry = xattr_entry_next(entry)) {
2846                 if (!valid_xattr_entry(entry, entries_end - (void *)entry)) {
2847                         ERROR("\"%"TS"\": extended attribute is corrupt or unsupported",
2848                               inode_any_full_path(inode));
2849                         return WIMLIB_ERR_INVALID_XATTR;
2850                 }
2851
2852                 bufsize += ALIGN(offsetof(FILE_FULL_EA_INFORMATION, EaName) +
2853                                  entry->name_len + 1 +
2854                                  le16_to_cpu(entry->value_len), 4);
2855         }
2856
2857         if (unlikely(bufsize != (u32)bufsize)) {
2858                 ERROR("\"%"TS"\": too many extended attributes to extract!",
2859                       inode_any_full_path(inode));
2860                 return WIMLIB_ERR_INVALID_XATTR;
2861         }
2862
2863         if (unlikely(bufsize > sizeof(_buf))) {
2864                 buf = MALLOC(bufsize);
2865                 if (!buf)
2866                         return WIMLIB_ERR_NOMEM;
2867         }
2868
2869         ea_prev = NULL;
2870         ea = (FILE_FULL_EA_INFORMATION *)buf;
2871         for (entry = entries; (void *)entry < entries_end;
2872              entry = xattr_entry_next(entry)) {
2873                 u8 *p;
2874
2875                 if (ea_prev)
2876                         ea_prev->NextEntryOffset = (u8 *)ea - (u8 *)ea_prev;
2877                 ea->Flags = entry->flags;
2878                 ea->EaNameLength = entry->name_len;
2879                 ea->EaValueLength = le16_to_cpu(entry->value_len);
2880                 p = mempcpy(ea->EaName, entry->name,
2881                             ea->EaNameLength + 1 + ea->EaValueLength);
2882                 while ((uintptr_t)p & 3)
2883                         *p++ = 0;
2884                 ea_prev = ea;
2885                 ea = (FILE_FULL_EA_INFORMATION *)p;
2886         }
2887         ea_prev->NextEntryOffset = 0;
2888         wimlib_assert((u8 *)ea - buf == bufsize);
2889
2890         status = NtSetEaFile(h, &ctx->iosb, buf, bufsize);
2891         if (unlikely(!NT_SUCCESS(status))) {
2892                 if (status == STATUS_EAS_NOT_SUPPORTED) {
2893                         /* This happens with Samba. */
2894                         WARNING("Filesystem advertised extended attribute (EA) support, but it doesn't\n"
2895                                 "          work.  EAs will not be extracted.");
2896                         ctx->common.supported_features.xattrs = 0;
2897                 } else if (status == STATUS_INVALID_EA_NAME) {
2898                         ctx->num_xattr_failures++;
2899                         if (ctx->num_xattr_failures < 5) {
2900                                 winnt_warning(status,
2901                                               L"Can't set extended attributes on \"%ls\"",
2902                                               current_path(ctx));
2903                         } else if (ctx->num_xattr_failures == 5) {
2904                                 WARNING("Suppressing further warnings about "
2905                                         "failure to set extended attributes.");
2906                         }
2907                 } else {
2908                         winnt_error(status, L"Can't set extended attributes on \"%ls\"",
2909                                     current_path(ctx));
2910                         ret = WIMLIB_ERR_SET_XATTR;
2911                         goto out;
2912                 }
2913         }
2914         ret = 0;
2915 out:
2916         if (buf != _buf)
2917                 FREE(buf);
2918         return ret;
2919 }
2920
2921 /* Set the security descriptor @desc, of @desc_size bytes, on the file with open
2922  * handle @h.  */
2923 static NTSTATUS
2924 set_security_descriptor(HANDLE h, const void *_desc,
2925                         size_t desc_size, struct win32_apply_ctx *ctx)
2926 {
2927         SECURITY_INFORMATION info;
2928         NTSTATUS status;
2929         SECURITY_DESCRIPTOR_RELATIVE *desc;
2930
2931         /*
2932          * Ideally, we would just pass in the security descriptor buffer as-is.
2933          * But it turns out that Windows can mess up the security descriptor
2934          * even when using the low-level NtSetSecurityObject() function:
2935          *
2936          * - Windows will clear SE_DACL_AUTO_INHERITED if it is set in the
2937          *   passed buffer.  To actually get Windows to set
2938          *   SE_DACL_AUTO_INHERITED, the application must set the non-persistent
2939          *   flag SE_DACL_AUTO_INHERIT_REQ.  As usual, Microsoft didn't bother
2940          *   to properly document either of these flags.  It's unclear how
2941          *   important SE_DACL_AUTO_INHERITED actually is, but to be safe we use
2942          *   the SE_DACL_AUTO_INHERIT_REQ workaround to set it if needed.
2943          *
2944          * - The above also applies to the equivalent SACL flags,
2945          *   SE_SACL_AUTO_INHERITED and SE_SACL_AUTO_INHERIT_REQ.
2946          *
2947          * - If the application says that it's setting
2948          *   DACL_SECURITY_INFORMATION, then Windows sets SE_DACL_PRESENT in the
2949          *   resulting security descriptor, even if the security descriptor the
2950          *   application provided did not have a DACL.  This seems to be
2951          *   unavoidable, since omitting DACL_SECURITY_INFORMATION would cause a
2952          *   default DACL to remain.  Fortunately, this behavior seems harmless,
2953          *   since the resulting DACL will still be "null" --- but it will be
2954          *   "the other representation of null".
2955          *
2956          * - The above also applies to SACL_SECURITY_INFORMATION and
2957          *   SE_SACL_PRESENT.  Again, it's seemingly unavoidable but "harmless"
2958          *   that Windows changes the representation of a "null SACL".
2959          */
2960         if (likely(desc_size <= STACK_MAX)) {
2961                 desc = alloca(desc_size);
2962         } else {
2963                 desc = MALLOC(desc_size);
2964                 if (!desc)
2965                         return STATUS_NO_MEMORY;
2966         }
2967
2968         memcpy(desc, _desc, desc_size);
2969
2970         if (likely(desc_size >= 4)) {
2971
2972                 if (desc->Control & SE_DACL_AUTO_INHERITED)
2973                         desc->Control |= SE_DACL_AUTO_INHERIT_REQ;
2974
2975                 if (desc->Control & SE_SACL_AUTO_INHERITED)
2976                         desc->Control |= SE_SACL_AUTO_INHERIT_REQ;
2977         }
2978
2979         /*
2980          * More API insanity.  We want to set the entire security descriptor
2981          * as-is.  But all available APIs require specifying the specific parts
2982          * of the security descriptor being set.  Especially annoying is that
2983          * mandatory integrity labels are part of the SACL, but they aren't set
2984          * with SACL_SECURITY_INFORMATION.  Instead, applications must also
2985          * specify LABEL_SECURITY_INFORMATION (Windows Vista, Windows 7) or
2986          * BACKUP_SECURITY_INFORMATION (Windows 8).  But at least older versions
2987          * of Windows don't error out if you provide these newer flags...
2988          *
2989          * Also, if the process isn't running as Administrator, then it probably
2990          * doesn't have SE_RESTORE_PRIVILEGE.  In this case, it will always get
2991          * the STATUS_PRIVILEGE_NOT_HELD error by trying to set the SACL, even
2992          * if the security descriptor it provided did not have a SACL.  By
2993          * default, in this case we try to recover and set as much of the
2994          * security descriptor as possible --- potentially excluding the DACL, and
2995          * even the owner, as well as the SACL.
2996          */
2997
2998         info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
2999                DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION |
3000                LABEL_SECURITY_INFORMATION | BACKUP_SECURITY_INFORMATION;
3001
3002
3003         /*
3004          * It's also worth noting that SetFileSecurity() is unusable because it
3005          * doesn't request "backup semantics" when it opens the file internally.
3006          * NtSetSecurityObject() seems to be the best function to use in backup
3007          * applications.  (SetSecurityInfo() should also work, but it's harder
3008          * to use and must call NtSetSecurityObject() internally anyway.
3009          * BackupWrite() is theoretically usable as well, but it's inflexible
3010          * and poorly documented.)
3011          */
3012
3013 retry:
3014         status = NtSetSecurityObject(h, info, desc);
3015         if (NT_SUCCESS(status))
3016                 goto out_maybe_free_desc;
3017
3018         /* Failed to set the requested parts of the security descriptor.  If the
3019          * error was permissions-related, try to set fewer parts of the security
3020          * descriptor, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled.  */
3021         if ((status == STATUS_PRIVILEGE_NOT_HELD ||
3022              status == STATUS_ACCESS_DENIED) &&
3023             !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3024         {
3025                 if (info & SACL_SECURITY_INFORMATION) {
3026                         info &= ~(SACL_SECURITY_INFORMATION |
3027                                   LABEL_SECURITY_INFORMATION |
3028                                   BACKUP_SECURITY_INFORMATION);
3029                         ctx->partial_security_descriptors++;
3030                         goto retry;
3031                 }
3032                 if (info & DACL_SECURITY_INFORMATION) {
3033                         info &= ~DACL_SECURITY_INFORMATION;
3034                         goto retry;
3035                 }
3036                 if (info & OWNER_SECURITY_INFORMATION) {
3037                         info &= ~OWNER_SECURITY_INFORMATION;
3038                         goto retry;
3039                 }
3040                 /* Nothing left except GROUP, and if we removed it we
3041                  * wouldn't have anything at all.  */
3042         }
3043
3044         /* No part of the security descriptor could be set, or
3045          * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full security
3046          * descriptor could not be set.  */
3047         if (!(info & SACL_SECURITY_INFORMATION))
3048                 ctx->partial_security_descriptors--;
3049         ctx->no_security_descriptors++;
3050
3051 out_maybe_free_desc:
3052         if (unlikely(desc_size > STACK_MAX))
3053                 FREE(desc);
3054         return status;
3055 }
3056
3057 /* Set metadata on the open file @h from the WIM inode @inode.  */
3058 static int
3059 do_apply_metadata_to_file(HANDLE h, const struct wim_inode *inode,
3060                           struct win32_apply_ctx *ctx)
3061 {
3062         FILE_BASIC_INFORMATION info;
3063         NTSTATUS status;
3064         int ret;
3065
3066         /* Set the file's object ID if present and object IDs are supported by
3067          * the filesystem.  */
3068         set_object_id(h, inode, ctx);
3069
3070         /* Set the file's extended attributes (EAs) if present and EAs are
3071          * supported by the filesystem.  */
3072         ret = set_xattrs(h, inode, ctx);
3073         if (ret)
3074                 return ret;
3075
3076         /* Set the file's security descriptor if present and we're not in
3077          * NO_ACLS mode  */
3078         if (inode_has_security_descriptor(inode) &&
3079             !(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
3080         {
3081                 const struct wim_security_data *sd;
3082                 const void *desc;
3083                 size_t desc_size;
3084
3085                 sd = wim_get_current_security_data(ctx->common.wim);
3086                 desc = sd->descriptors[inode->i_security_id];
3087                 desc_size = sd->sizes[inode->i_security_id];
3088
3089                 status = set_security_descriptor(h, desc, desc_size, ctx);
3090                 if (!NT_SUCCESS(status) &&
3091                     (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
3092                 {
3093                         winnt_error(status,
3094                                     L"Can't set security descriptor on \"%ls\"",
3095                                     current_path(ctx));
3096                         return WIMLIB_ERR_SET_SECURITY;
3097                 }
3098         }
3099
3100         /* Set attributes and timestamps  */
3101         info.CreationTime.QuadPart = inode->i_creation_time;
3102         info.LastAccessTime.QuadPart = inode->i_last_access_time;
3103         info.LastWriteTime.QuadPart = inode->i_last_write_time;
3104         info.ChangeTime.QuadPart = 0;
3105         if (ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES) {
3106                 info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3107         } else {
3108                 info.FileAttributes = inode->i_attributes & ~SPECIAL_ATTRIBUTES;
3109                 if (info.FileAttributes == 0)
3110                         info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
3111         }
3112
3113         status = NtSetInformationFile(h, &ctx->iosb, &info, sizeof(info),
3114                                       FileBasicInformation);
3115         /* On FAT volumes we get STATUS_INVALID_PARAMETER if we try to set
3116          * attributes on the root directory.  (Apparently because FAT doesn't
3117          * actually have a place to store those attributes!)  */
3118         if (!NT_SUCCESS(status)
3119             && !(status == STATUS_INVALID_PARAMETER &&
3120                  dentry_is_root(inode_first_extraction_dentry(inode))))
3121         {
3122                 winnt_error(status, L"Can't set basic metadata on \"%ls\"",
3123                             current_path(ctx));
3124                 return WIMLIB_ERR_SET_ATTRIBUTES;
3125         }
3126
3127         return 0;
3128 }
3129
3130 static int
3131 apply_metadata_to_file(const struct wim_dentry *dentry,
3132                        struct win32_apply_ctx *ctx)
3133 {
3134         const struct wim_inode *inode = dentry->d_inode;
3135         DWORD perms;
3136         HANDLE h;
3137         NTSTATUS status;
3138         int ret;
3139
3140         perms = FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | WRITE_DAC |
3141                 WRITE_OWNER | ACCESS_SYSTEM_SECURITY;
3142
3143         build_extraction_path(dentry, ctx);
3144
3145         /* Open a handle with as many relevant permissions as possible.  */
3146         while (!NT_SUCCESS(status = do_create_file(&h, perms, NULL,
3147                                                    0, FILE_OPEN, 0, ctx)))
3148         {
3149                 if (status == STATUS_PRIVILEGE_NOT_HELD ||
3150                     status == STATUS_ACCESS_DENIED)
3151                 {
3152                         if (perms & ACCESS_SYSTEM_SECURITY) {
3153                                 perms &= ~ACCESS_SYSTEM_SECURITY;
3154                                 continue;
3155                         }
3156                         if (perms & WRITE_DAC) {
3157                                 perms &= ~WRITE_DAC;
3158                                 continue;
3159                         }
3160                         if (perms & WRITE_OWNER) {
3161                                 perms &= ~WRITE_OWNER;
3162                                 continue;
3163                         }
3164                 }
3165                 winnt_error(status, L"Can't open \"%ls\" to set metadata",
3166                             current_path(ctx));
3167                 return WIMLIB_ERR_OPEN;
3168         }
3169
3170         ret = do_apply_metadata_to_file(h, inode, ctx);
3171
3172         NtClose(h);
3173
3174         return ret;
3175 }
3176
3177 static int
3178 apply_metadata(struct list_head *dentry_list, struct win32_apply_ctx *ctx)
3179 {
3180         const struct wim_dentry *dentry;
3181         int ret;
3182
3183         /* We go in reverse so that metadata is set on all a directory's
3184          * children before the directory itself.  This avoids any potential
3185          * problems with attributes, timestamps, or security descriptors.  */
3186         list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node)
3187         {
3188                 ret = apply_metadata_to_file(dentry, ctx);
3189                 ret = check_apply_error(dentry, ctx, ret);
3190                 if (ret)
3191                         return ret;
3192                 ret = report_file_metadata_applied(&ctx->common);
3193                 if (ret)
3194                         return ret;
3195         }
3196         return 0;
3197 }
3198
3199 /* Issue warnings about problems during the extraction for which warnings were
3200  * not already issued (due to the high number of potential warnings if we issued
3201  * them per-file).  */
3202 static void
3203 do_warnings(const struct win32_apply_ctx *ctx)
3204 {
3205         if (ctx->partial_security_descriptors == 0
3206             && ctx->no_security_descriptors == 0
3207             && ctx->num_set_short_name_failures == 0
3208         #if 0
3209             && ctx->num_remove_short_name_failures == 0
3210         #endif
3211             )
3212                 return;
3213
3214         WARNING("Extraction to \"%ls\" complete, but with one or more warnings:",
3215                 ctx->common.target);
3216         if (ctx->num_set_short_name_failures) {
3217                 WARNING("- Could not set short names on %lu files or directories",
3218                         ctx->num_set_short_name_failures);
3219         }
3220 #if 0
3221         if (ctx->num_remove_short_name_failures) {
3222                 WARNING("- Could not remove short names on %lu files or directories"
3223                         "          (This is expected on Vista and earlier)",
3224                         ctx->num_remove_short_name_failures);
3225         }
3226 #endif
3227         if (ctx->partial_security_descriptors) {
3228                 WARNING("- Could only partially set the security descriptor\n"
3229                         "            on %lu files or directories.",
3230                         ctx->partial_security_descriptors);
3231         }
3232         if (ctx->no_security_descriptors) {
3233                 WARNING("- Could not set security descriptor at all\n"
3234                         "            on %lu files or directories.",
3235                         ctx->no_security_descriptors);
3236         }
3237         if (ctx->partial_security_descriptors || ctx->no_security_descriptors) {
3238                 WARNING("To fully restore all security descriptors, run the program\n"
3239                         "          with Administrator rights.");
3240         }
3241 }
3242
3243 static u64
3244 count_dentries(const struct list_head *dentry_list)
3245 {
3246         const struct list_head *cur;
3247         u64 count = 0;
3248
3249         list_for_each(cur, dentry_list)
3250                 count++;
3251
3252         return count;
3253 }
3254
3255 /* Extract files from a WIM image to a directory on Windows  */
3256 static int
3257 win32_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
3258 {
3259         int ret;
3260         struct win32_apply_ctx *ctx = (struct win32_apply_ctx *)_ctx;
3261         u64 dentry_count;
3262
3263         ret = prepare_target(dentry_list, ctx);
3264         if (ret)
3265                 goto out;
3266
3267         if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3268                 ret = start_wimboot_extraction(dentry_list, ctx);
3269                 if (ret)
3270                         goto out;
3271         }
3272
3273         ctx->windows_build_number = xml_get_windows_build_number(ctx->common.wim->xml_info,
3274                                                                  ctx->common.wim->current_image);
3275
3276         dentry_count = count_dentries(dentry_list);
3277
3278         ret = start_file_structure_phase(&ctx->common, dentry_count);
3279         if (ret)
3280                 goto out;
3281
3282         ret = create_directories(dentry_list, ctx);
3283         if (ret)
3284                 goto out;
3285
3286         ret = create_nondirectories(dentry_list, ctx);
3287         if (ret)
3288                 goto out;
3289
3290         ret = end_file_structure_phase(&ctx->common);
3291         if (ret)
3292                 goto out;
3293
3294         struct read_blob_callbacks cbs = {
3295                 .begin_blob     = win32_begin_extract_blob,
3296                 .continue_blob  = win32_extract_chunk,
3297                 .end_blob       = win32_end_extract_blob,
3298                 .ctx            = ctx,
3299         };
3300         ret = extract_blob_list(&ctx->common, &cbs);
3301         if (ret)
3302                 goto out;
3303
3304         ret = start_file_metadata_phase(&ctx->common, dentry_count);
3305         if (ret)
3306                 goto out;
3307
3308         ret = apply_metadata(dentry_list, ctx);
3309         if (ret)
3310                 goto out;
3311
3312         ret = end_file_metadata_phase(&ctx->common);
3313         if (ret)
3314                 goto out;
3315
3316         if (unlikely(ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_WIMBOOT)) {
3317                 ret = end_wimboot_extraction(ctx);
3318                 if (ret)
3319                         goto out;
3320         }
3321
3322         do_warnings(ctx);
3323 out:
3324         close_target_directory(ctx);
3325         if (ctx->target_ntpath.Buffer)
3326                 HeapFree(GetProcessHeap(), 0, ctx->target_ntpath.Buffer);
3327         FREE(ctx->pathbuf.Buffer);
3328         FREE(ctx->print_buffer);
3329         FREE(ctx->wimboot.wims);
3330         if (ctx->prepopulate_pats) {
3331                 FREE(ctx->prepopulate_pats->strings);
3332                 FREE(ctx->prepopulate_pats);
3333         }
3334         FREE(ctx->mem_prepopulate_pats);
3335         FREE(ctx->data_buffer);
3336         return ret;
3337 }
3338
3339 const struct apply_operations win32_apply_ops = {
3340         .name                   = "Windows",
3341         .get_supported_features = win32_get_supported_features,
3342         .extract                = win32_extract,
3343         .will_back_from_wim     = win32_will_back_from_wim,
3344         .context_size           = sizeof(struct win32_apply_ctx),
3345 };
3346
3347 #endif /* __WIN32__ */