node.fs.* moved into "/posix.js"
authorRyan Dahl <ry@tinyclouds.org>
Wed, 28 Oct 2009 21:45:40 +0000 (22:45 +0100)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 28 Oct 2009 21:45:46 +0000 (22:45 +0100)
use require("/posix.js") to access them.

12 files changed:
doc/api.txt
lib/file.js
lib/posix.js [new file with mode: 0644]
src/file.js
test/mjsunit/common.js
test/mjsunit/test-buffered-file.js
test/mjsunit/test-file-cat-noexist.js
test/mjsunit/test-fs-sendfile.js
test/mjsunit/test-fs-stat.js
test/mjsunit/test-fs-write.js
test/mjsunit/test-mkdir-rmdir.js
test/mjsunit/test-readdir.js

index ac22e24..9d4d0d7 100644 (file)
@@ -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()+::
 
index dd34278..6875837 100644 (file)
@@ -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 (file)
index 0000000..8dd2b39
--- /dev/null
@@ -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];
+}
index 8983b88..acd4f13 100644 (file)
@@ -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);
-};
index 808f676..f7d0a95 100644 (file)
@@ -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");
 
index 23502b5..5d1385a 100644 (file)
@@ -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;
       });
     });
index a35fb59..a34da80 100644 (file)
@@ -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);
index f4f9dc2..59b48b7 100644 (file)
@@ -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);
     });
   });
index 33c14ba..8b87f43 100644 (file)
@@ -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++;
 
index b72d4bd..e4dbcb9 100644 (file)
@@ -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();
       });
     });
   });
index 6fedb5c..369cf3a 100644 (file)
@@ -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) {
index 8e53e14..4463ae9 100644 (file)
@@ -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) {