uvco 0.1
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
uvco::Directory Class Reference

#include <fs.h>

Classes

struct  DirEnt
 

Public Member Functions

 Directory (const Directory &)=default
 
 Directory (Directory &&other) noexcept
 
Directoryoperator= (const Directory &)=delete
 
Directoryoperator= (Directory &&other) noexcept
 
 ~Directory ()
 
Promise< std::vector< DirEnt > > read (unsigned count=64)
 Read up to count directory entries.
 
Promise< unsigned int > read (std::span< DirEnt > buffer)
 Read up to buffer.size() directory entries into that buffer.
 
Promise< void > close ()
 Close the directory.
 

Static Public Member Functions

static Promise< void > mkdir (const Loop &loop, std::string_view path, int mode=0755)
 Create a directory.
 
static Promise< void > rmdir (const Loop &loop, std::string_view path)
 Remove a directory. It must be empty.
 
static Promise< Directoryopen (const Loop &loop, std::string_view path)
 Open a directory for reading.
 
static MultiPromise< DirEntreadAll (const Loop &loop, std::string_view path)
 Read all directory entries of the given directory.
 

Private Member Functions

 Directory (uv_loop_t *loop, uv_dir_t *dir)
 

Private Attributes

uv_loop_t * loop_
 
uv_dir_t * dir_
 

Constructor & Destructor Documentation

◆ Directory() [1/3]

uvco::Directory::Directory ( const Directory )
default

◆ Directory() [2/3]

uvco::Directory::Directory ( Directory &&  other)
noexcept
97 : loop_{other.loop_}, dir_{other.dir_} {
98 other.loop_ = nullptr;
99 other.dir_ = nullptr;
100}
uv_dir_t * dir_
Definition fs.h:66
uv_loop_t * loop_
Definition fs.h:65

◆ ~Directory()

uvco::Directory::~Directory ( )
113 {
114 if (dir_ != nullptr) {
115 auto req = std::make_unique<uv_fs_t>();
116 uv_fs_closedir(loop_, req.release(), dir_, nullptr);
117 fmt::print(stderr, "Directory closed in dtor; this leaks memory. Please "
118 "use co_await close() instead\n");
119 }
120}

◆ Directory() [3/3]

uvco::Directory::Directory ( uv_loop_t *  loop,
uv_dir_t *  dir 
)
inlineexplicitprivate
63: loop_{loop}, dir_{dir} {}

Member Function Documentation

◆ close()

Promise< void > uvco::Directory::close ( )

Close the directory.

199 {
200 FileOpAwaiter_ awaiter;
201 uv_fs_closedir(loop_, &awaiter.req(), dir_, FileOpAwaiter_::uvCallback());
202 co_await awaiter;
203 dir_ = nullptr;
204 co_return;
205}

◆ mkdir()

Promise< void > uvco::Directory::mkdir ( const Loop loop,
std::string_view  path,
int  mode = 0755 
)
static

Create a directory.

123 {
124 FileOpAwaiter_ awaiter;
125
126 uv_fs_mkdir(loop.uvloop(), &awaiter.req(), path.data(), mode,
127 FileOpAwaiter_::uvCallback());
128
129 co_await awaiter;
130
131 co_return;
132}

◆ open()

Promise< Directory > uvco::Directory::open ( const Loop loop,
std::string_view  path 
)
static

Open a directory for reading.

145 {
146 FileOpAwaiter_ awaiter;
147
148 uv_fs_opendir(loop.uvloop(), &awaiter.req(), path.data(),
149 FileOpAwaiter_::uvCallback());
150
151 co_await awaiter;
152
153 co_return Directory{loop.uvloop(), (uv_dir_t *)awaiter.req().ptr};
154}
Directory(const Directory &)=default

◆ operator=() [1/2]

Directory & uvco::Directory::operator= ( const Directory )
delete

◆ operator=() [2/2]

Directory & uvco::Directory::operator= ( Directory &&  other)
noexcept
102 {
103 if (this == &other) {
104 return *this;
105 }
106 loop_ = other.loop_;
107 dir_ = other.dir_;
108 other.loop_ = nullptr;
109 other.dir_ = nullptr;
110 return *this;
111}

◆ read() [1/2]

Promise< unsigned int > uvco::Directory::read ( std::span< DirEnt buffer)

Read up to buffer.size() directory entries into that buffer.

163 {
164 // dirents vector must be declared before awaiter, because awaiter will free
165 // contents of dirents.
166 std::vector<uv_dirent_t> dirents{buffer.size()};
167 dir_->dirents = dirents.data();
168 dir_->nentries = dirents.size();
169
170 FileOpAwaiter_ awaiter;
171
172 uv_fs_readdir(loop_, &awaiter.req(), dir_, FileOpAwaiter_::uvCallback());
173
174 co_await awaiter;
175
176 unsigned int nentries = awaiter.req().result;
177 for (unsigned i = 0; i < nentries; ++i) {
178 buffer[i].name = dirents[i].name;
179 buffer[i].type = dirents[i].type;
180 }
181 co_return nentries;
182}

◆ read() [2/2]

Promise< std::vector< Directory::DirEnt > > uvco::Directory::read ( unsigned  count = 64)

Read up to count directory entries.

156 {
157 std::vector<DirEnt> result{count};
158 const unsigned int size = co_await read(result);
159 result.resize(size);
160 co_return result;
161}
Promise< std::vector< DirEnt > > read(unsigned count=64)
Read up to count directory entries.
Definition fs.cc:156

◆ readAll()

MultiPromise< Directory::DirEnt > uvco::Directory::readAll ( const Loop loop,
std::string_view  path 
)
static

Read all directory entries of the given directory.

185 {
186 FileOpAwaiter_ awaiter;
187 uv_dirent_t dirent;
188 const std::string pathCopy{path};
189
190 uv_fs_scandir(loop.uvloop(), &awaiter.req(), pathCopy.c_str(), 0,
191 FileOpAwaiter_::uvCallback());
192
193 co_await awaiter;
194 while (UV_EOF != uv_fs_scandir_next(&awaiter.req(), &dirent)) {
195 co_yield DirEnt{dirent.name, dirent.type};
196 }
197}

◆ rmdir()

Promise< void > uvco::Directory::rmdir ( const Loop loop,
std::string_view  path 
)
static

Remove a directory. It must be empty.

134 {
135 FileOpAwaiter_ awaiter;
136
137 uv_fs_rmdir(loop.uvloop(), &awaiter.req(), path.data(),
138 FileOpAwaiter_::uvCallback());
139
140 co_await awaiter;
141
142 co_return;
143}

Member Data Documentation

◆ dir_

uv_dir_t* uvco::Directory::dir_
private

◆ loop_

uv_loop_t* uvco::Directory::loop_
private

The documentation for this class was generated from the following files: