diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-04-30 20:35:37 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-02 12:40:06 +0200 |
commit | 2196bc61b8ba67c91927ba28538538b89ccf73f1 (patch) | |
tree | 7ef4006d078908bef8477adc7844ae015501c5f7 /mkfs | |
parent | 76267654ebd43c1dfa28aa290b0d320df9781b2d (diff) |
Move super block init/write code to libsquashfs.a
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'mkfs')
-rw-r--r-- | mkfs/Makemodule.am | 2 | ||||
-rw-r--r-- | mkfs/mksquashfs.c | 46 | ||||
-rw-r--r-- | mkfs/mksquashfs.h | 6 | ||||
-rw-r--r-- | mkfs/super.c | 126 |
4 files changed, 43 insertions, 137 deletions
diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am index 838b7b9..e5ef93c 100644 --- a/mkfs/Makemodule.am +++ b/mkfs/Makemodule.am @@ -1,5 +1,5 @@ mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/mksquashfs.h mkfs/block.c -mksquashfs_SOURCES += mkfs/options.c mkfs/super.c +mksquashfs_SOURCES += mkfs/options.c mksquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a if WITH_LZMA diff --git a/mkfs/mksquashfs.c b/mkfs/mksquashfs.c index 418d669..7503978 100644 --- a/mkfs/mksquashfs.c +++ b/mkfs/mksquashfs.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include "mksquashfs.h" +#include "util.h" static void print_tree(int level, tree_node_t *node) { @@ -34,6 +35,41 @@ static void print_tree(int level, tree_node_t *node) } } +static int padd_file(sqfs_info_t *info) +{ + size_t padd_sz = info->super.bytes_used % info->opt.devblksz; + uint8_t *buffer; + ssize_t ret; + + if (padd_sz == 0) + return 0; + + padd_sz = info->opt.devblksz - padd_sz; + + buffer = calloc(1, padd_sz); + if (buffer == NULL) { + perror("padding output file to block size"); + return -1; + } + + ret = write_retry(info->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 main(int argc, char **argv) { int status = EXIT_FAILURE; @@ -43,8 +79,10 @@ int main(int argc, char **argv) process_command_line(&info.opt, argc, argv); - if (sqfs_super_init(&info) != 0) + if (sqfs_super_init(&info.super, info.opt.blksz, info.opt.def_mtime, + info.opt.compressor)) { return EXIT_FAILURE; + } info.outfd = open(info.opt.outfile, info.opt.outmode, 0644); if (info.outfd < 0) { @@ -52,7 +90,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - if (sqfs_super_write(&info)) + if (sqfs_super_write(&info.super, info.outfd)) goto out_outfd; if (fstree_init(&info.fs, info.opt.blksz, info.opt.def_mtime, @@ -78,10 +116,10 @@ int main(int argc, char **argv) if (write_data_to_image(&info)) goto out_cmp; - if (sqfs_super_write(&info)) + if (sqfs_super_write(&info.super, info.outfd)) goto out_cmp; - if (sqfs_padd_file(&info)) + if (padd_file(&info)) goto out_cmp; status = EXIT_SUCCESS; diff --git a/mkfs/mksquashfs.h b/mkfs/mksquashfs.h index a9b163f..2e0183e 100644 --- a/mkfs/mksquashfs.h +++ b/mkfs/mksquashfs.h @@ -48,12 +48,6 @@ typedef struct { void process_command_line(options_t *opt, int argc, char **argv); -int sqfs_super_init(sqfs_info_t *info); - -int sqfs_padd_file(sqfs_info_t *info); - -int sqfs_super_write(sqfs_info_t *info); - int write_data_to_image(sqfs_info_t *info); #endif /* MKSQUASHFS_H */ diff --git a/mkfs/super.c b/mkfs/super.c deleted file mode 100644 index c924b6c..0000000 --- a/mkfs/super.c +++ /dev/null @@ -1,126 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#include "mksquashfs.h" -#include "util.h" - -#include <endian.h> - -int sqfs_super_init(sqfs_info_t *info) -{ - unsigned int i; - - if (info->opt.blksz & (info->opt.blksz - 1)) { - fputs("Block size must be a power of 2!\n", stderr); - return -1; - } - - if (info->opt.blksz < 8192 || info->opt.blksz >= (1 << 24)) { - fputs("Block size must be between 8k and 1M\n", stderr); - return -1; - } - - memset(&info->super, 0, sizeof(info->super)); - info->super.magic = SQFS_MAGIC; - info->super.modification_time = info->opt.def_mtime; - info->super.block_size = info->opt.blksz; - info->super.compression_id = info->opt.compressor; - info->super.flags = SQFS_FLAG_NO_FRAGMENTS | SQFS_FLAG_NO_XATTRS; - info->super.version_major = SQFS_VERSION_MAJOR; - info->super.version_minor = SQFS_VERSION_MINOR; - info->super.bytes_used = sizeof(info->super); - info->super.id_table_start = 0xFFFFFFFFFFFFFFFFUL; - info->super.xattr_id_table_start = 0xFFFFFFFFFFFFFFFFUL; - info->super.inode_table_start = 0xFFFFFFFFFFFFFFFFUL; - info->super.directory_table_start = 0xFFFFFFFFFFFFFFFFUL; - info->super.fragment_table_start = 0xFFFFFFFFFFFFFFFFUL; - info->super.export_table_start = 0xFFFFFFFFFFFFFFFFUL; - - for (i = info->opt.blksz; i != 0x01; i >>= 1) - info->super.block_log += 1; - - return 0; -} - -int sqfs_padd_file(sqfs_info_t *info) -{ - size_t padd_sz = info->super.bytes_used % info->opt.devblksz; - uint8_t *buffer; - ssize_t ret; - - if (padd_sz == 0) - return 0; - - padd_sz = info->opt.devblksz - padd_sz; - - buffer = calloc(1, padd_sz); - if (buffer == NULL) { - perror("padding output file to block size"); - return -1; - } - - ret = write_retry(info->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(sqfs_info_t *info) -{ - sqfs_super_t copy; - ssize_t ret; - - copy.magic = htole32(info->super.magic); - copy.inode_count = htole32(info->super.inode_count); - copy.modification_time = htole32(info->super.modification_time); - copy.block_size = htole32(info->super.block_size); - copy.fragment_entry_count = htole32(info->super.fragment_entry_count); - copy.compression_id = htole16(info->super.compression_id); - copy.block_log = htole16(info->super.block_log); - copy.flags = htole16(info->super.flags); - copy.id_count = htole16(info->super.id_count); - copy.version_major = htole16(info->super.version_major); - copy.version_minor = htole16(info->super.version_minor); - copy.root_inode_ref = htole64(info->super.root_inode_ref); - copy.bytes_used = htole64(info->super.bytes_used); - copy.id_table_start = htole64(info->super.id_table_start); - copy.xattr_id_table_start = htole64(info->super.xattr_id_table_start); - copy.inode_table_start = htole64(info->super.inode_table_start); - copy.directory_table_start = htole64(info->super.directory_table_start); - copy.fragment_table_start = htole64(info->super.fragment_table_start); - copy.export_table_start = htole64(info->super.export_table_start); - - if (lseek(info->outfd, 0, SEEK_SET) == (off_t)-1) - goto fail_seek; - - ret = write_retry(info->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(info->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; -} |