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