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

Commit

Permalink
unix: implement uv_fs_lstat
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Aug 30, 2011
1 parent dbc1cb0 commit a6ed175
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
50 changes: 46 additions & 4 deletions src/unix/fs.c
Expand Up @@ -57,6 +57,7 @@ void uv_fs_req_cleanup(uv_fs_t* req) {
break;

case UV_FS_STAT:
case UV_FS_LSTAT:
req->ptr = NULL;
break;

Expand Down Expand Up @@ -98,7 +99,7 @@ static int uv__fs_after(eio_req* eio) {
}
req->ptr = malloc(buflen);
memcpy(req->ptr, req->eio->ptr2, buflen);
} else if (req->fs_type == UV_FS_STAT) {
} else if (req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT) {
req->ptr = req->eio->ptr2;
}

Expand Down Expand Up @@ -548,12 +549,53 @@ int uv_fs_futime(uv_fs_t* req, uv_file file, double atime, double mtime, uv_fs_c


int uv_fs_lstat(uv_fs_t* req, const char* path, uv_fs_cb cb) {
assert(0 && "implement me");
return -1;
char* pathdup = path;
int pathlen;

uv_fs_req_init(req, UV_FS_LSTAT, cb);

/* TODO do this without duplicating the string. */
/* TODO security */
pathdup = strdup(path);
pathlen = strlen(path);

if (pathlen > 0 && path[pathlen - 1] == '\\') {
/* TODO do not modify input string */
pathdup[pathlen - 1] = '\0';
}

if (cb) {
/* async */
uv_ref();
req->eio = eio_lstat(pathdup, EIO_PRI_DEFAULT, uv__fs_after, req);

free(pathdup);

if (!req->eio) {
uv_err_new(NULL, ENOMEM);
return -1;
}

} else {
/* sync */
req->result = lstat(pathdup, &req->statbuf);

free(pathdup);

if (req->result < 0) {
uv_err_new(NULL, errno);
return -1;
}

req->ptr = &req->statbuf;
}

return 0;
}


int uv_fs_link(uv_fs_t* req, const char* path, const char* new_path, uv_fs_cb cb) {
int uv_fs_link(uv_fs_t* req, const char* path, const char* new_path,
uv_fs_cb cb) {
assert(0 && "implement me");
return -1;
}
Expand Down
13 changes: 11 additions & 2 deletions test/test-fs.c
Expand Up @@ -231,7 +231,7 @@ static void readdir_cb(uv_fs_t* req) {

static void stat_cb(uv_fs_t* req) {
ASSERT(req == &stat_req);
ASSERT(req->fs_type == UV_FS_STAT);
ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
ASSERT(req->result != -1);
ASSERT(req->ptr);
stat_cb_count++;
Expand Down Expand Up @@ -435,11 +435,20 @@ TEST_IMPL(fs_async_dir) {
r = uv_fs_stat(&stat_req, "test_dir", stat_cb);
ASSERT(r == 0);
uv_run();

r = uv_fs_stat(&stat_req, "test_dir\\", stat_cb);
ASSERT(r == 0);
uv_run();

ASSERT(stat_cb_count == 2);
r = uv_fs_lstat(&stat_req, "test_dir", stat_cb);
ASSERT(r == 0);
uv_run();

r = uv_fs_lstat(&stat_req, "test_dir\\", stat_cb);
ASSERT(r == 0);
uv_run();

ASSERT(stat_cb_count == 4);

r = uv_fs_unlink(&unlink_req, "test_dir/file1", unlink_cb);
ASSERT(r == 0);
Expand Down

0 comments on commit a6ed175

Please sign in to comment.