Improve helper functions for setting blob locations
authorEric Biggers <ebiggers3@gmail.com>
Thu, 2 Apr 2015 04:43:54 +0000 (23:43 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Fri, 3 Apr 2015 23:35:46 +0000 (18:35 -0500)
include/wimlib/blob_table.h
src/blob_table.c
src/extract.c
src/resource.c
src/unix_apply.c
src/write.c

index e273de2531b8fd16280b5737c6421526e32afb75..c86fac0d932efc9507b05c4549a3d83b1e0b6bc7 100644 (file)
@@ -345,13 +345,31 @@ blob_extraction_targets(struct blob_descriptor *blob)
                return blob->blob_extraction_targets;
 }
 
+/*
+ * Declare that the specified blob is located in the specified WIM resource at
+ * the specified offset.  The caller is expected to set blob->size if required.
+ */
 static inline void
 blob_set_is_located_in_wim_resource(struct blob_descriptor *blob,
-                                   struct wim_resource_descriptor *rdesc)
+                                   struct wim_resource_descriptor *rdesc,
+                                   u64 offset_in_res)
 {
        blob->blob_location = BLOB_IN_WIM;
        blob->rdesc = rdesc;
        list_add_tail(&blob->rdesc_node, &rdesc->blob_list);
+       blob->offset_in_res = offset_in_res;
+}
+
+/*
+ * Declare that the specified blob is located in the specified non-solid WIM
+ * resource.  In this case, the blob data is the entire uncompressed resource.
+ */
+static inline void
+blob_set_is_located_in_nonsolid_wim_resource(struct blob_descriptor *blob,
+                                            struct wim_resource_descriptor *rdesc)
+{
+       blob_set_is_located_in_wim_resource(blob, rdesc, 0);
+       blob->size = rdesc->uncompressed_size;
 }
 
 static inline void
@@ -361,6 +379,15 @@ blob_unset_is_located_in_wim_resource(struct blob_descriptor *blob)
        blob->blob_location = BLOB_NONEXISTENT;
 }
 
+static inline void
+blob_set_is_located_in_attached_buffer(struct blob_descriptor *blob,
+                                      void *buffer, size_t size)
+{
+       blob->blob_location = BLOB_IN_ATTACHED_BUFFER;
+       blob->attached_buffer = buffer;
+       blob->size = size;
+}
+
 extern struct blob_descriptor *
 new_blob_from_data_buffer(const void *buffer, size_t size,
                          struct blob_table *blob_table);
index c94635f105b57add5fc422cf6017da9c57944bbb..cf25d005225c67af63791125760f892dd0f4487a 100644 (file)
@@ -740,8 +740,7 @@ assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
        blob->size = reshdr->size_in_wim;
        for (size_t i = 0; i < num_rdescs; i++) {
                if (offset + blob->size <= rdescs[i]->uncompressed_size) {
-                       blob->offset_in_res = offset;
-                       blob_set_is_located_in_wim_resource(blob, rdescs[i]);
+                       blob_set_is_located_in_wim_resource(blob, rdescs[i], offset);
                        return 0;
                }
                offset -= rdescs[i]->uncompressed_size;
@@ -1011,10 +1010,7 @@ read_blob_table(WIMStruct *wim)
 
                        wim_res_hdr_to_desc(&reshdr, wim, rdesc);
 
-                       cur_blob->offset_in_res = 0;
-                       cur_blob->size = reshdr.uncompressed_size;
-
-                       blob_set_is_located_in_wim_resource(cur_blob, rdesc);
+                       blob_set_is_located_in_nonsolid_wim_resource(cur_blob, rdesc);
                }
 
                /* cur_blob is now a blob bound to a resource.  */
@@ -1275,9 +1271,7 @@ new_blob_from_data_buffer(const void *buffer, size_t size,
                        free_blob_descriptor(blob);
                        return NULL;
                }
-               blob->blob_location = BLOB_IN_ATTACHED_BUFFER;
-               blob->attached_buffer = buffer_copy;
-               blob->size = size;
+               blob_set_is_located_in_attached_buffer(blob, buffer_copy, size);
                copy_hash(blob->hash, hash);
                blob_table_insert(blob_table, blob);
        }
