From 3a851dfe87c88ac1d4dddc2a26cc48b037f852f9 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 8 Sep 2019 14:53:30 +0200 Subject: Replace direct file I/O with abstraction layer This should make it easier to use libsquashfs with custom setups that embedd a squashfs image inside something else. Also, it should make it easier to port to non unix-like platforms. Signed-off-by: David Oberhollenzer --- include/data_reader.h | 2 +- include/data_writer.h | 2 +- include/highlevel.h | 11 ++++++----- include/sqfs/compress.h | 4 ++-- include/sqfs/id_table.h | 4 ++-- include/sqfs/io.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ include/sqfs/meta_reader.h | 2 +- include/sqfs/meta_writer.h | 2 +- include/sqfs/predef.h | 1 + include/sqfs/super.h | 4 ++-- include/sqfs/table.h | 4 ++-- include/sqfs/xattr.h | 2 +- include/util.h | 3 +++ 13 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 include/sqfs/io.h (limited to 'include') diff --git a/include/data_reader.h b/include/data_reader.h index a45c3e2..846ffd1 100644 --- a/include/data_reader.h +++ b/include/data_reader.h @@ -23,7 +23,7 @@ typedef struct data_reader_t data_reader_t; Prints error messsages to stderr on failure. */ -data_reader_t *data_reader_create(int fd, sqfs_super_t *super, +data_reader_t *data_reader_create(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp); void data_reader_destroy(data_reader_t *data); diff --git a/include/data_writer.h b/include/data_writer.h index 89eb2c8..4d86bbf 100644 --- a/include/data_writer.h +++ b/include/data_writer.h @@ -36,7 +36,7 @@ enum { Returns NULL on failure and prints errors to stderr. */ data_writer_t *data_writer_create(sqfs_super_t *super, sqfs_compressor_t *cmp, - int outfd, size_t devblksize, + sqfs_file_t *file, size_t devblksize, unsigned int num_jobs); void data_writer_destroy(data_writer_t *data); diff --git a/include/highlevel.h b/include/highlevel.h index ffcc44c..4c560f7 100644 --- a/include/highlevel.h +++ b/include/highlevel.h @@ -17,6 +17,7 @@ #include "sqfs/meta_writer.h" #include "sqfs/xattr.h" #include "sqfs/dir.h" +#include "sqfs/io.h" #include "data_reader.h" #include "fstree.h" @@ -26,9 +27,9 @@ typedef struct { sqfs_compressor_t *cmp; data_reader_t *data; + sqfs_file_t *file; sqfs_super_t super; fstree_t fs; - int sqfsfd; } sqfs_reader_t; enum RDTREE_FLAGS { @@ -52,7 +53,7 @@ enum RDTREE_FLAGS { Returns 0 on success. Prints error messages to stderr on failure. */ -int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs, +int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs, sqfs_compressor_t *cmp, sqfs_id_table_t *idtbl); /* @@ -72,14 +73,14 @@ tree_node_t *tree_node_from_inode(sqfs_inode_generic_t *inode, Returns 0 on success. Prints error messages to stderr on failure. */ int deserialize_fstree(fstree_t *out, sqfs_super_t *super, - sqfs_compressor_t *cmp, int fd, int flags); + sqfs_compressor_t *cmp, sqfs_file_t *file, int flags); /* Generate a squahfs xattr table from a file system tree. Returns 0 on success. Prints error messages to stderr on failure. */ -int write_xattr(int outfd, fstree_t *fs, sqfs_super_t *super, +int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, sqfs_compressor_t *cmp); /* @@ -87,7 +88,7 @@ int write_xattr(int outfd, fstree_t *fs, sqfs_super_t *super, Returns 0 on success. Prints error messages to stderr on failure. */ -int write_export_table(int outfd, fstree_t *fs, sqfs_super_t *super, +int write_export_table(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, sqfs_compressor_t *cmp); /* Print out fancy statistics for squashfs packing tools */ diff --git a/include/sqfs/compress.h b/include/sqfs/compress.h index f0e3b76..e781e02 100644 --- a/include/sqfs/compress.h +++ b/include/sqfs/compress.h @@ -18,12 +18,12 @@ struct sqfs_compressor_t { /* Write compressor options to the output file if necessary. Returns the number of bytes written or -1 on failure. Internally prints error messages to stderr. */ - int (*write_options)(sqfs_compressor_t *cmp, int fd); + int (*write_options)(sqfs_compressor_t *cmp, sqfs_file_t *file); /* Read compressor options to the input file. Returns zero on success, -1 on failure. Internally prints error messages to stderr. */ - int (*read_options)(sqfs_compressor_t *cmp, int fd); + int (*read_options)(sqfs_compressor_t *cmp, sqfs_file_t *file); /* Compress or uncompress a chunk of data. diff --git a/include/sqfs/id_table.h b/include/sqfs/id_table.h index 3610640..6b058ae 100644 --- a/include/sqfs/id_table.h +++ b/include/sqfs/id_table.h @@ -25,12 +25,12 @@ SQFS_API int sqfs_id_table_id_to_index(sqfs_id_table_t *tbl, uint32_t id, /* Write an ID table to a SquashFS image. Returns 0 on success. Internally prints error message to stderr. */ -SQFS_API int sqfs_id_table_write(sqfs_id_table_t *tbl, int outfd, +SQFS_API int sqfs_id_table_write(sqfs_id_table_t *tbl, sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp); /* Read an ID table from a SquashFS image. Returns 0 on success. Internally prints error messages to stderr. */ -SQFS_API int sqfs_id_table_read(sqfs_id_table_t *tbl, int fd, +SQFS_API int sqfs_id_table_read(sqfs_id_table_t *tbl, sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp); SQFS_API int sqfs_id_table_index_to_id(const sqfs_id_table_t *tbl, diff --git a/include/sqfs/io.h b/include/sqfs/io.h new file mode 100644 index 0000000..f290c2d --- /dev/null +++ b/include/sqfs/io.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * io.h + * + * Copyright (C) 2019 David Oberhollenzer + */ +#ifndef SQFS_IO_H +#define SQFS_IO_H + +#include "sqfs/predef.h" + +typedef enum { + SQFS_FILE_OPEN_READ_ONLY = 0x01, + + SQFS_FILE_OPEN_OVERWRITE = 0x02, + + SQFS_FILE_OPEN_ALL_FLAGS = 0x03, +} E_SQFS_FILE_OPEN_FLAGS; + +struct sqfs_file_t { + void (*destroy)(sqfs_file_t *file); + + int (*read_at)(sqfs_file_t *file, uint64_t offset, + void *buffer, size_t size); + + int (*write_at)(sqfs_file_t *file, uint64_t offset, + const void *buffer, size_t size); + + uint64_t (*get_size)(sqfs_file_t *file); + + int (*truncate)(sqfs_file_t *file, uint64_t size); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, int flags); + +#ifdef __cplusplus +} +#endif + +#endif /* SQFS_IO_H */ diff --git a/include/sqfs/meta_reader.h b/include/sqfs/meta_reader.h index f5008d4..00a6d65 100644 --- a/include/sqfs/meta_reader.h +++ b/include/sqfs/meta_reader.h @@ -19,7 +19,7 @@ extern "C" { Start offset and limit can be specified to do bounds checking against a subregion of the filesystem image. */ -SQFS_API sqfs_meta_reader_t *sqfs_meta_reader_create(int fd, +SQFS_API sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file, sqfs_compressor_t *cmp, uint64_t start, uint64_t limit); diff --git a/include/sqfs/meta_writer.h b/include/sqfs/meta_writer.h index 9cd1598..3b255f2 100644 --- a/include/sqfs/meta_writer.h +++ b/include/sqfs/meta_writer.h @@ -18,7 +18,7 @@ extern "C" { If keep_in_mem is true, the blocks are collected in memory and must be explicitly flushed to disk using meta_write_write_to_file. */ -SQFS_API sqfs_meta_writer_t *sqfs_meta_writer_create(int fd, +SQFS_API sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file, sqfs_compressor_t *cmp, bool keep_in_mem); diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index 6df257c..df08b1b 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -47,6 +47,7 @@ typedef struct sqfs_id_table_t sqfs_id_table_t; typedef struct sqfs_meta_reader_t sqfs_meta_reader_t; typedef struct sqfs_meta_writer_t sqfs_meta_writer_t; typedef struct sqfs_xattr_reader_t sqfs_xattr_reader_t; +typedef struct sqfs_file_t sqfs_file_t; typedef struct sqfs_fragment_t sqfs_fragment_t; typedef struct sqfs_dir_header_t sqfs_dir_header_t; diff --git a/include/sqfs/super.h b/include/sqfs/super.h index b1ce82b..a8697a1 100644 --- a/include/sqfs/super.h +++ b/include/sqfs/super.h @@ -73,10 +73,10 @@ SQFS_API int sqfs_super_init(sqfs_super_t *super, size_t block_size, E_SQFS_COMPRESSOR compressor); /* Returns 0 on success. Prints error messages to stderr on failure. */ -SQFS_API int sqfs_super_write(sqfs_super_t *super, int fd); +SQFS_API int sqfs_super_write(sqfs_super_t *super, sqfs_file_t *file); /* Returns 0 on success. Prints error messages to stderr on failure. */ -SQFS_API int sqfs_super_read(sqfs_super_t *super, int fd); +SQFS_API int sqfs_super_read(sqfs_super_t *super, sqfs_file_t *file); #ifdef __cplusplus } diff --git a/include/sqfs/table.h b/include/sqfs/table.h index f837f6b..7767b9a 100644 --- a/include/sqfs/table.h +++ b/include/sqfs/table.h @@ -24,12 +24,12 @@ extern "C" { Returns 0 on success. Internally prints error messages to stderr. */ -SQFS_API int sqfs_write_table(int outfd, sqfs_super_t *super, +SQFS_API int sqfs_write_table(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp, const void *data, size_t table_size, uint64_t *start); -SQFS_API int sqfs_read_table(int fd, sqfs_compressor_t *cmp, +SQFS_API int sqfs_read_table(sqfs_file_t *file, sqfs_compressor_t *cmp, size_t table_size, uint64_t location, uint64_t lower_limit, uint64_t upper_limit, void **out); diff --git a/include/sqfs/xattr.h b/include/sqfs/xattr.h index 0f9cf32..23587d1 100644 --- a/include/sqfs/xattr.h +++ b/include/sqfs/xattr.h @@ -58,7 +58,7 @@ SQFS_API int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr); SQFS_API void sqfs_xattr_reader_destroy(sqfs_xattr_reader_t *xr); -SQFS_API sqfs_xattr_reader_t *sqfs_xattr_reader_create(int sqfsfd, +SQFS_API sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file, sqfs_super_t *super, sqfs_compressor_t *cmp); diff --git a/include/util.h b/include/util.h index 93b8a04..e2089da 100644 --- a/include/util.h +++ b/include/util.h @@ -109,6 +109,9 @@ int popd(void); SQFS_INTERNAL int padd_file(int outfd, uint64_t size, size_t blocksize); +SQFS_INTERNAL +int padd_sqfs(sqfs_file_t *file, uint64_t size, size_t blocksize); + /* Helper for allocating data structures with flexible array members. -- cgit v1.2.3