From 810882c1ad15c6eb6bc0fbe6225f58a01989c290 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 1 Mar 2010 10:14:49 -0800 Subject: [PATCH] Move 'fs' module out of src/node.js into its own file --- lib/fs.js | 252 ++++++++++++++++++++++++++++++++++++++++++++ src/node.js | 345 +++++++++--------------------------------------------------- 2 files changed, 299 insertions(+), 298 deletions(-) create mode 100644 lib/fs.js diff --git a/lib/fs.js b/lib/fs.js new file mode 100644 index 0000000..afa6623 --- /dev/null +++ b/lib/fs.js @@ -0,0 +1,252 @@ +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."); +}; + diff --git a/src/node.js b/src/node.js index e76afa3..a764d02 100644 --- a/src/node.js +++ b/src/node.js @@ -285,39 +285,6 @@ process.unwatchFile = function (filename) { } }; -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) { @@ -371,278 +338,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 +452,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 +462,7 @@ var path = pathModule.exports; function existsSync (path) { try { - fs.statSync(path); + process.fs.stat(path); return true; } catch (e) { return false; @@ -932,7 +681,7 @@ function cat (id, callback) { } }); } else { - fs.readFile(id, callback); + process.fs.readFile(id, callback); } } @@ -971,7 +720,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(/^\#\!.*/, ''); -- 2.7.4