From 27a6409a4acfa3ab413dc3ff013ad761f6bf5e95 Mon Sep 17 00:00:00 2001 From: Stefan Haberland Date: Wed, 20 Nov 2019 15:30:12 +0100 Subject: [PATCH] zipl: clean up temporary bootmap file in case of error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit zipl creates a temp bootmap file to keep the original file in case of an error. Delete this temp file in case of an error. Signed-off-by: Stefan Haberland Reviewed-by: Philipp Rudo Signed-off-by: Jan Höppner --- zipl/include/misc.h | 1 + zipl/src/bootmap.c | 6 +++--- zipl/src/misc.c | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/zipl/include/misc.h b/zipl/include/misc.h index 5a349a7b..f222d01e 100644 --- a/zipl/include/misc.h +++ b/zipl/include/misc.h @@ -46,6 +46,7 @@ char* misc_make_path(char* dirname, char* filename); int misc_temp_dev(dev_t dev, int blockdev, char** devno); int misc_temp_dev_from_file(char* file, char** devno); void misc_free_temp_dev(char* device); +void misc_free_temp_file(char *filename); int misc_check_writable_directory(const char* directory); int misc_check_readable_file(const char* filename); int misc_check_writable_device(const char* devno, int blockdev, int chardev); diff --git a/zipl/src/bootmap.c b/zipl/src/bootmap.c index f5cb37c6..fd54a5c6 100644 --- a/zipl/src/bootmap.c +++ b/zipl/src/bootmap.c @@ -1286,9 +1286,7 @@ bootmap_create(struct job_data *job, disk_blockptr_t *program_table, break; } if (dry_run) { - if (remove(filename) == -1) - fprintf(stderr, "Warning: could not remove temporary " - "file %s!\n", filename); + misc_free_temp_file(filename); } else if (job->id != job_dump_partition) { /* Rename to final bootmap name */ mapname = misc_make_path(job->target.bootmap_dir, @@ -1315,6 +1313,8 @@ out_disk_free_info: disk_free_info(info); out_close_fd: close(fd); + if (job->id != job_dump_partition) + misc_free_temp_file(filename); out_free_filename: free(filename); return -1; diff --git a/zipl/src/misc.c b/zipl/src/misc.c index 057c9a0b..dff5c218 100644 --- a/zipl/src/misc.c +++ b/zipl/src/misc.c @@ -366,6 +366,16 @@ misc_free_temp_dev(char* device) free(device); } +/* Delete temporary bootmap file */ +void +misc_free_temp_file(char *filename) +{ + if (remove(filename)) { + fprintf(stderr, + "Warning: Could not remove temporary file %s: %s", + filename, strerror(errno)); + } +} /* Write COUNT bytes from memory at location DATA to the file identified by * file descriptor FD. Return 0 when all bytes were successfully written,