From 9f2d63162e82d423faeda9c7449d6edd819e4591 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 23 Sep 2023 11:16:46 +0200 Subject: Cleanup: remove rest of libio Move the directory iterator test to libsquashfs, move the tree scanning code to libcommon. Signed-off-by: David Oberhollenzer --- Makefile.am | 1 - bin/gensquashfs/Makemodule.am | 12 +- bin/gensquashfs/src/mkfs.h | 2 +- include/dir_tree_iterator.h | 87 +++++++ include/io/dir_iterator.h | 87 ------- include/tar/tar.h | 1 - lib/common/Makemodule.am | 21 +- lib/common/src/dir_tree_iterator.c | 257 ++++++++++++++++++++ lib/common/test/dir_tree_iterator.c | 254 +++++++++++++++++++ lib/common/test/dir_tree_iterator2.c | 225 +++++++++++++++++ lib/common/test/dir_tree_iterator3.c | 108 +++++++++ lib/io/Makemodule.am | 34 --- lib/io/src/dir_tree_iterator.c | 257 -------------------- lib/io/test/dir_iterator.c | 417 -------------------------------- lib/io/test/dir_tree_iterator.c | 253 ------------------- lib/io/test/dir_tree_iterator2.c | 224 ----------------- lib/io/test/dir_tree_iterator3.c | 107 -------- 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 | 1 - lib/io/test/testdir/dirc/file_c1 | 0 lib/io/test/testdir/dirc/file_c2 | 1 - lib/sqfs/Makemodule.am | 9 +- lib/sqfs/test/dir_iterator.c | 417 ++++++++++++++++++++++++++++++++ lib/sqfs/test/testdir/dira/file_a0 | 0 lib/sqfs/test/testdir/dira/file_a1 | 0 lib/sqfs/test/testdir/dira/file_a2 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x0 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x1 | 0 lib/sqfs/test/testdir/dirb/dirx/file_x2 | 0 lib/sqfs/test/testdir/dirb/file_b0 | 0 lib/sqfs/test/testdir/dirb/file_b1 | 0 lib/sqfs/test/testdir/dirb/file_b2 | 0 lib/sqfs/test/testdir/dirc/file_c0 | 1 + lib/sqfs/test/testdir/dirc/file_c1 | 0 lib/sqfs/test/testdir/dirc/file_c2 | 1 + lib/tar/src/iterator.c | 1 + lib/tar/test/tar_iterator.c | 1 + lib/tar/test/tar_iterator2.c | 1 + lib/tar/test/tar_iterator3.c | 1 + lib/tar/test/tar_write_simple.c | 1 + 48 files changed, 1389 insertions(+), 1393 deletions(-) create mode 100644 include/dir_tree_iterator.h delete mode 100644 include/io/dir_iterator.h create mode 100644 lib/common/src/dir_tree_iterator.c create mode 100644 lib/common/test/dir_tree_iterator.c create mode 100644 lib/common/test/dir_tree_iterator2.c create mode 100644 lib/common/test/dir_tree_iterator3.c delete mode 100644 lib/io/Makemodule.am delete mode 100644 lib/io/src/dir_tree_iterator.c delete mode 100644 lib/io/test/dir_iterator.c delete mode 100644 lib/io/test/dir_tree_iterator.c delete mode 100644 lib/io/test/dir_tree_iterator2.c delete mode 100644 lib/io/test/dir_tree_iterator3.c delete mode 100644 lib/io/test/testdir/dira/file_a0 delete mode 100644 lib/io/test/testdir/dira/file_a1 delete mode 100644 lib/io/test/testdir/dira/file_a2 delete mode 100644 lib/io/test/testdir/dirb/dirx/file_x0 delete mode 100644 lib/io/test/testdir/dirb/dirx/file_x1 delete mode 100644 lib/io/test/testdir/dirb/dirx/file_x2 delete mode 100644 lib/io/test/testdir/dirb/file_b0 delete mode 100644 lib/io/test/testdir/dirb/file_b1 delete mode 100644 lib/io/test/testdir/dirb/file_b2 delete mode 100644 lib/io/test/testdir/dirc/file_c0 delete mode 100644 lib/io/test/testdir/dirc/file_c1 delete mode 100644 lib/io/test/testdir/dirc/file_c2 create mode 100644 lib/sqfs/test/dir_iterator.c create mode 100644 lib/sqfs/test/testdir/dira/file_a0 create mode 100644 lib/sqfs/test/testdir/dira/file_a1 create mode 100644 lib/sqfs/test/testdir/dira/file_a2 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x0 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x1 create mode 100644 lib/sqfs/test/testdir/dirb/dirx/file_x2 create mode 100644 lib/sqfs/test/testdir/dirb/file_b0 create mode 100644 lib/sqfs/test/testdir/dirb/file_b1 create mode 100644 lib/sqfs/test/testdir/dirb/file_b2 create mode 100644 lib/sqfs/test/testdir/dirc/file_c0 create mode 100644 lib/sqfs/test/testdir/dirc/file_c1 create mode 100644 lib/sqfs/test/testdir/dirc/file_c2 diff --git a/Makefile.am b/Makefile.am index 141973e..771deaf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,7 +28,6 @@ TESTS = include lib/sqfs/Makemodule.am include lib/util/Makemodule.am include lib/xfrm/Makemodule.am -include lib/io/Makemodule.am include lib/fstree/Makemodule.am include lib/common/Makemodule.am diff --git a/bin/gensquashfs/Makemodule.am b/bin/gensquashfs/Makemodule.am index 81a88a1..6410b1a 100644 --- a/bin/gensquashfs/Makemodule.am +++ b/bin/gensquashfs/Makemodule.am @@ -3,7 +3,7 @@ gensquashfs_SOURCES = bin/gensquashfs/src/mkfs.c bin/gensquashfs/src/mkfs.h \ bin/gensquashfs/src/apply_xattr.c bin/gensquashfs/src/filemap_xattr.c\ bin/gensquashfs/src/fstree_from_file.c \ bin/gensquashfs/src/sort_by_file.c bin/gensquashfs/src/glob.c -gensquashfs_LDADD = libcommon.a libsquashfs.la libio.a libfstree.a +gensquashfs_LDADD = libcommon.a libsquashfs.la libfstree.a gensquashfs_LDADD += libutil.a libcompat.a $(LZO_LIBS) $(PTHREAD_LIBS) gensquashfs_CPPFLAGS = $(AM_CPPFLAGS) gensquashfs_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) @@ -33,7 +33,7 @@ test_fstree_from_file_SOURCES = bin/gensquashfs/test/fstree_from_file.c \ bin/gensquashfs/src/mkfs.h test_fstree_from_file_CPPFLAGS = $(AM_CPPFLAGS) test_fstree_from_file_CPPFLAGS += -I$(top_srcdir)/bin/gensquashfs/src -test_fstree_from_file_LDADD = libsquashfs.la libcommon.a libfstree.a libio.a \ +test_fstree_from_file_LDADD = libsquashfs.la libcommon.a libfstree.a \ libutil.a libcompat.a test_fstree_from_file2_SOURCES = bin/gensquashfs/test/fstree_from_file2.c \ @@ -42,7 +42,7 @@ test_fstree_from_file2_SOURCES = bin/gensquashfs/test/fstree_from_file2.c \ bin/gensquashfs/src/mkfs.h test_fstree_from_file2_CPPFLAGS = $(AM_CPPFLAGS) test_fstree_from_file2_CPPFLAGS += -I$(top_srcdir)/bin/gensquashfs/src -test_fstree_from_file2_LDADD = libsquashfs.la libcommon.a libfstree.a libio.a \ +test_fstree_from_file2_LDADD = libsquashfs.la libcommon.a libfstree.a \ libutil.a libcompat.a test_fstree_glob1_SOURCES = bin/gensquashfs/test/fstree_glob1.c \ @@ -51,7 +51,7 @@ test_fstree_glob1_SOURCES = bin/gensquashfs/test/fstree_glob1.c \ bin/gensquashfs/src/mkfs.h test_fstree_glob1_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/bin/gensquashfs/src test_fstree_glob1_CPPFLAGS += -DTESTPATH=$(GENDATADIR) -test_fstree_glob1_LDADD = libsquashfs.la libcommon.a libfstree.a libio.a \ +test_fstree_glob1_LDADD = libsquashfs.la libcommon.a libfstree.a \ libutil.a libcompat.a test_sort_file_SOURCES = bin/gensquashfs/test/sort_file.c \ @@ -60,7 +60,7 @@ test_sort_file_SOURCES = bin/gensquashfs/test/sort_file.c \ bin/gensquashfs/src/glob.c \ bin/gensquashfs/src/mkfs.h test_sort_file_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/bin/gensquashfs/src -test_sort_file_LDADD = libsquashfs.la libcommon.a libfstree.a libio.a \ +test_sort_file_LDADD = libsquashfs.la libcommon.a libfstree.a \ libutil.a libcompat.a fstree_fuzz_SOURCES = bin/gensquashfs/test/fstree_fuzz.c \ @@ -68,7 +68,7 @@ fstree_fuzz_SOURCES = bin/gensquashfs/test/fstree_fuzz.c \ bin/gensquashfs/src/glob.c \ bin/gensquashfs/src/mkfs.h fstree_fuzz_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/bin/gensquashfs/src -fstree_fuzz_LDADD = libsquashfs.la libcommon.a libfstree.a libio.a \ +fstree_fuzz_LDADD = libsquashfs.la libcommon.a libfstree.a \ libutil.a libcompat.a GENSQUASHFS_TESTS = \ diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h index 06774df..8e69fc1 100644 --- a/bin/gensquashfs/src/mkfs.h +++ b/bin/gensquashfs/src/mkfs.h @@ -11,7 +11,7 @@ #include "config.h" #include "common.h" -#include "io/dir_iterator.h" +#include "dir_tree_iterator.h" #include "util/util.h" #include "util/parse.h" diff --git a/include/dir_tree_iterator.h b/include/dir_tree_iterator.h new file mode 100644 index 0000000..e9a4a79 --- /dev/null +++ b/include/dir_tree_iterator.h @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * dir_tree_iterator.h + * + * Copyright (C) 2023 David Oberhollenzer + */ +#ifndef IO_DIR_ITERATOR_H +#define IO_DIR_ITERATOR_H + +#include "sqfs/dir_entry.h" +#include "sqfs/predef.h" + +enum { + DIR_SCAN_NO_SOCK = 0x0001, + DIR_SCAN_NO_SLINK = 0x0002, + DIR_SCAN_NO_FILE = 0x0004, + DIR_SCAN_NO_BLK = 0x0008, + DIR_SCAN_NO_DIR = 0x0010, + DIR_SCAN_NO_CHR = 0x0020, + DIR_SCAN_NO_FIFO = 0x0040, + + DIR_SCAN_KEEP_TIME = 0x0100, + DIR_SCAN_KEEP_UID = 0x0200, + DIR_SCAN_KEEP_GID = 0x0400, + DIR_SCAN_KEEP_MODE = 0x0800, + + DIR_SCAN_ONE_FILESYSTEM = 0x1000, + DIR_SCAN_NO_RECURSION = 0x2000, + DIR_SCAN_NO_HARDLINKS = 0x4000, + DIR_SCAN_MATCH_FULL_PATH = 0x8000, +}; + +typedef struct { + sqfs_u32 flags; + sqfs_u32 def_uid; + sqfs_u32 def_gid; + sqfs_u32 def_mode; + sqfs_s64 def_mtime; + + /** + * @brief A prefix to attach to all returend paths + * + * If not null, this string and an additional "/" are prepended to + * all entries returned by the iterator. + */ + const char *prefix; + + /** + * @brief A glob pattern that either the name must match + * + * If this is not NULL, only paths that match this globbing pattern + * are returned. If the flag DIR_SCAN_MATCH_FULL_PATH is set, the + * entire path must match, slashes cannot match wild card characters. + * If not set, only the last part of the path is tested. The iterator + * still recurses into directories, it simply doesn't report them if + * they don't match. + */ + const char *name_pattern; +} dir_tree_cfg_t; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create a stacked, recursive directory tree iterator + * + * The underlying implementation automatically recurses into sub directories + * and returns a flattened list of entries, where each entry represents a full + * path. Advanced filtering, path pre-fixing et cetera can be configured. + * The typical "." and ".." entries are filtered out as well. + * + * @param path A path to a directory on the file system. + * @param cfg A @ref dir_tree_cfg_t filtering configuration. + * + * @return A pointer to a sqfs_dir_iterator_t implementation on success, + * NULL on error (message is printed to stderr). + */ +SQFS_INTERNAL +sqfs_dir_iterator_t *dir_tree_iterator_create(const char *path, + const dir_tree_cfg_t *cfg); + +#ifdef __cplusplus +} +#endif + +#endif /* IO_DIR_ITERATOR_H */ diff --git a/include/io/dir_iterator.h b/include/io/dir_iterator.h deleted file mode 100644 index 7154b08..0000000 --- a/include/io/dir_iterator.h +++ /dev/null @@ -1,87 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * dir_iterator.h - * - * Copyright (C) 2023 David Oberhollenzer - */ -#ifndef IO_DIR_ITERATOR_H -#define IO_DIR_ITERATOR_H - -#include "sqfs/dir_entry.h" -#include "sqfs/predef.h" - -enum { - DIR_SCAN_NO_SOCK = 0x0001, - DIR_SCAN_NO_SLINK = 0x0002, - DIR_SCAN_NO_FILE = 0x0004, - DIR_SCAN_NO_BLK = 0x0008, - DIR_SCAN_NO_DIR = 0x0010, - DIR_SCAN_NO_CHR = 0x0020, - DIR_SCAN_NO_FIFO = 0x0040, - - DIR_SCAN_KEEP_TIME = 0x0100, - DIR_SCAN_KEEP_UID = 0x0200, - DIR_SCAN_KEEP_GID = 0x0400, - DIR_SCAN_KEEP_MODE = 0x0800, - - DIR_SCAN_ONE_FILESYSTEM = 0x1000, - DIR_SCAN_NO_RECURSION = 0x2000, - DIR_SCAN_NO_HARDLINKS = 0x4000, - DIR_SCAN_MATCH_FULL_PATH = 0x8000, -}; - -typedef struct { - sqfs_u32 flags; - sqfs_u32 def_uid; - sqfs_u32 def_gid; - sqfs_u32 def_mode; - sqfs_s64 def_mtime; - - /** - * @brief A prefix to attach to all returend paths - * - * If not null, this string and an additional "/" are prepended to - * all entries returned by the iterator. - */ - const char *prefix; - - /** - * @brief A glob pattern that either the name must match - * - * If this is not NULL, only paths that match this globbing pattern - * are returned. If the flag DIR_SCAN_MATCH_FULL_PATH is set, the - * entire path must match, slashes cannot match wild card characters. - * If not set, only the last part of the path is tested. The iterator - * still recurses into directories, it simply doesn't report them if - * they don't match. - */ - const char *name_pattern; -} dir_tree_cfg_t; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Create a stacked, recursive directory tree iterator - * - * The underlying implementation automatically recurses into sub directories - * and returns a flattened list of entries, where each entry represents a full - * path. Advanced filtering, path pre-fixing et cetera can be configured. - * The typical "." and ".." entries are filtered out as well. - * - * @param path A path to a directory on the file system. - * @param cfg A @ref dir_tree_cfg_t filtering configuration. - * - * @return A pointer to a sqfs_dir_iterator_t implementation on success, - * NULL on error (message is printed to stderr). - */ -SQFS_INTERNAL -sqfs_dir_iterator_t *dir_tree_iterator_create(const char *path, - const dir_tree_cfg_t *cfg); - -#ifdef __cplusplus -} -#endif - -#endif /* IO_DIR_ITERATOR_H */ diff --git a/include/tar/tar.h b/include/tar/tar.h index 84cffb5..6b1bed8 100644 --- a/include/tar/tar.h +++ b/include/tar/tar.h @@ -9,7 +9,6 @@ #include "config.h" #include "compat.h" -#include "io/dir_iterator.h" #include "sqfs/io.h" #include diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index 9438d3a..5230fcf 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -7,7 +7,8 @@ libcommon_a_SOURCES = include/common.h include/simple_writer.h \ lib/common/src/writer/serialize_fstree.c lib/common/src/writer/finish.c\ lib/common/src/fstree_cli.c lib/common/src/perror.c \ lib/common/src/dir_tree.c lib/common/src/read_tree.c \ - lib/common/src/stream.c + lib/common/src/stream.c lib/common/src/dir_tree_iterator.c \ + include/dir_tree_iterator.h lib/common/src/dir_tree_iterator.c libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS) if WITH_LZO @@ -26,8 +27,24 @@ test_fstree_cli_LDADD = libcommon.a libutil.a libcompat.a test_get_node_path_SOURCES = lib/common/test/get_node_path.c test_get_node_path_LDADD = libcommon.a libsquashfs.la libcompat.a +test_dir_tree_iterator_SOURCES = lib/common/test/dir_tree_iterator.c +test_dir_tree_iterator_LDADD = libcommon.a libsquashfs.la libutil.a libcompat.a +test_dir_tree_iterator_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/sqfs/test/testdir + +test_dir_tree_iterator2_SOURCES = lib/common/test/dir_tree_iterator2.c +test_dir_tree_iterator2_LDADD = libcommon.a libsquashfs.la libutil.a libcompat.a +test_dir_tree_iterator2_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator2_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/sqfs/test/testdir + +test_dir_tree_iterator3_SOURCES = lib/common/test/dir_tree_iterator3.c +test_dir_tree_iterator3_LDADD = libcommon.a libsquashfs.la libutil.a libcompat.a +test_dir_tree_iterator3_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/sqfs/test/testdir + LIBCOMMON_TESTS = \ - test_istream_mem test_fstree_cli test_get_node_path + test_istream_mem test_fstree_cli test_get_node_path \ + test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 check_PROGRAMS += $(LIBCOMMON_TESTS) TESTS += $(LIBCOMMON_TESTS) diff --git a/lib/common/src/dir_tree_iterator.c b/lib/common/src/dir_tree_iterator.c new file mode 100644 index 0000000..f161507 --- /dev/null +++ b/lib/common/src/dir_tree_iterator.c @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * dir_tree_iterator.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" +#include "dir_tree_iterator.h" +#include "util/util.h" +#include "sqfs/error.h" +#include "sqfs/io.h" + +#include +#include + +typedef struct { + sqfs_dir_iterator_t base; + + dir_tree_cfg_t cfg; + int state; + sqfs_dir_iterator_t *rec; +} dir_tree_iterator_t; + +static bool should_skip(const dir_tree_iterator_t *dir, const sqfs_dir_entry_t *ent) +{ + unsigned int type_mask; + + if ((dir->cfg.flags & DIR_SCAN_ONE_FILESYSTEM)) { + if (ent->flags & SQFS_DIR_ENTRY_FLAG_MOUNT_POINT) + 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 sqfs_dir_entry_t *expand_path(const dir_tree_iterator_t *it, sqfs_dir_entry_t *ent) +{ + if (it->cfg.prefix != NULL && it->cfg.prefix[0] != '\0') { + size_t plen = strlen(it->cfg.prefix) + 1; + size_t slen = strlen(ent->name) + 1; + void *new = realloc(ent, sizeof(*ent) + plen + slen); + + if (new == NULL) { + free(ent); + return NULL; + } + + ent = new; + memmove(ent->name + plen, ent->name, slen); + + memcpy(ent->name, it->cfg.prefix, plen - 1); + ent->name[plen - 1] = '/'; + } + + return ent; +} + +static void apply_changes(const dir_tree_iterator_t *it, sqfs_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; + + sqfs_drop(it->rec); + free(it); +} + +static int next(sqfs_dir_iterator_t *base, sqfs_dir_entry_t **out) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + sqfs_dir_entry_t *ent; + int ret; + + if (it->state) + return it->state; +retry: + *out = NULL; + ent = NULL; + + for (;;) { + ret = it->rec->next(it->rec, &ent); + if (ret != 0) { + it->state = ret; + return ret; + } + + if (!should_skip(it, ent)) + break; + + if (S_ISDIR(ent->mode)) + it->rec->ignore_subdir(it->rec); + free(ent); + ent = NULL; + } + + 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) + it->rec->ignore_subdir(it->rec); + + 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; +} + +static int read_link(sqfs_dir_iterator_t *base, char **out) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + + if (it->state) + return it->state; + + return it->rec->read_link(it->rec, out); +} + +static int open_subdir(sqfs_dir_iterator_t *base, sqfs_dir_iterator_t **out) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + + if (it->state) + return it->state; + + return it->rec->open_subdir(it->rec, out); +} + +static void ignore_subdir(sqfs_dir_iterator_t *base) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + + if (it->state == 0) + it->rec->ignore_subdir(it->rec); +} + +static int open_file_ro(sqfs_dir_iterator_t *base, sqfs_istream_t **out) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + + if (it->state) + return it->state; + + return it->rec->open_file_ro(it->rec, out); +} + +static int read_xattr(sqfs_dir_iterator_t *base, sqfs_xattr_t **out) +{ + dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; + + if (it->state) + return it->state; + + return it->rec->read_xattr(it->rec, out); +} + +sqfs_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)); + sqfs_dir_iterator_t *dir; + int ret; + + if (it == NULL) { + perror(path); + return NULL; + } + + it->cfg = *cfg; + + ret = sqfs_dir_iterator_create_native(&dir, path, 0); + if (ret) { + perror(path); + goto fail; + } + + ret = sqfs_dir_iterator_create_recursive(&it->rec, dir); + sqfs_drop(dir); + if (ret) + goto fail_oom; + + if (!(cfg->flags & DIR_SCAN_NO_HARDLINKS)) { + ret = sqfs_hard_link_filter_create(&dir, it->rec); + sqfs_drop(it->rec); + it->rec = dir; + if (ret) + goto fail_oom; + } + + sqfs_object_init(it, destroy, NULL); + ((sqfs_dir_iterator_t *)it)->next = next; + ((sqfs_dir_iterator_t *)it)->read_link = read_link; + ((sqfs_dir_iterator_t *)it)->open_subdir = open_subdir; + ((sqfs_dir_iterator_t *)it)->ignore_subdir = ignore_subdir; + ((sqfs_dir_iterator_t *)it)->open_file_ro = open_file_ro; + ((sqfs_dir_iterator_t *)it)->read_xattr = read_xattr; + + return (sqfs_dir_iterator_t *)it; +fail_oom: + fprintf(stderr, "%s: out of memory\n", path); +fail: + free(it); + return NULL; +} diff --git a/lib/common/test/dir_tree_iterator.c b/lib/common/test/dir_tree_iterator.c new file mode 100644 index 0000000..2a065d8 --- /dev/null +++ b/lib/common/test/dir_tree_iterator.c @@ -0,0 +1,254 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "sqfs/io.h" +#include "compat.h" +#include "common.h" + +static int compare_entries(const void *a, const void *b) +{ + const sqfs_dir_entry_t *const *lhs = a; + const sqfs_dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + sqfs_dir_entry_t *ent[17]; + sqfs_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; + cfg.flags = DIR_SCAN_NO_HARDLINKS; + + 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->ignore_subdir(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->ignore_subdir(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/common/test/dir_tree_iterator2.c b/lib/common/test/dir_tree_iterator2.c new file mode 100644 index 0000000..4d92260 --- /dev/null +++ b/lib/common/test/dir_tree_iterator2.c @@ -0,0 +1,225 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "sqfs/io.h" +#include "compat.h" +#include "common.h" + +static int compare_entries(const void *a, const void *b) +{ + const sqfs_dir_entry_t *const *lhs = a; + const sqfs_dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + sqfs_dir_entry_t *ent[17]; + sqfs_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_SCAN_NO_HARDLINKS; + + 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_SCAN_NO_HARDLINKS; + + 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_SCAN_NO_HARDLINKS; + + 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"; + cfg.flags = DIR_SCAN_NO_HARDLINKS; + + 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/common/test/dir_tree_iterator3.c b/lib/common/test/dir_tree_iterator3.c new file mode 100644 index 0000000..612a7eb --- /dev/null +++ b/lib/common/test/dir_tree_iterator3.c @@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_tree_iterator2.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "dir_tree_iterator.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "sqfs/io.h" +#include "compat.h" +#include "common.h" + +static int compare_entries(const void *a, const void *b) +{ + const sqfs_dir_entry_t *const *lhs = a; + const sqfs_dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +int main(int argc, char **argv) +{ + sqfs_dir_entry_t *ent[17]; + sqfs_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*"; + cfg.flags = DIR_SCAN_NO_HARDLINKS; + + 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_SCAN_NO_HARDLINKS; + + 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/Makemodule.am b/lib/io/Makemodule.am deleted file mode 100644 index 75a3e59..0000000 --- a/lib/io/Makemodule.am +++ /dev/null @@ -1,34 +0,0 @@ -libio_a_SOURCES = include/io/dir_iterator.h \ - lib/io/src/dir_tree_iterator.c -libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) -libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS) - -noinst_LIBRARIES += libio.a - -LIBIO_TESTS = test_dir_iterator \ - test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 - -test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c -test_dir_iterator_LDADD = libio.a libsquashfs.la 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 libsquashfs.la 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 libsquashfs.la 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 libsquashfs.la libutil.a libcompat.a -test_dir_tree_iterator3_CPPFLAGS = $(AM_CPPFLAGS) -test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir - -check_PROGRAMS += $(LIBIO_TESTS) -TESTS += $(LIBIO_TESTS) - -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 deleted file mode 100644 index 5688489..0000000 --- a/lib/io/src/dir_tree_iterator.c +++ /dev/null @@ -1,257 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * dir_tree_iterator.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" -#include "io/dir_iterator.h" -#include "util/util.h" -#include "sqfs/error.h" -#include "sqfs/io.h" - -#include -#include - -typedef struct { - sqfs_dir_iterator_t base; - - dir_tree_cfg_t cfg; - int state; - sqfs_dir_iterator_t *rec; -} dir_tree_iterator_t; - -static bool should_skip(const dir_tree_iterator_t *dir, const sqfs_dir_entry_t *ent) -{ - unsigned int type_mask; - - if ((dir->cfg.flags & DIR_SCAN_ONE_FILESYSTEM)) { - if (ent->flags & SQFS_DIR_ENTRY_FLAG_MOUNT_POINT) - 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 sqfs_dir_entry_t *expand_path(const dir_tree_iterator_t *it, sqfs_dir_entry_t *ent) -{ - if (it->cfg.prefix != NULL && it->cfg.prefix[0] != '\0') { - size_t plen = strlen(it->cfg.prefix) + 1; - size_t slen = strlen(ent->name) + 1; - void *new = realloc(ent, sizeof(*ent) + plen + slen); - - if (new == NULL) { - free(ent); - return NULL; - } - - ent = new; - memmove(ent->name + plen, ent->name, slen); - - memcpy(ent->name, it->cfg.prefix, plen - 1); - ent->name[plen - 1] = '/'; - } - - return ent; -} - -static void apply_changes(const dir_tree_iterator_t *it, sqfs_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; - - sqfs_drop(it->rec); - free(it); -} - -static int next(sqfs_dir_iterator_t *base, sqfs_dir_entry_t **out) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - sqfs_dir_entry_t *ent; - int ret; - - if (it->state) - return it->state; -retry: - *out = NULL; - ent = NULL; - - for (;;) { - ret = it->rec->next(it->rec, &ent); - if (ret != 0) { - it->state = ret; - return ret; - } - - if (!should_skip(it, ent)) - break; - - if (S_ISDIR(ent->mode)) - it->rec->ignore_subdir(it->rec); - free(ent); - ent = NULL; - } - - 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) - it->rec->ignore_subdir(it->rec); - - 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; -} - -static int read_link(sqfs_dir_iterator_t *base, char **out) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - - if (it->state) - return it->state; - - return it->rec->read_link(it->rec, out); -} - -static int open_subdir(sqfs_dir_iterator_t *base, sqfs_dir_iterator_t **out) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - - if (it->state) - return it->state; - - return it->rec->open_subdir(it->rec, out); -} - -static void ignore_subdir(sqfs_dir_iterator_t *base) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - - if (it->state == 0) - it->rec->ignore_subdir(it->rec); -} - -static int open_file_ro(sqfs_dir_iterator_t *base, sqfs_istream_t **out) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - - if (it->state) - return it->state; - - return it->rec->open_file_ro(it->rec, out); -} - -static int read_xattr(sqfs_dir_iterator_t *base, sqfs_xattr_t **out) -{ - dir_tree_iterator_t *it = (dir_tree_iterator_t *)base; - - if (it->state) - return it->state; - - return it->rec->read_xattr(it->rec, out); -} - -sqfs_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)); - sqfs_dir_iterator_t *dir; - int ret; - - if (it == NULL) { - perror(path); - return NULL; - } - - it->cfg = *cfg; - - ret = sqfs_dir_iterator_create_native(&dir, path, 0); - if (ret) { - perror(path); - goto fail; - } - - ret = sqfs_dir_iterator_create_recursive(&it->rec, dir); - sqfs_drop(dir); - if (ret) - goto fail_oom; - - if (!(cfg->flags & DIR_SCAN_NO_HARDLINKS)) { - ret = sqfs_hard_link_filter_create(&dir, it->rec); - sqfs_drop(it->rec); - it->rec = dir; - if (ret) - goto fail_oom; - } - - sqfs_object_init(it, destroy, NULL); - ((sqfs_dir_iterator_t *)it)->next = next; - ((sqfs_dir_iterator_t *)it)->read_link = read_link; - ((sqfs_dir_iterator_t *)it)->open_subdir = open_subdir; - ((sqfs_dir_iterator_t *)it)->ignore_subdir = ignore_subdir; - ((sqfs_dir_iterator_t *)it)->open_file_ro = open_file_ro; - ((sqfs_dir_iterator_t *)it)->read_xattr = read_xattr; - - return (sqfs_dir_iterator_t *)it; -fail_oom: - fprintf(stderr, "%s: out of memory\n", path); -fail: - free(it); - return NULL; -} diff --git a/lib/io/test/dir_iterator.c b/lib/io/test/dir_iterator.c deleted file mode 100644 index 56610b6..0000000 --- a/lib/io/test/dir_iterator.c +++ /dev/null @@ -1,417 +0,0 @@ -/* 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 "sqfs/io.h" -#include "compat.h" - -static int compare_entries(const void *a, const void *b) -{ - const sqfs_dir_entry_t *const *lhs = a; - const sqfs_dir_entry_t *const *rhs = b; - - return strcmp((*lhs)->name, (*rhs)->name); -} - -static int compare_files(const void *a, const void *b) -{ - const sqfs_istream_t *const *lhs = a; - const sqfs_istream_t *const *rhs = b; - - return strcmp((*lhs)->get_filename(*lhs), - (*rhs)->get_filename(*rhs)); -} - -int main(int argc, char **argv) -{ - sqfs_dir_iterator_t *dir, *suba, *subb, *subc, *sub; - sqfs_dir_entry_t *dent, *ent[6]; - sqfs_istream_t *files[3]; - char buffer[128]; - size_t i, j; - int ret; - (void)argc; (void)argv; - - /* scan the top level hierarchy */ - ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); - TEST_EQUAL_I(ret, 0); - 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 */ - ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dira", 0); - TEST_EQUAL_I(ret, 0); - 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 */ - ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirb", 0); - TEST_EQUAL_I(ret, 0); - 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 */ - ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirc", 0); - TEST_EQUAL_I(ret, 0); - 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; - - ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); - TEST_EQUAL_I(ret, 0); - 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 */ - j = 0; - - 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); - - TEST_ASSERT(j < 3); - ret = subc->open_file_ro(subc, &(files[j++])); - TEST_EQUAL_I(ret, 0); - } - } - - TEST_EQUAL_UI(j, 3); - - ret = subc->next(subc, &dent); - TEST_NULL(dent); - TEST_ASSERT(ret > 0); - subc = sqfs_drop(subc); - - qsort(ent, 5, sizeof(ent[0]), compare_entries); - qsort(files, 3, sizeof(files[0]), compare_files); - - 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_STR_EQUAL(files[0]->get_filename(files[0]), "file_c0"); - TEST_STR_EQUAL(files[1]->get_filename(files[1]), "file_c1"); - TEST_STR_EQUAL(files[2]->get_filename(files[2]), "file_c2"); - - ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); - TEST_EQUAL_I(ret, 12); - buffer[ret] = '\0'; - TEST_STR_EQUAL(buffer, "test string\n"); - ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); - TEST_EQUAL_I(ret, 0); - - ret = sqfs_istream_read(files[1], buffer, sizeof(buffer)); - TEST_EQUAL_I(ret, 0); - - ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); - TEST_EQUAL_I(ret, 10); - buffer[ret] = '\0'; - TEST_STR_EQUAL(buffer, "你好!\n"); - ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); - TEST_EQUAL_I(ret, 0); - - for (i = 0; i < j; ++i) - sqfs_drop(files[i]); - - return EXIT_SUCCESS; -} diff --git a/lib/io/test/dir_tree_iterator.c b/lib/io/test/dir_tree_iterator.c deleted file mode 100644 index 6c7f1bf..0000000 --- a/lib/io/test/dir_tree_iterator.c +++ /dev/null @@ -1,253 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * dir_tree_iterator.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#include "io/dir_iterator.h" -#include "sqfs/error.h" -#include "util/test.h" -#include "sqfs/io.h" -#include "compat.h" - -static int compare_entries(const void *a, const void *b) -{ - const sqfs_dir_entry_t *const *lhs = a; - const sqfs_dir_entry_t *const *rhs = b; - - return strcmp((*lhs)->name, (*rhs)->name); -} - -int main(int argc, char **argv) -{ - sqfs_dir_entry_t *ent[17]; - sqfs_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; - cfg.flags = DIR_SCAN_NO_HARDLINKS; - - 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->ignore_subdir(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->ignore_subdir(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 deleted file mode 100644 index 9184957..0000000 --- a/lib/io/test/dir_tree_iterator2.c +++ /dev/null @@ -1,224 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * dir_tree_iterator2.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" - -#include "io/dir_iterator.h" -#include "sqfs/error.h" -#include "util/test.h" -#include "sqfs/io.h" -#include "compat.h" - -static int compare_entries(const void *a, const void *b) -{ - const sqfs_dir_entry_t *const *lhs = a; - const sqfs_dir_entry_t *const *rhs = b; - - return strcmp((*lhs)->name, (*rhs)->name); -} - -int main(int argc, char **argv) -{ - sqfs_dir_entry_t *ent[17]; - sqfs_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_SCAN_NO_HARDLINKS; - - 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_SCAN_NO_HARDLINKS; - - 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_SCAN_NO_HARDLINKS; - - 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"; - cfg.flags = DIR_SCAN_NO_HARDLINKS; - - 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 deleted file mode 100644 index ccb6bdb..0000000 --- a/lib/io/test/dir_tree_iterator3.c +++ /dev/null @@ -1,107 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * dir_tree_iterator2.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" - -#include "io/dir_iterator.h" -#include "sqfs/error.h" -#include "util/test.h" -#include "sqfs/io.h" -#include "compat.h" - -static int compare_entries(const void *a, const void *b) -{ - const sqfs_dir_entry_t *const *lhs = a; - const sqfs_dir_entry_t *const *rhs = b; - - return strcmp((*lhs)->name, (*rhs)->name); -} - -int main(int argc, char **argv) -{ - sqfs_dir_entry_t *ent[17]; - sqfs_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*"; - cfg.flags = DIR_SCAN_NO_HARDLINKS; - - 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_SCAN_NO_HARDLINKS; - - 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 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dira/file_a1 b/lib/io/test/testdir/dira/file_a1 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dira/file_a2 b/lib/io/test/testdir/dira/file_a2 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/dirx/file_x0 b/lib/io/test/testdir/dirb/dirx/file_x0 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/dirx/file_x1 b/lib/io/test/testdir/dirb/dirx/file_x1 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/dirx/file_x2 b/lib/io/test/testdir/dirb/dirx/file_x2 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/file_b0 b/lib/io/test/testdir/dirb/file_b0 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/file_b1 b/lib/io/test/testdir/dirb/file_b1 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirb/file_b2 b/lib/io/test/testdir/dirb/file_b2 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirc/file_c0 b/lib/io/test/testdir/dirc/file_c0 deleted file mode 100644 index 71c1922..0000000 --- a/lib/io/test/testdir/dirc/file_c0 +++ /dev/null @@ -1 +0,0 @@ -test string diff --git a/lib/io/test/testdir/dirc/file_c1 b/lib/io/test/testdir/dirc/file_c1 deleted file mode 100644 index e69de29..0000000 diff --git a/lib/io/test/testdir/dirc/file_c2 b/lib/io/test/testdir/dirc/file_c2 deleted file mode 100644 index efde6ef..0000000 --- a/lib/io/test/testdir/dirc/file_c2 +++ /dev/null @@ -1 +0,0 @@ -你好! diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 41ea608..df0b21a 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -139,11 +139,18 @@ test_rec_dir_LDADD = libsquashfs.la libutil.a libcompat.a test_hl_dir_SOURCES = lib/sqfs/test/hl_dir.c test_hl_dir_LDADD = libsquashfs.la libutil.a libcompat.a +test_dir_iterator_SOURCES = lib/sqfs/test/dir_iterator.c +test_dir_iterator_LDADD = libsquashfs.la libutil.a libcompat.a +test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) +test_dir_iterator_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/sqfs/test/testdir + LIBSQFS_TESTS = \ test_abi test_xattr test_table test_xattr_writer \ test_istream_read test_istream_skip test_stream_splice test_rec_dir \ - test_hl_dir + test_hl_dir test_dir_iterator noinst_PROGRAMS += xattr_benchmark check_PROGRAMS += $(LIBSQFS_TESTS) TESTS += $(LIBSQFS_TESTS) + +EXTRA_DIST += $(top_srcdir)/lib/sqfs/test/testdir diff --git a/lib/sqfs/test/dir_iterator.c b/lib/sqfs/test/dir_iterator.c new file mode 100644 index 0000000..701b8f0 --- /dev/null +++ b/lib/sqfs/test/dir_iterator.c @@ -0,0 +1,417 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * dir_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "sqfs/dir_entry.h" +#include "sqfs/error.h" +#include "util/test.h" +#include "sqfs/io.h" +#include "compat.h" + +static int compare_entries(const void *a, const void *b) +{ + const sqfs_dir_entry_t *const *lhs = a; + const sqfs_dir_entry_t *const *rhs = b; + + return strcmp((*lhs)->name, (*rhs)->name); +} + +static int compare_files(const void *a, const void *b) +{ + const sqfs_istream_t *const *lhs = a; + const sqfs_istream_t *const *rhs = b; + + return strcmp((*lhs)->get_filename(*lhs), + (*rhs)->get_filename(*rhs)); +} + +int main(int argc, char **argv) +{ + sqfs_dir_iterator_t *dir, *suba, *subb, *subc, *sub; + sqfs_dir_entry_t *dent, *ent[6]; + sqfs_istream_t *files[3]; + char buffer[128]; + size_t i, j; + int ret; + (void)argc; (void)argv; + + /* scan the top level hierarchy */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); + TEST_EQUAL_I(ret, 0); + 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 */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dira", 0); + TEST_EQUAL_I(ret, 0); + 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 */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirb", 0); + TEST_EQUAL_I(ret, 0); + 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 */ + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH "/dirc", 0); + TEST_EQUAL_I(ret, 0); + 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; + + ret = sqfs_dir_iterator_create_native(&dir, TEST_PATH, 0); + TEST_EQUAL_I(ret, 0); + 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 */ + j = 0; + + 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); + + TEST_ASSERT(j < 3); + ret = subc->open_file_ro(subc, &(files[j++])); + TEST_EQUAL_I(ret, 0); + } + } + + TEST_EQUAL_UI(j, 3); + + ret = subc->next(subc, &dent); + TEST_NULL(dent); + TEST_ASSERT(ret > 0); + subc = sqfs_drop(subc); + + qsort(ent, 5, sizeof(ent[0]), compare_entries); + qsort(files, 3, sizeof(files[0]), compare_files); + + 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_STR_EQUAL(files[0]->get_filename(files[0]), "file_c0"); + TEST_STR_EQUAL(files[1]->get_filename(files[1]), "file_c1"); + TEST_STR_EQUAL(files[2]->get_filename(files[2]), "file_c2"); + + ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 12); + buffer[ret] = '\0'; + TEST_STR_EQUAL(buffer, "test string\n"); + ret = sqfs_istream_read(files[0], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + ret = sqfs_istream_read(files[1], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 10); + buffer[ret] = '\0'; + TEST_STR_EQUAL(buffer, "你好!\n"); + ret = sqfs_istream_read(files[2], buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + for (i = 0; i < j; ++i) + sqfs_drop(files[i]); + + return EXIT_SUCCESS; +} diff --git a/lib/sqfs/test/testdir/dira/file_a0 b/lib/sqfs/test/testdir/dira/file_a0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dira/file_a1 b/lib/sqfs/test/testdir/dira/file_a1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dira/file_a2 b/lib/sqfs/test/testdir/dira/file_a2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x0 b/lib/sqfs/test/testdir/dirb/dirx/file_x0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x1 b/lib/sqfs/test/testdir/dirb/dirx/file_x1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/dirx/file_x2 b/lib/sqfs/test/testdir/dirb/dirx/file_x2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b0 b/lib/sqfs/test/testdir/dirb/file_b0 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b1 b/lib/sqfs/test/testdir/dirb/file_b1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirb/file_b2 b/lib/sqfs/test/testdir/dirb/file_b2 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirc/file_c0 b/lib/sqfs/test/testdir/dirc/file_c0 new file mode 100644 index 0000000..71c1922 --- /dev/null +++ b/lib/sqfs/test/testdir/dirc/file_c0 @@ -0,0 +1 @@ +test string diff --git a/lib/sqfs/test/testdir/dirc/file_c1 b/lib/sqfs/test/testdir/dirc/file_c1 new file mode 100644 index 0000000..e69de29 diff --git a/lib/sqfs/test/testdir/dirc/file_c2 b/lib/sqfs/test/testdir/dirc/file_c2 new file mode 100644 index 0000000..efde6ef --- /dev/null +++ b/lib/sqfs/test/testdir/dirc/file_c2 @@ -0,0 +1 @@ +你好! diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c index 8f3353e..570f687 100644 --- a/lib/tar/src/iterator.c +++ b/lib/tar/src/iterator.c @@ -12,6 +12,7 @@ #include "util/util.h" #include "xfrm/wrap.h" #include "compat.h" +#include "sqfs/dir_entry.h" #include #include diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c index b90d5ca..4e89380 100644 --- a/lib/tar/test/tar_iterator.c +++ b/lib/tar/test/tar_iterator.c @@ -9,6 +9,7 @@ #include "util/test.h" #include "sqfs/error.h" #include "sqfs/io.h" +#include "sqfs/dir_entry.h" #ifndef TESTUID #define TESTUID 1000 diff --git a/lib/tar/test/tar_iterator2.c b/lib/tar/test/tar_iterator2.c index 630f579..e983c27 100644 --- a/lib/tar/test/tar_iterator2.c +++ b/lib/tar/test/tar_iterator2.c @@ -8,6 +8,7 @@ #include "tar/tar.h" #include "util/test.h" #include "sqfs/io.h" +#include "sqfs/dir_entry.h" static const struct { uint64_t offset; diff --git a/lib/tar/test/tar_iterator3.c b/lib/tar/test/tar_iterator3.c index 05940a2..e9bb472 100644 --- a/lib/tar/test/tar_iterator3.c +++ b/lib/tar/test/tar_iterator3.c @@ -10,6 +10,7 @@ #include "util/test.h" #include "sqfs/error.h" #include "sqfs/io.h" +#include "sqfs/dir_entry.h" int main(int argc, char **argv) { diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index c82fe08..225841e 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -9,6 +9,7 @@ #include "sqfs/io.h" #include "util/test.h" #include "sqfs/xattr.h" +#include "sqfs/dir_entry.h" #include "compat.h" static void hex_dump(const sqfs_u8 *data, size_t size) -- cgit v1.2.3