]> wimlib.net Git - wimlib/blob - src/ntfs-capture.c
94e74cee4f360cb68a2f3c78e47642d7edf7b533
[wimlib] / src / ntfs-capture.c
1 /*
2  * ntfs-capture.c
3  *
4  * Capture a WIM image from a NTFS volume.  We capture everything we can,
5  * including security data and alternate data streams.
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27
28 #include "config.h"
29
30 #include <ntfs-3g/endians.h>
31 #include <ntfs-3g/types.h>
32
33 #include "buffer_io.h"
34 #include "dentry.h"
35 #include "lookup_table.h"
36 #include "security.h"
37 #include "wimlib_internal.h"
38
39 #include <ntfs-3g/layout.h>
40 #include <ntfs-3g/acls.h>
41 #include <ntfs-3g/attrib.h>
42 #include <ntfs-3g/misc.h>
43 #include <ntfs-3g/reparse.h>
44 #include <ntfs-3g/security.h> /* ntfs-3g/security.h before ntfs-3g/xattrs.h */
45 #include <ntfs-3g/xattrs.h>
46 #include <ntfs-3g/volume.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <errno.h>
50
51 #ifdef HAVE_ALLOCA_H
52 #include <alloca.h>
53 #endif
54
55 static inline ntfschar *
56 attr_record_name(ATTR_RECORD *ar)
57 {
58         return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
59 }
60
61 /* Calculates the SHA1 message digest of a NTFS attribute.
62  *
63  * @ni:  The NTFS inode containing the attribute.
64  * @ar:  The ATTR_RECORD describing the attribute.
65  * @md:  If successful, the returned SHA1 message digest.
66  * @reparse_tag_ret:    Optional pointer into which the first 4 bytes of the
67  *                              attribute will be written (to get the reparse
68  *                              point ID)
69  *
70  * Return 0 on success or nonzero on error.
71  */
72 static int
73 ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
74                   u8 md[SHA1_HASH_SIZE],
75                   bool is_reparse_point,
76                   u32 *reparse_tag_ret)
77 {
78         s64 pos = 0;
79         s64 bytes_remaining;
80         char buf[BUFFER_SIZE];
81         ntfs_attr *na;
82         SHA_CTX ctx;
83
84         na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
85                             ar->name_length);
86         if (!na) {
87                 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
88                 return WIMLIB_ERR_NTFS_3G;
89         }
90
91         bytes_remaining = na->data_size;
92
93         if (is_reparse_point) {
94                 if (ntfs_attr_pread(na, 0, 8, buf) != 8)
95                         goto out_error;
96                 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
97                 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
98                 pos = 8;
99                 bytes_remaining -= 8;
100         }
101
102         sha1_init(&ctx);
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                         goto out_error;
107                 sha1_update(&ctx, buf, to_read);
108                 pos += to_read;
109                 bytes_remaining -= to_read;
110         }
111         sha1_final(md, &ctx);
112         ntfs_attr_close(na);
113         return 0;
114 out_error:
115         ERROR_WITH_ERRNO("Error reading NTFS attribute");
116         return WIMLIB_ERR_NTFS_3G;
117 }
118
119 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
120  * lookup table */
121 static int
122 capture_ntfs_streams(struct wim_inode *inode,
123                      ntfs_inode *ni,
124                      char *path,
125                      size_t path_len,
126                      struct wim_lookup_table *lookup_table,
127                      ntfs_volume **ntfs_vol_p,
128                      ATTR_TYPES type)
129 {
130         ntfs_attr_search_ctx *actx;
131         u8 attr_hash[SHA1_HASH_SIZE];
132         struct ntfs_location *ntfs_loc = NULL;
133         int ret = 0;
134         struct wim_lookup_table_entry *lte;
135
136         DEBUG2("Capturing NTFS data streams from `%s'", path);
137
138         /* Get context to search the streams of the NTFS file. */
139         actx = ntfs_attr_get_search_ctx(ni, NULL);
140         if (!actx) {
141                 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
142                                  "context");
143                 return WIMLIB_ERR_NTFS_3G;
144         }
145
146         /* Capture each data stream or reparse data stream. */
147         while (!ntfs_attr_lookup(type, NULL, 0,
148                                  CASE_SENSITIVE, 0, NULL, 0, actx))
149         {
150                 u32 reparse_tag;
151                 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
152                 u64 name_length = actx->attr->name_length;
153                 if (data_size == 0) {
154                         if (errno != 0) {
155                                 ERROR_WITH_ERRNO("Failed to get size of attribute of "
156                                                  "`%s'", path);
157                                 ret = WIMLIB_ERR_NTFS_3G;
158                                 goto out_put_actx;
159                         }
160                         /* Empty stream.  No lookup table entry is needed. */
161                         lte = NULL;
162                 } else {
163                         if (type == AT_REPARSE_POINT && data_size < 8) {
164                                 ERROR("`%s': reparse point buffer too small",
165                                       path);
166                                 ret = WIMLIB_ERR_NTFS_3G;
167                                 goto out_put_actx;
168                         }
169                         /* Checksum the stream. */
170                         ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash,
171                                                 type == AT_REPARSE_POINT, &reparse_tag);
172                         if (ret != 0)
173                                 goto out_put_actx;
174
175                         if (type == AT_REPARSE_POINT)
176                                 inode->i_reparse_tag = reparse_tag;
177
178                         /* Make a lookup table entry for the stream, or use an existing
179                          * one if there's already an identical stream. */
180                         lte = __lookup_resource(lookup_table, attr_hash);
181                         ret = WIMLIB_ERR_NOMEM;
182                         if (lte) {
183                                 lte->refcnt++;
184                         } else {
185                                 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
186                                 if (!ntfs_loc)
187                                         goto out_put_actx;
188                                 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
189                                 ntfs_loc->path = MALLOC(path_len + 1);
190                                 if (!ntfs_loc->path)
191                                         goto out_free_ntfs_loc;
192                                 memcpy(ntfs_loc->path, path, path_len + 1);
193                                 if (name_length) {
194                                         ntfs_loc->stream_name = MALLOC(name_length * 2);
195                                         if (!ntfs_loc->stream_name)
196                                                 goto out_free_ntfs_loc;
197                                         memcpy(ntfs_loc->stream_name,
198                                                attr_record_name(actx->attr),
199                                                actx->attr->name_length * 2);
200                                         ntfs_loc->stream_name_nchars = name_length;
201                                 }
202
203                                 lte = new_lookup_table_entry();
204                                 if (!lte)
205                                         goto out_free_ntfs_loc;
206                                 lte->ntfs_loc = ntfs_loc;
207                                 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
208                                 if (type == AT_REPARSE_POINT) {
209                                         ntfs_loc->is_reparse_point = true;
210                                         lte->resource_entry.original_size = data_size - 8;
211                                         lte->resource_entry.size = data_size - 8;
212                                 } else {
213                                         ntfs_loc->is_reparse_point = false;
214                                         lte->resource_entry.original_size = data_size;
215                                         lte->resource_entry.size = data_size;
216                                 }
217                                 ntfs_loc = NULL;
218                                 copy_hash(lte->hash, attr_hash);
219                                 lookup_table_insert(lookup_table, lte);
220                         }
221                 }
222                 if (name_length == 0) {
223                         /* Unnamed data stream.  Put the reference to it in the
224                          * dentry's inode. */
225                         if (inode->i_lte) {
226                                 WARNING("Found two un-named data streams for "
227                                         "`%s'", path);
228                                 free_lookup_table_entry(lte);
229                         } else {
230                                 inode->i_lte = lte;
231                         }
232                 } else {
233                         /* Named data stream.  Put the reference to it in the
234                          * alternate data stream entries */
235                         struct wim_ads_entry *new_ads_entry;
236
237                         new_ads_entry = inode_add_ads_utf16le(inode,
238                                                               attr_record_name(actx->attr),
239                                                               name_length * 2);
240                         if (!new_ads_entry)
241                                 goto out_free_lte;
242                         wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
243                         new_ads_entry->lte = lte;
244                 }
245         }
246         ret = 0;
247         goto out_put_actx;
248 out_free_lte:
249         free_lookup_table_entry(lte);
250 out_free_ntfs_loc:
251         if (ntfs_loc) {
252                 FREE(ntfs_loc->path);
253                 FREE(ntfs_loc->stream_name);
254                 FREE(ntfs_loc);
255         }
256 out_put_actx:
257         ntfs_attr_put_search_ctx(actx);
258         if (ret == 0)
259                 DEBUG2("Successfully captured NTFS streams from `%s'", path);
260         else
261                 ERROR("Failed to capture NTFS streams from `%s", path);
262         return ret;
263 }
264
265 /* Red-black tree that maps NTFS inode numbers to DOS names */
266 struct dos_name_map {
267         struct rb_root rb_root;
268 };
269
270 struct dos_name_node {
271         struct rb_node rb_node;
272         char dos_name[24];
273         int name_nbytes;
274         u64 ntfs_ino;
275 };
276
277 /* Inserts a new DOS name into the map */
278 static int
279 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
280                 size_t name_nbytes, u64 ntfs_ino)
281 {
282         struct dos_name_node *new_node;
283         struct rb_node **p;
284         struct rb_root *root;
285         struct rb_node *rb_parent;
286
287         DEBUG("DOS name_len = %zu", name_nbytes);
288         new_node = MALLOC(sizeof(struct dos_name_node));
289         if (!new_node)
290                 return -1;
291
292         /* DOS names are supposed to be 12 characters max (that's 24 bytes,
293          * assuming 2-byte ntfs characters) */
294         wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
295
296         /* Initialize the DOS name, DOS name length, and NTFS inode number of
297          * the red-black tree node */
298         memcpy(new_node->dos_name, dos_name, name_nbytes);
299         new_node->name_nbytes = name_nbytes;
300         new_node->ntfs_ino = ntfs_ino;
301
302         /* Insert the red-black tree node */
303         root = &map->rb_root;
304         p = &root->rb_node;
305         rb_parent = NULL;
306         while (*p) {
307                 struct dos_name_node *this;
308
309                 this = container_of(*p, struct dos_name_node, rb_node);
310                 rb_parent = *p;
311                 if (new_node->ntfs_ino < this->ntfs_ino)
312                         p = &((*p)->rb_left);
313                 else if (new_node->ntfs_ino > this->ntfs_ino)
314                         p = &((*p)->rb_right);
315                 else {
316                         /* This should be impossible since a NTFS inode cannot
317                          * have multiple DOS names, and we only should get each
318                          * DOS name entry once from the ntfs_readdir() calls. */
319                         ERROR("NTFS inode %"PRIu64" has multiple DOS names",
320                               ntfs_ino);
321                         return -1;
322                 }
323         }
324         rb_link_node(&new_node->rb_node, rb_parent, p);
325         rb_insert_color(&new_node->rb_node, root);
326         DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
327         return 0;
328 }
329
330 /* Returns a structure that contains the DOS name and its length for a NTFS
331  * inode, or NULL if the inode has no DOS name. */
332 static struct dos_name_node *
333 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
334 {
335         struct rb_node *node = map->rb_root.rb_node;
336         while (node) {
337                 struct dos_name_node *this;
338                 this = container_of(node, struct dos_name_node, rb_node);
339                 if (ntfs_ino < this->ntfs_ino)
340                         node = node->rb_left;
341                 else if (ntfs_ino > this->ntfs_ino)
342                         node = node->rb_right;
343                 else
344                         return this;
345         }
346         return NULL;
347 }
348
349 static int
350 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
351 {
352         const struct dos_name_map *map = arg;
353         const struct dos_name_node *node;
354
355         if (dentry->is_win32_name) {
356                 node = lookup_dos_name(map, dentry->d_inode->i_ino);
357                 if (node) {
358                         dentry->short_name = MALLOC(node->name_nbytes + 2);
359                         if (!dentry->short_name)
360                                 return WIMLIB_ERR_NOMEM;
361                         memcpy(dentry->short_name, node->dos_name,
362                                node->name_nbytes);
363                         dentry->short_name[node->name_nbytes / 2] = 0;
364                         dentry->short_name_nbytes = node->name_nbytes;
365                         DEBUG("Assigned DOS name to ino %"PRIu64,
366                               dentry->d_inode->i_ino);
367                 } else {
368                         WARNING("NTFS inode %"PRIu64" has Win32 name with no "
369                                 "corresponding DOS name",
370                                 dentry->d_inode->i_ino);
371                 }
372         }
373         return 0;
374 }
375
376 static void
377 free_dos_name_tree(struct rb_node *node) {
378         if (node) {
379                 free_dos_name_tree(node->rb_left);
380                 free_dos_name_tree(node->rb_right);
381                 FREE(container_of(node, struct dos_name_node, rb_node));
382         }
383 }
384
385 static void
386 destroy_dos_name_map(struct dos_name_map *map)
387 {
388         free_dos_name_tree(map->rb_root.rb_node);
389 }
390
391 struct readdir_ctx {
392         struct wim_dentry *parent;
393         ntfs_inode *dir_ni;
394         char *path;
395         size_t path_len;
396         struct wim_lookup_table *lookup_table;
397         struct wim_inode_table *inode_table;
398         struct sd_set *sd_set;
399         struct dos_name_map *dos_name_map;
400         const struct wimlib_capture_config *config;
401         ntfs_volume **ntfs_vol_p;
402         int add_image_flags;
403         wimlib_progress_func_t progress_func;
404 };
405
406 static int
407 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
408                                  ntfs_inode *dir_ni,
409                                  ntfs_inode *ni,
410                                  char *path,
411                                  size_t path_len,
412                                  int name_type,
413                                  struct wim_lookup_table *lookup_table,
414                                  struct wim_inode_table *inode_table,
415                                  struct sd_set *sd_set,
416                                  const struct wimlib_capture_config *config,
417                                  ntfs_volume **ntfs_vol_p,
418                                  int add_image_flags,
419                                  wimlib_progress_func_t progress_func);
420
421 static int
422 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
423                          const int name_nchars, const int name_type,
424                          const s64 pos, const MFT_REF mref,
425                          const unsigned dt_type)
426 {
427         struct readdir_ctx *ctx;
428         size_t mbs_name_nbytes;
429         char *mbs_name;
430         struct wim_dentry *child;
431         int ret;
432         size_t path_len;
433         size_t name_nbytes = name_nchars * sizeof(ntfschar);
434
435         ctx = dirent;
436         if (name_type & FILE_NAME_DOS) {
437                 /* If this is the entry for a DOS name, store it for later. */
438                 ret = insert_dos_name(ctx->dos_name_map, name,
439                                       name_nbytes, mref & MFT_REF_MASK_CPU);
440
441                 /* Return now if an error occurred or if this is just a DOS name
442                  * and not a Win32+DOS name. */
443                 if (ret != 0 || name_type == FILE_NAME_DOS)
444                         goto out;
445         }
446         ret = utf16le_to_tstr(name, name_nbytes,
447                               &mbs_name, &mbs_name_nbytes);
448         if (ret)
449                 goto out;
450
451         if (mbs_name[0] == '.' &&
452              (mbs_name[1] == '\0' ||
453               (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
454                 /* . or .. entries
455                  *
456                  * note: name_type is POSIX for these, so DOS names will not
457                  * have been inserted for them.  */
458                 ret = 0;
459                 goto out_free_mbs_name;
460         }
461
462         /* Open the inode for this directory entry and recursively capture the
463          * directory tree rooted at it */
464         ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
465         if (!ni) {
466                 ERROR_WITH_ERRNO("Failed to open NTFS inode");
467                 ret = -1;
468                 goto out_free_mbs_name;
469         }
470         path_len = ctx->path_len;
471         if (path_len != 1)
472                 ctx->path[path_len++] = '/';
473         memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
474         path_len += mbs_name_nbytes;
475         child = NULL;
476         ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
477                                                ni, ctx->path, path_len, name_type,
478                                                ctx->lookup_table,
479                                                ctx->inode_table,
480                                                ctx->sd_set,
481                                                ctx->config, ctx->ntfs_vol_p,
482                                                ctx->add_image_flags,
483                                                ctx->progress_func);
484         if (child)
485                 dentry_add_child(ctx->parent, child);
486         ntfs_inode_close(ni);
487 out_free_mbs_name:
488         FREE(mbs_name);
489 out:
490         return ret;
491 }
492
493 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
494  * At the same time, update the WIM lookup table with lookup table entries for
495  * the NTFS streams, and build an array of security descriptors.
496  */
497 static int
498 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
499                                  ntfs_inode *dir_ni,
500                                  ntfs_inode *ni,
501                                  char *path,
502                                  size_t path_len,
503                                  int name_type,
504                                  struct wim_lookup_table *lookup_table,
505                                  struct wim_inode_table *inode_table,
506                                  struct sd_set *sd_set,
507                                  const struct wimlib_capture_config *config,
508                                  ntfs_volume **ntfs_vol_p,
509                                  int add_image_flags,
510                                  wimlib_progress_func_t progress_func)
511 {
512         u32 attributes;
513         int ret;
514         struct wim_dentry *root;
515         struct wim_inode *inode;
516
517         if (exclude_path(path, path_len, config, false)) {
518                 /* Exclude a file or directory tree based on the capture
519                  * configuration file */
520                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE)
521                     && progress_func)
522                 {
523                         union wimlib_progress_info info;
524                         info.scan.cur_path = path;
525                         info.scan.excluded = true;
526                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
527                 }
528                 root = NULL;
529                 ret = 0;
530                 goto out;
531         }
532
533         /* Get file attributes */
534         struct SECURITY_CONTEXT ctx;
535         memset(&ctx, 0, sizeof(ctx));
536         ctx.vol = ni->vol;
537         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
538                                          ni, dir_ni, (char *)&attributes,
539                                          sizeof(u32));
540         if (ret != 4) {
541                 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
542                                  path);
543                 return WIMLIB_ERR_NTFS_3G;
544         }
545
546         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
547             && progress_func)
548         {
549                 union wimlib_progress_info info;
550                 info.scan.cur_path = path;
551                 info.scan.excluded = false;
552                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
553         }
554
555         /* Create a WIM dentry with an associated inode, which may be shared */
556         ret = inode_table_new_dentry(inode_table,
557                                      path_basename_with_len(path, path_len),
558                                      ni->mft_no,
559                                      0,
560                                      &root);
561         if (ret)
562                 return ret;
563
564         inode = root->d_inode;
565
566         if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
567                 goto out;
568
569         if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
570                 root->is_win32_name = 1;
571         inode->i_creation_time    = le64_to_cpu(ni->creation_time);
572         inode->i_last_write_time  = le64_to_cpu(ni->last_data_change_time);
573         inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
574         inode->i_attributes       = le32_to_cpu(attributes);
575         inode->i_resolved         = 1;
576
577         if (attributes & FILE_ATTR_REPARSE_POINT) {
578                 /* Junction point, symbolic link, or other reparse point */
579                 ret = capture_ntfs_streams(inode, ni, path,
580                                            path_len, lookup_table,
581                                            ntfs_vol_p, AT_REPARSE_POINT);
582         } else if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
583
584                 /* Normal directory */
585                 s64 pos = 0;
586                 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
587                 struct readdir_ctx ctx = {
588                         .parent          = root,
589                         .dir_ni          = ni,
590                         .path            = path,
591                         .path_len        = path_len,
592                         .lookup_table    = lookup_table,
593                         .inode_table     = inode_table,
594                         .sd_set          = sd_set,
595                         .dos_name_map    = &dos_name_map,
596                         .config          = config,
597                         .ntfs_vol_p      = ntfs_vol_p,
598                         .add_image_flags = add_image_flags,
599                         .progress_func   = progress_func,
600                 };
601                 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
602                 if (ret) {
603                         ERROR_WITH_ERRNO("ntfs_readdir()");
604                         ret = WIMLIB_ERR_NTFS_3G;
605                 } else {
606                         ret = for_dentry_child(root, set_dentry_dos_name,
607                                                &dos_name_map);
608                 }
609                 destroy_dos_name_map(&dos_name_map);
610         } else {
611                 /* Normal file */
612                 ret = capture_ntfs_streams(inode, ni, path,
613                                            path_len, lookup_table,
614                                            ntfs_vol_p, AT_DATA);
615         }
616         if (ret)
617                 goto out;
618
619         if (!(add_image_flags & WIMLIB_ADD_IMAGE_FLAG_NO_ACLS)) {
620                 /* Get security descriptor */
621                 char _sd[1];
622                 char *sd = _sd;
623                 errno = 0;
624                 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
625                                                  ni, dir_ni, sd,
626                                                  sizeof(sd));
627                 if (ret > sizeof(sd)) {
628                         sd = alloca(ret);
629                         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
630                                                          ni, dir_ni, sd, ret);
631                 }
632                 if (ret > 0) {
633                         inode->i_security_id = sd_set_add_sd(sd_set, sd, ret);
634                         if (inode->i_security_id == -1) {
635                                 ERROR("Out of memory");
636                                 ret = WIMLIB_ERR_NOMEM;
637                                 goto out;
638                         }
639                         DEBUG("Added security ID = %u for `%s'",
640                               inode->i_security_id, path);
641                         ret = 0;
642                 } else if (ret < 0) {
643                         ERROR_WITH_ERRNO("Failed to get security information from "
644                                          "`%s'", path);
645                         ret = WIMLIB_ERR_NTFS_3G;
646                 } else {
647                         inode->i_security_id = -1;
648                         DEBUG("No security ID for `%s'", path);
649                 }
650         }
651 out:
652         if (ret == 0)
653                 *root_ret = root;
654         else
655                 free_dentry_tree(root, lookup_table);
656         return ret;
657 }
658
659 int
660 build_dentry_tree_ntfs(struct wim_dentry **root_p,
661                        const char *device,
662                        struct wim_lookup_table *lookup_table,
663                        struct wim_inode_table *inode_table,
664                        struct sd_set *sd_set,
665                        const struct wimlib_capture_config *config,
666                        int add_image_flags,
667                        wimlib_progress_func_t progress_func,
668                        void *extra_arg)
669 {
670         ntfs_volume *vol;
671         ntfs_inode *root_ni;
672         int ret;
673         ntfs_volume **ntfs_vol_p = extra_arg;
674
675         DEBUG("Mounting NTFS volume `%s' read-only", device);
676
677 #ifdef HAVE_NTFS_MNT_RDONLY
678         /* NTFS-3g 2013 */
679         vol = ntfs_mount(device, NTFS_MNT_RDONLY);
680 #else
681         /* NTFS-3g 2011, 2012 */
682         vol = ntfs_mount(device, MS_RDONLY);
683 #endif
684         if (!vol) {
685                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
686                                  device);
687                 return WIMLIB_ERR_NTFS_3G;
688         }
689         ntfs_open_secure(vol);
690
691         /* We don't want to capture the special NTFS files such as $Bitmap.  Not
692          * to be confused with "hidden" or "system" files which are real files
693          * that we do need to capture.  */
694         NVolClearShowSysFiles(vol);
695
696         DEBUG("Opening root NTFS dentry");
697         root_ni = ntfs_inode_open(vol, FILE_root);
698         if (!root_ni) {
699                 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
700                                  "`%s'", device);
701                 ret = WIMLIB_ERR_NTFS_3G;
702                 goto out;
703         }
704
705         /* Currently we assume that all the paths fit into this length and there
706          * is no check for overflow. */
707         char *path = MALLOC(32768);
708         if (!path) {
709                 ERROR("Could not allocate memory for NTFS pathname");
710                 ret = WIMLIB_ERR_NOMEM;
711                 goto out_cleanup;
712         }
713
714         path[0] = '/';
715         path[1] = '\0';
716         ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
717                                                FILE_NAME_POSIX, lookup_table,
718                                                inode_table,
719                                                sd_set,
720                                                config, ntfs_vol_p,
721                                                add_image_flags,
722                                                progress_func);
723 out_cleanup:
724         FREE(path);
725         ntfs_inode_close(root_ni);
726 out:
727         ntfs_index_ctx_put(vol->secure_xsii);
728         ntfs_index_ctx_put(vol->secure_xsdh);
729         ntfs_inode_close(vol->secure_ni);
730
731         if (ret) {
732                 if (ntfs_umount(vol, FALSE) != 0) {
733                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
734                                          device);
735                         if (ret == 0)
736                                 ret = WIMLIB_ERR_NTFS_3G;
737                 }
738         } else {
739                 /* We need to leave the NTFS volume mounted so that we can read
740                  * the NTFS files again when we are actually writing the WIM */
741                 *ntfs_vol_p = vol;
742         }
743         return ret;
744 }