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