]> wimlib.net Git - wimlib/blobdiff - src/ntfs-apply.c
extract_wim_resource() refactor
[wimlib] / src / ntfs-apply.c
index 7522ec898f9e6aada30cde4fde4ea33d9eddecc7..f1ff0dd1adc6f28713b496ba35b826e9902c03fb 100644 (file)
@@ -34,7 +34,7 @@
 #include "wimlib_internal.h"
 #include "dentry.h"
 #include "lookup_table.h"
-#include "io.h"
+#include "buffer_io.h"
 #include <ntfs-3g/layout.h>
 #include <ntfs-3g/acls.h>
 #include <ntfs-3g/attrib.h>
 #include <stdlib.h>
 #include <unistd.h>
 
+static int extract_wim_chunk_to_ntfs_attr(const u8 *buf, size_t len,
+                                         u64 offset, void *arg)
+{
+       ntfs_attr *na = arg;
+       if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
+               return 0;
+       } else {
+               ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
+               return WIMLIB_ERR_WRITE;
+       }
+}
+
 /*
  * Extracts a WIM resource to a NTFS attribute.
  */
@@ -51,37 +63,8 @@ static int
 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
                                  ntfs_attr *na)
 {
-       u64 bytes_remaining = wim_resource_size(lte);
-       u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
-       u64 offset = 0;
-       int ret = 0;
-       u8 hash[SHA1_HASH_SIZE];
-
-       SHA_CTX ctx;
-       sha1_init(&ctx);
-
-       while (bytes_remaining) {
-               u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
-               ret = read_wim_resource(lte, buf, to_read, offset, 0);
-               if (ret != 0)
-                       break;
-               sha1_update(&ctx, buf, to_read);
-               if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
-                       ERROR_WITH_ERRNO("Error extracting WIM resource");
-                       return WIMLIB_ERR_WRITE;
-               }
-               bytes_remaining -= to_read;
-               offset += to_read;
-       }
-       sha1_final(hash, &ctx);
-       if (!hashes_equal(hash, lte->hash)) {
-               ERROR("Invalid checksum on a WIM resource "
-                     "(detected when extracting to NTFS stream)");
-               ERROR("The following WIM resource is invalid:");
-               print_lookup_table_entry(lte);
-               return WIMLIB_ERR_INVALID_RESOURCE_HASH;
-       }
-       return 0;
+       return extract_wim_resource(lte, wim_resource_size(lte),
+                                   extract_wim_chunk_to_ntfs_attr, na);
 }
 
 /* Writes the data streams to a NTFS file
@@ -161,10 +144,10 @@ static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
  *
  * Return 0 on success, nonzero on failure.
  */
-static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
-                                  const struct inode *inode,
-                                  ntfs_inode *dir_ni,
-                                  ntfs_inode **to_ni_ret)
+static int apply_hardlink_ntfs(const struct dentry *from_dentry,
+                              const struct inode *inode,
+                              ntfs_inode *dir_ni,
+                              ntfs_inode **to_ni_ret)
 {
        int ret;
        char *p;
@@ -313,8 +296,8 @@ static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
        return 0;
 }
 
-static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
-                                   struct apply_args *args);
+static int do_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
+                               struct apply_args *args);
 
 /*
  * If @dentry is part of a hard link group, search for hard-linked dentries in
@@ -355,8 +338,8 @@ static int preapply_dentry_with_dos_name(struct dentry *dentry,
 
                DEBUG("pre-applying DOS name `%s'",
                      dentry_with_dos_name->full_path_utf8);
-               ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
-                                              *dir_ni_p, args);
+               ret = do_apply_dentry_ntfs(dentry_with_dos_name,
+                                          *dir_ni_p, args);
                if (ret != 0)
                        return ret;
                p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
@@ -387,8 +370,8 @@ static int preapply_dentry_with_dos_name(struct dentry *dentry,
  *
  * @return:  0 on success; nonzero on failure.
  */
-static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
-                                   struct apply_args *args)
+static int do_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
+                               struct apply_args *args)
 {
        int ret = 0;
        mode_t type;
@@ -417,8 +400,8 @@ static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
                         * group.  We can make a hard link instead of extracting
                         * the file data. */
                        if (inode->extracted_file) {
-                               ret = wim_apply_hardlink_ntfs(dentry, inode,
-                                                             dir_ni, &ni);
+                               ret = apply_hardlink_ntfs(dentry, inode,
+                                                         dir_ni, &ni);
                                is_hardlink = true;
                                if (ret)
                                        goto out_close_dir_ni;
@@ -559,9 +542,8 @@ out_close_dir_ni:
        return ret;
 }
 
-static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
-                                     ntfs_volume *vol,
-                                     const WIMStruct *w)
+static int apply_root_dentry_ntfs(const struct dentry *dentry,
+                                 ntfs_volume *vol, const WIMStruct *w)
 {
        ntfs_inode *ni;
        int ret = 0;
@@ -582,7 +564,7 @@ static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
 }
 
 /* Applies a WIM dentry to the NTFS volume */
-int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
+int apply_dentry_ntfs(struct dentry *dentry, void *arg)
 {
        struct apply_args *args = arg;
        ntfs_volume *vol             = args->vol;
@@ -600,7 +582,6 @@ int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
                if (inode_unnamed_lte_resolved(dentry->d_inode))
                        return 0;
 
-
        DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
 
        if ((extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) &&
@@ -612,7 +593,7 @@ int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
        }
 
        if (dentry_is_root(dentry))
-               return wim_apply_root_dentry_ntfs(dentry, vol, w);
+               return apply_root_dentry_ntfs(dentry, vol, w);
 
        p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
        do {
@@ -630,10 +611,10 @@ int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
                                 dir_name);
                return WIMLIB_ERR_NTFS_3G;
        }
-       return do_wim_apply_dentry_ntfs(dentry, dir_ni, arg);
+       return do_apply_dentry_ntfs(dentry, dir_ni, arg);
 }
 
-int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
+int apply_dentry_timestamps_ntfs(struct dentry *dentry, void *arg)
 {
        struct apply_args *args = arg;
        ntfs_volume *vol = args->vol;