diff options
author | Scott Prager <splinterofchaos@gmail.com> | 2014-09-20 17:34:57 -0400 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2015-03-31 11:20:23 -0300 |
commit | 99869989c8be45033f82b567f988a67af6ffda7b (patch) | |
tree | aaffd7ec5f77571f0bec2303c8419beee3d44c2d | |
parent | 27ead64da044f841515d52e273dc3f963e6c678c (diff) | |
download | rneovim-99869989c8be45033f82b567f988a67af6ffda7b.tar.gz rneovim-99869989c8be45033f82b567f988a67af6ffda7b.tar.bz2 rneovim-99869989c8be45033f82b567f988a67af6ffda7b.zip |
os_scandir(), scandir_next(), and os_closedir()
-rw-r--r-- | src/nvim/os/fs.c | 33 | ||||
-rw-r--r-- | src/nvim/os/fs_defs.h | 5 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 6200685076..d583323b1f 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -348,6 +348,39 @@ int os_rmdir(const char *path) return result; } +/// Opens a directory. +/// @param[out] dir The Directory object. +/// @param path Path to the directory. +/// @returns true if dir contains one or more items, false if not or an error +/// occurred. +bool os_scandir(Directory *dir, const char *path) + FUNC_ATTR_NONNULL_ALL +{ + int r = uv_fs_scandir(uv_default_loop(), &dir->request, path, 0, NULL); + if (r <= 0) { + os_closedir(dir); + } + return r > 0; +} + +/// Increments the directory pointer. +/// @param dir The Directory object. +/// @returns a pointer to the next path in `dir` or `NULL`. +const char *os_scandir_next(Directory *dir) + FUNC_ATTR_NONNULL_ALL +{ + int err = uv_fs_scandir_next(&dir->request, &dir->ent); + return err != UV_EOF ? dir->ent.name : NULL; +} + +/// Frees memory associated with `os_scandir()`. +/// @param dir The directory. +void os_closedir(Directory *dir) + FUNC_ATTR_NONNULL_ALL +{ + uv_fs_req_cleanup(&dir->request); +} + /// Remove a file. /// /// @return `0` for success, non-zero for failure. diff --git a/src/nvim/os/fs_defs.h b/src/nvim/os/fs_defs.h index e4d79aab66..ddd382a3cb 100644 --- a/src/nvim/os/fs_defs.h +++ b/src/nvim/os/fs_defs.h @@ -16,4 +16,9 @@ typedef struct { #define FILE_ID_EMPTY (FileID) {.inode = 0, .device_id = 0} +typedef struct { + uv_fs_t request; ///< @private The request to uv for the directory. + uv_dirent_t ent; ///< @private The entry information. +} Directory; + #endif // NVIM_OS_FS_DEFS_H |