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