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