From 3d8b14e6f785f72f1b4c399a9fcccbf1ca7ec633 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 28 Oct 2009 22:45:40 +0100 Subject: [PATCH] node.fs.* moved into "/posix.js" use require("/posix.js") to access them. --- doc/api.txt | 57 ++++++++++++++------------- lib/file.js | 13 +++--- lib/posix.js | 35 ++++++++++++++++ src/file.js | 32 --------------- test/mjsunit/common.js | 1 + test/mjsunit/test-buffered-file.js | 4 +- test/mjsunit/test-file-cat-noexist.js | 2 +- test/mjsunit/test-fs-sendfile.js | 4 +- test/mjsunit/test-fs-stat.js | 4 +- test/mjsunit/test-fs-write.js | 10 ++--- test/mjsunit/test-mkdir-rmdir.js | 4 +- test/mjsunit/test-readdir.js | 2 +- 12 files changed, 88 insertions(+), 80 deletions(-) create mode 100644 lib/posix.js diff --git a/doc/api.txt b/doc/api.txt index ac22e243ef..9d4d0d7680 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -464,14 +464,17 @@ will be sent +"SIGTERM"+. See signal(7) for a list of available signals. -=== File I/O +=== POSIX module -File I/O is provided by simple wrappers around standard POSIX functions. -All POSIX wrappers have a similar form. -They return a promise (+node.Promise+). Example: +File I/O is provided by simple wrappers around standard POSIX functions. To +use this module do +require("/posix.js")+. + +All POSIX wrappers have a similar form. They return a promise +(+node.Promise+). Example: ------------------------------------------------------------------------------ -var promise = node.fs.unlink("/tmp/hello"); +var posix = require("/posix.js"); +var promise = posix.unlink("/tmp/hello"); promise.addCallback(function () { puts("successfully deleted /tmp/hello"); }); @@ -481,8 +484,8 @@ There is no guaranteed ordering to the POSIX wrappers. The following is very much prone to error ------------------------------------------------------------------------------ -node.fs.rename("/tmp/hello", "/tmp/world"); -node.fs.stat("/tmp/world").addCallback(function (stats) { +posix.rename("/tmp/hello", "/tmp/world"); +posix.stat("/tmp/world").addCallback(function (stats) { puts("stats: " + JSON.stringify(stats)); }); ------------------------------------------------------------------------------ @@ -491,8 +494,8 @@ It could be that +stat()+ is executed before the +rename()+. The correct way to do this is to chain the promises. ------------------------------------------------------------------------------ -node.fs.rename("/tmp/hello", "/tmp/world").addCallback(function () { - node.fs.stat("/tmp/world").addCallback(function (stats) { +posix.rename("/tmp/hello", "/tmp/world").addCallback(function () { + posix.stat("/tmp/world").addCallback(function (stats) { puts("stats: " + JSON.stringify(stats)); }); }); @@ -501,65 +504,65 @@ node.fs.rename("/tmp/hello", "/tmp/world").addCallback(function () { Or use the +promise.wait()+ functionality: ------------------------------------------------------------------------------ -node.fs.rename("/tmp/hello", "/tmp/world").wait(); -node.fs.stat("/tmp/world").addCallback(function (stats) { +posix.rename("/tmp/hello", "/tmp/world").wait(); +posix.stat("/tmp/world").addCallback(function (stats) { puts("stats: " + JSON.stringify(stats)); }); ------------------------------------------------------------------------------ -+node.fs.rename(path1, path2)+ :: ++posix.rename(path1, path2)+ :: See rename(2). - on success: no parameters. - on error: no parameters. -+node.fs.stat(path)+ :: ++posix.stat(path)+ :: See stat(2). - - on success: Returns +node.fs.Stats+ object. It looks like this: + - on success: Returns +posix.Stats+ object. It looks like this: +{ dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000, rdev: 0, size: 4096, blksize: 4096, blocks: 8, atime: "2009-06-29T11:11:55Z", mtime: "2009-06-29T11:11:40Z", ctime: "2009-06-29T11:11:40Z" }+ - See the +node.fs.Stats+ section below for more information. + See the +posix.Stats+ section below for more information. - on error: no parameters. -+node.fs.unlink(path)+ :: ++posix.unlink(path)+ :: See unlink(2) - on success: no parameters. - on error: no parameters. -+node.fs.rmdir(path)+ :: ++posix.rmdir(path)+ :: See rmdir(2) - on success: no parameters. - on error: no parameters. -+node.fs.mkdir(path, mode)+ :: ++posix.mkdir(path, mode)+ :: See mkdir(2) - on success: no parameters. - on error: no parameters. -+node.fs.readdir(path)+ :: ++posix.readdir(path)+ :: Reads the contents of a directory. - on success: One argument, an array containing the names (strings) of the files in the directory (excluding "." and ".."). - on error: no parameters. -+node.fs.close(fd)+ :: ++posix.close(fd)+ :: See close(2) - on success: no parameters. - on error: no parameters. -+node.fs.open(path, flags, mode)+:: ++posix.open(path, flags, mode)+:: See open(2). The constants like +O_CREAT+ are defined at +node.O_CREAT+. - on success: +fd+ is given as the parameter. - on error: no parameters. -+node.fs.write(fd, data, position, encoding)+:: ++posix.write(fd, data, position, encoding)+:: Write data to the file specified by +fd+. +position+ refers to the offset from the beginning of the file where this data should be written. If +position+ is +null+, the data will be written at the current position. @@ -568,7 +571,7 @@ node.fs.stat("/tmp/world").addCallback(function (stats) { - on error: no parameters. -+node.fs.read(fd, length, position, encoding)+:: ++posix.read(fd, length, position, encoding)+:: Read data from the file specified by +fd+. + @@ -581,12 +584,12 @@ reading from in the file. - on success: returns +data, bytes_read+, what was read from the file. - on error: no parameters. -+node.fs.cat(filename, encoding="utf8")+:: ++posix.cat(filename, encoding="utf8")+:: Outputs the entire contents of a file. Example: + -------------------------------- -node.fs.cat("/etc/passwd").addCallback(function (content) { +posix.cat("/etc/passwd").addCallback(function (content) { puts(content); }); -------------------------------- @@ -594,9 +597,9 @@ node.fs.cat("/etc/passwd").addCallback(function (content) { - on success: returns +data+, what was read from the file. - on error: no parameters. -==== +node.fs.Stats+ +==== +posix.Stats+ -Objects returned from +node.fs.stat()+ are of this type. +Objects returned from +posix.stat()+ are of this type. +stats.isFile()+:: diff --git a/lib/file.js b/lib/file.js index dd3427876e..68758377f7 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,3 +1,4 @@ +var posix = require("/posix.js"); /*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */ /*globals exports, node, __filename */ @@ -15,23 +16,23 @@ function debugObject (obj) { } } -exports.read = node.fs.cat; +exports.read = posix.cat; exports.write = function (filename, data, encoding) { var promise = new node.Promise(); encoding = encoding || "utf8"; // default to utf8 - node.fs.open(filename, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0666) + posix.open(filename, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0666) .addCallback(function (fd) { function doWrite (_data) { - node.fs.write(fd, _data, 0, encoding) + posix.write(fd, _data, 0, encoding) .addErrback(function () { - node.fs.close(fd); + posix.close(fd); }) .addCallback(function (written) { if (written === _data.length) { - node.fs.close(fd); + posix.close(fd); } else { doWrite(_data.slice(written)); } @@ -118,7 +119,7 @@ proto._maybeDispatch = function () { if (!args[3] && (method === "read" || method === "write")) { args[3] = self.encoding; } - promise = node.fs[method].apply(self, args); + promise = posix[method].apply(self, args); userPromise = self.currentAction.promise; diff --git a/lib/posix.js b/lib/posix.js new file mode 100644 index 0000000000..8dd2b39fcf --- /dev/null +++ b/lib/posix.js @@ -0,0 +1,35 @@ +node.fs.Stats.prototype._checkModeProperty = function (property) { + return ((this.mode & property) === property); +}; + +node.fs.Stats.prototype.isDirectory = function () { + return this._checkModeProperty(node.S_IFDIR); +}; + +node.fs.Stats.prototype.isFile = function () { + return this._checkModeProperty(node.S_IFREG); +}; + +node.fs.Stats.prototype.isBlockDevice = function () { + return this._checkModeProperty(node.S_IFBLK); +}; + +node.fs.Stats.prototype.isCharacterDevice = function () { + return this._checkModeProperty(node.S_IFCHR); +}; + +node.fs.Stats.prototype.isSymbolicLink = function () { + return this._checkModeProperty(node.S_IFLNK); +}; + +node.fs.Stats.prototype.isFIFO = function () { + return this._checkModeProperty(node.S_IFIFO); +}; + +node.fs.Stats.prototype.isSocket = function () { + return this._checkModeProperty(node.S_IFSOCK); +}; + +for (var key in node.fs) { + if (node.fs.hasOwnProperty(key)) exports[key] = node.fs[key]; +} diff --git a/src/file.js b/src/file.js index 8983b88c73..acd4f13705 100644 --- a/src/file.js +++ b/src/file.js @@ -37,35 +37,3 @@ node.fs.cat = function (path, encoding) { }); return promise; }; - -node.fs.Stats.prototype._checkModeProperty = function (property) { - return ((this.mode & property) === property); -}; - -node.fs.Stats.prototype.isDirectory = function () { - return this._checkModeProperty(node.S_IFDIR); -}; - -node.fs.Stats.prototype.isFile = function () { - return this._checkModeProperty(node.S_IFREG); -}; - -node.fs.Stats.prototype.isBlockDevice = function () { - return this._checkModeProperty(node.S_IFBLK); -}; - -node.fs.Stats.prototype.isCharacterDevice = function () { - return this._checkModeProperty(node.S_IFCHR); -}; - -node.fs.Stats.prototype.isSymbolicLink = function () { - return this._checkModeProperty(node.S_IFLNK); -}; - -node.fs.Stats.prototype.isFIFO = function () { - return this._checkModeProperty(node.S_IFIFO); -}; - -node.fs.Stats.prototype.isSocket = function () { - return this._checkModeProperty(node.S_IFSOCK); -}; diff --git a/test/mjsunit/common.js b/test/mjsunit/common.js index 808f676e81..f7d0a955d2 100644 --- a/test/mjsunit/common.js +++ b/test/mjsunit/common.js @@ -7,4 +7,5 @@ require.paths.unshift(exports.libDir); var mjsunit = require("/mjsunit.js"); var utils = require("/utils.js"); node.mixin(exports, mjsunit, utils); +exports.posix = require("/posix.js"); diff --git a/test/mjsunit/test-buffered-file.js b/test/mjsunit/test-buffered-file.js index 23502b554b..5d1385abe9 100644 --- a/test/mjsunit/test-buffered-file.js +++ b/test/mjsunit/test-buffered-file.js @@ -16,7 +16,7 @@ setTimeout(function () { file.write("world\n"); file.close().addCallback(function () { error("file closed..."); - var out = node.fs.cat(testTxt).wait(); + var out = posix.cat(testTxt).wait(); print("the file contains: "); p(out); assertEquals("hello\nworld\nhello\nworld\n", out); @@ -24,7 +24,7 @@ setTimeout(function () { file2.read(5).addCallback(function (data) { puts("read(5): " + JSON.stringify(data)); assertEquals("hello", data); - node.fs.unlink(testTxt).addCallback(function () { + posix.unlink(testTxt).addCallback(function () { fileUnlinked = true; }); }); diff --git a/test/mjsunit/test-file-cat-noexist.js b/test/mjsunit/test-file-cat-noexist.js index a35fb595d1..a34da80b88 100644 --- a/test/mjsunit/test-file-cat-noexist.js +++ b/test/mjsunit/test-file-cat-noexist.js @@ -2,7 +2,7 @@ node.mixin(require("common.js")); var got_error = false; var filename = node.path.join(fixturesDir, "does_not_exist.txt"); -var promise = node.fs.cat(filename, "raw"); +var promise = posix.cat(filename, "raw"); promise.addCallback(function (content) { debug("cat returned some content: " + content); diff --git a/test/mjsunit/test-fs-sendfile.js b/test/mjsunit/test-fs-sendfile.js index f4f9dc2736..59b48b7fd9 100644 --- a/test/mjsunit/test-fs-sendfile.js +++ b/test/mjsunit/test-fs-sendfile.js @@ -19,8 +19,8 @@ server.listen(PORT); var client = tcp.createConnection(PORT); client.addListener("connect", function () { - node.fs.open(x,node.O_RDONLY, 0666).addCallback(function (fd) { - node.fs.sendfile(client.fd, fd, 0, expected.length).addCallback(function (size) { + posix.open(x,node.O_RDONLY, 0666).addCallback(function (fd) { + posix.sendfile(client.fd, fd, 0, expected.length).addCallback(function (size) { assertEquals(expected.length, size); }); }); diff --git a/test/mjsunit/test-fs-stat.js b/test/mjsunit/test-fs-stat.js index 33c14ba3d8..8b87f439e5 100644 --- a/test/mjsunit/test-fs-stat.js +++ b/test/mjsunit/test-fs-stat.js @@ -4,7 +4,7 @@ var got_error = false; var success_count = 0; var stats; -var promise = node.fs.stat("."); +var promise = posix.stat("."); promise.addCallback(function (_stats) { stats = _stats; @@ -17,7 +17,7 @@ promise.addErrback(function () { }); puts("stating: " + __filename); -node.fs.stat(__filename).addCallback(function (s) { +posix.stat(__filename).addCallback(function (s) { p(s); success_count++; diff --git a/test/mjsunit/test-fs-write.js b/test/mjsunit/test-fs-write.js index b72d4bd328..e4dbcb9ccc 100644 --- a/test/mjsunit/test-fs-write.js +++ b/test/mjsunit/test-fs-write.js @@ -4,12 +4,12 @@ var path = node.path.join(fixturesDir, "write.txt"); var expected = "hello"; var found; -node.fs.open(path, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0644).addCallback(function (file) { - node.fs.write(file, expected, 0, "utf8").addCallback(function() { - node.fs.close(file).addCallback(function() { - node.fs.cat(path, node.UTF8).addCallback(function(contents) { +posix.open(path, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0644).addCallback(function (file) { + posix.write(file, expected, 0, "utf8").addCallback(function() { + posix.close(file).addCallback(function() { + posix.cat(path, node.UTF8).addCallback(function(contents) { found = contents; - node.fs.unlink(path).wait(); + posix.unlink(path).wait(); }); }); }); diff --git a/test/mjsunit/test-mkdir-rmdir.js b/test/mjsunit/test-mkdir-rmdir.js index 6fedb5c1f8..369cf3a144 100644 --- a/test/mjsunit/test-mkdir-rmdir.js +++ b/test/mjsunit/test-mkdir-rmdir.js @@ -7,10 +7,10 @@ var d = node.path.join(fixtures, "dir"); var mkdir_error = false; var rmdir_error = false; -node.fs.mkdir(d, 0x666).addCallback(function () { +posix.mkdir(d, 0x666).addCallback(function () { puts("mkdir okay!"); - node.fs.rmdir(d).addCallback(function () { + posix.rmdir(d).addCallback(function () { puts("rmdir okay!"); }).addErrback(function (e) { diff --git a/test/mjsunit/test-readdir.js b/test/mjsunit/test-readdir.js index 8e53e14e6c..4463ae9218 100644 --- a/test/mjsunit/test-readdir.js +++ b/test/mjsunit/test-readdir.js @@ -2,7 +2,7 @@ node.mixin(require("common.js")); var got_error = false; -var promise = node.fs.readdir(fixturesDir); +var promise = posix.readdir(fixturesDir); puts("readdir " + fixturesDir); promise.addCallback(function (files) {