diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-31 18:33:31 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-31 19:15:21 +0200 |
commit | c4a945dd254af75d334fdd881076c665184faa31 (patch) | |
tree | 8b071d87e291c032183725479981f9344034aff6 /lib/fstree/fstree_from_file.c | |
parent | c42d8e4ead7c20d29bf3996a6a87db0b348f8692 (diff) |
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstree/fstree_from_file.c')
-rw-r--r-- | lib/fstree/fstree_from_file.c | 58 |
1 files changed, 25 insertions, 33 deletions
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 <sys/sysmacros.h> #include <sys/types.h> +#include <unistd.h> #include <stdlib.h> #include <stdint.h> #include <string.h> @@ -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; |