summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-10 22:10:46 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-10 22:10:58 +0200
commit69de75de97c17ef7370b26944ee286e4b3dc6266 (patch)
tree3f35ecefdb56aaae6fc806a099dba50777f01a3d
parent41fd1d4b9cd42eb28bf889719b8ce1a81af444b9 (diff)
cleanup: move entire fstree serialization to libsqfs.a
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/meta_writer.h42
-rw-r--r--include/table.h16
-rw-r--r--lib/Makemodule.am2
-rw-r--r--lib/sqfs/serialize_fstree.c (renamed from mkfs/meta.c)14
-rw-r--r--lib/sqfs/write_dir.c (renamed from mkfs/write_dir.c)8
-rw-r--r--lib/sqfs/write_inode.c (renamed from mkfs/write_inode.c)10
-rw-r--r--mkfs/Makemodule.am3
-rw-r--r--mkfs/mkfs.c4
-rw-r--r--mkfs/mkfs.h12
9 files changed, 84 insertions, 27 deletions
diff --git a/include/meta_writer.h b/include/meta_writer.h
index 7036b6a..df0c70c 100644
--- a/include/meta_writer.h
+++ b/include/meta_writer.h
@@ -4,6 +4,20 @@
#include "compress.h"
#include "squashfs.h"
+#include "id_table.h"
+#include "fstree.h"
+
+typedef struct {
+ tree_node_t *node;
+ uint32_t block;
+ uint32_t offset;
+} idx_ref_t;
+
+typedef struct {
+ size_t num_nodes;
+ size_t max_nodes;
+ idx_ref_t idx_nodes[];
+} dir_index_t;
typedef struct meta_writer_t meta_writer_t;
@@ -27,4 +41,32 @@ void meta_writer_get_position(const meta_writer_t *m, uint64_t *block_start,
/* Reset all internal state, including the current block start position. */
void meta_writer_reset(meta_writer_t *m);
+/*
+ High level helper function that writes squashfs directory entries to
+ a meta data writer.
+
+ The dir_info_t structure is used to generate the listing and updated
+ accordingly (such as writing back the header position and total size).
+ A directory index is created on the fly and returned in *index.
+ A single free() call is sufficient.
+
+ Returns 0 on success. Prints error messages to stderr on failure.
+ */
+int meta_writer_write_dir(meta_writer_t *dm, dir_info_t *dir,
+ dir_index_t **index);
+
+/*
+ High level helper function to serialize a tree_node_t to a squashfs inode
+ and write it to a meta data writer.
+
+ The inode is written to `im`. If it is a directory node, the directory
+ contents are written to `dm` using meta_writer_write_dir. The given
+ id_table_t is used to store the uid and gid on the fly and write the
+ coresponding indices to the inode structure.
+
+ Returns 0 on success. Prints error messages to stderr on failure.
+ */
+int meta_writer_write_inode(fstree_t *fs, id_table_t *idtbl, meta_writer_t *im,
+ meta_writer_t *dm, tree_node_t *node);
+
#endif /* META_WRITER_H */
diff --git a/include/table.h b/include/table.h
index 6d61c85..57d8654 100644
--- a/include/table.h
+++ b/include/table.h
@@ -4,6 +4,7 @@
#include "squashfs.h"
#include "compress.h"
+#include "fstree.h"
#include <stdint.h>
#include <stddef.h>
@@ -23,4 +24,19 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, const void *data,
size_t entsize, size_t count, uint64_t *startblock,
compressor_t *cmp);
+/*
+ High level helper function to serialize an entire file system tree to
+ a squashfs inode table and directory table.
+
+ The data is written to the given file descriptor and the super block is
+ update accordingly (inode and directory table start and total size).
+
+ The function internally creates two meta data writers and uses
+ meta_writer_write_inode to serialize the inode table of the fstree.
+
+ Returns 0 on success. Prints error messages to stderr on failure.
+ */
+int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
+ compressor_t *cmp, id_table_t *idtbl);
+
#endif /* TABLE_H */
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index 536cad0..bf4ed61 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -17,6 +17,8 @@ libsquashfs_a_SOURCES += lib/sqfs/read_super.c lib/sqfs/meta_reader.c
libsquashfs_a_SOURCES += include/meta_reader.h lib/sqfs/id_table_write.c
libsquashfs_a_SOURCES += lib/sqfs/id_table_read.c lib/sqfs/read_inode.c
libsquashfs_a_SOURCES += lib/sqfs/readdir.c lib/sqfs/frag_reader.c
+libsquashfs_a_SOURCES += lib/sqfs/write_dir.c lib/sqfs/write_inode.c
+libsquashfs_a_SOURCES += lib/sqfs/serialize_fstree.c
libsquashfs_a_SOURCES += include/frag_reader.h
libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c
diff --git a/mkfs/meta.c b/lib/sqfs/serialize_fstree.c
index 369149f..95b9144 100644
--- a/mkfs/meta.c
+++ b/lib/sqfs/serialize_fstree.c
@@ -1,9 +1,13 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
-#include "mkfs.h"
+#include "meta_writer.h"
+#include "table.h"
#include "util.h"
-int sqfs_write_inodes(sqfs_super_t *super, fstree_t *fs, int outfd,
- compressor_t *cmp, id_table_t *idtbl)
+#include <unistd.h>
+#include <stdio.h>
+
+int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
+ compressor_t *cmp, id_table_t *idtbl)
{
meta_writer_t *im, *dm;
uint8_t buffer[1024];
@@ -31,8 +35,8 @@ int sqfs_write_inodes(sqfs_super_t *super, fstree_t *fs, int outfd,
goto fail_im;
for (i = 2; i < fs->inode_tbl_size; ++i) {
- if (write_inode(fs, idtbl, im, dm,
- fs->inode_table[i])) {
+ if (meta_writer_write_inode(fs, idtbl, im, dm,
+ fs->inode_table[i])) {
goto fail;
}
}
diff --git a/mkfs/write_dir.c b/lib/sqfs/write_dir.c
index c8afb9b..b334864 100644
--- a/mkfs/write_dir.c
+++ b/lib/sqfs/write_dir.c
@@ -1,9 +1,12 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
-#include "mkfs.h"
+#include "meta_writer.h"
#include "util.h"
#include <assert.h>
#include <endian.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
static int get_type(mode_t mode)
{
@@ -45,7 +48,8 @@ static int dir_index_grow(dir_index_t **index)
return 0;
}
-int write_dir(meta_writer_t *dm, dir_info_t *dir, dir_index_t **index)
+int meta_writer_write_dir(meta_writer_t *dm, dir_info_t *dir,
+ dir_index_t **index)
{
size_t i, size, count;
sqfs_dir_header_t hdr;
diff --git a/mkfs/write_inode.c b/lib/sqfs/write_inode.c
index 6890cb6..961013c 100644
--- a/mkfs/write_inode.c
+++ b/lib/sqfs/write_inode.c
@@ -1,9 +1,11 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
-#include "mkfs.h"
+#include "meta_writer.h"
#include "util.h"
#include <assert.h>
#include <endian.h>
+#include <stdlib.h>
+#include <string.h>
static int get_type(tree_node_t *node)
{
@@ -43,8 +45,8 @@ static size_t hard_link_count(tree_node_t *n)
return 1;
}
-int write_inode(fstree_t *fs, id_table_t *idtbl, meta_writer_t *im,
- meta_writer_t *dm, tree_node_t *node)
+int meta_writer_write_inode(fstree_t *fs, id_table_t *idtbl, meta_writer_t *im,
+ meta_writer_t *dm, tree_node_t *node)
{
dir_index_t *diridx = NULL;
uint16_t uid_idx, gid_idx;
@@ -69,7 +71,7 @@ int write_inode(fstree_t *fs, id_table_t *idtbl, meta_writer_t *im,
if (S_ISDIR(node->mode)) {
di = node->data.dir;
- if (write_dir(dm, di, &diridx))
+ if (meta_writer_write_dir(dm, di, &diridx))
return -1;
if ((di->start_block) > 0xFFFFFFFFUL || di->size > 0xFFFF ||
diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am
index 55f30c7..3dd639d 100644
--- a/mkfs/Makemodule.am
+++ b/mkfs/Makemodule.am
@@ -1,6 +1,5 @@
gensquashfs_SOURCES = mkfs/mkfs.c mkfs/mkfs.h mkfs/block.c
-gensquashfs_SOURCES += mkfs/options.c mkfs/meta.c mkfs/xattr.c
-gensquashfs_SOURCES += mkfs/write_dir.c mkfs/write_inode.c
+gensquashfs_SOURCES += mkfs/options.c mkfs/xattr.c
gensquashfs_LDADD = libsquashfs.a libfstree.a libcompress.a libutil.a
gensquashfs_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c
index 7c3fa28..019a233 100644
--- a/mkfs/mkfs.c
+++ b/mkfs/mkfs.c
@@ -114,8 +114,8 @@ int main(int argc, char **argv)
if (write_data_to_image(&info))
goto out_cmp;
- if (sqfs_write_inodes(&info.super, &info.fs, info.outfd,
- info.cmp, &info.idtbl)) {
+ if (sqfs_serialize_fstree(info.outfd, &info.super, &info.fs,
+ info.cmp, &info.idtbl)) {
goto out_cmp;
}
diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h
index 36f412d..5d16c8b 100644
--- a/mkfs/mkfs.h
+++ b/mkfs/mkfs.h
@@ -19,18 +19,6 @@
#include <errno.h>
typedef struct {
- tree_node_t *node;
- uint32_t block;
- uint32_t offset;
-} idx_ref_t;
-
-typedef struct {
- size_t num_nodes;
- size_t max_nodes;
- idx_ref_t idx_nodes[];
-} dir_index_t;
-
-typedef struct {
unsigned int def_uid;
unsigned int def_gid;
unsigned int def_mode;