Browse Source

add File.stat File.exists File.strerror

v0.7.4-release
Ryan 16 years ago
parent
commit
e7dd20dc6e
  1. 19
      src/file.cc
  2. 10
      src/file.js
  3. 21
      test/cat.js
  4. 14
      test/stats.js

19
src/file.cc

@ -76,6 +76,7 @@ public:
static Handle<Value> Read (const Arguments& args);
static int AfterRead (eio_req *req);
private:
bool HasUtf8Encoding (void);
Persistent<Object> handle_;
@ -90,6 +91,8 @@ public:
static Handle<Value> Stat (const Arguments& args);
static int AfterStat (eio_req *req);
static Handle<Value> StrError (const Arguments& args);
};
Handle<Value>
@ -184,6 +187,21 @@ FileSystem::AfterStat (eio_req *req)
return 0;
}
Handle<Value>
FileSystem::StrError (const Arguments& args)
{
if (args.Length() < 1) return v8::Undefined();
if (!args[0]->IsNumber()) return v8::Undefined();
HandleScope scope;
int errorno = args[0]->IntegerValue();
Local<String> message = String::New(strerror(errorno));
return scope.Close(message);
}
///////////////////// FILE /////////////////////
File::File (Handle<Object> handle)
@ -483,6 +501,7 @@ NodeInit_file (Handle<Object> target)
// file system methods
JS_SET_METHOD(fs, "_ffi_rename", FileSystem::Rename);
JS_SET_METHOD(fs, "_ffi_stat", FileSystem::Stat);
JS_SET_METHOD(fs, "strerror", FileSystem::StrError);
fs->Set(String::NewSymbol("STDIN_FILENO"), Integer::New(STDIN_FILENO));
fs->Set(String::NewSymbol("STDOUT_FILENO"), Integer::New(STDOUT_FILENO));
fs->Set(String::NewSymbol("STDERR_FILENO"), Integer::New(STDERR_FILENO));

10
src/file.js

@ -2,6 +2,16 @@ File.rename = function (file1, file2, callback) {
this._addAction("rename", [file1, file2], callback);
};
File.stat = function (path, callback) {
this._addAction("stat", [path], callback);
};
File.exists = function (path, callback) {
this._addAction("stat", [path], function (status) {
callback(status == 0);
});
}
File.prototype.puts = function (data, callback) {
this.write(data + "\n", callback);
};

21
test/cat.js

@ -0,0 +1,21 @@
var filename = ARGV[2];
function cat (file) {
file.read(100, function (status, data) {
if (status == 0 && data) {
stdout.write(data.encodeUtf8());
cat(file);
} else {
file.close();
}
});
}
var f = new File;
f.open(filename, "r", function (status) {
puts("open!");
if (status == 0)
cat(f);
else
puts("error?");
})

14
test/stats.js

@ -0,0 +1,14 @@
File.stat(ARGV[2], function (status, stats) {
if (status == 0) {
for (var i in stats) {
puts(i + " : " + stats[i]);
}
} else {
puts("error: " + File.strerror(status));
}
});
File.exists(ARGV[2], function (r) {
puts("file exists: " + r);
});
Loading…
Cancel
Save