]> wimlib.net Git - wimlib/blobdiff - src/ntfs-3g_capture.c
configure.ac: generate version number from git commit and tags
[wimlib] / src / ntfs-3g_capture.c
index 8408a461f01fb9604437ea3a537f4c4145ec173b..1745cbd1f6dff8308bb5759d77d2a70234b6931d 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 /*
- * Copyright (C) 2012-2016 Eric Biggers
+ * Copyright (C) 2012-2017 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -31,6 +31,7 @@
 #include <errno.h>
 
 #include <ntfs-3g/attrib.h>
+#include <ntfs-3g/compat.h> /* for ENODATA, if needed */
 #include <ntfs-3g/object_id.h>
 #include <ntfs-3g/reparse.h>
 #include <ntfs-3g/security.h>
@@ -64,6 +65,7 @@
 struct ntfs_volume_wrapper {
        ntfs_volume *vol;
        size_t refcnt;
+       bool dedup_warned;
 };
 
 /* Description of where data is located in an NTFS volume  */
@@ -115,7 +117,8 @@ open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
 
 int
 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
-                          const struct read_blob_callbacks *cbs)
+                          const struct consume_chunk_callback *cb,
+                          bool recover_data)
 {
        const struct ntfs_location *loc = blob->ntfs_loc;
        ntfs_volume *vol = loc->volume->vol;
@@ -152,7 +155,7 @@ read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
                }
                pos += to_read;
                bytes_remaining -= to_read;
-               ret = call_consume_chunk(buf, to_read, cbs);
+               ret = consume_chunk(cb, buf, to_read);
                if (ret)
                        goto out_close_ntfs_attr;
        }
@@ -230,6 +233,27 @@ read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
        return 0;
 }
 
+
+static void
+warn_special_reparse_points(const struct wim_inode *inode,
+                           const struct scan_params *params,
+                           struct ntfs_volume_wrapper *volume)
+{
+       if (inode->i_reparse_tag == WIM_IO_REPARSE_TAG_DEDUP &&
+           (params->add_flags & WIMLIB_ADD_FLAG_WINCONFIG) &&
+           !volume->dedup_warned)
+       {
+               WARNING(
+         "Filesystem includes files deduplicated with Windows'\n"
+"          Data Deduplication feature, which to properly restore\n"
+"          would require that the chunk store in \"System Volume Information\"\n"
+"          be included in the WIM image. By default \"System Volume Information\"\n"
+"          is excluded, so you may want to use a custom capture configuration\n"
+"          file which includes it.");
+               volume->dedup_warned = true;
+       }
+}
+
 static int
 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
 {
@@ -607,8 +631,6 @@ destroy_dos_name_map(struct dos_name_map *map)
 
 struct readdir_ctx {
        struct wim_dentry *parent;
-       char *path;
-       size_t path_len;
        struct dos_name_map dos_name_map;
        struct ntfs_volume_wrapper *volume;
        struct scan_params *params;
@@ -618,8 +640,7 @@ struct readdir_ctx {
 static int
 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
                                    const MFT_REF mref,
-                                   char *path,
-                                   size_t path_len,
+                                   const char *filename,
                                    int name_type,
                                    struct ntfs_volume_wrapper *volume,
                                    struct scan_params *params);
@@ -630,10 +651,11 @@ filldir(void *_ctx, const ntfschar *name, const int name_nchars,
        const unsigned dt_type)
 {
        struct readdir_ctx *ctx = _ctx;
+       struct scan_params *params = ctx->params;
        const size_t name_nbytes = name_nchars * sizeof(ntfschar);
        char *mbs_name;
        size_t mbs_name_nbytes;
-       size_t path_len;
+       size_t orig_path_nchars;
        struct wim_dentry *child;
        int ret;
 
@@ -655,16 +677,17 @@ filldir(void *_ctx, const ntfschar *name, const int name_nchars,
        if (should_ignore_filename(mbs_name, mbs_name_nbytes))
                goto out_free_mbs_name;
 
-       path_len = ctx->path_len;
-       if (path_len != 1)
-               ctx->path[path_len++] = '/';
-       memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
-       path_len += mbs_name_nbytes;
+       ret = WIMLIB_ERR_NOMEM;
+       if (!pathbuf_append_name(params, mbs_name, mbs_name_nbytes,
+                                &orig_path_nchars))
+               goto out_free_mbs_name;
+
        child = NULL;
-       ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
-                                                 path_len, name_type,
-                                                 ctx->volume, ctx->params);
-       attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
+       ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, mbs_name,
+                                                 name_type, ctx->volume,
+                                                 params);
+       pathbuf_truncate(params, orig_path_nchars);
+       attach_scanned_tree(ctx->parent, child, params->blob_table);
 out_free_mbs_name:
        FREE(mbs_name);
 out:
@@ -673,8 +696,7 @@ out:
 }
 
 static int
-ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
-                         struct wim_dentry *parent,
+ntfs_3g_recurse_directory(ntfs_inode *ni, struct wim_dentry *parent,
                          struct ntfs_volume_wrapper *volume,
                          struct scan_params *params)
 {
@@ -682,22 +704,20 @@ ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
        s64 pos = 0;
        struct readdir_ctx ctx = {
                .parent          = parent,
-               .path            = path,
-               .path_len        = path_len,
                .dos_name_map    = { .root = NULL },
                .volume          = volume,
                .params          = params,
                .ret             = 0,
        };
        ret = ntfs_readdir(ni, &pos, &ctx, filldir);
-       path[path_len] = '\0';
        if (unlikely(ret)) {
                if (ctx.ret) {
                        /* wimlib error  */
                        ret = ctx.ret;
                } else {
                        /* error from ntfs_readdir() itself  */
-                       ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
+                       ERROR_WITH_ERRNO("Error reading directory \"%s\"",
+                                        params->cur_path);
                        ret = WIMLIB_ERR_NTFS_3G;
                }
        } else {
@@ -717,19 +737,19 @@ ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
 static int
 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                                    const MFT_REF mref,
-                                   char *path,
-                                   size_t path_len,
+                                   const char *filename,
                                    int name_type,
                                    struct ntfs_volume_wrapper *volume,
                                    struct scan_params *params)
 {
+       const char *path = params->cur_path;
        u32 attributes;
        int ret;
        struct wim_dentry *root = NULL;
        struct wim_inode *inode = NULL;
        ntfs_inode *ni = NULL;
 
-       ret = try_exclude(path, params);
+       ret = try_exclude(params);
        if (unlikely(ret < 0)) /* Excluded? */
                goto out_progress;
        if (unlikely(ret > 0)) /* Error? */
@@ -758,14 +778,12 @@ ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                        ret = WIMLIB_ERR_UNSUPPORTED_FILE;
                        goto out;
                }
-               params->progress.scan.cur_path = path;
                ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
                goto out;
        }
 
        /* Create a WIM dentry with an associated inode, which may be shared */
-       ret = inode_table_new_dentry(params->inode_table,
-                                    path_basename_with_len(path, path_len),
+       ret = inode_table_new_dentry(params->inode_table, filename,
                                     ni->mft_no, 0, false, &root);
        if (ret)
                goto out;
@@ -792,6 +810,8 @@ ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
                                                volume, AT_REPARSE_POINT);
                if (ret)
                        goto out;
+
+               warn_special_reparse_points(inode, params, volume);
        }
 
        /* Load the object ID.  */
