From 3836f72fbfa5dfccaa93631d0d1f40fc1c4034a4 Mon Sep 17 00:00:00 2001 From: "Koushik K. Dutta" Date: Thu, 11 Mar 2010 22:17:43 -0800 Subject: [PATCH] delete script before running it, otherwise you have potential recovery loops --- extendedcommands.c | 40 ++++++++++++++++++++++------------------ mtdutils/mtdutils.h | 2 -- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/extendedcommands.c b/extendedcommands.c index f38ffd0..5185859 100644 --- a/extendedcommands.c +++ b/extendedcommands.c @@ -413,20 +413,8 @@ int extendedcommand_file_exists() return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info); } -int run_script(char* filename) +int run_script_from_buffer(char* script_data, int script_len, char* filename) { - struct stat file_info; - if (0 != stat(filename, &file_info)) { - printf("Error executing stat on file: %s\n", filename); - return 1; - } - - int script_len = file_info.st_size; - char* script_data = (char*)malloc(script_len); - FILE *file = fopen(filename, "rb"); - fread(script_data, script_len, 1, file); - fclose(file); - /* Parse the script. Note that the script and parse tree are never freed. */ const AmCommandList *commands = parseAmendScript(script_data, script_len); @@ -455,6 +443,25 @@ int run_script(char* filename) return 0; } +int run_script(char* filename, int delete_file) +{ + struct stat file_info; + if (0 != stat(filename, &file_info)) { + printf("Error executing stat on file: %s\n", filename); + return 1; + } + + int script_len = file_info.st_size; + char* script_data = (char*)malloc(script_len); + FILE *file = fopen(filename, "rb"); + fread(script_data, script_len, 1, file); + fclose(file); + if (delete_file) + remove(filename); + + return run_script_from_buffer(script_data, script_len, filename); +} + int run_and_remove_extendedcommand() { int i = 0; @@ -470,10 +477,7 @@ int run_and_remove_extendedcommand() ui_print("Timed out waiting for SD card... continuing anyways."); } - - int ret = run_script(EXTENDEDCOMMAND_SCRIPT); - remove(EXTENDEDCOMMAND_SCRIPT); - return ret; + return run_script(EXTENDEDCOMMAND_SCRIPT, 1); } int amend_main(int argc, char** argv) @@ -488,5 +492,5 @@ int amend_main(int argc, char** argv) if (register_update_commands(&ctx)) { LOGE("Can't install update commands\n"); } - return run_script(argv[1]); + return run_script(argv[1], 0); } diff --git a/mtdutils/mtdutils.h b/mtdutils/mtdutils.h index bd0ce17..528a5bb 100644 --- a/mtdutils/mtdutils.h +++ b/mtdutils/mtdutils.h @@ -52,6 +52,4 @@ off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */ off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos); int mtd_write_close(MtdWriteContext *); -ssize_t mtd_read_raw(MtdReadContext *ctx, char *data, size_t len); - #endif // MTDUTILS_H_