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