aboutsummaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-11-07 16:04:40 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-11-07 16:12:47 +0100
commit411f659fe0140bacbd56f8503cda269816d4a887 (patch)
tree1b1f97a2964d550292790d949471eff96776d11c /lib/common
parent73e853f9660072abf0ae68cbb5d9753ac6e9034a (diff)
Cleanup: libcommon: try to split up some of the larger files
The sqsf_writer is moved to a separate header, as well as the compressor related stuff. The statistics function is moved into the writer code, as this is the only place that actually uses it. The writer code itself is split up into a hand full of file in their own subdirectory. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/Makemodule.am11
-rw-r--r--lib/common/comp_lzo.c2
-rw-r--r--lib/common/comp_opt.c2
-rw-r--r--lib/common/compress.c5
-rw-r--r--lib/common/statistics.c59
-rw-r--r--lib/common/writer/cleanup.c40
-rw-r--r--lib/common/writer/finish.c176
-rw-r--r--lib/common/writer/init.c (renamed from lib/common/writer.c)149
-rw-r--r--lib/common/writer/serialize_fstree.c (renamed from lib/common/serialize_fstree.c)0
9 files changed, 233 insertions, 211 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am
index e191e29..dd0a0d0 100644
--- a/lib/common/Makemodule.am
+++ b/lib/common/Makemodule.am
@@ -1,12 +1,15 @@
-libcommon_a_SOURCES = lib/common/serialize_fstree.c lib/common/statistics.c
-libcommon_a_SOURCES += lib/common/inode_stat.c lib/common/hardlink.c
+libcommon_a_SOURCES = lib/common/inode_stat.c lib/common/hardlink.c
libcommon_a_SOURCES += lib/common/print_version.c lib/common/data_reader_dump.c
libcommon_a_SOURCES += lib/common/compress.c lib/common/comp_opt.c
libcommon_a_SOURCES += lib/common/data_writer.c include/common.h
libcommon_a_SOURCES += lib/common/get_path.c lib/common/data_writer_ostream.c
-libcommon_a_SOURCES += lib/common/writer.c lib/common/perror.c
+libcommon_a_SOURCES += lib/common/perror.c
libcommon_a_SOURCES += lib/common/mkdir_p.c lib/common/parse_size.c
-libcommon_a_SOURCES += lib/common/print_size.c
+libcommon_a_SOURCES += lib/common/print_size.c include/simple_writer.h
+libcommon_a_SOURCES += include/compress_cli.h
+libcommon_a_SOURCES += lib/common/writer/init.c lib/common/writer/cleanup.c
+libcommon_a_SOURCES += lib/common/writer/serialize_fstree.c
+libcommon_a_SOURCES += lib/common/writer/finish.c
libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS)
if WITH_LZO
diff --git a/lib/common/comp_lzo.c b/lib/common/comp_lzo.c
index 3452647..21dc7b2 100644
--- a/lib/common/comp_lzo.c
+++ b/lib/common/comp_lzo.c
@@ -5,7 +5,7 @@
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
-#include "common.h"
+#include "compress_cli.h"
#include <stdbool.h>
#include <stdlib.h>
diff --git a/lib/common/comp_opt.c b/lib/common/comp_opt.c
index f77ac20..a0d4cf7 100644
--- a/lib/common/comp_opt.c
+++ b/lib/common/comp_opt.c
@@ -4,11 +4,13 @@
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
+#include "config.h"
#include "common.h"
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
+#include <stdio.h>
#include <ctype.h>
typedef struct {
diff --git a/lib/common/compress.c b/lib/common/compress.c
index 9a66095..b11efbd 100644
--- a/lib/common/compress.c
+++ b/lib/common/compress.c
@@ -4,8 +4,11 @@
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
-#include "common.h"
+#include "config.h"
+#include "compress_cli.h"
+
#include <assert.h>
+#include <stdio.h>
static int cmp_ids[] = {
SQFS_COMP_XZ,
diff --git a/lib/common/statistics.c b/lib/common/statistics.c
deleted file mode 100644
index 82e545d..0000000
--- a/lib/common/statistics.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * statistics.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "common.h"
-
-#include <stdio.h>
-
-void sqfs_print_statistics(const sqfs_super_t *super,
- const sqfs_block_processor_t *blk,
- const sqfs_block_writer_t *wr)
-{
- const sqfs_block_processor_stats_t *proc_stats;
- sqfs_u64 bytes_written, blocks_written;
- char read_sz[32], written_sz[32];
- size_t ratio;
-
- proc_stats = sqfs_block_processor_get_stats(blk);
- blocks_written = wr->get_block_count(wr);
-
- bytes_written = super->inode_table_start - sizeof(*super);
-
- if (proc_stats->input_bytes_read > 0) {
- ratio = (100 * bytes_written) / proc_stats->input_bytes_read;
- } else {
- ratio = 100;
- }
-
- print_size(proc_stats->input_bytes_read, read_sz, false);
- print_size(bytes_written, written_sz, false);
-
- fputs("---------------------------------------------------\n", stdout);
- printf("Data bytes read: %s\n", read_sz);
- printf("Data bytes written: %s\n", written_sz);
- printf("Data compression ratio: " PRI_SZ "%%\n", ratio);
- fputc('\n', stdout);
-
- printf("Data blocks written: " PRI_U64 "\n", blocks_written);
- printf("Out of which where fragment blocks: " PRI_U64 "\n",
- proc_stats->frag_block_count);
-
- printf("Duplicate blocks omitted: " PRI_U64 "\n",
- proc_stats->data_block_count + proc_stats->frag_block_count -
- blocks_written);
-
- printf("Sparse blocks omitted: " PRI_U64 "\n",
- proc_stats->sparse_block_count);
- fputc('\n', stdout);
-
- printf("Fragments actually written: " PRI_U64 "\n",
- proc_stats->actual_frag_count);
- printf("Duplicated fragments omitted: " PRI_U64 "\n",
- proc_stats->total_frag_count - proc_stats->actual_frag_count);
- printf("Total number of inodes: %u\n", super->inode_count);
- printf("Number of unique group/user IDs: %u\n", super->id_count);
- fputc('\n', stdout);
-}
diff --git a/lib/common/writer/cleanup.c b/lib/common/writer/cleanup.c
new file mode 100644
index 0000000..16e846b
--- /dev/null
+++ b/lib/common/writer/cleanup.c
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * cleanup.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "simple_writer.h"
+
+#include <stdlib.h>
+
+void sqfs_writer_cleanup(sqfs_writer_t *sqfs, int status)
+{
+ if (sqfs->xwr != NULL)
+ sqfs_destroy(sqfs->xwr);
+
+ sqfs_destroy(sqfs->dirwr);
+ sqfs_destroy(sqfs->dm);
+ sqfs_destroy(sqfs->im);
+ sqfs_destroy(sqfs->idtbl);
+ sqfs_destroy(sqfs->data);
+ sqfs_destroy(sqfs->blkwr);
+ sqfs_destroy(sqfs->fragtbl);
+ sqfs_destroy(sqfs->cmp);
+ fstree_cleanup(&sqfs->fs);
+ sqfs_destroy(sqfs->outfile);
+
+ if (status != EXIT_SUCCESS) {
+#if defined(_WIN32) || defined(__WINDOWS__)
+ WCHAR *path = path_to_windows(sqfs->filename);
+
+ if (path != NULL)
+ DeleteFileW(path);
+
+ free(path);
+#else
+ unlink(sqfs->filename);
+#endif
+ }
+}
+
diff --git a/lib/common/writer/finish.c b/lib/common/writer/finish.c
new file mode 100644
index 0000000..e005628
--- /dev/null
+++ b/lib/common/writer/finish.c
@@ -0,0 +1,176 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * finish.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "simple_writer.h"
+#include "common.h"
+
+#include <stdlib.h>
+
+static void print_statistics(const sqfs_super_t *super,
+ const sqfs_block_processor_t *blk,
+ const sqfs_block_writer_t *wr)
+{
+ const sqfs_block_processor_stats_t *proc_stats;
+ sqfs_u64 bytes_written, blocks_written;
+ char read_sz[32], written_sz[32];
+ size_t ratio;
+
+ proc_stats = sqfs_block_processor_get_stats(blk);
+ blocks_written = wr->get_block_count(wr);
+
+ bytes_written = super->inode_table_start - sizeof(*super);
+
+ if (proc_stats->input_bytes_read > 0) {
+ ratio = (100 * bytes_written) / proc_stats->input_bytes_read;
+ } else {
+ ratio = 100;
+ }
+
+ print_size(proc_stats->input_bytes_read, read_sz, false);
+ print_size(bytes_written, written_sz, false);
+
+ fputs("---------------------------------------------------\n", stdout);
+ printf("Data bytes read: %s\n", read_sz);
+ printf("Data bytes written: %s\n", written_sz);
+ printf("Data compression ratio: " PRI_SZ "%%\n", ratio);
+ fputc('\n', stdout);
+
+ printf("Data blocks written: " PRI_U64 "\n", blocks_written);
+ printf("Out of which where fragment blocks: " PRI_U64 "\n",
+ proc_stats->frag_block_count);
+
+ printf("Duplicate blocks omitted: " PRI_U64 "\n",
+ proc_stats->data_block_count + proc_stats->frag_block_count -
+ blocks_written);
+
+ printf("Sparse blocks omitted: " PRI_U64 "\n",
+ proc_stats->sparse_block_count);
+ fputc('\n', stdout);
+
+ printf("Fragments actually written: " PRI_U64 "\n",
+ proc_stats->actual_frag_count);
+ printf("Duplicated fragments omitted: " PRI_U64 "\n",
+ proc_stats->total_frag_count - proc_stats->actual_frag_count);
+ printf("Total number of inodes: %u\n", super->inode_count);
+ printf("Number of unique group/user IDs: %u\n", super->id_count);
+ fputc('\n', stdout);
+}
+
+static int padd_sqfs(sqfs_file_t *file, sqfs_u64 size, size_t blocksize)
+{
+ size_t padd_sz = size % blocksize;
+ int status = -1;
+ sqfs_u8 *buffer;
+
+ if (padd_sz == 0)
+ return 0;
+
+ padd_sz = blocksize - padd_sz;
+
+ buffer = calloc(1, padd_sz);
+ if (buffer == NULL)
+ goto fail_errno;
+
+ if (file->write_at(file, file->get_size(file),
+ buffer, padd_sz)) {
+ goto fail_errno;
+ }
+
+ status = 0;
+out:
+ free(buffer);
+ return status;
+fail_errno:
+ perror("padding output file to block size");
+ goto out;
+}
+
+int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
+{
+ int ret;
+
+ if (!cfg->quiet)
+ fputs("Waiting for remaining data blocks...\n", stdout);
+
+ ret = sqfs_block_processor_finish(sqfs->data);
+ if (ret) {
+ sqfs_perror(cfg->filename, "finishing data blocks", ret);
+ return -1;
+ }
+
+ if (!cfg->quiet)
+ fputs("Writing inodes and directories...\n", stdout);
+
+ sqfs->super.inode_count = sqfs->fs.unique_inode_count;
+
+ if (sqfs_serialize_fstree(cfg->filename, sqfs))
+ return -1;
+
+ if (!cfg->quiet)
+ fputs("Writing fragment table...\n", stdout);
+
+ ret = sqfs_frag_table_write(sqfs->fragtbl, sqfs->outfile,
+ &sqfs->super, sqfs->cmp);
+ if (ret) {
+ sqfs_perror(cfg->filename, "writing fragment table", ret);
+ return -1;
+ }
+
+ if (cfg->exportable) {
+ if (!cfg->quiet)
+ fputs("Writing export table...\n", stdout);
+
+
+ ret = sqfs_dir_writer_write_export_table(sqfs->dirwr,
+ sqfs->outfile, sqfs->cmp,
+ sqfs->fs.root->inode_num,
+ sqfs->fs.root->inode_ref,
+ &sqfs->super);
+ if (ret)
+ return -1;
+ }
+
+ if (!cfg->quiet)
+ fputs("Writing ID table...\n", stdout);
+
+ ret = sqfs_id_table_write(sqfs->idtbl, sqfs->outfile,
+ &sqfs->super, sqfs->cmp);
+ if (ret) {
+ sqfs_perror(cfg->filename, "writing ID table", ret);
+ return -1;
+ }
+
+ if (!cfg->no_xattr) {
+ if (!cfg->quiet)
+ fputs("Writing extended attributes...\n", stdout);
+
+ ret = sqfs_xattr_writer_flush(sqfs->xwr, sqfs->outfile,
+ &sqfs->super, sqfs->cmp);
+ if (ret) {
+ sqfs_perror(cfg->filename,
+ "writing extended attributes", ret);
+ return -1;
+ }
+ }
+
+ sqfs->super.bytes_used = sqfs->outfile->get_size(sqfs->outfile);
+
+ ret = sqfs_super_write(&sqfs->super, sqfs->outfile);
+ if (ret) {
+ sqfs_perror(cfg->filename, "updating super block", ret);
+ return -1;
+ }
+
+ if (padd_sqfs(sqfs->outfile, sqfs->super.bytes_used,
+ cfg->devblksize)) {
+ return -1;
+ }
+
+ if (!cfg->quiet)
+ print_statistics(&sqfs->super, sqfs->data, sqfs->blkwr);
+
+ return 0;
+}
diff --git a/lib/common/writer.c b/lib/common/writer/init.c
index 9032a99..d606d26 100644
--- a/lib/common/writer.c
+++ b/lib/common/writer/init.c
@@ -1,9 +1,11 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
- * writer.c
+ * init.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
+#include "simple_writer.h"
+#include "compress_cli.h"
#include "common.h"
#include <string.h>
@@ -26,35 +28,6 @@ static size_t os_get_num_jobs(void)
}
#endif
-static int padd_sqfs(sqfs_file_t *file, sqfs_u64 size, size_t blocksize)
-{
- size_t padd_sz = size % blocksize;
- int status = -1;
- sqfs_u8 *buffer;
-
- if (padd_sz == 0)
- return 0;
-
- padd_sz = blocksize - padd_sz;
-
- buffer = calloc(1, padd_sz);
- if (buffer == NULL)
- goto fail_errno;
-
- if (file->write_at(file, file->get_size(file),
- buffer, padd_sz)) {
- goto fail_errno;
- }
-
- status = 0;
-out:
- free(buffer);
- return status;
-fail_errno:
- perror("padding output file to block size");
- goto out;
-}
-
void sqfs_writer_cfg_init(sqfs_writer_cfg_t *cfg)
{
memset(cfg, 0, sizeof(*cfg));
@@ -211,119 +184,3 @@ fail_file:
sqfs_destroy(sqfs->outfile);
return -1;
}
-
-int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
-{
- int ret;
-
- if (!cfg->quiet)
- fputs("Waiting for remaining data blocks...\n", stdout);
-
- ret = sqfs_block_processor_finish(sqfs->data);
- if (ret) {
- sqfs_perror(cfg->filename, "finishing data blocks", ret);
- return -1;
- }
-
- if (!cfg->quiet)
- fputs("Writing inodes and directories...\n", stdout);
-
- sqfs->super.inode_count = sqfs->fs.unique_inode_count;
-
- if (sqfs_serialize_fstree(cfg->filename, sqfs))
- return -1;
-
- if (!cfg->quiet)
- fputs("Writing fragment table...\n", stdout);
-
- ret = sqfs_frag_table_write(sqfs->fragtbl, sqfs->outfile,
- &sqfs->super, sqfs->cmp);
- if (ret) {
- sqfs_perror(cfg->filename, "writing fragment table", ret);
- return -1;
- }
-
- if (cfg->exportable) {
- if (!cfg->quiet)
- fputs("Writing export table...\n", stdout);
-
-
- ret = sqfs_dir_writer_write_export_table(sqfs->dirwr,
- sqfs->outfile, sqfs->cmp,
- sqfs->fs.root->inode_num,
- sqfs->fs.root->inode_ref,
- &sqfs->super);
- if (ret)
- return -1;
- }
-
- if (!cfg->quiet)
- fputs("Writing ID table...\n", stdout);
-
- ret = sqfs_id_table_write(sqfs->idtbl, sqfs->outfile,
- &sqfs->super, sqfs->cmp);
- if (ret) {
- sqfs_perror(cfg->filename, "writing ID table", ret);
- return -1;
- }
-
- if (!cfg->no_xattr) {
- if (!cfg->quiet)
- fputs("Writing extended attributes...\n", stdout);
-
- ret = sqfs_xattr_writer_flush(sqfs->xwr, sqfs->outfile,
- &sqfs->super, sqfs->cmp);
- if (ret) {
- sqfs_perror(cfg->filename, "writing extended attributes", ret);
- return -1;
- }
- }
-
- sqfs->super.bytes_used = sqfs->outfile->get_size(sqfs->outfile);
-
- ret = sqfs_super_write(&sqfs->super, sqfs->outfile);
- if (ret) {
- sqfs_perror(cfg->filename, "updating super block", ret);
- return -1;
- }
-
- if (padd_sqfs(sqfs->outfile, sqfs->super.bytes_used,
- cfg->devblksize)) {
- return -1;
- }
-
- if (!cfg->quiet)
- sqfs_print_statistics(&sqfs->super, sqfs->data, sqfs->blkwr);
-
- return 0;
-}
-
-void sqfs_writer_cleanup(sqfs_writer_t *sqfs, int status)
-{
- if (sqfs->xwr != NULL)
- sqfs_destroy(sqfs->xwr);
-
- sqfs_destroy(sqfs->dirwr);
- sqfs_destroy(sqfs->dm);
- sqfs_destroy(sqfs->im);
- sqfs_destroy(sqfs->idtbl);
- sqfs_destroy(sqfs->data);
- sqfs_destroy(sqfs->blkwr);
- sqfs_destroy(sqfs->fragtbl);
- sqfs_destroy(sqfs->cmp);
- fstree_cleanup(&sqfs->fs);
- sqfs_destroy(sqfs->outfile);
-
- if (status != EXIT_SUCCESS) {
-#if defined(_WIN32) || defined(__WINDOWS__)
- WCHAR *path = path_to_windows(sqfs->filename);
-
- if (path != NULL)
- DeleteFileW(path);
-
- free(path);
-#else
- unlink(sqfs->filename);
-#endif
- }
-}
diff --git a/lib/common/serialize_fstree.c b/lib/common/writer/serialize_fstree.c
index c69f0ea..c69f0ea 100644
--- a/lib/common/serialize_fstree.c
+++ b/lib/common/writer/serialize_fstree.c