From b82ef28d9d27df56fa3b7c00c8c4209c227ae68c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 12 Feb 2010 01:04:14 -0800 Subject: [PATCH] Rename 'posix' module to 'fs' --- doc/api.txt | 56 +++++++++++++-------------- lib/file.js | 14 +++---- lib/posix.js | 1 + src/node.js | 8 ++-- test/mjsunit/common.js | 2 +- test/mjsunit/test-buffered-file.js | 4 +- test/mjsunit/test-eio-race.js | 20 +++++----- test/mjsunit/test-eio-race2.js | 4 +- test/mjsunit/test-eio-race3.js | 4 +- test/mjsunit/test-eio-race4.js | 2 +- test/mjsunit/test-file-cat-noexist.js | 2 +- test/mjsunit/test-fs-stat.js | 4 +- test/mjsunit/test-fs-write.js | 10 ++--- test/mjsunit/test-http-tls.js | 6 +-- test/mjsunit/test-mkdir-rmdir.js | 4 +- test/mjsunit/test-readdir.js | 2 +- test/mjsunit/test-sync-cat.js | 2 +- test/mjsunit/test-tcp-tls.js | 8 ++-- 18 files changed, 77 insertions(+), 76 deletions(-) create mode 100644 lib/posix.js diff --git a/doc/api.txt b/doc/api.txt index 33d4d67181c..98fe9a4b7a3 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -174,7 +174,7 @@ process.watchFile(f, function (curr, prev) { }); ------------------------- + -These stat objects are instances of +posix.Stat+. +These stat objects are instances of +fs.Stat+. +process.unwatchFile(filename)+:: Stop watching for changes on +filename+. @@ -609,16 +609,16 @@ will be sent +"SIGTERM"+. See signal(7) for a list of available signals. === POSIX module File I/O is provided by simple wrappers around standard POSIX functions. To -use this module do +require("posix")+. +use this module do +require("fs")+. All POSIX wrappers have a similar form. They return a promise (+events.Promise+). Example of deleting a file: ------------------------------------------------------------------------------ -var posix = require("posix"), - sys = require("sys"); +var fs = require("fs"), + sys = require("sys"); -var promise = posix.unlink("/tmp/hello"); +var promise = fs.unlink("/tmp/hello"); promise.addCallback(function () { sys.puts("successfully deleted /tmp/hello"); @@ -629,8 +629,8 @@ There is no guaranteed ordering to the POSIX wrappers. The following is very much prone to error ------------------------------------------------------------------------------ -posix.rename("/tmp/hello", "/tmp/world"); -posix.stat("/tmp/world").addCallback(function (stats) { +fs.rename("/tmp/hello", "/tmp/world"); +fs.stat("/tmp/world").addCallback(function (stats) { sys.puts("stats: " + JSON.stringify(stats)); }); ------------------------------------------------------------------------------ @@ -639,8 +639,8 @@ It could be that +stat()+ is executed before the +rename()+. The correct way to do this is to chain the promises. ------------------------------------------------------------------------------ -posix.rename("/tmp/hello", "/tmp/world").addCallback(function () { - posix.stat("/tmp/world").addCallback(function (stats) { +fs.rename("/tmp/hello", "/tmp/world").addCallback(function () { + fs.stat("/tmp/world").addCallback(function (stats) { sys.puts("stats: " + JSON.stringify(stats)); }); }); @@ -649,25 +649,25 @@ posix.rename("/tmp/hello", "/tmp/world").addCallback(function () { Or use the +promise.wait()+ functionality: ------------------------------------------------------------------------------ -posix.rename("/tmp/hello", "/tmp/world").wait(); -var stats = posix.stat("/tmp/world").wait(); +fs.rename("/tmp/hello", "/tmp/world").wait(); +var stats = fs.stat("/tmp/world").wait(); sys.puts("stats: " + JSON.stringify(stats)); ------------------------------------------------------------------------------ -+posix.rename(path1, path2)+ :: ++fs.rename(path1, path2)+ :: See rename(2). - on success: no parameters. - on error: no parameters. -+posix.truncate(fd, len)+ :: ++fs.truncate(fd, len)+ :: See ftruncate(2). - on success: no parameters. - on error: no parameters. -+posix.stat(path)+ :: ++fs.stat(path)+ :: See stat(2). -- on success: Returns +posix.Stats+ object. It looks like this: +- on success: Returns +fs.Stats+ object. It looks like this: + ------------------------------------------------------------------------------ { dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000, @@ -676,49 +676,49 @@ See stat(2). "2009-06-29T11:11:40Z" }+ ------------------------------------------------------------------------------ + -See the +posix.Stats+ section below for more information. +See the +fs.Stats+ section below for more information. - on error: no parameters. -+posix.unlink(path)+ :: ++fs.unlink(path)+ :: See unlink(2) - on success: no parameters. - on error: no parameters. -+posix.rmdir(path)+ :: ++fs.rmdir(path)+ :: See rmdir(2) - on success: no parameters. - on error: no parameters. -+posix.mkdir(path, mode)+ :: ++fs.mkdir(path, mode)+ :: See mkdir(2) - on success: no parameters. - on error: no parameters. -+posix.readdir(path)+ :: ++fs.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. -+posix.close(fd)+ :: ++fs.close(fd)+ :: See close(2) - on success: no parameters. - on error: no parameters. -+posix.open(path, flags, mode)+:: ++fs.open(path, flags, mode)+:: See open(2). The constants like +O_CREAT+ are defined at +process.O_CREAT+. - on success: +fd+ is given as the parameter. - on error: no parameters. -+posix.write(fd, data, position, encoding)+:: ++fs.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. @@ -726,7 +726,7 @@ See the +posix.Stats+ section below for more information. - on success: returns an integer +written+ which specifies how many _bytes_ were written. - on error: no parameters. -+posix.read(fd, length, position, encoding)+:: ++fs.read(fd, length, position, encoding)+:: Read data from the file specified by +fd+. + +length+ is an integer specifying the number of @@ -738,11 +738,11 @@ See the +posix.Stats+ section below for more information. - on success: returns +data, bytes_read+, what was read from the file. - on error: no parameters. -+posix.cat(filename, encoding="utf8")+:: ++fs.cat(filename, encoding="utf8")+:: Outputs the entire contents of a file. Example: + -------------------------------- -posix.cat("/etc/passwd").addCallback(function (content) { +fs.cat("/etc/passwd").addCallback(function (content) { sys.puts(content); }); -------------------------------- @@ -750,9 +750,9 @@ posix.cat("/etc/passwd").addCallback(function (content) { - on success: returns +data+, what was read from the file. - on error: no parameters. -==== +posix.Stats+ +==== +fs.Stats+ -Objects returned from +posix.stat()+ are of this type. +Objects returned from +fs.stat()+ are of this type. +stats.isFile()+:: diff --git a/lib/file.js b/lib/file.js index 93025870f2f..c024f545781 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,4 +1,4 @@ -var posix = require("./posix"); +var fs = require("./fs"); var events = require('events'); /*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */ /*globals exports, node, __filename */ @@ -17,24 +17,24 @@ function debugObject (obj) { } } -exports.read = posix.cat; +exports.read = fs.cat; exports.write = function (filename, data, encoding) { var promise = new events.Promise(); encoding = encoding || "utf8"; // default to utf8 - posix.open(filename, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0666) + fs.open(filename, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0666) .addCallback(function (fd) { function doWrite (_data) { - posix.write(fd, _data, 0, encoding) + fs.write(fd, _data, 0, encoding) .addErrback(function () { - posix.close(fd); + fs.close(fd); promise.emitError(); }) .addCallback(function (written) { if (written === _data.length) { - posix.close(fd); + fs.close(fd); promise.emitSuccess(); } else { doWrite(_data.slice(written)); @@ -122,7 +122,7 @@ proto._maybeDispatch = function () { if (!args[3] && (method === "read" || method === "write")) { args[3] = self.encoding; } - promise = posix[method].apply(self, args); + promise = fs[method].apply(self, args); userPromise = self.currentAction.promise; diff --git a/lib/posix.js b/lib/posix.js new file mode 100644 index 00000000000..d705b0ee206 --- /dev/null +++ b/lib/posix.js @@ -0,0 +1 @@ +throw new Error("The 'posix' module has been renamed to 'fs'"); diff --git a/src/node.js b/src/node.js index 96825fa81ee..052651de0b0 100644 --- a/src/node.js +++ b/src/node.js @@ -501,7 +501,7 @@ function debug (x) { -var posixModule = createInternalModule("posix", function (exports) { +var fsModule = createInternalModule("fs", function (exports) { exports.Stats = process.Stats; function callback (promise) { @@ -695,7 +695,7 @@ var posixModule = createInternalModule("posix", function (exports) { }; }); -var posix = posixModule.exports; +var fs = fsModule.exports; var pathModule = createInternalModule("path", function (exports) { @@ -757,7 +757,7 @@ var pathModule = createInternalModule("path", function (exports) { }; exports.exists = function (path, callback) { - var p = posix.stat(path); + var p = fs.stat(path); p.addCallback(function () { callback(true); }); p.addErrback(function () { callback(false); }); }; @@ -927,7 +927,7 @@ function cat (id, loadPromise) { loadPromise.emitError(new Error("could not load core module \"http\"")); }); } else { - promise = posix.cat(id); + promise = fs.cat(id); } return promise; diff --git a/test/mjsunit/common.js b/test/mjsunit/common.js index fc56968e7bb..7f2a277ff90 100644 --- a/test/mjsunit/common.js +++ b/test/mjsunit/common.js @@ -11,5 +11,5 @@ var sys = require("sys"); process.mixin(exports, sys); exports.assert = require('assert'); -exports.posix = require("posix"); +exports.fs = require("fs"); exports.path = path; diff --git a/test/mjsunit/test-buffered-file.js b/test/mjsunit/test-buffered-file.js index 21a55ca11c3..dbea2297151 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 = posix.cat(testTxt).wait(); + var out = fs.cat(testTxt).wait(); print("the file contains: "); p(out); assert.equal("hello\nworld\nhello\nworld\n", out); @@ -24,7 +24,7 @@ setTimeout(function () { file2.read(5).addCallback(function (data) { puts("read(5): " + JSON.stringify(data)); assert.equal("hello", data); - posix.unlink(testTxt).addCallback(function () { + fs.unlink(testTxt).addCallback(function () { fileUnlinked = true; }); }); diff --git a/test/mjsunit/test-eio-race.js b/test/mjsunit/test-eio-race.js index d77e791e85d..20eb3825f26 100644 --- a/test/mjsunit/test-eio-race.js +++ b/test/mjsunit/test-eio-race.js @@ -2,18 +2,18 @@ process.mixin(require("./common")); var count = 100, - posix = require('posix'); + fs = require('fs'); function tryToKillEventLoop() { puts('trying to kill event loop ...'); - posix.stat(__filename) + fs.stat(__filename) .addCallback(function() { - puts('first posix.stat succeeded ...'); + puts('first fs.stat succeeded ...'); - posix.stat(__filename) + fs.stat(__filename) .addCallback(function() { - puts('second posix.stat succeeded ...'); + puts('second fs.stat succeeded ...'); puts('could not kill event loop, retrying...'); setTimeout(function () { @@ -25,26 +25,26 @@ function tryToKillEventLoop() { }, 1); }) .addErrback(function() { - throw new Exception('second posix.stat failed') + throw new Exception('second fs.stat failed') }) }) .addErrback(function() { - throw new Exception('first posix.stat failed') + throw new Exception('first fs.stat failed') }); } // Generate a lot of thread pool events var pos = 0; -posix.open('/dev/zero', process.O_RDONLY, 0666).addCallback(function (rd) { +fs.open('/dev/zero', process.O_RDONLY, 0666).addCallback(function (rd) { function readChunk () { - posix.read(rd, 1024, pos, 'binary').addCallback(function (chunk, bytesRead) { + fs.read(rd, 1024, pos, 'binary').addCallback(function (chunk, bytesRead) { if (chunk) { pos += bytesRead; //puts(pos); readChunk(); } else { - posix.close(rd); + fs.close(rd); throw new Exception(BIG_FILE+' should not end before the issue shows up'); } }).addErrback(function () { diff --git a/test/mjsunit/test-eio-race2.js b/test/mjsunit/test-eio-race2.js index c52c81425c3..5e904f2253e 100644 --- a/test/mjsunit/test-eio-race2.js +++ b/test/mjsunit/test-eio-race2.js @@ -1,6 +1,6 @@ process.mixin(require("./common")); var testTxt = path.join(fixturesDir, "x.txt"); -var posix = require('posix'); +var fs = require('fs'); setTimeout(function () { // put this in a timeout, just so it doesn't get bunched up with the @@ -8,7 +8,7 @@ setTimeout(function () { N = 30; for (var i=0; i < N; i++) { puts("start " + i); - posix.cat(testTxt).addCallback(function(data) { + fs.cat(testTxt).addCallback(function(data) { puts("finish"); }).addErrback(function (e) { puts("error! " + e); diff --git a/test/mjsunit/test-eio-race3.js b/test/mjsunit/test-eio-race3.js index 30e4b921f2f..af4411c28b5 100644 --- a/test/mjsunit/test-eio-race3.js +++ b/test/mjsunit/test-eio-race3.js @@ -2,10 +2,10 @@ process.mixin(require("./common")); puts('first stat ...'); -posix.stat(__filename) +fs.stat(__filename) .addCallback( function(stats) { puts('second stat ...'); - posix.stat(__filename) + fs.stat(__filename) .timeout(1000) .wait(); diff --git a/test/mjsunit/test-eio-race4.js b/test/mjsunit/test-eio-race4.js index 93039e115b0..3dea273decc 100644 --- a/test/mjsunit/test-eio-race4.js +++ b/test/mjsunit/test-eio-race4.js @@ -4,7 +4,7 @@ var N = 100; var j = 0; for (var i = 0; i < N; i++) { - posix.stat("does-not-exist-" + i) // these files don't exist + fs.stat("does-not-exist-" + i) // these files don't exist .addErrback(function (e) { j++; // only makes it to about 17 puts("finish " + j); diff --git a/test/mjsunit/test-file-cat-noexist.js b/test/mjsunit/test-file-cat-noexist.js index fc0d4a43ce6..d11e002169b 100644 --- a/test/mjsunit/test-file-cat-noexist.js +++ b/test/mjsunit/test-file-cat-noexist.js @@ -2,7 +2,7 @@ process.mixin(require("./common")); var got_error = false; var filename = path.join(fixturesDir, "does_not_exist.txt"); -var promise = posix.cat(filename, "raw"); +var promise = fs.cat(filename, "raw"); promise.addCallback(function (content) { debug("cat returned some content: " + content); diff --git a/test/mjsunit/test-fs-stat.js b/test/mjsunit/test-fs-stat.js index 37010b8d3b5..94b2f3fdde7 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 = posix.stat("."); +var promise = fs.stat("."); promise.addCallback(function (_stats) { stats = _stats; @@ -17,7 +17,7 @@ promise.addErrback(function () { }); puts("stating: " + __filename); -posix.stat(__filename).addCallback(function (s) { +fs.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 b2f7378b01a..f8074d5bf46 100644 --- a/test/mjsunit/test-fs-write.js +++ b/test/mjsunit/test-fs-write.js @@ -4,12 +4,12 @@ var fn = path.join(fixturesDir, "write.txt"); var expected = "hello"; var found; -posix.open(fn, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0644).addCallback(function (file) { - posix.write(file, expected, 0, "utf8").addCallback(function() { - posix.close(file).addCallback(function() { - posix.cat(fn, process.UTF8).addCallback(function(contents) { +fs.open(fn, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0644).addCallback(function (file) { + fs.write(file, expected, 0, "utf8").addCallback(function() { + fs.close(file).addCallback(function() { + fs.cat(fn, process.UTF8).addCallback(function(contents) { found = contents; - posix.unlink(fn).wait(); + fs.unlink(fn).wait(); }); }); }); diff --git a/test/mjsunit/test-http-tls.js b/test/mjsunit/test-http-tls.js index 1a1b1e4f1d3..d4b91ade9ab 100644 --- a/test/mjsunit/test-http-tls.js +++ b/test/mjsunit/test-http-tls.js @@ -21,9 +21,9 @@ var responses_sent = 0; var responses_recvd = 0; var body0 = ""; var body1 = ""; -var caPem = posix.cat(fixturesDir+"/test_ca.pem").wait(); -var certPem = posix.cat(fixturesDir+"/test_cert.pem").wait(); -var keyPem = posix.cat(fixturesDir+"/test_key.pem").wait(); +var caPem = fs.cat(fixturesDir+"/test_ca.pem").wait(); +var certPem = fs.cat(fixturesDir+"/test_cert.pem").wait(); +var keyPem = fs.cat(fixturesDir+"/test_key.pem").wait(); var http_server=http.createServer(function (req, res) { diff --git a/test/mjsunit/test-mkdir-rmdir.js b/test/mjsunit/test-mkdir-rmdir.js index dfab84fcff1..9eb78ce5144 100644 --- a/test/mjsunit/test-mkdir-rmdir.js +++ b/test/mjsunit/test-mkdir-rmdir.js @@ -7,10 +7,10 @@ var d = path.join(fixtures, "dir"); var mkdir_error = false; var rmdir_error = false; -posix.mkdir(d, 0x666).addCallback(function () { +fs.mkdir(d, 0x666).addCallback(function () { puts("mkdir okay!"); - posix.rmdir(d).addCallback(function () { + fs.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 8cc0ee8c938..09b457536d1 100644 --- a/test/mjsunit/test-readdir.js +++ b/test/mjsunit/test-readdir.js @@ -2,7 +2,7 @@ process.mixin(require("./common")); var got_error = false; -var promise = posix.readdir(fixturesDir); +var promise = fs.readdir(fixturesDir); puts("readdir " + fixturesDir); promise.addCallback(function (files) { diff --git a/test/mjsunit/test-sync-cat.js b/test/mjsunit/test-sync-cat.js index 6e2ff309422..a602b1a1516 100644 --- a/test/mjsunit/test-sync-cat.js +++ b/test/mjsunit/test-sync-cat.js @@ -2,4 +2,4 @@ process.mixin(require('./common')); var fixture = path.join(__dirname, "fixtures/x.txt"); -assert.equal("xyz\n", posix.catSync(fixture)); \ No newline at end of file +assert.equal("xyz\n", fs.catSync(fixture)); \ No newline at end of file diff --git a/test/mjsunit/test-tcp-tls.js b/test/mjsunit/test-tcp-tls.js index 8ea488271df..d8f7459c026 100644 --- a/test/mjsunit/test-tcp-tls.js +++ b/test/mjsunit/test-tcp-tls.js @@ -1,6 +1,6 @@ process.mixin(require("./common")); tcp = require("tcp"); -posix=require("posix"); +fs=require("fs"); var tests_run = 0; @@ -105,9 +105,9 @@ try { } if (have_tls) { - var caPem = posix.cat(fixturesDir+"/test_ca.pem").wait(); - var certPem = posix.cat(fixturesDir+"/test_cert.pem").wait(); - var keyPem = posix.cat(fixturesDir+"/test_key.pem").wait(); + var caPem = fs.cat(fixturesDir+"/test_ca.pem").wait(); + var certPem = fs.cat(fixturesDir+"/test_cert.pem").wait(); + var keyPem = fs.cat(fixturesDir+"/test_key.pem").wait(); /* All are run at once, so run on different ports */ tlsTest(20443, "localhost", caPem, keyPem, certPem);