From c4a945dd254af75d334fdd881076c665184faa31 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 31 May 2019 18:33:31 +0200 Subject: gensquashfs: chdir into directory where the input file lies This makes path handling a lot easier. We no longer need to paste paths together to get input file relative paths, since we are in the same directory. Signed-off-by: David Oberhollenzer --- lib/fstree/fstree_from_file.c | 58 +++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index aa09780..c2fac7a 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -163,37 +164,16 @@ static int add_file(fstree_t *fs, const char *filename, size_t line_num, const char *path, uint16_t mode, uint32_t uid, uint32_t gid, const char *extra) { - char *infile = NULL; tree_node_t *node; - const char *ptr; struct stat sb; - int ret; - - ptr = strrchr(filename, '/'); - - if (extra == NULL || *extra == '\0') { - if (ptr == NULL) { - extra = path; - } else { - ret = asprintf(&infile, "%.*s/%s", - (int)(filename - ptr), filename, path); - if (ret < 0) - goto fail_asprintf; - } - } else if (*extra != '/' && ptr != NULL) { - ret = asprintf(&infile, "%.*s/%s", - (int)(filename - ptr), filename, extra); - if (ret < 0) - goto fail_asprintf; - } - if (infile != NULL) - extra = infile; + if (extra == NULL || *extra == '\0') + extra = path; if (stat(extra, &sb) != 0) { fprintf(stderr, "%s: %zu: stat %s: %s\n", filename, line_num, extra, strerror(errno)); - goto fail; + return -1; } node = fstree_add_file(fs, path, mode, uid, gid, sb.st_size, extra); @@ -201,18 +181,10 @@ static int add_file(fstree_t *fs, const char *filename, size_t line_num, if (node == NULL) { fprintf(stderr, "%s: %zu: adding %s as %s: %s\n", filename, line_num, extra, path, strerror(errno)); - goto fail; + return -1; } - free(infile); return 0; -fail: - free(infile); - return -1; -fail_asprintf: - fprintf(stderr, "%s: %zu: pasting together file path: %s\n", - filename, line_num, strerror(errno)); - return -1; } static const struct { @@ -394,6 +366,7 @@ int fstree_from_file(fstree_t *fs, const char *filename) { FILE *fp = fopen(filename, "rb"); size_t n, line_num = 0; + const char *ptr; ssize_t ret; char *line; @@ -402,6 +375,25 @@ int fstree_from_file(fstree_t *fs, const char *filename) return -1; } + 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); + free(line); + return -1; + } + + free(line); + line = NULL; + } + for (;;) { line = NULL; n = 0; -- cgit v1.2.3