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