index 56ec644a0b2d31aad4fcb68fe32b442151daf557..5945a4d804cddde3aa90865ab3354d9e8cc32d56 100644 (file)
@@ -181,9 +181,7 @@ read_pwm_blob_header(WIMStruct *pwm, struct blob_descriptor *blob,
        reshdr.offset_in_wim = pwm->in_fd.offset;
        reshdr.uncompressed_size = le64_to_cpu(buf.blob_hdr.uncompressed_size);
        wim_res_hdr_to_desc(&reshdr, pwm, rdesc);
-       blob_set_is_located_in_wim_resource(blob, rdesc);
-       blob->size = rdesc->uncompressed_size;
-       blob->offset_in_res = 0;
+       blob_set_is_located_in_nonsolid_wim_resource(blob, rdesc);
        blob->is_metadata = (rdesc->flags & WIM_RESHDR_FLAG_METADATA) != 0;
        return 0;
 
@@ -234,11 +232,8 @@ read_blobs_from_pipe(struct apply_ctx *ctx,
                    && (needed_blob = lookup_blob(blob_table, found_blob->hash))
                    && (needed_blob->out_refcnt))
                {
-                       needed_blob->offset_in_res = found_blob->offset_in_res;
-                       needed_blob->size = found_blob->size;
-
                        blob_unset_is_located_in_wim_resource(found_blob);
-                       blob_set_is_located_in_wim_resource(needed_blob, rdesc);
+                       blob_set_is_located_in_nonsolid_wim_resource(needed_blob, rdesc);
 
                        ret = (*cbs->begin_blob)(needed_blob,
                                                 cbs->begin_blob_ctx);
index 11e09d67b0df0b91a3bacfc4a59620da63d874b9..a49507f3778df55b652b54df4c0e00b5e480cf3a 100644 (file)
@@ -912,10 +912,7 @@ wim_reshdr_to_data(const struct wim_reshdr *reshdr, WIMStruct *wim, void **buf_r
        struct blob_descriptor blob;
 
        wim_res_hdr_to_desc(reshdr, wim, &rdesc);
-       blob_set_is_located_in_wim_resource(&blob, &rdesc);
-
-       blob.size = rdesc.uncompressed_size;
-       blob.offset_in_res = 0;
+       blob_set_is_located_in_nonsolid_wim_resource(&blob, &rdesc);
 
        return read_full_blob_into_alloc_buf(&blob, buf_ret);
 }
@@ -929,10 +926,7 @@ wim_reshdr_to_hash(const struct wim_reshdr *reshdr, WIMStruct *wim,
        int ret;
 
        wim_res_hdr_to_desc(reshdr, wim, &rdesc);
-       blob_set_is_located_in_wim_resource(&blob, &rdesc);
-
-       blob.size = rdesc.uncompressed_size;
-       blob.offset_in_res = 0;
+       blob_set_is_located_in_nonsolid_wim_resource(&blob, &rdesc);
        blob.unhashed = 1;
 
        ret = sha1_blob(&blob);
index a0b62519c02a974b46a7df8a80f341703c24621b..e6bd2321276baeae20b1f177c21b211a24cd45df 100644 (file)
@@ -503,9 +503,8 @@ unix_create_symlink(const struct wim_inode *inode, const char *path,
        int ret;
        struct blob_descriptor blob_override;
 
-       blob_override.blob_location = BLOB_IN_ATTACHED_BUFFER;
-       blob_override.attached_buffer = (void *)rpdata;
-       blob_override.size = rpdatalen;
+       blob_set_is_located_in_attached_buffer(&blob_override,
+                                              (void *)rpdata, rpdatalen);
 
        ret = wim_inode_readlink(inode, link_target,
                                 sizeof(link_target) - 1, &blob_override);
index 75a6fcf2e454a0785248ea800c985fe9ea60faa2..6be215e3e76570bd38aa12fc28afec740911f92f 100644 (file)
@@ -1788,9 +1788,7 @@ write_wim_resource_from_buffer(const void *buf,
        int ret;
        struct blob_descriptor blob;
 
-       blob.blob_location = BLOB_IN_ATTACHED_BUFFER;
-       blob.attached_buffer = (void*)buf;
-       blob.size = buf_size;
+       blob_set_is_located_in_attached_buffer(&blob, (void *)buf, buf_size);
        sha1_buffer(buf, buf_size, blob.hash);
        blob.unhashed = 0;
        blob.is_metadata = is_metadata;