X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fdelete_image.c;h=c95819e980d787ecd0d3996176f5e1688bf9f65a;hp=389d230917dd5d3e1c9c982e0ff902be7e17ecc8;hb=13c6ce3160fce7c40008d1d182325c8b42450d1e;hpb=9e2571b03cd9c71d11b3dad9ea5dcfa43f50deb4 diff --git a/src/delete_image.c b/src/delete_image.c index 389d2309..c95819e9 100644 --- a/src/delete_image.c +++ b/src/delete_image.c @@ -31,63 +31,63 @@ WIMLIBAPI int wimlib_delete_image(WIMStruct *w, 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; } - 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) + if (ret) return ret; } - DEBUG("Deleting image %d", image); + if (image == WIMLIB_ALL_IMAGES) { + last = w->hdr.image_count; + first = 1; + } else { + last = image; + first = 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; + for (image = last; image >= first; image--) { + DEBUG("Deleting image %d", image); - /* 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); + /* 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) + return ret; - /* 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)); + /* 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(w->image_metadata[image - 1], w->lookup_table); - /* Decrement the image count. */ - if (--w->hdr.image_count == 0) { - FREE(w->image_metadata); - w->image_metadata = NULL; - } + /* 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(w->image_metadata[0])); - /* 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--; + /* Decrement the image count. */ + --w->hdr.image_count; - w->current_image = WIMLIB_NO_IMAGE; + /* 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--; - /* Remove the image from the XML information. */ - xml_delete_image(&w->wim_info, image); + w->current_image = WIMLIB_NO_IMAGE; - w->deletion_occurred = 1; + /* Remove the image from the XML information. */ + xml_delete_image(&w->wim_info, image); + + w->deletion_occurred = 1; + } return 0; }