]> wimlib.net Git - wimlib/blob - src/ntfs-capture.c
Compile on FreeBSD
[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 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 #ifdef WITH_NTFS_3G
31 #include <ntfs-3g/endians.h>
32 #include <ntfs-3g/types.h>
33 #endif
34
35 #include "wimlib_internal.h"
36
37
38 #ifdef WITH_NTFS_3G
39 #include "dentry.h"
40 #include "lookup_table.h"
41 #include "io.h"
42 #include <ntfs-3g/layout.h>
43 #include <ntfs-3g/acls.h>
44 #include <ntfs-3g/attrib.h>
45 #include <ntfs-3g/misc.h>
46 #include <ntfs-3g/reparse.h>
47 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
48 #include <ntfs-3g/xattrs.h>
49 #include <ntfs-3g/volume.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <errno.h>
53
54 #if 0
55 extern int ntfs_get_inode_security(ntfs_inode *ni, u32 selection, char *buf,
56                                    u32 buflen, u32 *psize);
57
58 extern u32 ntfs_get_inode_attributes(ntfs_inode *ni);
59 #endif
60
61 /* Structure that allows searching the security descriptors by SHA1 message
62  * digest. */
63 struct sd_set {
64         struct wim_security_data *sd;
65         struct sd_node *root;
66 };
67
68 /* Binary tree node of security descriptors, indexed by the @hash field. */
69 struct sd_node {
70         int security_id;
71         u8 hash[SHA1_HASH_SIZE];
72         struct sd_node *left;
73         struct sd_node *right;
74 };
75
76 static void free_sd_tree(struct sd_node *root)
77 {
78         if (root) {
79                 free_sd_tree(root->left);
80                 free_sd_tree(root->right);
81                 FREE(root);
82         }
83 }
84 /* Frees a security descriptor index set. */
85 static void destroy_sd_set(struct sd_set *sd_set)
86 {
87         free_sd_tree(sd_set->root);
88 }
89
90 /* Inserts a a new node into the security descriptor index tree. */
91 static void insert_sd_node(struct sd_node *new, struct sd_node *root)
92 {
93         int cmp = hashes_cmp(new->hash, root->hash);
94         if (cmp < 0) {
95                 if (root->left)
96                         insert_sd_node(new, root->left);
97                 else 
98                         root->left = new;
99         } else if (cmp > 0) {
100                 if (root->right)
101                         insert_sd_node(new, root->right);
102                 else 
103                         root->right = new;
104         } else {
105                 wimlib_assert(0);
106         }
107 }
108
109 /* Returns the security ID of the security data having a SHA1 message digest of
110  * @hash in the security descriptor index tree rooted at @root. 
111  *
112  * If not found, return -1. */
113 static int lookup_sd(const u8 hash[SHA1_HASH_SIZE], struct sd_node *root)
114 {
115         int cmp;
116         if (!root)
117                 return -1;
118         cmp = hashes_cmp(hash, root->hash);
119         if (cmp < 0)
120                 return lookup_sd(hash, root->left);
121         else if (cmp > 0)
122                 return lookup_sd(hash, root->right);
123         else
124                 return root->security_id;
125 }
126
127 /*
128  * Adds a security descriptor to the indexed security descriptor set as well as
129  * the corresponding `struct wim_security_data', and returns the new security
130  * ID; or, if there is an existing security descriptor that is the same, return
131  * the security ID for it.  If a new security descriptor cannot be allocated,
132  * return -1.
133  */
134 static int sd_set_add_sd(struct sd_set *sd_set, const char descriptor[],
135                          size_t size)
136 {
137         u8 hash[SHA1_HASH_SIZE];
138         int security_id;
139         struct sd_node *new;
140         u8 **descriptors;
141         u64 *sizes;
142         u8 *descr_copy;
143         struct wim_security_data *sd;
144
145         sha1_buffer((const u8*)descriptor, size, hash);
146
147         security_id = lookup_sd(hash, sd_set->root);
148         if (security_id >= 0)
149                 return security_id;
150
151         new = MALLOC(sizeof(*new));
152         if (!new)
153                 goto out;
154         descr_copy = MALLOC(size);
155         if (!descr_copy)
156                 goto out_free_node;
157
158         sd = sd_set->sd;
159
160         memcpy(descr_copy, descriptor, size);
161         new->security_id = sd->num_entries;
162         new->left = NULL;
163         new->right = NULL;
164         copy_hash(new->hash, hash);
165
166
167         descriptors = REALLOC(sd->descriptors,
168                               (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
169         if (!descriptors)
170                 goto out_free_descr;
171         sd->descriptors = descriptors;
172         sizes = REALLOC(sd->sizes,
173                         (sd->num_entries + 1) * sizeof(sd->sizes[0]));
174         if (!sizes)
175                 goto out_free_descr;
176         sd->sizes = sizes;
177         sd->descriptors[sd->num_entries] = descr_copy;
178         sd->sizes[sd->num_entries] = size;
179         sd->num_entries++;
180         DEBUG("There are now %d security descriptors", sd->num_entries);
181         sd->total_length += size + sizeof(sd->sizes[0]);
182
183         if (sd_set->root)
184                 insert_sd_node(new, sd_set->root);
185         else
186                 sd_set->root = new;
187         return new->security_id;
188 out_free_descr:
189         FREE(descr_copy);
190 out_free_node:
191         FREE(new);
192 out:
193         return -1;
194 }
195
196 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
197 {
198         return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
199 }
200
201 /* Calculates the SHA1 message digest of a NTFS attribute. 
202  *
203  * @ni:  The NTFS inode containing the attribute.
204  * @ar:  The ATTR_RECORD describing the attribute.
205  * @md:  If successful, the returned SHA1 message digest.
206  * @reparse_tag_ret:    Optional pointer into which the first 4 bytes of the
207  *                              attribute will be written (to get the reparse
208  *                              point ID)
209  *
210  * Return 0 on success or nonzero on error.
211  */
212 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
213                              u8 md[SHA1_HASH_SIZE],
214                              u32 *reparse_tag_ret)
215 {
216         s64 pos = 0;
217         s64 bytes_remaining;
218         char buf[4096];
219         ntfs_attr *na;
220         SHA_CTX ctx;
221
222         na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
223                             ar->name_length);
224         if (!na) {
225                 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
226                 return WIMLIB_ERR_NTFS_3G;
227         }
228
229         bytes_remaining = na->data_size;
230         sha1_init(&ctx);
231
232         DEBUG2("Calculating SHA1 message digest (%"PRIu64" bytes)",
233                bytes_remaining);
234
235         while (bytes_remaining) {
236                 s64 to_read = min(bytes_remaining, sizeof(buf));
237                 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
238                         ERROR_WITH_ERRNO("Error reading NTFS attribute");
239                         return WIMLIB_ERR_NTFS_3G;
240                 }
241                 if (bytes_remaining == na->data_size && reparse_tag_ret)
242                         *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
243                 sha1_update(&ctx, buf, to_read);
244                 pos += to_read;
245                 bytes_remaining -= to_read;
246         }
247         sha1_final(md, &ctx);
248         ntfs_attr_close(na);
249         return 0;
250 }
251
252 /* Load the streams from a WIM file or reparse point in the NTFS volume into the
253  * WIM lookup table */
254 static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
255                                 char path[], size_t path_len,
256                                 struct lookup_table *lookup_table,
257                                 ntfs_volume **ntfs_vol_p,
258                                 ATTR_TYPES type)
259 {
260
261         ntfs_attr_search_ctx *actx;
262         u8 attr_hash[SHA1_HASH_SIZE];
263         struct ntfs_location *ntfs_loc = NULL;
264         int ret = 0;
265         struct lookup_table_entry *lte;
266
267         DEBUG2("Capturing NTFS data streams from `%s'", path);
268
269         /* Get context to search the streams of the NTFS file. */
270         actx = ntfs_attr_get_search_ctx(ni, NULL);
271         if (!actx) {
272                 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
273                                  "context");
274                 return WIMLIB_ERR_NTFS_3G;
275         }
276
277         /* Capture each data stream or reparse data stream. */
278         while (!ntfs_attr_lookup(type, NULL, 0,
279                                  CASE_SENSITIVE, 0, NULL, 0, actx))
280         {
281                 char *stream_name_utf8;
282                 size_t stream_name_utf16_len;
283                 u32 reparse_tag;
284                 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
285                 u64 name_length = actx->attr->name_length;
286
287                 if (data_size == 0) { 
288                         if (errno != 0) {
289                                 ERROR_WITH_ERRNO("Failed to get size of attribute of "
290                                                  "`%s'", path);
291                                 ret = WIMLIB_ERR_NTFS_3G;
292                                 goto out_put_actx;
293                         }
294                         /* Empty stream.  No lookup table entry is needed. */
295                         lte = NULL;
296                 } else {
297                         if (type == AT_REPARSE_POINT && data_size < 8) {
298                                 ERROR("`%s': reparse point buffer too small",
299                                       path);
300                                 ret = WIMLIB_ERR_NTFS_3G;
301                                 goto out_put_actx;
302                         }
303                         /* Checksum the stream. */
304                         ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash, &reparse_tag);
305                         if (ret != 0)
306                                 goto out_put_actx;
307
308                         /* Make a lookup table entry for the stream, or use an existing
309                          * one if there's already an identical stream. */
310                         lte = __lookup_resource(lookup_table, attr_hash);
311                         ret = WIMLIB_ERR_NOMEM;
312                         if (lte) {
313                                 lte->refcnt++;
314                         } else {
315                                 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
316                                 if (!ntfs_loc)
317                                         goto out_put_actx;
318                                 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
319                                 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
320                                 if (!ntfs_loc->path_utf8)
321                                         goto out_free_ntfs_loc;
322                                 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
323                                 if (name_length) {
324                                         ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
325                                         if (!ntfs_loc->stream_name_utf16)
326                                                 goto out_free_ntfs_loc;
327                                         memcpy(ntfs_loc->stream_name_utf16,
328                                                attr_record_name(actx->attr),
329                                                actx->attr->name_length * 2);
330                                         ntfs_loc->stream_name_utf16_num_chars = name_length;
331                                 }
332
333                                 lte = new_lookup_table_entry();
334                                 if (!lte)
335                                         goto out_free_ntfs_loc;
336                                 lte->ntfs_loc = ntfs_loc;
337                                 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
338                                 if (type == AT_REPARSE_POINT) {
339                                         dentry->reparse_tag = reparse_tag;
340                                         ntfs_loc->is_reparse_point = true;
341                                         lte->resource_entry.original_size = data_size - 8;
342                                         lte->resource_entry.size = data_size - 8;
343                                 } else {
344                                         ntfs_loc->is_reparse_point = false;
345                                         lte->resource_entry.original_size = data_size;
346                                         lte->resource_entry.size = data_size;
347                                 }
348                                 ntfs_loc = NULL;
349                                 DEBUG("Add resource for `%s' (size = %zu)",
350                                       dentry->file_name_utf8,
351                                       lte->resource_entry.original_size);
352                                 copy_hash(lte->hash, attr_hash);
353                                 lookup_table_insert(lookup_table, lte);
354                         }
355                 }
356                 if (name_length == 0) {
357                         /* Unnamed data stream.  Put the reference to it in the
358                          * dentry. */
359                         if (dentry->lte) {
360                                 ERROR("Found two un-named data streams for "
361                                       "`%s'", path);
362                                 ret = WIMLIB_ERR_NTFS_3G;
363                                 goto out_free_lte;
364                         }
365                         dentry->lte = lte;
366                 } else {
367                         /* Named data stream.  Put the reference to it in the
368                          * alternate data stream entries */
369                         struct ads_entry *new_ads_entry;
370                         size_t stream_name_utf8_len;
371                         stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
372                                                          name_length * 2,
373                                                          &stream_name_utf8_len);
374                         if (!stream_name_utf8)
375                                 goto out_free_lte;
376                         new_ads_entry = dentry_add_ads(dentry, stream_name_utf8);
377                         FREE(stream_name_utf8);
378                         if (!new_ads_entry)
379                                 goto out_free_lte;
380
381                         wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
382                                 
383                         new_ads_entry->lte = lte;
384                 }
385         }
386         ret = 0;
387         goto out_put_actx;
388 out_free_lte:
389         free_lookup_table_entry(lte);
390 out_free_ntfs_loc:
391         if (ntfs_loc) {
392                 FREE(ntfs_loc->path_utf8);
393                 FREE(ntfs_loc->stream_name_utf16);
394                 FREE(ntfs_loc);
395         }
396 out_put_actx:
397         ntfs_attr_put_search_ctx(actx);
398         if (ret == 0)
399                 DEBUG2("Successfully captured NTFS streams from `%s'", path);
400         else
401                 ERROR("Failed to capture NTFS streams from `%s", path);
402         return ret;
403 }
404
405 struct readdir_ctx {
406         struct dentry       *parent;
407         ntfs_inode          *dir_ni;
408         char                *path;
409         size_t               path_len;
410         struct lookup_table *lookup_table;
411         struct sd_set       *sd_set;
412         const struct capture_config *config;
413         ntfs_volume        **ntfs_vol_p;
414         int                  flags;
415 };
416
417 static int
418 build_dentry_tree_ntfs_recursive(struct dentry **root_p, ntfs_inode *dir_ni,
419                                  ntfs_inode *ni, char path[], size_t path_len,
420                                  int name_type,
421                                  struct lookup_table *lookup_table,
422                                  struct sd_set *sd_set,
423                                  const struct capture_config *config,
424                                  ntfs_volume **ntfs_vol_p,
425                                  int flags);
426
427 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
428                                     const int name_len, const int name_type,
429                                     const s64 pos, const MFT_REF mref,
430                                     const unsigned dt_type)
431 {
432         struct readdir_ctx *ctx;
433         size_t utf8_name_len;
434         char *utf8_name;
435         struct dentry *child = NULL;
436         int ret;
437         size_t path_len;
438
439         if (name_type == FILE_NAME_DOS)
440                 return 0;
441
442         ret = -1;
443
444         utf8_name = utf16_to_utf8((const char*)name, name_len * 2,
445                                   &utf8_name_len);
446         if (!utf8_name)
447                 goto out;
448
449         if (utf8_name[0] == '.' &&
450              (utf8_name[1] == '\0' ||
451               (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
452                 ret = 0;
453                 goto out_free_utf8_name;
454         }
455
456         ctx = dirent;
457
458         ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
459         if (!ni) {
460                 ERROR_WITH_ERRNO("Failed to open NTFS inode");
461                 ret = 1;
462         }
463         path_len = ctx->path_len;
464         if (path_len != 1)
465                 ctx->path[path_len++] = '/';
466         memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
467         path_len += utf8_name_len;
468         ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
469                                                ni, ctx->path, path_len, name_type,
470                                                ctx->lookup_table, ctx->sd_set,
471                                                ctx->config, ctx->ntfs_vol_p,
472                                                ctx->flags);
473
474         if (child)
475                 link_dentry(child, ctx->parent);
476
477         ntfs_inode_close(ni);
478 out_free_utf8_name:
479         FREE(utf8_name);
480 out:
481         return ret;
482 }
483
484 static int change_dentry_short_name(struct dentry *dentry,
485                                     const char short_name_utf8[],
486                                     int short_name_utf8_len)
487 {
488         size_t short_name_utf16_len;
489         char *short_name_utf16;
490         short_name_utf16 = utf8_to_utf16(short_name_utf8, short_name_utf8_len,
491                                          &short_name_utf16_len);
492         if (!short_name_utf16) {
493                 ERROR_WITH_ERRNO("Failed to convert short name to UTF-16");
494                 return WIMLIB_ERR_NOMEM;
495         }
496         dentry->short_name = short_name_utf16;
497         dentry->short_name_len = short_name_utf16_len;
498         return 0;
499 }
500
501 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
502
503 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
504  * At the same time, update the WIM lookup table with lookup table entries for
505  * the NTFS streams, and build an array of security descriptors.
506  */
507 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
508                                             ntfs_inode *dir_ni,
509                                             ntfs_inode *ni,
510                                             char path[],
511                                             size_t path_len,
512                                             int name_type,
513                                             struct lookup_table *lookup_table,
514                                             struct sd_set *sd_set,
515                                             const struct capture_config *config,
516                                             ntfs_volume **ntfs_vol_p,
517                                             int flags)
518 {
519         u32 attributes;
520         int mrec_flags;
521         u32 sd_size = 0;
522         int ret;
523         char dos_name_utf8[64];
524         struct dentry *root;
525
526         mrec_flags = ni->mrec->flags;
527 #ifdef HAVE_NTFS_INODE_FUNCTIONS
528         attributes = ntfs_get_inode_attributes(ni);
529 #else
530         struct SECURITY_CONTEXT ctx;
531         memset(&ctx, 0, sizeof(ctx));
532         ctx.vol = ni->vol;
533         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
534                                          ni, dir_ni, (char *)&attributes,
535                                          sizeof(u32));
536         if (ret != 4) {
537                 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
538                                  path);
539                 return WIMLIB_ERR_NTFS_3G;
540         }
541 #endif
542
543         if (exclude_path(path, config, false)) {
544                 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE) {
545                         const char *file_type;
546                         if (attributes & MFT_RECORD_IS_DIRECTORY)
547                                 file_type = "directory";
548                         else
549                                 file_type = "file";
550                         printf("Excluding %s `%s' from capture\n",
551                                file_type, path);
552                 }
553                 *root_p = NULL;
554                 return 0;
555         }
556
557         if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
558                 printf("Scanning `%s'\n", path);
559
560         root = new_dentry(path_basename(path));
561         if (!root)
562                 return WIMLIB_ERR_NOMEM;
563         *root_p = root;
564
565         if (dir_ni && (name_type == FILE_NAME_WIN32_AND_DOS
566                        || name_type == FILE_NAME_WIN32))
567         {
568                 ret = ntfs_get_ntfs_dos_name(ni, dir_ni, dos_name_utf8,
569                                              sizeof(dos_name_utf8));
570                 if (ret > 0) {
571                         DEBUG("Changing short name of `%s'", path);
572                         ret = change_dentry_short_name(root, dos_name_utf8,
573                                                        ret);
574                         if (ret != 0)
575                                 return ret;
576                 } else {
577                 #ifdef ENODATA
578                         if (errno != ENODATA) {
579                                 ERROR_WITH_ERRNO("Error getting DOS name "
580                                                  "of `%s'", path);
581                                 return WIMLIB_ERR_NTFS_3G;
582                         }
583                 #endif
584                 }
585         }
586
587         root->creation_time    = le64_to_cpu(ni->creation_time);
588         root->last_write_time  = le64_to_cpu(ni->last_data_change_time);
589         root->last_access_time = le64_to_cpu(ni->last_access_time);
590         root->attributes       = le32_to_cpu(attributes);
591         root->link_group_id    = ni->mft_no;
592         root->resolved         = true;
593
594         if (attributes & FILE_ATTR_REPARSE_POINT) {
595                 /* Junction point, symbolic link, or other reparse point */
596                 ret = capture_ntfs_streams(root, ni, path, path_len,
597                                            lookup_table, ntfs_vol_p,
598                                            AT_REPARSE_POINT);
599         } else if (mrec_flags & MFT_RECORD_IS_DIRECTORY) {
600
601                 /* Normal directory */
602                 s64 pos = 0;
603                 struct readdir_ctx ctx = {
604                         .parent       = root,
605                         .dir_ni       = ni,
606                         .path         = path,
607                         .path_len     = path_len,
608                         .lookup_table = lookup_table,
609                         .sd_set       = sd_set,
610                         .config       = config,
611                         .ntfs_vol_p   = ntfs_vol_p,
612                         .flags        = flags,
613                 };
614                 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
615                 if (ret != 0) {
616                         ERROR_WITH_ERRNO("ntfs_readdir()");
617                         ret = WIMLIB_ERR_NTFS_3G;
618                 }
619         } else {
620                 /* Normal file */
621                 ret = capture_ntfs_streams(root, ni, path, path_len,
622                                            lookup_table, ntfs_vol_p,
623                                            AT_DATA);
624         }
625         if (ret != 0)
626                 return ret;
627
628 #ifdef HAVE_NTFS_INODE_FUNCTIONS
629         ret = ntfs_get_inode_security(ni,
630                                       OWNER_SECURITY_INFORMATION |
631                                       GROUP_SECURITY_INFORMATION |
632                                       DACL_SECURITY_INFORMATION  |
633                                       SACL_SECURITY_INFORMATION,
634                                       NULL, 0, &sd_size);
635         char sd[sd_size];
636         ret = ntfs_get_inode_security(ni,
637                                       OWNER_SECURITY_INFORMATION |
638                                       GROUP_SECURITY_INFORMATION |
639                                       DACL_SECURITY_INFORMATION  |
640                                       SACL_SECURITY_INFORMATION,
641                                       sd, sd_size, &sd_size);
642         if (ret == 0) {
643                 ERROR_WITH_ERRNO("Failed to get security information from "
644                                  "`%s'", path);
645                 ret = WIMLIB_ERR_NTFS_3G;
646         } else {
647                 if (ret > 0) {
648                         /*print_security_descriptor(sd, sd_size);*/
649                         root->security_id = sd_set_add_sd(sd_set, sd, ret);
650                         if (root->security_id == -1) {
651                                 ERROR("Out of memory");
652                                 return WIMLIB_ERR_NOMEM;
653                         }
654                         DEBUG("Added security ID = %u for `%s'",
655                               root->security_id, path);
656                 } else { 
657                         root->security_id = -1;
658                         DEBUG("No security ID for `%s'", path);
659                 }
660                 ret = 0;
661         }
662 #else
663         char _sd[1];
664         char *sd = _sd;
665         errno = 0;
666         ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
667                                          ni, dir_ni, sd,
668                                          sizeof(sd));
669         if (ret > sizeof(sd)) {
670                 sd = alloca(ret);
671                 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
672                                                  ni, dir_ni, sd, ret);
673         }
674         if (ret > 0) {
675                 root->security_id = sd_set_add_sd(sd_set, sd, ret);
676                 if (root->security_id == -1) {
677                         ERROR("Out of memory");
678                         return WIMLIB_ERR_NOMEM;
679                 }
680                 DEBUG("Added security ID = %u for `%s'",
681                       root->security_id, path);
682                 ret = 0;
683         } else if (ret < 0) {
684                 ERROR_WITH_ERRNO("Failed to get security information from "
685                                  "`%s'", path);
686                 ret = WIMLIB_ERR_NTFS_3G;
687         } else {
688                 root->security_id = -1;
689                 DEBUG("No security ID for `%s'", path);
690         }
691 #endif
692         return ret;
693 }
694
695 static int build_dentry_tree_ntfs(struct dentry **root_p,
696                                   const char *device,
697                                   struct lookup_table *lookup_table,
698                                   struct wim_security_data *sd,
699                                   const struct capture_config *config,
700                                   int flags,
701                                   void *extra_arg)
702 {
703         ntfs_volume *vol;
704         ntfs_inode *root_ni;
705         int ret = 0;
706         struct sd_set sd_set = {
707                 .sd = sd,
708                 .root = NULL,
709         };
710         ntfs_volume **ntfs_vol_p = extra_arg;
711
712         DEBUG("Mounting NTFS volume `%s' read-only", device);
713         
714         vol = ntfs_mount(device, MS_RDONLY);
715         if (!vol) {
716                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
717                                  device);
718                 return WIMLIB_ERR_NTFS_3G;
719         }
720         ntfs_open_secure(vol);
721
722         /* We don't want to capture the special NTFS files such as $Bitmap.  Not
723          * to be confused with "hidden" or "system" files which are real files
724          * that we do need to capture.  */
725         NVolClearShowSysFiles(vol);
726
727         DEBUG("Opening root NTFS dentry");
728         root_ni = ntfs_inode_open(vol, FILE_root);
729         if (!root_ni) {
730                 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
731                                  "`%s'", device);
732                 ret = WIMLIB_ERR_NTFS_3G;
733                 goto out;
734         }
735
736         /* Currently we assume that all the UTF-8 paths fit into this length and
737          * there is no check for overflow. */
738         char *path = MALLOC(32768);
739         if (!path) {
740                 ERROR("Could not allocate memory for NTFS pathname");
741                 goto out_cleanup;
742         }
743
744         path[0] = '/';
745         path[1] = '\0';
746         ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
747                                                FILE_NAME_POSIX, lookup_table,
748                                                &sd_set, config, ntfs_vol_p,
749                                                flags);
750 out_cleanup:
751         FREE(path);
752         ntfs_inode_close(root_ni);
753         destroy_sd_set(&sd_set);
754
755 out:
756         if (ret) {
757                 if (ntfs_umount(vol, FALSE) != 0) {
758                         ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
759                                          device);
760                         if (ret == 0)
761                                 ret = WIMLIB_ERR_NTFS_3G;
762                 }
763         } else {
764                 /* We need to leave the NTFS volume mounted so that we can read
765                  * the NTFS files again when we are actually writing the WIM */
766                 *ntfs_vol_p = vol;
767         }
768         return ret;
769 }
770
771
772
773 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
774                                                 const char *device,
775                                                 const char *name,
776                                                 const char *config_str,
777                                                 size_t config_len,
778                                                 int flags)
779 {
780         if (flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
781                 ERROR("Cannot dereference files when capturing directly from NTFS");
782                 return WIMLIB_ERR_INVALID_PARAM;
783         }
784         return do_add_image(w, device, name, config_str, config_len, flags,
785                             build_dentry_tree_ntfs, &w->ntfs_vol);
786 }
787
788 #else /* WITH_NTFS_3G */
789 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
790                                                 const char *device,
791                                                 const char *name,
792                                                 const char *config_str,
793                                                 size_t config_len,
794                                                 int flags)
795 {
796         ERROR("wimlib was compiled without support for NTFS-3g, so");
797         ERROR("we cannot capture a WIM image directly from a NTFS volume");
798         return WIMLIB_ERR_UNSUPPORTED;
799 }
800 #endif /* WITH_NTFS_3G */