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