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