From 5e4d53c713d3f01ae2d24a7d2311d65761b143dc Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 28 Sep 2019 21:04:20 +0200 Subject: Move fstree_from_dir to gensquashfs code It's actually the only user and the dir-scanner xattr code is going to get a lot closer integerated with libsquashfs. Signed-off-by: David Oberhollenzer --- include/fstree.h | 15 --- lib/fstree/Makemodule.am | 2 +- lib/fstree/fstree_from_dir.c | 243 ------------------------------------------- mkfs/Makemodule.am | 1 + mkfs/dirscan.c | 228 ++++++++++++++++++++++++++++++++++++++++ mkfs/mkfs.h | 15 +++ 6 files changed, 245 insertions(+), 259 deletions(-) delete mode 100644 lib/fstree/fstree_from_dir.c create mode 100644 mkfs/dirscan.c diff --git a/include/fstree.h b/include/fstree.h index 337b598..110ad1f 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -26,14 +26,6 @@ typedef struct dir_info_t dir_info_t; typedef struct fstree_t fstree_t; typedef struct tree_xattr_t tree_xattr_t; -enum { - DIR_SCAN_KEEP_TIME = 0x01, - - DIR_SCAN_ONE_FILESYSTEM = 0x02, - - DIR_SCAN_READ_XATTR = 0x04, -}; - /* Encapsulates a set of key-value pairs attached to a tree_node_t */ struct tree_xattr_t { /* Number of key-value pairs */ @@ -230,13 +222,6 @@ void fstree_xattr_deduplicate(fstree_t *fs); */ int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp); -/* - Recursively scan a directory and generate a file system tree from it. - - Returns 0 on success, prints errors to stderr. - */ -int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags); - /* Add labels from an SELinux labeling file to all tree nodes. Returns 0 on success. Internally prints errors to stderr. */ int fstree_relabel_selinux(fstree_t *fs, const char *filename); diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am index 6d3498a..960ab1d 100644 --- a/lib/fstree/Makemodule.am +++ b/lib/fstree/Makemodule.am @@ -1,5 +1,5 @@ libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c -libfstree_a_SOURCES += lib/fstree/fstree_sort.c lib/fstree/fstree_from_dir.c +libfstree_a_SOURCES += lib/fstree/fstree_sort.c libfstree_a_SOURCES += lib/fstree/gen_inode_table.c lib/fstree/get_path.c libfstree_a_SOURCES += lib/fstree/mknode.c libfstree_a_SOURCES += lib/fstree/add_by_path.c lib/fstree/xattr.c diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c deleted file mode 100644 index d68fafa..0000000 --- a/lib/fstree/fstree_from_dir.c +++ /dev/null @@ -1,243 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * fstree_from_dir.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#include "fstree.h" -#include "util.h" - -#ifdef HAVE_SYS_XATTR_H -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -static char *get_file_path(tree_node_t *n, const char *name) -{ - char *ptr, *new; - int ret; - - if (n->parent == NULL) { - ptr = strdup(name); - if (ptr == NULL) - goto fail; - return ptr; - } - - ptr = fstree_get_path(n); - if (ptr == NULL) - goto fail; - - ret = canonicalize_name(ptr); - assert(ret == 0); - - new = realloc(ptr, strlen(ptr) + strlen(name) + 2); - if (new == NULL) - goto fail; - - ptr = new; - strcat(ptr, "/"); - strcat(ptr, name); - return ptr; -fail: - perror("getting absolute file path"); - free(ptr); - return NULL; -} - -#ifdef HAVE_SYS_XATTR_H -static int populate_xattr(fstree_t *fs, tree_node_t *node) -{ - char *key, *value = NULL, *buffer = NULL; - ssize_t buflen, vallen, keylen; - - buflen = listxattr(node->name, NULL, 0); - - if (buflen < 0) { - perror("listxattr"); - return -1; - } - - if (buflen == 0) - return 0; - - buffer = malloc(buflen); - if (buffer == NULL) { - perror("xattr name buffer"); - return -1; - } - - buflen = listxattr(node->name, buffer, buflen); - if (buflen == -1) { - perror("listxattr"); - goto fail; - } - - key = buffer; - while (buflen > 0) { - vallen = getxattr(node->name, key, NULL, 0); - if (vallen == -1) - goto fail; - - if (vallen > 0) { - value = alloc_string(vallen); - if (value == NULL) { - perror("xattr value buffer"); - goto fail; - } - - vallen = getxattr(node->name, key, value, vallen); - if (vallen == -1) { - perror("getxattr"); - goto fail; - } - - value[vallen] = 0; - if (fstree_add_xattr(fs, node, key, value)) - goto fail; - - free(value); - value = NULL; - } - - keylen = strlen(key) + 1; - buflen -= keylen; - key += keylen; - } - - free(buffer); - return 0; -fail: - free(value); - free(buffer); - return -1; -} -#endif - -static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart, - unsigned int flags) -{ - char *extra = NULL; - struct dirent *ent; - struct stat sb; - tree_node_t *n; - DIR *dir; - - dir = opendir("."); - if (dir == NULL) { - perror("opendir"); - return -1; - } - - for (;;) { - errno = 0; - ent = readdir(dir); - - if (ent == NULL) { - if (errno) { - perror("readdir"); - goto fail; - } - break; - } - - if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, ".")) - continue; - - if (fstatat(AT_FDCWD, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) { - perror(ent->d_name); - goto fail; - } - - if ((flags & DIR_SCAN_ONE_FILESYSTEM) && sb.st_dev != devstart) - continue; - - if (S_ISLNK(sb.st_mode)) { - extra = alloc_string(sb.st_size); - if (extra == NULL) - goto fail_rdlink; - - if (readlink(ent->d_name, extra, sb.st_size) < 0) - goto fail_rdlink; - - extra[sb.st_size] = '\0'; - } else if (S_ISREG(sb.st_mode)) { - extra = get_file_path(root, ent->d_name); - if (extra == NULL) - goto fail; - } - - if (!(flags & DIR_SCAN_KEEP_TIME)) - sb.st_mtim = fs->defaults.st_mtim; - - n = fstree_mknode(root, ent->d_name, strlen(ent->d_name), - extra, &sb); - if (n == NULL) { - perror("creating tree node"); - goto fail; - } - -#ifdef HAVE_SYS_XATTR_H - if (flags & DIR_SCAN_READ_XATTR) { - if (populate_xattr(fs, n)) - goto fail; - } -#endif - - free(extra); - extra = NULL; - } - - closedir(dir); - - for (n = root->data.dir.children; n != NULL; n = n->next) { - if (S_ISDIR(n->mode)) { - if (pushd(n->name)) - return -1; - - if (populate_dir(fs, n, devstart, flags)) - return -1; - - if (popd()) - return -1; - } - } - - return 0; -fail_rdlink: - perror("readlink"); -fail: - closedir(dir); - free(extra); - return -1; -} - -int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags) -{ - struct stat sb; - int ret; - - if (stat(path, &sb)) { - perror(path); - return -1; - } - - if (pushd(path)) - return -1; - - ret = populate_dir(fs, fs->root, sb.st_dev, flags); - - if (popd()) - ret = -1; - - return ret; -} diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am index 1955852..f1e0d0d 100644 --- a/mkfs/Makemodule.am +++ b/mkfs/Makemodule.am @@ -1,4 +1,5 @@ gensquashfs_SOURCES = mkfs/mkfs.c mkfs/mkfs.h mkfs/options.c +gensquashfs_SOURCES += mkfs/dirscan.c gensquashfs_LDADD = libsqfshelper.a libsquashfs.la libfstree.a libutil.la gensquashfs_LDADD += $(LIBSELINUX_LIBS) gensquashfs_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/mkfs/dirscan.c b/mkfs/dirscan.c new file mode 100644 index 0000000..5cb955a --- /dev/null +++ b/mkfs/dirscan.c @@ -0,0 +1,228 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * fstree_from_dir.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "mkfs.h" + +static char *get_file_path(tree_node_t *n, const char *name) +{ + char *ptr, *new; + int ret; + + if (n->parent == NULL) { + ptr = strdup(name); + if (ptr == NULL) + goto fail; + return ptr; + } + + ptr = fstree_get_path(n); + if (ptr == NULL) + goto fail; + + ret = canonicalize_name(ptr); + assert(ret == 0); + + new = realloc(ptr, strlen(ptr) + strlen(name) + 2); + if (new == NULL) + goto fail; + + ptr = new; + strcat(ptr, "/"); + strcat(ptr, name); + return ptr; +fail: + perror("getting absolute file path"); + free(ptr); + return NULL; +} + +#ifdef HAVE_SYS_XATTR_H +static int populate_xattr(fstree_t *fs, tree_node_t *node) +{ + char *key, *value = NULL, *buffer = NULL; + ssize_t buflen, vallen, keylen; + + buflen = listxattr(node->name, NULL, 0); + + if (buflen < 0) { + perror("listxattr"); + return -1; + } + + if (buflen == 0) + return 0; + + buffer = malloc(buflen); + if (buffer == NULL) { + perror("xattr name buffer"); + return -1; + } + + buflen = listxattr(node->name, buffer, buflen); + if (buflen == -1) { + perror("listxattr"); + goto fail; + } + + key = buffer; + while (buflen > 0) { + vallen = getxattr(node->name, key, NULL, 0); + if (vallen == -1) + goto fail; + + if (vallen > 0) { + value = alloc_string(vallen); + if (value == NULL) { + perror("xattr value buffer"); + goto fail; + } + + vallen = getxattr(node->name, key, value, vallen); + if (vallen == -1) { + perror("getxattr"); + goto fail; + } + + value[vallen] = 0; + if (fstree_add_xattr(fs, node, key, value)) + goto fail; + + free(value); + value = NULL; + } + + keylen = strlen(key) + 1; + buflen -= keylen; + key += keylen; + } + + free(buffer); + return 0; +fail: + free(value); + free(buffer); + return -1; +} +#endif + +static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart, + unsigned int flags) +{ + char *extra = NULL; + struct dirent *ent; + struct stat sb; + tree_node_t *n; + DIR *dir; + + dir = opendir("."); + if (dir == NULL) { + perror("opendir"); + return -1; + } + + for (;;) { + errno = 0; + ent = readdir(dir); + + if (ent == NULL) { + if (errno) { + perror("readdir"); + goto fail; + } + break; + } + + if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, ".")) + continue; + + if (fstatat(AT_FDCWD, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) { + perror(ent->d_name); + goto fail; + } + + if ((flags & DIR_SCAN_ONE_FILESYSTEM) && sb.st_dev != devstart) + continue; + + if (S_ISLNK(sb.st_mode)) { + extra = alloc_string(sb.st_size); + if (extra == NULL) + goto fail_rdlink; + + if (readlink(ent->d_name, extra, sb.st_size) < 0) + goto fail_rdlink; + + extra[sb.st_size] = '\0'; + } else if (S_ISREG(sb.st_mode)) { + extra = get_file_path(root, ent->d_name); + if (extra == NULL) + goto fail; + } + + if (!(flags & DIR_SCAN_KEEP_TIME)) + sb.st_mtim = fs->defaults.st_mtim; + + n = fstree_mknode(root, ent->d_name, strlen(ent->d_name), + extra, &sb); + if (n == NULL) { + perror("creating tree node"); + goto fail; + } + +#ifdef HAVE_SYS_XATTR_H + if (flags & DIR_SCAN_READ_XATTR) { + if (populate_xattr(fs, n)) + goto fail; + } +#endif + + free(extra); + extra = NULL; + } + + closedir(dir); + + for (n = root->data.dir.children; n != NULL; n = n->next) { + if (S_ISDIR(n->mode)) { + if (pushd(n->name)) + return -1; + + if (populate_dir(fs, n, devstart, flags)) + return -1; + + if (popd()) + return -1; + } + } + + return 0; +fail_rdlink: + perror("readlink"); +fail: + closedir(dir); + free(extra); + return -1; +} + +int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags) +{ + struct stat sb; + int ret; + + if (stat(path, &sb)) { + perror(path); + return -1; + } + + if (pushd(path)) + return -1; + + ret = populate_dir(fs, fs->root, sb.st_dev, flags); + + if (popd()) + ret = -1; + + return ret; +} diff --git a/mkfs/mkfs.h b/mkfs/mkfs.h index 0e374dc..bce98d8 100644 --- a/mkfs/mkfs.h +++ b/mkfs/mkfs.h @@ -19,12 +19,17 @@ #include "fstree.h" #include "util.h" +#ifdef HAVE_SYS_XATTR_H +#include +#endif + #include #include #include #include #include #include +#include #include #include #include @@ -48,6 +53,16 @@ typedef struct { char *comp_extra; } options_t; +enum { + DIR_SCAN_KEEP_TIME = 0x01, + + DIR_SCAN_ONE_FILESYSTEM = 0x02, + + DIR_SCAN_READ_XATTR = 0x04, +}; + void process_command_line(options_t *opt, int argc, char **argv); +int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags); + #endif /* MKFS_H */ -- cgit v1.2.3