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