From 6261ef9505abb26d79cc48f24bce0d4de8419a45 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 5 Oct 2019 23:18:30 +0200 Subject: Improve error reporting for tar2sqfs and gensquashfs Signed-off-by: David Oberhollenzer --- include/highlevel.h | 9 ++-- lib/sqfshelper/data_writer.c | 30 ++++++++--- lib/sqfshelper/serialize_fstree.c | 105 +++++++++++++++++++++++------------- lib/sqfshelper/write_export_table.c | 5 +- lib/sqfshelper/writer.c | 61 +++++++++++++++------ mkfs/dirscan.c | 21 +++++--- mkfs/mkfs.c | 24 +++++---- mkfs/selinux.c | 2 +- tar/tar2sqfs.c | 37 +++++++------ 9 files changed, 193 insertions(+), 101 deletions(-) diff --git a/include/highlevel.h b/include/highlevel.h index f990fbf..068ae01 100644 --- a/include/highlevel.h +++ b/include/highlevel.h @@ -82,7 +82,8 @@ typedef struct { Returns 0 on success. Prints error messages to stderr on failure. */ -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); /* @@ -90,7 +91,8 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs, Returns 0 on success. Prints error messages to stderr on failure. */ -int write_export_table(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, +int write_export_table(const char *filename, sqfs_file_t *file, + fstree_t *fs, sqfs_super_t *super, sqfs_compressor_t *cmp); /* Print out fancy statistics for squashfs packing tools */ @@ -118,7 +120,8 @@ sqfs_file_t *sqfs_get_stdin_file(const sparse_map_t *map, sqfs_u64 size); void register_stat_hooks(sqfs_data_writer_t *data, data_writer_stats_t *stats); -int write_data_from_file(sqfs_data_writer_t *data, sqfs_inode_generic_t *inode, +int write_data_from_file(const char *filename, sqfs_data_writer_t *data, + sqfs_inode_generic_t *inode, sqfs_file_t *file, int flags); void sqfs_writer_cfg_init(sqfs_writer_cfg_t *cfg); diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c index aa51b26..960cf77 100644 --- a/lib/sqfshelper/data_writer.c +++ b/lib/sqfshelper/data_writer.c @@ -11,15 +11,19 @@ static sqfs_u8 buffer[4096]; -int write_data_from_file(sqfs_data_writer_t *data, sqfs_inode_generic_t *inode, - sqfs_file_t *file, int flags) +int write_data_from_file(const char *filename, sqfs_data_writer_t *data, + sqfs_inode_generic_t *inode, sqfs_file_t *file, + int flags) { sqfs_u64 filesz, offset; size_t diff; int ret; - if (sqfs_data_writer_begin_file(data, inode, flags)) + ret = sqfs_data_writer_begin_file(data, inode, flags); + if (ret) { + sqfs_perror(filename, "beginning file data blocks", ret); return -1; + } sqfs_inode_get_file_size(inode, &filesz); @@ -31,13 +35,23 @@ int write_data_from_file(sqfs_data_writer_t *data, sqfs_inode_generic_t *inode, } ret = file->read_at(file, offset, buffer, diff); - if (ret) - return ret; + if (ret) { + sqfs_perror(filename, "reading file range", ret); + return -1; + } ret = sqfs_data_writer_append(data, buffer, diff); - if (ret) - return ret; + if (ret) { + sqfs_perror(filename, "packing file data", ret); + return -1; + } + } + + ret = sqfs_data_writer_end_file(data); + if (ret) { + sqfs_perror(filename, "finishing file data", ret); + return -1; } - return sqfs_data_writer_end_file(data); + return 0; } 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; } diff --git a/lib/sqfshelper/write_export_table.c b/lib/sqfshelper/write_export_table.c index 857cb5e..154688e 100644 --- a/lib/sqfshelper/write_export_table.c +++ b/lib/sqfshelper/write_export_table.c @@ -12,7 +12,8 @@ #include #include -int write_export_table(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, +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; @@ -35,6 +36,8 @@ int write_export_table(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, 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; diff --git a/lib/sqfshelper/writer.c b/lib/sqfshelper/writer.c index 76a12fc..e914d2d 100644 --- a/lib/sqfshelper/writer.c +++ b/lib/sqfshelper/writer.c @@ -6,6 +6,8 @@ */ #include "config.h" +#include "sqfs/error.h" + #include "highlevel.h" #include "util.h" @@ -47,17 +49,24 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) goto fail_fs; } - if (sqfs_super_init(&sqfs->super, wrcfg->block_size, - sqfs->fs.defaults.st_mtime, wrcfg->comp_id)) { + ret = sqfs_super_init(&sqfs->super, wrcfg->block_size, + sqfs->fs.defaults.st_mtime, wrcfg->comp_id); + if (ret) { + sqfs_perror(wrcfg->filename, "initializing super block", ret); goto fail_cmp; } - if (sqfs_super_write(&sqfs->super, sqfs->outfile)) + ret = sqfs_super_write(&sqfs->super, sqfs->outfile); + if (ret) { + sqfs_perror(wrcfg->filename, "writing super block", ret); goto fail_cmp; + } ret = sqfs->cmp->write_options(sqfs->cmp, sqfs->outfile); - if (ret < 0) + if (ret < 0) { + sqfs_perror(wrcfg->filename, "writing compressor options", ret); goto fail_cmp; + } if (ret > 0) sqfs->super.flags |= SQFS_FLAG_COMPRESSOR_OPTIONS; @@ -75,14 +84,18 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) register_stat_hooks(sqfs->data, &sqfs->stats); sqfs->idtbl = sqfs_id_table_create(); - if (sqfs->idtbl == NULL) + if (sqfs->idtbl == NULL) { + sqfs_perror(wrcfg->filename, "creating ID table", + SQFS_ERROR_ALLOC); goto fail_data; + } if (!wrcfg->no_xattr) { sqfs->xwr = sqfs_xattr_writer_create(); if (sqfs->xwr == NULL) { - perror("creating xattr writer"); + sqfs_perror(wrcfg->filename, "creating xattr writer", + SQFS_ERROR_ALLOC); goto fail; } } @@ -105,8 +118,13 @@ fail_file: int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg) { - if (sqfs_data_writer_finish(sqfs->data)) + int ret; + + ret = sqfs_data_writer_finish(sqfs->data); + if (ret) { + sqfs_perror(cfg->filename, "finishing data blocks", ret); return -1; + } tree_node_sort_recursive(sqfs->fs.root); if (fstree_gen_inode_table(&sqfs->fs)) @@ -114,36 +132,45 @@ 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(sqfs->outfile, &sqfs->super, + if (sqfs_serialize_fstree(cfg->filename, sqfs->outfile, &sqfs->super, &sqfs->fs, sqfs->cmp, sqfs->idtbl)) { return -1; } - if (sqfs_data_writer_write_fragment_table(sqfs->data, &sqfs->super)) + ret = sqfs_data_writer_write_fragment_table(sqfs->data, &sqfs->super); + if (ret) { + sqfs_perror(cfg->filename, "writing fragment table", ret); return -1; + } if (cfg->exportable) { - if (write_export_table(sqfs->outfile, &sqfs->fs, + if (write_export_table(cfg->filename, sqfs->outfile, &sqfs->fs, &sqfs->super, sqfs->cmp)) { return -1; } } - if (sqfs_id_table_write(sqfs->idtbl, sqfs->outfile, - &sqfs->super, sqfs->cmp)) { + ret = sqfs_id_table_write(sqfs->idtbl, sqfs->outfile, + &sqfs->super, sqfs->cmp); + if (ret) { + sqfs_perror(cfg->filename, "writing ID table", ret); return -1; } - if (sqfs_xattr_writer_flush(sqfs->xwr, sqfs->outfile, - &sqfs->super, sqfs->cmp)) { - fputs("Error writing xattr table\n", stderr); + ret = sqfs_xattr_writer_flush(sqfs->xwr, sqfs->outfile, + &sqfs->super, sqfs->cmp); + if (ret) { + sqfs_perror(cfg->filename, "writing extended attributes", ret); return -1; } sqfs->super.bytes_used = sqfs->outfile->get_size(sqfs->outfile); - if (sqfs_super_write(&sqfs->super, sqfs->outfile)) - return 0; + ret = sqfs_super_write(&sqfs->super, sqfs->outfile); + if (ret) { + sqfs_perror(cfg->filename, "updating super block", ret); + return -1; + } if (padd_sqfs(sqfs->outfile, sqfs->super.bytes_used, cfg->devblksize)) { diff --git a/mkfs/dirscan.c b/mkfs/dirscan.c index 6d6b745..06f4d79 100644 --- a/mkfs/dirscan.c +++ b/mkfs/dirscan.c @@ -44,6 +44,7 @@ static int populate_xattr(sqfs_xattr_writer_t *xwr, tree_node_t *node) { char *key, *value = NULL, *buffer = NULL; ssize_t buflen, vallen, keylen; + int ret; buflen = listxattr(node->name, NULL, 0); @@ -76,19 +77,22 @@ static int populate_xattr(sqfs_xattr_writer_t *xwr, tree_node_t *node) if (vallen > 0) { value = calloc(1, vallen); if (value == NULL) { - perror("xattr value buffer"); + perror("allocating xattr value buffer"); goto fail; } vallen = getxattr(node->name, key, value, vallen); if (vallen == -1) { - perror("getxattr"); + fprintf(stderr, "%s: getxattr: %s\n", + node->name, strerror(errno)); goto fail; } - if (sqfs_xattr_writer_add(xwr, key, value, vallen)) { - fputs("Error storing xattr key-value pair\n", - stderr); + ret = sqfs_xattr_writer_add(xwr, key, value, vallen); + if (ret) { + sqfs_perror(node->name, + "storing xattr key-value pairs", + ret); goto fail; } @@ -119,6 +123,7 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart, struct stat sb; tree_node_t *n; DIR *dir; + int ret; dir = opendir("."); if (dir == NULL) { @@ -202,8 +207,10 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart, free(path); } - if (sqfs_xattr_writer_end(xwr, &n->xattr_idx)) { - fputs("error generating xattr index\n", stderr); + ret = sqfs_xattr_writer_end(xwr, &n->xattr_idx); + if (ret) { + sqfs_perror(n->name, + "completing xattr key-value pairs", ret); return -1; } diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 16f9bc0..b000652 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -73,7 +73,8 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs, fi->user_ptr = inode; - ret = write_data_from_file(data, inode, file, 0); + ret = write_data_from_file(fi->input_file, data, + inode, file, 0); file->destroy(file); if (ret) @@ -86,18 +87,20 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs, return restore_working_dir(opt); } -static int relabel_tree_dfs(sqfs_xattr_writer_t *xwr, tree_node_t *n, - void *selinux_handle) +static int relabel_tree_dfs(const char *filename, sqfs_xattr_writer_t *xwr, + tree_node_t *n, void *selinux_handle) { char *path = fstree_get_path(n); + int ret; if (path == NULL) { perror("getting absolute node path for SELinux relabeling"); return -1; } - if (sqfs_xattr_writer_begin(xwr)) { - fputs("error recoding xattr key-value pairs\n", stderr); + ret = sqfs_xattr_writer_begin(xwr); + if (ret) { + sqfs_perror(filename, "recording xattr key-value pairs", ret); return -1; } @@ -106,8 +109,10 @@ static int relabel_tree_dfs(sqfs_xattr_writer_t *xwr, tree_node_t *n, return -1; } - if (sqfs_xattr_writer_end(xwr, &n->xattr_idx)) { - fputs("error generating xattr index\n", stderr); + ret = sqfs_xattr_writer_end(xwr, &n->xattr_idx); + if (ret) { + sqfs_perror(filename, "flushing completed key-value pairs", + ret); return -1; } @@ -115,7 +120,7 @@ static int relabel_tree_dfs(sqfs_xattr_writer_t *xwr, tree_node_t *n, if (S_ISDIR(n->mode)) { for (n = n->data.dir.children; n != NULL; n = n->next) { - if (relabel_tree_dfs(xwr, n, selinux_handle)) + if (relabel_tree_dfs(filename, xwr, n, selinux_handle)) return -1; } } @@ -144,7 +149,8 @@ static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr, fclose(fp); if (ret == 0 && selinux_handle != NULL) - ret = relabel_tree_dfs(xwr, fs->root, selinux_handle); + ret = relabel_tree_dfs(opt->cfg.filename, xwr, + fs->root, selinux_handle); return ret; } diff --git a/mkfs/selinux.c b/mkfs/selinux.c index f2c8fee..678723b 100644 --- a/mkfs/selinux.c +++ b/mkfs/selinux.c @@ -27,7 +27,7 @@ int selinux_relable_node(void *sehnd, sqfs_xattr_writer_t *xwr, free(context); if (ret) - fputs("Error relabeling tree node\n", stderr); + sqfs_perror(node->name, "storing SELinux xattr", ret); return ret; fail: diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index 9533229..375a413 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -243,7 +243,7 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi, } } - ret = write_data_from_file(sqfs.data, inode, file, 0); + ret = write_data_from_file(hdr->name, sqfs.data, inode, file, 0); file->destroy(file); sqfs.stats.bytes_read += filesize; @@ -259,35 +259,38 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi, static int copy_xattr(tree_node_t *node, tar_header_decoded_t *hdr) { tar_xattr_t *xattr; + int ret; - if (sqfs_xattr_writer_begin(sqfs.xwr)) { - fputs("Error beginning xattr block\n", stderr); + ret = sqfs_xattr_writer_begin(sqfs.xwr); + if (ret) { + sqfs_perror(hdr->name, "beginning xattr block", ret); return -1; } for (xattr = hdr->xattr; xattr != NULL; xattr = xattr->next) { if (!sqfs_has_xattr(xattr->key)) { - if (dont_skip) { - fprintf(stderr, "Cannot encode xattr key '%s' " - "in squashfs\n", xattr->key); - return -1; - } + fprintf(stderr, "%s: squashfs does not " + "support xattr prefix of %s\n", + dont_skip ? "ERROR" : "WARNING", + xattr->key); - fprintf(stderr, "WARNING: squashfs does not " - "support xattr prefix of %s\n", xattr->key); + if (dont_skip) + return -1; continue; } - if (sqfs_xattr_writer_add(sqfs.xwr, xattr->key, xattr->value, - strlen(xattr->value))) { - fputs("Error converting xattr key-value pair\n", - stderr); + ret = sqfs_xattr_writer_add(sqfs.xwr, xattr->key, xattr->value, + strlen(xattr->value)); + if (ret) { + sqfs_perror(hdr->name, "storing xattr key-value pair", + ret); return -1; } } - if (sqfs_xattr_writer_end(sqfs.xwr, &node->xattr_idx)) { - fputs("Error completing xattr block\n", stderr); + ret = sqfs_xattr_writer_end(sqfs.xwr, &node->xattr_idx); + if (ret) { + sqfs_perror(hdr->name, "completing xattr block", ret); return -1; } @@ -372,7 +375,7 @@ static int process_tar_ball(void) if (skip) { fprintf(stderr, "%s: broken sparse " - "file layout)\n", hdr.name); + "file layout\n", hdr.name); } } -- cgit v1.2.3