]> wimlib.net Git - wimlib/blob - src/ntfs-3g_capture.c
README.WINDOWS: describe 64-bit build by default
[wimlib] / src / ntfs-3g_capture.c
1 /*
2  * ntfs-3g_capture.c
3  *
4  * Capture a WIM image directly from an NTFS volume using libntfs-3g.  We capture
5  * everything we can, including security data and alternate data streams.
6  */
7
8 /*
9  * Copyright (C) 2012-2016 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see http://www.gnu.org/licenses/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #ifdef WITH_NTFS_3G
30
31 #include <errno.h>
32
33 #include <ntfs-3g/attrib.h>
34 #include <ntfs-3g/compat.h> /* for ENODATA, if needed */
35 #include <ntfs-3g/object_id.h>
36 #include <ntfs-3g/reparse.h>
37 #include <ntfs-3g/security.h>
38 #include <ntfs-3g/volume.h>
39
40 #include "wimlib/alloca.h"
41 #include "wimlib/assert.h"
42 #include "wimlib/blob_table.h"
43 #include "wimlib/dentry.h"
44 #include "wimlib/encoding.h"
45 #include "wimlib/endianness.h"
46 #include "wimlib/error.h"
47 #include "wimlib/ntfs_3g.h"
48 #include "wimlib/object_id.h"
49 #include "wimlib/paths.h"
50 #include "wimlib/reparse.h"
51 #include "wimlib/scan.h"
52 #include "wimlib/security.h"
53
54 /* NTFS-3G 2013 renamed MS_RDONLY to NTFS_MNT_RDONLY.  We can't check for the
55  * existence of NTFS_MNT_RDONLY at compilation time because it's an enum.  We
56  * also can't check for MS_RDONLY being missing because it's also a system
57  * constant.  So check if the NTFS-3G specific MS_IGNORE_HIBERFILE is defined;
58  * if yes, then we need to use the old MS_RDONLY.  */
59 #ifdef MS_IGNORE_HIBERFILE
60 #  define NTFS_MNT_RDONLY MS_RDONLY
61 #endif
62
63 /* A reference-counted NTFS volume than is automatically unmounted when the
64  * reference count reaches 0  */
65 struct ntfs_volume_wrapper {
66         ntfs_volume *vol;
67         size_t refcnt;
68 };
69
70 /* Description of where data is located in an NTFS volume  */
71 struct ntfs_location {
72         struct ntfs_volume_wrapper *volume;
73         u64 mft_no;
74         ATTR_TYPES attr_type;
75         u32 attr_name_nchars;
76         ntfschar *attr_name;
77         u64 sort_key;
78 };
79
80 static struct ntfs_volume_wrapper *
81 get_ntfs_volume(struct ntfs_volume_wrapper *volume)
82 {
83         volume->refcnt++;
84         return volume;
85 }
86
87 static void
88 put_ntfs_volume(struct ntfs_volume_wrapper *volume)
89 {
90         if (--volume->refcnt == 0) {
91                 ntfs_umount(volume->vol, FALSE);
92                 FREE(volume);
93         }
94 }
95
96 static inline const ntfschar *
97 attr_record_name(const ATTR_RECORD *record)
98 {
99         return (const ntfschar *)
100                 ((const u8 *)record + le16_to_cpu(record->name_offset));
101 }
102
103 static ntfs_attr *
104 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
105 {
106         ntfs_attr *na;
107
108         na = ntfs_attr_open(ni, loc->attr_type, loc->attr_name,
109                             loc->attr_name_nchars);
110         if (!na) {
111                 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
112                                  loc->mft_no);
113         }
114         return na;
115 }
116
117 int
118 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
119                            const struct read_blob_callbacks *cbs)
120 {
121         const struct ntfs_location *loc = blob->ntfs_loc;
122         ntfs_volume *vol = loc->volume->vol;
123         ntfs_inode *ni;
124         ntfs_attr *na;
125         s64 pos;
126         s64 bytes_remaining;
127         int ret;
128         u8 buf[BUFFER_SIZE];
129
130         ni = ntfs_inode_open(vol, loc->mft_no);
131         if (!ni) {
132                 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
133                                  loc->mft_no);
134                 ret = WIMLIB_ERR_NTFS_3G;
135                 goto out;
136         }
137
138         na = open_ntfs_attr(ni, loc);
139         if (!na) {
140                 ret = WIMLIB_ERR_NTFS_3G;
141                 goto out_close_ntfs_inode;
142         }
143
144         pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
145         bytes_remaining = size;
146         while (bytes_remaining) {
147                 s64 to_read = min(bytes_remaining, sizeof(buf));
148                 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
149                         ERROR_WITH_ERRNO("Error reading data from NTFS inode "
150                                          "%"PRIu64, loc->mft_no);
151                         ret = WIMLIB_ERR_NTFS_3G;
152                         goto out_close_ntfs_attr;
153                 }
154                 pos += to_read;
155                 bytes_remaining -= to_read;
156                 ret = call_consume_chunk(buf, to_read, cbs);
157                 if (ret)
158                         goto out_close_ntfs_attr;
159         }
160         ret = 0;
161 out_close_ntfs_attr:
162         ntfs_attr_close(na);
163 out_close_ntfs_inode:
164         ntfs_inode_close(ni);
165 out:
166         return ret;
167 }
168
169 void
170 free_ntfs_location(struct ntfs_location *loc)
171 {
172         put_ntfs_volume(loc->volume);
173         if (loc->attr_name != AT_UNNAMED)
174                 FREE(loc->attr_name);
175         FREE(loc);
176 }
177
178 struct ntfs_location *
179 clone_ntfs_location(const struct ntfs_location *loc)
180 {
181         struct ntfs_location *new = memdup(loc, sizeof(*loc));
182         if (!new)
183                 goto err0;
184         if (loc->attr_name != AT_UNNAMED) {
185                 new->attr_name = utf16le_dup(loc->attr_name);
186                 if (!new->attr_name)
187                         goto err1;
188         }
189         new->volume = get_ntfs_volume(loc->volume);
190         return new;
191
192 err1:
193         FREE(new);
194 err0:
195         return NULL;
196 }
197
198 int
199 cmp_ntfs_locations(const struct ntfs_location *loc1,
200                    const struct ntfs_location *loc2)
201 {
202         return cmp_u64(loc1->sort_key, loc2->sort_key);
203 }
204
205 /* Read rptag and rpreserved from the NTFS inode and save them in the WIM inode.
206  */
207 static int
208 read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
209 {
210         struct {
211                 le32 rptag;
212                 le16 rpdatalen;
213                 le16 rpreserved;
214         } hdr;
215         s64 res;
216         ntfs_attr *na;
217
218         na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0);
219         if (!na)
220                 return WIMLIB_ERR_NTFS_3G;
221
222         res = ntfs_attr_pread(na, 0, sizeof(hdr), &hdr);
223
224         ntfs_attr_close(na);
225
226         if (res != sizeof(hdr))
227                 return WIMLIB_ERR_NTFS_3G;
228
229         inode->i_reparse_tag = le32_to_cpu(hdr.rptag);
230         inode->i_rp_reserved = le16_to_cpu(hdr.rpreserved);
231         return 0;
232 }
233
234 static int
235 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
236 {
237         switch (type) {
238         case AT_DATA:
239                 return STREAM_TYPE_DATA;
240         case AT_REPARSE_POINT:
241                 return STREAM_TYPE_REPARSE_POINT;
242         default:
243                 wimlib_assert(0);
244                 return STREAM_TYPE_UNKNOWN;
245         }
246 }
247
248 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
249  * first by starting LCN of the attribute if available, otherwise no sort order
250  * is defined.  This usually results in better sequential access to the volume.
251  */
252 static int
253 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
254 {
255         ntfs_attr *na;
256         runlist_element *rl;
257
258         na = open_ntfs_attr(ni, loc);
259         if (!na)
260                 return WIMLIB_ERR_NTFS_3G;
261
262         rl = ntfs_attr_find_vcn(na, 0);
263         if (rl && rl->lcn != LCN_HOLE)
264                 loc->sort_key = rl->lcn;
265         else
266                 loc->sort_key = 0;
267
268         ntfs_attr_close(na);
269         return 0;
270 }
271
272 /*
273  * Add a new stream to the specified inode, with duplicate checking.
274  *
275  * This works around a problem where NTFS-3G can list multiple unnamed data
276  * streams for a single file.  In this case we can only keep one.  We'll prefer
277  * one that is nonempty.
278  */
279 static int
280 add_stream(struct wim_inode *inode, const char *path, int stream_type,
281            const utf16lechar *stream_name, struct blob_descriptor **blob_p,
282            struct list_head *unhashed_blobs)
283 {
284         struct blob_descriptor *blob = *blob_p;
285         struct wim_inode_stream *strm;
286
287         strm = inode_get_stream(inode, stream_type, stream_name);
288         if (unlikely(strm)) {
289                 /* Stream already existed.  */
290                 if (!blob)
291                         return 0;
292                 if (stream_blob_resolved(strm)) {
293                         WARNING("\"%s\" has multiple nonempty streams "
294                                 "with the same type and name!  Only the first "
295                                 "will be saved.", path);
296                         return 0;
297                 }
298                 inode_replace_stream_blob(inode, strm, blob, NULL);
299         } else {
300                 strm = inode_add_stream(inode, stream_type, stream_name, blob);
301                 if (unlikely(!strm))
302                         return WIMLIB_ERR_NOMEM;
303         }
304         prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
305         *blob_p = NULL;
306         return 0;
307
308 }
309
310 /* Save information about an NTFS attribute (stream) to a WIM inode.  */
311 static int
312 scan_ntfs_attr(struct wim_inode *inode,
313                ntfs_inode *ni,
314                const char *path,
315                struct list_head *unhashed_blobs,
316                struct ntfs_volume_wrapper *volume,
317                ATTR_TYPES type,
318                const ATTR_RECORD *record)
319 {
320         u64 data_size = ntfs_get_attribute_value_length(record);
321         const u32 name_nchars = record->name_length;
322         struct blob_descriptor *blob = NULL;
323         utf16lechar *stream_name = (utf16lechar *)NO_STREAM_NAME;
324         int ret;
325
326         if (unlikely(name_nchars)) {
327                 /* Named stream  */
328                 stream_name = utf16le_dupz(attr_record_name(record),
329                                            name_nchars * sizeof(ntfschar));
330                 if (!stream_name) {
331                         ret = WIMLIB_ERR_NOMEM;
332                         goto out_cleanup;
333                 }
334         }
335
336         if (unlikely(type == AT_REPARSE_POINT)) {
337                 if (data_size < REPARSE_DATA_OFFSET) {
338                         ERROR("Reparse point attribute of \"%s\" "
339                               "is too short!", path);
340                         ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
341                         goto out_cleanup;
342                 }
343                 data_size -= REPARSE_DATA_OFFSET;
344
345                 ret = read_reparse_header(ni, inode);
346                 if (ret) {
347                         ERROR_WITH_ERRNO("Error reading reparse point header "
348                                          "of \"%s\"", path);
349                         goto out_cleanup;
350                 }
351         }
352
353         /* If the stream is non-empty, set up a blob descriptor for it.  */
354         if (data_size != 0) {
355                 blob = new_blob_descriptor();
356                 if (unlikely(!blob)) {
357                         ret = WIMLIB_ERR_NOMEM;
358                         goto out_cleanup;
359                 }
360
361                 blob->ntfs_loc = MALLOC(sizeof(struct ntfs_location));
362                 if (unlikely(!blob->ntfs_loc)) {
363                         ret = WIMLIB_ERR_NOMEM;
364                         goto out_cleanup;
365                 }
366
367                 blob->blob_location = BLOB_IN_NTFS_VOLUME;
368                 blob->size = data_size;
369                 blob->ntfs_loc->volume = get_ntfs_volume(volume);
370                 blob->ntfs_loc->mft_no = ni->mft_no;
371                 blob->ntfs_loc->attr_type = type;
372
373                 if (unlikely(name_nchars)) {
374                         blob->ntfs_loc->attr_name_nchars = name_nchars;
375                         blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
376                         if (!blob->ntfs_loc->attr_name) {
377                                 ret = WIMLIB_ERR_NOMEM;
378                                 goto out_cleanup;
379                         }
380                 } else {
381                         blob->ntfs_loc->attr_name_nchars = 0;
382                         blob->ntfs_loc->attr_name = AT_UNNAMED;
383                 }
384
385                 ret = set_attr_sort_key(ni, blob->ntfs_loc);
386                 if (ret)
387                         goto out_cleanup;
388         }
389
390         ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
391                          stream_name, &blob, unhashed_blobs);
392 out_cleanup:
393         free_blob_descriptor(blob);
394         if (stream_name != NO_STREAM_NAME)
395                 FREE(stream_name);
396         return ret;
397 }
398
399 /* Scan attributes of the specified type from a file in the NTFS volume  */
400 static int
401 scan_ntfs_attrs_with_type(struct wim_inode *inode,
402                           ntfs_inode *ni,
403                           const char *path,
404                           struct list_head *unhashed_blobs,
405                           struct ntfs_volume_wrapper *volume,
406                           ATTR_TYPES type)
407 {
408         ntfs_attr_search_ctx *actx;
409         int ret;
410
411         actx = ntfs_attr_get_search_ctx(ni, NULL);
412         if (!actx) {
413                 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
414                                  "context for \"%s\"", path);
415                 return WIMLIB_ERR_NTFS_3G;
416         }
417
418         while (!ntfs_attr_lookup(type, NULL, 0,
419                                  CASE_SENSITIVE, 0, NULL, 0, actx))
420         {
421                 ret = scan_ntfs_attr(inode,
422                                      ni,
423                                      path,
424                                      unhashed_blobs,
425                                      volume,
426                                      type,
427                                      actx->attr);
428                 if (ret)
429                         goto out_put_actx;
430         }
431         if (errno != ENOENT) {
432                 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
433                 ret = WIMLIB_ERR_NTFS_3G;
434                 goto out_put_actx;
435         }
436         ret = 0;
437 out_put_actx:
438         ntfs_attr_put_search_ctx(actx);
439         return ret;
440 }
441
442 static noinline_for_stack int
443 load_object_id(ntfs_inode *ni, struct wim_inode *inode)
444 {
445         OBJECT_ID_ATTR attr;
446         int len;
447
448         len = ntfs_get_ntfs_object_id(ni, (char *)&attr, sizeof(attr));
449         if (likely(len == -ENODATA || len == 0))
450                 return 0;
451         if (len < 0)
452                 return WIMLIB_ERR_NTFS_3G;
453         if (!inode_set_object_id(inode, &attr, len))
454                 return WIMLIB_ERR_NOMEM;
455         return 0;
456 }
457
458 /* Load the security descriptor of an NTFS inode into the corresponding WIM
459  * inode and the WIM image's security descriptor set.  */
460 static noinline_for_stack int
461 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
462                         ntfs_volume *vol, struct wim_sd_set *sd_set)
463 {
464         struct SECURITY_CONTEXT scx = {.vol = vol};
465         char _buf[4096];
466         char *buf = _buf;
467         size_t avail_size = sizeof(_buf);
468         int ret;
469
470 retry:
471         ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
472         if (unlikely(ret < 0)) {
473                 ret = WIMLIB_ERR_NTFS_3G;
474                 goto out;
475         }
476
477         if (unlikely(ret > avail_size)) {
478                 if (unlikely(buf != _buf))
479                         FREE(buf);
480                 buf = MALLOC(ret);
481                 if (!buf) {
482                         ret = WIMLIB_ERR_NOMEM;
483                         goto out;
484                 }
485                 avail_size = ret;
486                 goto retry;
487         }
488
489         if (likely(ret > 0)) {
490                 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
491                 if (unlikely(inode->i_security_id < 0)) {
492                         ret = WIMLIB_ERR_NOMEM;
493                         goto out;
494                 }
495         }
496
497         ret = 0;
498 out:
499         if (unlikely(buf != _buf))
500                 FREE(buf);
501         return ret;
502 }
503
504 /* Binary tree that maps NTFS inode numbers to DOS names */
505 struct dos_name_map {
506         struct avl_tree_node *root;
507 };
508
509 struct dos_name_node {
510         struct avl_tree_node index_node;
511         char dos_name[24];
512         int name_nbytes;
513         u64 ntfs_ino;
514 };
515
516 #define DOS_NAME_NODE(avl_node) \
517         avl_tree_entry(avl_node, struct dos_name_node, index_node)
518
519 static int
520 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
521                      const struct avl_tree_node *n2)
522 {
523         return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
524                        DOS_NAME_NODE(n2)->ntfs_ino);
525 }
526
527 /* Inserts a new DOS name into the map */
528 static int
529 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
530                 size_t name_nbytes, u64 ntfs_ino)
531 {
532         struct dos_name_node *new_node;
533
534         new_node = MALLOC(sizeof(struct dos_name_node));
535         if (!new_node)
536                 return WIMLIB_ERR_NOMEM;
537
538         /* DOS names are supposed to be 12 characters max (that's 24 bytes,
539          * assuming 2-byte ntfs characters) */
540         wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
541
542         /* Initialize the DOS name, DOS name length, and NTFS inode number of
543          * the search tree node */
544         memcpy(new_node->dos_name, dos_name, name_nbytes);
545         new_node->name_nbytes = name_nbytes;
546         new_node->ntfs_ino = ntfs_ino;
547
548         /* Insert the search tree node */
549         if (avl_tree_insert(&map->root, &new_node->index_node,
550                             _avl_cmp_by_ntfs_ino))
551         {
552                 /* This should be impossible since an NTFS inode cannot have
553                  * multiple DOS names, and we only should get each DOS name
554                  * entry once from the ntfs_readdir() calls. */
555                 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
556                 FREE(new_node);
557         }
558         return 0;
559 }
560
561 /* Returns a structure that contains the DOS name and its length for an NTFS
562  * inode, or NULL if the inode has no DOS name. */
563 static struct dos_name_node *
564 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
565 {
566         struct dos_name_node dummy;
567         struct avl_tree_node *res;
568
569         dummy.ntfs_ino = ntfs_ino;
570
571         res = avl_tree_lookup_node(map->root, &dummy.index_node,
572                                    _avl_cmp_by_ntfs_ino);
573         if (!res)
574                 return NULL;
575         return DOS_NAME_NODE(res);
576 }
577
578 static int
579 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
580 {
581         const struct dos_name_node *node;
582
583         if (dentry->d_is_win32_name) {
584                 node = lookup_dos_name(map, dentry->d_inode->i_ino);
585                 if (node) {
586                         dentry->d_short_name = utf16le_dupz(node->dos_name,
587                                                             node->name_nbytes);
588                         if (!dentry->d_short_name)
589                                 return WIMLIB_ERR_NOMEM;
590                         dentry->d_short_name_nbytes = node->name_nbytes;
591                 } else {
592                         WARNING("NTFS inode %"PRIu64" has Win32 name with no "
593                                 "corresponding DOS name",
594                                 dentry->d_inode->i_ino);
595                 }
596         }
597         return 0;
598 }
599
600 static void
601 destroy_dos_name_map(struct dos_name_map *map)
602 {
603         struct dos_name_node *node;
604         avl_tree_for_each_in_postorder(node, map->root,
605                                        struct dos_name_node, index_node)
606                 FREE(node);
607 }
608
609 struct readdir_ctx {
610         struct wim_dentry *parent;
611         char *path;
612         size_t path_len;
613         struct dos_name_map dos_name_map;
614         struct ntfs_volume_wrapper *volume;
615         struct scan_params *params;
616         int ret;
617 };
618
619 static int
620 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
621                                     const MFT_REF mref,
622                                     char *path,
623                                     size_t path_len,
624                                     int name_type,
625                                     struct ntfs_volume_wrapper *volume,
626                                     struct scan_params *params);
627
628 static int
629 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
630         const int name_type, const s64 pos, const MFT_REF mref,
631         const unsigned dt_type)
632 {
633         struct readdir_ctx *ctx = _ctx;
634         const size_t name_nbytes = name_nchars * sizeof(ntfschar);
635         char *mbs_name;
636         size_t mbs_name_nbytes;
637         size_t path_len;
638         struct wim_dentry *child;
639         int ret;
640
641         if (name_type & FILE_NAME_DOS) {
642                 /* If this is the entry for a DOS name, store it for later. */
643                 ret = insert_dos_name(&ctx->dos_name_map, name,
644                                       name_nbytes, MREF(mref));
645
646                 /* Return now if an error occurred or if this is just a DOS name
647                  * and not a Win32+DOS name. */
648                 if (ret != 0 || name_type == FILE_NAME_DOS)
649                         goto out;
650         }
651
652         ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
653         if (ret)
654                 goto out;
655
656         if (should_ignore_filename(mbs_name, mbs_name_nbytes))
657                 goto out_free_mbs_name;
658
659         path_len = ctx->path_len;
660         if (path_len != 1)
661                 ctx->path[path_len++] = '/';
662         memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
663         path_len += mbs_name_nbytes;
664         child = NULL;
665         ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
666                                                   path_len, name_type,
667                                                   ctx->volume, ctx->params);
668         attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
669 out_free_mbs_name:
670         FREE(mbs_name);
671 out:
672         ctx->ret = ret;
673         return ret;
674 }
675
676 static int
677 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
678                           struct wim_dentry *parent,
679                           struct ntfs_volume_wrapper *volume,
680                           struct scan_params *params)
681 {
682         int ret;
683         s64 pos = 0;
684         struct readdir_ctx ctx = {
685                 .parent          = parent,
686                 .path            = path,
687                 .path_len        = path_len,
688                 .dos_name_map    = { .root = NULL },
689                 .volume          = volume,
690                 .params          = params,
691                 .ret             = 0,
692         };
693         ret = ntfs_readdir(ni, &pos, &ctx, filldir);
694         path[path_len] = '\0';
695         if (unlikely(ret)) {
696                 if (ctx.ret) {
697                         /* wimlib error  */
698                         ret = ctx.ret;
699                 } else {
700                         /* error from ntfs_readdir() itself  */
701                         ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
702                         ret = WIMLIB_ERR_NTFS_3G;
703                 }
704         } else {
705                 struct wim_dentry *child;
706
707                 ret = 0;
708                 for_dentry_child(child, parent) {
709                         ret = set_dentry_dos_name(child, &ctx.dos_name_map);
710                         if (ret)
711                                 break;
712                 }
713         }
714         destroy_dos_name_map(&ctx.dos_name_map);
715         return ret;
716 }
717
718 static int
719 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
720                                     const MFT_REF mref,
721                                     char *path,
722                                     size_t path_len,
723                                     int name_type,
724                                     struct ntfs_volume_wrapper *volume,
725                                     struct scan_params *params)
726 {
727         u32 attributes;
728         int ret;
729         struct wim_dentry *root = NULL;
730         struct wim_inode *inode = NULL;
731         ntfs_inode *ni = NULL;
732
733         ret = try_exclude(path, params);
734         if (unlikely(ret < 0)) /* Excluded? */
735                 goto out_progress;
736         if (unlikely(ret > 0)) /* Error? */
737                 goto out;
738
739         ni = ntfs_inode_open(volume->vol, mref);
740         if (!ni) {
741                 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
742                 ret = WIMLIB_ERR_NTFS_3G;
743                 goto out;
744         }
745
746         /* Get file attributes */
747         ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
748         if (ret != sizeof(attributes)) {
749                 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
750                 ret = WIMLIB_ERR_NTFS_3G;
751                 goto out;
752         }
753
754         if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
755                 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
756                 {
757                         ERROR("Can't archive \"%s\" because NTFS-3G capture mode "
758                               "does not support encrypted files and directories", path);
759                         ret = WIMLIB_ERR_UNSUPPORTED_FILE;
760                         goto out;
761                 }
762                 params->progress.scan.cur_path = path;
763                 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
764                 goto out;
765         }
766
767         /* Create a WIM dentry with an associated inode, which may be shared */
768         ret = inode_table_new_dentry(params->inode_table,
769                                      path_basename_with_len(path, path_len),
770                                      ni->mft_no, 0, false, &root);
771         if (ret)
772                 goto out;
773
774         if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
775                 root->d_is_win32_name = 1;
776
777         inode = root->d_inode;
778
779         if (inode->i_nlink > 1) {
780                 /* Shared inode; nothing more to do */
781                 goto out_progress;
782         }
783
784         inode->i_creation_time    = le64_to_cpu(ni->creation_time);
785         inode->i_last_write_time  = le64_to_cpu(ni->last_data_change_time);
786         inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
787         inode->i_attributes       = attributes;
788
789         if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
790                 /* Scan the reparse point stream.  */
791                 ret = scan_ntfs_attrs_with_type(inode, ni, path,
792                                                 params->unhashed_blobs,
793                                                 volume, AT_REPARSE_POINT);
794                 if (ret)
795                         goto out;
796         }
797
798         /* Load the object ID.  */
799         ret = load_object_id(ni, inode);
800         if (ret) {
801                 ERROR_WITH_ERRNO("Error reading object ID of \"%s\"", path);
802                 goto out;
803         }
804
805         /* Scan the data streams.
806          *
807          * Note: directories should not have an unnamed data stream, but they
808          * may have named data streams.  Nondirectories (including reparse
809          * points) can have an unnamed data stream as well as named data
810          * streams.  */
811         ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
812                                         volume, AT_DATA);
813         if (ret)
814                 goto out;
815
816         /* Reparse-point fixups are a no-op because in NTFS-3G capture mode we
817          * only allow capturing an entire volume. */
818         if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
819             inode_is_symlink(inode))
820                 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
821
822         if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
823                 ret = get_security_descriptor(ni, inode, volume->vol,
824                                               params->sd_set);
825                 if (ret) {
826                         ERROR_WITH_ERRNO("Error reading security descriptor "
827                                          "of \"%s\"", path);
828                         goto out;
829                 }
830         }
831
832         if (inode_is_directory(inode)) {
833                 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
834                                                 volume, params);
835                 if (ret)
836                         goto out;
837         }
838
839 out_progress:
840         params->progress.scan.cur_path = path;
841         if (root == NULL)
842                 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
843         else
844                 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
845 out:
846         if (ni)
847                 ntfs_inode_close(ni);
848         if (unlikely(ret)) {
849                 free_dentry_tree(root, params->blob_table);
850                 root = NULL;
851                 ret = report_scan_error(params, ret, path);
852         }
853         *root_ret = root;
854         return ret;
855 }
856
857 int
858 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
859                           const char *device, struct scan_params *params)
860 {
861         struct ntfs_volume_wrapper *volume;
862         ntfs_volume *vol;
863         char *path;
864         int ret;
865
866         volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
867         if (!volume)
868                 return WIMLIB_ERR_NOMEM;
869
870         vol = ntfs_mount(device, NTFS_MNT_RDONLY);
871         if (!vol) {
872                 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
873                                  device);
874                 FREE(volume);
875                 return WIMLIB_ERR_NTFS_3G;
876         }
877
878         volume->vol = vol;
879         volume->refcnt = 1;
880
881         ntfs_open_secure(vol);
882
883         /* We don't want to capture the special NTFS files such as $Bitmap.  Not
884          * to be confused with "hidden" or "system" files which are real files
885          * that we do need to capture.  */
886         NVolClearShowSysFiles(vol);
887
888         /* Currently we assume that all the paths fit into this length and there
889          * is no check for overflow.  */
890         path = MALLOC(32768);
891         if (!path) {
892                 ret = WIMLIB_ERR_NOMEM;
893                 goto out_put_ntfs_volume;
894         }
895
896         path[0] = '/';
897         path[1] = '\0';
898         ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
899                                                   FILE_NAME_POSIX, volume,
900                                                   params);
901         FREE(path);
902 out_put_ntfs_volume:
903         ntfs_index_ctx_put(vol->secure_xsii);
904         ntfs_index_ctx_put(vol->secure_xsdh);
905         ntfs_inode_close(vol->secure_ni);
906         put_ntfs_volume(volume);
907         return ret;
908 }
909 #endif /* WITH_NTFS_3G */