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