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