Browse Source

node.fs.* moved into "/posix.js"

use require("/posix.js") to access them.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
3d8b14e6f7
  1. 57
      doc/api.txt
  2. 13
      lib/file.js
  3. 35
      lib/posix.js
  4. 32
      src/file.js
  5. 1
      test/mjsunit/common.js
  6. 4
      test/mjsunit/test-buffered-file.js
  7. 2
      test/mjsunit/test-file-cat-noexist.js
  8. 4
      test/mjsunit/test-fs-sendfile.js
  9. 4
      test/mjsunit/test-fs-stat.js
  10. 10
      test/mjsunit/test-fs-write.js
  11. 4
      test/mjsunit/test-mkdir-rmdir.js
  12. 2
      test/mjsunit/test-readdir.js

57
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. File I/O is provided by simple wrappers around standard POSIX functions. To
All POSIX wrappers have a similar form. use this module do +require("/posix.js")+.
They return a promise (+node.Promise+). Example:
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 () { promise.addCallback(function () {
puts("successfully deleted /tmp/hello"); 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 following is very much prone to error
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
node.fs.rename("/tmp/hello", "/tmp/world"); posix.rename("/tmp/hello", "/tmp/world");
node.fs.stat("/tmp/world").addCallback(function (stats) { posix.stat("/tmp/world").addCallback(function (stats) {
puts("stats: " + JSON.stringify(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. The correct way to do this is to chain the promises.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
node.fs.rename("/tmp/hello", "/tmp/world").addCallback(function () { posix.rename("/tmp/hello", "/tmp/world").addCallback(function () {
node.fs.stat("/tmp/world").addCallback(function (stats) { posix.stat("/tmp/world").addCallback(function (stats) {
puts("stats: " + JSON.stringify(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: Or use the +promise.wait()+ functionality:
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
node.fs.rename("/tmp/hello", "/tmp/world").wait(); posix.rename("/tmp/hello", "/tmp/world").wait();
node.fs.stat("/tmp/world").addCallback(function (stats) { posix.stat("/tmp/world").addCallback(function (stats) {
puts("stats: " + JSON.stringify(stats)); puts("stats: " + JSON.stringify(stats));
}); });
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
+node.fs.rename(path1, path2)+ :: +posix.rename(path1, path2)+ ::
See rename(2). See rename(2).
- on success: no parameters. - on success: no parameters.
- on error: no parameters. - on error: no parameters.
+node.fs.stat(path)+ :: +posix.stat(path)+ ::
See stat(2). 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, +{ dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000,
rdev: 0, size: 4096, blksize: 4096, blocks: 8, atime: 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:55Z", mtime: "2009-06-29T11:11:40Z", ctime:
"2009-06-29T11:11:40Z" }+ "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. - on error: no parameters.
+node.fs.unlink(path)+ :: +posix.unlink(path)+ ::
See unlink(2) See unlink(2)
- on success: no parameters. - on success: no parameters.
- on error: no parameters. - on error: no parameters.
+node.fs.rmdir(path)+ :: +posix.rmdir(path)+ ::
See rmdir(2) See rmdir(2)
- on success: no parameters. - on success: no parameters.
- on error: no parameters. - on error: no parameters.
+node.fs.mkdir(path, mode)+ :: +posix.mkdir(path, mode)+ ::
See mkdir(2) See mkdir(2)
- on success: no parameters. - on success: no parameters.
- on error: no parameters. - on error: no parameters.
+node.fs.readdir(path)+ :: +posix.readdir(path)+ ::
Reads the contents of a directory. Reads the contents of a directory.
- on success: One argument, an array containing the names (strings) of the - on success: One argument, an array containing the names (strings) of the
files in the directory (excluding "." and ".."). files in the directory (excluding "." and "..").
- on error: no parameters. - on error: no parameters.
+node.fs.close(fd)+ :: +posix.close(fd)+ ::
See close(2) See close(2)
- on success: no parameters. - on success: no parameters.
- on error: 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+. See open(2). The constants like +O_CREAT+ are defined at +node.O_CREAT+.
- on success: +fd+ is given as the parameter. - on success: +fd+ is given as the parameter.
- on error: no parameters. - 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 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 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. +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. - 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+. 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 success: returns +data, bytes_read+, what was read from the file.
- on error: no parameters. - on error: no parameters.
+node.fs.cat(filename, encoding="utf8")+:: +posix.cat(filename, encoding="utf8")+::
Outputs the entire contents of a file. Example: 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); 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 success: returns +data+, what was read from the file.
- on error: no parameters. - 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()+:: +stats.isFile()+::

13
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 */ /*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
/*globals exports, node, __filename */ /*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) { exports.write = function (filename, data, encoding) {
var promise = new node.Promise(); var promise = new node.Promise();
encoding = encoding || "utf8"; // default to utf8 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) { .addCallback(function (fd) {
function doWrite (_data) { function doWrite (_data) {
node.fs.write(fd, _data, 0, encoding) posix.write(fd, _data, 0, encoding)
.addErrback(function () { .addErrback(function () {
node.fs.close(fd); posix.close(fd);
}) })
.addCallback(function (written) { .addCallback(function (written) {
if (written === _data.length) { if (written === _data.length) {
node.fs.close(fd); posix.close(fd);
} else { } else {
doWrite(_data.slice(written)); doWrite(_data.slice(written));
} }
@ -118,7 +119,7 @@ proto._maybeDispatch = function () {
if (!args[3] && (method === "read" || method === "write")) { if (!args[3] && (method === "read" || method === "write")) {
args[3] = self.encoding; args[3] = self.encoding;
} }
promise = node.fs[method].apply(self, args); promise = posix[method].apply(self, args);
userPromise = self.currentAction.promise; userPromise = self.currentAction.promise;

35
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];
}

32
src/file.js

@ -37,35 +37,3 @@ node.fs.cat = function (path, encoding) {
}); });
return promise; 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);
};

1
test/mjsunit/common.js

@ -7,4 +7,5 @@ require.paths.unshift(exports.libDir);
var mjsunit = require("/mjsunit.js"); var mjsunit = require("/mjsunit.js");
var utils = require("/utils.js"); var utils = require("/utils.js");
node.mixin(exports, mjsunit, utils); node.mixin(exports, mjsunit, utils);
exports.posix = require("/posix.js");

4
test/mjsunit/test-buffered-file.js

@ -16,7 +16,7 @@ setTimeout(function () {
file.write("world\n"); file.write("world\n");
file.close().addCallback(function () { file.close().addCallback(function () {
error("file closed..."); error("file closed...");
var out = node.fs.cat(testTxt).wait(); var out = posix.cat(testTxt).wait();
print("the file contains: "); print("the file contains: ");
p(out); p(out);
assertEquals("hello\nworld\nhello\nworld\n", out); assertEquals("hello\nworld\nhello\nworld\n", out);
@ -24,7 +24,7 @@ setTimeout(function () {
file2.read(5).addCallback(function (data) { file2.read(5).addCallback(function (data) {
puts("read(5): " + JSON.stringify(data)); puts("read(5): " + JSON.stringify(data));
assertEquals("hello", data); assertEquals("hello", data);
node.fs.unlink(testTxt).addCallback(function () { posix.unlink(testTxt).addCallback(function () {
fileUnlinked = true; fileUnlinked = true;
}); });
}); });

2
test/mjsunit/test-file-cat-noexist.js

@ -2,7 +2,7 @@ node.mixin(require("common.js"));
var got_error = false; var got_error = false;
var filename = node.path.join(fixturesDir, "does_not_exist.txt"); 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) { promise.addCallback(function (content) {
debug("cat returned some content: " + content); debug("cat returned some content: " + content);

4
test/mjsunit/test-fs-sendfile.js

@ -19,8 +19,8 @@ server.listen(PORT);
var client = tcp.createConnection(PORT); var client = tcp.createConnection(PORT);
client.addListener("connect", function () { client.addListener("connect", function () {
node.fs.open(x,node.O_RDONLY, 0666).addCallback(function (fd) { posix.open(x,node.O_RDONLY, 0666).addCallback(function (fd) {
node.fs.sendfile(client.fd, fd, 0, expected.length).addCallback(function (size) { posix.sendfile(client.fd, fd, 0, expected.length).addCallback(function (size) {
assertEquals(expected.length, size); assertEquals(expected.length, size);
}); });
}); });

4
test/mjsunit/test-fs-stat.js

@ -4,7 +4,7 @@ var got_error = false;
var success_count = 0; var success_count = 0;
var stats; var stats;
var promise = node.fs.stat("."); var promise = posix.stat(".");
promise.addCallback(function (_stats) { promise.addCallback(function (_stats) {
stats = _stats; stats = _stats;
@ -17,7 +17,7 @@ promise.addErrback(function () {
}); });
puts("stating: " + __filename); puts("stating: " + __filename);
node.fs.stat(__filename).addCallback(function (s) { posix.stat(__filename).addCallback(function (s) {
p(s); p(s);
success_count++; success_count++;

10
test/mjsunit/test-fs-write.js

@ -4,12 +4,12 @@ var path = node.path.join(fixturesDir, "write.txt");
var expected = "hello"; var expected = "hello";
var found; var found;
node.fs.open(path, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0644).addCallback(function (file) { posix.open(path, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0644).addCallback(function (file) {
node.fs.write(file, expected, 0, "utf8").addCallback(function() { posix.write(file, expected, 0, "utf8").addCallback(function() {
node.fs.close(file).addCallback(function() { posix.close(file).addCallback(function() {
node.fs.cat(path, node.UTF8).addCallback(function(contents) { posix.cat(path, node.UTF8).addCallback(function(contents) {
found = contents; found = contents;
node.fs.unlink(path).wait(); posix.unlink(path).wait();
}); });
}); });
}); });

4
test/mjsunit/test-mkdir-rmdir.js

@ -7,10 +7,10 @@ var d = node.path.join(fixtures, "dir");
var mkdir_error = false; var mkdir_error = false;
var rmdir_error = false; var rmdir_error = false;
node.fs.mkdir(d, 0x666).addCallback(function () { posix.mkdir(d, 0x666).addCallback(function () {
puts("mkdir okay!"); puts("mkdir okay!");
node.fs.rmdir(d).addCallback(function () { posix.rmdir(d).addCallback(function () {
puts("rmdir okay!"); puts("rmdir okay!");
}).addErrback(function (e) { }).addErrback(function (e) {

2
test/mjsunit/test-readdir.js

@ -2,7 +2,7 @@ node.mixin(require("common.js"));
var got_error = false; var got_error = false;
var promise = node.fs.readdir(fixturesDir); var promise = posix.readdir(fixturesDir);
puts("readdir " + fixturesDir); puts("readdir " + fixturesDir);
promise.addCallback(function (files) { promise.addCallback(function (files) {

Loading…
Cancel
Save