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