From 942c3dd30a15a5462ad42f309fcd4fa80cfcad3a Mon Sep 17 00:00:00 2001 From: Michael Olbrich Date: Fri, 23 Feb 2024 16:54:11 +0100 Subject: [PATCH] genimage: improve tmppath handling Right now cleanup is partially broken: If the tmppath exists, then its content is removed at the end as expected. However, if the tmppath is missing then it is created but nothing is removed. Change this to remove the directory including its content at the end when the directory was created but stick to removing just the content if the directory existed before genimage was called. Fixes: #237 Signed-off-by: Michael Olbrich --- genimage.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/genimage.c b/genimage.c index 5bd235b..d626dec 100644 --- a/genimage.c +++ b/genimage.c @@ -576,7 +576,11 @@ const char *mountpath(const struct image *image) return mp->mountpath; } -static int tmppath_generated; +static enum { + TMPPATH_NONE, + TMPPATH_CHECKED, + TMPPATH_CREATED +} tmppath_generated; static void check_tmp_path(void) { @@ -595,6 +599,7 @@ static void check_tmp_path(void) ret = systemp(NULL, "mkdir -p \"%s\"", tmppath()); if (ret) exit(1); + tmppath_generated = TMPPATH_CREATED; return; } @@ -607,14 +612,22 @@ static void check_tmp_path(void) exit(1); } } - tmppath_generated = 1; + tmppath_generated = TMPPATH_CHECKED; closedir(dir); } static void cleanup(void) { - if (tmppath_generated) - systemp(NULL, "rm -rf \"%s\"/*", tmppath()); + switch (tmppath_generated) { + case TMPPATH_CREATED: + systemp(NULL, "rm -rf \"%s/\"", tmppath()); + break; + case TMPPATH_CHECKED: + systemp(NULL, "rm -rf \"%s\"/*", tmppath()); + break; + default: + break; + } } static cfg_opt_t top_opts[] = {