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