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