@@ -829,14 +849,12 @@ ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
        }
 
        if (inode_is_directory(inode)) {
-               ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
-                                               volume, params);
+               ret = ntfs_3g_recurse_directory(ni, root, volume, params);
                if (ret)
                        goto out;
        }
 
 out_progress:
-       params->progress.scan.cur_path = path;
        if (root == NULL)
                ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
        else
@@ -847,7 +865,7 @@ out:
        if (unlikely(ret)) {
                free_dentry_tree(root, params->blob_table);
                root = NULL;
-               ret = report_scan_error(params, ret, path);
+               ret = report_scan_error(params, ret);
        }
        *root_ret = root;
        return ret;
@@ -859,10 +877,9 @@ ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
 {
        struct ntfs_volume_wrapper *volume;
        ntfs_volume *vol;
-       char *path;
        int ret;
 
-       volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
+       volume = CALLOC(1, sizeof(struct ntfs_volume_wrapper));
        if (!volume)
                return WIMLIB_ERR_NOMEM;
 
@@ -877,31 +894,40 @@ ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
        volume->vol = vol;
        volume->refcnt = 1;
 
-       ntfs_open_secure(vol);
+       /* Currently, libntfs-3g users that need to read security descriptors
+        * are required to call ntfs_open_secure() to open the volume's security
+        * descriptor index, "$Secure".  This is only required to work on NTFS
+        * v3.0+, as older versions did not have a security descriptor index. */
+       if (ntfs_open_secure(vol) && vol->major_ver >= 3) {
+               ERROR_WITH_ERRNO("Unable to open security descriptor index of "
+                                "NTFS volume \"%s\"", device);
+               ret = WIMLIB_ERR_NTFS_3G;
+               goto out_put_ntfs_volume;
+       }
 
        /* We don't want to capture the special NTFS files such as $Bitmap.  Not
         * to be confused with "hidden" or "system" files which are real files
         * that we do need to capture.  */
        NVolClearShowSysFiles(vol);
 
-       /* Currently we assume that all the paths fit into this length and there
-        * is no check for overflow.  */
-       path = MALLOC(32768);
-       if (!path) {
-               ret = WIMLIB_ERR_NOMEM;
-               goto out_put_ntfs_volume;
-       }
+       ret = pathbuf_init(params, "/");
+       if (ret)
+               goto out_close_secure;
 
-       path[0] = '/';
-       path[1] = '\0';
-       ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
+       ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, "",
                                                  FILE_NAME_POSIX, volume,
                                                  params);
-       FREE(path);
+out_close_secure:
+       /* Undo the effects of ntfs_open_secure().  This is not yet done
+        * automatically by ntfs_umount().  But NULL out the inode to
+        * potentially be robust against future versions doing so. */
+       if (vol->secure_ni) {
+               ntfs_index_ctx_put(vol->secure_xsii);
+               ntfs_index_ctx_put(vol->secure_xsdh);
+               ntfs_inode_close(vol->secure_ni);
+               vol->secure_ni = NULL;
+       }
 out_put_ntfs_volume:
-       ntfs_index_ctx_put(vol->secure_xsii);
-       ntfs_index_ctx_put(vol->secure_xsdh);
-       ntfs_inode_close(vol->secure_ni);
        put_ntfs_volume(volume);
        return ret;
 }