summaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-12-15 17:01:04 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-12-16 14:46:24 +0100
commit7f567bb6ee1954e2d8a886b1d3dcb8cb4a1ed486 (patch)
tree2415e990e0e6b63fd092d6c6f7fdaf63ccf014ad /lib/common
parent5e2298a3a3f00e6f55ceda2b1c35192d67c9feb4 (diff)
Use the sqfs_dir_writer_t to create the NFS export table
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/Makemodule.am2
-rw-r--r--lib/common/serialize_fstree.c67
-rw-r--r--lib/common/write_export_table.c43
-rw-r--r--lib/common/writer.c52
4 files changed, 61 insertions, 103 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am
index 7e1ec63..e97085b 100644
--- a/lib/common/Makemodule.am
+++ b/lib/common/Makemodule.am
@@ -1,5 +1,5 @@
libcommon_a_SOURCES = lib/common/serialize_fstree.c lib/common/statistics.c
-libcommon_a_SOURCES += lib/common/write_export_table.c lib/common/inode_stat.c
+libcommon_a_SOURCES += lib/common/inode_stat.c
libcommon_a_SOURCES += lib/common/print_version.c lib/common/data_reader_dump.c
libcommon_a_SOURCES += lib/common/compress.c lib/common/comp_opt.c
libcommon_a_SOURCES += lib/common/data_writer.c include/common.h
diff --git a/lib/common/serialize_fstree.c b/lib/common/serialize_fstree.c
index 457c485..939b1c1 100644
--- a/lib/common/serialize_fstree.c
+++ b/lib/common/serialize_fstree.c
@@ -97,8 +97,7 @@ fail:
return NULL;
}
-static int serialize_tree_node(const char *filename, sqfs_dir_writer_t *dirwr,
- sqfs_meta_writer_t *im, sqfs_id_table_t *idtbl,
+static int serialize_tree_node(const char *filename, sqfs_writer_t *wr,
tree_node_t *n)
{
sqfs_inode_generic_t *inode;
@@ -107,7 +106,7 @@ static int serialize_tree_node(const char *filename, sqfs_dir_writer_t *dirwr,
int ret;
if (S_ISDIR(n->mode)) {
- inode = write_dir_entries(filename, dirwr, n);
+ inode = write_dir_entries(filename, wr->dirwr, n);
ret = SQFS_ERROR_INTERNAL;
} else if (S_ISREG(n->mode)) {
inode = n->data.file.user_ptr;
@@ -127,88 +126,56 @@ static int serialize_tree_node(const char *filename, sqfs_dir_writer_t *dirwr,
sqfs_inode_set_xattr_index(inode, n->xattr_idx);
- ret = sqfs_id_table_id_to_index(idtbl, n->uid,
+ ret = sqfs_id_table_id_to_index(wr->idtbl, n->uid,
&inode->base.uid_idx);
if (ret)
goto out;
- ret = sqfs_id_table_id_to_index(idtbl, n->gid,
+ ret = sqfs_id_table_id_to_index(wr->idtbl, n->gid,
&inode->base.gid_idx);
if (ret)
goto out;
- sqfs_meta_writer_get_position(im, &block, &offset);
+ sqfs_meta_writer_get_position(wr->im, &block, &offset);
n->inode_ref = (block << 16) | offset;
- ret = sqfs_meta_writer_write_inode(im, inode);
+ ret = sqfs_meta_writer_write_inode(wr->im, inode);
out:
free(inode);
return ret;
}
-int sqfs_serialize_fstree(const char *filename, sqfs_file_t *file,
- sqfs_super_t *super, fstree_t *fs,
- sqfs_compressor_t *cmp, sqfs_id_table_t *idtbl)
+int sqfs_serialize_fstree(const char *filename, sqfs_writer_t *wr)
{
- sqfs_meta_writer_t *im, *dm;
- sqfs_dir_writer_t *dirwr;
size_t i;
int ret;
- im = sqfs_meta_writer_create(file, cmp, 0);
- if (im == NULL) {
- ret = SQFS_ERROR_ALLOC;
- goto out_err;
- }
-
- dm = sqfs_meta_writer_create(file, cmp,
- SQFS_META_WRITER_KEEP_IN_MEMORY);
- if (dm == NULL) {
- ret = SQFS_ERROR_ALLOC;
- goto out_im;
- }
+ wr->super.inode_table_start = wr->outfile->get_size(wr->outfile);
- dirwr = sqfs_dir_writer_create(dm, 0);
- if (dirwr == NULL) {
- ret = SQFS_ERROR_ALLOC;
- goto out_dm;
- }
-
- super->inode_table_start = file->get_size(file);
-
- for (i = 0; i < fs->inode_tbl_size; ++i) {
- ret = serialize_tree_node(filename, dirwr, im, idtbl,
- fs->inode_table[i]);
+ for (i = 0; i < wr->fs.inode_tbl_size; ++i) {
+ ret = serialize_tree_node(filename, wr, wr->fs.inode_table[i]);
if (ret)
goto out;
}
- ret = sqfs_meta_writer_flush(im);
+ ret = sqfs_meta_writer_flush(wr->im);
if (ret)
goto out;
- ret = sqfs_meta_writer_flush(dm);
+ ret = sqfs_meta_writer_flush(wr->dm);
if (ret)
goto out;
- super->root_inode_ref = fs->root->inode_ref;
- super->directory_table_start = file->get_size(file);
+ wr->super.root_inode_ref = wr->fs.root->inode_ref;
+ wr->super.directory_table_start = wr->outfile->get_size(wr->outfile);
- ret = sqfs_meta_write_write_to_file(dm);
+ ret = sqfs_meta_write_write_to_file(wr->dm);
if (ret)
goto out;
ret = 0;
out:
- sqfs_dir_writer_destroy(dirwr);
-out_dm:
- sqfs_meta_writer_destroy(dm);
-out_im:
- sqfs_meta_writer_destroy(im);
-out_err:
- if (ret < 0) {
- sqfs_perror(filename, "storing filesystem tree",
- ret);
- }
+ if (ret)
+ sqfs_perror(filename, "storing filesystem tree", ret);
return ret;
}
diff --git a/lib/common/write_export_table.c b/lib/common/write_export_table.c
deleted file mode 100644
index c0d4993..0000000
--- a/lib/common/write_export_table.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * write_export_table.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "common.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-
-int write_export_table(const char *filename, sqfs_file_t *file,
- fstree_t *fs, sqfs_super_t *super,
- sqfs_compressor_t *cmp)
-{
- sqfs_u64 *table, start;
- size_t i, size;
- int ret;
-
- if (fs->inode_tbl_size < 1)
- return 0;
-
- table = calloc(1, sizeof(sqfs_u64) * fs->inode_tbl_size);
-
- if (table == NULL) {
- perror("Allocating NFS export table");
- return -1;
- }
-
- for (i = 0; i < fs->inode_tbl_size; ++i) {
- table[i] = htole64(fs->inode_table[i]->inode_ref);
- }
-
- size = sizeof(sqfs_u64) * fs->inode_tbl_size;
- ret = sqfs_write_table(file, cmp, table, size, &start);
- if (ret)
- sqfs_perror(filename, "writing NFS export table", ret);
-
- super->export_table_start = start;
- super->flags |= SQFS_FLAG_EXPORTABLE;
- free(table);
- return ret;
-}
diff --git a/lib/common/writer.c b/lib/common/writer.c
index d67f76b..0b7f378 100644
--- a/lib/common/writer.c
+++ b/lib/common/writer.c
@@ -68,7 +68,7 @@ void sqfs_writer_cfg_init(sqfs_writer_cfg_t *cfg)
int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
{
sqfs_compressor_config_t cfg;
- int ret;
+ int ret, flags;
if (compressor_cfg_init_options(&cfg, wrcfg->comp_id,
wrcfg->block_size,
@@ -149,14 +149,42 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
if (sqfs->xwr == NULL) {
sqfs_perror(wrcfg->filename, "creating xattr writer",
SQFS_ERROR_ALLOC);
- goto fail;
+ goto fail_id;
}
}
+ sqfs->im = sqfs_meta_writer_create(sqfs->outfile, sqfs->cmp, 0);
+ if (sqfs->im == NULL) {
+ fputs("Error creating inode meta data writer.\n", stderr);
+ goto fail_xwr;
+ }
+
+ sqfs->dm = sqfs_meta_writer_create(sqfs->outfile, sqfs->cmp,
+ SQFS_META_WRITER_KEEP_IN_MEMORY);
+ if (sqfs->dm == NULL) {
+ fputs("Error creating directory meta data writer.\n", stderr);
+ goto fail_im;
+ }
+
+ flags = 0;
+ if (wrcfg->exportable)
+ flags |= SQFS_DIR_WRITER_CREATE_EXPORT_TABLE;
+
+ sqfs->dirwr = sqfs_dir_writer_create(sqfs->dm, flags);
+ if (sqfs->dirwr == NULL) {
+ fputs("Error creating directory table writer.\n", stderr);
+ goto fail_dm;
+ }
+
return 0;
-fail:
+fail_dm:
+ sqfs_meta_writer_destroy(sqfs->dm);
+fail_im:
+ sqfs_meta_writer_destroy(sqfs->im);
+fail_xwr:
if (sqfs->xwr != NULL)
sqfs_xattr_writer_destroy(sqfs->xwr);
+fail_id:
sqfs_id_table_destroy(sqfs->idtbl);
fail_data:
sqfs_data_writer_destroy(sqfs->data);
@@ -191,10 +219,8 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
sqfs->super.inode_count = sqfs->fs.inode_tbl_size;
- if (sqfs_serialize_fstree(cfg->filename, sqfs->outfile, &sqfs->super,
- &sqfs->fs, sqfs->cmp, sqfs->idtbl)) {
+ if (sqfs_serialize_fstree(cfg->filename, sqfs))
return -1;
- }
if (!cfg->quiet)
fputs("Writing fragment table...\n", stdout);
@@ -209,10 +235,14 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
if (!cfg->quiet)
fputs("Writing export table...\n", stdout);
- if (write_export_table(cfg->filename, sqfs->outfile, &sqfs->fs,
- &sqfs->super, sqfs->cmp)) {
+
+ ret = sqfs_dir_writer_write_export_table(sqfs->dirwr,
+ sqfs->outfile, sqfs->cmp,
+ sqfs->fs.root->inode_num,
+ sqfs->fs.root->inode_ref,
+ &sqfs->super);
+ if (ret)
return -1;
- }
}
if (!cfg->quiet)
@@ -260,6 +290,10 @@ void sqfs_writer_cleanup(sqfs_writer_t *sqfs)
{
if (sqfs->xwr != NULL)
sqfs_xattr_writer_destroy(sqfs->xwr);
+
+ sqfs_dir_writer_destroy(sqfs->dirwr);
+ sqfs_meta_writer_destroy(sqfs->dm);
+ sqfs_meta_writer_destroy(sqfs->im);
sqfs_id_table_destroy(sqfs->idtbl);
sqfs_data_writer_destroy(sqfs->data);
sqfs->cmp->destroy(sqfs->cmp);