Support for fstat(2) through fs.fstat() and fs.fstatSync(). Added test cases, updated...
authorBen Noordhuis <bnoordhuis@bender.(none)>
Wed, 12 May 2010 20:35:33 +0000 (22:35 +0200)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 12 May 2010 21:20:43 +0000 (14:20 -0700)
doc/api.markdown
lib/fs.js
src/node_file.cc
test/simple/test-fs-stat.js

index 72d2994..17e9e4d 100644 (file)
@@ -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
@@ -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)
 
index dbfec12..033b8b7 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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);
 };
@@ -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);
 };
index f9fd27c..a576b5c 100644 (file)
@@ -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;
@@ -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;
 
@@ -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);
index 5f5333d..eb19251 100644 (file)
@@ -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) {
@@ -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);
 });