summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/serialize_fstree.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqfshelper/serialize_fstree.c')
-rw-r--r--lib/sqfshelper/serialize_fstree.c105
1 files changed, 67 insertions, 38 deletions
diff --git a/lib/sqfshelper/serialize_fstree.c b/lib/sqfshelper/serialize_fstree.c
index 9efcb04..05a9d3a 100644
--- a/lib/sqfshelper/serialize_fstree.c
+++ b/lib/sqfshelper/serialize_fstree.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "sqfs/meta_writer.h"
+#include "sqfs/error.h"
#include "sqfs/inode.h"
#include "sqfs/dir.h"
@@ -29,7 +30,7 @@ static sqfs_inode_generic_t *tree_node_to_inode(tree_node_t *node)
inode = alloc_flex(sizeof(*inode), 1, extra);
if (inode == NULL) {
- perror("creating inode from file system tree node");
+ perror("creating inode");
return NULL;
}
@@ -66,7 +67,8 @@ static sqfs_inode_generic_t *tree_node_to_inode(tree_node_t *node)
return inode;
}
-static sqfs_inode_generic_t *write_dir_entries(sqfs_dir_writer_t *dirw,
+static sqfs_inode_generic_t *write_dir_entries(const char *filename,
+ sqfs_dir_writer_t *dirw,
tree_node_t *node)
{
sqfs_u32 xattr, parent_inode;
@@ -74,32 +76,38 @@ static sqfs_inode_generic_t *write_dir_entries(sqfs_dir_writer_t *dirw,
tree_node_t *it;
int ret;
- if (sqfs_dir_writer_begin(dirw, 0))
- return NULL;
+ ret = sqfs_dir_writer_begin(dirw, 0);
+ if (ret)
+ goto fail;
for (it = node->data.dir.children; it != NULL; it = it->next) {
ret = sqfs_dir_writer_add_entry(dirw, it->name, it->inode_num,
it->inode_ref, it->mode);
if (ret)
- return NULL;
+ goto fail;
}
- if (sqfs_dir_writer_end(dirw))
- return NULL;
+ ret = sqfs_dir_writer_end(dirw);
+ if (ret)
+ goto fail;
xattr = node->xattr_idx;
parent_inode = (node->parent == NULL) ? 0 : node->parent->inode_num;
inode = sqfs_dir_writer_create_inode(dirw, 0, xattr, parent_inode);
if (inode == NULL) {
- perror("creating inode");
- return NULL;
+ ret = SQFS_ERROR_ALLOC;
+ goto fail;
}
return inode;
+fail:
+ sqfs_perror(filename, "recoding directory entries", ret);
+ return NULL;
}
-int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs,
+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)
{
sqfs_inode_generic_t *inode;
@@ -112,17 +120,23 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs,
size_t i;
im = sqfs_meta_writer_create(file, cmp, 0);
- if (im == NULL)
- return -1;
+ 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)
+ if (dm == NULL) {
+ ret = SQFS_ERROR_ALLOC;
goto out_im;
+ }
dirwr = sqfs_dir_writer_create(dm);
- if (dirwr == NULL)
+ if (dirwr == NULL) {
+ ret = SQFS_ERROR_ALLOC;
goto out_dm;
+ }
super->inode_table_start = file->get_size(file);
@@ -130,16 +144,28 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs,
n = fs->inode_table[i];
if (S_ISDIR(n->mode)) {
- inode = write_dir_entries(dirwr, n);
+ inode = write_dir_entries(filename, dirwr, n);
+
+ if (inode == NULL) {
+ ret = 1;
+ goto out;
+ }
} else if (S_ISREG(n->mode)) {
inode = n->data.file.user_ptr;
n->data.file.user_ptr = NULL;
+
+ if (inode == NULL) {
+ ret = SQFS_ERROR_INTERNAL;
+ goto out;
+ }
} else {
inode = tree_node_to_inode(n);
- }
- if (inode == NULL)
- goto out;
+ if (inode == NULL) {
+ ret = SQFS_ERROR_ALLOC;
+ goto out;
+ }
+ }
inode->base.mode = n->mode;
inode->base.mod_time = n->mod_time;
@@ -147,37 +173,39 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs,
sqfs_inode_set_xattr_index(inode, n->xattr_idx);
- if (sqfs_id_table_id_to_index(idtbl, n->uid,
- &inode->base.uid_idx)) {
- goto fail_id;
- }
+ ret = sqfs_id_table_id_to_index(idtbl, n->uid,
+ &inode->base.uid_idx);
+ if (ret)
+ goto out;
- if (sqfs_id_table_id_to_index(idtbl, n->gid,
- &inode->base.gid_idx)) {
- goto fail_id;
- }
+ ret = sqfs_id_table_id_to_index(idtbl, n->gid,
+ &inode->base.gid_idx);
+ if (ret)
+ goto out;
sqfs_meta_writer_get_position(im, &block, &offset);
fs->inode_table[i]->inode_ref = (block << 16) | offset;
- if (sqfs_meta_writer_write_inode(im, inode)) {
- free(inode);
- goto out;
- }
-
+ ret = sqfs_meta_writer_write_inode(im, inode);
free(inode);
+
+ if (ret)
+ goto out;
}
- if (sqfs_meta_writer_flush(im))
+ ret = sqfs_meta_writer_flush(im);
+ if (ret)
goto out;
- if (sqfs_meta_writer_flush(dm))
+ ret = sqfs_meta_writer_flush(dm);
+ if (ret)
goto out;
super->root_inode_ref = fs->root->inode_ref;
super->directory_table_start = file->get_size(file);
- if (sqfs_meta_write_write_to_file(dm))
+ ret = sqfs_meta_write_write_to_file(dm);
+ if (ret)
goto out;
ret = 0;
@@ -187,9 +215,10 @@ 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);
+ }
return ret;
-fail_id:
- fputs("failed to allocate IDs\n", stderr);
- free(inode);
- goto out;
}