aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/io/src')
-rw-r--r--lib/io/src/dir_tree_iterator.c310
-rw-r--r--lib/io/src/unix/dir_iterator.c204
-rw-r--r--lib/io/src/win32/dir_iterator.c218
3 files changed, 732 insertions, 0 deletions
diff --git a/lib/io/src/dir_tree_iterator.c b/lib/io/src/dir_tree_iterator.c
new file mode 100644
index 0000000..b05eeb3
--- /dev/null
+++ b/lib/io/src/dir_tree_iterator.c
@@ -0,0 +1,310 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * dir_tree_iterator.c
+ *
+ * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "io/dir_tree_iterator.h"
+#include "util/util.h"
+#include "sqfs/error.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct dir_stack_t {
+ struct dir_stack_t *next;
+ dir_iterator_t *dir;
+ char name[];
+} dir_stack_t;
+
+typedef struct {
+ dir_iterator_t base;
+
+ dir_tree_cfg_t cfg;
+ int state;
+ dir_stack_t *top;
+} dir_tree_iterator_t;
+
+static void pop(dir_tree_iterator_t *it)
+{
+ if (it->top != NULL) {
+ dir_stack_t *ent = it->top;
+ it->top = it->top->next;
+
+ sqfs_drop(ent->dir);
+ free(ent);
+ }
+}
+
+static int push(dir_tree_iterator_t *it, const char *name, dir_iterator_t *dir)
+{
+ dir_stack_t *ent = alloc_flex(sizeof(*ent), 1, strlen(name) + 1);
+
+ if (ent == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ strcpy(ent->name, name);
+ ent->dir = sqfs_grab(dir);
+ ent->next = it->top;
+ it->top = ent;
+ return 0;
+}
+
+static bool should_skip(const dir_tree_iterator_t *dir, const dir_entry_t *ent)
+{
+ unsigned int type_mask;
+
+ if (!strcmp(ent->name, ".") || !strcmp(ent->name, ".."))
+ return true;
+
+ if ((dir->cfg.flags & DIR_SCAN_ONE_FILESYSTEM)) {
+ if (ent->dev != ((const dir_iterator_t *)dir)->dev)
+ return true;
+ }
+
+ switch (ent->mode & S_IFMT) {
+ case S_IFSOCK: type_mask = DIR_SCAN_NO_SOCK; break;
+ case S_IFLNK: type_mask = DIR_SCAN_NO_SLINK; break;
+ case S_IFREG: type_mask = DIR_SCAN_NO_FILE; break;
+ case S_IFBLK: type_mask = DIR_SCAN_NO_BLK; break;
+ case S_IFCHR: type_mask = DIR_SCAN_NO_CHR; break;
+ case S_IFIFO: type_mask = DIR_SCAN_NO_FIFO; break;
+ default: type_mask = 0; break;
+ }
+
+ return (dir->cfg.flags & type_mask) != 0;
+}
+
+static dir_entry_t *expand_path(const dir_tree_iterator_t *it, dir_entry_t *ent)
+{
+ size_t slen = strlen(ent->name) + 1, plen = 0;
+ dir_stack_t *sit;
+ char *dst;
+
+ for (sit = it->top; sit != NULL; sit = sit->next) {
+ if (sit->name[0] != '\0')
+ plen += strlen(sit->name) + 1;
+ }
+
+ if (it->cfg.prefix != NULL && it->cfg.prefix[0] != '\0')
+ plen += strlen(it->cfg.prefix) + 1;
+
+ if (plen > 0) {
+ void *new = realloc(ent, sizeof(*ent) + plen + slen);
+ if (new == NULL) {
+ free(ent);
+ return NULL;
+ }
+
+ ent = new;
+ memmove(ent->name + plen, ent->name, slen);
+ dst = ent->name + plen;
+
+ for (sit = it->top; sit != NULL; sit = sit->next) {
+ size_t len = strlen(sit->name);
+ if (len > 0) {
+ *(--dst) = '/';
+ dst -= len;
+ memcpy(dst, sit->name, len);
+ }
+ }
+
+ if (it->cfg.prefix != NULL && it->cfg.prefix[0] != '\0') {
+ size_t len = strlen(it->cfg.prefix);
+ memcpy(ent->name, it->cfg.prefix, len);
+ ent->name[len] = '/';
+ }
+ }
+
+ return ent;
+}
+
+static void apply_changes(const dir_tree_iterator_t *it, dir_entry_t *ent)
+{
+ if (!(it->cfg.flags & DIR_SCAN_KEEP_TIME))
+ ent->mtime = it->cfg.def_mtime;
+
+ if (!(it->cfg.flags & DIR_SCAN_KEEP_UID))
+ ent->uid = it->cfg.def_uid;
+
+ if (!(it->cfg.flags & DIR_SCAN_KEEP_GID))
+ ent->gid = it->cfg.def_gid;
+
+ if (!(it->cfg.flags & DIR_SCAN_KEEP_MODE)) {
+ ent->mode &= ~(07777);
+ ent->mode |= it->cfg.def_mode & 07777;
+ }
+}
+
+/*****************************************************************************/
+
+static void destroy(sqfs_object_t *obj)
+{
+ dir_tree_iterator_t *it = (dir_tree_iterator_t *)obj;
+
+ while (it->top != NULL)
+ pop(it);
+
+ free(it);
+}
+
+static int next(dir_iterator_t *base, dir_entry_t **out)
+{
+ dir_tree_iterator_t *it = (dir_tree_iterator_t *)base;
+ dir_iterator_t *sub;
+ dir_entry_t *ent;
+ int ret;
+retry:
+ *out = NULL;
+ sub = NULL;
+ ent = NULL;
+
+ if (it->state != 0)
+ return it->state;
+
+ for (;;) {
+ if (it->top == NULL) {
+ ret = 1;
+ goto fail;
+ }
+
+ ret = it->top->dir->next(it->top->dir, &ent);
+ if (ret < 0)
+ goto fail;
+
+ if (ret > 0) {
+ pop(it);
+ continue;
+ }
+
+ if (should_skip(it, ent)) {
+ free(ent);
+ ent = NULL;
+ continue;
+ }
+
+ break;
+ }
+
+ ent = expand_path(it, ent);
+ if (ent == NULL) {
+ it->state = SQFS_ERROR_ALLOC;
+ return it->state;
+ }
+
+ apply_changes(it, ent);
+
+ if (S_ISDIR(ent->mode)) {
+ if (!(it->cfg.flags & DIR_SCAN_NO_RECURSION)) {
+ const char *name = strrchr(ent->name, '/');
+ name = (name == NULL) ? ent->name : (name + 1);
+
+ ret = it->top->dir->open_subdir(it->top->dir, &sub);
+ if (ret != 0)
+ goto fail;
+
+ ret = push(it, name, sub);
+ sqfs_drop(sub);
+ if (ret != 0)
+ goto fail;
+ }
+
+ if (it->cfg.flags & DIR_SCAN_NO_DIR) {
+ free(ent);
+ goto retry;
+ }
+ }
+
+ if (it->cfg.name_pattern != NULL) {
+ if (it->cfg.flags & DIR_SCAN_MATCH_FULL_PATH) {
+ ret = fnmatch(it->cfg.name_pattern,
+ ent->name, FNM_PATHNAME);
+ } else {
+ const char *name = strrchr(ent->name, '/');
+ name = (name == NULL) ? ent->name : (name + 1);
+
+ ret = fnmatch(it->cfg.name_pattern, name, 0);
+ }
+
+ if (ret != 0) {
+ free(ent);
+ goto retry;
+ }
+ }
+
+ *out = ent;
+ return it->state;
+fail:
+ free(ent);
+ it->state = ret;
+ return it->state;
+}
+
+static int read_link(dir_iterator_t *base, char **out)
+{
+ dir_tree_iterator_t *it = (dir_tree_iterator_t *)base;
+
+ if (it->top == NULL) {
+ *out = NULL;
+ return SQFS_ERROR_NO_ENTRY;
+ }
+
+ return it->top->dir->read_link(it->top->dir, out);
+}
+
+static int open_subdir(dir_iterator_t *base, dir_iterator_t **out)
+{
+ dir_tree_iterator_t *it = (dir_tree_iterator_t *)base;
+
+ if (it->top == NULL) {
+ *out = NULL;
+ return SQFS_ERROR_NO_ENTRY;
+ }
+
+ return it->top->dir->open_subdir(it->top->dir, out);
+}
+
+dir_iterator_t *dir_tree_iterator_create(const char *path,
+ const dir_tree_cfg_t *cfg)
+{
+ dir_tree_iterator_t *it = calloc(1, sizeof(*it));
+ dir_iterator_t *dir;
+ int ret;
+
+ if (it == NULL) {
+ perror(path);
+ return NULL;
+ }
+
+ it->cfg = *cfg;
+
+ dir = dir_iterator_create(path);
+ if (dir == NULL)
+ goto fail;
+
+ ret = push(it, "", dir);
+ dir = sqfs_drop(dir);
+ if (ret != 0) {
+ fprintf(stderr, "%s: out of memory\n", path);
+ goto fail;
+ }
+
+ sqfs_object_init(it, destroy, NULL);
+ ((dir_iterator_t *)it)->dev = it->top->dir->dev;
+ ((dir_iterator_t *)it)->next = next;
+ ((dir_iterator_t *)it)->read_link = read_link;
+ ((dir_iterator_t *)it)->open_subdir = open_subdir;
+
+ return (dir_iterator_t *)it;
+fail:
+ free(it);
+ return NULL;
+}
+
+void dir_tree_iterator_skip(dir_iterator_t *base)
+{
+ dir_tree_iterator_t *it = (dir_tree_iterator_t *)base;
+
+ pop(it);
+}
diff --git a/lib/io/src/unix/dir_iterator.c b/lib/io/src/unix/dir_iterator.c
new file mode 100644
index 0000000..d67fe83
--- /dev/null
+++ b/lib/io/src/unix/dir_iterator.c
@@ -0,0 +1,204 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * dir_iterator.c
+ *
+ * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "io/dir_iterator.h"
+#include "util/util.h"
+#include "sqfs/error.h"
+
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
+
+typedef struct {
+ dir_iterator_t base;
+
+ struct dirent *ent;
+ struct stat sb;
+ int state;
+ DIR *dir;
+} unix_dir_iterator_t;
+
+static void dir_destroy(sqfs_object_t *obj)
+{
+ unix_dir_iterator_t *it = (unix_dir_iterator_t *)obj;
+
+ closedir(it->dir);
+ free(it);
+}
+
+static int dir_read_link(dir_iterator_t *base, char **out)
+{
+ unix_dir_iterator_t *it = (unix_dir_iterator_t *)base;
+ ssize_t ret;
+ size_t size;
+ char *str;
+
+ *out = NULL;
+
+ if (it->state < 0)
+ return it->state;
+
+ if (it->state > 0 || it->ent == NULL)
+ return SQFS_ERROR_NO_ENTRY;
+
+ if ((sizeof(it->sb.st_size) > sizeof(size_t)) &&
+ it->sb.st_size > SIZE_MAX) {
+ return SQFS_ERROR_ALLOC;
+ }
+
+ if (SZ_ADD_OV((size_t)it->sb.st_size, 1, &size))
+ return SQFS_ERROR_ALLOC;
+
+ str = calloc(1, size);
+ if (str == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ ret = readlinkat(dirfd(it->dir), it->ent->d_name,
+ str, (size_t)it->sb.st_size);
+ if (ret < 0) {
+ free(str);
+ return SQFS_ERROR_IO;
+ }
+
+ str[ret] = '\0';
+
+ *out = str;
+ return 0;
+}
+
+static int dir_next(dir_iterator_t *base, dir_entry_t **out)
+{
+ unix_dir_iterator_t *it = (unix_dir_iterator_t *)base;
+ dir_entry_t *decoded;
+ size_t len;
+
+ *out = NULL;
+ if (it->state != 0)
+ return it->state;
+
+ errno = 0;
+ it->ent = readdir(it->dir);
+
+ if (it->ent == NULL) {
+ if (errno != 0) {
+ it->state = SQFS_ERROR_IO;
+ } else {
+ it->state = 1;
+ }
+
+ return it->state;
+ }
+
+ if (fstatat(dirfd(it->dir), it->ent->d_name,
+ &it->sb, AT_SYMLINK_NOFOLLOW)) {
+ it->state = SQFS_ERROR_IO;
+ return it->state;
+ }
+
+ len = strlen(it->ent->d_name);
+
+ decoded = alloc_flex(sizeof(*decoded), 1, len + 1);
+ if (decoded == NULL) {
+ it->state = SQFS_ERROR_ALLOC;
+ return it->state;
+ }
+
+ memcpy(decoded->name, it->ent->d_name, len);
+ decoded->mtime = it->sb.st_mtime;
+ decoded->dev = it->sb.st_dev;
+ decoded->rdev = it->sb.st_rdev;
+ decoded->uid = it->sb.st_uid;
+ decoded->gid = it->sb.st_gid;
+ decoded->mode = it->sb.st_mode;
+
+ *out = decoded;
+ return it->state;
+}
+
+static int dir_open_subdir(dir_iterator_t *base, dir_iterator_t **out)
+{
+ const unix_dir_iterator_t *it = (const unix_dir_iterator_t *)base;
+ unix_dir_iterator_t *sub = NULL;
+ int fd;
+
+ *out = NULL;
+
+ if (it->state < 0)
+ return it->state;
+
+ if (it->state > 0 || it->ent == NULL)
+ return SQFS_ERROR_NO_ENTRY;
+
+ fd = openat(dirfd(it->dir), it->ent->d_name, O_RDONLY | O_DIRECTORY);
+ if (fd < 0) {
+ if (errno == ENOTDIR)
+ return SQFS_ERROR_NOT_DIR;
+ return SQFS_ERROR_IO;
+ }
+
+ sub = calloc(1, sizeof(*sub));
+ if (sub == NULL)
+ goto fail_alloc;
+
+ sub->dir = fdopendir(fd);
+ if (sub->dir == NULL)
+ goto fail_alloc;
+
+ if (fstat(dirfd(sub->dir), &sub->sb)) {
+ free(sub);
+ return SQFS_ERROR_IO;
+ }
+
+ sqfs_object_init(sub, dir_destroy, NULL);
+ ((dir_iterator_t *)sub)->dev = sub->sb.st_dev;
+ ((dir_iterator_t *)sub)->next = dir_next;
+ ((dir_iterator_t *)sub)->read_link = dir_read_link;
+ ((dir_iterator_t *)sub)->open_subdir = dir_open_subdir;
+
+ *out = (dir_iterator_t *)sub;
+ return 0;
+fail_alloc:
+ free(sub);
+ close(fd);
+ return SQFS_ERROR_ALLOC;
+}
+
+dir_iterator_t *dir_iterator_create(const char *path)
+{
+ unix_dir_iterator_t *it = calloc(1, sizeof(*it));
+
+ if (it == NULL) {
+ perror(path);
+ return NULL;
+ }
+
+ it->state = 0;
+ it->dir = opendir(path);
+
+ if (it->dir == NULL) {
+ perror(path);
+ free(it);
+ return NULL;
+ }
+
+ if (fstat(dirfd(it->dir), &it->sb)) {
+ perror(path);
+ closedir(it->dir);
+ free(it);
+ return NULL;
+ }
+
+ sqfs_object_init(it, dir_destroy, NULL);
+ ((dir_iterator_t *)it)->dev = it->sb.st_dev;
+ ((dir_iterator_t *)it)->next = dir_next;
+ ((dir_iterator_t *)it)->read_link = dir_read_link;
+ ((dir_iterator_t *)it)->open_subdir = dir_open_subdir;
+
+ return (dir_iterator_t *)it;
+}
diff --git a/lib/io/src/win32/dir_iterator.c b/lib/io/src/win32/dir_iterator.c
new file mode 100644
index 0000000..23d00f4
--- /dev/null
+++ b/lib/io/src/win32/dir_iterator.c
@@ -0,0 +1,218 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * dir_iterator.c
+ *
+ * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "io/dir_iterator.h"
+#include "util/util.h"
+#include "sqfs/error.h"
+
+#include <windows.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define UNIX_EPOCH_ON_W32 11644473600UL
+#define W32_TICS_PER_SEC 10000000UL
+
+typedef struct {
+ dir_iterator_t base;
+
+ WIN32_FIND_DATAW ent;
+ HANDLE dirhnd;
+ int state;
+ bool is_first;
+
+ WCHAR path[];
+} dir_iterator_win32_t;
+
+static sqfs_s64 w32time_to_unix(const FILETIME *ft)
+{
+ sqfs_u64 w32ts;
+
+ w32ts = ft->dwHighDateTime;
+ w32ts <<= 32UL;
+ w32ts |= ft->dwLowDateTime;
+
+ w32ts /= W32_TICS_PER_SEC;
+
+ if (w32ts <= UNIX_EPOCH_ON_W32)
+ return -((sqfs_s64)(UNIX_EPOCH_ON_W32 - w32ts));
+
+ return w32ts - UNIX_EPOCH_ON_W32;
+}
+
+static int dir_iterator_read_link(dir_iterator_t *it, char **out)
+{
+ (void)it;
+ *out = NULL;
+ return SQFS_ERROR_UNSUPPORTED;
+}
+
+static int dir_iterator_next(dir_iterator_t *it, dir_entry_t **out)
+{
+ dir_iterator_win32_t *w32 = (dir_iterator_win32_t *)it;
+ dir_entry_t *ent = NULL;
+ DWORD length;
+
+ if (w32->state == 0 && !w32->is_first) {
+ if (!FindNextFileW(w32->dirhnd, &w32->ent)) {
+ if (GetLastError() == ERROR_NO_MORE_FILES) {
+ w32->state = 1;
+ } else {
+ w32->state = SQFS_ERROR_IO;
+ }
+ }
+ }
+
+ w32->is_first = false;
+
+ if (w32->state != 0)
+ goto out;
+
+ length = WideCharToMultiByte(CP_UTF8, 0, w32->ent.cFileName,
+ -1, NULL, 0, NULL, NULL);
+ if (length <= 0) {
+ w32->state = SQFS_ERROR_ALLOC;
+ goto out;
+ }
+
+ ent = alloc_flex(sizeof(*ent), 1, length + 1);
+ if (ent == NULL) {
+ w32->state = SQFS_ERROR_ALLOC;
+ goto out;
+ }
+
+ WideCharToMultiByte(CP_UTF8, 0, w32->ent.cFileName, -1,
+ ent->name, length + 1, NULL, NULL);
+
+ if (w32->ent.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
+ ent->mode = S_IFDIR | 0755;
+ } else {
+ ent->mode = S_IFREG | 0644;
+ }
+
+ ent->mtime = w32time_to_unix(&(w32->ent.ftLastWriteTime));
+out:
+ *out = ent;
+ return w32->state;
+}
+
+static void dir_iterator_destroy(sqfs_object_t *obj)
+{
+ dir_iterator_win32_t *dir = (dir_iterator_win32_t *)obj;
+
+ FindClose(dir->dirhnd);
+ free(dir);
+}
+
+static int dir_iterator_open_subdir(dir_iterator_t *it, dir_iterator_t **out)
+{
+ const dir_iterator_win32_t *dir = (const dir_iterator_win32_t *)it;
+ dir_iterator_win32_t *sub = NULL;
+ size_t plen, slen, total;
+
+ *out = NULL;
+
+ if (dir->state != 0)
+ return (dir->state > 0) ? SQFS_ERROR_NO_ENTRY : dir->state;
+
+ if (!(dir->ent.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
+ return SQFS_ERROR_NOT_DIR;
+
+ plen = wcslen(dir->path) - 1;
+ slen = wcslen(dir->ent.cFileName);
+ total = plen + slen + 3;
+
+ sub = alloc_flex(sizeof(*sub), sizeof(WCHAR), total);
+ if (sub == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ memcpy(sub->path, dir->path, plen * sizeof(WCHAR));
+ memcpy(sub->path + plen, dir->ent.cFileName, slen * sizeof(WCHAR));
+ sub->path[plen + slen ] = '\\';
+ sub->path[plen + slen + 1] = '*';
+ sub->path[plen + slen + 2] = '\0';
+
+ sqfs_object_init(sub, dir_iterator_destroy, NULL);
+ ((dir_iterator_t *)sub)->next = dir_iterator_next;
+ ((dir_iterator_t *)sub)->read_link = dir_iterator_read_link;
+ ((dir_iterator_t *)sub)->open_subdir = dir_iterator_open_subdir;
+ sub->is_first = true;
+ sub->state = 0;
+
+ sub->dirhnd = FindFirstFileW(sub->path, &sub->ent);
+ if (sub->dirhnd == INVALID_HANDLE_VALUE) {
+ free(sub);
+ return SQFS_ERROR_IO;
+ }
+
+ *out = (dir_iterator_t *)sub;
+ return 0;
+}
+
+dir_iterator_t *dir_iterator_create(const char *path)
+{
+ dir_iterator_win32_t *it;
+ size_t len, newlen;
+ WCHAR *wpath = NULL;
+ void *new = NULL;
+
+ /* convert path to UTF-16, append "\\*" */
+ wpath = path_to_windows(path);
+ if (wpath == NULL)
+ goto fail_alloc;
+
+ len = wcslen(wpath);
+ newlen = len + 1;
+
+ if (len > 0 && wpath[len - 1] != '\\')
+ newlen += 1;
+
+ new = realloc(wpath, sizeof(wpath[0]) * (newlen + 1));
+ if (new == NULL)
+ goto fail_alloc;
+
+ wpath = new;
+
+ if (len > 0 && wpath[len - 1] != '\\')
+ wpath[len++] = '\\';
+
+ wpath[len++] = '*';
+ wpath[len++] = '\0';
+
+ /* create the sourrounding iterator structure */
+ new = realloc(wpath, sizeof(*it) + len * sizeof(wpath[0]));
+ if (new == NULL)
+ goto fail_alloc;
+
+ it = new;
+ wpath = NULL;
+ memmove(it->path, new, len * sizeof(wpath[0]));
+
+ /* initialize */
+ memset(it, 0, offsetof(dir_iterator_win32_t, path));
+ sqfs_object_init(it, dir_iterator_destroy, NULL);
+
+ ((dir_iterator_t *)it)->next = dir_iterator_next;
+ ((dir_iterator_t *)it)->read_link = dir_iterator_read_link;
+ ((dir_iterator_t *)it)->open_subdir = dir_iterator_open_subdir;
+ it->is_first = true;
+ it->state = 0;
+
+ /* get the directory handle AND the first entry */
+ it->dirhnd = FindFirstFileW(it->path, &it->ent);
+
+ if (it->dirhnd == INVALID_HANDLE_VALUE) {
+ w32_perror(path);
+ free(it);
+ return NULL;
+ }
+
+ return (dir_iterator_t *)it;
+fail_alloc:
+ fprintf(stderr, "%s: allocation failure.\n", path);
+ free(wpath);
+ return NULL;
+}