]> wimlib.net Git - wimlib/blobdiff - src/split.c
Use list_replace() instead of list_transfer()
[wimlib] / src / split.c
index f38c332ada5d25b1e882de6272195f18e820bf57..6124a67da81c09317ca5c2ba993c05cb4f6e3067 100644 (file)
  */
 
 /*
- * Copyright (C) 2012 Eric Biggers
+ * Copyright (C) 2012, 2013 Eric Biggers
  *
- * This file is part of wimlib, a library for working with WIM files.
+ * 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
+ * Software Foundation; either version 3 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 General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * 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 General Public License for more
+ * This file 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 General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
  */
 
-#include "wimlib_internal.h"
-#include "lookup_table.h"
-#include "xml.h"
-#include "io.h"
-
-struct split_args {
-       WIMStruct *w;
-       char *swm_base_name;
-       size_t swm_base_name_len;
-       const char *swm_suffix;
-       struct lookup_table_entry *lte_chain_head;
-       struct lookup_table_entry *lte_chain_tail;
-       int    part_number;
-       int    write_flags;
-       long   size_remaining;
-       size_t part_size;
-       u64    total_bytes;
-       u64    total_bytes_written;
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib.h"
+#include "wimlib/alloca.h"
+#include "wimlib/blob_table.h"
+#include "wimlib/error.h"
+#include "wimlib/list.h"
+#include "wimlib/metadata.h"
+#include "wimlib/progress.h"
+#include "wimlib/resource.h"
+#include "wimlib/wim.h"
+#include "wimlib/write.h"
+
+struct swm_part_info {
+       struct list_head blob_list;
+       u64 size;
 };
 
-static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
-                     int write_flags)
+static void
+copy_part_info(struct swm_part_info *dst, struct swm_part_info *src)
 {
-       off_t lookup_table_offset = ftello(w->out_fp);
-       int ret;
-
-       DEBUG("Writing lookup table for SWM (offset %"PRIu64")",
-                       lookup_table_offset);
-
-       while (lte_chain_head != NULL) {
-               ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
-               if (ret != 0)
-                       return ret;
-               struct lookup_table_entry *prev = lte_chain_head;
-               lte_chain_head = lte_chain_head->next_lte_in_swm;
-               prev->next_lte_in_swm = NULL;
-       }
-       off_t xml_data_offset = ftello(w->out_fp);
-
-       if (lookup_table_offset == -1 || xml_data_offset == -1)
-               return WIMLIB_ERR_WRITE;
-       w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
-       w->hdr.lookup_table_res_entry.size =
-                               xml_data_offset - lookup_table_offset;
-       ret = finish_write(w, WIM_ALL_IMAGES,
-                          write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
-       if (ret != 0)
-               return ret;
-
-       ret = fclose(w->out_fp);
-       if (ret != 0)
-               ret = WIMLIB_ERR_WRITE;
-       w->out_fp = NULL;
-       return ret;
+       list_replace(&src->blob_list, &dst->blob_list);
+       dst->size = src->size;
 }
 
-static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
+struct swm_info {
+       struct swm_part_info *parts;
+       unsigned num_parts;
+       unsigned num_alloc_parts;
+       u64 total_bytes;
+       u64 max_part_size;
+};
+
+static int
+write_split_wim(WIMStruct *orig_wim, const tchar *swm_name,
+               struct swm_info *swm_info, int write_flags)
 {
-       struct split_args *args = (struct split_args*)__args;
-       WIMStruct *w = args->w;
+       size_t swm_name_len;
+       tchar *swm_name_buf;
+       const tchar *dot;
+       tchar *swm_suffix;
+       size_t swm_base_name_len;
+
+       union wimlib_progress_info progress;
+       unsigned part_number;
        int ret;
+       u8 guid[GUID_SIZE];
+
+       swm_name_len = tstrlen(swm_name);
+       swm_name_buf = alloca((swm_name_len + 20) * sizeof(tchar));
+       tstrcpy(swm_name_buf, swm_name);
+       dot = tstrchr(swm_name_buf, T('.'));
+       if (dot) {
+               swm_base_name_len = dot - swm_name_buf;
+               swm_suffix = alloca((tstrlen(dot) + 1) * sizeof(tchar));
+               tstrcpy(swm_suffix, dot);
+       } else {
+               swm_base_name_len = swm_name_len;
+               swm_suffix = alloca(1 * sizeof(tchar));
+               swm_suffix[0] = T('\0');
+       }
 
-       /* metadata resources were already written. */
-       if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
-               return 0;
+       progress.split.completed_bytes = 0;
+       progress.split.total_bytes = 0;
+       for (part_number = 1; part_number <= swm_info->num_parts; part_number++)
+               progress.split.total_bytes += swm_info->parts[part_number - 1].size;
+       progress.split.total_parts = swm_info->num_parts;
 
-       if (args->size_remaining < 0 ||
-                       (u64)args->size_remaining < lte->resource_entry.size) {
+       generate_guid(guid);
 
-               /* No space for this resource.  Finish the previous swm and
-                * start a new one. */
+       for (part_number = 1; part_number <= swm_info->num_parts; part_number++) {
+               int part_write_flags;
+               wimlib_progress_func_t progfunc;
 
-               ret = finish_swm(w, args->lte_chain_head, args->write_flags);
+               if (part_number != 1) {
+                       tsprintf(swm_name_buf + swm_base_name_len,
+                                T("%u%"TS), part_number, swm_suffix);
+               }
 
-               args->lte_chain_tail = NULL;
-               args->lte_chain_head = NULL;
+               progress.split.cur_part_number = part_number;
+               progress.split.part_name = swm_name_buf;
 
-               sprintf(args->swm_base_name + args->swm_base_name_len, "%d",
-                       ++args->part_number);
-               strcat(args->swm_base_name, args->swm_suffix);
+               ret = call_progress(orig_wim->progfunc,
+                                   WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
+                                   &progress,
+                                   orig_wim->progctx);
+               if (ret)
+                       return ret;
 
-               w->hdr.part_number = args->part_number;
+               part_write_flags = write_flags;
+               part_write_flags |= WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES;
+               if (part_number != 1)
+                       part_write_flags |= WIMLIB_WRITE_FLAG_NO_METADATA;
+
+               progfunc = orig_wim->progfunc;
+               orig_wim->progfunc = NULL;
+               ret = write_wim_part(orig_wim,
+                                    progress.split.part_name,
+                                    WIMLIB_ALL_IMAGES,
+                                    part_write_flags,
+                                    1,
+                                    part_number,
+                                    swm_info->num_parts,
+                                    &swm_info->parts[part_number - 1].blob_list,
+                                    guid);
+               orig_wim->progfunc = progfunc;
+               if (ret)
+                       return ret;
 
-               if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
-                       printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
-                              "%.0f%% done)\n",
-                              args->swm_base_name,
-                              args->total_bytes_written,
-                              args->total_bytes,
-                              (double)args->total_bytes_written /
-                                  (double)args->total_bytes * 100.0);
+               progress.split.completed_bytes += swm_info->parts[part_number - 1].size;
 
-               ret = begin_write(w, args->swm_base_name, args->write_flags);
-               if (ret != 0)
+               ret = call_progress(orig_wim->progfunc,
+                                   WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
+                                   &progress,
+                                   orig_wim->progctx);
+               if (ret)
                        return ret;
-               args->size_remaining = args->part_size;
        }
-       args->size_remaining -= lte->resource_entry.size;
-       args->total_bytes_written += lte->resource_entry.size;
-       if (args->lte_chain_tail)
-               args->lte_chain_tail->next_lte_in_swm = lte;
-       else
-               args->lte_chain_head = lte;
-       args->lte_chain_tail = lte;
-       return copy_resource(lte, w);
+       return 0;
 }
 
-/* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
- * size at most @part_size. */
-WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name,
-                          size_t part_size, int flags)
+static int
+add_blob_to_swm(struct blob_descriptor *blob, void *_swm_info)
 {
-       int ret;
-       WIMStruct *w;
-       int write_flags = 0;
-       size_t swm_name_len = strlen(swm_name);
-       size_t swm_base_name_len;
-       char name[swm_name_len + 20];
-       char *swm_suffix;
+       struct swm_info *swm_info = _swm_info;
+       u64 blob_stored_size;
 
-       struct lookup_table_entry *lte_chain_head = NULL;
-       struct lookup_table_entry *lte_chain_tail = NULL;
-       long size_remaining = part_size;
-       u64 total_bytes_written = 0;
-       u64 total_bytes;
+       if (blob->blob_location == BLOB_IN_WIM)
+               blob_stored_size = blob->rdesc->size_in_wim;
+       else
+               blob_stored_size = blob->size;
+
+       /* - Start first part if no parts have been started so far;
+        * - Start next part if adding this blob exceeds maximum part size,
+        *   UNLESS the blob is metadata or if no blobs at all have been added
+        *   to the current part.
+        */
+       if (swm_info->num_parts == 0 ||
+           ((swm_info->parts[swm_info->num_parts - 1].size +
+                       blob_stored_size >= swm_info->max_part_size)
+            && !(blob->is_metadata ||
+                 swm_info->parts[swm_info->num_parts - 1].size == 0)))
+       {
+               if (swm_info->num_parts == swm_info->num_alloc_parts) {
+                       struct swm_part_info *parts;
+                       size_t num_alloc_parts = swm_info->num_alloc_parts;
+
+                       num_alloc_parts += 8;
+                       parts = MALLOC(num_alloc_parts * sizeof(parts[0]));
+                       if (!parts)
+                               return WIMLIB_ERR_NOMEM;
+
+                       for (unsigned i = 0; i < swm_info->num_parts; i++)
+                               copy_part_info(&parts[i], &swm_info->parts[i]);
+
+                       FREE(swm_info->parts);
+                       swm_info->parts = parts;
+                       swm_info->num_alloc_parts = num_alloc_parts;
+               }
+               swm_info->num_parts++;
+               INIT_LIST_HEAD(&swm_info->parts[swm_info->num_parts - 1].blob_list);
+               swm_info->parts[swm_info->num_parts - 1].size = 0;
+       }
+       swm_info->parts[swm_info->num_parts - 1].size += blob_stored_size;
+       if (!blob->is_metadata) {
+               list_add_tail(&blob->write_blobs_list,
+                             &swm_info->parts[swm_info->num_parts - 1].blob_list);
+       }
+       swm_info->total_bytes += blob_stored_size;
+       return 0;
+}
 
