From 9f932f92cfed14f5dc1bfa7371bab64980f102be Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 1 Sep 2011 15:36:01 -0700 Subject: [PATCH] add test fs_chmod, implement uv_fs_fchmod and uv_fs_chmod on unix --- src/unix/fs.c | 48 ++++++++++++++++++++-- test/test-fs.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++ test/test-list.h | 2 + 3 files changed, 148 insertions(+), 4 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index 264226bc..b3ed0b33 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -570,8 +570,28 @@ int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd, int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path, int mode, uv_fs_cb cb) { - assert(0 && "implement me"); - return -1; + uv_fs_req_init(loop, req, UV_FS_CHMOD, cb); + + if (cb) { + /* async */ + uv_ref(loop); + req->eio = eio_chmod(path, mode, EIO_PRI_DEFAULT, uv__fs_after, req); + if (!req->eio) { + uv_err_new(loop, ENOMEM); + return -1; + } + + } else { + /* sync */ + req->result = chmod(path, mode); + + if (req->result) { + uv_err_new(loop, errno); + return -1; + } + } + + return 0; } @@ -658,8 +678,28 @@ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file, int mode, uv_fs_cb cb) { - assert(0 && "implement me"); - return -1; + uv_fs_req_init(loop, req, UV_FS_FCHMOD, cb); + + if (cb) { + /* async */ + uv_ref(loop); + req->eio = eio_fchmod(file, mode, EIO_PRI_DEFAULT, uv__fs_after, req); + if (!req->eio) { + uv_err_new(loop, ENOMEM); + return -1; + } + + } else { + /* sync */ + req->result = fchmod(file, mode); + + if (req->result) { + uv_err_new(loop, errno); + return -1; + } + } + + return 0; } diff --git a/test/test-fs.c b/test/test-fs.c index d8b85efc..1d172e5e 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -58,6 +58,8 @@ static int fdatasync_cb_count; static int ftruncate_cb_count; static int sendfile_cb_count; static int fstat_cb_count; +static int chmod_cb_count; +static int fchmod_cb_count; static uv_loop_t* loop; @@ -81,6 +83,40 @@ static char buf[32]; static char test_buf[] = "test-buffer\n"; +void check_permission(const char* filename, int mode) { + int r; + uv_fs_t req; + struct stat* s; + + r = uv_fs_stat(uv_default_loop(), &req, filename, NULL); + ASSERT(r == 0); + ASSERT(req.result == 0); + + s = req.ptr; + ASSERT((s->st_mode & 0777) == mode); + + uv_fs_req_cleanup(&req); +} + + +static void fchmod_cb(uv_fs_t* req) { + ASSERT(req->fs_type == UV_FS_FCHMOD); + ASSERT(req->result == 0); + fchmod_cb_count++; + uv_fs_req_cleanup(req); + check_permission("test_file", 0600); +} + + +static void chmod_cb(uv_fs_t* req) { + ASSERT(req->fs_type == UV_FS_CHMOD); + ASSERT(req->result == 0); + chmod_cb_count++; + uv_fs_req_cleanup(req); + check_permission("test_file", 0200); +} + + static void unlink_cb(uv_fs_t* req) { ASSERT(req == &unlink_req); ASSERT(req->fs_type == UV_FS_UNLINK); @@ -611,3 +647,69 @@ TEST_IMPL(fs_fstat) { return 0; } + + +TEST_IMPL(fs_chmod) { + int r; + uv_fs_t req; + uv_file file; + + /* Setup. */ + unlink("test_file"); + + uv_init(); + + loop = uv_default_loop(); + + r = uv_fs_open(loop, &req, "test_file", O_RDWR | O_CREAT, 0, NULL); + ASSERT(r == 0); + ASSERT(req.result != -1); + file = req.result; + uv_fs_req_cleanup(&req); + + r = uv_fs_write(loop, &req, file, test_buf, sizeof(test_buf), -1, NULL); + ASSERT(r == 0); + ASSERT(req.result == sizeof(test_buf)); + uv_fs_req_cleanup(&req); + + /* Make the file write-only */ + r = uv_fs_chmod(loop, &req, "test_file", 0200, NULL); + ASSERT(r == 0); + ASSERT(req.result == 0); + uv_fs_req_cleanup(&req); + + check_permission("test_file", 0200); + + /* Make the file read+write with sync uv_fs_fchmod */ + r = uv_fs_fchmod(loop, &req, file, 0600, NULL); + ASSERT(r == 0); + ASSERT(req.result == 0); + uv_fs_req_cleanup(&req); + + check_permission("test_file", 0600); + + /* async chmod */ + r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb); + ASSERT(r == 0); + uv_run(loop); + ASSERT(chmod_cb_count == 1); + + /* async fchmod */ + r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb); + ASSERT(r == 0); + uv_run(loop); + ASSERT(fchmod_cb_count == 1); + + close(file); + + /* + * Run the loop just to check we don't have make any extranious uv_ref() + * calls. This should drop out immediately. + */ + uv_run(loop); + + /* Cleanup. */ + unlink("test_file"); + + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index 22bc12e8..032060d8 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -77,6 +77,7 @@ TEST_DECLARE (fs_file_sync) TEST_DECLARE (fs_async_dir) TEST_DECLARE (fs_async_sendfile) TEST_DECLARE (fs_fstat) +TEST_DECLARE (fs_chmod) TEST_DECLARE (threadpool_queue_work_simple) #ifdef _WIN32 TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows) @@ -181,6 +182,7 @@ TASK_LIST_START TEST_ENTRY (fs_async_dir) TEST_ENTRY (fs_async_sendfile) TEST_ENTRY (fs_fstat) + TEST_ENTRY (fs_chmod) TEST_ENTRY (threadpool_queue_work_simple)