summaryrefslogtreecommitdiff
path: root/lib/sqfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-05 17:38:08 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-05 17:45:59 +0200
commit88f75857702bcc6a2a423570912140c125ec518a (patch)
treea1ec54e2520e076395208872d36606f917c8ca34 /lib/sqfs
parent3a340b12eb9b7ed86a47391345cb836fa662b2d9 (diff)
cleanup: unify all the code that reads squashfs images
This commit creates a new data structure called 'sqfs_reader_t' that takes care of all the repetetive tasks like opening the file, reading the super block, creating the compressor, deserializing an fstree and creating a data reader. This in turn makes it possible to remove all the duplicate code from rdsquashfs and sqfs2tar. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r--lib/sqfs/sqfs_reader.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/sqfs/sqfs_reader.c b/lib/sqfs/sqfs_reader.c
new file mode 100644
index 0000000..85cf97a
--- /dev/null
+++ b/lib/sqfs/sqfs_reader.c
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * sqfs_reader.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "highlevel.h"
+
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int sqfs_reader_open(sqfs_reader_t *rd, const char *filename, int rdtree_flags)
+{
+ memset(rd, 0, sizeof(*rd));
+
+ rd->sqfsfd = open(filename, O_RDONLY);
+ if (rd->sqfsfd < 0) {
+ perror(filename);
+ return -1;
+ }
+
+ if (sqfs_super_read(&rd->super, rd->sqfsfd))
+ goto fail_fd;
+
+ if (!compressor_exists(rd->super.compression_id)) {
+ fprintf(stderr, "%s: unknown compressor used.\n", filename);
+ goto fail_fd;
+ }
+
+ rd->cmp = compressor_create(rd->super.compression_id, false,
+ rd->super.block_size, NULL);
+ if (rd->cmp == NULL)
+ goto fail_fd;
+
+ if (rd->super.flags & SQFS_FLAG_COMPRESSOR_OPTIONS) {
+ if (rd->cmp->read_options(rd->cmp, rd->sqfsfd))
+ goto fail_cmp;
+ }
+
+ if (deserialize_fstree(&rd->fs, &rd->super, rd->cmp, rd->sqfsfd,
+ rdtree_flags)) {
+ goto fail_cmp;
+ }
+
+ fstree_gen_file_list(&rd->fs);
+
+ rd->data = data_reader_create(rd->sqfsfd, &rd->super, rd->cmp);
+ if (rd->data == NULL)
+ goto fail_fs;
+
+ return 0;
+fail_fs:
+ fstree_cleanup(&rd->fs);
+fail_cmp:
+ rd->cmp->destroy(rd->cmp);
+fail_fd:
+ close(rd->sqfsfd);
+ memset(rd, 0, sizeof(*rd));
+ return -1;
+}
+
+void sqfs_reader_close(sqfs_reader_t *rd)
+{
+ data_reader_destroy(rd->data);
+ fstree_cleanup(&rd->fs);
+ rd->cmp->destroy(rd->cmp);
+ close(rd->sqfsfd);
+ memset(rd, 0, sizeof(*rd));
+}