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