@@ -53,6 +53,7 @@ char *sdup(const char *str) {
}
static char* TMPDIR = NULL;
+static char* TMPDIRSCRIPT = NULL;
const char* get_tmpdir(void)
{
@@ -77,6 +78,18 @@ const char* get_tmpdir(void)
return TMPDIR;
}
+const char* get_tmpdirscripts(void)
+{
+ if (TMPDIRSCRIPT != NULL) {
+ return TMPDIRSCRIPT;
+ }
+
+ if (asprintf(&TMPDIRSCRIPT, "%s%s", get_tmpdir(), SCRIPTS_DIR_SUFFIX) == -1) {
+ TMPDIRSCRIPT = (char*)"/tmp/" SCRIPTS_DIR_SUFFIX;
+ }
+ return TMPDIRSCRIPT;
+}
+
static int countargc(char *args, char **argv)
{
int count = 0;
@@ -257,14 +257,10 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile)
/* Extract all scripts, preinstall scripts must be run now */
if (fromfile) {
- char* TMPDIR_SCRIPTS = alloca(strlen(TMPDIR)+strlen(SCRIPTS_DIR_SUFFIX)+1);
- if (sprintf(TMPDIR_SCRIPTS, "%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX) < 0) {
- ERROR("preparing script extraction path failed!");
- return -1;
- }
- ret = extract_script(fdsw, &sw->scripts, TMPDIR_SCRIPTS);
+ const char* tmpdir_scripts = get_tmpdirscripts();
+ ret = extract_script(fdsw, &sw->scripts, tmpdir_scripts);
if (ret) {
- ERROR("extracting script to %s failed", TMPDIR_SCRIPTS);
+ ERROR("extracting script to %s failed", tmpdir_scripts);
return ret;
}
}
@@ -421,7 +417,7 @@ void cleanup_files(struct swupdate_cfg *software) {
}
LIST_FOREACH(img, &software->scripts, next) {
if (img->fname[0]) {
- if (snprintf(fn, sizeof(fn), "%s%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX,
+ if (snprintf(fn, sizeof(fn), "%s%s", get_tmpdirscripts(),
img->fname) >= (int)sizeof(fn)) {
ERROR("Path too long: %s%s", TMPDIR, img->fname);
}
@@ -46,9 +46,8 @@ static int start_lua_script(struct img_type *img, void *data)
const char *output;
script_fn scriptfn;
lua_State *L = luaL_newstate(); /* opens Lua */
- const char* TMPDIR = get_tmpdir();
- char filename[MAX_IMAGE_FNAME + strlen(TMPDIR) +
- strlen(SCRIPTS_DIR_SUFFIX) + 2];
+ const char* tmp = get_tmpdirscripts();
+ char filename[MAX_IMAGE_FNAME + strlen(tmp) + 2];
if (!data)
return -1;
@@ -68,7 +67,7 @@ static int start_lua_script(struct img_type *img, void *data)
}
snprintf(filename, sizeof(filename),
- "%s%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX, img->fname);
+ "%s%s", tmp, img->fname);
TRACE("Calling Lua %s", filename);
luaL_openlibs(L); /* opens the standard libraries */
@@ -40,19 +40,19 @@ static void shell_postinstall_handler(void);
static int execute_shell_script(struct img_type *img, const char *fnname)
{
int ret;
- const char* TMPDIR = get_tmpdir();
+ const char* tmp = get_tmpdirscripts();
- char shellscript[MAX_IMAGE_FNAME + strlen(TMPDIR) +
- strlen(SCRIPTS_DIR_SUFFIX) + strlen("postinst") + 2];
+ char shellscript[MAX_IMAGE_FNAME + strlen(tmp) +
+ strlen("postinst") + 2];
snprintf(shellscript, sizeof(shellscript),
- "%s%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX, img->fname);
+ "%s%s", tmp, img->fname);
if (chmod(shellscript, S_IRUSR | S_IWUSR | S_IXUSR)) {
ERROR("Execution bit cannot be set for %s", shellscript);
return -1;
}
snprintf(shellscript, sizeof(shellscript),
- "%s%s%s %s", TMPDIR, SCRIPTS_DIR_SUFFIX, img->fname, fnname);
+ "%s%s %s", tmp, img->fname, fnname);
ret = system(shellscript);
if (WIFEXITED(ret)) {
@@ -195,5 +195,6 @@ static inline int decompress_image(int __attribute__ ((__unused__))infile,
#endif
const char* get_tmpdir(void);
+const char* get_tmpdirscripts(void);
#endif
Scripts are stored in a separate directory. The path to the directory is allocated each time it is accessed, but is is fixed for the whole time SWUpdate is running. Factorize calls and use a static instance for the path. Signed-off-by: Stefano Babic <sbabic@denx.de> --- core/util.c | 13 +++++++++++++ corelib/installer.c | 12 ++++-------- handlers/lua_scripthandler.c | 7 +++---- handlers/shell_scripthandler.c | 10 +++++----- include/util.h | 1 + 5 files changed, 26 insertions(+), 17 deletions(-)