X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fsplit.c;h=df384f82d30fdd0bddc10083f5a17e66a3ed7da7;hb=12f851cd2fbd19c6b9d5358756d4b7c0a9944b92;hp=f22780bbae654b73eb7d6a3c6c5299c3c183eb14;hpb=4f9947253f6a08f4a922d3efe0880e3039b3eb5e;p=wimlib diff --git a/src/split.c b/src/split.c index f22780bb..df384f82 100644 --- a/src/split.c +++ b/src/split.c @@ -2,23 +2,25 @@ * split.c * * Split a WIM file into parts. - * + */ + +/* * 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 General Public License as published by the Free + * Software Foundation; either version 3 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 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 General Public License + * along with wimlib; if not, see http://www.gnu.org/licenses/. */ #include "wimlib_internal.h" @@ -26,13 +28,12 @@ #include "xml.h" #include "io.h" -struct args { +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; + struct list_head *lte_list; int part_number; int write_flags; long size_remaining; @@ -41,77 +42,70 @@ struct args { u64 total_bytes_written; }; -static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head, +static int finish_swm(WIMStruct *w, struct list_head *lte_list, int write_flags) { off_t lookup_table_offset = ftello(w->out_fp); int ret; + struct lookup_table_entry *lte; - DEBUG("Writing lookup table for SWM (offset %"PRIu64")\n", + 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); + list_for_each_entry(lte, lte_list, staging_list) { + ret = write_lookup_table_entry(lte, w->out_fp); if (ret != 0) return ret; - struct lookup_table_entry *prev = lte_chain_head; - lte_chain_head = prev->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 = + w->hdr.lookup_table_res_entry.size = xml_data_offset - lookup_table_offset; - ret = finish_write(w, WIM_ALL_IMAGES, write_flags, 0); - if (ret != 0) - return ret; - - ret = fclose(w->out_fp); - if (ret != 0) - ret = WIMLIB_ERR_WRITE; - w->out_fp = NULL; - return ret; + w->hdr.lookup_table_res_entry.original_size = + xml_data_offset - lookup_table_offset; + w->hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA; + return finish_write(w, WIM_ALL_IMAGES, + write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE); } static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args) { - struct args *args = (struct args*)__args; + struct split_args *args = (struct split_args*)__args; WIMStruct *w = args->w; - FILE *out_fp = w->out_fp; int ret; /* metadata resources were already written. */ if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) return 0; - if (args->size_remaining < 0 || + if (args->size_remaining < 0 || (u64)args->size_remaining < lte->resource_entry.size) { /* No space for this resource. Finish the previous swm and * start a new one. */ - ret = finish_swm(w, args->lte_chain_head, args->write_flags); - - args->lte_chain_tail = NULL; - args->lte_chain_head = NULL; + ret = finish_swm(w, args->lte_list, args->write_flags); + if (ret != 0) + return ret; + INIT_LIST_HEAD(args->lte_list); - sprintf(args->swm_base_name + args->swm_base_name_len, "%d", - ++args->part_number); - strcat(args->swm_base_name, args->swm_suffix); + sprintf(args->swm_base_name + args->swm_base_name_len, "%d%s", + ++args->part_number, args->swm_suffix); w->hdr.part_number = args->part_number; 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); + "%.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); ret = begin_write(w, args->swm_base_name, args->write_flags); if (ret != 0) @@ -120,17 +114,13 @@ static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args) } 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; + list_add(<e->staging_list, args->lte_list); return copy_resource(lte, w); } /* 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, +WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, size_t part_size, int flags) { int ret; @@ -141,8 +131,6 @@ WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, char name[swm_name_len + 20]; char *swm_suffix; - 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; @@ -176,37 +164,34 @@ WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, } if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS) - printf("Writing `%s' (%.2f %% done)\n", - swm_name, + printf("Writing `%s' (%.2f %% done)\n", + swm_name, (double)total_bytes_written / (double)total_bytes * 100.0); w->write_metadata = true; + LIST_HEAD(lte_list); for (int i = 0; i < w->hdr.image_count; i++) { - struct lookup_table_entry *metadata_lte; - metadata_lte = w->image_metadata[i].lookup_table_entry; + DEBUG("Writing metadata resource %d", i); + + 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; + list_add(&metadata_lte->staging_list, <e_list); } w->write_metadata = false; - struct args args = { + 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, + .lte_list = <e_list, .part_number = 1, .write_flags = write_flags, .size_remaining = size_remaining, @@ -219,7 +204,7 @@ WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, if (ret != 0) return ret; - ret = finish_swm(w, args.lte_chain_head, write_flags); + ret = finish_swm(w, <e_list, write_flags); if (ret != 0) return ret; @@ -239,22 +224,23 @@ WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, FILE *fp = fopen(p, "r+b"); if (!fp) { - ERROR("Failed to open `%s': %m\n", p); + ERROR_WITH_ERRNO("Failed to open `%s'", p); return WIMLIB_ERR_OPEN; } - char buf[4]; - put_u16(buf, i); - put_u16(buf + 2, total_parts); + u8 buf[4]; + put_u16(&buf[0], i); + put_u16(&buf[2], total_parts); - if (fseek(fp, 40, SEEK_SET) != 0 || + if (fseek(fp, 40, SEEK_SET) != 0 || fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf) || fclose(fp) != 0) { - ERROR("Error overwriting header of `%s': %m\n", name); + ERROR_WITH_ERRNO("Error overwriting header of `%s'", + name); return WIMLIB_ERR_WRITE; } } if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS) - printf("Done!\n"); + puts("Done!"); wimlib_free(w); return 0; }