X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fdelete_image.c;h=2ad5a27a8f0d4e95962968dc90852e6e5631f190;hp=49babb9ae359ef981d792112506ba39ac17ecc13;hb=439beeac4b861e7a95169d7d4397acddb440e73a;hpb=10a87017a0a82d34ed3981e1f5e586b5b8613e3f diff --git a/src/delete_image.c b/src/delete_image.c index 49babb9a..2ad5a27a 100644 --- a/src/delete_image.c +++ b/src/delete_image.c @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 2012, 2013 Biggers + * Copyright (C) 2012, 2013 Eric Biggers * * This file is part of wimlib, a library for working with WIM files. * @@ -21,72 +21,70 @@ * along with wimlib; if not, see http://www.gnu.org/licenses/. */ -#include "wimlib_internal.h" -#include "xml.h" -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif -/* - * Deletes an image from the WIM. - */ -WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image) +#include "wimlib.h" +#include "wimlib/error.h" +#include "wimlib/metadata.h" +#include "wimlib/util.h" +#include "wimlib/wim.h" +#include "wimlib/xml.h" + +/* API function documented in wimlib.h */ +WIMLIBAPI int +wimlib_delete_image(WIMStruct *wim, int image) { - int i; int ret; + int first, last; - if (w->hdr.total_parts != 1) { - ERROR("Deleting an image from a split WIM is not supported."); - return WIMLIB_ERR_SPLIT_UNSUPPORTED; - } + ret = can_delete_from_wim(wim); + if (ret) + return ret; if (image == WIMLIB_ALL_IMAGES) { - for (i = w->hdr.image_count; i >= 1; i--) { - ret = wimlib_delete_image(w, i); - if (ret != 0) - return ret; - } - return 0; - } - - if (!w->all_images_verified) { - ret = wim_run_full_verifications(w); - if (ret != 0) - return ret; + last = wim->hdr.image_count; + first = 1; + } else { + last = image; + first = image; } - DEBUG("Deleting image %d", image); + for (image = last; image >= first; image--) { + DEBUG("Deleting image %d", image); - /* Even if the dentry tree is not allocated, we must select it (and - * therefore allocate it) so that we can decrement the reference counts - * in the lookup table. */ - ret = select_wim_image(w, image); - if (ret != 0) - return ret; + /* Even if the dentry tree is not allocated, we must select it (and + * therefore allocate it) so that we can decrement the reference counts + * in the lookup table. */ + ret = select_wim_image(wim, image); + if (ret) + return ret; - /* Free the dentry tree, any lookup table entries that have their refcnt - * decremented to 0, and the security data. */ - destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table); + /* Unless the image metadata is shared by another WIMStruct, free the + * dentry tree, any lookup table entries that have their refcnt + * decremented to 0, and the security data. */ + put_image_metadata(wim->image_metadata[image - 1], wim->lookup_table); - /* Get rid of the empty slot in the image metadata array. */ - memmove(&w->image_metadata[image - 1], &w->image_metadata[image], - (w->hdr.image_count - image) * sizeof(struct wim_image_metadata)); + /* Get rid of the empty slot in the image metadata array. */ + for (int i = image - 1; i < wim->hdr.image_count - 1; i++) + wim->image_metadata[i] = wim->image_metadata[i + 1]; - /* Decrement the image count. */ - if (--w->hdr.image_count == 0) { - FREE(w->image_metadata); - w->image_metadata = NULL; - } + /* Decrement the image count. */ + --wim->hdr.image_count; - /* Fix the boot index. */ - if (w->hdr.boot_idx == image) - w->hdr.boot_idx = 0; - else if (w->hdr.boot_idx > image) - w->hdr.boot_idx--; + /* Fix the boot index. */ + if (wim->hdr.boot_idx == image) + wim->hdr.boot_idx = 0; + else if (wim->hdr.boot_idx > image) + wim->hdr.boot_idx--; - w->current_image = WIMLIB_NO_IMAGE; + wim->current_image = WIMLIB_NO_IMAGE; - /* Remove the image from the XML information. */ - xml_delete_image(&w->wim_info, image); + /* Remove the image from the XML information. */ + xml_delete_image(&wim->wim_info, image); - w->deletion_occurred = 1; + wim->deletion_occurred = 1; + } return 0; }