diff mbox series

[1/2] Add accessor to get script directory

Message ID 1507125704-2423-1-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series [1/2] Add accessor to get script directory | expand

Commit Message

Stefano Babic Oct. 4, 2017, 2:01 p.m. UTC
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(-)
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 821b6df..30a5ffe 100644
--- a/core/util.c
+++ b/core/util.c
@@ -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;
diff --git a/corelib/installer.c b/corelib/installer.c
index fffdff6..05a1f5a 100644
--- a/corelib/installer.c
+++ b/corelib/installer.c
@@ -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);
 			}
diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c
index e9cfd46..206f2d0 100644
--- a/handlers/lua_scripthandler.c
+++ b/handlers/lua_scripthandler.c
@@ -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 */
diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c
index 016c246..8bae91c 100644
--- a/handlers/shell_scripthandler.c
+++ b/handlers/shell_scripthandler.c
@@ -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)) {
diff --git a/include/util.h b/include/util.h
index 24ec127..3abc84b 100644
--- a/include/util.h
+++ b/include/util.h
@@ -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