From db1d4e6b9734691c3ec02045462b9ed9ce5d2777 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 24 Nov 2022 10:07:32 +0100 Subject: [PATCH] repart: Make sure we cleanup image file on failure if we create it --- src/partition/repart.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index e3d9e4f661..30de655c2b 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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); -- 2.25.1