Skip to content
Browse files

adding more fs functions; updated file_test.cpp

  • Loading branch information...
1 parent ab03198 commit 3eda3bdc03913cc5142812b4400fd182aa4d2f76 @d5 committed Feb 9, 2012
Showing with 155 additions and 0 deletions.
  1. +3 −0 file_test.cpp
  2. +152 −0 native/fs.h
View
3 file_test.cpp
@@ -2,12 +2,15 @@
#include <native/native.h>
using namespace native;
+// usage: (executable) src_file dest_file
+
int main(int argc, char** argv) {
if(argc < 3) {
std::cout << "usage: " << argv[0] << " SRC_FILE DEST_FILE" << std::endl;
return 1;
}
+ // read contents from source and then write it to destination.
file::read(argv[1], [=](const std::string& str, error e){
if(e) {
std::cout << "file::read() failed: " << e.str() << std::endl;
View
152 native/fs.h
@@ -211,6 +211,158 @@ namespace native
}
return true;
}
+
+ template<typename callback_t> // void(error e)
+ bool close(file_handle fd, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_close(uv_default_loop(), req, fd, [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_CLOSE);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool unlink(const std::string& path, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_unlink(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_UNLINK);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool mkdir(const std::string& path, int mode, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_mkdir(uv_default_loop(), req, path.c_str(), mode, [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_MKDIR);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool rmdir(const std::string& path, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_rmdir(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_RMDIR);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool rename(const std::string& path, const std::string& new_path, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_rename(uv_default_loop(), req, path.c_str(), new_path.c_str(), [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_RENAME);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool chmod(const std::string& path, int mode, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_chmod(uv_default_loop(), req, path.c_str(), mode, [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_CHMOD);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool chown(const std::string& path, int uid, int gid, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_chown(uv_default_loop(), req, path.c_str(), uid, gid, [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_CHOWN);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+#if 0
+ template<typename callback_t>
+ bool readdir(const std::string& path, int flags, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_readdir(uv_default_loop(), req, path.c_str(), flags, [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_READDIR);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool stat(const std::string& path, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_stat(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_STAT);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+
+ template<typename callback_t> // void(error e)
+ bool fstat(const std::string& path, callback_t callback)
+ {
+ auto req = internal::create_req(callback);
+ if(uv_fs_fstat(uv_default_loop(), req, path.c_str(), [](uv_fs_t* req){
+ assert(req->fs_type == UV_FS_FSTAT);
+ internal::invoke_from_req<callback_t>(req, req->errorno?error(req->errorno):error());
+ internal::delete_req(req);
+ })) {
+ internal::delete_req(req);
+ return false;
+ }
+ return true;
+ }
+#endif
}
class file

0 comments on commit 3eda3bd

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