Skip to content

Commit

Permalink
Cleanup: gensquashfs: merge xattr scanning code
Browse files Browse the repository at this point in the history
The fstree from file and directory xattr scanning code essentially do
the same thing now. Except the later also _optionally_ reads xattrs
from a directory source. Merge the two code paths.

Signed-off-by: David Oberhollenzer <[email protected]>
  • Loading branch information
AgentD committed Oct 20, 2023
1 parent 084deb9 commit dce63f7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 72 deletions.
2 changes: 1 addition & 1 deletion bin/gensquashfs/Makemodule.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
gensquashfs_SOURCES = bin/gensquashfs/src/mkfs.c bin/gensquashfs/src/mkfs.h \
bin/gensquashfs/src/options.c bin/gensquashfs/src/selinux.c \
bin/gensquashfs/src/dirscan_xattr.c bin/gensquashfs/src/filemap_xattr.c\
bin/gensquashfs/src/apply_xattr.c bin/gensquashfs/src/filemap_xattr.c\
bin/gensquashfs/src/fstree_from_file.c \
bin/gensquashfs/src/fstree_from_dir.c \
bin/gensquashfs/src/sort_by_file.c bin/gensquashfs/src/glob.c
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* dirscan_xattr.c
* apply_xattr.c
*
* Copyright (C) 2019 David Oberhollenzer <[email protected]>
*/
Expand Down Expand Up @@ -207,8 +207,8 @@ static int xattr_xcan_dfs(const char *path_prefix, void *selinux_handle,
return ret;
}

int xattrs_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr)
int apply_xattrs(fstree_t *fs, const char *path, void *selinux_handle,
void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr)
{
if (xwr == NULL)
return 0;
Expand Down
70 changes: 4 additions & 66 deletions bin/gensquashfs/src/mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,66 +93,6 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs,
return 0;
}

static int relabel_tree_dfs(const char *filename, sqfs_xattr_writer_t *xwr,
tree_node_t *n, void *selinux_handle,
void *xattrmap)
{
char *path = fstree_get_path(n);
int ret;

if (path == NULL) {
perror("getting absolute node path for SELinux relabeling");
return -1;
}

ret = sqfs_xattr_writer_begin(xwr, 0);
if (ret) {
sqfs_perror(filename, "recording xattr key-value pairs", ret);
return -1;
}

if (xattrmap != NULL)
ret = xattr_apply_map_file(path, xattrmap, xwr);

if (ret == 0 && selinux_handle != NULL)
ret = selinux_relable_node(selinux_handle, xwr, n, path);

free(path);
if (ret == 0)
ret = sqfs_xattr_writer_end(xwr, &n->xattr_idx);

if (ret) {
sqfs_perror(filename, "flushing completed key-value pairs",
ret);
return -1;
}

if (S_ISDIR(n->mode)) {
for (n = n->data.children; n != NULL; n = n->next) {
if (relabel_tree_dfs(filename, xwr, n,
selinux_handle, xattrmap)) {
return -1;
}
}
}

return 0;
}

static int read_fstree(fstree_t *fs, options_t *opt, sqfs_xattr_writer_t *xwr,
void *selinux_handle, void *xattrmap)
{
int ret;

ret = fstree_from_file(fs, opt->infile, opt->packdir);

if (ret == 0 && (selinux_handle != NULL || xattrmap != NULL))
ret = relabel_tree_dfs(opt->cfg.filename, xwr,
fs->root, selinux_handle, xattrmap);

return ret;
}

static void override_owner_dfs(const options_t *opt, tree_node_t *n)
{
if (opt->force_uid)
Expand Down Expand Up @@ -219,7 +159,7 @@ int main(int argc, char **argv)
if (ret != 0)
goto out;
} else {
if (read_fstree(&sqfs.fs, &opt, sqfs.xwr, sehnd, xattrmap))
if (fstree_from_file(&sqfs.fs, opt.infile, opt.packdir))
goto out;
}

Expand All @@ -229,11 +169,9 @@ int main(int argc, char **argv)
if (fstree_post_process(&sqfs.fs))
goto out;

if (opt.infile == NULL) {
if (xattrs_from_dir(&sqfs.fs, opt.packdir, sehnd, xattrmap,
sqfs.xwr, opt.scan_xattr)) {
goto out;
}
if (apply_xattrs(&sqfs.fs, opt.packdir, sehnd, xattrmap,
sqfs.xwr, opt.infile == NULL && opt.scan_xattr)) {
goto out;
}

if (sortfile != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions bin/gensquashfs/src/mkfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ struct XattrMap {

void process_command_line(options_t *opt, int argc, char **argv);

int xattrs_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr);
int apply_xattrs(fstree_t *fs, const char *path, void *selinux_handle,
void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr);

void *xattr_open_map_file(const char *path);

Expand Down

0 comments on commit dce63f7

Please sign in to comment.