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