From a6ed1757abd47d0e56039273affee85795bfae41 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 30 Aug 2011 01:32:53 -0700 Subject: [PATCH] unix: implement uv_fs_lstat --- src/unix/fs.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- test/test-fs.c | 13 +++++++++++-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index e053cb53..b6fb16f7 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -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; @@ -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; } @@ -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; } diff --git a/test/test-fs.c b/test/test-fs.c index 58fdcacf..7ad35ea8 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -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++; @@ -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);