summaryrefslogtreecommitdiff
path: root/mkfs
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-28 23:08:39 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-28 23:34:17 +0200
commitd758950ac88c2c6759d1616ac8be2c70c9dcf761 (patch)
treee96860712abea784525a2b7feb3a690b443e1372 /mkfs
parent9bcb6edfe419d390acddc2ed7d0c04d37b753ac3 (diff)
Replace fstree/sqfshelper xattr code with sqfs_xattr_writer_t
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'mkfs')
-rw-r--r--mkfs/dirscan.c37
-rw-r--r--mkfs/mkfs.c47
-rw-r--r--mkfs/mkfs.h5
-rw-r--r--mkfs/selinux.c9
4 files changed, 71 insertions, 27 deletions
diff --git a/mkfs/dirscan.c b/mkfs/dirscan.c
index 160fbc3..e33e6f7 100644
--- a/mkfs/dirscan.c
+++ b/mkfs/dirscan.c
@@ -40,7 +40,7 @@ fail:
}
#ifdef HAVE_SYS_XATTR_H
-static int populate_xattr(fstree_t *fs, tree_node_t *node)
+static int populate_xattr(sqfs_xattr_writer_t *xwr, tree_node_t *node)
{
char *key, *value = NULL, *buffer = NULL;
ssize_t buflen, vallen, keylen;
@@ -74,7 +74,7 @@ static int populate_xattr(fstree_t *fs, tree_node_t *node)
goto fail;
if (vallen > 0) {
- value = alloc_string(vallen);
+ value = calloc(1, vallen);
if (value == NULL) {
perror("xattr value buffer");
goto fail;
@@ -86,9 +86,11 @@ static int populate_xattr(fstree_t *fs, tree_node_t *node)
goto fail;
}
- value[vallen] = 0;
- if (fstree_add_xattr(fs, node, key, value))
+ if (sqfs_xattr_writer_add(xwr, key, value, vallen)) {
+ fputs("Error storing xattr key-value pair\n",
+ stderr);
goto fail;
+ }
free(value);
value = NULL;
@@ -109,7 +111,8 @@ fail:
#endif
static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
- void *selinux_handle, unsigned int flags)
+ void *selinux_handle, sqfs_xattr_writer_t *xwr,
+ unsigned int flags)
{
char *extra = NULL, *path;
struct dirent *ent;
@@ -171,9 +174,14 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
goto fail;
}
+ if (sqfs_xattr_writer_begin(xwr)) {
+ fputs("error recoding xattr key-value pairs\n", stderr);
+ return -1;
+ }
+
#ifdef HAVE_SYS_XATTR_H
if (flags & DIR_SCAN_READ_XATTR) {
- if (populate_xattr(fs, n))
+ if (populate_xattr(xwr, n))
goto fail;
}
#endif
@@ -185,7 +193,8 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
goto fail;
}
- if (selinux_relable_node(selinux_handle, fs, n, path)) {
+ if (selinux_relable_node(selinux_handle, xwr,
+ n, path)) {
free(path);
goto fail;
}
@@ -193,6 +202,11 @@ 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);
+ return -1;
+ }
+
free(extra);
extra = NULL;
}
@@ -204,8 +218,10 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
if (pushd(n->name))
return -1;
- if (populate_dir(fs, n, devstart, selinux_handle, flags))
+ if (populate_dir(fs, n, devstart, selinux_handle,
+ xwr, flags)) {
return -1;
+ }
if (popd())
return -1;
@@ -222,7 +238,7 @@ fail:
}
int fstree_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
- unsigned int flags)
+ sqfs_xattr_writer_t *xwr, unsigned int flags)
{
struct stat sb;
int ret;
@@ -235,7 +251,8 @@ int fstree_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
if (pushd(path))
return -1;
- ret = populate_dir(fs, fs->root, sb.st_dev, selinux_handle, flags);
+ ret = populate_dir(fs, fs->root, sb.st_dev, selinux_handle,
+ xwr, flags);
if (popd())
ret = -1;
diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c
index c3a4e73..2facc51 100644
--- a/mkfs/mkfs.c
+++ b/mkfs/mkfs.c
@@ -89,7 +89,8 @@ static int pack_files(sqfs_data_writer_t *data, fstree_t *fs,
return restore_working_dir(opt);
}
-static int relabel_tree_dfs(fstree_t *fs, tree_node_t *n, void *selinux_handle)
+static int relabel_tree_dfs(sqfs_xattr_writer_t *xwr, tree_node_t *n,
+ void *selinux_handle)
{
char *path = fstree_get_path(n);
@@ -98,16 +99,26 @@ static int relabel_tree_dfs(fstree_t *fs, tree_node_t *n, void *selinux_handle)
return -1;
}
- if (selinux_relable_node(selinux_handle, fs, n, path)) {
+ if (sqfs_xattr_writer_begin(xwr)) {
+ fputs("error recoding xattr key-value pairs\n", stderr);
+ return -1;
+ }
+
+ if (selinux_relable_node(selinux_handle, xwr, n, path)) {
free(path);
return -1;
}
+ if (sqfs_xattr_writer_end(xwr, &n->xattr_idx)) {
+ fputs("error generating xattr index\n", stderr);
+ return -1;
+ }
+
free(path);
if (S_ISDIR(n->mode)) {
for (n = n->data.dir.children; n != NULL; n = n->next) {
- if (relabel_tree_dfs(fs, n, selinux_handle))
+ if (relabel_tree_dfs(xwr, n, selinux_handle))
return -1;
}
}
@@ -115,14 +126,15 @@ static int relabel_tree_dfs(fstree_t *fs, tree_node_t *n, void *selinux_handle)
return 0;
}
-static int read_fstree(fstree_t *fs, options_t *opt, void *selinux_handle)
+static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr,
+ void *selinux_handle)
{
FILE *fp;
int ret;
if (opt->infile == NULL) {
return fstree_from_dir(fs, opt->packdir, selinux_handle,
- opt->dirscan_flags);
+ xwr, opt->dirscan_flags);
}
fp = fopen(opt->infile, "rb");
@@ -144,7 +156,7 @@ static int read_fstree(fstree_t *fs, options_t *opt, void *selinux_handle)
return -1;
if (ret == 0 && selinux_handle != NULL)
- ret = relabel_tree_dfs(fs, fs->root, selinux_handle);
+ ret = relabel_tree_dfs(xwr, fs->root, selinux_handle);
return ret;
}
@@ -155,6 +167,7 @@ int main(int argc, char **argv)
sqfs_compressor_config_t cfg;
data_writer_stats_t stats;
sqfs_data_writer_t *data;
+ sqfs_xattr_writer_t *xwr;
sqfs_compressor_t *cmp;
sqfs_id_table_t *idtbl;
sqfs_file_t *outfile;
@@ -197,10 +210,16 @@ int main(int argc, char **argv)
goto out_outfile;
}
- if (read_fstree(&fs, &opt, sehnd)) {
+ xwr = sqfs_xattr_writer_create();
+ if (xwr == NULL) {
+ perror("creating Xattr writer");
+ goto out_outfile;
+ }
+
+ if (read_fstree(&fs, &opt, xwr, sehnd)) {
if (sehnd != NULL)
selinux_close_context_file(sehnd);
- goto out_outfile;
+ goto out_xwr;
}
if (sehnd != NULL) {
@@ -211,18 +230,16 @@ int main(int argc, char **argv)
tree_node_sort_recursive(fs.root);
if (fstree_gen_inode_table(&fs))
- goto out_outfile;
+ goto out_xwr;
fstree_gen_file_list(&fs);
super.inode_count = fs.inode_tbl_size - 2;
- fstree_xattr_deduplicate(&fs);
-
cmp = sqfs_compressor_create(&cfg);
if (cmp == NULL) {
fputs("Error creating compressor\n", stderr);
- goto out_outfile;
+ goto out_xwr;
}
ret = cmp->write_options(cmp, outfile);
@@ -257,8 +274,10 @@ int main(int argc, char **argv)
if (sqfs_id_table_write(idtbl, outfile, &super, cmp))
goto out_data;
- if (write_xattr(outfile, &fs, &super, cmp))
+ if (sqfs_xattr_writer_flush(xwr, outfile, &super, cmp)) {
+ fputs("Error writing xattr table\n", stderr);
goto out_data;
+ }
super.bytes_used = outfile->get_size(outfile);
@@ -276,6 +295,8 @@ out_data:
sqfs_data_writer_destroy(data);
out_cmp:
cmp->destroy(cmp);
+out_xwr:
+ sqfs_xattr_writer_destroy(xwr);
out_outfile:
outfile->destroy(outfile);
out_idtbl:
diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h
index 3a139a7..16dcac7 100644
--- a/mkfs/mkfs.h
+++ b/mkfs/mkfs.h
@@ -9,6 +9,7 @@
#include "config.h"
+#include "sqfs/xattr_writer.h"
#include "sqfs/meta_writer.h"
#include "sqfs/compress.h"
#include "sqfs/id_table.h"
@@ -69,12 +70,12 @@ enum {
void process_command_line(options_t *opt, int argc, char **argv);
int fstree_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
- unsigned int flags);
+ sqfs_xattr_writer_t *xwr, unsigned int flags);
void *selinux_open_context_file(const char *filename);
-int selinux_relable_node(void *sehnd, fstree_t *fs,
+int selinux_relable_node(void *sehnd, sqfs_xattr_writer_t *xwr,
tree_node_t *node, const char *path);
void selinux_close_context_file(void *sehnd);
diff --git a/mkfs/selinux.c b/mkfs/selinux.c
index a4cda71..d5de9a8 100644
--- a/mkfs/selinux.c
+++ b/mkfs/selinux.c
@@ -10,7 +10,7 @@
#define XATTR_VALUE_SELINUX "system_u:object_r:unlabeled_t:s0"
#ifdef WITH_SELINUX
-int selinux_relable_node(void *sehnd, fstree_t *fs,
+int selinux_relable_node(void *sehnd, sqfs_xattr_writer_t *xwr,
tree_node_t *node, const char *path)
{
char *context = NULL;
@@ -22,8 +22,13 @@ int selinux_relable_node(void *sehnd, fstree_t *fs,
goto fail;
}
- ret = fstree_add_xattr(fs, node, XATTR_NAME_SELINUX, context);
+ ret = sqfs_xattr_writer_add(xwr, XATTR_NAME_SELINUX,
+ context, strlen(context));
free(context);
+
+ if (ret)
+ fputs("Error relabeling tree node\n", stderr);
+
return ret;
fail:
perror("relabeling files");