]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Make sure we cleanup image file on failure if we create it
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 24 Nov 2022 09:07:32 +0000 (10:07 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 29 Nov 2022 09:08:51 +0000 (10:08 +0100)
src/partition/repart.c

index e3d9e4f66108415596bae323fa3bcb072c3bbf33..30de655c2bf3102d897db5f435aab197d86346e3 100644 (file)
@@ -264,6 +264,7 @@ typedef struct Context {
         sd_id128_t seed;
 
         char *node;
+        bool node_is_our_file;
         int backing_fd;
 
         bool from_scratch;
@@ -472,7 +473,10 @@ static Context *context_free(Context *context) {
                 fdisk_unref_context(context->fdisk_context);
 
         safe_close(context->backing_fd);
-        free(context->node);
+        if (context->node_is_our_file)
+                unlink_and_free(context->node);
+        else
+                free(context->node);
 
         return mfree(context);
 }
@@ -6040,6 +6044,7 @@ static int find_root(Context *context) {
                                 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
 
                         context->node = TAKE_PTR(s);
+                        context->node_is_our_file = true;
                         context->backing_fd = TAKE_FD(fd);
                         return 0;
                 }
@@ -6481,6 +6486,8 @@ static int run(int argc, char *argv[]) {
 
         (void) context_dump(context, /*late=*/ true);
 
+        context->node = mfree(context->node);
+
         LIST_FOREACH(partitions, p, context->partitions)
                 p->split_path = mfree(p->split_path);