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