]> wimlib.net Git - wimlib/blob - src/ntfs-capture.c
Clean up headers
[wimlib] / src / ntfs-capture.c
1 /*
2  * ntfs-capture.c
3  *
4  * Capture a WIM image from a NTFS volume.  We capture everything we can,
5  * 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
28 #include "config.h"
29
30 #include <ntfs-3g/endians.h>
31 #include <ntfs-3g/types.h>
32
33 #include "buffer_io.h"
34 #include "dentry.h"
35 #include "lookup_table.h"
36 #include "security.h"
37 #include "wimlib_internal.h"
38
39 #include <ntfs-3g/layout.h>
40 #include <ntfs-3g/acls.h>
41 #include <ntfs-3g/attrib.h>
42 #include <ntfs-3g/misc.h>
43 #include <ntfs-3g/reparse.h>
44 #include <ntfs-3g/security.h> /* ntfs-3g/security.h before ntfs-3g/xattrs.h */
45 #include <ntfs-3g/xattrs.h>
46 #include <ntfs-3g/volume.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <errno.h>
50
51 #ifdef HAVE_ALLOCA_H
52 #include <alloca.h>
53 #endif
54
55 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
56 {
57         return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
58 }
59
60 /* Calculates the SHA1 message digest of a NTFS attribute.
61  *
62  * @ni:  The NTFS inode containing the attribute.
63  * @ar:  The ATTR_RECORD describing the attribute.
64  * @md:  If successful, the returned SHA1 message digest.
65  * @reparse_tag_ret:    Optional pointer into which the first 4 bytes of the
66  *                              attribute will be written (to get the reparse
67  *                              point ID)
68  *
69  * Return 0 on success or nonzero on error.
70  */
71 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
72                              u8 md[SHA1_HASH_SIZE],
73                              bool is_reparse_point,
74                              u32 *reparse_tag_ret)
75 {
76         s64 pos = 0;
77         s64 bytes_remaining;
78         char buf[BUFFER_SIZE];
79         ntfs_attr *na;
80         SHA_CTX ctx;
81
82         na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
83                             ar->name_length);
84         if (!na) {
85                 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
86                 return WIMLIB_ERR_NTFS_3G;
87         }
88
89         bytes_remaining = na->data_size;
90
91         if (is_reparse_point) {
92                 if (ntfs_attr_pread(na, 0, 8, buf) != 8)
93                         goto out_error;
94                 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
95                 DEBUG("ReparseTag = %#x", *reparse_tag_ret);
96                 pos = 8;
97                 bytes_remaining -= 8;
98         }
99
100         sha1_init(&ctx);
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                         goto out_error;
105                 sha1_update(&ctx, buf, to_read);
106                 pos += to_read;
107                 bytes_remaining -= to_read;
108         }
109         sha1_final(md, &ctx);
110         ntfs_attr_close(na);
111         return 0;
112 out_error:
113         ERROR_WITH_ERRNO("Error reading NTFS attribute");
114         return WIMLIB_ERR_NTFS_3G;
115 }
116
117 /* Load the streams from a file or reparse point in the NTFS volume into the WIM
118  * lookup table */
119 static int capture_ntfs_streams(struct wim_dentry *dentry, ntfs_inode *ni,
120                                 char path[], size_t path_len,
121                                 struct wim_lookup_table *lookup_table,
122                                 ntfs_volume **ntfs_vol_p,
123                                 ATTR_TYPES type)
124 {
125         ntfs_attr_search_ctx *actx;
126         u8 attr_hash[SHA1_HASH_SIZE];
127         struct ntfs_location *ntfs_loc = NULL;
128         int ret = 0;
129         struct wim_lookup_table_entry *lte;
130
131         DEBUG2("Capturing NTFS data streams from `%s'", path);
132
133         /* Get context to search the streams of the NTFS file. */
134         actx = ntfs_attr_get_search_ctx(ni, NULL);
135         if (!actx) {
136                 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
137                                  "context");
138                 return WIMLIB_ERR_NTFS_3G;
139         }
140
141         /* Capture each data stream or reparse data stream. */
142         while (!ntfs_attr_lookup(type, NULL, 0,
143                                  CASE_SENSITIVE, 0, NULL, 0, actx))
144         {
145                 char *stream_name_utf8;
146                 u32 reparse_tag;
147                 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
148                 u64 name_length = actx->attr->name_length;
149
150                 if (data_size == 0) {
151                         if (errno != 0) {
152                                 ERROR_WITH_ERRNO("Failed to get size of attribute of "
153                                                  "`%s'", path);
154                                 ret = WIMLIB_ERR_NTFS_3G;
155                                 goto out_put_actx;
156                         }
157                         /* Empty stream.  No lookup table entry is needed. */
158                         lte = NULL;
159                 } else {
160                         if (type == AT_REPARSE_POINT && data_size < 8) {
161                                 ERROR("`%s': reparse point buffer too small",
162                                       path);
163                                 ret = WIMLIB_ERR_NTFS_3G;
164                                 goto out_put_actx;
165                         }
166                         /* Checksum the stream. */
167                         ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash,
168                                                 type == AT_REPARSE_POINT, &reparse_tag);
169                         if (ret != 0)
170                                 goto out_put_actx;
171
172                         if (type == AT_REPARSE_POINT)
173                                 dentry->d_inode->i_reparse_tag = reparse_tag;
174
175                         /* Make a lookup table entry for the stream, or use an existing
176                          * one if there's already an identical stream. */
177                         lte = __lookup_resource(lookup_table, attr_hash);
178                         ret = WIMLIB_ERR_NOMEM;
179                         if (lte) {
180                                 lte->refcnt++;
181                         } else {
182                                 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
183                                 if (!ntfs_loc)
184                                         goto out_put_actx;
185                                 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
186                                 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
187                                 if (!ntfs_loc->path_utf8)
188                                         goto out_free_ntfs_loc;
189                                 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
190                                 if (name_length) {
191                                         ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
192                                         if (!ntfs_loc->stream_name_utf16)
193                                                 goto out_free_ntfs_loc;
194                                         memcpy(ntfs_loc->stream_name_utf16,
195                                                attr_record_name(actx->attr),
196                                                actx->attr->name_length * 2);
197                                         ntfs_loc->stream_name_utf16_num_chars = name_length;
198                                 }
199
200                                 lte = new_lookup_table_entry();
201                                 if (!lte)
202                                         goto out_free_ntfs_loc;
203                                 lte->ntfs_loc = ntfs_loc;
204                                 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
205                                 if (type == AT_REPARSE_POINT) {
206                                         ntfs_loc->is_reparse_point = true;
207                                         lte->resource_entry.original_size = data_size - 8;
208                                         lte->resource_entry.size = data_size - 8;
209                                 } else {
210                                         ntfs_loc->is_reparse_point = false;
211                                         lte->resource_entry.original_size = data_size;
212                                         lte->resource_entry.size = data_size;
213                                 }
214                                 ntfs_loc = NULL;
215                                 DEBUG("Add resource for `%s' (size = %"PRIu64")",
216                                       dentry->file_name_utf8,
217                                       lte->resource_entry.original_size);
218                                 copy_hash(lte->hash, attr_hash);
219                                 lookup_table_insert(lookup_table, lte);
220                         }
221                 }
222                 if (name_length == 0) {
223                         /* Unnamed data stream.  Put the reference to it in the
224                          * dentry's inode. */
225                 #if 0
226                         if (dentry->d_inode->i_lte) {
227                                 ERROR("Found two un-named data streams for "
228                                       "`%s'", path);
229                                 ret = WIMLIB_ERR_NTFS_3G;
230                                 goto out_free_lte;
231                         }
232                         dentry->d_inode->i_lte = lte;
233                 #else
234                         if (dentry->d_inode->i_lte) {
235                                 WARNING("Found two un-named data streams for "
236                                         "`%s'", path);
237                                 free_lookup_table_entry(lte);
238                         } else {
239                                 dentry->d_inode->i_lte = lte;
240                         }
241                 #endif
242                 } else {
243                         /* Named data stream.  Put the reference to it in the
244                          * alternate data stream entries */
245                         struct wim_ads_entry *new_ads_entry;
246                         size_t stream_name_utf8_len;
247
248                         ret = utf16_to_utf8((const char*)attr_record_name(actx->attr),
249                                             name_length * 2,
250                                             &stream_name_utf8,
251                                             &stream_name_utf8_len);
252                         if (ret != 0)
253                                 goto out_free_lte;
254                         new_ads_entry = inode_add_ads(dentry->d_inode, stream_name_utf8);
255                         FREE(stream_name_utf8);
256                         if (!new_ads_entry)
257                                 goto out_free_lte;
258
259                         wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
260
261                         new_ads_entry->lte = lte;
262                 }
263         }
264         ret = 0;
265         goto out_put_actx;
266 out_free_lte:
267         free_lookup_table_entry(lte);
268 out_free_ntfs_loc:
269         if (ntfs_loc) {
270                 FREE(ntfs_loc->path_utf8);
271                 FREE(ntfs_loc->stream_name_utf16);
272                 FREE(ntfs_loc);
273         }
274 out_put_actx:
275         ntfs_attr_put_search_ctx(actx);
276         if (ret == 0)
277                 DEBUG2("Successfully captured NTFS streams from `%s'", path);
278         else
279                 ERROR("Failed to capture NTFS streams from `%s", path);
280         return ret;
281 }
282
283 /* Red-black tree that maps NTFS inode numbers to DOS names */
284 struct dos_name_map {
285         struct rb_root rb_root;
286 };
287
288 struct dos_name_node {
289         struct rb_node rb_node;
290         char dos_name[24];
291         int name_len_bytes;
292         u64 ntfs_ino;
293 };
294
295 /* Inserts a new DOS name into the map */
296 static int insert_dos_name(struct dos_name_map *map,
297                            const ntfschar *dos_name, int name_len,
298                            u64 ntfs_ino)
299 {
300         struct dos_name_node *new_node;
301         struct rb_node **p;
302         struct rb_root *root;
303         struct rb_node *rb_parent;
304
305         DEBUG("DOS name_len = %d", name_len);
306         new_node = MALLOC(sizeof(struct dos_name_node));
307         if (!new_node)
308                 return -1;
309
310         /* DOS names are supposed to be 12 characters max (that's 24 bytes,
311          * assuming 2-byte ntfs characters) */
312         wimlib_assert(name_len * sizeof(ntfschar) <= sizeof(new_node->dos_name));
313
314         /* Initialize the DOS name, DOS name length, and NTFS inode number of
315          * the red-black tree node */
316         memcpy(new_node->dos_name, dos_name, name_len * sizeof(ntfschar));
317         new_node->name_len_bytes = name_len * sizeof(ntfschar);
318         new_node->ntfs_ino = ntfs_ino;
319
320         /* Insert the red-black tree node */
321         root = &map->rb_root;
322         p = &root->rb_node;
323         rb_parent = NULL;
324         while (*p) {
325                 struct dos_name_node *this;
326
327                 this = container_of(*p, struct dos_name_node, rb_node);
328                 rb_parent = *p;
329                 if (new_node->ntfs_ino < this->ntfs_ino)
330                         p = &((*p)->rb_left);
331                 else if (new_node->ntfs_ino > this->ntfs_ino)
332                         p = &((*p)->rb_right);
333                 else {
334                         /* This should be impossible since a NTFS inode cannot
335                          * have multiple DOS names, and we only should get each
336                          * DOS name entry once from the ntfs_readdir() calls. */
337                         ERROR("NTFS inode %"PRIu64" has multiple DOS names",
338                               ntfs_ino);
339                         return -1;
340                 }
341         }
342         rb_link_node(&new_node->rb_node, rb_parent, p);
343         rb_insert_color(&new_node->rb_node, root);
344         DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
345         return 0;
346 }
347
348 /* Returns a structure that contains the DOS name and its length for a NTFS
349  * inode, or NULL if the inode has no DOS name. */
350 static struct dos_name_node *
351 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
352 {
353         struct rb_node *node = map->rb_root.rb_node;
354         while (node) {
355                 struct dos_name_node *this;
356                 this = container_of(node, struct dos_name_node, rb_node);
357                 if (ntfs_ino < this->ntfs_ino)
358                         node = node->rb_left;
359                 else if (ntfs_ino > this->ntfs_ino)
360                         node = node->rb_right;
361                 else
362                         return this;
363         }
364         return NULL;
365 }
366
367 static int set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
368 {
369         const struct dos_name_map *map = arg;
370         const struct dos_name_node *node;
371
372         if (dentry->is_win32_name) {
373                 node = lookup_dos_name(map, dentry->d_inode->i_ino);
374                 if (node) {
375                         dentry->short_name = MALLOC(node->name_len_bytes);
376                         if (!dentry->short_name)
377                                 return WIMLIB_ERR_NOMEM;
378                         memcpy(dentry->short_name, node->dos_name,
379                                node->name_len_bytes);
380                         dentry->short_name_len = node->name_len_bytes;
381                         DEBUG("Assigned DOS name to ino %"PRIu64,
382                               dentry->d_inode->i_ino);
383                 } else {
384                         WARNING("NTFS inode %"PRIu64" has Win32 name with no "
385                                 "corresponding DOS name",
386                                 dentry->d_inode->i_ino);
387                 }
388         }
389         return 0;
390 }
391
392 static void free_dos_name_tree(struct rb_node *node) {
393         if (node) {
394                 free_dos_name_tree(node->rb_left);
395                 free_dos_name_tree(node->rb_right);
396                 FREE(container_of(node, struct dos_name_node, rb_node));
397         }
398 }
399
400 static void destroy_dos_name_map(struct dos_name_map *map)
401 {
402         free_dos_name_tree(map->rb_root.rb_node);
403 }
404
405 struct readdir_ctx {
406         struct wim_dentry *parent;
407         ntfs_inode *dir_ni;
408         char *path;
409         size_t path_len;
410         struct wim_lookup_table *lookup_table;
411         struct sd_set *sd_set;
412         struct dos_name_map *dos_name_map;
413         const struct capture_config *config;
414         ntfs_volume **ntfs_vol_p;
415         int add_image_flags;
416         wimlib_progress_func_t progress_func;
417 };
418
419 static int
420 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p, ntfs_inode *dir_ni,
421                                  ntfs_inode *ni, char path[], size_t path_len,
422                                  int name_type,
423                                  struct wim_lookup_table *lookup_table,
424                                  struct sd_set *sd_set,
425                                  const struct capture_config *config,
426                                  ntfs_volume **ntfs_vol_p,
427                                  int add_image_flags,
428                                  wimlib_progress_func_t progress_func);
429
430 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
431                                     const int name_len, const int name_type,
432                                     const s64 pos, const MFT_REF mref,
433                                     const unsigned dt_type)
434 {
435         struct readdir_ctx *ctx;
436         size_t utf8_name_len;
437         char *utf8_name;
438         struct wim_dentry *child;
439         int ret;
440         size_t path_len;
441
442         ctx = dirent;
443         if (name_type & FILE_NAME_DOS) {
444                 /* If this is the entry for a DOS name, store it for later. */
445                 ret = insert_dos_name(ctx->dos_name_map, name,
446                                       name_len, mref & MFT_REF_MASK_CPU);
447
448                 /* Return now if an error occurred or if this is just a DOS name
449                  * and not a Win32+DOS name. */
450                 if (ret != 0 || name_type == FILE_NAME_DOS)
451                         return ret;
452         }
453         ret = utf16_to_utf8((const char*)name, name_len * 2,
454                             &utf8_name, &utf8_name_len);
455         if (ret != 0)
456                 return -1;
457
458         if (utf8_name[0] == '.' &&
459              (utf8_name[1] == '\0' ||
460               (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
461                 /* . or .. entries
462                  *
463                  * note: name_type is POSIX for these, so DOS names will not
464                  * have been inserted for them.  */
465                 ret = 0;
466                 goto out_free_utf8_name;
467         }
468
469         /* Open the inode for this directory entry and recursively capture the
470          * directory tree rooted at it */
471         ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
472         if (!ni) {
473                 ERROR_WITH_ERRNO("Failed to open NTFS inode");
474                 goto out_free_utf8_name;
475         }
476         path_len = ctx->path_len;
477         if (path_len != 1)
478                 ctx->path[path_len++] = '/';
479         memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
480         path_len += utf8_name_len;
481         child = NULL;
482         ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
483                                                ni, ctx->path, path_len, name_type,
484                                                ctx->lookup_table, ctx->sd_set,
485                                                ctx->config, ctx->ntfs_vol_p,
486                                                ctx->add_image_flags,
487                                                ctx->progress_func);
488         if (child)
489                 dentry_add_child(ctx->parent, child);
490         ntfs_inode_close(ni);
491 out_free_utf8_name:
492         FREE(utf8_name);
493         return ret;
494 }
495
496 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
497  * At the same time, update the WIM lookup table with lookup table entries for
498  * the NTFS streams, and build an array of security descriptors.
499  */
500 static int build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
501                                             ntfs_inode *dir_ni,
502                                             ntfs_inode *ni,
503                                             char path[],
504                                             size_t path_len,
505                                             int name_type,
506                                             struct wim_lookup_table *lookup_table,
507                                             struct sd_set *sd_set,
508                                             const struct capture_config *config,
509                                             ntfs_volume **ntfs_vol_p,
510                                             int add_image_flags,
511                                             wimlib_progress_func_t progress_func)
512 {
513         u32 attributes;
514         int ret;
515         struct wim_dentry *root;
516
517         if (exclude_path(path, config, false)) {
518                 /* Exclude a file or directory tree based on the capture
519                  * configuration file */
520                 if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
521                     && progress_func)
522                 {
523                         union wimlib_progress_info info;
524                         info.scan.cur_path = path;
525                         info.scan.excluded = true;
526                         progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
527                 }
528                 *root_p = NULL;
529                 return 0;
530         }
531
532         /* Get file attributes */
533         struct SECURITY_CONTEXT ctx;
534         memset(&ctx, 0, sizeof(ctx));
535         ctx.vol = ni->vol;
536         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
537                                          ni, dir_ni, (char *)&attributes,
538                                          sizeof(u32));
539         if (ret != 4) {
540                 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
541                                  path);
542                 return WIMLIB_ERR_NTFS_3G;
543         }
544
545         if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
546             && progress_func)
547         {
548                 union wimlib_progress_info info;
549                 info.scan.cur_path = path;
550                 info.scan.excluded = false;
551                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
552         }
553
554         /* Create the new WIM dentry */
555         root = new_dentry_with_timeless_inode(path_basename(path));
556         if (!root) {
557                 if (errno == EILSEQ)
558                         return WIMLIB_ERR_INVALID_UTF8_STRING;
559                 else if (errno == ENOMEM)
560                         return WIMLIB_ERR_NOMEM;
561                 else
562                         return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
563         }
564         *root_p = root;
565
566         if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name */
567                 root->is_win32_name = 1;
568         root->d_inode->i_creation_time    = le64_to_cpu(ni->creation_time);
569         root->d_inode->i_last_write_time  = le64_to_cpu(ni->last_data_change_time);
570         root->d_inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
571         root->d_inode->i_attributes       = le32_to_cpu(attributes);
572         root->d_inode->i_ino              = ni->mft_no;
573         root->d_inode->i_resolved         = 1;
574
575         if (attributes & FILE_ATTR_REPARSE_POINT) {
576                 /* Junction point, symbolic link, or other reparse point */
577                 ret = capture_ntfs_streams(root, ni, path, path_len,
578                                            lookup_table, ntfs_vol_p,
579                                            AT_REPARSE_POINT);
580         } else if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
581
582                 /* Normal directory */
583                 s64 pos = 0;
584                 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
585                 struct readdir_ctx ctx = {
586                         .parent          = root,
587                         .dir_ni          = ni,
588                         .path            = path,
589                         .path_len        = path_len,
590                         .lookup_table    = lookup_table,
591                         .sd_set          = sd_set,
592                         .dos_name_map    = &dos_name_map,
593                         .config          = config,
594                         .ntfs_vol_p      = ntfs_vol_p,
595                         .add_image_flags = add_image_flags,
596                         .progress_func   = progress_func,
597                 };
598                 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
599                 if (ret) {
600                         ERROR_WITH_ERRNO("ntfs_readdir()");
601                         ret = WIMLIB_ERR_NTFS_3G;
602                 } else {
603                         ret = for_dentry_child(root, set_dentry_dos_name,
604                                                &dos_name_map);
605                 }
606                 destroy_dos_name_map(&dos_name_map);
607         } else {
608                 /* Normal file */
609                 ret = capture_ntfs_streams(root, ni, path, path_len,
610                                            lookup_table, ntfs_vol_p,
611                                            AT_DATA);
612         }
613         if (ret != 0)
614                 return ret;
615
616         /* Get security descriptor */
617         char _sd[1];
618         char *sd = _sd;
619         errno = 0;
620         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
621                                          ni, dir_ni, sd,
622                                          sizeof(sd));
623         if (ret > sizeof(sd)) {
624                 sd = alloca(ret);
625                 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
626                                                  ni, dir_ni, sd, ret);
627         }
628         if (ret > 0) {
629                 root->d_inode->i_security_id = sd_set_add_sd(sd_set, sd, ret);
630                 if (root->d_inode->i_security_id == -1) {
631                         ERROR("Out of memory");
632                         return WIMLIB_ERR_NOMEM;
633                 }
634                 DEBUG("Added security ID = %u for `%s'",
635                       root->d_inode->i_security_id, path);
636                 ret = 0;
637         } else if (ret < 0) {
638                 ERROR_WITH_ERRNO("Failed to get security information from "
639                                  "`%s'", path);
640                 ret = WIMLIB_ERR_NTFS_3G;
641         } else {
642                 root->d_inode->i_security_id = -1;
643                 DEBUG("No security ID for `%s'", path);
644         }
645         return ret;
646 }
647
648 int build_dentry_tree_ntfs(struct wim_dentry **root_p,
649                            const char *device,
650                            struct wim_lookup_table *lookup_table,
651                            struct wim_security_data *sd,
652                            const struct capture_config *config,
653                            int add_image_flags,
654                            wimlib_progress_func_t progress_func,
655                            void *extra_arg)
656 {
657         ntfs_volume *vol;
658         ntfs_inode *root_ni;
659         int ret;
660         struct sd_set sd_set = {
661                 .sd = sd,
662                 .rb_root = {NULL},
663         };
664         ntfs_volume **ntfs_vol_p = extra_arg;
665
666         DEBUG("Mounting NTFS volume `%s' read-only", device);
667
668 #ifdef HAVE_NTFS_MNT_RDONLY
669         /* NTFS-3g 2013 */
670         vol = ntfs_mount(device, NTFS_MNT_RDONLY);
671 #else
672         /* NTFS-3g 2011, 2012 */
673         vol = ntfs_mount(device, MS_RDONLY);
674 #endif
675         if (!vol) {
676                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
677                                  device);
678                 return WIMLIB_ERR_NTFS_3G;
679         }
680         ntfs_open_secure(vol);
681
682         /* We don't want to capture the special NTFS files such as $Bitmap.  Not
683          * to be confused with "hidden" or "system" files which are real files
684          * that we do need to capture.  */
685         NVolClearShowSysFiles(vol);
686
687         DEBUG("Opening root NTFS dentry");
688         root_ni = ntfs_inode_open(vol, FILE_root);
689         if (!root_ni) {
690                 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
691                                  "`%s'", device);
692                 ret = WIMLIB_ERR_NTFS_3G;
693                 goto out;
694         }
695
696         /* Currently we assume that all the UTF-8 paths fit into this length and
697          * there is no check for overflow. */
698         char *path = MALLOC(32768);
699         if (!path) {
700                 ERROR("Could not allocate memory for NTFS pathname");
701                 ret = WIMLIB_ERR_NOMEM;
702                 goto out_cleanup;
703         }
704
705         path[0] = '/';
706         path[1] = '\0';
707         ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
708                                                FILE_NAME_POSIX, lookup_table,
709                                                &sd_set,
710                                                config, ntfs_vol_p,
711                                                add_image_flags,
712                                                progress_func);
713 out_cleanup:
714         FREE(path);
715         ntfs_inode_close(root_ni);
716         destroy_sd_set(&sd_set);
717 out:
718         ntfs_index_ctx_put(vol->secure_xsii);
719         ntfs_index_ctx_put(vol->secure_xsdh);
720         ntfs_inode_close(vol->secure_ni);
721
722         if (ret) {
723                 if (ntfs_umount(vol, FALSE) != 0) {
724                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
725                                          device);
726                         if (ret == 0)
727                                 ret = WIMLIB_ERR_NTFS_3G;
728                 }
729         } else {
730                 /* We need to leave the NTFS volume mounted so that we can read
731                  * the NTFS files again when we are actually writing the WIM */
732                 *ntfs_vol_p = vol;
733         }
734         return ret;
735 }