f940179cd58f2aed786ed4637d5985ff5bb4050b
[wimlib] / src / win32_capture.c
1 /*
2  * win32_capture.c - Windows-specific code for capturing files into a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef __WIN32__
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib/win32_common.h"
31
32 #include "wimlib/capture.h"
33 #include "wimlib/endianness.h"
34 #include "wimlib/error.h"
35 #include "wimlib/lookup_table.h"
36 #include "wimlib/paths.h"
37 #include "wimlib/reparse.h"
38
39 #define MAX_GET_SD_ACCESS_DENIED_WARNINGS 1
40 #define MAX_GET_SACL_PRIV_NOTHELD_WARNINGS 1
41 #define MAX_CAPTURE_LONG_PATH_WARNINGS 5
42
43 struct win32_capture_state {
44         unsigned long num_get_sd_access_denied;
45         unsigned long num_get_sacl_priv_notheld;
46         unsigned long num_long_path_warnings;
47 };
48
49
50 static const wchar_t *capture_access_denied_msg =
51 L"         If you are not running this program as the administrator, you may\n"
52  "         need to do so, so that all data and metadata can be backed up.\n"
53  "         Otherwise, there may be no way to access the desired data or\n"
54  "         metadata without taking ownership of the file or directory.\n"
55  ;
56
57 int
58 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
59                        u64 size,
60                        consume_data_callback_t cb,
61                        u32 in_chunk_size,
62                        void *ctx_or_buf,
63                        int _ignored_flags)
64 {
65         int ret = 0;
66         void *out_buf;
67         bool out_buf_malloced;
68         u64 bytes_remaining;
69         const size_t stack_max = 32768;
70
71         HANDLE hFile = win32_open_existing_file(lte->file_on_disk,
72                                                 FILE_READ_DATA);
73         if (hFile == INVALID_HANDLE_VALUE) {
74                 set_errno_from_GetLastError();
75                 ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
76                 return WIMLIB_ERR_OPEN;
77         }
78
79         out_buf_malloced = false;
80         if (cb) {
81                 if (in_chunk_size <= stack_max) {
82                         out_buf = alloca(in_chunk_size);
83                 } else {
84                         out_buf = MALLOC(in_chunk_size);
85                         if (out_buf == NULL) {
86                                 ret = WIMLIB_ERR_NOMEM;
87                                 goto out_close_handle;
88                         }
89                         out_buf_malloced = true;
90                 }
91         } else {
92                 out_buf = ctx_or_buf;
93         }
94
95         bytes_remaining = size;
96         while (bytes_remaining) {
97                 DWORD bytesToRead, bytesRead;
98
99                 bytesToRead = min(in_chunk_size, bytes_remaining);
100                 if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
101                     bytesRead != bytesToRead)
102                 {
103                         set_errno_from_GetLastError();
104                         ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
105                                          lte->file_on_disk);
106                         ret = WIMLIB_ERR_READ;
107                         break;
108                 }
109                 bytes_remaining -= bytesRead;
110                 if (cb) {
111                         ret = (*cb)(out_buf, bytesRead, ctx_or_buf);
112                         if (ret)
113                                 break;
114                 } else {
115                         out_buf += bytesRead;
116                 }
117         }
118         if (out_buf_malloced)
119                 FREE(out_buf);
120 out_close_handle:
121         CloseHandle(hFile);
122         return ret;
123 }
124
125 struct win32_encrypted_read_ctx {
126         consume_data_callback_t read_prefix_cb;
127         void *read_prefix_ctx_or_buf;
128         int wimlib_err_code;
129         void *buf;
130         size_t buf_filled;
131         u64 bytes_remaining;
132         u32 in_chunk_size;
133 };
134
135 static DWORD WINAPI
136 win32_encrypted_export_cb(unsigned char *_data, void *_ctx, unsigned long len)
137 {
138         const void *data = _data;
139         struct win32_encrypted_read_ctx *ctx = _ctx;
140         u32 in_chunk_size = ctx->in_chunk_size;
141         int ret;
142
143         DEBUG("len = %lu", len);
144         if (ctx->read_prefix_cb) {
145                 /* The length of the buffer passed to the ReadEncryptedFileRaw()
146                  * export callback is undocumented, so we assume it may be of
147                  * arbitrary size. */
148                 size_t bytes_to_buffer = min(ctx->bytes_remaining - ctx->buf_filled,
149                                              len);
150                 while (bytes_to_buffer) {
151                         size_t bytes_to_copy_to_buf =
152                                 min(bytes_to_buffer, in_chunk_size - ctx->buf_filled);
153
154                         memcpy(ctx->buf + ctx->buf_filled, data,
155                                bytes_to_copy_to_buf);
156                         ctx->buf_filled += bytes_to_copy_to_buf;
157                         data += bytes_to_copy_to_buf;
158                         bytes_to_buffer -= bytes_to_copy_to_buf;
159
160                         if (ctx->buf_filled == in_chunk_size ||
161                             ctx->buf_filled == ctx->bytes_remaining)
162                         {
163                                 ret = (*ctx->read_prefix_cb)(ctx->buf,
164                                                              ctx->buf_filled,
165                                                              ctx->read_prefix_ctx_or_buf);
166                                 if (ret) {
167                                         ctx->wimlib_err_code = ret;
168                                         /* Shouldn't matter what error code is returned
169                                          * here, as long as it isn't ERROR_SUCCESS. */
170                                         return ERROR_READ_FAULT;
171                                 }
172                                 ctx->bytes_remaining -= ctx->buf_filled;
173                                 ctx->buf_filled = 0;
174                         }
175                 }
176         } else {
177                 size_t len_to_copy = min(len, ctx->bytes_remaining);
178                 ctx->read_prefix_ctx_or_buf = mempcpy(ctx->read_prefix_ctx_or_buf,
179                                                       data,
180                                                       len_to_copy);
181                 ctx->bytes_remaining -= len_to_copy;
182         }
183         return ERROR_SUCCESS;
184 }
185
186 int
187 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
188                                  u64 size,
189                                  consume_data_callback_t cb,
190                                  u32 in_chunk_size,
191                                  void *ctx_or_buf,
192                                  int _ignored_flags)
193 {
194         struct win32_encrypted_read_ctx export_ctx;
195         DWORD err;
196         void *file_ctx;
197         int ret;
198
199         DEBUG("Reading %"PRIu64" bytes from encryted file \"%ls\"",
200               size, lte->file_on_disk);
201
202         export_ctx.read_prefix_cb = cb;
203         export_ctx.read_prefix_ctx_or_buf = ctx_or_buf;
204         export_ctx.wimlib_err_code = 0;
205         if (cb) {
206                 export_ctx.buf = MALLOC(in_chunk_size);
207                 if (!export_ctx.buf)
208                         return WIMLIB_ERR_NOMEM;
209         } else {
210                 export_ctx.buf = NULL;
211         }
212         export_ctx.buf_filled = 0;
213         export_ctx.bytes_remaining = size;
214
215         err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
216         if (err != ERROR_SUCCESS) {
217                 set_errno_from_win32_error(err);
218                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
219                                  "for raw read", lte->file_on_disk);
220                 ret = WIMLIB_ERR_OPEN;
221                 goto out_free_buf;
222         }
223         err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
224                                    &export_ctx, file_ctx);
225         if (err != ERROR_SUCCESS) {
226                 set_errno_from_win32_error(err);
227                 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
228                                  lte->file_on_disk);
229                 ret = export_ctx.wimlib_err_code;
230                 if (ret == 0)
231                         ret = WIMLIB_ERR_READ;
232         } else if (export_ctx.bytes_remaining != 0) {
233                 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
234                       "encryted file \"%ls\"",
235                       size - export_ctx.bytes_remaining, size,
236                       lte->file_on_disk);
237                 ret = WIMLIB_ERR_READ;
238         } else {
239                 ret = 0;
240         }
241         CloseEncryptedFileRaw(file_ctx);
242 out_free_buf:
243         FREE(export_ctx.buf);
244         return ret;
245 }
246
247
248 static u64
249 FILETIME_to_u64(const FILETIME *ft)
250 {
251         return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
252 }
253
254 /* Load the short name of a file into a WIM dentry.
255  *
256  * If we can't read the short filename for some reason, we just ignore the error
257  * and assume the file has no short name.  This shouldn't be an issue, since the
258  * short names are essentially obsolete anyway.
259  */
260 static int
261 win32_get_short_name(HANDLE hFile, const wchar_t *path, struct wim_dentry *dentry)
262 {
263
264         /* It's not any harder to just make the NtQueryInformationFile() system
265          * call ourselves, and it saves a dumb call to FindFirstFile() which of
266          * course has to create its own handle.  */
267 #ifdef WITH_NTDLL
268         if (func_NtQueryInformationFile) {
269                 NTSTATUS status;
270                 IO_STATUS_BLOCK io_status;
271                 u8 buf[128] _aligned_attribute(8);
272                 const FILE_NAME_INFORMATION *info;
273
274                 status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
275                                                         FileAlternateNameInformation);
276                 info = (const FILE_NAME_INFORMATION*)buf;
277                 if (status == STATUS_SUCCESS && info->FileNameLength != 0) {
278                         dentry->short_name = MALLOC(info->FileNameLength + 2);
279                         if (!dentry->short_name)
280                                 return WIMLIB_ERR_NOMEM;
281                         memcpy(dentry->short_name, info->FileName,
282                                info->FileNameLength);
283                         dentry->short_name[info->FileNameLength / 2] = L'\0';
284                         dentry->short_name_nbytes = info->FileNameLength;
285                 }
286                 return 0;
287         }
288 #endif
289
290         WIN32_FIND_DATAW dat;
291         HANDLE hFind;
292         int ret = 0;
293
294         hFind = FindFirstFile(path, &dat);
295         if (hFind != INVALID_HANDLE_VALUE) {
296                 if (dat.cAlternateFileName[0] != L'\0') {
297                         DEBUG("\"%ls\": short name \"%ls\"", path, dat.cAlternateFileName);
298                         size_t short_name_nbytes = wcslen(dat.cAlternateFileName) *
299                                                    sizeof(wchar_t);
300                         size_t n = short_name_nbytes + sizeof(wchar_t);
301                         dentry->short_name = MALLOC(n);
302                         if (dentry->short_name) {
303                                 memcpy(dentry->short_name, dat.cAlternateFileName, n);
304                                 dentry->short_name_nbytes = short_name_nbytes;
305                         } else {
306                                 ret = WIMLIB_ERR_NOMEM;
307                         }
308                 }
309                 FindClose(hFind);
310         }
311         return ret;
312 }
313
314 /*
315  * win32_query_security_descriptor() - Query a file's security descriptor
316  *
317  * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
318  * format, and we currently have a handle opened with as many relevant
319  * permissions as possible.  At this point, on Windows there are a number of
320  * options for reading a file's security descriptor:
321  *
322  * GetFileSecurity():  This takes in a path and returns the
323  * SECURITY_DESCRIPTOR_RELATIVE.  Problem: this uses an internal handle, not
324  * ours, and the handle created internally doesn't specify
325  * FILE_FLAG_BACKUP_SEMANTICS.  Therefore there can be access denied errors on
326  * some files and directories, even when running as the Administrator.
327  *
328  * GetSecurityInfo():  This takes in a handle and returns the security
329  * descriptor split into a bunch of different parts.  This should work, but it's
330  * dumb because we have to put the security descriptor back together again.
331  *
332  * BackupRead():  This can read the security descriptor, but this is a
333  * difficult-to-use API, probably only works as the Administrator, and the
334  * format of the returned data is not well documented.
335  *
336  * NtQuerySecurityObject():  This is exactly what we need, as it takes in a
337  * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
338  * format.  Only problem is that it's a ntdll function and therefore not
339  * officially part of the Win32 API.  Oh well.
340  */
341 static DWORD
342 win32_query_security_descriptor(HANDLE hFile, const wchar_t *path,
343                                 SECURITY_INFORMATION requestedInformation,
344                                 SECURITY_DESCRIPTOR *buf,
345                                 DWORD bufsize, DWORD *lengthNeeded)
346 {
347 #ifdef WITH_NTDLL
348         if (func_NtQuerySecurityObject) {
349                 NTSTATUS status;
350
351                 status = (*func_NtQuerySecurityObject)(hFile,
352                                                        requestedInformation, buf,
353                                                        bufsize, lengthNeeded);
354                 /* Since it queries an already-open handle, NtQuerySecurityObject()
355                  * apparently returns STATUS_ACCESS_DENIED rather than
356                  * STATUS_PRIVILEGE_NOT_HELD.  */
357                 if (status == STATUS_ACCESS_DENIED)
358                         return ERROR_PRIVILEGE_NOT_HELD;
359                 else
360                         return (*func_RtlNtStatusToDosError)(status);
361         }
362 #endif
363         if (GetFileSecurity(path, requestedInformation, buf,
364                             bufsize, lengthNeeded))
365                 return ERROR_SUCCESS;
366         else
367                 return GetLastError();
368 }
369
370 static int
371 win32_get_security_descriptor(HANDLE hFile,
372                               const wchar_t *path,
373                               struct wim_inode *inode,
374                               struct wim_sd_set *sd_set,
375                               struct win32_capture_state *state,
376                               int add_flags)
377 {
378         SECURITY_INFORMATION requestedInformation;
379         u8 _buf[4096];
380         u8 *buf;
381         size_t bufsize;
382         DWORD lenNeeded;
383         DWORD err;
384         int ret;
385
386         requestedInformation = DACL_SECURITY_INFORMATION |
387                                SACL_SECURITY_INFORMATION |
388                                OWNER_SECURITY_INFORMATION |
389                                GROUP_SECURITY_INFORMATION;
390         buf = _buf;
391         bufsize = sizeof(_buf);
392         for (;;) {
393                 err = win32_query_security_descriptor(hFile, path,
394                                                       requestedInformation,
395                                                       (SECURITY_DESCRIPTOR*)buf,
396                                                       bufsize, &lenNeeded);
397                 switch (err) {
398                 case ERROR_SUCCESS:
399                         goto have_descriptor;
400                 case ERROR_INSUFFICIENT_BUFFER:
401                         wimlib_assert(buf == _buf);
402                         buf = MALLOC(lenNeeded);
403                         if (!buf)
404                                 return WIMLIB_ERR_NOMEM;
405                         bufsize = lenNeeded;
406                         break;
407                 case ERROR_PRIVILEGE_NOT_HELD:
408                         if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
409                                 goto fail;
410                         if (requestedInformation & SACL_SECURITY_INFORMATION) {
411                                 state->num_get_sacl_priv_notheld++;
412                                 requestedInformation &= ~SACL_SECURITY_INFORMATION;
413                                 break;
414                         }
415                         /* Fall through */
416                 case ERROR_ACCESS_DENIED:
417                         if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS)
418                                 goto fail;
419                         state->num_get_sd_access_denied++;
420                         ret = 0;
421                         goto out_free_buf;
422                 default:
423                 fail:
424                         set_errno_from_win32_error(err);
425                         ERROR_WITH_ERRNO("Failed to read security descriptor of \"%ls\"", path);
426                         ret = WIMLIB_ERR_READ;
427                         goto out_free_buf;
428                 }
429         }
430
431 have_descriptor:
432         inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
433         if (inode->i_security_id < 0)
434                 ret = WIMLIB_ERR_NOMEM;
435         else
436                 ret = 0;
437 out_free_buf:
438         if (buf != _buf)
439                 FREE(buf);
440         return ret;
441 }
442
443 static int
444 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
445                                   wchar_t *path,
446                                   size_t path_num_chars,
447                                   struct add_image_params *params,
448                                   struct win32_capture_state *state,
449                                   unsigned vol_flags);
450
451 /* Reads the directory entries of directory and recursively calls
452  * win32_build_dentry_tree() on them.  */
453 static int
454 win32_recurse_directory(HANDLE hDir,
455                         wchar_t *dir_path,
456                         size_t dir_path_num_chars,
457                         struct wim_dentry *root,
458                         struct add_image_params *params,
459                         struct win32_capture_state *state,
460                         unsigned vol_flags)
461 {
462         int ret;
463
464         DEBUG("Recurse to directory \"%ls\"", dir_path);
465
466         /* Using NtQueryDirectoryFile() we can re-use the same open handle,
467          * which we opened with FILE_FLAG_BACKUP_SEMANTICS (probably not the
468          * case for the FindFirstFile() API; it's not documented).  */
469 #ifdef WITH_NTDLL
470         if (!func_NtQueryDirectoryFile)
471                 goto use_FindFirstFile;
472
473         NTSTATUS status;
474         IO_STATUS_BLOCK io_status;
475         const size_t bufsize = 8192;
476         u8 *buf;
477         BOOL restartScan = TRUE;
478         const FILE_NAMES_INFORMATION *info;
479
480         buf = MALLOC(bufsize);
481         if (!buf)
482                 return WIMLIB_ERR_NOMEM;
483         for (;;) {
484                 status = (*func_NtQueryDirectoryFile)(hDir, NULL, NULL, NULL,
485                                                       &io_status, buf, bufsize,
486                                                       FileNamesInformation,
487                                                       FALSE, NULL, restartScan);
488                 restartScan = FALSE;
489                 if (status != STATUS_SUCCESS) {
490                         if (status == STATUS_NO_MORE_FILES ||
491                             status == STATUS_NO_MORE_ENTRIES ||
492                             status == STATUS_NO_MORE_MATCHES) {
493                                 ret = 0;
494                         } else if (status == STATUS_NOT_IMPLEMENTED ||
495                                    status == STATUS_NOT_SUPPORTED ||
496                                    status == STATUS_INVALID_INFO_CLASS) {
497                                 FREE(buf);
498                                 goto use_FindFirstFile;
499                         } else {
500                                 set_errno_from_nt_status(status);
501                                 ERROR_WITH_ERRNO("Failed to read directory "
502                                                  "\"%ls\"", dir_path);
503                                 ret = WIMLIB_ERR_READ;
504                         }
505                         goto out_free_buf;
506                 }
507                 wimlib_assert(io_status.Information != 0);
508                 info = (const FILE_NAMES_INFORMATION*)buf;
509                 for (;;) {
510                         if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
511                             !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
512                                                            info->FileName[1] == L'.'))
513                         {
514                                 wchar_t *p;
515                                 struct wim_dentry *child;
516
517                                 p = dir_path + dir_path_num_chars;
518                                 *p++ = L'\\';
519                                 p = wmempcpy(p, info->FileName,
520                                              info->FileNameLength / 2);
521                                 *p = '\0';
522
523                                 ret = win32_build_dentry_tree_recursive(
524                                                                 &child,
525                                                                 dir_path,
526                                                                 p - dir_path,
527                                                                 params,
528                                                                 state,
529                                                                 vol_flags);
530
531                                 dir_path[dir_path_num_chars] = L'\0';
532
533                                 if (ret)
534                                         goto out_free_buf;
535                                 if (child)
536                                         dentry_add_child(root, child);
537                         }
538                         if (info->NextEntryOffset == 0)
539                                 break;
540                         info = (const FILE_NAMES_INFORMATION*)
541                                         ((const u8*)info + info->NextEntryOffset);
542                 }
543         }
544 out_free_buf:
545         FREE(buf);
546         return ret;
547 #endif
548
549 use_FindFirstFile:
550         ;
551         WIN32_FIND_DATAW dat;
552         HANDLE hFind;
553         DWORD err;
554
555         /* Begin reading the directory by calling FindFirstFileW.  Unlike UNIX
556          * opendir(), FindFirstFileW has file globbing built into it.  But this
557          * isn't what we actually want, so just add a dummy glob to get all
558          * entries. */
559         dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
560         dir_path[dir_path_num_chars + 1] = L'*';
561         dir_path[dir_path_num_chars + 2] = L'\0';
562         hFind = FindFirstFile(dir_path, &dat);
563         dir_path[dir_path_num_chars] = L'\0';
564
565         if (hFind == INVALID_HANDLE_VALUE) {
566                 err = GetLastError();
567                 if (err == ERROR_FILE_NOT_FOUND) {
568                         return 0;
569                 } else {
570                         set_errno_from_win32_error(err);
571                         ERROR_WITH_ERRNO("Failed to read directory \"%ls\"",
572                                          dir_path);
573                         return WIMLIB_ERR_READ;
574                 }
575         }
576         ret = 0;
577         do {
578                 /* Skip . and .. entries */
579                 if (dat.cFileName[0] == L'.' &&
580                     (dat.cFileName[1] == L'\0' ||
581                      (dat.cFileName[1] == L'.' &&
582                       dat.cFileName[2] == L'\0')))
583                         continue;
584                 size_t filename_len = wcslen(dat.cFileName);
585
586                 dir_path[dir_path_num_chars] = OS_PREFERRED_PATH_SEPARATOR;
587                 wmemcpy(dir_path + dir_path_num_chars + 1,
588                         dat.cFileName,
589                         filename_len + 1);
590
591                 struct wim_dentry *child;
592                 size_t path_len = dir_path_num_chars + 1 + filename_len;
593                 ret = win32_build_dentry_tree_recursive(&child,
594                                                         dir_path,
595                                                         path_len,
596                                                         params,
597                                                         state,
598                                                         vol_flags);
599                 dir_path[dir_path_num_chars] = L'\0';
600                 if (ret)
601                         goto out_find_close;
602                 if (child)
603                         dentry_add_child(root, child);
604         } while (FindNextFile(hFind, &dat));
605         err = GetLastError();
606         if (err != ERROR_NO_MORE_FILES) {
607                 set_errno_from_win32_error(err);
608                 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
609                 if (ret == 0)
610                         ret = WIMLIB_ERR_READ;
611         }
612 out_find_close:
613         FindClose(hFind);
614         return ret;
615 }
616
617 /* Reparse point fixup status code */
618 enum rp_status {
619         /* Reparse point corresponded to an absolute symbolic link or junction
620          * point that pointed outside the directory tree being captured, and
621          * therefore was excluded. */
622         RP_EXCLUDED       = 0x0,
623
624         /* Reparse point was not fixed as it was either a relative symbolic
625          * link, a mount point, or something else we could not understand. */
626         RP_NOT_FIXED      = 0x1,
627
628         /* Reparse point corresponded to an absolute symbolic link or junction
629          * point that pointed inside the directory tree being captured, where
630          * the target was specified by a "full" \??\ prefixed path, and
631          * therefore was fixed to be relative to the root of the directory tree
632          * being captured. */
633         RP_FIXED_FULLPATH = 0x2,
634
635         /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
636          * have the \??\ prefix.  It may have begun with a drive letter though.
637          * */
638         RP_FIXED_ABSPATH  = 0x4,
639
640         /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
641         RP_FIXED          = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
642 };
643
644 /* Given the "substitute name" target of a Windows reparse point, try doing a
645  * fixup where we change it to be absolute relative to the root of the directory
646  * tree being captured.
647  *
648  * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
649  * set.
650  *
651  * @capture_root_ino and @capture_root_dev indicate the inode number and device
652  * of the root of the directory tree being captured.  They are meant to identify
653  * this directory (as an alternative to its actual path, which could potentially
654  * be reached via multiple destinations due to other symbolic links).  This may
655  * not work properly on FAT, which doesn't seem to supply proper inode numbers
656  * or file IDs.  However, FAT doesn't support reparse points so this function
657  * wouldn't even be called anyway.
658  */
659 static enum rp_status
660 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
661                                  u64 capture_root_ino, u64 capture_root_dev,
662                                  u32 rptag)
663 {
664         u16 target_nchars = *target_nbytes_p / 2;
665         size_t stripped_chars;
666         wchar_t *orig_target;
667         int ret;
668
669         ret = parse_substitute_name(target, *target_nbytes_p, rptag);
670         if (ret < 0)
671                 return RP_NOT_FIXED;
672         stripped_chars = ret;
673         if (stripped_chars)
674                 stripped_chars -= 2;
675         target[target_nchars] = L'\0';
676         orig_target = target;
677         target = capture_fixup_absolute_symlink(target + stripped_chars,
678                                                 capture_root_ino, capture_root_dev);
679         if (!target)
680                 return RP_EXCLUDED;
681         target_nchars = wcslen(target);
682         wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
683         *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
684         DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
685         if (stripped_chars)
686                 return RP_FIXED_FULLPATH;
687         else
688                 return RP_FIXED_ABSPATH;
689 }
690
691 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
692  * failure. */
693 static int
694 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
695                         u64 capture_root_ino, u64 capture_root_dev,
696                         const wchar_t *path)
697 {
698         struct reparse_data rpdata;
699         int ret;
700         enum rp_status rp_status;
701
702         ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
703         if (ret)
704                 return -ret;
705
706         rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
707                                                      &rpdata.substitute_name_nbytes,
708                                                      capture_root_ino,
709                                                      capture_root_dev,
710                                                      le32_to_cpu(*(le32*)rpbuf));
711         if (rp_status & RP_FIXED) {
712                 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
713                 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
714                 wmemcpy(substitute_name_copy, rpdata.substitute_name,
715                         rpdata.substitute_name_nbytes / 2);
716                 rpdata.substitute_name = substitute_name_copy;
717                 rpdata.print_name = substitute_name_copy;
718                 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
719                 if (rp_status == RP_FIXED_FULLPATH) {
720                         /* "full path", meaning \??\ prefixed.  We should not
721                          * include this prefix in the print name, as it is
722                          * apparently meant for the filesystem driver only. */
723                         rpdata.print_name += 4;
724                         rpdata.print_name_nbytes -= 8;
725                 }
726                 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
727                 if (ret == 0)
728                         ret = rp_status;
729                 else
730                         ret = -ret;
731         } else {
732                 if (rp_status == RP_EXCLUDED) {
733                         size_t print_name_nchars = rpdata.print_name_nbytes / 2;
734                         wchar_t print_name0[print_name_nchars + 1];
735                         print_name0[print_name_nchars] = L'\0';
736                         wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
737                         WARNING("Ignoring %ls pointing out of capture directory:\n"
738                                 "          \"%ls\" -> \"%ls\"\n"
739                                 "          (Use --norpfix to capture all symbolic links "
740                                 "and junction points as-is)",
741                                 (rpdata.rptag == WIM_IO_REPARSE_TAG_SYMLINK) ?
742                                         L"absolute symbolic link" : L"junction point",
743                                 path, print_name0);
744                 }
745                 ret = rp_status;
746         }
747         return ret;
748 }
749
750 /*
751  * Loads the reparse point data from a reparse point into memory, optionally
752  * fixing the targets of absolute symbolic links and junction points to be
753  * relative to the root of capture.
754  *
755  * @hFile:  Open handle to the reparse point.
756  * @path:   Path to the reparse point.  Used for error messages only.
757  * @params: Additional parameters, including whether to do reparse point fixups
758  *          or not.
759  * @rpbuf:  Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
760  *          the reparse point buffer will be loaded.
761  * @rpbuflen_ret:  On success, the length of the reparse point buffer in bytes
762  *                 is written to this location.
763  *
764  * Returns:
765  *      On success, returns an `enum rp_status' value that indicates if and/or
766  *      how the reparse point fixup was done.
767  *
768  *      On failure, returns a negative value that is a negated WIMLIB_ERR_*
769  *      code.
770  */
771 static int
772 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
773                        struct add_image_params *params,
774                        u8 *rpbuf, u16 *rpbuflen_ret)
775 {
776         DWORD bytesReturned;
777         u32 reparse_tag;
778         int ret;
779         u16 rpbuflen;
780
781         DEBUG("Loading reparse data from \"%ls\"", path);
782         if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
783                              NULL, /* "Not used with this operation; set to NULL" */
784                              0, /* "Not used with this operation; set to 0" */
785                              rpbuf, /* "A pointer to a buffer that
786                                                    receives the reparse point data */
787                              REPARSE_POINT_MAX_SIZE, /* "The size of the output
788                                                         buffer, in bytes */
789                              &bytesReturned,
790                              NULL))
791         {
792                 set_errno_from_GetLastError();
793                 ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
794                 return -WIMLIB_ERR_READ;
795         }
796         if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
797                 ERROR("Reparse data on \"%ls\" is invalid", path);
798                 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
799         }
800
801         rpbuflen = bytesReturned;
802         reparse_tag = le32_to_cpu(*(le32*)rpbuf);
803         if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
804             (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
805              reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
806         {
807                 /* Try doing reparse point fixup */
808                 ret = win32_capture_try_rpfix(rpbuf,
809                                               &rpbuflen,
810                                               params->capture_root_ino,
811                                               params->capture_root_dev,
812                                               path);
813         } else {
814                 ret = RP_NOT_FIXED;
815         }
816         *rpbuflen_ret = rpbuflen;
817         return ret;
818 }
819
820 static DWORD WINAPI
821 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
822                               unsigned long len)
823 {
824         *(u64*)_size_ret += len;
825         return ERROR_SUCCESS;
826 }
827
828 static int
829 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
830 {
831         DWORD err;
832         void *file_ctx;
833         int ret;
834
835         err = OpenEncryptedFileRaw(path, 0, &file_ctx);
836         if (err != ERROR_SUCCESS) {
837                 set_errno_from_win32_error(err);
838                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
839                                  "for raw read", path);
840                 return WIMLIB_ERR_OPEN;
841         }
842         *size_ret = 0;
843         err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
844                                    size_ret, file_ctx);
845         if (err != ERROR_SUCCESS) {
846                 set_errno_from_win32_error(err);
847                 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
848                                  "\"%ls\"", path);
849                 ret = WIMLIB_ERR_READ;
850         } else {
851                 ret = 0;
852         }
853         CloseEncryptedFileRaw(file_ctx);
854         return ret;
855 }
856
857 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
858  * stream); calculates its SHA1 message digest and either creates a `struct
859  * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
860  * wim_lookup_table_entry' for an identical stream.
861  *
862  * @path:               Path to the file (UTF-16LE).
863  *
864  * @path_num_chars:     Number of 2-byte characters in @path.
865  *
866  * @inode:              WIM inode to save the stream into.
867  *
868  * @lookup_table:       Stream lookup table for the WIM.
869  *
870  * @dat:                A `WIN32_FIND_STREAM_DATA' structure that specifies the
871  *                      stream name.
872  *
873  * Returns 0 on success; nonzero on failure.
874  */
875 static int
876 win32_capture_stream(const wchar_t *path,
877                      size_t path_num_chars,
878                      struct wim_inode *inode,
879                      struct wim_lookup_table *lookup_table,
880                      WIN32_FIND_STREAM_DATA *dat)
881 {
882         struct wim_ads_entry *ads_entry;
883         struct wim_lookup_table_entry *lte;
884         int ret;
885         wchar_t *stream_name, *colon;
886         size_t stream_name_nchars;
887         bool is_named_stream;
888         wchar_t *spath;
889         size_t spath_nchars;
890         size_t spath_buf_nbytes;
891         const wchar_t *relpath_prefix;
892         const wchar_t *colonchar;
893
894         DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
895
896         /* The stream name should be returned as :NAME:TYPE */
897         stream_name = dat->cStreamName;
898         if (*stream_name != L':')
899                 goto out_invalid_stream_name;
900         stream_name += 1;
901         colon = wcschr(stream_name, L':');
902         if (colon == NULL)
903                 goto out_invalid_stream_name;
904
905         if (wcscmp(colon + 1, L"$DATA")) {
906                 /* Not a DATA stream */
907                 ret = 0;
908                 goto out;
909         }
910
911         *colon = '\0';
912
913         stream_name_nchars = colon - stream_name;
914         is_named_stream = (stream_name_nchars != 0);
915
916         if (is_named_stream) {
917                 /* Allocate an ADS entry for the named stream. */
918                 ads_entry = inode_add_ads_utf16le(inode, stream_name,
919                                                   stream_name_nchars * sizeof(wchar_t));
920                 if (!ads_entry) {
921                         ret = WIMLIB_ERR_NOMEM;
922                         goto out;
923                 }
924         }
925
926         /* If zero length stream, no lookup table entry needed. */
927         if ((u64)dat->StreamSize.QuadPart == 0) {
928                 ret = 0;
929                 goto out;
930         }
931
932         /* Create a UTF-16LE string @spath that gives the filename, then a
933          * colon, then the stream name.  Or, if it's an unnamed stream, just the
934          * filename.  It is MALLOC()'ed so that it can be saved in the
935          * wim_lookup_table_entry if needed.
936          *
937          * As yet another special case, relative paths need to be changed to
938          * begin with an explicit "./" so that, for example, a file t:ads, where
939          * :ads is the part we added, is not interpreted as a file on the t:
940          * drive. */
941         spath_nchars = path_num_chars;
942         relpath_prefix = L"";
943         colonchar = L"";
944         if (is_named_stream) {
945                 spath_nchars += 1 + stream_name_nchars;
946                 colonchar = L":";
947                 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
948                         spath_nchars += 2;
949                         static const wchar_t _relpath_prefix[] =
950                                 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
951                         relpath_prefix = _relpath_prefix;
952                 }
953         }
954
955         spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
956         spath = MALLOC(spath_buf_nbytes);
957
958         tsprintf(spath, L"%ls%ls%ls%ls",
959                  relpath_prefix, path, colonchar, stream_name);
960
961         /* Make a new wim_lookup_table_entry */
962         lte = new_lookup_table_entry();
963         if (!lte) {
964                 ret = WIMLIB_ERR_NOMEM;
965                 goto out_free_spath;
966         }
967         lte->file_on_disk = spath;
968         spath = NULL;
969         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
970                 u64 encrypted_size;
971                 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
972                 ret = win32_get_encrypted_file_size(path, &encrypted_size);
973                 if (ret)
974                         goto out_free_spath;
975                 lte->resource_entry.original_size = encrypted_size;
976         } else {
977                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
978                 lte->resource_entry.original_size = (u64)dat->StreamSize.QuadPart;
979         }
980
981         u32 stream_id;
982         if (is_named_stream) {
983                 stream_id = ads_entry->stream_id;
984                 ads_entry->lte = lte;
985         } else {
986                 stream_id = 0;
987                 inode->i_lte = lte;
988         }
989         lookup_table_insert_unhashed(lookup_table, lte, inode, stream_id);
990         ret = 0;
991 out_free_spath:
992         FREE(spath);
993 out:
994         return ret;
995 out_invalid_stream_name:
996         ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
997         ret = WIMLIB_ERR_READ;
998         goto out;
999 }
1000
1001 /* Load information about the streams of an open file into a WIM inode.
1002  *
1003  * By default, we use the NtQueryInformationFile() system call instead of
1004  * FindFirstStream() and FindNextStream().  This is done for two reasons:
1005  *
1006  * - FindFirstStream() opens its own handle to the file or directory and
1007  *   apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
1008  *   causing access denied errors on certain files (even when running as the
1009  *   Administrator).
1010  * - FindFirstStream() and FindNextStream() is only available on Windows Vista
1011  *   and later, whereas the stream support in NtQueryInformationFile() was
1012  *   already present in Windows XP.
1013  */
1014 static int
1015 win32_capture_streams(HANDLE *hFile_p,
1016                       const wchar_t *path,
1017                       size_t path_num_chars,
1018                       struct wim_inode *inode,
1019                       struct wim_lookup_table *lookup_table,
1020                       u64 file_size,
1021                       unsigned vol_flags)
1022 {
1023         WIN32_FIND_STREAM_DATA dat;
1024         int ret;
1025 #ifdef WITH_NTDLL
1026         u8 _buf[8192] _aligned_attribute(8);
1027         u8 *buf;
1028         size_t bufsize;
1029         IO_STATUS_BLOCK io_status;
1030         NTSTATUS status;
1031         const FILE_STREAM_INFORMATION *info;
1032 #endif
1033         HANDLE hFind;
1034         DWORD err;
1035
1036         DEBUG("Capturing streams from \"%ls\"", path);
1037
1038         if (!(vol_flags & FILE_NAMED_STREAMS))
1039                 goto unnamed_only;
1040
1041 #ifdef WITH_NTDLL
1042         if (!func_NtQueryInformationFile)
1043                 goto use_FindFirstStream;
1044
1045         buf = _buf;
1046         bufsize = sizeof(_buf);
1047
1048         /* Get a buffer containing the stream information.  */
1049         for (;;) {
1050                 status = (*func_NtQueryInformationFile)(*hFile_p, &io_status,
1051                                                         buf, bufsize,
1052                                                         FileStreamInformation);
1053                 if (status == STATUS_SUCCESS) {
1054                         break;
1055                 } else if (status == STATUS_BUFFER_OVERFLOW) {
1056                         u8 *newbuf;
1057
1058                         bufsize *= 2;
1059                         if (buf == _buf)
1060                                 newbuf = MALLOC(bufsize);
1061                         else
1062                                 newbuf = REALLOC(buf, bufsize);
1063
1064                         if (!newbuf) {
1065                                 ret = WIMLIB_ERR_NOMEM;
1066                                 goto out_free_buf;
1067                         }
1068                         buf = newbuf;
1069                 } else if (status == STATUS_NOT_IMPLEMENTED ||
1070                            status == STATUS_NOT_SUPPORTED ||
1071                            status == STATUS_INVALID_INFO_CLASS) {
1072                         goto use_FindFirstStream;
1073                 } else {
1074                         set_errno_from_nt_status(status);
1075                         ERROR_WITH_ERRNO("Failed to read streams of %ls", path);
1076                         ret = WIMLIB_ERR_READ;
1077                         goto out_free_buf;
1078                 }
1079         }
1080
1081         if (io_status.Information == 0) {
1082                 /* No stream information.  */
1083                 ret = 0;
1084                 goto out_free_buf;
1085         }
1086
1087         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
1088                 /* OpenEncryptedFileRaw() seems to fail with
1089                  * ERROR_SHARING_VIOLATION if there are any handles opened to
1090                  * the file.  */
1091                 CloseHandle(*hFile_p);
1092                 *hFile_p = INVALID_HANDLE_VALUE;
1093         }
1094
1095         /* Parse one or more stream information structures.  */
1096         info = (const FILE_STREAM_INFORMATION*)buf;
1097         for (;;) {
1098                 if (info->StreamNameLength <= sizeof(dat.cStreamName) - 2) {
1099                         dat.StreamSize = info->StreamSize;
1100                         memcpy(dat.cStreamName, info->StreamName, info->StreamNameLength);
1101                         dat.cStreamName[info->StreamNameLength / 2] = L'\0';
1102
1103                         /* Capture the stream.  */
1104                         ret = win32_capture_stream(path, path_num_chars, inode,
1105                                                    lookup_table, &dat);
1106                         if (ret)
1107                                 goto out_free_buf;
1108                 }
1109                 if (info->NextEntryOffset == 0) {
1110                         /* No more stream information.  */
1111                         ret = 0;
1112                         break;
1113                 }
1114                 /* Advance to next stream information.  */
1115                 info = (const FILE_STREAM_INFORMATION*)
1116                                 ((const u8*)info + info->NextEntryOffset);
1117         }
1118 out_free_buf:
1119         /* Free buffer if allocated on heap.  */
1120         if (buf != _buf)
1121                 FREE(buf);
1122         return ret;
1123 #endif /* WITH_NTDLL */
1124
1125 use_FindFirstStream:
1126         if (win32func_FindFirstStreamW == NULL)
1127                 goto unnamed_only;
1128         hFind = win32func_FindFirstStreamW(path, FindStreamInfoStandard, &dat, 0);
1129         if (hFind == INVALID_HANDLE_VALUE) {
1130                 err = GetLastError();
1131                 if (err == ERROR_CALL_NOT_IMPLEMENTED ||
1132                     err == ERROR_NOT_SUPPORTED ||
1133                     err == ERROR_INVALID_FUNCTION ||
1134                     err == ERROR_INVALID_PARAMETER)
1135                         goto unnamed_only;
1136
1137                 /* Seems legal for this to return ERROR_HANDLE_EOF on reparse
1138                  * points and directories */
1139                 if ((inode->i_attributes &
1140                     (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1141                     && err == ERROR_HANDLE_EOF)
1142                 {
1143                         DEBUG("ERROR_HANDLE_EOF (ok)");
1144                         return 0;
1145                 } else {
1146                         if (err == ERROR_ACCESS_DENIED) {
1147                                 WARNING("Failed to look up data streams "
1148                                         "of \"%ls\": Access denied!\n%ls",
1149                                         path, capture_access_denied_msg);
1150                                 return 0;
1151                         } else {
1152                                 set_errno_from_win32_error(err);
1153                                 ERROR_WITH_ERRNO("Failed to look up data streams "
1154                                                  "of \"%ls\"", path);
1155                                 return WIMLIB_ERR_READ;
1156                         }
1157                 }
1158         }
1159         do {
1160                 ret = win32_capture_stream(path,
1161                                            path_num_chars,
1162                                            inode, lookup_table,
1163                                            &dat);
1164                 if (ret)
1165                         goto out_find_close;
1166         } while (win32func_FindNextStreamW(hFind, &dat));
1167         err = GetLastError();
1168         if (err != ERROR_HANDLE_EOF) {
1169                 set_errno_from_win32_error(err);
1170                 ERROR_WITH_ERRNO("Error reading data streams from "
1171                                  "\"%ls\"", path);
1172                 ret = WIMLIB_ERR_READ;
1173         }
1174 out_find_close:
1175         FindClose(hFind);
1176         return ret;
1177
1178 unnamed_only:
1179         /* FindFirstStream() API is not available, or the volume does not
1180          * support named streams.  Only capture the unnamed data stream. */
1181         DEBUG("Only capturing unnamed data stream");
1182         if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1183                                    FILE_ATTRIBUTE_REPARSE_POINT))
1184                 return 0;
1185
1186         wcscpy(dat.cStreamName, L"::$DATA");
1187         dat.StreamSize.QuadPart = file_size;
1188         return win32_capture_stream(path, path_num_chars,
1189                                     inode, lookup_table, &dat);
1190 }
1191
1192 static int
1193 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
1194                                   wchar_t *path,
1195                                   size_t path_num_chars,
1196                                   struct add_image_params *params,
1197                                   struct win32_capture_state *state,
1198                                   unsigned vol_flags)
1199 {
1200         struct wim_dentry *root = NULL;
1201         struct wim_inode *inode;
1202         DWORD err;
1203         u64 file_size;
1204         int ret;
1205         u8 *rpbuf;
1206         u16 rpbuflen;
1207         u16 not_rpfixed;
1208         HANDLE hFile;
1209         DWORD desiredAccess;
1210
1211         params->progress.scan.cur_path = path;
1212
1213         if (exclude_path(path, path_num_chars, params->config, true)) {
1214                 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
1215                         ERROR("Cannot exclude the root directory from capture");
1216                         ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
1217                         goto out;
1218                 }
1219                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED);
1220                 ret = 0;
1221                 goto out;
1222         }
1223
1224 #if 0
1225         if (path_num_chars >= 4 &&
1226             !wmemcmp(path, L"\\\\?\\", 4) &&
1227             path_num_chars + 1 - 4 > MAX_PATH &&
1228             state->num_long_path_warnings < MAX_CAPTURE_LONG_PATH_WARNINGS)
1229         {
1230                 WARNING("Path \"%ls\" exceeds MAX_PATH", path);
1231                 if (++state->num_long_path_warnings == MAX_CAPTURE_LONG_PATH_WARNINGS)
1232                         WARNING("Suppressing further warnings about long paths.");
1233         }
1234 #endif
1235
1236         do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK);
1237
1238         desiredAccess = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
1239                         READ_CONTROL | ACCESS_SYSTEM_SECURITY;
1240 again:
1241         hFile = win32_open_existing_file(path, desiredAccess);
1242         if (hFile == INVALID_HANDLE_VALUE) {
1243                 err = GetLastError();
1244                 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD) {
1245                         if (desiredAccess & ACCESS_SYSTEM_SECURITY) {
1246                                 desiredAccess &= ~ACCESS_SYSTEM_SECURITY;
1247                                 goto again;
1248                         }
1249                         if (desiredAccess & READ_CONTROL) {
1250                                 desiredAccess &= ~READ_CONTROL;
1251                                 goto again;
1252                         }
1253                 }
1254                 set_errno_from_GetLastError();
1255                 ERROR_WITH_ERRNO("Failed to open \"%ls\" for reading", path);
1256                 ret = WIMLIB_ERR_OPEN;
1257                 goto out;
1258         }
1259
1260         BY_HANDLE_FILE_INFORMATION file_info;
1261         if (!GetFileInformationByHandle(hFile, &file_info)) {
1262                 set_errno_from_GetLastError();
1263                 ERROR_WITH_ERRNO("Failed to get file information for \"%ls\"",
1264                                  path);
1265                 ret = WIMLIB_ERR_STAT;
1266                 goto out_close_handle;
1267         }
1268
1269         if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1270                 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1271                 ret = win32_get_reparse_data(hFile, path, params,
1272                                              rpbuf, &rpbuflen);
1273                 if (ret < 0) {
1274                         /* WIMLIB_ERR_* (inverted) */
1275                         ret = -ret;
1276                         goto out_close_handle;
1277                 } else if (ret & RP_FIXED) {
1278                         not_rpfixed = 0;
1279                 } else if (ret == RP_EXCLUDED) {
1280                         ret = 0;
1281                         goto out_close_handle;
1282                 } else {
1283                         not_rpfixed = 1;
1284                 }
1285         }
1286
1287         /* Create a WIM dentry with an associated inode, which may be shared.
1288          *
1289          * However, we need to explicitly check for directories and files with
1290          * only 1 link and refuse to hard link them.  This is because Windows
1291          * has a bug where it can return duplicate File IDs for files and
1292          * directories on the FAT filesystem. */
1293         ret = inode_table_new_dentry(&params->inode_table,
1294                                      path_basename_with_len(path, path_num_chars),
1295                                      ((u64)file_info.nFileIndexHigh << 32) |
1296                                          (u64)file_info.nFileIndexLow,
1297                                      file_info.dwVolumeSerialNumber,
1298                                      (file_info.nNumberOfLinks <= 1 ||
1299                                         (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1300                                      &root);
1301         if (ret)
1302                 goto out_close_handle;
1303
1304         ret = win32_get_short_name(hFile, path, root);
1305         if (ret)
1306                 goto out_close_handle;
1307
1308         inode = root->d_inode;
1309
1310         if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
1311                 goto out_close_handle;
1312
1313         inode->i_attributes = file_info.dwFileAttributes;
1314         inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1315         inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1316         inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1317         inode->i_resolved = 1;
1318
1319         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1320
1321         if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1322             && (vol_flags & FILE_PERSISTENT_ACLS))
1323         {
1324                 ret = win32_get_security_descriptor(hFile, path, inode,
1325                                                     &params->sd_set, state,
1326                                                     params->add_flags);
1327                 if (ret)
1328                         goto out_close_handle;
1329         }
1330
1331         file_size = ((u64)file_info.nFileSizeHigh << 32) |
1332                      (u64)file_info.nFileSizeLow;
1333
1334
1335         /* Capture the unnamed data stream (only should be present for regular
1336          * files) and any alternate data streams. */
1337         ret = win32_capture_streams(&hFile,
1338                                     path,
1339                                     path_num_chars,
1340                                     inode,
1341                                     params->lookup_table,
1342                                     file_size,
1343                                     vol_flags);
1344         if (ret)
1345                 goto out_close_handle;
1346
1347         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1348                 /* Reparse point: set the reparse data (which we read already)
1349                  * */
1350                 inode->i_not_rpfixed = not_rpfixed;
1351                 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1352                 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1353                                                params->lookup_table);
1354         } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1355                 /* Directory (not a reparse point) --- recurse to children */
1356
1357                 if (hFile == INVALID_HANDLE_VALUE) {
1358                         /* Re-open handle that was closed to read raw encrypted
1359                          * data.  */
1360                         hFile = win32_open_existing_file(path, FILE_READ_DATA);
1361                         if (hFile == INVALID_HANDLE_VALUE) {
1362                                 set_errno_from_GetLastError();
1363                                 ERROR_WITH_ERRNO("Failed to reopen \"%ls\"",
1364                                                  path);
1365                                 ret = WIMLIB_ERR_OPEN;
1366                                 goto out_close_handle;
1367                         }
1368                 }
1369                 ret = win32_recurse_directory(hFile,
1370                                               path,
1371                                               path_num_chars,
1372                                               root,
1373                                               params,
1374                                               state,
1375                                               vol_flags);
1376         }
1377 out_close_handle:
1378         CloseHandle(hFile);
1379 out:
1380         if (ret == 0)
1381                 *root_ret = root;
1382         else
1383                 free_dentry_tree(root, params->lookup_table);
1384         return ret;
1385 }
1386
1387 static void
1388 win32_do_capture_warnings(const wchar_t *path,
1389                           const struct win32_capture_state *state,
1390                           int add_flags)
1391 {
1392         if (state->num_get_sacl_priv_notheld == 0 &&
1393             state->num_get_sd_access_denied == 0)
1394                 return;
1395
1396         WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1397         if (state->num_get_sacl_priv_notheld != 0) {
1398                 WARNING("- Could not capture SACL (System Access Control List)\n"
1399                         "            on %lu files or directories.",
1400                         state->num_get_sacl_priv_notheld);
1401         }
1402         if (state->num_get_sd_access_denied != 0) {
1403                 WARNING("- Could not capture security descriptor at all\n"
1404                         "            on %lu files or directories.",
1405                         state->num_get_sd_access_denied);
1406         }
1407         WARNING("To fully capture all security descriptors, run the program\n"
1408                 "          with Administrator rights.");
1409 }
1410
1411 #define WINDOWS_NT_MAX_PATH 32768
1412
1413 /* Win32 version of capturing a directory tree */
1414 int
1415 win32_build_dentry_tree(struct wim_dentry **root_ret,
1416                         const wchar_t *root_disk_path,
1417                         struct add_image_params *params)
1418 {
1419         size_t path_nchars;
1420         wchar_t *path;
1421         int ret;
1422         struct win32_capture_state state;
1423         unsigned vol_flags;
1424         DWORD dret;
1425         bool need_prefix_free = false;
1426
1427         if (!win32func_FindFirstStreamW
1428 #ifdef WITH_NTDLL
1429             && !func_NtQueryInformationFile
1430 #endif
1431            )
1432         {
1433                 WARNING("Running on Windows XP or earlier; "
1434                         "alternate data streams will not be captured.");
1435         }
1436
1437         path_nchars = wcslen(root_disk_path);
1438         if (path_nchars > WINDOWS_NT_MAX_PATH)
1439                 return WIMLIB_ERR_INVALID_PARAM;
1440
1441         ret = win32_get_file_and_vol_ids(root_disk_path,
1442                                          &params->capture_root_ino,
1443                                          &params->capture_root_dev);
1444         if (ret) {
1445                 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1446                 return ret;
1447         }
1448
1449         win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1450
1451         /* WARNING: There is no check for overflow later when this buffer is
1452          * being used!  But it's as long as the maximum path length understood
1453          * by Windows NT (which is NOT the same as MAX_PATH). */
1454         path = MALLOC(WINDOWS_NT_MAX_PATH * sizeof(wchar_t));
1455         if (!path)
1456                 return WIMLIB_ERR_NOMEM;
1457
1458         /* Work around defective behavior in Windows where paths longer than 260
1459          * characters are not supported by default; instead they need to be
1460          * turned into absolute paths and prefixed with "\\?\".  */
1461
1462         if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1463                 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 4,
1464                                        &path[4], NULL);
1465
1466                 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 4) {
1467                         WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1468                         wmemcpy(path, root_disk_path, path_nchars + 1);
1469                 } else {
1470                         wmemcpy(path, L"\\\\?\\", 4);
1471                         path_nchars = 4 + dret;
1472                         /* Update pattern prefix */
1473                         if (params->config != NULL)
1474                         {
1475                                 params->config->_prefix = TSTRDUP(path);
1476                                 params->config->_prefix_num_tchars = path_nchars;
1477                                 if (params->config->_prefix == NULL)
1478                                 {
1479                                         ret = WIMLIB_ERR_NOMEM;
1480                                         goto out_free_path;
1481                                 }
1482                                 need_prefix_free = true;
1483                         }
1484                 }
1485         } else {
1486                 wmemcpy(path, root_disk_path, path_nchars + 1);
1487         }
1488
1489         memset(&state, 0, sizeof(state));
1490         ret = win32_build_dentry_tree_recursive(root_ret, path,
1491                                                 path_nchars, params,
1492                                                 &state, vol_flags);
1493         if (need_prefix_free)
1494                 FREE(params->config->_prefix);
1495 out_free_path:
1496         FREE(path);
1497         if (ret == 0)
1498                 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1499         return ret;
1500 }
1501
1502 #endif /* __WIN32__ */