diff --git a/src/file.cc b/src/file.cc index 3baf676d6..edb4e7bcc 100644 --- a/src/file.cc +++ b/src/file.cc @@ -76,6 +76,7 @@ public: static Handle Read (const Arguments& args); static int AfterRead (eio_req *req); + private: bool HasUtf8Encoding (void); Persistent handle_; @@ -90,6 +91,8 @@ public: static Handle Stat (const Arguments& args); static int AfterStat (eio_req *req); + + static Handle StrError (const Arguments& args); }; Handle @@ -184,6 +187,21 @@ FileSystem::AfterStat (eio_req *req) return 0; } +Handle +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 message = String::New(strerror(errorno)); + + return scope.Close(message); +} + ///////////////////// FILE ///////////////////// File::File (Handle handle) @@ -483,6 +501,7 @@ NodeInit_file (Handle 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)); diff --git a/src/file.js b/src/file.js index 1c89ee607..9d133cce6 100644 --- a/src/file.js +++ b/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); }; diff --git a/test/cat.js b/test/cat.js new file mode 100644 index 000000000..b8cc48909 --- /dev/null +++ b/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?"); +}) diff --git a/test/stats.js b/test/stats.js new file mode 100644 index 000000000..4d670f85b --- /dev/null +++ b/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); +});