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