From dfcb5d93c6fb3d5c1c933275622d938e83da6a70 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 10 Jun 2019 00:28:55 +0200 Subject: gensquashfs: do pushd/popd when needed instead of chdir This commit replaces the chdir to the input directory with pushd/popd when building the fstree and again when packing files. This simplifies handling of other file paths given on the command line that have to be accessed and are relative to the original working directories. Signed-off-by: David Oberhollenzer --- include/fstree.h | 5 ++++- lib/fstree/fstree_from_file.c | 23 +++++++++-------------- mkfs/block.c | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/fstree.h b/include/fstree.h index 33fa8b8..3c9e2af 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -222,7 +222,10 @@ void fstree_xattr_deduplicate(fstree_t *fs); Parses the file format accepted by gensquashfs and produce a file system tree from it. File input paths are interpreted as relative to the given root dir. If rootdir is NULL, use the path where the input file is as root - dir. The function actually chdirs into the root dir. + dir. + + This function tries to temporarily change the working directory, so if it + fails, the current working directory is undefined. On failure, an error report with filename and line number is written to stderr. diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index 5a87222..affa769 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -365,6 +365,7 @@ out_desc: int fstree_from_file(fstree_t *fs, const char *filename, const char *rootdir) { FILE *fp = fopen(filename, "rb"); + bool need_restore = false; size_t n, line_num = 0; const char *ptr; ssize_t ret; @@ -379,26 +380,16 @@ int fstree_from_file(fstree_t *fs, const char *filename, const char *rootdir) ptr = strrchr(filename, '/'); if (ptr != NULL) { - line = strndup(filename, ptr - filename); - if (line == NULL) { - perror("composing root path"); - return -1; - } - - if (chdir(line)) { - perror(line); + if (pushdn(filename, ptr - filename)) { free(line); return -1; } - - free(line); - line = NULL; + need_restore = true; } } else { - if (chdir(rootdir)) { - perror(rootdir); + if (pushd(rootdir)) return -1; - } + need_restore = true; } for (;;) { @@ -433,6 +424,10 @@ int fstree_from_file(fstree_t *fs, const char *filename, const char *rootdir) } fclose(fp); + + if (need_restore && popd() != 0) + return -1; + return 0; fail_line: free(line); diff --git a/mkfs/block.c b/mkfs/block.c index 545df53..de2c962 100644 --- a/mkfs/block.c +++ b/mkfs/block.c @@ -211,8 +211,25 @@ static int find_and_process_files(sqfs_info_t *info, tree_node_t *n, int write_data_to_image(sqfs_info_t *info) { + bool need_restore = false; + const char *ptr; int ret; + if (info->opt.packdir != NULL) { + if (pushd(info->opt.packdir)) + return -1; + need_restore = true; + } else { + ptr = strrchr(info->opt.infile, '/'); + + if (ptr != NULL) { + if (pushdn(info->opt.infile, ptr - info->opt.infile)) + return -1; + + need_restore = true; + } + } + info->block = malloc(info->super.block_size); if (info->block == NULL) { @@ -245,5 +262,9 @@ int write_data_to_image(sqfs_info_t *info) info->block = NULL; info->fragment = NULL; info->scratch = NULL; + + if (need_restore) + ret = popd(); + return ret; } -- cgit v1.2.3