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