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 --- lib/fstree/fstree_from_file.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'lib') 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); -- cgit v1.2.3