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