diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2024-03-04 19:59:15 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2024-03-05 16:28:05 +0100 |
commit | 8208b020c4d22b3fd02c32be4d1503a0dab8327f (patch) | |
tree | 6b63d3197a7b48828371c6fbea084370ae450b0d | |
parent | 4378835b3730d778bbc5bb7916bb076accddbb03 (diff) | |
download | barebox-8208b020c4d2.tar.gz barebox-8208b020c4d2.tar.xz |
fs: factor out opendir iteration
In preparation for supporting fdopendir and rewinddir, add an __opendir
helper that is reused in a follow-up commit.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20240304190038.3486881-31-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r-- | fs/fs.c | 52 |
1 files changed, 32 insertions, 20 deletions
@@ -2708,31 +2708,49 @@ out: } EXPORT_SYMBOL(symlink); -static void release_dir(DIR *d) +static void __release_dir(DIR *d) { struct readdir_entry *entry, *tmp; list_for_each_entry_safe(entry, tmp, &d->entries, list) { free(entry); } - - free(d); } -DIR *opendir(const char *pathname) +static int __opendir(DIR *d) { int ret; - struct dentry *dir; - struct inode *inode; struct file file = {}; - DIR *d; - struct path path = {}; + struct path *path = &d->path; + struct dentry *dir = path->dentry; struct readdir_callback rd = { .ctx = { .actor = fillonedir, }, }; + file.f_path.dentry = dir; + file.f_inode = d_inode(dir); + file.f_op = dir->d_inode->i_fop; + + INIT_LIST_HEAD(&d->entries); + rd.dir = d; + + ret = file.f_op->iterate(&file, &rd.ctx); + if (ret) + __release_dir(d); + + return ret; +} + +DIR *opendir(const char *pathname) +{ + int ret; + struct dentry *dir; + struct inode *inode; + DIR *d; + struct path path = {}; + ret = filename_lookup(getname(pathname), LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); if (ret) @@ -2752,24 +2770,17 @@ DIR *opendir(const char *pathname) goto out_put; } - file.f_path.dentry = dir; - file.f_inode = d_inode(dir); - file.f_op = dir->d_inode->i_fop; - d = xzalloc(sizeof(*d)); d->path = path; - INIT_LIST_HEAD(&d->entries); - rd.dir = d; - - ret = file.f_op->iterate(&file, &rd.ctx); + ret = __opendir(d); if (ret) - goto out_release; + goto out_free; return d; -out_release: - release_dir(d); +out_free: + free(d); out_put: path_put(&path); out: @@ -2785,7 +2796,8 @@ int closedir(DIR *dir) return errno_set(-EBADF); path_put(&dir->path); - release_dir(dir); + __release_dir(dir); + free(dir); return 0; } |