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