diff options
author | Hung-Te Lin <hungte@chromium.org> | 2018-09-04 15:27:41 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-09-10 01:13:19 -0700 |
commit | f917d4d2324a9316603f50e8311639a6093180a3 (patch) | |
tree | 4d2efe8e5c7199096db78084b2e8dddae97a2f06 | |
parent | e0b83db84ae33e3b02c975c522e924f13d896d79 (diff) | |
download | vboot-f917d4d2324a9316603f50e8311639a6093180a3.tar.gz |
futility: cmd_update: Create and remove temporary files properly
When running on DUT we should create temporary files using system calls instead
of using hard-coded path and file name.
The new create_temp_file() will collect all temporary files and remove them
all when the remove_temp_files() is invoked (usually at end of program).
BUG=chromium:875551
TEST=make futil; tests/futility/run_test_scripts.sh $(pwd)/build/futility
BRANCH=None
Change-Id: I866dd8dfe7acbf8c5a586249ea2d19f33891672d
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1203334
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | futility/cmd_update.c | 69 |
1 files changed, 64 insertions, 5 deletions
diff --git a/futility/cmd_update.c b/futility/cmd_update.c index 9055ec4a..8392c79e 100644 --- a/futility/cmd_update.c +++ b/futility/cmd_update.c @@ -11,6 +11,7 @@ #include <getopt.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include "2rsa.h" #include "crossystem.h" @@ -122,6 +123,61 @@ struct updater_config { struct system_property system_properties[SYS_PROP_MAX]; }; +struct tempfile { + char *filepath; + struct tempfile *next; +}; + +static struct tempfile *tempfiles; + +/* + * Helper function to create a new temporary file. + * All files created will be removed by function remove_all_temp_files(). + * Returns the path of new file, or NULL on failure. + */ +static const char *create_temp_file() +{ + struct tempfile *new_temp; + char new_path[] = P_tmpdir "/fwupdater.XXXXXX"; + int fd; + + fd = mkstemp(new_path); + if (fd < 0) { + ERROR("Failed to create new temp file in %s", new_path); + return NULL; + } + close(fd); + new_temp = (struct tempfile *)malloc(sizeof(*new_temp)); + if (new_temp) + new_temp->filepath = strdup(new_path); + if (!new_temp || !new_temp->filepath) { + remove(new_path); + free(new_temp); + ERROR("Failed to allocate buffer for new temp file."); + return NULL; + } + DEBUG("Created new temporary file: %s.", new_path); + new_temp->next = tempfiles; + tempfiles = new_temp; + return new_temp->filepath; +} + +/* + * Helper function to remove all files created by create_temp_file(). + * This is intended to be called only once at end of program execution. + */ +static void remove_all_temp_files() +{ + while (tempfiles != NULL) { + struct tempfile *target = tempfiles; + DEBUG("Remove temporary file: %s.", target->filepath); + remove(target->filepath); + free(target->filepath); + tempfiles = target->next; + free(target); + } +} + /* * Strip a string (usually from shell execution output) by removing all the * trailing space characters (space, new line, tab, ... etc). @@ -555,9 +611,10 @@ static int emulate_system_image(const char *file_name, static int load_system_image(struct updater_config *cfg, struct firmware_image *image) { - /* TODO(hungte) replace by mkstemp */ - const char *tmp_file = "/tmp/.fwupdate.read"; + const char *tmp_file = create_temp_file(); + if (!tmp_file) + return -1; RETURN_ON_FAILURE(host_flashrom( FLASHROM_READ, tmp_file, image->programmer, 0, NULL)); return load_image(tmp_file, image); @@ -718,11 +775,13 @@ static int write_firmware(struct updater_config *cfg, const struct firmware_image *image, const char *section_name) { - /* TODO(hungte) replace by mkstemp */ - const char *tmp_file = "/tmp/.fwupdate.write"; + const char *tmp_file = create_temp_file(); const char *programmer = cfg->emulate ? image->emulation : image->programmer; + if (!tmp_file) + return -1; + if (cfg->emulate) { printf("%s: (emulation) %s %s from %s to %s.\n", __FUNCTION__, @@ -1642,8 +1701,8 @@ static int do_update(int argc, char *argv[]) printf(">> %s: Firmware updater %s.\n", errorcnt ? "FAILED": "DONE", errorcnt ? "stopped due to error" : "exited successfully"); - unload_updater_config(&cfg); + remove_all_temp_files(); return !!errorcnt; } |