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