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

Commit

Permalink
Browse files Browse the repository at this point in the history
Add uv_fs_t.path on unix and tests
Windows implementation missing #177
  • Loading branch information
ry committed Sep 5, 2011
1 parent b6ede6c commit a18860a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions include/uv.h
Expand Up @@ -890,6 +890,7 @@ struct uv_fs_s {
uv_fs_cb cb;
ssize_t result;
void* ptr;
char* path;
int errorno;
UV_FS_PRIVATE_FIELDS
};
Expand Down
31 changes: 21 additions & 10 deletions src/unix/fs.c
Expand Up @@ -40,7 +40,7 @@
#define ARGS4(a,b,c,d) (a), (b), (c), (d)

#define WRAP_EIO(type, eiofunc, func, args) \
uv_fs_req_init(loop, req, type, cb); \
uv_fs_req_init(loop, req, type, path, cb); \
if (cb) { \
/* async */ \
uv_ref(loop); \
Expand All @@ -61,7 +61,7 @@


static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type,
uv_fs_cb cb) {
char* path, uv_fs_cb cb) {
/* Make sure the thread pool is initialized. */
uv_eio_init(loop);

Expand All @@ -72,12 +72,16 @@ static void uv_fs_req_init(uv_loop_t* loop, uv_fs_t* req, uv_fs_type fs_type,
req->cb = cb;
req->result = 0;
req->ptr = NULL;
req->path = path ? strdup(path) : NULL;
req->errorno = 0;
req->eio = NULL;
}


void uv_fs_req_cleanup(uv_fs_t* req) {
free(req->path);
req->path = NULL;

switch (req->fs_type) {
case UV_FS_READDIR:
assert(req->ptr);
Expand Down Expand Up @@ -168,13 +172,14 @@ static int uv__fs_after(eio_req* eio) {


int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_CLOSE, eio_close, close, ARGS1(file));
}


int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
int mode, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_OPEN, cb);
uv_fs_req_init(loop, req, UV_FS_OPEN, path, cb);

if (cb) {
/* async */
Expand Down Expand Up @@ -202,7 +207,7 @@ int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,

int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
size_t length, off_t offset, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_READ, cb);
uv_fs_req_init(loop, req, UV_FS_READ, NULL, cb);

if (cb) {
/* async */
Expand Down Expand Up @@ -238,7 +243,7 @@ int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {

int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file, void* buf,
size_t length, off_t offset, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_WRITE, cb);
uv_fs_req_init(loop, req, UV_FS_WRITE, NULL, cb);

if (cb) {
/* async */
Expand Down Expand Up @@ -284,7 +289,7 @@ int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags,
size_t size = 0;
size_t d_namlen = 0;

uv_fs_req_init(loop, req, UV_FS_READDIR, cb);
uv_fs_req_init(loop, req, UV_FS_READDIR, path, cb);

if (cb) {
/* async */
Expand Down Expand Up @@ -340,7 +345,7 @@ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
char* pathdup = path;
int pathlen;

uv_fs_req_init(loop, req, UV_FS_STAT, cb);
uv_fs_req_init(loop, req, UV_FS_STAT, path, cb);

/* TODO do this without duplicating the string. */
/* TODO security */
Expand Down Expand Up @@ -383,7 +388,7 @@ int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {


int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
uv_fs_req_init(loop, req, UV_FS_FSTAT, cb);
uv_fs_req_init(loop, req, UV_FS_FSTAT, NULL, cb);

if (cb) {
/* async */
Expand Down Expand Up @@ -418,23 +423,27 @@ int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path, const char* ne


int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FSYNC, eio_fsync, fsync, ARGS1(file))
}


int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FDATASYNC, eio_fdatasync, fdatasync, ARGS1(file))
}


int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file, off_t offset,
uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FTRUNCATE, eio_ftruncate, ftruncate, ARGS2(file, offset))
}


int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd, uv_file in_fd,
off_t in_offset, size_t length, uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_SENDFILE, eio_sendfile, eio_sendfile_sync,
ARGS4(out_fd, in_fd, in_offset, length))
}
Expand Down Expand Up @@ -471,7 +480,7 @@ int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) {
char* pathdup = path;
int pathlen;

uv_fs_req_init(loop, req, UV_FS_LSTAT, cb);
uv_fs_req_init(loop, req, UV_FS_LSTAT, path, cb);

/* TODO do this without duplicating the string. */
/* TODO security */
Expand Down Expand Up @@ -533,7 +542,7 @@ int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,

status = -1;

uv_fs_req_init(loop, req, UV_FS_READLINK, cb);
uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb);

if (cb) {
if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req))) {
Expand Down Expand Up @@ -581,6 +590,7 @@ 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) {
char* path = NULL;
WRAP_EIO(UV_FS_FCHMOD, eio_fchmod, fchmod, ARGS2(file, mode))
}

Expand All @@ -593,6 +603,7 @@ int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path, int uid,

int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file, int uid, int gid,
uv_fs_cb cb) {
char* path = NULL;
WRAP_EIO(UV_FS_FCHOWN, eio_fchown, fchown, ARGS3(file, uid, gid))
}

Expand Down
9 changes: 9 additions & 0 deletions test/test-fs.c
Expand Up @@ -240,6 +240,8 @@ static void open_cb(uv_fs_t* req) {
ASSERT(0);
}
open_cb_count++;
ASSERT(req->path);
ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
uv_fs_req_cleanup(req);
memset(buf, 0, sizeof(buf));
r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1,
Expand Down Expand Up @@ -306,6 +308,8 @@ static void mkdir_cb(uv_fs_t* req) {
ASSERT(req->fs_type == UV_FS_MKDIR);
ASSERT(req->result != -1);
mkdir_cb_count++;
ASSERT(req->path);
ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
uv_fs_req_cleanup(req);
}

Expand All @@ -315,6 +319,8 @@ static void rmdir_cb(uv_fs_t* req) {
ASSERT(req->fs_type == UV_FS_RMDIR);
ASSERT(req->result != -1);
rmdir_cb_count++;
ASSERT(req->path);
ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
uv_fs_req_cleanup(req);
}

Expand All @@ -327,6 +333,8 @@ static void readdir_cb(uv_fs_t* req) {
ASSERT(memcmp(req->ptr, "file1\0file2\0", 12) == 0
|| memcmp(req->ptr, "file2\0file1\0", 12) == 0);
readdir_cb_count++;
ASSERT(req->path);
ASSERT(memcmp(req->path, "test_dir\0", 9) == 0);
uv_fs_req_cleanup(req);
ASSERT(!req->ptr);
}
Expand Down Expand Up @@ -544,6 +552,7 @@ TEST_IMPL(fs_async_dir) {
r = uv_fs_readdir(loop, &readdir_req, "test_dir", 0, NULL);
readdir_cb(&readdir_req);


r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
ASSERT(r == 0);
uv_run(loop);
Expand Down

0 comments on commit a18860a

Please sign in to comment.