|
@ -1,45 +1,45 @@ |
|
|
var sys = require('sys'), |
|
|
var sys = require('sys'), |
|
|
events = require('events'); |
|
|
events = require('events'); |
|
|
|
|
|
|
|
|
var fs = exports; |
|
|
var fs = process.binding('fs'); |
|
|
|
|
|
|
|
|
exports.Stats = process.Stats; |
|
|
exports.Stats = fs.Stats; |
|
|
|
|
|
|
|
|
process.Stats.prototype._checkModeProperty = function (property) { |
|
|
fs.Stats.prototype._checkModeProperty = function (property) { |
|
|
return ((this.mode & property) === property); |
|
|
return ((this.mode & property) === property); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isDirectory = function () { |
|
|
fs.Stats.prototype.isDirectory = function () { |
|
|
return this._checkModeProperty(process.S_IFDIR); |
|
|
return this._checkModeProperty(process.S_IFDIR); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isFile = function () { |
|
|
fs.Stats.prototype.isFile = function () { |
|
|
return this._checkModeProperty(process.S_IFREG); |
|
|
return this._checkModeProperty(process.S_IFREG); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isBlockDevice = function () { |
|
|
fs.Stats.prototype.isBlockDevice = function () { |
|
|
return this._checkModeProperty(process.S_IFBLK); |
|
|
return this._checkModeProperty(process.S_IFBLK); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isCharacterDevice = function () { |
|
|
fs.Stats.prototype.isCharacterDevice = function () { |
|
|
return this._checkModeProperty(process.S_IFCHR); |
|
|
return this._checkModeProperty(process.S_IFCHR); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isSymbolicLink = function () { |
|
|
fs.Stats.prototype.isSymbolicLink = function () { |
|
|
return this._checkModeProperty(process.S_IFLNK); |
|
|
return this._checkModeProperty(process.S_IFLNK); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isFIFO = function () { |
|
|
fs.Stats.prototype.isFIFO = function () { |
|
|
return this._checkModeProperty(process.S_IFIFO); |
|
|
return this._checkModeProperty(process.S_IFIFO); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
process.Stats.prototype.isSocket = function () { |
|
|
fs.Stats.prototype.isSocket = function () { |
|
|
return this._checkModeProperty(process.S_IFSOCK); |
|
|
return this._checkModeProperty(process.S_IFSOCK); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function readAll (fd, pos, content, encoding, callback) { |
|
|
function readAll (fd, pos, content, encoding, callback) { |
|
|
process.fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) { |
|
|
fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
if (callback) callback(err); |
|
|
if (callback) callback(err); |
|
|
} else if (chunk) { |
|
|
} else if (chunk) { |
|
@ -47,7 +47,7 @@ function readAll (fd, pos, content, encoding, callback) { |
|
|
pos += bytesRead; |
|
|
pos += bytesRead; |
|
|
readAll(fd, pos, content, encoding, callback); |
|
|
readAll(fd, pos, content, encoding, callback); |
|
|
} else { |
|
|
} else { |
|
|
process.fs.close(fd, function (err) { |
|
|
fs.close(fd, function (err) { |
|
|
if (callback) callback(err, content); |
|
|
if (callback) callback(err, content); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -58,7 +58,7 @@ exports.readFile = function (path, encoding_, callback) { |
|
|
var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8'; |
|
|
var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8'; |
|
|
var callback_ = arguments[arguments.length - 1]; |
|
|
var callback_ = arguments[arguments.length - 1]; |
|
|
var callback = (typeof(callback_) == 'function' ? callback_ : null); |
|
|
var callback = (typeof(callback_) == 'function' ? callback_ : null); |
|
|
process.fs.open(path, process.O_RDONLY, 0666, function (err, fd) { |
|
|
fs.open(path, process.O_RDONLY, 0666, function (err, fd) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
if (callback) callback(err); |
|
|
if (callback) callback(err); |
|
|
} else { |
|
|
} else { |
|
@ -70,17 +70,17 @@ exports.readFile = function (path, encoding_, callback) { |
|
|
exports.readFileSync = function (path, encoding) { |
|
|
exports.readFileSync = function (path, encoding) { |
|
|
encoding = encoding || "utf8"; // default to utf8
|
|
|
encoding = encoding || "utf8"; // default to utf8
|
|
|
|
|
|
|
|
|
var fd = process.fs.open(path, process.O_RDONLY, 0666); |
|
|
var fd = fs.open(path, process.O_RDONLY, 0666); |
|
|
var content = ''; |
|
|
var content = ''; |
|
|
var pos = 0; |
|
|
var pos = 0; |
|
|
var r; |
|
|
var r; |
|
|
|
|
|
|
|
|
while ((r = process.fs.read(fd, 4*1024, pos, encoding)) && r[0]) { |
|
|
while ((r = fs.read(fd, 4*1024, pos, encoding)) && r[0]) { |
|
|
content += r[0]; |
|
|
content += r[0]; |
|
|
pos += r[1] |
|
|
pos += r[1] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
process.fs.close(fd); |
|
|
fs.close(fd); |
|
|
|
|
|
|
|
|
return content; |
|
|
return content; |
|
|
}; |
|
|
}; |
|
@ -109,145 +109,145 @@ function noop () {} |
|
|
// list to make the arguments clear.
|
|
|
// list to make the arguments clear.
|
|
|
|
|
|
|
|
|
exports.close = function (fd, callback) { |
|
|
exports.close = function (fd, callback) { |
|
|
process.fs.close(fd, callback || noop); |
|
|
fs.close(fd, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.closeSync = function (fd) { |
|
|
exports.closeSync = function (fd) { |
|
|
return process.fs.close(fd); |
|
|
return fs.close(fd); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.open = function (path, flags, mode, callback) { |
|
|
exports.open = function (path, flags, mode, callback) { |
|
|
if (mode === undefined) { mode = 0666; } |
|
|
if (mode === undefined) { mode = 0666; } |
|
|
process.fs.open(path, stringToFlags(flags), mode, callback || noop); |
|
|
fs.open(path, stringToFlags(flags), mode, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.openSync = function (path, flags, mode) { |
|
|
exports.openSync = function (path, flags, mode) { |
|
|
if (mode === undefined) { mode = 0666; } |
|
|
if (mode === undefined) { mode = 0666; } |
|
|
return process.fs.open(path, stringToFlags(flags), mode); |
|
|
return fs.open(path, stringToFlags(flags), mode); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.read = function (fd, length, position, encoding, callback) { |
|
|
exports.read = function (fd, length, position, encoding, callback) { |
|
|
encoding = encoding || "binary"; |
|
|
encoding = encoding || "binary"; |
|
|
process.fs.read(fd, length, position, encoding, callback || noop); |
|
|
fs.read(fd, length, position, encoding, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.readSync = function (fd, length, position, encoding) { |
|
|
exports.readSync = function (fd, length, position, encoding) { |
|
|
encoding = encoding || "binary"; |
|
|
encoding = encoding || "binary"; |
|
|
return process.fs.read(fd, length, position, encoding); |
|
|
return fs.read(fd, length, position, encoding); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.write = function (fd, data, position, encoding, callback) { |
|
|
exports.write = function (fd, data, position, encoding, callback) { |
|
|
encoding = encoding || "binary"; |
|
|
encoding = encoding || "binary"; |
|
|
process.fs.write(fd, data, position, encoding, callback || noop); |
|
|
fs.write(fd, data, position, encoding, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.writeSync = function (fd, data, position, encoding) { |
|
|
exports.writeSync = function (fd, data, position, encoding) { |
|
|
encoding = encoding || "binary"; |
|
|
encoding = encoding || "binary"; |
|
|
return process.fs.write(fd, data, position, encoding); |
|
|
return fs.write(fd, data, position, encoding); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.rename = function (oldPath, newPath, callback) { |
|
|
exports.rename = function (oldPath, newPath, callback) { |
|
|
process.fs.rename(oldPath, newPath, callback || noop); |
|
|
fs.rename(oldPath, newPath, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.renameSync = function (oldPath, newPath) { |
|
|
exports.renameSync = function (oldPath, newPath) { |
|
|
return process.fs.rename(oldPath, newPath); |
|
|
return fs.rename(oldPath, newPath); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.truncate = function (fd, len, callback) { |
|
|
exports.truncate = function (fd, len, callback) { |
|
|
process.fs.truncate(fd, len, callback || noop); |
|
|
fs.truncate(fd, len, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.truncateSync = function (fd, len) { |
|
|
exports.truncateSync = function (fd, len) { |
|
|
return process.fs.truncate(fd, len); |
|
|
return fs.truncate(fd, len); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.rmdir = function (path, callback) { |
|
|
exports.rmdir = function (path, callback) { |
|
|
process.fs.rmdir(path, callback || noop); |
|
|
fs.rmdir(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.rmdirSync = function (path) { |
|
|
exports.rmdirSync = function (path) { |
|
|
return process.fs.rmdir(path); |
|
|
return fs.rmdir(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.mkdir = function (path, mode, callback) { |
|
|
exports.mkdir = function (path, mode, callback) { |
|
|
process.fs.mkdir(path, mode, callback || noop); |
|
|
fs.mkdir(path, mode, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.mkdirSync = function (path, mode) { |
|
|
exports.mkdirSync = function (path, mode) { |
|
|
return process.fs.mkdir(path, mode); |
|
|
return fs.mkdir(path, mode); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.sendfile = function (outFd, inFd, inOffset, length, callback) { |
|
|
exports.sendfile = function (outFd, inFd, inOffset, length, callback) { |
|
|
process.fs.sendfile(outFd, inFd, inOffset, length, callback || noop); |
|
|
fs.sendfile(outFd, inFd, inOffset, length, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.sendfileSync = function (outFd, inFd, inOffset, length) { |
|
|
exports.sendfileSync = function (outFd, inFd, inOffset, length) { |
|
|
return process.fs.sendfile(outFd, inFd, inOffset, length); |
|
|
return fs.sendfile(outFd, inFd, inOffset, length); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.readdir = function (path, callback) { |
|
|
exports.readdir = function (path, callback) { |
|
|
process.fs.readdir(path, callback || noop); |
|
|
fs.readdir(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.readdirSync = function (path) { |
|
|
exports.readdirSync = function (path) { |
|
|
return process.fs.readdir(path); |
|
|
return fs.readdir(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.lstat = function (path, callback) { |
|
|
exports.lstat = function (path, callback) { |
|
|
process.fs.lstat(path, callback || noop); |
|
|
fs.lstat(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.stat = function (path, callback) { |
|
|
exports.stat = function (path, callback) { |
|
|
process.fs.stat(path, callback || noop); |
|
|
fs.stat(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.lstatSync = function (path) { |
|
|
exports.lstatSync = function (path) { |
|
|
return process.fs.lstat(path); |
|
|
return fs.lstat(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.statSync = function (path) { |
|
|
exports.statSync = function (path) { |
|
|
return process.fs.stat(path); |
|
|
return fs.stat(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.readlink = function (path, callback) { |
|
|
exports.readlink = function (path, callback) { |
|
|
process.fs.readlink(path, callback || noop); |
|
|
fs.readlink(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.readlinkSync = function (path) { |
|
|
exports.readlinkSync = function (path) { |
|
|
return process.fs.readlink(path); |
|
|
return fs.readlink(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.symlink = function (destination, path, callback) { |
|
|
exports.symlink = function (destination, path, callback) { |
|
|
process.fs.symlink(destination, path, callback || noop); |
|
|
fs.symlink(destination, path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.symlinkSync = function (destination, path) { |
|
|
exports.symlinkSync = function (destination, path) { |
|
|
return process.fs.symlink(destination, path); |
|
|
return fs.symlink(destination, path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.link = function (srcpath, dstpath, callback) { |
|
|
exports.link = function (srcpath, dstpath, callback) { |
|
|
process.fs.link(srcpath, dstpath, callback || noop); |
|
|
fs.link(srcpath, dstpath, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.linkSync = function (srcpath, dstpath) { |
|
|
exports.linkSync = function (srcpath, dstpath) { |
|
|
return process.fs.link(srcpath, dstpath); |
|
|
return fs.link(srcpath, dstpath); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.unlink = function (path, callback) { |
|
|
exports.unlink = function (path, callback) { |
|
|
process.fs.unlink(path, callback || noop); |
|
|
fs.unlink(path, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.unlinkSync = function (path) { |
|
|
exports.unlinkSync = function (path) { |
|
|
return process.fs.unlink(path); |
|
|
return fs.unlink(path); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.chmod = function (path, mode, callback) { |
|
|
exports.chmod = function (path, mode, callback) { |
|
|
process.fs.chmod(path, mode, callback || noop); |
|
|
fs.chmod(path, mode, callback || noop); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
exports.chmodSync = function (path, mode) { |
|
|
exports.chmodSync = function (path, mode) { |
|
|
return process.fs.chmod(path, mode); |
|
|
return fs.chmod(path, mode); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function writeAll (fd, data, encoding, callback) { |
|
|
function writeAll (fd, data, encoding, callback) { |
|
@ -322,7 +322,7 @@ exports.watchFile = function (filename) { |
|
|
if (filename in statWatchers) { |
|
|
if (filename in statWatchers) { |
|
|
stat = statWatchers[filename]; |
|
|
stat = statWatchers[filename]; |
|
|
} else { |
|
|
} else { |
|
|
statWatchers[filename] = new process.Stat(); |
|
|
statWatchers[filename] = new fs.StatWatcher(); |
|
|
stat = statWatchers[filename]; |
|
|
stat = statWatchers[filename]; |
|
|
stat.start(filename, options.persistent, options.interval); |
|
|
stat.start(filename, options.persistent, options.interval); |
|
|
} |
|
|
} |
|
@ -500,7 +500,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) { |
|
|
exports.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
self.emit('error', err); |
|
|
self.emit('error', err); |
|
|
self.readable = false; |
|
|
self.readable = false; |
|
@ -529,7 +529,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fs.open(this.path, this.flags, this.mode, function(err, fd) { |
|
|
exports.open(this.path, this.flags, this.mode, function(err, fd) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
self.emit('error', err); |
|
|
self.emit('error', err); |
|
|
self.readable = false; |
|
|
self.readable = false; |
|
@ -545,7 +545,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) { |
|
|
this.readable = false; |
|
|
this.readable = false; |
|
|
|
|
|
|
|
|
function close() { |
|
|
function close() { |
|
|
fs.close(self.fd, function(err) { |
|
|
exports.close(self.fd, function(err) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
if (cb) { |
|
|
if (cb) { |
|
|
cb(err); |
|
|
cb(err); |
|
@ -608,7 +608,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { |
|
|
queue = [], |
|
|
queue = [], |
|
|
busy = false; |
|
|
busy = false; |
|
|
|
|
|
|
|
|
queue.push([fs.open, this.path, this.flags, this.mode, undefined]); |
|
|
queue.push([exports.open, this.path, this.flags, this.mode, undefined]); |
|
|
|
|
|
|
|
|
function flush() { |
|
|
function flush() { |
|
|
if (busy) { |
|
|
if (busy) { |
|
@ -639,7 +639,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// stop flushing after close
|
|
|
// stop flushing after close
|
|
|
if (method === fs.close) { |
|
|
if (method === exports.close) { |
|
|
if (cb) { |
|
|
if (cb) { |
|
|
cb(null); |
|
|
cb(null); |
|
|
} |
|
|
} |
|
@ -648,7 +648,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// save reference for file pointer
|
|
|
// save reference for file pointer
|
|
|
if (method === fs.open) { |
|
|
if (method === exports.open) { |
|
|
self.fd = arguments[1]; |
|
|
self.fd = arguments[1]; |
|
|
self.emit('open', self.fd); |
|
|
self.emit('open', self.fd); |
|
|
} else if (cb) { |
|
|
} else if (cb) { |
|
@ -660,11 +660,11 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Inject the file pointer
|
|
|
// Inject the file pointer
|
|
|
if (method !== fs.open) { |
|
|
if (method !== exports.open) { |
|
|
args.unshift(self.fd); |
|
|
args.unshift(self.fd); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
method.apply(null, args); |
|
|
method.apply(this, args); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.write = function(data, cb) { |
|
|
this.write = function(data, cb) { |
|
@ -672,20 +672,20 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) { |
|
|
throw new Error('stream not writeable'); |
|
|
throw new Error('stream not writeable'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
queue.push([fs.write, data, undefined, this.encoding, cb]); |
|
|
queue.push([exports.write, data, undefined, this.encoding, cb]); |
|
|
flush(); |
|
|
flush(); |
|
|
return false; |
|
|
return false; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.close = function(cb) { |
|
|
this.close = function(cb) { |
|
|
this.writeable = false; |
|
|
this.writeable = false; |
|
|
queue.push([fs.close, cb]); |
|
|
queue.push([exports.close, cb]); |
|
|
flush(); |
|
|
flush(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.forceClose = function(cb) { |
|
|
this.forceClose = function(cb) { |
|
|
this.writeable = false; |
|
|
this.writeable = false; |
|
|
fs.close(self.fd, function(err) { |
|
|
exports.close(self.fd, function(err) { |
|
|
if (err) { |
|
|
if (err) { |
|
|
if (cb) { |
|
|
if (cb) { |
|
|
cb(err); |
|
|
cb(err); |
|
|