diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-05-14 13:13:38 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-05-14 13:13:38 +0200 |
commit | 20a4e5acd78dcb5f681ab5320adb0289cebfc277 (patch) | |
tree | 6f394a029f4e985d7323ec9ef8a09a3a6610c280 | |
parent | 7d01bbb78c11cb54c96cf9708916a116190364cb (diff) |
libio: Add a function to open an istream_t from a directory iterator
For the native versions, this is currently dummied out, always returning
an error number. The idea is to laster wrap the libtar interface around
a directory iterator, here we need that method to support the existing
use case in tar2sqfs.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/io/dir_iterator.h | 14 | ||||
-rw-r--r-- | lib/io/src/dir_tree_iterator.c | 13 | ||||
-rw-r--r-- | lib/io/src/unix/dir_iterator.c | 9 | ||||
-rw-r--r-- | lib/io/src/win32/dir_iterator.c | 9 |
4 files changed, 45 insertions, 0 deletions
diff --git a/include/io/dir_iterator.h b/include/io/dir_iterator.h index 60c3796..b7212b3 100644 --- a/include/io/dir_iterator.h +++ b/include/io/dir_iterator.h @@ -8,6 +8,7 @@ #define IO_DIR_ITERATOR_H #include "sqfs/predef.h" +#include "io/istream.h" /** * @struct dir_entry_t @@ -136,6 +137,19 @@ typedef struct dir_iterator_t { * @param it A pointer to the iterator itself. */ void (*ignore_subdir)(struct dir_iterator_t *it); + + /** + * @brief If the last entry was a regular file, open it. + * + * If next() returned a file, this can be used to create an istream + * to read from it. + * + * @param it A pointer to the iterator itself. + * @param out Returns a pointer to a @ref istream_t on success. + * + * @return Zero on success, negative @ref SQFS_ERROR value on failure. + */ + int (*open_file_ro)(struct dir_iterator_t *it, istream_t **out); } dir_iterator_t; enum { diff --git a/lib/io/src/dir_tree_iterator.c b/lib/io/src/dir_tree_iterator.c index 604efd5..67d33c3 100644 --- a/lib/io/src/dir_tree_iterator.c +++ b/lib/io/src/dir_tree_iterator.c @@ -272,6 +272,18 @@ static void ignore_subdir(dir_iterator_t *base) pop(it); } +static int open_file_ro(dir_iterator_t *base, istream_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_file_ro(it->top->dir, out); +} + dir_iterator_t *dir_tree_iterator_create(const char *path, const dir_tree_cfg_t *cfg) { @@ -303,6 +315,7 @@ dir_iterator_t *dir_tree_iterator_create(const char *path, ((dir_iterator_t *)it)->read_link = read_link; ((dir_iterator_t *)it)->open_subdir = open_subdir; ((dir_iterator_t *)it)->ignore_subdir = ignore_subdir; + ((dir_iterator_t *)it)->open_file_ro = open_file_ro; return (dir_iterator_t *)it; fail: diff --git a/lib/io/src/unix/dir_iterator.c b/lib/io/src/unix/dir_iterator.c index d2727ad..8d30b3f 100644 --- a/lib/io/src/unix/dir_iterator.c +++ b/lib/io/src/unix/dir_iterator.c @@ -126,6 +126,13 @@ static void dir_ignore_subdir(dir_iterator_t *it) (void)it; } +static int dir_open_file_ro(dir_iterator_t *it, istream_t **out) +{ + (void)it; + *out = NULL; + return SQFS_ERROR_UNSUPPORTED; +} + 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; @@ -166,6 +173,7 @@ static int dir_open_subdir(dir_iterator_t *base, dir_iterator_t **out) ((dir_iterator_t *)sub)->read_link = dir_read_link; ((dir_iterator_t *)sub)->open_subdir = dir_open_subdir; ((dir_iterator_t *)sub)->ignore_subdir = dir_ignore_subdir; + ((dir_iterator_t *)sub)->open_file_ro = dir_open_file_ro; *out = (dir_iterator_t *)sub; return 0; @@ -206,6 +214,7 @@ dir_iterator_t *dir_iterator_create(const char *path) ((dir_iterator_t *)it)->read_link = dir_read_link; ((dir_iterator_t *)it)->open_subdir = dir_open_subdir; ((dir_iterator_t *)it)->ignore_subdir = dir_ignore_subdir; + ((dir_iterator_t *)it)->open_file_ro = dir_open_file_ro; return (dir_iterator_t *)it; } diff --git a/lib/io/src/win32/dir_iterator.c b/lib/io/src/win32/dir_iterator.c index 94fd6e6..d8ac8f3 100644 --- a/lib/io/src/win32/dir_iterator.c +++ b/lib/io/src/win32/dir_iterator.c @@ -112,6 +112,13 @@ static void dir_iterator_ignore_subdir(dir_iterator_t *it) (void)it; } +static int dir_iterator_open_file_ro(dir_iterator_t *it, istream_t **out) +{ + (void)it; + *out = NULL; + return SQFS_ERROR_UNSUPPORTED; +} + 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; @@ -145,6 +152,7 @@ static int dir_iterator_open_subdir(dir_iterator_t *it, dir_iterator_t **out) ((dir_iterator_t *)sub)->read_link = dir_iterator_read_link; ((dir_iterator_t *)sub)->open_subdir = dir_iterator_open_subdir; ((dir_iterator_t *)sub)->ignore_subdir = dir_iterator_ignore_subdir; + ((dir_iterator_t *)sub)->open_file_ro = dir_iterator_open_file_ro; sub->is_first = true; sub->state = 0; @@ -205,6 +213,7 @@ dir_iterator_t *dir_iterator_create(const char *path) ((dir_iterator_t *)it)->read_link = dir_iterator_read_link; ((dir_iterator_t *)it)->open_subdir = dir_iterator_open_subdir; ((dir_iterator_t *)it)->ignore_subdir = dir_iterator_ignore_subdir; + ((dir_iterator_t *)it)->open_file_ro = dir_iterator_open_file_ro; it->is_first = true; it->state = 0; |