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