From e501c9a4bc230dab034571c2078a12a8a856c67a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 13 May 2023 17:18:43 +0200 Subject: Move directory iterator from libutil to libio Signed-off-by: David Oberhollenzer --- lib/io/Makemodule.am | 29 ++- lib/io/src/dir_tree_iterator.c | 310 ++++++++++++++++++++++++++++ lib/io/src/unix/dir_iterator.c | 204 +++++++++++++++++++ lib/io/src/win32/dir_iterator.c | 218 ++++++++++++++++++++ lib/io/test/dir_iterator.c | 367 ++++++++++++++++++++++++++++++++++ lib/io/test/dir_tree_iterator.c | 251 +++++++++++++++++++++++ lib/io/test/dir_tree_iterator2.c | 222 ++++++++++++++++++++ lib/io/test/dir_tree_iterator3.c | 105 ++++++++++ lib/io/test/testdir/dira/file_a0 | 0 lib/io/test/testdir/dira/file_a1 | 0 lib/io/test/testdir/dira/file_a2 | 0 lib/io/test/testdir/dirb/dirx/file_x0 | 0 lib/io/test/testdir/dirb/dirx/file_x1 | 0 lib/io/test/testdir/dirb/dirx/file_x2 | 0 lib/io/test/testdir/dirb/file_b0 | 0 lib/io/test/testdir/dirb/file_b1 | 0 lib/io/test/testdir/dirb/file_b2 | 0 lib/io/test/testdir/dirc/file_c0 | 0 lib/io/test/testdir/dirc/file_c1 | 0 lib/io/test/testdir/dirc/file_c2 | 0 20 files changed, 1704 insertions(+), 2 deletions(-) create mode 100644 lib/io/src/dir_tree_iterator.c create mode 100644 lib/io/src/unix/dir_iterator.c create mode 100644 lib/io/src/win32/dir_iterator.c create mode 100644 lib/io/test/dir_iterator.c create mode 100644 lib/io/test/dir_tree_iterator.c create mode 100644 lib/io/test/dir_tree_iterator2.c create mode 100644 lib/io/test/dir_tree_iterator3.c create mode 100644 lib/io/test/testdir/dira/file_a0 create mode 100644 lib/io/test/testdir/dira/file_a1 create mode 100644 lib/io/test/testdir/dira/file_a2 create mode 100644 lib/io/test/testdir/dirb/dirx/file_x0 create mode 100644 lib/io/test/testdir/dirb/dirx/file_x1 create mode 100644 lib/io/test/testdir/dirb/dirx/file_x2 create mode 100644 lib/io/test/testdir/dirb/file_b0 create mode 100644 lib/io/test/testdir/dirb/file_b1 create mode 100644 lib/io/test/testdir/dirb/file_b2 create mode 100644 lib/io/test/testdir/dirc/file_c0 create mode 100644 lib/io/test/testdir/dirc/file_c1 create mode 100644 lib/io/test/testdir/dirc/file_c2 (limited to 'lib/io') diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index 0480ae1..9f97540 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,22 +1,26 @@ libio_a_SOURCES = include/io/istream.h include/io/ostream.h include/io/xfrm.h \ include/io/file.h include/io/std.h \ + include/io/dir_iterator.h include/io/dir_tree_iterator.h \ lib/io/src/internal.h lib/io/src/ostream.c \ lib/io/src/istream.c lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ - lib/io/src/xfrm/istream.c + lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS) if WINDOWS libio_a_SOURCES += lib/io/src/win32/ostream.c lib/io/src/win32/istream.c +libio_a_SOURCES += lib/io/src/win32/dir_iterator.c libio_a_CFLAGS += -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 else libio_a_SOURCES += lib/io/src/unix/ostream.c lib/io/src/unix/istream.c +libio_a_SOURCES += lib/io/src/unix/dir_iterator.c endif noinst_LIBRARIES += libio.a LIBIO_TESTS = test_get_line test_sparse_fb test_istream_read \ - test_istream_skip test_stream_splice + test_istream_skip test_stream_splice test_dir_iterator \ + test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 test_get_line_SOURCES = lib/io/test/get_line.c test_get_line_LDADD = libio.a libcompat.a @@ -35,6 +39,26 @@ test_istream_skip_LDADD = libio.a libutil.a libcompat.a test_stream_splice_SOURCES = lib/io/test/stream_splice.c test_stream_splice_LDADD = libio.a libutil.a libcompat.a +test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c +test_dir_iterator_LDADD = libio.a libutil.a libcompat.a +test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_iterator_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir + +test_dir_tree_iterator_SOURCES = lib/io/test/dir_tree_iterator.c +test_dir_tree_iterator_LDADD = libio.a libutil.a libcompat.a +test_dir_tree_iterator_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir + +test_dir_tree_iterator2_SOURCES = lib/io/test/dir_tree_iterator2.c +test_dir_tree_iterator2_LDADD = libio.a libutil.a libcompat.a +test_dir_tree_iterator2_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator2_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir + +test_dir_tree_iterator3_SOURCES = lib/io/test/dir_tree_iterator3.c +test_dir_tree_iterator3_LDADD = libio.a libutil.a libcompat.a +test_dir_tree_iterator3_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir + if WITH_XZ test_io_xfrm_xz_SOURCES = lib/io/test/xfrm.c test_io_xfrm_xz_LDADD = libio.a libxfrm.a libcompat.a $(XZ_LIBS) @@ -73,3 +97,4 @@ check_PROGRAMS += $(LIBIO_TESTS) TESTS += $(LIBIO_TESTS) EXTRA_DIST += $(top_srcdir)/lib/io/test/get_line.txt +EXTRA_DIST += $(top_srcdir)/lib/io/test/testdir 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 + */ +#include "config.h" +#include "io/dir_tree_iterator.h" +#include "util/util.h" +#include "sqfs/error.h" + +#include +#include + +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 + */ +#include "config.h" +#include "io/dir_iterator.h" +#include "util/util.h" +#include "sqfs/error.h" + +#include +#include +#include +#include +#include + +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 + */ +#include "config.h" +#include "io/dir_iterator.h" +#include "util/util.h" +#include "sqfs/error.h" + +#include +#include +#include + +#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; +} diff --git a/lib/io/test/dir_iterator.c b/lib/io/test/dir_iterator.c new file mode 100644 index 0000000..57e76ab --- /dev/null +++ b/lib/io/test/dir_iterator.c @@ -0,0 +1,367 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "io/dir_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_iterator_t *dir, *suba, *subb, *subc, *sub; + dir_entry_t *dent, *ent[6]; + size_t i; + int ret; + (void)argc; (void)argv; + + /* scan the top level hierarchy */ + dir = dir_iterator_create(TEST_PATH); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* scan first sub hierarchy */ + dir = dir_iterator_create(TEST_PATH "/dira"); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_a0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_a1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_a2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* scan second sub hierarchy */ + dir = dir_iterator_create(TEST_PATH "/dirb"); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NOT_NULL(ent[5]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 6, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + + for (i = 0; i < 6; ++i) + free(ent[i]); + + /* scan first sub hierarchy */ + dir = dir_iterator_create(TEST_PATH "/dirc"); + TEST_NOT_NULL(dir); + + ret = dir->next(dir, &ent[0]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[1]); + TEST_NOT_NULL(ent[1]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[2]); + TEST_NOT_NULL(ent[2]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[3]); + TEST_NOT_NULL(ent[3]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[4]); + TEST_NOT_NULL(ent[4]); + TEST_EQUAL_I(ret, 0); + + ret = dir->next(dir, &ent[5]); + TEST_NULL(ent[5]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_c1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_c2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* test sub directory iterators */ + suba = NULL; + subb = NULL; + subc = NULL; + + dir = dir_iterator_create(TEST_PATH); + TEST_NOT_NULL(dir); + + for (i = 0; i < 5; ++i) { + ret = dir->next(dir, &dent); + TEST_NOT_NULL(dent); + TEST_EQUAL_I(ret, 0); + + if (!strcmp(dent->name, "dira")) { + TEST_NULL(suba); + ret = dir->open_subdir(dir, &suba); + TEST_NOT_NULL(suba); + TEST_EQUAL_I(ret, 0); + } else if (!strcmp(dent->name, "dirb")) { + TEST_NULL(subb); + ret = dir->open_subdir(dir, &subb); + TEST_NOT_NULL(subb); + TEST_EQUAL_I(ret, 0); + } else if (!strcmp(dent->name, "dirc")) { + TEST_NULL(subc); + ret = dir->open_subdir(dir, &subc); + TEST_NOT_NULL(subc); + TEST_EQUAL_I(ret, 0); + } + + free(dent); + } + + ret = dir->next(dir, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + dir = sqfs_drop(dir); + + TEST_NOT_NULL(suba); + TEST_NOT_NULL(subb); + TEST_NOT_NULL(subc); + + /* sub iterator a */ + for (i = 0; i < 5; ++i) { + ret = suba->next(suba, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = suba->open_subdir(suba, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + } + } + + ret = suba->next(suba, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + suba = sqfs_drop(suba); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_a0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_a1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_a2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + /* sub iterator b */ + for (i = 0; i < 6; ++i) { + ret = subb->next(subb, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = subb->open_subdir(subb, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + } + } + + ret = subb->next(subb, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + subb = sqfs_drop(subb); + + qsort(ent, 6, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_b0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_b1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "file_b2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + + for (i = 0; i < 6; ++i) + free(ent[i]); + + /* sub iterator c */ + for (i = 0; i < 5; ++i) { + ret = subc->next(subc, &ent[i]); + TEST_NOT_NULL(ent[0]); + TEST_EQUAL_I(ret, 0); + + if (S_ISREG(ent[i]->mode)) { + ret = subc->open_subdir(subc, &sub); + TEST_NULL(sub); + TEST_EQUAL_I(ret, SQFS_ERROR_NOT_DIR); + } + } + + ret = subc->next(subc, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + subc = sqfs_drop(subc); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + + TEST_STR_EQUAL(ent[0]->name, "."); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, ".."); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "file_c1"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "file_c2"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + + for (i = 0; i < 5; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/io/test/dir_tree_iterator.c b/lib/io/test/dir_tree_iterator.c new file mode 100644 index 0000000..75e32fb --- /dev/null +++ b/lib/io/test/dir_tree_iterator.c @@ -0,0 +1,251 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "io/dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_entry_t *ent[17]; + dir_iterator_t *dir; + dir_tree_cfg_t cfg; + size_t i; + int ret; + (void)argc; (void)argv; + + memset(&cfg, 0, sizeof(cfg)); + cfg.def_mtime = 1337; + cfg.def_uid = 42; + cfg.def_gid = 23; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 16; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[16]); + TEST_NULL(ent[16]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 16, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 16; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_EQUAL_UI(ent[0]->mtime, 1337); + TEST_EQUAL_UI(ent[0]->uid, 42); + TEST_EQUAL_UI(ent[0]->gid, 23); + TEST_STR_EQUAL(ent[1]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_EQUAL_UI(ent[1]->mtime, 1337); + TEST_EQUAL_UI(ent[1]->uid, 42); + TEST_EQUAL_UI(ent[1]->gid, 23); + TEST_STR_EQUAL(ent[2]->name, "dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_EQUAL_UI(ent[2]->mtime, 1337); + TEST_EQUAL_UI(ent[2]->uid, 42); + TEST_EQUAL_UI(ent[2]->gid, 23); + TEST_STR_EQUAL(ent[3]->name, "dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_EQUAL_UI(ent[3]->mtime, 1337); + TEST_EQUAL_UI(ent[3]->uid, 42); + TEST_EQUAL_UI(ent[3]->gid, 23); + TEST_STR_EQUAL(ent[4]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + TEST_EQUAL_UI(ent[4]->mtime, 1337); + TEST_EQUAL_UI(ent[4]->uid, 42); + TEST_EQUAL_UI(ent[4]->gid, 23); + TEST_STR_EQUAL(ent[5]->name, "dirb/dirx"); + TEST_ASSERT(S_ISDIR(ent[5]->mode)); + TEST_EQUAL_UI(ent[5]->mtime, 1337); + TEST_EQUAL_UI(ent[5]->uid, 42); + TEST_EQUAL_UI(ent[5]->gid, 23); + TEST_STR_EQUAL(ent[6]->name, "dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_EQUAL_UI(ent[6]->mtime, 1337); + TEST_EQUAL_UI(ent[6]->uid, 42); + TEST_EQUAL_UI(ent[6]->gid, 23); + TEST_STR_EQUAL(ent[7]->name, "dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_EQUAL_UI(ent[7]->mtime, 1337); + TEST_EQUAL_UI(ent[7]->uid, 42); + TEST_EQUAL_UI(ent[7]->gid, 23); + TEST_STR_EQUAL(ent[8]->name, "dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + TEST_EQUAL_UI(ent[8]->mtime, 1337); + TEST_EQUAL_UI(ent[8]->uid, 42); + TEST_EQUAL_UI(ent[8]->gid, 23); + TEST_STR_EQUAL(ent[9]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[9]->mode)); + TEST_EQUAL_UI(ent[9]->mtime, 1337); + TEST_EQUAL_UI(ent[9]->uid, 42); + TEST_EQUAL_UI(ent[9]->gid, 23); + TEST_STR_EQUAL(ent[10]->name, "dirb/file_b1"); + TEST_ASSERT(S_ISREG(ent[10]->mode)); + TEST_EQUAL_UI(ent[10]->mtime, 1337); + TEST_EQUAL_UI(ent[10]->uid, 42); + TEST_EQUAL_UI(ent[10]->gid, 23); + TEST_STR_EQUAL(ent[11]->name, "dirb/file_b2"); + TEST_ASSERT(S_ISREG(ent[11]->mode)); + TEST_EQUAL_UI(ent[11]->mtime, 1337); + TEST_EQUAL_UI(ent[11]->uid, 42); + TEST_EQUAL_UI(ent[11]->gid, 23); + TEST_STR_EQUAL(ent[12]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[12]->mode)); + TEST_EQUAL_UI(ent[12]->mtime, 1337); + TEST_EQUAL_UI(ent[12]->uid, 42); + TEST_EQUAL_UI(ent[12]->gid, 23); + TEST_STR_EQUAL(ent[13]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[13]->mode)); + TEST_EQUAL_UI(ent[13]->mtime, 1337); + TEST_EQUAL_UI(ent[13]->uid, 42); + TEST_EQUAL_UI(ent[13]->gid, 23); + TEST_STR_EQUAL(ent[14]->name, "dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[14]->mode)); + TEST_EQUAL_UI(ent[14]->mtime, 1337); + TEST_EQUAL_UI(ent[14]->uid, 42); + TEST_EQUAL_UI(ent[14]->gid, 23); + TEST_STR_EQUAL(ent[15]->name, "dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[15]->mode)); + TEST_EQUAL_UI(ent[15]->mtime, 1337); + TEST_EQUAL_UI(ent[15]->uid, 42); + TEST_EQUAL_UI(ent[15]->gid, 23); + + for (i = 0; i < 16; ++i) + free(ent[i]); + + /* retry with skipping */ + printf("**********\n"); + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 13; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + + if (!strcmp(ent[i]->name, "dirb/dirx")) + dir_tree_iterator_skip(dir); + } + + ret = dir->next(dir, &ent[13]); + TEST_NULL(ent[13]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 13, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 13; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "dirb/dirx"); + TEST_ASSERT(S_ISDIR(ent[5]->mode)); + TEST_STR_EQUAL(ent[6]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_STR_EQUAL(ent[7]->name, "dirb/file_b1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_STR_EQUAL(ent[8]->name, "dirb/file_b2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + TEST_STR_EQUAL(ent[9]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[9]->mode)); + TEST_STR_EQUAL(ent[10]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[10]->mode)); + TEST_STR_EQUAL(ent[11]->name, "dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[11]->mode)); + TEST_STR_EQUAL(ent[12]->name, "dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[12]->mode)); + + for (i = 0; i < 13; ++i) + free(ent[i]); + + /* retry with skipping */ + printf("**********\n"); + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 9; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + + if (!strcmp(ent[i]->name, "dirb")) + dir_tree_iterator_skip(dir); + } + + ret = dir->next(dir, &ent[9]); + TEST_NULL(ent[9]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 9, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 9; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[5]->mode)); + TEST_STR_EQUAL(ent[6]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_STR_EQUAL(ent[7]->name, "dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_STR_EQUAL(ent[8]->name, "dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + + for (i = 0; i < 9; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/io/test/dir_tree_iterator2.c b/lib/io/test/dir_tree_iterator2.c new file mode 100644 index 0000000..d3c4990 --- /dev/null +++ b/lib/io/test/dir_tree_iterator2.c @@ -0,0 +1,222 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "io/dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_entry_t *ent[17]; + dir_iterator_t *dir; + dir_tree_cfg_t cfg; + size_t i; + int ret; + (void)argc; (void)argv; + + /********** without files **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_FILE; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 4; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[4]); + TEST_NULL(ent[4]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 4, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 4; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirb/dirx"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[3]->mode)); + + for (i = 0; i < 4; ++i) + free(ent[i]); + + /********** recursive but without dirs **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_DIR; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 12; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[12]); + TEST_NULL(ent[12]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 12, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 12; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[5]->mode)); + TEST_STR_EQUAL(ent[6]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_STR_EQUAL(ent[7]->name, "dirb/file_b1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_STR_EQUAL(ent[8]->name, "dirb/file_b2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + TEST_STR_EQUAL(ent[9]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[9]->mode)); + TEST_STR_EQUAL(ent[10]->name, "dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[10]->mode)); + TEST_STR_EQUAL(ent[11]->name, "dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[11]->mode)); + + for (i = 0; i < 12; ++i) + free(ent[i]); + + /********** non-recursive **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.flags |= DIR_SCAN_NO_RECURSION; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 3; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[3]); + TEST_NULL(ent[3]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 3, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 3; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb"); + TEST_ASSERT(S_ISDIR(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirc"); + TEST_ASSERT(S_ISDIR(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + /********** with prefix inserted **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.prefix = "foobar"; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 16; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[16]); + TEST_NULL(ent[16]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 16, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 16; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "foobar/dira"); + TEST_ASSERT(S_ISDIR(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "foobar/dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "foobar/dira/file_a1"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + TEST_STR_EQUAL(ent[3]->name, "foobar/dira/file_a2"); + TEST_ASSERT(S_ISREG(ent[3]->mode)); + TEST_STR_EQUAL(ent[4]->name, "foobar/dirb"); + TEST_ASSERT(S_ISDIR(ent[4]->mode)); + TEST_STR_EQUAL(ent[5]->name, "foobar/dirb/dirx"); + TEST_ASSERT(S_ISDIR(ent[5]->mode)); + TEST_STR_EQUAL(ent[6]->name, "foobar/dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[6]->mode)); + TEST_STR_EQUAL(ent[7]->name, "foobar/dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[7]->mode)); + TEST_STR_EQUAL(ent[8]->name, "foobar/dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[8]->mode)); + TEST_STR_EQUAL(ent[9]->name, "foobar/dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[9]->mode)); + TEST_STR_EQUAL(ent[10]->name, "foobar/dirb/file_b1"); + TEST_ASSERT(S_ISREG(ent[10]->mode)); + TEST_STR_EQUAL(ent[11]->name, "foobar/dirb/file_b2"); + TEST_ASSERT(S_ISREG(ent[11]->mode)); + TEST_STR_EQUAL(ent[12]->name, "foobar/dirc"); + TEST_ASSERT(S_ISDIR(ent[12]->mode)); + TEST_STR_EQUAL(ent[13]->name, "foobar/dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[13]->mode)); + TEST_STR_EQUAL(ent[14]->name, "foobar/dirc/file_c1"); + TEST_ASSERT(S_ISREG(ent[14]->mode)); + TEST_STR_EQUAL(ent[15]->name, "foobar/dirc/file_c2"); + TEST_ASSERT(S_ISREG(ent[15]->mode)); + + for (i = 0; i < 16; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/io/test/dir_tree_iterator3.c b/lib/io/test/dir_tree_iterator3.c new file mode 100644 index 0000000..6fce085 --- /dev/null +++ b/lib/io/test/dir_tree_iterator3.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "io/dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const dir_entry_t *const *lhs = a; + const dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + dir_entry_t *ent[17]; + dir_iterator_t *dir; + dir_tree_cfg_t cfg; + size_t i; + int ret; + (void)argc; (void)argv; + + /********** match name **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.name_pattern = "file_x*"; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 3; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[3]); + TEST_NULL(ent[3]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 3, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 3; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dirb/dirx/file_x0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb/dirx/file_x1"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirb/dirx/file_x2"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + /********** match path **********/ + memset(&cfg, 0, sizeof(cfg)); + cfg.name_pattern = "dir*/file_*0"; + cfg.flags |= DIR_SCAN_MATCH_FULL_PATH; + + dir = dir_tree_iterator_create(TEST_PATH, &cfg); + TEST_NOT_NULL(dir); + + for (i = 0; i < 3; ++i) { + ret = dir->next(dir, &ent[i]); + TEST_NOT_NULL(ent[i]); + TEST_EQUAL_I(ret, 0); + printf("READ %s\n", ent[i]->name); + } + + ret = dir->next(dir, &ent[3]); + TEST_NULL(ent[3]); + TEST_ASSERT(ret > 0); + + dir = sqfs_drop(dir); + + qsort(ent, 3, sizeof(ent[0]), compare_entries); + + printf("After sort:\n"); + for (i = 0; i < 3; ++i) + printf("%s\n", ent[i]->name); + + TEST_STR_EQUAL(ent[0]->name, "dira/file_a0"); + TEST_ASSERT(S_ISREG(ent[0]->mode)); + TEST_STR_EQUAL(ent[1]->name, "dirb/file_b0"); + TEST_ASSERT(S_ISREG(ent[1]->mode)); + TEST_STR_EQUAL(ent[2]->name, "dirc/file_c0"); + TEST_ASSERT(S_ISREG(ent[2]->mode)); + + for (i = 0; i < 3; ++i) + free(ent[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/io/test/testdir/dira/file_a0 b/lib/io/test/testdir/dira/file_a0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dira/file_a1 b/lib/io/test/testdir/dira/file_a1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dira/file_a2 b/lib/io/test/testdir/dira/file_a2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/dirx/file_x0 b/lib/io/test/testdir/dirb/dirx/file_x0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/dirx/file_x1 b/lib/io/test/testdir/dirb/dirx/file_x1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/dirx/file_x2 b/lib/io/test/testdir/dirb/dirx/file_x2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/file_b0 b/lib/io/test/testdir/dirb/file_b0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/file_b1 b/lib/io/test/testdir/dirb/file_b1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirb/file_b2 b/lib/io/test/testdir/dirb/file_b2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirc/file_c0 b/lib/io/test/testdir/dirc/file_c0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirc/file_c1 b/lib/io/test/testdir/dirc/file_c1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/io/test/testdir/dirc/file_c2 b/lib/io/test/testdir/dirc/file_c2 new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3