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