A file descriptor.
More...
#include <fs.h>
◆ File() [1/3]
| uvco::File::File |
( |
const File & | | ) |
|
|
delete |
◆ File() [2/3]
| uvco::File::File |
( |
File && | other | ) |
|
|
noexcept |
228 {
229 if (this == &other) {
230 return;
231 }
234 other.file_ = -1;
235}
uv_loop_t * loop_
Definition fs.h:113
uv_file file_
Definition fs.h:114
◆ ~File()
void close()
Close a file.
Definition fs.cc:313
◆ File() [3/3]
| uvco::File::File |
( |
uv_loop_t * | loop, |
|
|
uv_file | file ) |
|
inlineprivate |
uv_file file() const
Access the libuv file handle.
Definition fs.cc:308
◆ close()
| void uvco::File::close |
( |
| ) |
|
Close a file.
313 {
315 uv_fs_close(
loop_,
new uv_fs_t{},
file(), FileOpAwaiter_::uvCallback());
316 }
318}
◆ file()
| uv_file uvco::File::file |
( |
| ) |
const |
|
nodiscard |
Access the libuv file handle.
308 {
309 BOOST_ASSERT(
file_ >= 0);
311}
◆ open()
| Promise< File > uvco::File::open |
( |
const Loop & | loop, |
|
|
std::string_view | path, |
|
|
int | flags = 0, |
|
|
int | mode = 0644 ) |
|
static |
Open a file asynchronously; flags and mode are optional and analogous to open(2).
249 {
250 FileOpAwaiter_ awaiter;
251
252 const std::string path_str(path);
253 uv_fs_open(loop.uvloop(), &awaiter.req(), path_str.c_str(), flags, mode,
254 FileOpAwaiter_::uvCallback());
255
256 co_await awaiter;
257
258 const auto fileDesc = static_cast<uv_file>(awaiter.req().result);
259
260 co_return File{loop.uvloop(), fileDesc};
261}
File(const File &)=delete
◆ operator=() [1/2]
| File & uvco::File::operator= |
( |
const File & | | ) |
|
|
delete |
◆ operator=() [2/2]
| File & uvco::File::operator= |
( |
File && | other | ) |
|
|
noexcept |
237 {
238 if (this == &other) {
239 return *this;
240 }
244 other.file_ = -1;
245 return *this;
246}
◆ read()
| Promise< size_t > uvco::File::read |
( |
std::string & | buffer, |
|
|
int64_t | offset = -1 ) |
Read up to buffer.size() bytes into that buffer, starting at offset (if offset >= 0) or at the current file position.
The provided buffer must be pre-sized to the desired number of bytes to read. The number of bytes read will be at most buffer.length(). After the operation, buffer is resized to the actual number of bytes read.
- Parameters
-
| buffer | A pre-sized string to read data into. |
| offset | The offset in the file to read from. Defaults to -1 (current file position). |
- Returns
- The number of bytes read.
TODO: generalize to any buffer type.
274 {
275 FileOpAwaiter_ awaiter;
276 size_t result = 0;
277
278 std::array<uv_buf_t, 1> bufs{};
279 bufs[0] = uv_buf_init(buffer.data(), buffer.length());
280
281 uv_fs_read(
loop_, &awaiter.req(),
file(), bufs.data(), 1, offset,
282 FileOpAwaiter_::uvCallback());
283
284 co_await awaiter;
285
286 result = awaiter.req().result;
287 buffer.resize(result);
288 co_return result;
289}
◆ unlink()
| Promise< void > uvco::File::unlink |
( |
const Loop & | loop, |
|
|
std::string_view | path ) |
|
static |
263 {
264 FileOpAwaiter_ awaiter;
265
266 const std::string path_str(path);
267 uv_fs_unlink(loop.uvloop(), &awaiter.req(), path_str.c_str(),
268 FileOpAwaiter_::uvCallback());
269
270 co_await awaiter;
271 co_return;
272}
◆ write()
| Promise< size_t > uvco::File::write |
( |
std::string_view | buffer, |
|
|
int64_t | offset = -1 ) |
Write contents of buffer to the underlying file at offset.
TODO: generalize to any buffer type.
291 {
292 FileOpAwaiter_ awaiter;
293 size_t result = 0;
294
295 std::array<uv_buf_t, 1> bufs{};
296
297 bufs[0] = uv_buf_init(const_cast<char *>(buffer.data()), buffer.length());
298
299 uv_fs_write(
loop_, &awaiter.req(),
file(), bufs.data(), 1, offset,
300 FileOpAwaiter_::uvCallback());
301
302 co_await awaiter;
303
304 result = awaiter.req().result;
305 co_return result;
306}
◆ file_
| uv_file uvco::File::file_ |
|
private |
◆ loop_
| uv_loop_t* uvco::File::loop_ |
|
private |
The documentation for this class was generated from the following files: