From 1e24f45ab120ea83a6faab2e4e14038547898f64 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 5 Oct 2019 20:06:50 +0200 Subject: Merge all the common code for generating images Signed-off-by: David Oberhollenzer --- mkfs/mkfs.c | 138 +++++++-------------------------------------------------- mkfs/mkfs.h | 19 +------- mkfs/options.c | 46 +++++++++---------- 3 files changed, 39 insertions(+), 164 deletions(-) (limited to 'mkfs') diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index e36f2f4..16f9bc0 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -42,7 +42,7 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs, return -1; for (fi = fs->files; fi != NULL; fi = fi->next) { - if (!opt->quiet) + if (!opt->cfg.quiet) printf("packing %s\n", fi->input_file); file = sqfs_open_file(fi->input_file, @@ -54,8 +54,8 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs, filesize = file->get_size(file); - max_blk_count = filesize / opt->blksz; - if (filesize % opt->blksz) + max_blk_count = filesize / opt->cfg.block_size; + if (filesize % opt->cfg.block_size) ++max_blk_count; inode = alloc_flex(sizeof(*inode), sizeof(sqfs_u32), @@ -83,9 +83,6 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs, stats->bytes_read += filesize; } - if (sqfs_data_writer_finish(data)) - return -1; - return restore_working_dir(opt); } @@ -154,63 +151,26 @@ static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr, int main(int argc, char **argv) { - int status = EXIT_FAILURE, ret; - sqfs_compressor_config_t cfg; - data_writer_stats_t stats; - sqfs_data_writer_t *data; - sqfs_xattr_writer_t *xwr; - sqfs_compressor_t *cmp; - sqfs_id_table_t *idtbl; - sqfs_file_t *outfile; + int status = EXIT_FAILURE; void *sehnd = NULL; - sqfs_super_t super; + sqfs_writer_t sqfs; options_t opt; - fstree_t fs; process_command_line(&opt, argc, argv); - if (compressor_cfg_init_options(&cfg, opt.compressor, - opt.blksz, opt.comp_extra)) { - return EXIT_FAILURE; - } - - if (fstree_init(&fs, opt.fs_defaults)) + if (sqfs_writer_init(&sqfs, &opt.cfg)) return EXIT_FAILURE; - if (sqfs_super_init(&super, opt.blksz, fs.defaults.st_mtime, - opt.compressor)) { - goto out_fstree; - } - - idtbl = sqfs_id_table_create(); - if (idtbl == NULL) - goto out_fstree; - - outfile = sqfs_open_file(opt.outfile, opt.outmode); - if (outfile == NULL) { - perror(opt.outfile); - goto out_idtbl; - } - - if (sqfs_super_write(&super, outfile)) - goto out_outfile; - if (opt.selinux != NULL) { sehnd = selinux_open_context_file(opt.selinux); if (sehnd == NULL) - goto out_outfile; - } - - xwr = sqfs_xattr_writer_create(); - if (xwr == NULL) { - perror("creating Xattr writer"); - goto out_outfile; + goto out; } - if (read_fstree(&fs, &opt, xwr, sehnd)) { + if (read_fstree(&sqfs.fs, &opt, sqfs.xwr, sehnd)) { if (sehnd != NULL) selinux_close_context_file(sehnd); - goto out_xwr; + goto out; } if (sehnd != NULL) { @@ -218,81 +178,17 @@ int main(int argc, char **argv) sehnd = NULL; } - tree_node_sort_recursive(fs.root); - - if (fstree_gen_inode_table(&fs)) - goto out_xwr; - - fstree_gen_file_list(&fs); - - super.inode_count = fs.inode_tbl_size; - - cmp = sqfs_compressor_create(&cfg); - if (cmp == NULL) { - fputs("Error creating compressor\n", stderr); - goto out_xwr; - } - - ret = cmp->write_options(cmp, outfile); - if (ret < 0) - goto out_cmp; - - if (ret > 0) - super.flags |= SQFS_FLAG_COMPRESSOR_OPTIONS; - - data = sqfs_data_writer_create(super.block_size, cmp, opt.num_jobs, - opt.max_backlog, opt.devblksz, outfile); - if (data == NULL) - goto out_cmp; - - memset(&stats, 0, sizeof(stats)); - register_stat_hooks(data, &stats); - - if (pack_files(data, &fs, &stats, &opt)) - goto out_data; - - if (sqfs_serialize_fstree(outfile, &super, &fs, cmp, idtbl)) - goto out_data; - - if (sqfs_data_writer_write_fragment_table(data, &super)) - goto out_data; - - if (opt.exportable) { - if (write_export_table(outfile, &fs, &super, cmp)) - goto out_data; - } - - if (sqfs_id_table_write(idtbl, outfile, &super, cmp)) - goto out_data; - - if (sqfs_xattr_writer_flush(xwr, outfile, &super, cmp)) { - fputs("Error writing xattr table\n", stderr); - goto out_data; - } - - super.bytes_used = outfile->get_size(outfile); - - if (sqfs_super_write(&super, outfile)) - goto out_data; + tree_node_sort_recursive(sqfs.fs.root); + fstree_gen_file_list(&sqfs.fs); - if (padd_sqfs(outfile, super.bytes_used, opt.devblksz)) - goto out_data; + if (pack_files(sqfs.data, &sqfs.fs, &sqfs.stats, &opt)) + goto out; - if (!opt.quiet) - sqfs_print_statistics(&super, &stats); + if (sqfs_writer_finish(&sqfs, &opt.cfg)) + goto out; status = EXIT_SUCCESS; -out_data: - sqfs_data_writer_destroy(data); -out_cmp: - cmp->destroy(cmp); -out_xwr: - sqfs_xattr_writer_destroy(xwr); -out_outfile: - outfile->destroy(outfile); -out_idtbl: - sqfs_id_table_destroy(idtbl); -out_fstree: - fstree_cleanup(&fs); +out: + sqfs_writer_cleanup(&sqfs); return status; } diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h index 686938e..0c204e0 100644 --- a/mkfs/mkfs.h +++ b/mkfs/mkfs.h @@ -9,13 +9,6 @@ #include "config.h" -#include "sqfs/xattr_writer.h" -#include "sqfs/meta_writer.h" -#include "sqfs/compressor.h" -#include "sqfs/id_table.h" -#include "sqfs/block.h" -#include "sqfs/io.h" - #include "highlevel.h" #include "fstree.h" #include "util.h" @@ -42,21 +35,11 @@ #include typedef struct { - E_SQFS_COMPRESSOR compressor; - char *fs_defaults; - int outmode; - int blksz; - int devblksz; + sqfs_writer_cfg_t cfg; unsigned int dirscan_flags; - unsigned int num_jobs; - size_t max_backlog; - bool exportable; - bool quiet; const char *infile; const char *packdir; - const char *outfile; const char *selinux; - char *comp_extra; } options_t; enum { diff --git a/mkfs/options.c b/mkfs/options.c index f15569a..d741d1a 100644 --- a/mkfs/options.c +++ b/mkfs/options.c @@ -151,12 +151,7 @@ void process_command_line(options_t *opt, int argc, char **argv) int i; memset(opt, 0, sizeof(*opt)); - opt->outmode = 0; - opt->compressor = compressor_get_default(); - opt->blksz = SQFS_DEFAULT_BLOCK_SIZE; - opt->devblksz = SQFS_DEVBLK_SIZE; - opt->num_jobs = 1; - opt->max_backlog = 0; + sqfs_writer_cfg_init(&opt->cfg); for (;;) { i = getopt_long(argc, argv, short_opts, long_opts, NULL); @@ -168,11 +163,11 @@ void process_command_line(options_t *opt, int argc, char **argv) have_compressor = true; if (sqfs_compressor_id_from_name(optarg, - &opt->compressor)) { + &opt->cfg.comp_id)) { have_compressor = false; } - if (!sqfs_compressor_exists(opt->compressor)) + if (!sqfs_compressor_exists(opt->cfg.comp_id)) have_compressor = false; if (!have_compressor) { @@ -182,24 +177,24 @@ void process_command_line(options_t *opt, int argc, char **argv) } break; case 'b': - opt->blksz = strtol(optarg, NULL, 0); + opt->cfg.block_size = strtol(optarg, NULL, 0); break; case 'j': - opt->num_jobs = strtol(optarg, NULL, 0); + opt->cfg.num_jobs = strtol(optarg, NULL, 0); break; case 'Q': - opt->max_backlog = strtol(optarg, NULL, 0); + opt->cfg.max_backlog = strtol(optarg, NULL, 0); break; case 'B': - opt->devblksz = strtol(optarg, NULL, 0); - if (opt->devblksz < 1024) { + opt->cfg.devblksize = strtol(optarg, NULL, 0); + if (opt->cfg.devblksize < 1024) { fputs("Device block size must be at " "least 1024\n", stderr); exit(EXIT_FAILURE); } break; case 'd': - opt->fs_defaults = optarg; + opt->cfg.fs_defaults = optarg; break; case 'k': opt->dirscan_flags |= DIR_SCAN_KEEP_TIME; @@ -213,16 +208,16 @@ void process_command_line(options_t *opt, int argc, char **argv) opt->dirscan_flags |= DIR_SCAN_ONE_FILESYSTEM; break; case 'e': - opt->exportable = true; + opt->cfg.exportable = true; break; case 'f': - opt->outmode |= SQFS_FILE_OPEN_OVERWRITE; + opt->cfg.outmode |= SQFS_FILE_OPEN_OVERWRITE; break; case 'q': - opt->quiet = true; + opt->cfg.quiet = true; break; case 'X': - opt->comp_extra = optarg; + opt->cfg.comp_extra = optarg; break; case 'F': opt->infile = optarg; @@ -249,14 +244,15 @@ void process_command_line(options_t *opt, int argc, char **argv) } } - if (opt->num_jobs < 1) - opt->num_jobs = 1; + if (opt->cfg.num_jobs < 1) + opt->cfg.num_jobs = 1; - if (opt->max_backlog < 1) - opt->max_backlog = 10 * opt->num_jobs; + if (opt->cfg.max_backlog < 1) + opt->cfg.max_backlog = 10 * opt->cfg.num_jobs; - if (opt->comp_extra != NULL && strcmp(opt->comp_extra, "help") == 0) { - compressor_print_help(opt->compressor); + if (opt->cfg.comp_extra != NULL && + strcmp(opt->cfg.comp_extra, "help") == 0) { + compressor_print_help(opt->cfg.comp_id); exit(EXIT_SUCCESS); } @@ -270,7 +266,7 @@ void process_command_line(options_t *opt, int argc, char **argv) goto fail_arg; } - opt->outfile = argv[optind++]; + opt->cfg.filename = argv[optind++]; return; fail_arg: fprintf(stderr, "Try `%s --help' for more information.\n", __progname); -- cgit v1.2.3