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 |
231 {
232 if (this == &other) {
233 return;
234 }
237 other.file_ = -1;
238}
uv_loop_t * loop_
Definition fs.h:113
uv_file file_
Definition fs.h:114
◆ ~File()
void close()
Close a file.
Definition fs.cc:316
◆ 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:311
◆ close()
| void uvco::File::close |
( |
| ) |
|
Close a file.
316 {
318 uv_fs_close(
loop_,
new uv_fs_t{},
file(), FileOpAwaiter_::uvCallback());
319 }
321}
◆ file()
| uv_file uvco::File::file |
( |
| ) |
const |
|
nodiscard |
Access the libuv file handle.
311 {
312 BOOST_ASSERT(
file_ >= 0);
314}
◆ 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).
252 {
253 FileOpAwaiter_ awaiter;
254
255 const std::string path_str(path);
256 uv_fs_open(loop.uvloop(), &awaiter.req(), path_str.c_str(), flags, mode,
257 FileOpAwaiter_::uvCallback());
258
259 co_await awaiter;
260
261 const auto fileDesc = static_cast<uv_file>(awaiter.req().result);
262
263 co_return File{loop.uvloop(), fileDesc};
264}
File(const File &)=delete
◆ operator=() [1/2]
| File & uvco::File::operator= |
( |
const File & | | ) |
|
|
delete |
◆ operator=() [2/2]
| File & uvco::File::operator= |
( |
File && | other | ) |
|
|
noexcept |
240 {
241 if (this == &other) {
242 return *this;
243 }
247 other.file_ = -1;
248 return *this;
249}
◆ 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.
277 {
278 FileOpAwaiter_ awaiter;
279 size_t result = 0;
280
281 std::array<uv_buf_t, 1> bufs{};
282 bufs[0] = uv_buf_init(buffer.data(), buffer.length());
283
284 uv_fs_read(
loop_, &awaiter.req(),
file(), bufs.data(), 1, offset,
285 FileOpAwaiter_::uvCallback());
286
287 co_await awaiter;
288
289 result = awaiter.req().result;
290 buffer.resize(result);
291 co_return result;
292}
◆ unlink()
| Promise< void > uvco::File::unlink |
( |
const Loop & | loop, |
|
|
std::string_view | path ) |
|
static |
266 {
267 FileOpAwaiter_ awaiter;
268
269 const std::string path_str(path);
270 uv_fs_unlink(loop.uvloop(), &awaiter.req(), path_str.c_str(),
271 FileOpAwaiter_::uvCallback());
272
273 co_await awaiter;
274 co_return;
275}
◆ 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.
294 {
295 FileOpAwaiter_ awaiter;
296 size_t result = 0;
297
298 std::array<uv_buf_t, 1> bufs{};
299
300 bufs[0] = uv_buf_init(const_cast<char *>(buffer.data()), buffer.length());
301
302 uv_fs_write(
loop_, &awaiter.req(),
file(), bufs.data(), 1, offset,
303 FileOpAwaiter_::uvCallback());
304
305 co_await awaiter;
306
307 result = awaiter.req().result;
308 co_return result;
309}
◆ 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: