diff options
-rw-r--r-- | include/squashfs.h | 6 | ||||
-rw-r--r-- | lib/Makemodule.am | 2 | ||||
-rw-r--r-- | lib/sqfs/super.c | 95 | ||||
-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 |
7 files changed, 145 insertions, 138 deletions
diff --git a/include/squashfs.h b/include/squashfs.h index 68f9fce..96a558b 100644 --- a/include/squashfs.h +++ b/include/squashfs.h @@ -3,6 +3,7 @@ #define SQUASHFS_H #include <stdint.h> +#include <stddef.h> #define SQFS_MAGIC 0x73717368 #define SQFS_VERSION_MAJOR 4 @@ -174,4 +175,9 @@ typedef enum { SQFS_INODE_EXT_SOCKET = 14, } E_SQFS_INODE_TYPE; +int sqfs_super_init(sqfs_super_t *super, size_t block_size, uint32_t mtime, + E_SQFS_COMPRESSOR compressor); + +int sqfs_super_write(sqfs_super_t *super, int fd); + #endif /* SQUASHFS_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index c969eff..86f1766 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -7,7 +7,7 @@ libcompress_a_CFLAGS = $(AM_CFLAGS) libcompress_a_CPPFLAGS = $(AM_CPPFLAGS) libsquashfs_a_SOURCES = include/meta_writer.h include/squashfs.h -libsquashfs_a_SOURCES += lib/sqfs/meta_writer.c +libsquashfs_a_SOURCES += lib/sqfs/meta_writer.c lib/sqfs/super.c libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c libutil_a_SOURCES += lib/util/read_retry.c include/util.h diff --git a/lib/sqfs/super.c b/lib/sqfs/super.c new file mode 100644 index 0000000..b402496 --- /dev/null +++ b/lib/sqfs/super.c @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "squashfs.h" +#include "util.h" + +#include <endian.h> +#include <string.h> +#include <unistd.h> +#include <stdio.h> + +int sqfs_super_init(sqfs_super_t *super, size_t block_size, uint32_t mtime, + E_SQFS_COMPRESSOR compressor) +{ + unsigned int i; + + if (block_size & (block_size - 1)) { + fputs("Block size must be a power of 2!\n", stderr); + return -1; + } + + if (block_size < 8192 || block_size >= (1 << 24)) { + fputs("Block size must be between 8k and 1M\n", stderr); + return -1; + } + + memset(super, 0, sizeof(*super)); + super->magic = SQFS_MAGIC; + super->modification_time = mtime; + super->block_size = block_size; + super->compression_id = compressor; + super->flags = SQFS_FLAG_NO_FRAGMENTS | SQFS_FLAG_NO_XATTRS; + super->version_major = SQFS_VERSION_MAJOR; + super->version_minor = SQFS_VERSION_MINOR; + super->bytes_used = sizeof(*super); + super->id_table_start = 0xFFFFFFFFFFFFFFFFUL; + super->xattr_id_table_start = 0xFFFFFFFFFFFFFFFFUL; + super->inode_table_start = 0xFFFFFFFFFFFFFFFFUL; + super->directory_table_start = 0xFFFFFFFFFFFFFFFFUL; + super->fragment_table_start = 0xFFFFFFFFFFFFFFFFUL; + super->export_table_start = 0xFFFFFFFFFFFFFFFFUL; + + for (i = block_size; i != 0x01; i >>= 1) + super->block_log += 1; + + return 0; +} + +int sqfs_super_write(sqfs_super_t *super, int fd) +{ + 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(fd, 0, SEEK_SET) == (off_t)-1) + goto fail_seek; + + ret = write_retry(fd, ©, 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(fd, 0, SEEK_END) == (off_t)-1) + goto fail_seek; + + return 0; +fail_seek: + perror("squashfs writing super block: seek on output file"); + return -1; +} 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; -} |