From 02232ffdbd7b927b71a9f38f4b78bbf99c675ec5 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 30 Apr 2019 12:26:09 +0200 Subject: Add super block and padding functions Signed-off-by: David Oberhollenzer --- mkfs/Makemodule.am | 4 +- mkfs/mksquashfs.c | 46 +++++++++++-------- mkfs/mksquashfs.h | 22 ++++++++++ mkfs/super.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 18 deletions(-) create mode 100644 mkfs/mksquashfs.h create mode 100644 mkfs/super.c diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am index 7b0b353..1f0acf2 100644 --- a/mkfs/Makemodule.am +++ b/mkfs/Makemodule.am @@ -1,5 +1,7 @@ -mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/options.c mkfs/options.h +mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/mksquashfs.h +mksquashfs_SOURCES += mkfs/options.c mkfs/options.h mksquashfs_SOURCES += mkfs/meta_writer.c mkfs/meta_writer.h +mksquashfs_SOURCES += mkfs/super.c mksquashfs_SOURCES += include/squashfs.h mksquashfs_LDADD = libutil.a libfstree.a libcompress.a diff --git a/mkfs/mksquashfs.c b/mkfs/mksquashfs.c index bc2a215..17de4ed 100644 --- a/mkfs/mksquashfs.c +++ b/mkfs/mksquashfs.c @@ -1,14 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "squashfs.h" -#include "options.h" -#include "fstree.h" - -#include -#include -#include -#include -#include -#include +#include "mksquashfs.h" static void print_tree(int level, tree_node_t *node) { @@ -43,28 +34,49 @@ static void print_tree(int level, tree_node_t *node) } } - int main(int argc, char **argv) { + int outfd, status = EXIT_FAILURE; + sqfs_super_t super; options_t opt; fstree_t fs; process_command_line(&opt, argc, argv); - if (fstree_init(&fs, opt.blksz, opt.def_mtime, opt.def_mode, - opt.def_uid, opt.def_gid)) { + if (sqfs_super_init(&super, &opt) != 0) return EXIT_FAILURE; - } - if (fstree_from_file(&fs, opt.infile)) { - fstree_cleanup(&fs); + outfd = open(opt.outfile, opt.outmode, 0644); + if (outfd < 0) { + perror(opt.outfile); return EXIT_FAILURE; } + if (sqfs_super_write(&super, outfd)) + goto out_outfd; + + if (fstree_init(&fs, opt.blksz, opt.def_mtime, opt.def_mode, + opt.def_uid, opt.def_gid)) { + goto out_outfd; + } + + if (fstree_from_file(&fs, opt.infile)) + goto out_fstree; + fstree_sort(&fs); print_tree(0, fs.root); + if (sqfs_super_write(&super, outfd)) + goto out_outfd; + + if (sqfs_padd_file(&super, &opt, outfd)) + goto out_fstree; + + status = EXIT_SUCCESS; +out_fstree: fstree_cleanup(&fs); - return EXIT_SUCCESS; +out_outfd: + close(outfd); + return status; } diff --git a/mkfs/mksquashfs.h b/mkfs/mksquashfs.h new file mode 100644 index 0000000..8dda29c --- /dev/null +++ b/mkfs/mksquashfs.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef MKSQUASHFS_H +#define MKSQUASHFS_H + +#include "squashfs.h" +#include "options.h" +#include "fstree.h" + +#include +#include +#include +#include +#include +#include + +int sqfs_super_init(sqfs_super_t *s, const options_t *opt); + +int sqfs_padd_file(sqfs_super_t *s, const options_t *opt, int outfd); + +int sqfs_super_write(const sqfs_super_t *super, int outfd); + +#endif /* MKSQUASHFS_H */ diff --git a/mkfs/super.c b/mkfs/super.c new file mode 100644 index 0000000..b3755d1 --- /dev/null +++ b/mkfs/super.c @@ -0,0 +1,126 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "mksquashfs.h" +#include "util.h" + +#include + +int sqfs_super_init(sqfs_super_t *s, const options_t *opt) +{ + unsigned int i; + + if (opt->blksz & (opt->blksz - 1)) { + fputs("Block size must be a power of 2!\n", stderr); + return -1; + } + + if (opt->blksz < 8192 || opt->blksz >= (1 << 24)) { + fputs("Block size must be between 8k and 1M\n", stderr); + return -1; + } + + memset(s, 0, sizeof(*s)); + s->magic = SQFS_MAGIC; + s->modification_time = opt->def_mtime; + s->block_size = opt->blksz; + s->compression_id = opt->compressor; + s->flags = SQFS_FLAG_NO_FRAGMENTS | SQFS_FLAG_NO_XATTRS; + s->version_major = SQFS_VERSION_MAJOR; + s->version_minor = SQFS_VERSION_MINOR; + s->bytes_used = sizeof(*s); + s->id_table_start = 0xFFFFFFFFFFFFFFFFUL; + s->xattr_id_table_start = 0xFFFFFFFFFFFFFFFFUL; + s->inode_table_start = 0xFFFFFFFFFFFFFFFFUL; + s->directory_table_start = 0xFFFFFFFFFFFFFFFFUL; + s->fragment_table_start = 0xFFFFFFFFFFFFFFFFUL; + s->export_table_start = 0xFFFFFFFFFFFFFFFFUL; + + for (i = opt->blksz; i != 0x01; i >>= 1) + s->block_log += 1; + + return 0; +} + +int sqfs_padd_file(sqfs_super_t *s, const options_t *opt, int outfd) +{ + size_t padd_sz = s->bytes_used % opt->devblksz; + uint8_t *buffer; + ssize_t ret; + + if (padd_sz == 0) + return 0; + + padd_sz = opt->devblksz - padd_sz; + + buffer = calloc(1, padd_sz); + if (buffer == NULL) { + perror("padding output file to block size"); + return -1; + } + + ret = write_retry(outfd, buffer, padd_sz); + + if (ret < 0) { + perror("Error padding squashfs image to page size"); + free(buffer); + return -1; + } + + if ((size_t)ret < padd_sz) { + fputs("Truncated write trying to padd squashfs image\n", + stderr); + return -1; + } + + free(buffer); + return 0; +} + +int sqfs_super_write(const sqfs_super_t *super, int outfd) +{ + sqfs_super_t copy; + ssize_t ret; + + copy.magic = htole32(super->magic); + copy.inode_count = htole32(super->inode_count); + copy.modification_time = htole32(super->modification_time); + copy.block_size = htole32(super->block_size); + copy.fragment_entry_count = htole32(super->fragment_entry_count); + copy.compression_id = htole16(super->compression_id); + copy.block_log = htole16(super->block_log); + copy.flags = htole16(super->flags); + copy.id_count = htole16(super->id_count); + copy.version_major = htole16(super->version_major); + copy.version_minor = htole16(super->version_minor); + copy.root_inode_ref = htole64(super->root_inode_ref); + copy.bytes_used = htole64(super->bytes_used); + copy.id_table_start = htole64(super->id_table_start); + copy.xattr_id_table_start = htole64(super->xattr_id_table_start); + copy.inode_table_start = htole64(super->inode_table_start); + copy.directory_table_start = htole64(super->directory_table_start); + copy.fragment_table_start = htole64(super->fragment_table_start); + copy.export_table_start = htole64(super->export_table_start); + + if (lseek(outfd, 0, SEEK_SET) == (off_t)-1) + goto fail_seek; + + ret = write_retry(outfd, ©, sizeof(copy)); + + if (ret < 0) { + perror("squashfs writing super block"); + return -1; + } + + if ((size_t)ret < sizeof(copy)) { + fputs("squashfs writing super block: truncated write\n", + stderr); + return -1; + } + + if (lseek(outfd, 0, SEEK_END) == (off_t)-1) + goto fail_seek; + + return 0; +fail_seek: + perror("squashfs writing super block: seek on output file"); + return -1; +} -- cgit v1.2.3