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