]> wimlib.net Git - wimlib/blobdiff - src/delete_image.c
system compression: add several more exclusion patterns
[wimlib] / src / delete_image.c
index ecd80478f7cf73d62461c53c5fb00b4294db7f0a..8844d6b31f7a3f90744d0dd983757cc866aa7e44 100644 (file)
@@ -3,90 +3,95 @@
  */
 
 /*
- * Copyright (C) 2012 Eric Biggers
+ * Copyright (C) 2012, 2013, 2014 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 "xml.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <string.h>
 
-/*
- * Deletes an image from the WIM.
- */
-WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
+#include "wimlib.h"
+#include "wimlib/metadata.h"
+#include "wimlib/wim.h"
+#include "wimlib/xml.h"
+
+/* Internal method for single-image deletion.  This doesn't set the
+ * image_deletion_occurred' flag on the WIMStruct.  */
+int
+delete_wim_image(WIMStruct *wim, int image)
 {
-       int i;
        int ret;
 
-       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)
-                       return ret;
-       }
-
-       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)
+       /* Load the metadata for the image to be deleted.  This is necessary
+        * because blobs referenced by files in the image need to have their
+        * reference counts decremented.  */
+       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);
+       /* Release the reference to the image metadata and decrement reference
+        * counts on the blobs referenced by files in the image.  */
+       put_image_metadata(wim->image_metadata[image - 1], wim->blob_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 image_metadata));
+       /* Remove the empty slot from the image metadata array.  */
+       memmove(&wim->image_metadata[image - 1], &wim->image_metadata[image],
+               (wim->hdr.image_count - image) *
+                       sizeof(wim->image_metadata[0]));
 
        /* Decrement the image count. */
-       if (--w->hdr.image_count == 0) {
-               FREE(w->image_metadata);
-               w->image_metadata = NULL;
-       }
+       --wim->hdr.image_count;
+
+       /* Remove the image from the XML information. */
+       xml_delete_image(&wim->wim_info, 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--;
+       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;
+       /* The image is no longer valid.  */
+       wim->current_image = WIMLIB_NO_IMAGE;
+       return 0;
+}
 
-       /* Remove the image from the XML information. */
-       xml_delete_image(&w->wim_info, image);
+/* API function documented in wimlib.h  */
+WIMLIBAPI int
+wimlib_delete_image(WIMStruct *wim, int image)
+{
+       int ret;
+       int first, last;
+
+       if (image == WIMLIB_ALL_IMAGES) {
+               /* Deleting all images  */
+               last = wim->hdr.image_count;
+               first = 1;
+       } else {
+               /* Deleting one image  */
+               last = image;
+               first = image;
+       }
 
-       w->deletion_occurred = true;
+       for (image = last; image >= first; image--) {
+               ret = delete_wim_image(wim, image);
+               if (ret)
+                       return ret;
+               wim->image_deletion_occurred = 1;
+       }
        return 0;
 }