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