Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
add test fs_chmod, implement uv_fs_fchmod and uv_fs_chmod on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 1, 2011
1 parent 2e60358 commit 9f932f9
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/unix/fs.c
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
}


Expand Down
102 changes: 102 additions & 0 deletions test/test-fs.c
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions test/test-list.h
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 9f932f9

Please sign in to comment.