]> wimlib.net Git - wimlib/blob - src/ntfs-3g_capture.c
header.c, lookup_table.c: Read/write data from structures
[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 = memdup(path, path_len + 1);
224                         if (!ntfs_loc->path) {
225                                 ret = WIMLIB_ERR_NOMEM;
226                                 goto out_free_ntfs_loc;
227                         }
228                         if (name_length) {
229                                 ntfs_loc->stream_name = memdup(attr_record_name(actx->attr),
230                                                                name_length * 2);
231                                 if (!ntfs_loc->stream_name) {
232                                         ret = WIMLIB_ERR_NOMEM;
233                                         goto out_free_ntfs_loc;
234                                 }
235                                 ntfs_loc->stream_name_nchars = name_length;
236                         }
237
238                         lte = new_lookup_table_entry();
239                         if (!lte) {
240                                 ret = WIMLIB_ERR_NOMEM;
241                                 goto out_free_ntfs_loc;
242                         }
243                         lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
244                         lte->ntfs_loc = ntfs_loc;
245                         ntfs_loc = NULL;
246                         if (type == AT_REPARSE_POINT) {
247                                 if (data_size < 8) {
248                                         ERROR("Invalid reparse data (only %u bytes)!",
249                                               (unsigned)data_size);
250                                         ret = WIMLIB_ERR_NTFS_3G;
251                                         goto out_free_lte;
252                                 }
253                                 lte->ntfs_loc->is_reparse_point = true;
254                                 lte->resource_entry.original_size = data_size - 8;
255                                 ret = read_reparse_tag(ni, lte->ntfs_loc,
256                                                        &inode->i_reparse_tag);
257                                 if (ret)
258                                         goto out_free_lte;
259                         } else {
260                                 lte->ntfs_loc->is_reparse_point = false;
261                                 lte->resource_entry.original_size = data_size;
262                         }
263                 }
264                 if (name_length == 0) {
265                         /* Unnamed data stream.  Put the reference to it in the
266                          * dentry's inode. */
267                         if (inode->i_lte) {
268                                 ERROR("Found two un-named data streams for `%s'",
269                                       path);
270                                 ret = WIMLIB_ERR_NTFS_3G;
271                                 goto out_free_lte;
272                         }
273                         stream_id = 0;
274                         inode->i_lte = lte;
275                 } else {
276                         /* Named data stream.  Put the reference to it in the
277                          * alternate data stream entries */
278                         struct wim_ads_entry *new_ads_entry;
279
280                         new_ads_entry = inode_add_ads_utf16le(inode,
281                                                               attr_record_name(actx->attr),
282                                                               name_length * 2);
283                         if (!new_ads_entry) {
284                                 ret = WIMLIB_ERR_NOMEM;
285                                 goto out_free_lte;
286                         }
287                         wimlib_assert(new_ads_entry->stream_name_nbytes == name_length * 2);
288                         stream_id = new_ads_entry->stream_id;
289                         new_ads_entry->lte = lte;
290                 }
291                 if (lte) {
292                         lookup_table_insert_unhashed(lookup_table, lte,
293                                                      inode, stream_id);
294                 }
295         }
296         if (errno == ENOENT) {
297                 ret = 0;
298         } else {
299                 ERROR_WITH_ERRNO("Error listing NTFS attributes from `%s'",
300                                  path);
301                 ret = WIMLIB_ERR_NTFS_3G;
302         }
303         goto out_put_actx;
304 out_free_lte:
305         free_lookup_table_entry(lte);
306 out_free_ntfs_loc:
307         if (ntfs_loc) {
308                 FREE(ntfs_loc->path);
309                 FREE(ntfs_loc->stream_name);
310                 FREE(ntfs_loc);
311         }
312 out_put_actx:
313         ntfs_attr_put_search_ctx(actx);
314         if (ret == 0)
315                 DEBUG2("Successfully captured NTFS streams from `%s'", path);
316         else
317                 ERROR("Failed to capture NTFS streams from `%s'", path);
318         return ret;
319 }
320
321 /* Red-black tree that maps NTFS inode numbers to DOS names */
322 struct dos_name_map {
323         struct rb_root rb_root;
324 };
325
326 struct dos_name_node {
327         struct rb_node rb_node;
328         char dos_name[24];
329         int name_nbytes;
330         u64 ntfs_ino;
331 };
332
333 /* Inserts a new DOS name into the map */
334 static int
335 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
336                 size_t name_nbytes, u64 ntfs_ino)
337 {
338         struct dos_name_node *new_node;
339         struct rb_node **p;
340         struct rb_root *root;
341         struct rb_node *rb_parent;
342
343         DEBUG("DOS name_len = %zu", name_nbytes);
344         new_node = MALLOC(sizeof(struct dos_name_node));
345         if (!new_node)
346                 return -1;
347
348         /* DOS names are supposed to be 12 characters max (that's 24 bytes,
349          * assuming 2-byte ntfs characters) */
350         wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
351
352         /* Initialize the DOS name, DOS name length, and NTFS inode number of
353          * the red-black tree node */
354         memcpy(new_node->dos_name, dos_name, name_nbytes);
355         new_node->name_nbytes = name_nbytes;
356         new_node->ntfs_ino = ntfs_ino;
357
358         /* Insert the red-black tree node */
359         root = &map->rb_root;
360         p = &root->rb_node;
361         rb_parent = NULL;
362         while (*p) {
363                 struct dos_name_node *this;
364
365                 this = container_of(*p, struct dos_name_node, rb_node);
366                 rb_parent = *p;
367                 if (new_node->ntfs_ino < this->ntfs_ino)
368                         p = &((*p)->rb_left);
369                 else if (new_node->ntfs_ino > this->ntfs_ino)
370                         p = &((*p)->rb_right);
371                 else {
372                         /* This should be impossible since a NTFS inode cannot
373                          * have multiple DOS names, and we only should get each
374                          * DOS name entry once from the ntfs_readdir() calls. */
375                         ERROR("NTFS inode %"PRIu64" has multiple DOS names",
376                               ntfs_ino);
377                         return -1;
378                 }
379         }
380         rb_link_node(&new_node->rb_node, rb_parent, p);
381         rb_insert_color(&new_node->rb_node, root);
382         DEBUG("Inserted DOS name for inode %"PRIu64, ntfs_ino);
383         return 0;
384 }
385
386 /* Returns a structure that contains the DOS name and its length for a NTFS
387  * inode, or NULL if the inode has no DOS name. */
388 static struct dos_name_node *
389 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
390 {
391         struct rb_node *node = map->rb_root.rb_node;
392         while (node) {
393                 struct dos_name_node *this;
394                 this = container_of(node, struct dos_name_node, rb_node);
395                 if (ntfs_ino < this->ntfs_ino)
396                         node = node->rb_left;
397                 else if (ntfs_ino > this->ntfs_ino)
398                         node = node->rb_right;
399                 else
400                         return this;
401         }
402         return NULL;
403 }
404
405 static int
406 set_dentry_dos_name(struct wim_dentry *dentry, void *arg)
407 {
408         const struct dos_name_map *map = arg;
409         const struct dos_name_node *node;
410
411         if (dentry->is_win32_name) {
412                 node = lookup_dos_name(map, dentry->d_inode->i_ino);
413                 if (node) {
414                         dentry->short_name = MALLOC(node->name_nbytes + 2);
415                         if (!dentry->short_name)
416                                 return WIMLIB_ERR_NOMEM;
417                         memcpy(dentry->short_name, node->dos_name,
418                                node->name_nbytes);
419                         dentry->short_name[node->name_nbytes / 2] = 0;
420                         dentry->short_name_nbytes = node->name_nbytes;
421                         DEBUG("Assigned DOS name to ino %"PRIu64,
422                               dentry->d_inode->i_ino);
423                 } else {
424                         WARNING("NTFS inode %"PRIu64" has Win32 name with no "
425                                 "corresponding DOS name",
426                                 dentry->d_inode->i_ino);
427                 }
428         }
429         return 0;
430 }
431
432 static void
433 free_dos_name_tree(struct rb_node *node) {
434         if (node) {
435                 free_dos_name_tree(node->rb_left);
436                 free_dos_name_tree(node->rb_right);
437                 FREE(container_of(node, struct dos_name_node, rb_node));
438         }
439 }
440
441 static void
442 destroy_dos_name_map(struct dos_name_map *map)
443 {
444         free_dos_name_tree(map->rb_root.rb_node);
445 }
446
447 struct readdir_ctx {
448         struct wim_dentry *parent;
449         ntfs_inode *dir_ni;
450         char *path;
451         size_t path_len;
452         struct dos_name_map *dos_name_map;
453         ntfs_volume *vol;
454         struct add_image_params *params;
455 };
456
457 static int
458 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_p,
459                                  ntfs_inode *dir_ni,
460                                  ntfs_inode *ni,
461                                  char *path,
462                                  size_t path_len,
463                                  int name_type,
464                                  ntfs_volume *ntfs_vol,
465                                  struct add_image_params *params);
466
467 static int
468 wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
469                          const int name_nchars, const int name_type,
470                          const s64 pos, const MFT_REF mref,
471                          const unsigned dt_type)
472 {
473         struct readdir_ctx *ctx;
474         size_t mbs_name_nbytes;
475         char *mbs_name;
476         struct wim_dentry *child;
477         int ret;
478         size_t path_len;
479         size_t name_nbytes = name_nchars * sizeof(ntfschar);
480
481         ctx = dirent;
482         if (name_type & FILE_NAME_DOS) {
483                 /* If this is the entry for a DOS name, store it for later. */
484                 ret = insert_dos_name(ctx->dos_name_map, name,
485                                       name_nbytes, mref & MFT_REF_MASK_CPU);
486
487                 /* Return now if an error occurred or if this is just a DOS name
488                  * and not a Win32+DOS name. */
489                 if (ret != 0 || name_type == FILE_NAME_DOS)
490                         goto out;
491         }
492         ret = utf16le_to_tstr(name, name_nbytes,
493                               &mbs_name, &mbs_name_nbytes);
494         if (ret)
495                 goto out;
496
497         if (mbs_name[0] == '.' &&
498              (mbs_name[1] == '\0' ||
499               (mbs_name[1] == '.' && mbs_name[2] == '\0'))) {
500                 /* . or .. entries
501                  *
502                  * note: name_type is POSIX for these, so DOS names will not
503                  * have been inserted for them.  */
504                 ret = 0;
505                 goto out_free_mbs_name;
506         }
507
508         /* Open the inode for this directory entry and recursively capture the
509          * directory tree rooted at it */
510         ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
511         if (!ni) {
512                 ERROR_WITH_ERRNO("Failed to open NTFS inode");
513                 ret = -1;
514                 goto out_free_mbs_name;
515         }
516         path_len = ctx->path_len;
517         if (path_len != 1)
518                 ctx->path[path_len++] = '/';
519         memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
520         path_len += mbs_name_nbytes;
521         child = NULL;
522         ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
523                                                ni, ctx->path, path_len, name_type,
524                                                ctx->vol, ctx->params);
525         if (child)
526                 dentry_add_child(ctx->parent, child);
527         ntfs_inode_close(ni);
528 out_free_mbs_name:
529         FREE(mbs_name);
530 out:
531         return ret;
532 }
533
534 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
535  * At the same time, update the WIM lookup table with lookup table entries for
536  * the NTFS streams, and build an array of security descriptors.
537  */
538 static int
539 build_dentry_tree_ntfs_recursive(struct wim_dentry **root_ret,
540                                  ntfs_inode *dir_ni,
541                                  ntfs_inode *ni,
542                                  char *path,
543                                  size_t path_len,
544                                  int name_type,
545                                  ntfs_volume *vol,
546                                  struct add_image_params *params)
547 {
548         u32 attributes;
549         int ret;
550         struct wim_dentry *root;
551         struct wim_inode *inode;
552         ATTR_TYPES stream_type;
553
554         if (exclude_path(path, path_len, params->config, false)) {
555                 /* Exclude a file or directory tree based on the capture
556                  * configuration file */
557                 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
558                     && params->progress_func)
559                 {
560                         union wimlib_progress_info info;
561                         info.scan.cur_path = path;
562                         info.scan.excluded = true;
563                         params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
564                 }
565                 root = NULL;
566                 ret = 0;
567                 goto out;
568         }
569
570         /* Get file attributes */
571         struct SECURITY_CONTEXT ctx;
572         memset(&ctx, 0, sizeof(ctx));
573         ctx.vol = vol;
574         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
575                                          ni, dir_ni, (char *)&attributes,
576                                          sizeof(u32));
577         if (ret != 4) {
578                 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
579                                  path);
580                 return WIMLIB_ERR_NTFS_3G;
581         }
582
583         if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
584             && params->progress_func)
585         {
586                 union wimlib_progress_info info;
587                 info.scan.cur_path = path;
588                 info.scan.excluded = false;
589                 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
590         }
591
592         /* Create a WIM dentry with an associated inode, which may be shared */
593         ret = inode_table_new_dentry(&params->inode_table,
594                                      path_basename_with_len(path, path_len),
595                                      ni->mft_no, 0, false, &root);
596         if (ret)
597                 return ret;
598
599         inode = root->d_inode;
600
601         if (inode->i_nlink > 1) /* Shared inode; nothing more to do */
602                 goto out;
603
604         if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
605                 root->is_win32_name = 1;
606         inode->i_creation_time    = le64_to_cpu(ni->creation_time);
607         inode->i_last_write_time  = le64_to_cpu(ni->last_data_change_time);
608         inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
609         inode->i_attributes       = le32_to_cpu(attributes);
610         inode->i_resolved         = 1;
611
612         if (attributes & FILE_ATTR_REPARSE_POINT)
613                 stream_type = AT_REPARSE_POINT;
614         else
615                 stream_type = AT_DATA;
616
617         /* Capture the file's streams; more specifically, this is supposed to:
618          *
619          * - Regular files: capture unnamed data stream and any named data
620          *   streams
621          * - Directories: capture any named data streams
622          * - Reparse points: capture reparse data only
623          */
624         ret = capture_ntfs_streams(inode, ni, path, path_len,
625                                    params->lookup_table, vol, stream_type);
626         if (ret)
627                 goto out;
628
629         if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
630
631                 /* Recurse to directory children */
632                 s64 pos = 0;
633                 struct dos_name_map dos_name_map = { .rb_root = {.rb_node = NULL} };
634                 struct readdir_ctx ctx = {
635                         .parent          = root,
636                         .dir_ni          = ni,
637                         .path            = path,
638                         .path_len        = path_len,
639                         .dos_name_map    = &dos_name_map,
640                         .vol             = vol,
641                         .params          = params,
642                 };
643                 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
644                 if (ret) {
645                         ERROR_WITH_ERRNO("ntfs_readdir()");
646                         ret = WIMLIB_ERR_NTFS_3G;
647                 } else {
648                         ret = for_dentry_child(root, set_dentry_dos_name,
649                                                &dos_name_map);
650                 }
651                 destroy_dos_name_map(&dos_name_map);
652                 if (ret)
653                         goto out;
654         }
655
656         /* Reparse-point fixups are a no-op because in NTFS-3g capture mode we
657          * only allow capturing an entire volume. */
658         if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
659             inode_is_symlink(inode))
660                 inode->i_not_rpfixed = 0;
661
662         if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
663                 /* Get security descriptor */
664                 char _sd[1];
665                 char *sd = _sd;
666                 errno = 0;
667                 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
668                                                  ni, dir_ni, sd,
669                                                  sizeof(sd));
670                 if (ret > sizeof(sd)) {
671                         sd = alloca(ret);
672                         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
673                                                          ni, dir_ni, sd, ret);
674                 }
675                 if (ret > 0) {
676                         inode->i_security_id = sd_set_add_sd(&params->sd_set,
677                                                              sd, ret);
678                         if (inode->i_security_id == -1) {
679                                 ERROR("Out of memory");
680                                 ret = WIMLIB_ERR_NOMEM;
681                                 goto out;
682                         }
683                         DEBUG("Added security ID = %u for `%s'",
684                               inode->i_security_id, path);
685                         ret = 0;
686                 } else if (ret < 0) {
687                         ERROR_WITH_ERRNO("Failed to get security information from "
688                                          "`%s'", path);
689                         ret = WIMLIB_ERR_NTFS_3G;
690                 } else {
691                         inode->i_security_id = -1;
692                         DEBUG("No security ID for `%s'", path);
693                 }
694         }
695 out:
696         if (ret == 0)
697                 *root_ret = root;
698         else
699                 free_dentry_tree(root, params->lookup_table);
700         return ret;
701 }
702
703
704 int
705 do_ntfs_umount(struct _ntfs_volume *vol)
706 {
707         DEBUG("Unmounting NTFS volume");
708         if (ntfs_umount(vol, FALSE))
709                 return WIMLIB_ERR_NTFS_3G;
710         else
711                 return 0;
712 }
713
714 int
715 build_dentry_tree_ntfs(struct wim_dentry **root_p,
716                        const char *device,
717                        struct add_image_params *params)
718 {
719         ntfs_volume *vol;
720         ntfs_inode *root_ni;
721         int ret;
722
723         DEBUG("Mounting NTFS volume `%s' read-only", device);
724
725 #if defined(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 #endif
732         if (!vol) {
733                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
734                                  device);
735                 return WIMLIB_ERR_NTFS_3G;
736         }
737         ntfs_open_secure(vol);
738
739         /* We don't want to capture the special NTFS files such as $Bitmap.  Not
740          * to be confused with "hidden" or "system" files which are real files
741          * that we do need to capture.  */
742         NVolClearShowSysFiles(vol);
743
744         DEBUG("Opening root NTFS dentry");
745         root_ni = ntfs_inode_open(vol, FILE_root);
746         if (!root_ni) {
747                 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
748                                  "`%s'", device);
749                 ret = WIMLIB_ERR_NTFS_3G;
750                 goto out;
751         }
752
753         /* Currently we assume that all the paths fit into this length and there
754          * is no check for overflow. */
755         char *path = MALLOC(32768);
756         if (!path) {
757                 ERROR("Could not allocate memory for NTFS pathname");
758                 ret = WIMLIB_ERR_NOMEM;
759                 goto out_cleanup;
760         }
761
762         path[0] = '/';
763         path[1] = '\0';
764         ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
765                                                FILE_NAME_POSIX, vol, params);
766 out_cleanup:
767         FREE(path);
768         ntfs_inode_close(root_ni);
769 out:
770         ntfs_index_ctx_put(vol->secure_xsii);
771         ntfs_index_ctx_put(vol->secure_xsdh);
772         ntfs_inode_close(vol->secure_ni);
773
774         if (ret) {
775                 if (do_ntfs_umount(vol)) {
776                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
777                                          device);
778                         if (ret == 0)
779                                 ret = WIMLIB_ERR_NTFS_3G;
780                 }
781         } else {
782                 /* We need to leave the NTFS volume mounted so that we can read
783                  * the NTFS files again when we are actually writing the WIM */
784                 *(ntfs_volume**)params->extra_arg = vol;
785         }
786         return ret;
787 }
788 #endif /* WITH_NTFS_3G */