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