]> wimlib.net Git - wimlib/blobdiff - src/resource.c
Use WIM_LINK_TYPE_NONE as default, as documented
[wimlib] / src / resource.c
index e315d33567594da7c33b41574293f780c75f77d7..a5f0e4d70ba9050b8e3f3380936d0929e6431c25 100644 (file)
@@ -2,24 +2,26 @@
  * resource.c
  *
  * Read uncompressed and compressed metadata and file resources.
- *
+ */
+
+/*
  * Copyright (C) 2010 Carl Thijssen
  * Copyright (C) 2012 Eric Biggers
  *
- * wimlib - Library for working with WIM files
+ * This file is part of wimlib, a library for working with WIM files.
  *
- * This library 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
- * Software Foundation; either version 2.1 of the License, or (at your option) any
- * later version.
+ * wimlib 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
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
  *
- * This library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU Lesser General Public License along
- * with this library; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
 #include "wimlib_internal.h"
@@ -32,8 +34,6 @@
 #include <unistd.h>
 #include <errno.h>
 
-/* Used for buffering FILE IO */
-#define BUFFER_SIZE 4096
 
 /* 
  * Reads all or part of a compressed resource into an in-memory buffer.
@@ -916,13 +916,13 @@ static int transfer_file_resource(FILE *in, u64 size, u64 original_size,
  * @return:    True on success, false on failure.
  */
 int read_metadata_resource(FILE *fp, const struct resource_entry *res_entry,
-                          int wim_ctype, struct dentry **root_dentry_p)
+                          int wim_ctype, struct image_metadata *image_metadata)
 {
        u8 *buf;
        int ctype;
        u32 dentry_offset;
        int ret;
-       struct dentry *dentry;
+       struct dentry *dentry = NULL;
 
        DEBUG("Reading metadata resource: length = %lu, offset = %lu\n",
                        res_entry->original_size, res_entry->offset);
@@ -955,11 +955,6 @@ int read_metadata_resource(FILE *fp, const struct resource_entry *res_entry,
 
        DEBUG("Finished reading metadata resource into memory.\n");
 
-#if 0
-       /* Read the security data into a WIMSecurityData structure. */
-       if (!read_security_data(buf, res_entry->original_size, sd))
-               goto err1;
-#endif
 
        dentry = MALLOC(sizeof(struct dentry));
        if (!dentry) {
@@ -974,7 +969,17 @@ int read_metadata_resource(FILE *fp, const struct resource_entry *res_entry,
         *
         * The security data starts with a 4-byte integer giving its total
         * length. */
+
+       /* Read the security data into a WIMSecurityData structure. */
+#ifdef ENABLE_SECURITY_DATA
+       ret = read_security_data(buf, res_entry->original_size, 
+                                &image_metadata->security_data);
+       if (ret != 0)
+               goto err1;
+#endif
        get_u32(buf, &dentry_offset);
+       if (dentry_offset == 0)
+               dentry_offset = 8;
        dentry_offset += (8 - dentry_offset % 8) % 8;
                
        ret = read_dentry(buf, res_entry->original_size, dentry_offset, dentry);
@@ -996,7 +1001,7 @@ int read_metadata_resource(FILE *fp, const struct resource_entry *res_entry,
        if (ret != 0)
                goto err2;
 
-       *root_dentry_p = dentry;
+       image_metadata->root_dentry = dentry;
        FREE(buf);
        return ret;
 err2:
@@ -1013,7 +1018,7 @@ int write_metadata_resource(WIMStruct *w)
        u8 *buf;
        u8 *p;
        int ret;
-       off_t subdir_offset;
+       u64 subdir_offset;
        struct dentry *root;
        struct lookup_table_entry *lte;
        struct resource_entry *res_entry;
@@ -1032,7 +1037,13 @@ int write_metadata_resource(WIMStruct *w)
        if (metadata_offset == -1)
                return WIMLIB_ERR_WRITE;
 
-       subdir_offset = 8 + root->length + 8;
+       #ifdef ENABLE_SECURITY_DATA
+       struct wim_security_data *sd = wim_security_data(w);
+       if (sd)
+               subdir_offset = sd->total_length + root->length + 8;
+       else
+       #endif
+               subdir_offset = 8 + root->length + 8;
        calculate_subdir_offsets(root, &subdir_offset);
        metadata_original_size = subdir_offset;
        buf = MALLOC(metadata_original_size);
@@ -1041,12 +1052,11 @@ int write_metadata_resource(WIMStruct *w)
                                "metadata resource\n", metadata_original_size);
                return WIMLIB_ERR_NOMEM;
        }
-       p = buf;
-       #if 0
+       #ifdef ENABLE_SECURITY_DATA
        /* Write the security data. */
-       p = write_security_data(wim_security_data(w), p);
+       p = write_security_data(sd, buf);
        #else
-       p = put_u32(p, 8); /* Total length of security data. */
+       p = put_u32(buf, 8); /* Total length of security data. */
        p = put_u32(p, 0); /* Number of security data entries. */
        #endif
 
@@ -1102,8 +1112,6 @@ int write_file_resource(struct dentry *dentry, void *wim_p)
        struct lookup_table_entry *lte;
        int in_wim_ctype;
        int out_wim_ctype;
-       int input_res_ctype;
-       struct resource_entry *input_res_entry;
        struct resource_entry *output_res_entry;
        u64 len;
        int ret;
@@ -1128,6 +1136,10 @@ int write_file_resource(struct dentry *dentry, void *wim_p)
        out_wim_ctype = wimlib_get_compression_type(w);
        output_res_entry = &lte->output_resource_entry;
 
+       /* do not write empty resources */
+       if (lte->resource_entry.original_size == 0)
+               return 0;
+
        /* Figure out if we can read the resource from the WIM file, or
         * if we have to read it from the filesystem outside. */
        if (lte->file_on_disk) {
@@ -1165,15 +1177,14 @@ int write_file_resource(struct dentry *dentry, void *wim_p)
                        in = w->fp;
                        in_wim_ctype = out_wim_ctype;
                }
-               input_res_entry = &lte->resource_entry;
-               input_res_ctype = resource_compression_type(
+               int input_res_ctype = resource_compression_type(
                                        in_wim_ctype, 
-                                       input_res_entry->flags);
+                                       lte->resource_entry.flags);
 
                ret = transfer_file_resource(in, 
-                                       input_res_entry->size,
-                                       input_res_entry->original_size, 
-                                       input_res_entry->offset,
+                                       lte->resource_entry.size,
+                                       lte->resource_entry.original_size, 
+                                       lte->resource_entry.offset,
                                        input_res_ctype, 
                                        out, 
                                        out_wim_ctype,