|
| 1 | +#ifndef __FS_H__ |
| 2 | +#define __FS_H__ |
| 3 | + |
| 4 | +#include "base.h" |
| 5 | +#include "callback.h" |
| 6 | + |
| 7 | +namespace native |
| 8 | +{ |
| 9 | + typedef uv_file file; |
| 10 | + |
| 11 | + // TODO: implement functions that accept loop pointer as extra argument. |
| 12 | + namespace fs |
| 13 | + { |
| 14 | + template<typename callback_t> |
| 15 | + bool open(const char* path, int flags, int mode, callback_t callback) |
| 16 | + { |
| 17 | + auto req = new uv_fs_t; |
| 18 | + req->data = new callbacks(1); |
| 19 | + callbacks::store(req->data, 0, callback); |
| 20 | + return uv_fs_open(uv_default_loop(), req, path, flags, mode, [](uv_fs_t* req){ |
| 21 | + assert(req->fs_type == UV_FS_OPEN); |
| 22 | + |
| 23 | + if(req->errorno || req->result < 0) |
| 24 | + { |
| 25 | + // TODO: handle error |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + callbacks::invoke<callback_t>(req->data, 0, req->result); |
| 30 | + } |
| 31 | + |
| 32 | + delete reinterpret_cast<callbacks*>(req->data); |
| 33 | + uv_fs_req_cleanup(req); |
| 34 | + delete req; |
| 35 | + })==0; |
| 36 | + } |
| 37 | + |
| 38 | + template<typename callback_t> |
| 39 | + bool read(file f, void* buf, size_t len, off_t offset, callback_t callback) |
| 40 | + { |
| 41 | + auto req = new uv_fs_t; |
| 42 | + req->data = new callbacks(1); |
| 43 | + callbacks::store(req->data, 0, callback); |
| 44 | + return uv_fs_read(uv_default_loop(), req, f, buf, len, offset, [](uv_fs_t* req){ |
| 45 | + assert(req->fs_type == UV_FS_READ); |
| 46 | + |
| 47 | + if(req->errorno) |
| 48 | + { |
| 49 | + // TODO: handle error |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + callbacks::invoke<callback_t>(req->data, 0, req); |
| 54 | + } |
| 55 | + |
| 56 | + delete reinterpret_cast<callbacks*>(req->data); |
| 57 | + uv_fs_req_cleanup(req); |
| 58 | + delete req; |
| 59 | + })==0; |
| 60 | + } |
| 61 | + |
| 62 | + template<typename callback_t> |
| 63 | + bool mkdir(const char* path, int mode, callback_t callback) |
| 64 | + { |
| 65 | + auto req = new uv_fs_t; |
| 66 | + req->data = new callbacks(1); |
| 67 | + callbacks::store(req->data, 0, callback); |
| 68 | + return uv_fs_mkdir(uv_default_loop(), req, path, mode, [](uv_fs_t* req){ |
| 69 | + assert(req->fs_type == UV_FS_MKDIR); |
| 70 | + |
| 71 | + if(req->errorno) |
| 72 | + { |
| 73 | + // TODO: handle error |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + callbacks::invoke<callback_t>(req->data, 0, req->result); |
| 78 | + } |
| 79 | + |
| 80 | + delete reinterpret_cast<callbacks*>(req->data); |
| 81 | + uv_fs_req_cleanup(req); |
| 82 | + delete req; |
| 83 | + })==0; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#endif |
0 commit comments