diff --git a/doc/api.txt b/doc/api.txt index 1b3b102db9..73f4f54c2e 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -153,30 +153,6 @@ Send a signal to a process. +pid+ is the process id and +signal+ is the signal to send; for example, "SIGINT" or "SIGUSR1". See kill(2) for more information. -+process.watchFile(filename, [options,] listener)+:: -Watch for changes on +filename+. The callback +listener+ will be called each -time the file changes. -+ -The second argument is optional. The +options+ if provided should be an -object containing two members a boolean, +persistent+, and +interval+, a -polling value in milliseconds. The default is +{persistent: true, interval: -0}+. -+ -The +listener+ gets two arguments the current stat object and the previous -stat object: -+ -------------------------- -process.watchFile(f, function (curr, prev) { - sys.puts("the current mtime is: " + curr.mtime); - sys.puts("the previous mtime was: " + prev.mtime); -}); -------------------------- -+ -These stat objects are instances of +fs.Stat+. - -+process.unwatchFile(filename)+:: -Stop watching for changes on +filename+. - +process.compile(source, scriptOrigin)+:: Just like +eval()+ except that you can specify a +scriptOrigin+ for better error reporting. @@ -646,6 +622,14 @@ The callback gets two arguments +(err, resolvedPath)+. Synchronous readlink(2). Returns the resolved path. ++fs.realpath(path, callback)+ :: +Asynchronous realpath(2). +The callback gets two arguments +(err, resolvedPath)+. + ++fs.realpathSync(path)+ :: +Synchronous realpath(2). Returns the resolved path. + + +fs.unlink(path, callback)+ :: Asynchronous unlink(2). No arguments other than a possible exception are given to the completion callback. @@ -755,6 +739,30 @@ fs.writeFile("message.txt", "Hello Node", function (err) { +fs.writeFileSync(filename, data, encoding="utf8")+:: The synchronous version of +fs.writeFile+. ++fs.watchFile(filename, [options,] listener)+:: +Watch for changes on +filename+. The callback +listener+ will be called each +time the file changes. ++ +The second argument is optional. The +options+ if provided should be an +object containing two members a boolean, +persistent+, and +interval+, a +polling value in milliseconds. The default is +{persistent: true, interval: +0}+. ++ +The +listener+ gets two arguments the current stat object and the previous +stat object: ++ +------------------------- +fs.watchFile(f, function (curr, prev) { + sys.puts("the current mtime is: " + curr.mtime); + sys.puts("the previous mtime was: " + prev.mtime); +}); +------------------------- ++ +These stat objects are instances of +fs.Stat+. + ++fs.unwatchFile(filename)+:: +Stop watching for changes on +filename+. + === +fs.Stats+ diff --git a/doc/index.html b/doc/index.html index d5f3661f3b..e28ae2e8d4 100644 --- a/doc/index.html +++ b/doc/index.html @@ -239,7 +239,9 @@ make install
git clone git://github.com/ry/node.git cd node -# edit/compile/test +(make your changes) +./configure --debug +make test-all # Check your patch with both debug and release builds git commit -m "Good description of what your patch does" git format-patch HEAD^diff --git a/lib/assert.js b/lib/assert.js index 4f6c46c9a7..0a8c19715e 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -23,7 +23,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // UTILITY - +var inherits = require('./sys').inherits; var pSlice = Array.prototype.slice; // 1. The assert module provides functions that throw @@ -47,7 +47,7 @@ assert.AssertionError = function AssertionError (options) { Error.captureStackTrace(this, stackStartFunction); } }; -process.inherits(assert.AssertionError, Error); +inherits(assert.AssertionError, Error); assert.AssertionError.prototype.toString = function() { if (this.message) { diff --git a/lib/fs.js b/lib/fs.js new file mode 100644 index 0000000000..5687424dc2 --- /dev/null +++ b/lib/fs.js @@ -0,0 +1,377 @@ +exports.Stats = process.Stats; + +process.Stats.prototype._checkModeProperty = function (property) { + return ((this.mode & property) === property); +}; + +process.Stats.prototype.isDirectory = function () { + return this._checkModeProperty(process.S_IFDIR); +}; + +process.Stats.prototype.isFile = function () { + return this._checkModeProperty(process.S_IFREG); +}; + +process.Stats.prototype.isBlockDevice = function () { + return this._checkModeProperty(process.S_IFBLK); +}; + +process.Stats.prototype.isCharacterDevice = function () { + return this._checkModeProperty(process.S_IFCHR); +}; + +process.Stats.prototype.isSymbolicLink = function () { + return this._checkModeProperty(process.S_IFLNK); +}; + +process.Stats.prototype.isFIFO = function () { + return this._checkModeProperty(process.S_IFIFO); +}; + +process.Stats.prototype.isSocket = function () { + return this._checkModeProperty(process.S_IFSOCK); +}; + + + +exports.readFile = process.fs.readFile; +exports.readFileSync = process.fs.readFileSync; + +// Used by fs.open and friends +function stringToFlags(flag) { + // Only mess with strings + if (typeof flag !== 'string') { + return flag; + } + switch (flag) { + case "r": return process.O_RDONLY; + case "r+": return process.O_RDWR; + case "w": return process.O_CREAT | process.O_TRUNC | process.O_WRONLY; + case "w+": return process.O_CREAT | process.O_TRUNC | process.O_RDWR; + case "a": return process.O_APPEND | process.O_CREAT | process.O_WRONLY; + case "a+": return process.O_APPEND | process.O_CREAT | process.O_RDWR; + default: throw new Error("Unknown file open flag: " + flag); + } +} + +function noop () {} + +// Yes, the follow could be easily DRYed up but I provide the explicit +// list to make the arguments clear. + +exports.close = function (fd, callback) { + process.fs.close(fd, callback || noop); +}; + +exports.closeSync = function (fd) { + return process.fs.close(fd); +}; + +exports.open = function (path, flags, mode, callback) { + if (mode === undefined) { mode = 0666; } + process.fs.open(path, stringToFlags(flags), mode, callback || noop); +}; + +exports.openSync = function (path, flags, mode) { + if (mode === undefined) { mode = 0666; } + return process.fs.open(path, stringToFlags(flags), mode); +}; + +exports.read = function (fd, length, position, encoding, callback) { + encoding = encoding || "binary"; + process.fs.read(fd, length, position, encoding, callback || noop); +}; + +exports.readSync = function (fd, length, position, encoding) { + encoding = encoding || "binary"; + return process.fs.read(fd, length, position, encoding); +}; + +exports.write = function (fd, data, position, encoding, callback) { + encoding = encoding || "binary"; + process.fs.write(fd, data, position, encoding, callback || noop); +}; + +exports.writeSync = function (fd, data, position, encoding) { + encoding = encoding || "binary"; + return process.fs.write(fd, data, position, encoding); +}; + +exports.rename = function (oldPath, newPath, callback) { + process.fs.rename(oldPath, newPath, callback || noop); +}; + +exports.renameSync = function (oldPath, newPath) { + return process.fs.rename(oldPath, newPath); +}; + +exports.truncate = function (fd, len, callback) { + process.fs.truncate(fd, len, callback || noop); +}; + +exports.truncateSync = function (fd, len) { + return process.fs.truncate(fd, len); +}; + +exports.rmdir = function (path, callback) { + process.fs.rmdir(path, callback || noop); +}; + +exports.rmdirSync = function (path) { + return process.fs.rmdir(path); +}; + +exports.mkdir = function (path, mode, callback) { + process.fs.mkdir(path, mode, callback || noop); +}; + +exports.mkdirSync = function (path, mode) { + return process.fs.mkdir(path, mode); +}; + +exports.sendfile = function (outFd, inFd, inOffset, length, callback) { + process.fs.sendfile(outFd, inFd, inOffset, length, callback || noop); +}; + +exports.sendfileSync = function (outFd, inFd, inOffset, length) { + return process.fs.sendfile(outFd, inFd, inOffset, length); +}; + +exports.readdir = function (path, callback) { + process.fs.readdir(path, callback || noop); +}; + +exports.readdirSync = function (path) { + return process.fs.readdir(path); +}; + +exports.lstat = function (path, callback) { + process.fs.lstat(path, callback || noop); +}; + +exports.stat = function (path, callback) { + process.fs.stat(path, callback || noop); +}; + +exports.lstatSync = function (path) { + return process.fs.lstat(path); +}; + +exports.statSync = function (path) { + return process.fs.stat(path); +}; + +exports.readlink = function (path, callback) { + process.fs.readlink(path, callback || noop); +}; + +exports.readlinkSync = function (path) { + return process.fs.readlink(path); +}; + +exports.symlink = function (destination, path, callback) { + process.fs.symlink(destination, path, callback || noop); +}; + +exports.symlinkSync = function (destination, path) { + return process.fs.symlink(destination, path); +}; + +exports.link = function (srcpath, dstpath, callback) { + process.fs.link(srcpath, dstpath, callback || noop); +}; + +exports.linkSync = function (srcpath, dstpath) { + return process.fs.link(srcpath, dstpath); +}; + +exports.unlink = function (path, callback) { + process.fs.unlink(path, callback || noop); +}; + +exports.unlinkSync = function (path) { + return process.fs.unlink(path); +}; + +exports.chmod = function (path, mode, callback) { + process.fs.chmod(path, mode, callback || noop); +}; + +exports.chmodSync = function (path, mode) { + return process.fs.chmod(path, mode); +}; + +function writeAll (fd, data, encoding, callback) { + exports.write(fd, data, 0, encoding, function (writeErr, written) { + if (writeErr) { + exports.close(fd, function () { + if (callback) callback(writeErr); + }); + } else { + if (written === data.length) { + exports.close(fd, callback); + } else { + writeAll(fd, data.slice(written), encoding, callback); + } + } + }); +} + +exports.writeFile = function (path, data, encoding_, callback) { + var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); + var callback_ = arguments[arguments.length - 1]; + var callback = (typeof(callback_) == 'function' ? callback_ : null); + exports.open(path, 'w', 0666, function (openErr, fd) { + if (openErr) { + if (callback) callback(openErr); + } else { + writeAll(fd, data, encoding, callback); + } + }); +}; + +exports.writeFileSync = function (path, data, encoding) { + encoding = encoding || "utf8"; // default to utf8 + var fd = exports.openSync(path, "w"); + var written = 0; + while (written < data.length) { + written += exports.writeSync(fd, data, 0, encoding); + data = data.slice(written); + } + exports.closeSync(fd); +}; + +exports.cat = function () { + throw new Error("fs.cat is deprecated. Please use fs.readFile instead."); +}; + + +exports.catSync = function () { + throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead."); +}; + +// Stat Change Watchers + +var statWatchers = {}; + +exports.watchFile = function (filename) { + var stat; + var options; + var listener; + + if ("object" == typeof arguments[1]) { + options = arguments[1]; + listener = arguments[2]; + } else { + options = {}; + listener = arguments[1]; + } + + if (options.persistent === undefined) options.persistent = true; + if (options.interval === undefined) options.interval = 0; + + if (filename in statWatchers) { + stat = statWatchers[filename]; + } else { + statWatchers[filename] = new process.Stat(); + stat = statWatchers[filename]; + stat.start(filename, options.persistent, options.interval); + } + stat.addListener("change", listener); + return stat; +}; + +exports.unwatchFile = function (filename) { + if (filename in statWatchers) { + stat = statWatchers[filename]; + stat.stop(); + statWatchers[filename] = undefined; + } +}; + +// Realpath + +var path = require('path'); +var dirname = path.dirname, + basename = path.basename, + normalize = path.normalize; + +function readlinkDeepSync(path, stats) { + var seen_links = {}, resolved_link, stats, file_id; + while (true) { + stats = stats || exports.lstatSync(path); + file_id = stats.dev.toString(32)+":"+stats.ino.toString(32); + if (file_id in seen_links) { + throw new Error("cyclic symbolic link at "+path); + } else { + seen_links[file_id] = 1; + if (stats.isSymbolicLink()) { + var newpath = exports.readlinkSync(path); + if (newpath.charAt(0) === '/') { + path = newpath; + } else { + var dir = dirname(path); + path = (dir !== '') ? dir + '/' + newpath : newpath; + } + } else { + return normalize(path); + } + } + stats = null; + } +} + +function readlinkDeep(path, stats, callback) { + var seen_links = {}, resolved_link, file_id; + function next(stats) { + file_id = stats.dev.toString(32)+":"+stats.ino.toString(32); + if (file_id in seen_links) { + callback(new Error("cyclic symbolic link at "+path)); + } else { + seen_links[file_id] = 1; + if (stats.isSymbolicLink()) { + exports.readlink(path, function(err, newpath) { + if (err) callback(err); + if (newpath.charAt(0) === '/') { + path = newpath; + } else { + var dir = dirname(path); + path = (dir !== '') ? dir + '/' + newpath : newpath; + } + _next(); + }); + } else { + callback(null, normalize(path)); + } + } + } + function _next() { + exports.lstat(path, function(err, stats){ + if (err) callback(err); + else next(stats); + }); + } + if (stats) next(stats); + else _next(); +} + +exports.realpathSync = function(path) { + var stats = exports.lstatSync(path); + if (stats.isSymbolicLink()) + return readlinkDeepSync(path, stats); + else + return normalize(path); +} + +exports.realpath = function(path, callback) { + var resolved_path = path; + if (!callback) return; + exports.lstat(path, function(err, stats){ + if (err) + callback(err); + else if (stats.isSymbolicLink()) + readlinkDeep(path, stats, callback); + else + callback(null, normalize(path)); + }); +} diff --git a/lib/http.js b/lib/http.js index 5e514e4be7..d9eab022ee 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,4 +1,4 @@ -var sys = require('sys'); +var sys = require('./sys'); var events = require('events'); var CRLF = "\r\n"; diff --git a/lib/sys.js b/lib/sys.js index 9c05342e48..5ad68e4926 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -95,11 +95,11 @@ exports.inspect = function (obj, showHidden, depth) { return braces[0] + base + braces[1]; } - if( recurseTimes < 0 ) { + if (recurseTimes < 0) { if (value instanceof RegExp) { return '' + value; } else { - return "[object Object]"; + return "[Object]"; } } @@ -129,10 +129,17 @@ exports.inspect = function (obj, showHidden, depth) { else { str = format(value[key], recurseTimes - 1); } - if( str.indexOf('\n') > -1 ) { - str = '\n' + str.split('\n').map(function(line) { + if (str.indexOf('\n') > -1) { + if (value instanceof Array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } + else { + str = '\n' + str.split('\n').map(function(line) { return ' ' + line; }).join('\n'); + } } } else { str = '[Circular]'; @@ -143,7 +150,7 @@ exports.inspect = function (obj, showHidden, depth) { return str; } name = JSON.stringify('' + key); - if( name.match(/^"([a-zA-Z_0-9]+)"$/) ) { + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { name = name.substr(1, name.length-2); } else { @@ -154,13 +161,17 @@ exports.inspect = function (obj, showHidden, depth) { return name + ": " + str; }); - + var numLinesEst = 0; var length = output.reduce(function(prev, cur) { + numLinesEst++; + if( cur.indexOf('\n') >= 0 ) { + numLinesEst++; + } return prev + cur.length + 1; },0); - if( length > 50 ) { - output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + '\n' +braces[1]; + if (length > 50) { + output = braces[0] + (base === '' ? '' : base + '\n,') + ' ' + output.join('\n, ') + (numLinesEst > 1 ? '\n' : ' ') + braces[1]; } else { output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; @@ -214,5 +225,12 @@ exports.exec = function (command, callback) { * prototype * @param {function} superCtor Constructor function to inherit prototype from */ -exports.inherits = process.inherits; +exports.inherits = function (ctor, superCtor) { + var tempCtor = function(){}; + tempCtor.prototype = superCtor.prototype; + ctor.super_ = superCtor; + ctor.prototype = new tempCtor(); + ctor.prototype.constructor = ctor; +}; + diff --git a/src/node.cc b/src/node.cc index 6d04cae97d..a0d42b18bc 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1250,7 +1250,13 @@ int main(int argc, char *argv[]) { evcom_ignore_sigpipe(); // Initialize the default ev loop. +#ifdef __sun + // TODO(Ryan) I'm experiencing abnormally high load using Solaris's + // EVBACKEND_PORT. Temporarally forcing select() until I debug. + ev_default_loop(EVBACKEND_SELECT); +#else ev_default_loop(EVFLAG_AUTO); +#endif ev_timer_init(&node::gc_timer, node::GCTimeout, GC_INTERVAL, GC_INTERVAL); diff --git a/src/node.js b/src/node.js index e76afa3e5b..221a5a2657 100644 --- a/src/node.js +++ b/src/node.js @@ -19,12 +19,15 @@ GLOBAL.print = removed("print() has moved. Use require('sys') to bring it back." GLOBAL.p = removed("p() has moved. Use require('sys') to bring it back."); process.debug = removed("process.debug() has moved. Use require('sys') to bring it back."); process.error = removed("process.error() has moved. Use require('sys') to bring it back."); +process.watchFile = removed("process.watchFile() has moved to fs.watchFile()"); +process.unwatchFile = removed("process.unwatchFile() has moved to fs.unwatchFile()"); GLOBAL.node = {}; node.createProcess = removed("node.createProcess() has been changed to process.createChildProcess() update your code"); node.exec = removed("process.exec() has moved. Use require('sys') to bring it back."); node.inherits = removed("node.inherits() has moved. Use require('sys') to access it."); +process.inherits = removed("process.inherits() has moved to sys.inherits."); node.http = {}; node.http.createServer = removed("node.http.createServer() has moved. Use require('http') to access it."); @@ -70,15 +73,6 @@ function createInternalModule (id, constructor) { }; -process.inherits = function (ctor, superCtor) { - var tempCtor = function(){}; - tempCtor.prototype = superCtor.prototype; - ctor.super_ = superCtor; - ctor.prototype = new tempCtor(); - ctor.prototype.constructor = ctor; -}; - - process.createChildProcess = function (file, args, env) { var child = new process.ChildProcess(); args = args || []; @@ -132,7 +126,7 @@ process.mixin = function() { if ( (source = arguments[i]) != null ) { // Extend the base object Object.getOwnPropertyNames(source).forEach(function(k){ - var d = Object.getOwnPropertyDescriptor(source, k); + var d = Object.getOwnPropertyDescriptor(source, k) || {value: source[k]}; if (d.get) { target.__defineGetter__(k, d.get); if (d.set) { @@ -246,79 +240,6 @@ process.addListener("newListener", function (event) { }); -// Stat Change Watchers - -var statWatchers = {}; - -process.watchFile = function (filename) { - var stat; - var options; - var listener; - - if ("object" == typeof arguments[1]) { - options = arguments[1]; - listener = arguments[2]; - } else { - options = {}; - listener = arguments[1]; - } - - if (options.persistent === undefined) options.persistent = true; - if (options.interval === undefined) options.interval = 0; - - if (filename in statWatchers) { - stat = statWatchers[filename]; - } else { - statWatchers[filename] = new process.Stat(); - stat = statWatchers[filename]; - stat.start(filename, options.persistent, options.interval); - } - stat.addListener("change", listener); - return stat; -}; - -process.unwatchFile = function (filename) { - if (filename in statWatchers) { - stat = statWatchers[filename]; - stat.stop(); - statWatchers[filename] = undefined; - } -}; - -process.Stats.prototype._checkModeProperty = function (property) { - return ((this.mode & property) === property); -}; - -process.Stats.prototype.isDirectory = function () { - return this._checkModeProperty(process.S_IFDIR); -}; - -process.Stats.prototype.isFile = function () { - return this._checkModeProperty(process.S_IFREG); -}; - -process.Stats.prototype.isBlockDevice = function () { - return this._checkModeProperty(process.S_IFBLK); -}; - -process.Stats.prototype.isCharacterDevice = function () { - return this._checkModeProperty(process.S_IFCHR); -}; - -process.Stats.prototype.isSymbolicLink = function () { - return this._checkModeProperty(process.S_IFLNK); -}; - -process.Stats.prototype.isFIFO = function () { - return this._checkModeProperty(process.S_IFIFO); -}; - -process.Stats.prototype.isSocket = function () { - return this._checkModeProperty(process.S_IFSOCK); -}; - - - // Timers function addTimerListener (callback) { var timer = this; @@ -371,278 +292,60 @@ function debug (x) { -var fsModule = createInternalModule("fs", function (exports) { - exports.Stats = process.Stats; - - // Used by fs.open and friends - function stringToFlags(flag) { - // Only mess with strings - if (typeof flag !== 'string') { - return flag; - } - switch (flag) { - case "r": return process.O_RDONLY; - case "r+": return process.O_RDWR; - case "w": return process.O_CREAT | process.O_TRUNC | process.O_WRONLY; - case "w+": return process.O_CREAT | process.O_TRUNC | process.O_RDWR; - case "a": return process.O_APPEND | process.O_CREAT | process.O_WRONLY; - case "a+": return process.O_APPEND | process.O_CREAT | process.O_RDWR; - default: throw new Error("Unknown file open flag: " + flag); - } - } - - function noop () {} - // Yes, the follow could be easily DRYed up but I provide the explicit - // list to make the arguments clear. - - exports.close = function (fd, callback) { - process.fs.close(fd, callback || noop); - }; - - exports.closeSync = function (fd) { - return process.fs.close(fd); - }; - - exports.open = function (path, flags, mode, callback) { - if (mode === undefined) { mode = 0666; } - process.fs.open(path, stringToFlags(flags), mode, callback || noop); - }; - - exports.openSync = function (path, flags, mode) { - if (mode === undefined) { mode = 0666; } - return process.fs.open(path, stringToFlags(flags), mode); - }; - - exports.read = function (fd, length, position, encoding, callback) { - encoding = encoding || "binary"; - process.fs.read(fd, length, position, encoding, callback || noop); - }; - - exports.readSync = function (fd, length, position, encoding) { - encoding = encoding || "binary"; - return process.fs.read(fd, length, position, encoding); - }; - - exports.write = function (fd, data, position, encoding, callback) { - encoding = encoding || "binary"; - process.fs.write(fd, data, position, encoding, callback || noop); - }; - - exports.writeSync = function (fd, data, position, encoding) { - encoding = encoding || "binary"; - return process.fs.write(fd, data, position, encoding); - }; - - exports.rename = function (oldPath, newPath, callback) { - process.fs.rename(oldPath, newPath, callback || noop); - }; - - exports.renameSync = function (oldPath, newPath) { - return process.fs.rename(oldPath, newPath); - }; - - exports.truncate = function (fd, len, callback) { - process.fs.truncate(fd, len, callback || noop); - }; - - exports.truncateSync = function (fd, len) { - return process.fs.truncate(fd, len); - }; - - exports.rmdir = function (path, callback) { - process.fs.rmdir(path, callback || noop); - }; - - exports.rmdirSync = function (path) { - return process.fs.rmdir(path); - }; - - exports.mkdir = function (path, mode, callback) { - process.fs.mkdir(path, mode, callback || noop); - }; - - exports.mkdirSync = function (path, mode) { - return process.fs.mkdir(path, mode); - }; - - exports.sendfile = function (outFd, inFd, inOffset, length, callback) { - process.fs.sendfile(outFd, inFd, inOffset, length, callback || noop); - }; - - exports.sendfileSync = function (outFd, inFd, inOffset, length) { - return process.fs.sendfile(outFd, inFd, inOffset, length); - }; - - exports.readdir = function (path, callback) { - process.fs.readdir(path, callback || noop); - }; - - exports.readdirSync = function (path) { - return process.fs.readdir(path); - }; - - exports.lstat = function (path, callback) { - process.fs.lstat(path, callback || noop); - }; - - exports.stat = function (path, callback) { - process.fs.stat(path, callback || noop); - }; - - exports.lstatSync = function (path) { - return process.fs.lstat(path); - }; - - exports.statSync = function (path) { - return process.fs.stat(path); - }; - - exports.readlink = function (path, callback) { - process.fs.readlink(path, callback || noop); - }; - - exports.readlinkSync = function (path) { - return process.fs.readlink(path); - }; - - exports.symlink = function (destination, path, callback) { - process.fs.symlink(destination, path, callback || noop); - }; - - exports.symlinkSync = function (destination, path) { - return process.fs.symlink(destination, path); - }; - - exports.link = function (srcpath, dstpath, callback) { - process.fs.link(srcpath, dstpath, callback || noop); - }; - - exports.linkSync = function (srcpath, dstpath) { - return process.fs.link(srcpath, dstpath); - }; - - exports.unlink = function (path, callback) { - process.fs.unlink(path, callback || noop); - }; - - exports.unlinkSync = function (path) { - return process.fs.unlink(path); - }; - - exports.chmod = function (path, mode, callback) { - process.fs.chmod(path, mode, callback || noop); - }; - - exports.chmodSync = function (path, mode) { - return process.fs.chmod(path, mode); - }; - - function writeAll (fd, data, encoding, callback) { - exports.write(fd, data, 0, encoding, function (writeErr, written) { - if (writeErr) { - exports.close(fd, function () { - if (callback) callback(writeErr); - }); - } else { - if (written === data.length) { - exports.close(fd, callback); - } else { - writeAll(fd, data.slice(written), encoding, callback); - } - } - }); - } - - exports.writeFile = function (path, data, encoding_, callback) { - var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); - var callback_ = arguments[arguments.length - 1]; - var callback = (typeof(callback_) == 'function' ? callback_ : null); - exports.open(path, 'w', 0666, function (openErr, fd) { - if (openErr) { - if (callback) callback(openErr); - } else { - writeAll(fd, data, encoding, callback); - } - }); - }; - - exports.writeFileSync = function (path, data, encoding) { - encoding = encoding || "utf8"; // default to utf8 - var fd = exports.openSync(path, "w"); - var written = 0; - while (written < data.length) { - written += exports.writeSync(fd, data, 0, encoding); - data = data.slice(written); +function readAll (fd, pos, content, encoding, callback) { + process.fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) { + if (err) { + if (callback) callback(err); + } else if (chunk) { + content += chunk; + pos += bytesRead; + readAll(fd, pos, content, encoding, callback); + } else { + process.fs.close(fd, function (err) { + if (callback) callback(err, content); + }); } - exports.closeSync(fd); - }; - - exports.cat = function () { - throw new Error("fs.cat is deprecated. Please use fs.readFile instead."); - }; - - function readAll (fd, pos, content, encoding, callback) { - exports.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) { - if (err) { - if (callback) callback(err); - } else if (chunk) { - content += chunk; - pos += bytesRead; - readAll(fd, pos, content, encoding, callback); - } else { - process.fs.close(fd, function (err) { - if (callback) callback(err, content); - }); - } - }); - } - - exports.readFile = function (path, encoding_, callback) { - var encoding = typeof(encoding_) == 'string' ? encoding : 'utf8'; - var callback_ = arguments[arguments.length - 1]; - var callback = (typeof(callback_) == 'function' ? callback_ : null); - exports.open(path, 'r', 0666, function (err, fd) { - if (err) { - if (callback) callback(err); - } else { - readAll(fd, 0, "", encoding, callback); - } - }); - }; - - exports.catSync = function () { - throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead."); - }; - - exports.readFileSync = function (path, encoding) { - encoding = encoding || "utf8"; // default to utf8 + }); +} - debug('readFileSync open'); +process.fs.readFile = function (path, encoding_, callback) { + var encoding = typeof(encoding_) == 'string' ? encoding : 'utf8'; + var callback_ = arguments[arguments.length - 1]; + var callback = (typeof(callback_) == 'function' ? callback_ : null); + process.fs.open(path, process.O_RDONLY, 0666, function (err, fd) { + if (err) { + if (callback) callback(err); + } else { + readAll(fd, 0, "", encoding, callback); + } + }); +}; - var fd = exports.openSync(path, "r"); - var content = ''; - var pos = 0; - var r; +process.fs.readFileSync = function (path, encoding) { + encoding = encoding || "utf8"; // default to utf8 - while ((r = exports.readSync(fd, 4*1024, pos, encoding)) && r[0]) { - debug('readFileSync read ' + r[1]); - content += r[0]; - pos += r[1] - } + debug('readFileSync open'); - debug('readFileSync close'); + var fd = process.fs.open(path, process.O_RDONLY, 0666); + var content = ''; + var pos = 0; + var r; - exports.closeSync(fd); + while ((r = process.fs.read(fd, 4*1024, pos, encoding)) && r[0]) { + debug('readFileSync read ' + r[1]); + content += r[0]; + pos += r[1] + } - debug('readFileSync done'); + debug('readFileSync close'); - return content; - }; -}); + process.fs.close(fd); -var fs = fsModule.exports; + debug('readFileSync done'); + return content; +}; var pathModule = createInternalModule("path", function (exports) { exports.join = function () { @@ -703,7 +406,7 @@ var pathModule = createInternalModule("path", function (exports) { }; exports.exists = function (path, callback) { - fs.stat(path, function (err, stats) { + process.fs.stat(path, function (err, stats) { if (callback) callback(err ? false : true); }); }; @@ -713,7 +416,7 @@ var path = pathModule.exports; function existsSync (path) { try { - fs.statSync(path); + process.fs.stat(path); return true; } catch (e) { return false; @@ -932,7 +635,7 @@ function cat (id, callback) { } }); } else { - fs.readFile(id, callback); + process.fs.readFile(id, callback); } } @@ -971,7 +674,7 @@ Module.prototype._loadContent = function (content, filename) { Module.prototype._loadScriptSync = function (filename) { - var content = fs.readFileSync(filename); + var content = process.fs.readFileSync(filename); // remove shebang content = content.replace(/^\#\!.*/, ''); diff --git a/src/node_file.cc b/src/node_file.cc index 5b3238c4e6..4efdc06a28 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -266,7 +266,7 @@ static Handle