aboutsummaryrefslogtreecommitdiff
path: root/unpack
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-05 01:03:16 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-05-05 01:56:50 +0200
commit949d1c2a5553b54fe6bc2b8c7aca10a892e595d7 (patch)
tree19f23bd62f8099a088926e45216546e813bec80f /unpack
parentda5656a8a696863e0d9941091c09c75b03a6070b (diff)
rdsquashfs: reorder unpack flags, add flag to produce listing
The listing command has been used successfully to do the following: - generate a prestine file system using gensquashfs - repeate multiple times: - generate a listing from the file system - unpack only the regular files from the file system - generate a new file system from the listing - run `diff` on the old and new filesystem and admire that they are identical - replace the old file system with the new one, since they are identical Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'unpack')
-rw-r--r--unpack/Makemodule.am2
-rw-r--r--unpack/describe.c73
-rw-r--r--unpack/rdsquashfs.c79
-rw-r--r--unpack/rdsquashfs.h2
-rw-r--r--unpack/restore_fstree.c31
5 files changed, 139 insertions, 48 deletions
diff --git a/unpack/Makemodule.am b/unpack/Makemodule.am
index 6a0eab0..d47cb5e 100644
--- a/unpack/Makemodule.am
+++ b/unpack/Makemodule.am
@@ -1,7 +1,7 @@
rdsquashfs_SOURCES = unpack/rdsquashfs.c unpack/tree_node_from_inode.c
rdsquashfs_SOURCES += unpack/rdsquashfs.h unpack/read_fstree.c
rdsquashfs_SOURCES += unpack/list_files.c unpack/extract_file.c
-rdsquashfs_SOURCES += unpack/restore_fstree.c
+rdsquashfs_SOURCES += unpack/restore_fstree.c unpack/describe.c
rdsquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a
if WITH_XZ
diff --git a/unpack/describe.c b/unpack/describe.c
new file mode 100644
index 0000000..94e7d22
--- /dev/null
+++ b/unpack/describe.c
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "rdsquashfs.h"
+
+#include <sys/sysmacros.h>
+
+static void print_name(tree_node_t *n)
+{
+ if (n->parent != NULL) {
+ print_name(n->parent);
+ fputc('/', stdout);
+ }
+
+ fputs(n->name, stdout);
+}
+
+static void print_perm(tree_node_t *n)
+{
+ printf(" 0%o %d %d", n->mode & (~S_IFMT), n->uid, n->gid);
+}
+
+static void print_simple(const char *type, tree_node_t *n, const char *extra)
+{
+ printf("%s ", type);
+ print_name(n);
+ print_perm(n);
+ if (extra != NULL)
+ printf(" %s", extra);
+ fputc('\n', stdout);
+}
+
+void describe_tree(tree_node_t *root, const char *unpack_root)
+{
+ tree_node_t *n;
+
+ switch (root->mode & S_IFMT) {
+ case S_IFSOCK:
+ print_simple("sock", root, NULL);
+ break;
+ case S_IFLNK:
+ print_simple("slink", root, root->data.slink_target);
+ break;
+ case S_IFIFO:
+ print_simple("pipe", root, NULL);
+ break;
+ case S_IFREG:
+ if (unpack_root != NULL) {
+ fputs("file ", stdout);
+ print_name(root);
+ print_perm(root);
+ printf(" %s", unpack_root);
+ print_name(root);
+ fputc('\n', stdout);
+ } else {
+ print_simple("file", root, NULL);
+ }
+ break;
+ case S_IFCHR:
+ case S_IFBLK: {
+ char buffer[32];
+ sprintf(buffer, "%c %d %d", S_ISCHR(root->mode) ? 'c' : 'b',
+ major(root->data.devno), minor(root->data.devno));
+ print_simple("nod", root, buffer);
+ break;
+ }
+ case S_IFDIR:
+ if (root->name[0] != '\0')
+ print_simple("dir", root, NULL);
+
+ for (n = root->data.dir->children; n != NULL; n = n->next)
+ describe_tree(n, unpack_root);
+ break;
+ }
+}
diff --git a/unpack/rdsquashfs.c b/unpack/rdsquashfs.c
index f09847d..a24434c 100644
--- a/unpack/rdsquashfs.c
+++ b/unpack/rdsquashfs.c
@@ -6,18 +6,20 @@ enum {
OP_LS,
OP_CAT,
OP_UNPACK,
+ OP_DESCRIBE,
};
static struct option long_opts[] = {
{ "list", required_argument, NULL, 'l' },
{ "cat", required_argument, NULL, 'c' },
- { "unpack-root", required_argument, NULL, 'u' },
- { "unpack-path", required_argument, NULL, 'p' },
+ { "unpack-root", required_argument, NULL, 'p' },
+ { "unpack-path", required_argument, NULL, 'u' },
{ "no-dev", no_argument, NULL, 'D' },
{ "no-sock", no_argument, NULL, 'S' },
{ "no-fifo", no_argument, NULL, 'F' },
{ "no-slink", no_argument, NULL, 'L' },
{ "no-empty-dir", no_argument, NULL, 'E' },
+ { "describe", no_argument, NULL, 'd' },
{ "chmod", no_argument, NULL, 'C' },
{ "chown", no_argument, NULL, 'O' },
{ "quiet", no_argument, NULL, 'q' },
@@ -25,7 +27,7 @@ static struct option long_opts[] = {
{ "version", no_argument, NULL, 'V' },
};
-static const char *short_opts = "l:c:u:p:DSFLCOEqhV";
+static const char *short_opts = "l:c:u:p:DSFLCOEdqhV";
static const char *help_string =
"Usage: %s [OPTIONS] <squashfs-file>\n"
@@ -33,27 +35,34 @@ static const char *help_string =
"View or extract the contents of a squashfs image.\n"
"\n"
"Possible options:\n"
-" --list, -l <path> Produce a directory listing for a given path in the\n"
-" squashfs image.\n"
-" --cat, -c <path> If the specified path is a regular file in the,\n"
-" image, dump its contents to stdout.\n"
-" --unpack-root <path> Unpack the contents of the filesystem into the\n"
-" specified path.\n"
-" --unpack-path <path> If specified, unpack this sub directory from the\n"
-" image instead of the filesystem root.\n"
-" --no-dev, -D Do not unpack device special files.\n"
-" --no-sock, -S Do not unpack socket files.\n"
-" --no-fifo, -F Do not unpack named pipes.\n"
-" --no-slink, -L Do not unpack symbolic links.\n"
-" --no-empty-dir, -E Do not unpack directories that would end up empty.\n"
-" --chmod, -C Change permission flags of unpacked files to those\n"
-" store in the squashfs image.\n"
-" --chown, -O Change ownership of unpacked files to the UID/GID\n"
-" set in the squashfs image.\n"
-" --quiet, -q Do not print out progress while unpacking.\n"
"\n"
-" --help, -h Print help text and exit.\n"
-" --version, -V Print version information and exit.\n"
+" --list, -l <path> Produce a directory listing for a given path in\n"
+" the squashfs image.\n"
+" --cat, -c <path> If the specified path is a regular file in the,\n"
+" image, dump its contents to stdout.\n"
+" --unpack-path, -u <path> Unpack this sub directory from the image. To\n"
+" unpack everything, simply specify /.\n"
+" --describe, -d Produce a file listing from the image.\n"
+"\n"
+" --unpack-root, -p <path> If used with --unpack-path, this is where the\n"
+" data unpacked to. If used with --describe, this\n"
+" is used as a prefix for the input path of\n"
+" regular files.\n"
+"\n"
+" --no-dev, -D Do not unpack device special files.\n"
+" --no-sock, -S Do not unpack socket files.\n"
+" --no-fifo, -F Do not unpack named pipes.\n"
+" --no-slink, -L Do not unpack symbolic links.\n"
+" --no-empty-dir, -E Do not unpack directories that would end up\n"
+" empty after applying the above rules.\n"
+" --chmod, -C Change permission flags of unpacked files to\n"
+" those store in the squashfs image.\n"
+" --chown, -O Change ownership of unpacked files to the\n"
+" UID/GID set in the squashfs image.\n"
+" --quiet, -q Do not print out progress while unpacking.\n"
+"\n"
+" --help, -h Print help text and exit.\n"
+" --version, -V Print version information and exit.\n"
"\n";
extern const char *__progname;
@@ -192,15 +201,18 @@ int main(int argc, char **argv)
op = OP_CAT;
cmdpath = get_path(cmdpath, optarg);
break;
+ case 'd':
+ op = OP_DESCRIBE;
+ break;
case 'l':
op = OP_LS;
cmdpath = get_path(cmdpath, optarg);
break;
- case 'u':
- op = OP_UNPACK;
+ case 'p':
unpack_root = optarg;
break;
- case 'p':
+ case 'u':
+ op = OP_UNPACK;
cmdpath = get_path(cmdpath, optarg);
break;
case 'q':
@@ -303,14 +315,10 @@ int main(int argc, char **argv)
goto out_fs;
break;
case OP_UNPACK:
- if (cmdpath == NULL) {
- n = fs.root;
- } else {
- n = find_node(fs.root, cmdpath);
- if (n == NULL) {
- perror(cmdpath);
- goto out_fs;
- }
+ n = find_node(fs.root, cmdpath);
+ if (n == NULL) {
+ perror(cmdpath);
+ goto out_fs;
}
if (load_fragment_table(&info, &super))
@@ -322,6 +330,9 @@ int main(int argc, char **argv)
if (restore_fstree(unpack_root, n, &info))
goto out_fs;
break;
+ case OP_DESCRIBE:
+ describe_tree(fs.root, unpack_root);
+ break;
}
status = EXIT_SUCCESS;
diff --git a/unpack/rdsquashfs.h b/unpack/rdsquashfs.h
index a9d5bdf..f1cd131 100644
--- a/unpack/rdsquashfs.h
+++ b/unpack/rdsquashfs.h
@@ -55,4 +55,6 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd);
int restore_fstree(const char *rootdir, tree_node_t *root,
unsqfs_info_t *info);
+void describe_tree(tree_node_t *root, const char *unpack_root);
+
#endif /* RDSQUASHFS_H */
diff --git a/unpack/restore_fstree.c b/unpack/restore_fstree.c
index c1320ea..3e7f500 100644
--- a/unpack/restore_fstree.c
+++ b/unpack/restore_fstree.c
@@ -117,27 +117,32 @@ int restore_fstree(const char *rootdir, tree_node_t *root, unsqfs_info_t *info)
if (mkdir_p(rootdir))
return -1;
- dirfd = open(rootdir, O_RDONLY | O_DIRECTORY);
- if (dirfd < 0) {
- perror(rootdir);
- return -1;
+ if (rootdir == NULL) {
+ dirfd = AT_FDCWD;
+ } else {
+ dirfd = open(rootdir, O_RDONLY | O_DIRECTORY);
+ if (dirfd < 0) {
+ perror(rootdir);
+ return -1;
+ }
}
if (S_ISDIR(root->mode)) {
for (n = root->data.dir->children; n != NULL; n = n->next) {
- if (create_node(dirfd, n, info)) {
- close(dirfd);
- return -1;
- }
+ if (create_node(dirfd, n, info))
+ goto fail;
}
return 0;
}
- if (create_node(dirfd, root, info)) {
- close(dirfd);
- return -1;
- }
+ if (create_node(dirfd, root, info))
+ goto fail;
- close(dirfd);
+ if (dirfd != AT_FDCWD)
+ close(dirfd);
return 0;
+fail:
+ if (dirfd != AT_FDCWD)
+ close(dirfd);
+ return -1;
}