-       ret = wimlib_open_wim(wimfile, flags, &w);
-       if (ret != 0)
-               return ret;
+/* API function documented in wimlib.h  */
+WIMLIBAPI int
+wimlib_split(WIMStruct *wim, const tchar *swm_name,
+            u64 part_size, int write_flags)
+{
+       struct swm_info swm_info;
+       unsigned i;
+       int ret;
 
-       total_bytes = wim_info_get_total_bytes(w->wim_info);
+       if (swm_name == NULL || swm_name[0] == T('\0') || part_size == 0)
+               return WIMLIB_ERR_INVALID_PARAM;
 
-       if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
-               write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
-       if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
-               write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
+       if (write_flags & ~WIMLIB_WRITE_MASK_PUBLIC)
+               return WIMLIB_ERR_INVALID_PARAM;
 
-       w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
-       w->hdr.boot_idx = 0;
-       randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
-       ret = begin_write(w, swm_name, write_flags);
-       if (ret != 0)
-               return ret;
+       if (!wim_has_metadata(wim))
+               return WIMLIB_ERR_METADATA_NOT_FOUND;
 
-       swm_suffix = strchr(swm_name, '.');
-       memcpy(name, swm_name, swm_name_len + 1);
-       if (swm_suffix) {
-               swm_base_name_len = swm_suffix - swm_name;
-       } else {
-               swm_base_name_len = swm_name_len;
-               name[sizeof(name) - 1] = '\0';
-               swm_suffix = &name[sizeof(name) - 1];
+       if (wim_has_solid_resources(wim)) {
+               ERROR("Splitting of WIM containing solid resources is not supported.\n"
+                     "        Export it in non-solid format first.");
+               return WIMLIB_ERR_UNSUPPORTED;
        }
 
-       if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
-               printf("Writing `%s' (%.2f %% done)\n",
-                       swm_name,
-                       (double)total_bytes_written /
-                               (double)total_bytes * 100.0);
-
-       w->write_metadata = true;
-       for (int i = 0; i < w->hdr.image_count; i++) {
-               struct lookup_table_entry *metadata_lte;
-
-               DEBUG("Writing metadata resource %d", i);
+       memset(&swm_info, 0, sizeof(swm_info));
+       swm_info.max_part_size = part_size;
 
-               metadata_lte = w->image_metadata[i].metadata_lte;
-               ret = copy_resource(metadata_lte, w);
-               if (ret != 0)
-                       return ret;
-               size_remaining -= metadata_lte->resource_entry.size;
-               total_bytes_written += metadata_lte->resource_entry.size;
-               if (lte_chain_tail)
-                       lte_chain_tail->next_lte_in_swm = metadata_lte;
-               else
-                       lte_chain_head = metadata_lte;
-               lte_chain_tail = metadata_lte;
+       for (i = 0; i < wim->hdr.image_count; i++) {
+               ret = add_blob_to_swm(wim->image_metadata[i]->metadata_blob,
+                                     &swm_info);
+               if (ret)
+                       goto out_free_swm_info;
        }
-       w->write_metadata = false;
-
-       struct split_args args = {
-               .w                 = w,
-               .swm_base_name     = name,
-               .swm_base_name_len = swm_base_name_len,
-               .swm_suffix        = swm_suffix,
-               .lte_chain_head    = lte_chain_head,
-               .lte_chain_tail    = lte_chain_tail,
-               .part_number       = 1,
-               .write_flags       = write_flags,
-               .size_remaining    = size_remaining,
-               .part_size         = part_size,
-               .total_bytes        = total_bytes,
-               .total_bytes_written = total_bytes_written,
-       };
-
-       ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
-       if (ret != 0)
-               return ret;
-
-       ret = finish_swm(w, args.lte_chain_head, write_flags);
-       if (ret != 0)
-               return ret;
-
-
-       /* The swms are all ready now, except the total_parts and part_number
-        * fields in their headers are wrong (we don't know the total parts
-        * until they are all written).  Fix them. */
-       int total_parts = args.part_number;
-       for (int i = 1; i <= total_parts; i++) {
-               const char *p;
-               if (i == 1) {
-                       p = swm_name;
-               } else {
-                       sprintf(name + swm_base_name_len, "%d", i);
-                       p = strcat(name, swm_suffix);
-               }
 
-               FILE *fp = fopen(p, "r+b");
-               if (!fp) {
-                       ERROR_WITH_ERRNO("Failed to open `%s'", p);
-                       return WIMLIB_ERR_OPEN;
-               }
-               u8 buf[4];
-               put_u16(&buf[0], i);
-               put_u16(&buf[2], total_parts);
-
-               if (fseek(fp, 40, SEEK_SET) != 0 ||
-                               fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
-                               || fclose(fp) != 0) {
-                       ERROR_WITH_ERRNO("Error overwriting header of `%s'",
-                                        name);
-                       return WIMLIB_ERR_WRITE;
-               }
-       }
-       if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
-               puts("Done!");
-       wimlib_free(w);
-       return 0;
+       ret = for_blob_in_table_sorted_by_sequential_order(wim->blob_table,
+                                                          add_blob_to_swm,
+                                                          &swm_info);
+       if (ret)
+               goto out_free_swm_info;
+
+       ret = write_split_wim(wim, swm_name, &swm_info, write_flags);
+out_free_swm_info:
+       FREE(swm_info.parts);
+       return ret;
 }