use require("/posix.js") to access them.
-=== 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");
});
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));
});
------------------------------------------------------------------------------
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));
});
});
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.
- 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+.
+
- 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);
});
--------------------------------
- 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()+::
+var posix = require("/posix.js");
/*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */
/*globals exports, node, __filename */
}
}
-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));
}
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;
--- /dev/null
+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];
+}
});
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);
-};
var mjsunit = require("/mjsunit.js");
var utils = require("/utils.js");
node.mixin(exports, mjsunit, utils);
+exports.posix = require("/posix.js");
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);
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;
});
});
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);
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);
});
});
var success_count = 0;
var stats;
-var promise = node.fs.stat(".");
+var promise = posix.stat(".");
promise.addCallback(function (_stats) {
stats = _stats;
});
puts("stating: " + __filename);
-node.fs.stat(__filename).addCallback(function (s) {
+posix.stat(__filename).addCallback(function (s) {
p(s);
success_count++;
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();
});
});
});
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) {
var got_error = false;
-var promise = node.fs.readdir(fixturesDir);
+var promise = posix.readdir(fixturesDir);
puts("readdir " + fixturesDir);
promise.addCallback(function (files) {