aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-07-13 20:11:10 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-08-10 09:26:59 +0200
commitb79fa32baf127c30d386888c3830129e132a855d (patch)
tree69ccf1a90c211ba2da134f23657bcd21586d65e5
parente16f993bba70e7541a32f0e3e7080d2ab3aeb762 (diff)
libio: dir iterator: merge common code paths for initialization
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--lib/io/src/unix/dir_iterator.c76
-rw-r--r--lib/io/src/win32/dir_iterator.c57
2 files changed, 62 insertions, 71 deletions
diff --git a/lib/io/src/unix/dir_iterator.c b/lib/io/src/unix/dir_iterator.c
index 1cec198..12e2ac1 100644
--- a/lib/io/src/unix/dir_iterator.c
+++ b/lib/io/src/unix/dir_iterator.c
@@ -140,10 +140,12 @@ static int dir_read_xattr(dir_iterator_t *it, sqfs_xattr_t **out)
return 0;
}
+static int create_iterator(dir_iterator_t **out, DIR *dir);
+
static int dir_open_subdir(dir_iterator_t *base, dir_iterator_t **out)
{
const unix_dir_iterator_t *it = (const unix_dir_iterator_t *)base;
- unix_dir_iterator_t *sub = NULL;
+ DIR *dir;
int fd;
*out = NULL;
@@ -161,59 +163,34 @@ static int dir_open_subdir(dir_iterator_t *base, dir_iterator_t **out)
return SQFS_ERROR_IO;
}
- sub = calloc(1, sizeof(*sub));
- if (sub == NULL)
- goto fail_alloc;
-
- sub->dir = fdopendir(fd);
- if (sub->dir == NULL)
- goto fail_alloc;
-
- if (fstat(dirfd(sub->dir), &sub->sb)) {
- free(sub);
+ dir = fdopendir(fd);
+ if (dir == NULL) {
+ int err = errno;
+ close(fd);
+ errno = err;
return SQFS_ERROR_IO;
}
- sqfs_object_init(sub, dir_destroy, NULL);
- sub->device = sub->sb.st_dev;
- ((dir_iterator_t *)sub)->next = dir_next;
- ((dir_iterator_t *)sub)->read_link = dir_read_link;
- ((dir_iterator_t *)sub)->open_subdir = dir_open_subdir;
- ((dir_iterator_t *)sub)->ignore_subdir = dir_ignore_subdir;
- ((dir_iterator_t *)sub)->open_file_ro = dir_open_file_ro;
- ((dir_iterator_t *)sub)->read_xattr = dir_read_xattr;
-
- *out = (dir_iterator_t *)sub;
- return 0;
-fail_alloc:
- free(sub);
- close(fd);
- return SQFS_ERROR_ALLOC;
+ return create_iterator(out, dir);
}
-dir_iterator_t *dir_iterator_create(const char *path)
+static int create_iterator(dir_iterator_t **out, DIR *dir)
{
unix_dir_iterator_t *it = calloc(1, sizeof(*it));
if (it == NULL) {
- perror(path);
- return NULL;
+ closedir(dir);
+ return SQFS_ERROR_ALLOC;
}
- it->state = 0;
- it->dir = opendir(path);
+ it->dir = dir;
- if (it->dir == NULL) {
- perror(path);
+ if (fstat(dirfd(dir), &it->sb)) {
+ int err = errno;
+ closedir(dir);
free(it);
- return NULL;
- }
-
- if (fstat(dirfd(it->dir), &it->sb)) {
- perror(path);
- closedir(it->dir);
- free(it);
- return NULL;
+ errno = err;
+ return SQFS_ERROR_IO;
}
sqfs_object_init(it, dir_destroy, NULL);
@@ -225,5 +202,20 @@ dir_iterator_t *dir_iterator_create(const char *path)
((dir_iterator_t *)it)->open_file_ro = dir_open_file_ro;
((dir_iterator_t *)it)->read_xattr = dir_read_xattr;
- return (dir_iterator_t *)it;
+ *out = (dir_iterator_t *)it;
+ return 0;
+}
+
+dir_iterator_t *dir_iterator_create(const char *path)
+{
+ dir_iterator_t *out;
+ DIR *dir;
+
+ dir = opendir(path);
+ if (dir == NULL || create_iterator(&out, dir) != 0) {
+ perror(path);
+ return NULL;
+ }
+
+ return out;
}
diff --git a/lib/io/src/win32/dir_iterator.c b/lib/io/src/win32/dir_iterator.c
index b7a47b9..d9e5b7c 100644
--- a/lib/io/src/win32/dir_iterator.c
+++ b/lib/io/src/win32/dir_iterator.c
@@ -133,11 +133,14 @@ static int dir_iterator_read_xattr(dir_iterator_t *it, sqfs_xattr_t **out)
return 0;
}
+static int dir_iterator_init(dir_iterator_win32_t *it);
+
static int dir_iterator_open_subdir(dir_iterator_t *it, dir_iterator_t **out)
{
const dir_iterator_win32_t *dir = (const dir_iterator_win32_t *)it;
dir_iterator_win32_t *sub = NULL;
size_t plen, slen, total;
+ int ret;
*out = NULL;
@@ -161,23 +164,33 @@ static int dir_iterator_open_subdir(dir_iterator_t *it, dir_iterator_t **out)
sub->path[plen + slen + 1] = '*';
sub->path[plen + slen + 2] = '\0';
- sqfs_object_init(sub, dir_iterator_destroy, NULL);
- ((dir_iterator_t *)sub)->next = dir_iterator_next;
- ((dir_iterator_t *)sub)->read_link = dir_iterator_read_link;
- ((dir_iterator_t *)sub)->open_subdir = dir_iterator_open_subdir;
- ((dir_iterator_t *)sub)->ignore_subdir = dir_iterator_ignore_subdir;
- ((dir_iterator_t *)sub)->open_file_ro = dir_iterator_open_file_ro;
- ((dir_iterator_t *)sub)->read_xattr = dir_iterator_read_xattr;
- sub->is_first = true;
- sub->state = 0;
-
- sub->dirhnd = FindFirstFileW(sub->path, &sub->ent);
- if (sub->dirhnd == INVALID_HANDLE_VALUE) {
+ ret = dir_iterator_init(sub);
+ if (ret != 0) {
free(sub);
- return SQFS_ERROR_IO;
+ sub = NULL;
}
*out = (dir_iterator_t *)sub;
+ return ret;
+}
+
+static int dir_iterator_init(dir_iterator_win32_t *it)
+{
+ sqfs_object_init(it, dir_iterator_destroy, NULL);
+
+ ((dir_iterator_t *)it)->next = dir_iterator_next;
+ ((dir_iterator_t *)it)->read_link = dir_iterator_read_link;
+ ((dir_iterator_t *)it)->open_subdir = dir_iterator_open_subdir;
+ ((dir_iterator_t *)it)->ignore_subdir = dir_iterator_ignore_subdir;
+ ((dir_iterator_t *)it)->open_file_ro = dir_iterator_open_file_ro;
+ ((dir_iterator_t *)it)->read_xattr = dir_iterator_read_xattr;
+ it->is_first = true;
+ it->state = 0;
+
+ it->dirhnd = FindFirstFileW(it->path, &it->ent);
+ if (it->dirhnd == INVALID_HANDLE_VALUE)
+ return SQFS_ERROR_IO;
+
return 0;
}
@@ -222,24 +235,10 @@ dir_iterator_t *dir_iterator_create(const char *path)
/* initialize */
memset(it, 0, offsetof(dir_iterator_win32_t, path));
- sqfs_object_init(it, dir_iterator_destroy, NULL);
-
- ((dir_iterator_t *)it)->next = dir_iterator_next;
- ((dir_iterator_t *)it)->read_link = dir_iterator_read_link;
- ((dir_iterator_t *)it)->open_subdir = dir_iterator_open_subdir;
- ((dir_iterator_t *)it)->ignore_subdir = dir_iterator_ignore_subdir;
- ((dir_iterator_t *)it)->open_file_ro = dir_iterator_open_file_ro;
- ((dir_iterator_t *)it)->read_xattr = dir_iterator_read_xattr;
- it->is_first = true;
- it->state = 0;
-
- /* get the directory handle AND the first entry */
- it->dirhnd = FindFirstFileW(it->path, &it->ent);
-
- if (it->dirhnd == INVALID_HANDLE_VALUE) {
+ if (dir_iterator_init(it) != 0) {
w32_perror(path);
free(it);
- return NULL;
+ it = NULL;
}
return (dir_iterator_t *)it;