Message ID | 20170908134547.9170-2-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: > Adapt the usages of former #define'd TMPDIR to > make use of get_tmpdir() instead. > > Signed-off-by: Christian Storm <christian.storm@siemens.com> > --- > core/cpio_utils.c | 1 + > core/swupdate.c | 16 ++++++++++++---- > corelib/installer.c | 11 +++++++++-- > corelib/stream_interface.c | 3 ++- > handlers/archive_handler.c | 9 ++++++++- > handlers/boot_handler.c | 1 + > handlers/raw_handler.c | 2 ++ > handlers/shell_scripthandler.c | 10 ++++++---- > include/globals.h | 8 ++++---- > 9 files changed, 45 insertions(+), 16 deletions(-) > > diff --git a/core/cpio_utils.c b/core/cpio_utils.c > index c3df86f..e8651a3 100644 > --- a/core/cpio_utils.c > +++ b/core/cpio_utils.c > @@ -342,6 +342,7 @@ off_t extract_sw_description(int fd, const char *descfile, off_t start) > char output_file[64]; > uint32_t checksum; > int fdout; > + const char* TMPDIR = get_tmpdir(); > > if (extract_cpio_header(fd, &fdh, &offset)) { > ERROR("CPIO Header wrong\n"); > diff --git a/core/swupdate.c b/core/swupdate.c > index 1f0ba0d..3057000 100644 > --- a/core/swupdate.c > +++ b/core/swupdate.c > @@ -305,7 +305,9 @@ static int install_from_file(char *fname, int check) > pos); > #endif > > - ret = parse(&swcfg, TMPDIR SW_DESCRIPTION_FILENAME); > + char* swdescfilename = alloca(strlen(get_tmpdir())+strlen(SW_DESCRIPTION_FILENAME)+1); > + sprintf(swdescfilename, "%s%s", get_tmpdir(), SW_DESCRIPTION_FILENAME); > + ret = parse(&swcfg, swdescfilename); > if (ret) { > ERROR("failed to parse " SW_DESCRIPTION_FILENAME "!\n"); > exit(1); > @@ -394,6 +396,12 @@ static int parse_image_selector(const char *selector, struct swupdate_cfg *sw) > return 0; > } > > +static void create_directory(const char* path) { > + char* dpath = alloca(strlen(get_tmpdir())+strlen(path)+1); > + sprintf(dpath, "%s%s", get_tmpdir(), path); > + mkdir(dpath, 0777); > +} > + > static void swupdate_init(struct swupdate_cfg *sw) > { > /* Initialize internal tree to store configuration */ > @@ -407,9 +415,9 @@ static void swupdate_init(struct swupdate_cfg *sw) > > > /* Create directories for scripts */ > - mkdir(SCRIPTS_DIR, 0777); > - mkdir(DATASRC_DIR, 0777); > - mkdir(DATADST_DIR, 0777); > + create_directory(SCRIPTS_DIR_SUFFIX); > + create_directory(DATASRC_DIR_SUFFIX); > + create_directory(DATADST_DIR_SUFFIX); > > #ifdef CONFIG_MTD > mtd_init(); > diff --git a/corelib/installer.c b/corelib/installer.c > index ad0e0f1..8a188a1 100644 > --- a/corelib/installer.c > +++ b/corelib/installer.c > @@ -86,6 +86,7 @@ int check_if_required(struct imglist *list, struct filehdr *pfdh, > int skip = SKIP_FILE; > struct img_type *img; > int img_skip = 0; > + const char* TMPDIR = get_tmpdir(); > > /* > * Check that not more as one image wnat to be streamed > @@ -204,7 +205,9 @@ static int update_bootloader_env(void) > int ret = 0; > > TRACE("Updating bootloader environment"); > - ret = bootloader_apply_list((char *)BOOT_SCRIPT); > + char* bootscript = alloca(strlen(get_tmpdir())+strlen(BOOT_SCRIPT_SUFFIX)+1); > + sprintf(bootscript, "%s%s", get_tmpdir(), BOOT_SCRIPT_SUFFIX); > + ret = bootloader_apply_list(bootscript); > if (ret < 0) > ERROR("Error updating bootloader environment"); > > @@ -250,6 +253,7 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile) > char filename[64]; > struct filehdr fdh; > struct stat buf; > + const char* TMPDIR = get_tmpdir(); > > /* Extract all scripts, preinstall scripts must be run now */ > if (fromfile) { > @@ -268,7 +272,9 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile) > } > > /* Update u-boot environment */ > - ret = prepare_boot_script(sw, BOOT_SCRIPT); > + char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1); > + sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX); > + ret = prepare_boot_script(sw, bootscript); > if (ret) { > return ret; > } > @@ -377,6 +383,7 @@ void cleanup_files(struct swupdate_cfg *software) { > struct img_type *img; > struct dict_entry *bootvar; > struct hw_type *hw; > + const char* TMPDIR = get_tmpdir(); > > LIST_FOREACH(img, &software->images, next) { > if (img->fname[0]) { > diff --git a/corelib/stream_interface.c b/corelib/stream_interface.c > index 30e99ce..fb56c96 100644 > --- a/corelib/stream_interface.c > +++ b/corelib/stream_interface.c > @@ -89,6 +89,7 @@ static int extract_file_to_tmp(int fd, const char *fname, unsigned long *poffs) > struct filehdr fdh; > int fdout; > uint32_t checksum; > + const char* TMPDIR = get_tmpdir(); > > if (extract_cpio_header(fd, &fdh, poffs)) { > return -1; > @@ -135,7 +136,7 @@ static int extract_files(int fd, struct swupdate_cfg *software) > int fdout; > struct img_type *img, *part; > char output_file[MAX_IMAGE_FNAME]; > - > + const char* TMPDIR = get_tmpdir(); > > /* preset the info about the install parts */ > > diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c > index 0568a41..f86d1f2 100644 > --- a/handlers/archive_handler.c > +++ b/handlers/archive_handler.c > @@ -34,7 +34,7 @@ > #include "handler.h" > #include "util.h" > > -#define FIFO TMPDIR "archivfifo" > +#define FIFO_FILE_NAME "archivfifo" > > /* Just to turn on during development */ > static int debug = 0; > @@ -103,6 +103,8 @@ extract(void *p) > * Enabling bzip2 is more expensive because the libbz2 library > * isn't very well factored. > */ > + char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); > + sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); > if ((r = archive_read_open_filename(a, FIFO, 4096))) { > ERROR("archive_read_open_filename(): %s %d\n", > archive_error_string(a), r); > @@ -154,6 +156,9 @@ static int install_archive_image(struct img_type *img, > void *status; > int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; > > + char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); > + sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); > + > pthread_attr_init(&attr); > pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); > > @@ -182,6 +187,8 @@ static int install_archive_image(struct img_type *img, > } > } > > + char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); > + sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); > unlink(FIFO); > ret = mkfifo(FIFO, 0666); > if (ret) { > diff --git a/handlers/boot_handler.c b/handlers/boot_handler.c > index f2d4aaf..41f87f7 100644 > --- a/handlers/boot_handler.c > +++ b/handlers/boot_handler.c > @@ -44,6 +44,7 @@ static int install_boot_environment(struct img_type *img, > char filename[64]; > struct stat statbuf; > > + const char* TMPDIR = get_tmpdir(); > if (snprintf(filename, sizeof(filename), "%s%s", TMPDIR, > img->fname) >= (int)sizeof(filename)) { > ERROR("Path too long: %s%s", TMPDIR, img->fname); > diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c > index 4b567fb..91e9bb7 100644 > --- a/handlers/raw_handler.c > +++ b/handlers/raw_handler.c > @@ -59,6 +59,8 @@ static int install_raw_file(struct img_type *img, > int fdout; > int ret = 0; > int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; > + char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); > + sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); > > if (strlen(img->path) == 0) { > ERROR("Missing path attribute"); > diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c > index adda3ad..9095a5f 100644 > --- a/handlers/shell_scripthandler.c > +++ b/handlers/shell_scripthandler.c > @@ -40,17 +40,19 @@ static void shell_postinstall_handler(void); > static int execute_shell_script(struct img_type *img, const char *fnname) > { > int ret; > - char shellscript[MAX_IMAGE_FNAME + > - strlen(SCRIPTS_DIR) + strlen("postinst") + 2]; > + const char* TMPDIR = get_tmpdir(); > + > + char shellscript[MAX_IMAGE_FNAME + strlen(TMPDIR) + > + strlen(SCRIPTS_DIR_SUFFIX) + strlen("postinst") + 2]; > > snprintf(shellscript, sizeof(shellscript), > - "%s%s", TMPDIR, img->fname); > + "%s%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX, 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", TMPDIR, img->fname, fnname); > + "%s%s%s %s", TMPDIR, SCRIPTS_DIR_SUFFIX, img->fname, fnname); > > ret = system(shellscript); > if (WIFEXITED(ret)) { > diff --git a/include/globals.h b/include/globals.h > index 9e0c2b9..6e87839 100644 > --- a/include/globals.h > +++ b/include/globals.h > @@ -34,10 +34,10 @@ > #define MAX_SEEK_STRING_SIZE 32 > > /* These are fixed path to temporary files */ > -#define SCRIPTS_DIR TMPDIR "scripts/" > -#define DATASRC_DIR TMPDIR "datasrc/" > -#define DATADST_DIR TMPDIR "datadst/" > -#define BOOT_SCRIPT TMPDIR "boot-script" > +#define SCRIPTS_DIR_SUFFIX "scripts/" > +#define DATASRC_DIR_SUFFIX "datasrc/" > +#define DATADST_DIR_SUFFIX "datadst/" > +#define BOOT_SCRIPT_SUFFIX "boot-script" > > #endif > > Applied to -master, thanks ! Best regards, Stefano Babic
diff --git a/core/cpio_utils.c b/core/cpio_utils.c index c3df86f..e8651a3 100644 --- a/core/cpio_utils.c +++ b/core/cpio_utils.c @@ -342,6 +342,7 @@ off_t extract_sw_description(int fd, const char *descfile, off_t start) char output_file[64]; uint32_t checksum; int fdout; + const char* TMPDIR = get_tmpdir(); if (extract_cpio_header(fd, &fdh, &offset)) { ERROR("CPIO Header wrong\n"); diff --git a/core/swupdate.c b/core/swupdate.c index 1f0ba0d..3057000 100644 --- a/core/swupdate.c +++ b/core/swupdate.c @@ -305,7 +305,9 @@ static int install_from_file(char *fname, int check) pos); #endif - ret = parse(&swcfg, TMPDIR SW_DESCRIPTION_FILENAME); + char* swdescfilename = alloca(strlen(get_tmpdir())+strlen(SW_DESCRIPTION_FILENAME)+1); + sprintf(swdescfilename, "%s%s", get_tmpdir(), SW_DESCRIPTION_FILENAME); + ret = parse(&swcfg, swdescfilename); if (ret) { ERROR("failed to parse " SW_DESCRIPTION_FILENAME "!\n"); exit(1); @@ -394,6 +396,12 @@ static int parse_image_selector(const char *selector, struct swupdate_cfg *sw) return 0; } +static void create_directory(const char* path) { + char* dpath = alloca(strlen(get_tmpdir())+strlen(path)+1); + sprintf(dpath, "%s%s", get_tmpdir(), path); + mkdir(dpath, 0777); +} + static void swupdate_init(struct swupdate_cfg *sw) { /* Initialize internal tree to store configuration */ @@ -407,9 +415,9 @@ static void swupdate_init(struct swupdate_cfg *sw) /* Create directories for scripts */ - mkdir(SCRIPTS_DIR, 0777); - mkdir(DATASRC_DIR, 0777); - mkdir(DATADST_DIR, 0777); + create_directory(SCRIPTS_DIR_SUFFIX); + create_directory(DATASRC_DIR_SUFFIX); + create_directory(DATADST_DIR_SUFFIX); #ifdef CONFIG_MTD mtd_init(); diff --git a/corelib/installer.c b/corelib/installer.c index ad0e0f1..8a188a1 100644 --- a/corelib/installer.c +++ b/corelib/installer.c @@ -86,6 +86,7 @@ int check_if_required(struct imglist *list, struct filehdr *pfdh, int skip = SKIP_FILE; struct img_type *img; int img_skip = 0; + const char* TMPDIR = get_tmpdir(); /* * Check that not more as one image wnat to be streamed @@ -204,7 +205,9 @@ static int update_bootloader_env(void) int ret = 0; TRACE("Updating bootloader environment"); - ret = bootloader_apply_list((char *)BOOT_SCRIPT); + char* bootscript = alloca(strlen(get_tmpdir())+strlen(BOOT_SCRIPT_SUFFIX)+1); + sprintf(bootscript, "%s%s", get_tmpdir(), BOOT_SCRIPT_SUFFIX); + ret = bootloader_apply_list(bootscript); if (ret < 0) ERROR("Error updating bootloader environment"); @@ -250,6 +253,7 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile) char filename[64]; struct filehdr fdh; struct stat buf; + const char* TMPDIR = get_tmpdir(); /* Extract all scripts, preinstall scripts must be run now */ if (fromfile) { @@ -268,7 +272,9 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile) } /* Update u-boot environment */ - ret = prepare_boot_script(sw, BOOT_SCRIPT); + char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1); + sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX); + ret = prepare_boot_script(sw, bootscript); if (ret) { return ret; } @@ -377,6 +383,7 @@ void cleanup_files(struct swupdate_cfg *software) { struct img_type *img; struct dict_entry *bootvar; struct hw_type *hw; + const char* TMPDIR = get_tmpdir(); LIST_FOREACH(img, &software->images, next) { if (img->fname[0]) { diff --git a/corelib/stream_interface.c b/corelib/stream_interface.c index 30e99ce..fb56c96 100644 --- a/corelib/stream_interface.c +++ b/corelib/stream_interface.c @@ -89,6 +89,7 @@ static int extract_file_to_tmp(int fd, const char *fname, unsigned long *poffs) struct filehdr fdh; int fdout; uint32_t checksum; + const char* TMPDIR = get_tmpdir(); if (extract_cpio_header(fd, &fdh, poffs)) { return -1; @@ -135,7 +136,7 @@ static int extract_files(int fd, struct swupdate_cfg *software) int fdout; struct img_type *img, *part; char output_file[MAX_IMAGE_FNAME]; - + const char* TMPDIR = get_tmpdir(); /* preset the info about the install parts */ diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c index 0568a41..f86d1f2 100644 --- a/handlers/archive_handler.c +++ b/handlers/archive_handler.c @@ -34,7 +34,7 @@ #include "handler.h" #include "util.h" -#define FIFO TMPDIR "archivfifo" +#define FIFO_FILE_NAME "archivfifo" /* Just to turn on during development */ static int debug = 0; @@ -103,6 +103,8 @@ extract(void *p) * Enabling bzip2 is more expensive because the libbz2 library * isn't very well factored. */ + char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); + sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); if ((r = archive_read_open_filename(a, FIFO, 4096))) { ERROR("archive_read_open_filename(): %s %d\n", archive_error_string(a), r); @@ -154,6 +156,9 @@ static int install_archive_image(struct img_type *img, void *status; int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; + char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); + sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); + pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); @@ -182,6 +187,8 @@ static int install_archive_image(struct img_type *img, } } + char* FIFO = alloca(strlen(get_tmpdir())+strlen(FIFO_FILE_NAME)+1); + sprintf(FIFO, "%s%s", get_tmpdir(), FIFO_FILE_NAME); unlink(FIFO); ret = mkfifo(FIFO, 0666); if (ret) { diff --git a/handlers/boot_handler.c b/handlers/boot_handler.c index f2d4aaf..41f87f7 100644 --- a/handlers/boot_handler.c +++ b/handlers/boot_handler.c @@ -44,6 +44,7 @@ static int install_boot_environment(struct img_type *img, char filename[64]; struct stat statbuf; + const char* TMPDIR = get_tmpdir(); if (snprintf(filename, sizeof(filename), "%s%s", TMPDIR, img->fname) >= (int)sizeof(filename)) { ERROR("Path too long: %s%s", TMPDIR, img->fname); diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c index 4b567fb..91e9bb7 100644 --- a/handlers/raw_handler.c +++ b/handlers/raw_handler.c @@ -59,6 +59,8 @@ static int install_raw_file(struct img_type *img, int fdout; int ret = 0; int use_mount = (strlen(img->device) && strlen(img->filesystem)) ? 1 : 0; + char* DATADST_DIR = alloca(strlen(get_tmpdir())+strlen(DATADST_DIR_SUFFIX)+1); + sprintf(DATADST_DIR, "%s%s", get_tmpdir(), DATADST_DIR_SUFFIX); if (strlen(img->path) == 0) { ERROR("Missing path attribute"); diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c index adda3ad..9095a5f 100644 --- a/handlers/shell_scripthandler.c +++ b/handlers/shell_scripthandler.c @@ -40,17 +40,19 @@ static void shell_postinstall_handler(void); static int execute_shell_script(struct img_type *img, const char *fnname) { int ret; - char shellscript[MAX_IMAGE_FNAME + - strlen(SCRIPTS_DIR) + strlen("postinst") + 2]; + const char* TMPDIR = get_tmpdir(); + + char shellscript[MAX_IMAGE_FNAME + strlen(TMPDIR) + + strlen(SCRIPTS_DIR_SUFFIX) + strlen("postinst") + 2]; snprintf(shellscript, sizeof(shellscript), - "%s%s", TMPDIR, img->fname); + "%s%s%s", TMPDIR, SCRIPTS_DIR_SUFFIX, 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", TMPDIR, img->fname, fnname); + "%s%s%s %s", TMPDIR, SCRIPTS_DIR_SUFFIX, img->fname, fnname); ret = system(shellscript); if (WIFEXITED(ret)) { diff --git a/include/globals.h b/include/globals.h index 9e0c2b9..6e87839 100644 --- a/include/globals.h +++ b/include/globals.h @@ -34,10 +34,10 @@ #define MAX_SEEK_STRING_SIZE 32 /* These are fixed path to temporary files */ -#define SCRIPTS_DIR TMPDIR "scripts/" -#define DATASRC_DIR TMPDIR "datasrc/" -#define DATADST_DIR TMPDIR "datadst/" -#define BOOT_SCRIPT TMPDIR "boot-script" +#define SCRIPTS_DIR_SUFFIX "scripts/" +#define DATASRC_DIR_SUFFIX "datasrc/" +#define DATADST_DIR_SUFFIX "datadst/" +#define BOOT_SCRIPT_SUFFIX "boot-script" #endif
Adapt the usages of former #define'd TMPDIR to make use of get_tmpdir() instead. Signed-off-by: Christian Storm <christian.storm@siemens.com> --- core/cpio_utils.c | 1 + core/swupdate.c | 16 ++++++++++++---- corelib/installer.c | 11 +++++++++-- corelib/stream_interface.c | 3 ++- handlers/archive_handler.c | 9 ++++++++- handlers/boot_handler.c | 1 + handlers/raw_handler.c | 2 ++ handlers/shell_scripthandler.c | 10 ++++++---- include/globals.h | 8 ++++---- 9 files changed, 45 insertions(+), 16 deletions(-)