summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2024-09-26 08:27:13 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2024-09-26 08:27:13 +0200
commitcd1ec786238202b987a7624e763dadc725a46a67 (patch)
treee37ced7ec184d7708c00fd98a516a0ede7d2cded
parent8f9966c8ea3ea8a854941d041e7fcb9eb4f772fb (diff)
Fix prefix path for file globing
Reported-by: Sebastian Birunt <sebastian.birunt@arm.com> Suggested-by: Sebastian Birunt <sebastian.birunt@arm.com> Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--bin/gensquashfs/fstree_from_dir.c50
-rw-r--r--tests/gensquashfs/fstree_from_dir.c9
-rw-r--r--tests/gensquashfs/fstree_glob1.c12
3 files changed, 68 insertions, 3 deletions
diff --git a/bin/gensquashfs/fstree_from_dir.c b/bin/gensquashfs/fstree_from_dir.c
index 5b3f003..d91cda6 100644
--- a/bin/gensquashfs/fstree_from_dir.c
+++ b/bin/gensquashfs/fstree_from_dir.c
@@ -283,9 +283,10 @@ static void discard_node(tree_node_t *root, tree_node_t *n)
static int populate_dir(int dir_fd, fstree_t *fs, tree_node_t *root,
dev_t devstart, scan_node_callback cb,
- void *user, unsigned int flags)
+ void *user, unsigned int flags, const char *file_prefix)
{
char *extra = NULL;
+ char *prefix = NULL;
struct dirent *ent;
int ret, childfd;
struct stat sb;
@@ -379,6 +380,29 @@ static int populate_dir(int dir_fd, fstree_t *fs, tree_node_t *root,
}
extra[sb.st_size] = '\0';
+ } else if (S_ISREG(sb.st_mode)) {
+ const char *src;
+
+ src = ent->d_name;
+ while (*src == '/')
+ ++src;
+
+ /* reconstruct base path relative file path */
+ if (file_prefix == NULL) {
+ extra = strdup(src);
+ } else {
+ size_t fxlen = strlen(file_prefix) + 1;
+ size_t srclen = strlen(src) + 1;
+
+ extra = malloc(fxlen + srclen);
+ if (extra != NULL) {
+ memcpy(extra, file_prefix, fxlen);
+ memcpy(extra + fxlen, src, srclen);
+ extra[fxlen - 1] = '/';
+ } else {
+ goto fail;
+ }
+ }
}
if (!(flags & DIR_SCAN_KEEP_TIME))
@@ -421,10 +445,29 @@ static int populate_dir(int dir_fd, fstree_t *fs, tree_node_t *root,
goto fail;
}
+ if (file_prefix == NULL) {
+ prefix = strdup(n->name);
+ } else {
+ size_t fxlen = strlen(file_prefix) + 1;
+ size_t namelen = strlen(n->name) + 1;
+
+ prefix = calloc(fxlen + namelen, sizeof(char));
+ if (prefix != NULL) {
+ memcpy(prefix, file_prefix, fxlen);
+ memcpy(prefix + fxlen, n->name, namelen);
+ prefix[fxlen - 1] = '/';
+ } else {
+ goto fail;
+ }
+ }
+
if (populate_dir(childfd, fs, n, devstart,
- cb, user, flags)) {
+ cb, user, flags, prefix)) {
goto fail;
}
+
+ free(prefix);
+ prefix = NULL;
}
}
@@ -435,6 +478,7 @@ fail_rdlink:
fail:
closedir(dir);
free(extra);
+ free(prefix);
return -1;
}
@@ -481,7 +525,7 @@ int fstree_from_subdir(fstree_t *fs, tree_node_t *root,
return -1;
}
- return populate_dir(fd, fs, root, sb.st_dev, cb, user, flags);
+ return populate_dir(fd, fs, root, sb.st_dev, cb, user, flags, subdir);
}
int fstree_from_dir(fstree_t *fs, tree_node_t *root,
diff --git a/tests/gensquashfs/fstree_from_dir.c b/tests/gensquashfs/fstree_from_dir.c
index 5e73fa4..39dfd19 100644
--- a/tests/gensquashfs/fstree_from_dir.c
+++ b/tests/gensquashfs/fstree_from_dir.c
@@ -24,18 +24,21 @@ static void check_hierarchy(tree_node_t *root, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_a0");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dira/file_a0");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_a1");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dira/file_a1");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_a2");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dira/file_a2");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -55,18 +58,21 @@ static void check_hierarchy(tree_node_t *root, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_b0");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirb/file_b0");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_b1");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirb/file_b1");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_b2");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirb/file_b2");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -86,18 +92,21 @@ static void check_hierarchy(tree_node_t *root, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_c0");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirc/file_c0");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_c1");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirc/file_c1");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "file_c2");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "dirc/file_c2");
TEST_ASSERT(m->parent == n);
m = m->next;
diff --git a/tests/gensquashfs/fstree_glob1.c b/tests/gensquashfs/fstree_glob1.c
index fbcbf91..398104e 100644
--- a/tests/gensquashfs/fstree_glob1.c
+++ b/tests/gensquashfs/fstree_glob1.c
@@ -41,6 +41,7 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/file-size/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -60,12 +61,14 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu-g.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/format-acceptance/gnu-g.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/format-acceptance/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -85,6 +88,7 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/large-mtime/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -104,6 +108,7 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/long-paths/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -123,6 +128,7 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/negative-mtime/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -142,30 +148,35 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu-small.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/sparse-files/gnu-small.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/sparse-files/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "pax-gnu0-0.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/sparse-files/pax-gnu0-0.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "pax-gnu0-1.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/sparse-files/pax-gnu0-1.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "pax-gnu1-0.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/sparse-files/pax-gnu1-0.tar");
TEST_ASSERT(m->parent == n);
m = m->next;
@@ -185,6 +196,7 @@ static void check_hierarchy(tree_node_t *root, bool subdir, bool recursive)
TEST_NOT_NULL(m);
TEST_STR_EQUAL(m->name, "gnu.tar");
TEST_ASSERT(S_ISREG(m->mode));
+ TEST_STR_EQUAL(m->data.file.input_file, "../libtar/data/user-group-largenum/gnu.tar");
TEST_ASSERT(m->parent == n);
m = m->next;