Skip to content
This repository
Browse code

adding more fs functions; updated file_test.cpp

  • Loading branch information...
commit 3eda3bdc03913cc5142812b4400fd182aa4d2f76 1 parent ab03198
Daniel Kang authored

Showing 2 changed files with 155 additions and 0 deletions. Show diff stats Hide diff stats

  1. +3 0  file_test.cpp
  2. +152 0 native/fs.h
3  file_test.cpp
@@ -2,12 +2,15 @@
2 2 #include <native/native.h>
3 3 using namespace native;
4 4
  5 +// usage: (executable) src_file dest_file
  6 +
5 7 int main(int argc, char** argv) {
6 8 if(argc < 3) {
7 9 std::cout << "usage: " << argv[0] << " SRC_FILE DEST_FILE" << std::endl;
8 10 return 1;
9 11 }
10 12
  13 + // read contents from source and then write it to destination.
11 14 file::read(argv[1], [=](const std::string& str, error e){
12 15 if(e) {
13 16 std::cout << "file::read() failed: " << e.str() << std::endl;
152 native/fs.h
@@ -211,6 +211,158 @@ namespace native
211 211 }
212 212 return true;
213 213 }
  214 +
  215 + template<typename callback_t> // void(error e)
  216 + bool close(file_handle fd, callback_t callback)
  217 + {
  218 + auto req = internal::create_req(callback);
  219 + if(uv_fs_close(uv_default_loop(), req, fd, [](uv_fs_t* req){
  220 + assert(req->fs_type == UV_FS_CLOSE);
  221 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  222 + internal::delete_req(req);
  223 + })) {
  224 + internal::delete_req(req);
  225 + return false;
  226 + }
  227 + return true;
  228 + }
  229 +
  230 + template<typename callback_t> // void(error e)
  231 + bool unlink(const std::string& path, callback_t callback)
  232 + {
  233 + auto req = internal::create_req(callback);
  234 + if(uv_fs_unlink(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
  235 + assert(req->fs_type == UV_FS_UNLINK);
  236 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  237 + internal::delete_req(req);
  238 + })) {
  239 + internal::delete_req(req);
  240 + return false;
  241 + }
  242 + return true;
  243 + }
  244 +
  245 + template<typename callback_t> // void(error e)
  246 + bool mkdir(const std::string& path, int mode, callback_t callback)
  247 + {
  248 + auto req = internal::create_req(callback);
  249 + if(uv_fs_mkdir(uv_default_loop(), req, path.c_str(), mode, [](uv_fs_t* req){
  250 + assert(req->fs_type == UV_FS_MKDIR);
  251 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  252 + internal::delete_req(req);
  253 + })) {
  254 + internal::delete_req(req);
  255 + return false;
  256 + }
  257 + return true;
  258 + }
  259 +
  260 + template<typename callback_t> // void(error e)
  261 + bool rmdir(const std::string& path, callback_t callback)
  262 + {
  263 + auto req = internal::create_req(callback);
  264 + if(uv_fs_rmdir(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
  265 + assert(req->fs_type == UV_FS_RMDIR);
  266 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  267 + internal::delete_req(req);
  268 + })) {
  269 + internal::delete_req(req);
  270 + return false;
  271 + }
  272 + return true;
  273 + }
  274 +
  275 + template<typename callback_t> // void(error e)
  276 + bool rename(const std::string& path, const std::string& new_path, callback_t callback)
  277 + {
  278 + auto req = internal::create_req(callback);
  279 + if(uv_fs_rename(uv_default_loop(), req, path.c_str(), new_path.c_str(), [](uv_fs_t* req){
  280 + assert(req->fs_type == UV_FS_RENAME);
  281 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  282 + internal::delete_req(req);
  283 + })) {
  284 + internal::delete_req(req);
  285 + return false;
  286 + }
  287 + return true;
  288 + }
  289 +
  290 + template<typename callback_t> // void(error e)
  291 + bool chmod(const std::string& path, int mode, callback_t callback)
  292 + {
  293 + auto req = internal::create_req(callback);
  294 + if(uv_fs_chmod(uv_default_loop(), req, path.c_str(), mode, [](uv_fs_t* req){
  295 + assert(req->fs_type == UV_FS_CHMOD);
  296 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  297 + internal::delete_req(req);
  298 + })) {
  299 + internal::delete_req(req);
  300 + return false;
  301 + }
  302 + return true;
  303 + }
  304 +
  305 + template<typename callback_t> // void(error e)
  306 + bool chown(const std::string& path, int uid, int gid, callback_t callback)
  307 + {
  308 + auto req = internal::create_req(callback);
  309 + if(uv_fs_chown(uv_default_loop(), req, path.c_str(), uid, gid, [](uv_fs_t* req){
  310 + assert(req->fs_type == UV_FS_CHOWN);
  311 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  312 + internal::delete_req(req);
  313 + })) {
  314 + internal::delete_req(req);
  315 + return false;
  316 + }
  317 + return true;
  318 + }
  319 +
  320 +#if 0
  321 + template<typename callback_t>
  322 + bool readdir(const std::string& path, int flags, callback_t callback)
  323 + {
  324 + auto req = internal::create_req(callback);
  325 + if(uv_fs_readdir(uv_default_loop(), req, path.c_str(), flags, [](uv_fs_t* req){
  326 + assert(req->fs_type == UV_FS_READDIR);
  327 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  328 + internal::delete_req(req);
  329 + })) {
  330 + internal::delete_req(req);
  331 + return false;
  332 + }
  333 + return true;
  334 + }
  335 +
  336 + template<typename callback_t> // void(error e)
  337 + bool stat(const std::string& path, callback_t callback)
  338 + {
  339 + auto req = internal::create_req(callback);
  340 + if(uv_fs_stat(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
  341 + assert(req->fs_type == UV_FS_STAT);
  342 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  343 + internal::delete_req(req);
  344 + })) {
  345 + internal::delete_req(req);
  346 + return false;
  347 + }
  348 + return true;
  349 + }
  350 +
  351 + template<typename callback_t> // void(error e)
  352 + bool fstat(const std::string& path, callback_t callback)
  353 + {
  354 + auto req = internal::create_req(callback);
  355 + if(uv_fs_fstat(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
  356 + assert(req->fs_type == UV_FS_FSTAT);
  357 + internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
  358 + internal::delete_req(req);
  359 + })) {
  360 + internal::delete_req(req);
  361 + return false;
  362 + }
  363 + return true;
  364 + }
  365 +#endif
214 366 }
215 367
216 368 class file

0 comments on commit 3eda3bd

Please sign in to comment.
Something went wrong with that request. Please try again.