Skip to content

Commit

Permalink
Support for fstat(2) through fs.fstat() and fs.fstatSync(). Added tes…
Browse files Browse the repository at this point in the history
…t cases, updated documentation.
  • Loading branch information
Ben Noordhuis authored and ry committed May 12, 2010
1 parent 67eeedd commit c6c77d5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
8 changes: 4 additions & 4 deletions doc/api.markdown
Expand Up @@ -1269,9 +1269,9 @@ Asynchronous chmod(2). No arguments other than a possible exception are given to

Synchronous chmod(2).

### fs.stat(path, callback), fs.lstat(path, callback)
### fs.stat(path, callback), fs.lstat(path, callback), fs.fstat(fd, 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:
Asynchronous stat(2), lstat(2) or fstat(2). The callback gets two arguments `(err, stats)` where `stats` is a `fs.Stats` object. It looks like this:

{ dev: 2049
, ino: 305352
Expand All @@ -1290,9 +1290,9 @@ Asynchronous stat(2) or lstat(2). The callback gets two arguments `(err, stats)`

See the `fs.Stats` section below for more information.

### fs.statSync(path), fs.lstatSync(path)
### fs.statSync(path), fs.lstatSync(path), fs.fstatSync(fd)

Synchronous stat(2) or lstat(2). Returns an instance of `fs.Stats`.
Synchronous stat(2), lstat(2) or fstat(2). Returns an instance of `fs.Stats`.

### fs.link(srcpath, dstpath, callback)

Expand Down
8 changes: 8 additions & 0 deletions lib/fs.js
Expand Up @@ -207,6 +207,10 @@ fs.readdirSync = function (path) {
return binding.readdir(path);
};

fs.fstat = function (fd, callback) {
binding.fstat(fd, callback || noop);
};

fs.lstat = function (path, callback) {
binding.lstat(path, callback || noop);
};
Expand All @@ -215,6 +219,10 @@ fs.stat = function (path, callback) {
binding.stat(path, callback || noop);
};

fs.fstatSync = function (fd) {
return binding.fstat(fd);
};

fs.lstatSync = function (path) {
return binding.lstat(path);
};
Expand Down
21 changes: 21 additions & 0 deletions src/node_file.cc
Expand Up @@ -73,6 +73,7 @@ static int After(eio_req *req) {

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

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

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

int fd = args[0]->Int32Value();

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

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

Expand Down Expand Up @@ -744,6 +764,7 @@ void File::Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "readdir", ReadDir);
NODE_SET_METHOD(target, "stat", Stat);
NODE_SET_METHOD(target, "lstat", LStat);
NODE_SET_METHOD(target, "fstat", FStat);
NODE_SET_METHOD(target, "link", Link);
NODE_SET_METHOD(target, "symlink", Symlink);
NODE_SET_METHOD(target, "readlink", ReadLink);
Expand Down
35 changes: 34 additions & 1 deletion test/simple/test-fs-stat.js
Expand Up @@ -23,6 +23,39 @@ fs.lstat(".", function (err, stats) {
}
});

// fstat
fs.open(".", "r", undefined, function(err, fd) {
assert.ok(!err);
assert.ok(fd);

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

// fstatSync
fs.open(".", "r", undefined, function(err, fd) {
var stats;
try {
stats = fs.fstatSync(fd);
} catch (err) {
got_error = true;
}
if (stats) {
p(stats);
assert.ok(stats.mtime instanceof Date);
success_count++;
}
fs.close(fd);
});

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

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

0 comments on commit c6c77d5

Please sign in to comment.