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