diff mbox series

[1/3] tmpdir: introduce get_tmpdir() respecting TMPDIR

Message ID 20170908134547.9170-1-christian.storm@siemens.com
State Accepted
Headers show
Series [1/3] tmpdir: introduce get_tmpdir() respecting TMPDIR | expand

Commit Message

Storm, Christian Sept. 8, 2017, 1:45 p.m. UTC
Introduce a get_tmpdir() method that is to be used instead
of the #define'd TMPDIR hard-coded to /tmp so that SWUpdate
respects the TMPDIR environment variable or, if it is unset,
falls back to /tmp as sane default and current behavior.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/util.c    | 23 +++++++++++++++++++++++
 include/util.h |  2 +-
 2 files changed, 24 insertions(+), 1 deletion(-)

Comments

Stefano Babic Sept. 12, 2017, 9:29 a.m. UTC | #1
On 08/09/2017 15:45, Christian Storm wrote:
> Introduce a get_tmpdir() method that is to be used instead
> of the #define'd TMPDIR hard-coded to /tmp so that SWUpdate
> respects the TMPDIR environment variable or, if it is unset,
> falls back to /tmp as sane default and current behavior.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  core/util.c    | 23 +++++++++++++++++++++++
>  include/util.h |  2 +-
>  2 files changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/core/util.c b/core/util.c
> index b714f29..fe2a657 100644
> --- a/core/util.c
> +++ b/core/util.c
> @@ -52,6 +52,29 @@ char *sdup(const char *str) {
>  	return p;
>  }
>  
> +static char* TMPDIR = NULL;
> +
> +const char* get_tmpdir(void)
> +{
> +	if (TMPDIR != NULL) {
> +		return TMPDIR;
> +	}
> +
> +	char *env_tmpdir = getenv("TMPDIR");
> +	if (env_tmpdir == NULL) {
> +		TMPDIR = (char*)"/tmp/";
> +		return TMPDIR;
> +	}
> +
> +	if (env_tmpdir[strlen(env_tmpdir)] == '/') {
> +		TMPDIR = env_tmpdir;
> +		return TMPDIR;
> +	}
> +
> +	asprintf(&TMPDIR, "%s/", env_tmpdir);
> +	return TMPDIR;
> +}
> +
>  static int countargc(char *args, char **argv)
>  {
>  	int count = 0;
> diff --git a/include/util.h b/include/util.h
> index 70a0acc..9fb7860 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -112,7 +112,6 @@ typedef void (*notifier) (RECOVERY_STATUS status, int level, const char *msg);
>  
>  #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
>  
> -#define TMPDIR		"/tmp/"
>  
>  #define LG_16 4
>  #define FROM_HEX(f) from_ascii (f, sizeof f, LG_16)
> @@ -196,5 +195,6 @@ static inline int decompress_image(int __attribute__ ((__unused__))infile,
>  }
>  #endif
>  
> +const char* get_tmpdir(void);
>  
>  #endif
> 
Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index b714f29..fe2a657 100644
--- a/core/util.c
+++ b/core/util.c
@@ -52,6 +52,29 @@  char *sdup(const char *str) {
 	return p;
 }
 
+static char* TMPDIR = NULL;
+
+const char* get_tmpdir(void)
+{
+	if (TMPDIR != NULL) {
+		return TMPDIR;
+	}
+
+	char *env_tmpdir = getenv("TMPDIR");
+	if (env_tmpdir == NULL) {
+		TMPDIR = (char*)"/tmp/";
+		return TMPDIR;
+	}
+
+	if (env_tmpdir[strlen(env_tmpdir)] == '/') {
+		TMPDIR = env_tmpdir;
+		return TMPDIR;
+	}
+
+	asprintf(&TMPDIR, "%s/", env_tmpdir);
+	return TMPDIR;
+}
+
 static int countargc(char *args, char **argv)
 {
 	int count = 0;
diff --git a/include/util.h b/include/util.h
index 70a0acc..9fb7860 100644
--- a/include/util.h
+++ b/include/util.h
@@ -112,7 +112,6 @@  typedef void (*notifier) (RECOVERY_STATUS status, int level, const char *msg);
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
-#define TMPDIR		"/tmp/"
 
 #define LG_16 4
 #define FROM_HEX(f) from_ascii (f, sizeof f, LG_16)
@@ -196,5 +195,6 @@  static inline int decompress_image(int __attribute__ ((__unused__))infile,
 }
 #endif
 
+const char* get_tmpdir(void);
 
 #endif