Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Expose fs.lstat
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs authored and ry committed Feb 22, 2010
1 parent d98ea70 commit 9acc8a6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
8 changes: 5 additions & 3 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ Synchronous rename(2).


+fs.stat(path, callback)+ ::
Asynchronous stat(2). The callback gets two arguments +(err, stats)+ where
+fs.lstat(path, callback)+ ::
Asynchronous stat(2) or lstat(2). The callback gets two arguments +(err, stats)+ where
+stats+ is a +fs.Stats+ object. It looks like this:
+
---------------------------------
Expand All @@ -601,7 +602,8 @@ Asynchronous stat(2). The callback gets two arguments +(err, stats)+ where
See the +fs.Stats+ section below for more information.

+fs.statSync(path)+ ::
Synchronous stat(2). Returns an instance of +fs.Stats+.
+fs.lstatSync(path)+ ::
Synchronous stat(2) or lstat(2). Returns an instance of +fs.Stats+.


+fs.unlink(path, callback)+ ::
Expand Down Expand Up @@ -713,7 +715,7 @@ The synchronous version of +fs.writeFile+.

=== +fs.Stats+

Objects returned from +fs.stat()+ are of this type.
Objects returned from +fs.stat()+ and +fs.lstat()+ are of this type.

+stats.isFile()+::

Expand Down
8 changes: 8 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,18 @@ var fsModule = createInternalModule("fs", function (exports) {
return process.fs.readdir(path);
};

exports.lstat = function (path, callback) {
process.fs.lstat(path, callback || noop);
};

exports.stat = function (path, callback) {
process.fs.stat(path, callback || noop);
};

exports.lstatSync = function (path) {
return process.fs.lstat(path);
};

exports.statSync = function (path) {
return process.fs.stat(path);
};
Expand Down
21 changes: 21 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static int After(eio_req *req) {
break;

case EIO_STAT:
case EIO_LSTAT:
{
struct stat *s = reinterpret_cast<struct stat*>(req->ptr2);
argc = 2;
Expand Down Expand Up @@ -188,6 +189,25 @@ static Handle<Value> Stat(const Arguments& args) {
}
}

static Handle<Value> LStat(const Arguments& args) {
HandleScope scope;

if (args.Length() < 1 || !args[0]->IsString()) {
return THROW_BAD_ARGS;
}

String::Utf8Value path(args[0]->ToString());

if (args[1]->IsFunction()) {
ASYNC_CALL(lstat, args[1], *path)
} else {
struct stat s;
int ret = lstat(*path, &s);
if (ret != 0) return ThrowException(errno_exception(errno));
return scope.Close(BuildStatsObject(&s));
}
}

static Handle<Value> Rename(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -488,6 +508,7 @@ void File::Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "sendfile", SendFile);
NODE_SET_METHOD(target, "readdir", ReadDir);
NODE_SET_METHOD(target, "stat", Stat);
NODE_SET_METHOD(target, "lstat", LStat);
NODE_SET_METHOD(target, "unlink", Unlink);
NODE_SET_METHOD(target, "write", Write);

Expand Down
12 changes: 11 additions & 1 deletion test/mjsunit/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ fs.stat(".", function (err, stats) {
}
});

fs.lstat(".", function (err, stats) {
if (err) {
got_error = true;
} else {
p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
});

puts("stating: " + __filename);
fs.stat(__filename, function (err, s) {
if (err) {
Expand Down Expand Up @@ -47,7 +57,7 @@ fs.stat(__filename, function (err, s) {
});

process.addListener("exit", function () {
assert.equal(2, success_count);
assert.equal(3, success_count);
assert.equal(false, got_error);
});

0 comments on commit 9acc8a6

Please sign in to comment.