|
|
@ -81,14 +81,14 @@ function rethrow() { |
|
|
|
} |
|
|
|
|
|
|
|
function maybeCallback(cb) { |
|
|
|
return typeof cb === 'function' ? cb : rethrow(); |
|
|
|
return IS_FUNCTION(cb) ? cb : rethrow(); |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure that callbacks run in the global context. Only use this function
|
|
|
|
// for callbacks that are passed to the binding layer, callbacks that are
|
|
|
|
// invoked from JS already run in the proper scope.
|
|
|
|
function makeCallback(cb) { |
|
|
|
if (typeof cb !== 'function') { |
|
|
|
if (!IS_FUNCTION(cb)) { |
|
|
|
return rethrow(); |
|
|
|
} |
|
|
|
|
|
|
@ -171,13 +171,13 @@ fs.existsSync = function(path) { |
|
|
|
fs.readFile = function(path, options, callback_) { |
|
|
|
var callback = maybeCallback(arguments[arguments.length - 1]); |
|
|
|
|
|
|
|
if (typeof options === 'function' || !options) { |
|
|
|
if (IS_FUNCTION(options) || !options) { |
|
|
|
options = { encoding: null, flag: 'r' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, flag: 'r' }; |
|
|
|
} else if (!options) { |
|
|
|
options = { encoding: null, flag: 'r' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
|
|
|
@ -260,9 +260,9 @@ fs.readFile = function(path, options, callback_) { |
|
|
|
fs.readFileSync = function(path, options) { |
|
|
|
if (!options) { |
|
|
|
options = { encoding: null, flag: 'r' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, flag: 'r' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
|
|
|
@ -332,7 +332,7 @@ fs.readFileSync = function(path, options) { |
|
|
|
// Used by binding.open and friends
|
|
|
|
function stringToFlags(flag) { |
|
|
|
// Only mess with strings
|
|
|
|
if (typeof flag !== 'string') { |
|
|
|
if (!IS_STRING(flag)) { |
|
|
|
return flag; |
|
|
|
} |
|
|
|
|
|
|
@ -381,16 +381,13 @@ fs.closeSync = function(fd) { |
|
|
|
}; |
|
|
|
|
|
|
|
function modeNum(m, def) { |
|
|
|
switch (typeof m) { |
|
|
|
case 'number': return m; |
|
|
|
case 'string': return parseInt(m, 8); |
|
|
|
default: |
|
|
|
if (def) { |
|
|
|
return modeNum(def); |
|
|
|
} else { |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
} |
|
|
|
if (IS_NUMBER(m)) |
|
|
|
return m; |
|
|
|
if (IS_STRING(m)) |
|
|
|
return parseInt(m, 8); |
|
|
|
if (def) |
|
|
|
return modeNum(def); |
|
|
|
return undefined; |
|
|
|
} |
|
|
|
|
|
|
|
fs.open = function(path, flags, mode, callback) { |
|
|
@ -411,7 +408,7 @@ fs.openSync = function(path, flags, mode) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.read = function(fd, buffer, offset, length, position, callback) { |
|
|
|
if (!Buffer.isBuffer(buffer)) { |
|
|
|
if (!IS_BUFFER(buffer)) { |
|
|
|
// legacy string interface (fd, length, position, encoding, callback)
|
|
|
|
var cb = arguments[4], |
|
|
|
encoding = arguments[3]; |
|
|
@ -442,7 +439,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) { |
|
|
|
|
|
|
|
fs.readSync = function(fd, buffer, offset, length, position) { |
|
|
|
var legacy = false; |
|
|
|
if (!Buffer.isBuffer(buffer)) { |
|
|
|
if (!IS_BUFFER(buffer)) { |
|
|
|
// legacy string interface (fd, length, position, encoding, callback)
|
|
|
|
legacy = true; |
|
|
|
var encoding = arguments[3]; |
|
|
@ -466,7 +463,7 @@ fs.readSync = function(fd, buffer, offset, length, position) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.write = function(fd, buffer, offset, length, position, callback) { |
|
|
|
if (!Buffer.isBuffer(buffer)) { |
|
|
|
if (!IS_BUFFER(buffer)) { |
|
|
|
// legacy string interface (fd, data, position, encoding, callback)
|
|
|
|
callback = arguments[4]; |
|
|
|
position = arguments[2]; |
|
|
@ -478,7 +475,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { |
|
|
|
} |
|
|
|
|
|
|
|
if (!length) { |
|
|
|
if (typeof callback == 'function') { |
|
|
|
if (IS_FUNCTION(callback)) { |
|
|
|
process.nextTick(function() { |
|
|
|
callback(undefined, 0); |
|
|
|
}); |
|
|
@ -497,7 +494,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.writeSync = function(fd, buffer, offset, length, position) { |
|
|
|
if (!Buffer.isBuffer(buffer)) { |
|
|
|
if (!IS_BUFFER(buffer)) { |
|
|
|
// legacy string interface (fd, data, position, encoding)
|
|
|
|
position = arguments[2]; |
|
|
|
assertEncoding(arguments[3]); |
|
|
@ -528,14 +525,14 @@ fs.renameSync = function(oldPath, newPath) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.truncate = function(path, len, callback) { |
|
|
|
if (typeof path === 'number') { |
|
|
|
if (IS_NUMBER(path)) { |
|
|
|
// legacy
|
|
|
|
return fs.ftruncate(path, len, callback); |
|
|
|
} |
|
|
|
if (typeof len === 'function') { |
|
|
|
if (IS_FUNCTION(len)) { |
|
|
|
callback = len; |
|
|
|
len = 0; |
|
|
|
} else if (typeof len === 'undefined') { |
|
|
|
} else if (IS_UNDEFINED(len)) { |
|
|
|
len = 0; |
|
|
|
} |
|
|
|
callback = maybeCallback(callback); |
|
|
@ -550,11 +547,11 @@ fs.truncate = function(path, len, callback) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.truncateSync = function(path, len) { |
|
|
|
if (typeof path === 'number') { |
|
|
|
if (IS_NUMBER(path)) { |
|
|
|
// legacy
|
|
|
|
return fs.ftruncateSync(path, len); |
|
|
|
} |
|
|
|
if (typeof len === 'undefined') { |
|
|
|
if (IS_UNDEFINED(len)) { |
|
|
|
len = 0; |
|
|
|
} |
|
|
|
// allow error to be thrown, but still close fd.
|
|
|
@ -568,17 +565,17 @@ fs.truncateSync = function(path, len) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.ftruncate = function(fd, len, callback) { |
|
|
|
if (typeof len === 'function') { |
|
|
|
if (IS_FUNCTION(len)) { |
|
|
|
callback = len; |
|
|
|
len = 0; |
|
|
|
} else if (typeof len === 'undefined') { |
|
|
|
} else if (IS_UNDEFINED(len)) { |
|
|
|
len = 0; |
|
|
|
} |
|
|
|
binding.ftruncate(fd, len, makeCallback(callback)); |
|
|
|
}; |
|
|
|
|
|
|
|
fs.ftruncateSync = function(fd, len) { |
|
|
|
if (typeof len === 'undefined') { |
|
|
|
if (IS_UNDEFINED(len)) { |
|
|
|
len = 0; |
|
|
|
} |
|
|
|
return binding.ftruncate(fd, len); |
|
|
@ -612,7 +609,7 @@ fs.fsyncSync = function(fd) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.mkdir = function(path, mode, callback) { |
|
|
|
if (typeof mode === 'function') callback = mode; |
|
|
|
if (IS_FUNCTION(mode)) callback = mode; |
|
|
|
callback = makeCallback(callback); |
|
|
|
if (!nullCheck(path, callback)) return; |
|
|
|
binding.mkdir(pathModule._makeLong(path), |
|
|
@ -692,7 +689,7 @@ function preprocessSymlinkDestination(path, type) { |
|
|
|
} |
|
|
|
|
|
|
|
fs.symlink = function(destination, path, type_, callback) { |
|
|
|
var type = (typeof type_ === 'string' ? type_ : null); |
|
|
|
var type = (IS_STRING(type_) ? type_ : null); |
|
|
|
var callback = makeCallback(arguments[arguments.length - 1]); |
|
|
|
|
|
|
|
if (!nullCheck(destination, callback)) return; |
|
|
@ -705,7 +702,7 @@ fs.symlink = function(destination, path, type_, callback) { |
|
|
|
}; |
|
|
|
|
|
|
|
fs.symlinkSync = function(destination, path, type) { |
|
|
|
type = (typeof type === 'string' ? type : null); |
|
|
|
type = (IS_STRING(type) ? type : null); |
|
|
|
|
|
|
|
nullCheck(destination); |
|
|
|
nullCheck(path); |
|
|
@ -843,10 +840,10 @@ fs.chownSync = function(path, uid, gid) { |
|
|
|
|
|
|
|
// converts Date or number to a fractional UNIX timestamp
|
|
|
|
function toUnixTimestamp(time) { |
|
|
|
if (typeof time == 'number') { |
|
|
|
if (IS_NUMBER(time)) { |
|
|
|
return time; |
|
|
|
} |
|
|
|
if (time instanceof Date) { |
|
|
|
if (IS_DATE(time)) { |
|
|
|
// convert to 123.456 UNIX timestamp
|
|
|
|
return time.getTime() / 1000; |
|
|
|
} |
|
|
@ -909,13 +906,13 @@ function writeAll(fd, buffer, offset, length, position, callback) { |
|
|
|
fs.writeFile = function(path, data, options, callback) { |
|
|
|
var callback = maybeCallback(arguments[arguments.length - 1]); |
|
|
|
|
|
|
|
if (typeof options === 'function' || !options) { |
|
|
|
if (IS_FUNCTION(options) || !options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, mode: 438, flag: 'w' }; |
|
|
|
} else if (!options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
|
|
|
@ -926,7 +923,7 @@ fs.writeFile = function(path, data, options, callback) { |
|
|
|
if (openErr) { |
|
|
|
if (callback) callback(openErr); |
|
|
|
} else { |
|
|
|
var buffer = Buffer.isBuffer(data) ? data : new Buffer('' + data, |
|
|
|
var buffer = IS_BUFFER(data) ? data : new Buffer('' + data, |
|
|
|
options.encoding || 'utf8'); |
|
|
|
var position = /a/.test(flag) ? null : 0; |
|
|
|
writeAll(fd, buffer, 0, buffer.length, position, callback); |
|
|
@ -937,9 +934,9 @@ fs.writeFile = function(path, data, options, callback) { |
|
|
|
fs.writeFileSync = function(path, data, options) { |
|
|
|
if (!options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, mode: 438, flag: 'w' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
|
|
|
@ -947,7 +944,7 @@ fs.writeFileSync = function(path, data, options) { |
|
|
|
|
|
|
|
var flag = options.flag || 'w'; |
|
|
|
var fd = fs.openSync(path, flag, options.mode); |
|
|
|
if (!Buffer.isBuffer(data)) { |
|
|
|
if (!IS_BUFFER(data)) { |
|
|
|
data = new Buffer('' + data, options.encoding || 'utf8'); |
|
|
|
} |
|
|
|
var written = 0; |
|
|
@ -966,13 +963,13 @@ fs.writeFileSync = function(path, data, options) { |
|
|
|
fs.appendFile = function(path, data, options, callback_) { |
|
|
|
var callback = maybeCallback(arguments[arguments.length - 1]); |
|
|
|
|
|
|
|
if (typeof options === 'function' || !options) { |
|
|
|
if (IS_FUNCTION(options) || !options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, mode: 438, flag: 'a' }; |
|
|
|
} else if (!options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
|
|
|
@ -984,9 +981,9 @@ fs.appendFile = function(path, data, options, callback_) { |
|
|
|
fs.appendFileSync = function(path, data, options) { |
|
|
|
if (!options) { |
|
|
|
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' }; |
|
|
|
} else if (typeof options === 'string') { |
|
|
|
} else if (IS_STRING(options)) { |
|
|
|
options = { encoding: options, mode: 438, flag: 'a' }; |
|
|
|
} else if (typeof options !== 'object') { |
|
|
|
} else if (!IS_OBJECT(options)) { |
|
|
|
throw new TypeError('Bad arguments'); |
|
|
|
} |
|
|
|
if (!options.flag) |
|
|
@ -1033,7 +1030,7 @@ fs.watch = function(filename) { |
|
|
|
var options; |
|
|
|
var listener; |
|
|
|
|
|
|
|
if ('object' == typeof arguments[1]) { |
|
|
|
if (IS_OBJECT(arguments[1])) { |
|
|
|
options = arguments[1]; |
|
|
|
listener = arguments[2]; |
|
|
|
} else { |
|
|
@ -1041,7 +1038,7 @@ fs.watch = function(filename) { |
|
|
|
listener = arguments[1]; |
|
|
|
} |
|
|
|
|
|
|
|
if (options.persistent === undefined) options.persistent = true; |
|
|
|
if (IS_UNDEFINED(options.persistent)) options.persistent = true; |
|
|
|
|
|
|
|
watcher = new FSWatcher(); |
|
|
|
watcher.start(filename, options.persistent); |
|
|
@ -1113,7 +1110,7 @@ fs.watchFile = function(filename) { |
|
|
|
persistent: true |
|
|
|
}; |
|
|
|
|
|
|
|
if ('object' == typeof arguments[1]) { |
|
|
|
if (IS_OBJECT(arguments[1])) { |
|
|
|
options = util._extend(options, arguments[1]); |
|
|
|
listener = arguments[2]; |
|
|
|
} else { |
|
|
@ -1140,7 +1137,7 @@ fs.unwatchFile = function(filename, listener) { |
|
|
|
|
|
|
|
var stat = statWatchers[filename]; |
|
|
|
|
|
|
|
if (typeof listener === 'function') { |
|
|
|
if (IS_FUNCTION(listener)) { |
|
|
|
stat.removeListener('change', listener); |
|
|
|
} else { |
|
|
|
stat.removeAllListeners('change'); |
|
|
@ -1249,7 +1246,7 @@ fs.realpathSync = function realpathSync(p, cache) { |
|
|
|
linkTarget = seenLinks[id]; |
|
|
|
} |
|
|
|
} |
|
|
|
if (linkTarget === null) { |
|
|
|
if (IS_NULL(linkTarget)) { |
|
|
|
fs.statSync(base); |
|
|
|
linkTarget = fs.readlinkSync(base); |
|
|
|
} |
|
|
@ -1271,7 +1268,7 @@ fs.realpathSync = function realpathSync(p, cache) { |
|
|
|
|
|
|
|
|
|
|
|
fs.realpath = function realpath(p, cache, cb) { |
|
|
|
if (typeof cb !== 'function') { |
|
|
|
if (!IS_FUNCTION(cb)) { |
|
|
|
cb = maybeCallback(cache); |
|
|
|
cache = null; |
|
|
|
} |
|
|
@ -1432,13 +1429,13 @@ function ReadStream(path, options) { |
|
|
|
options.autoClose : true; |
|
|
|
this.pos = undefined; |
|
|
|
|
|
|
|
if (this.start !== undefined) { |
|
|
|
if ('number' !== typeof this.start) { |
|
|
|
if (!IS_UNDEFINED(this.start)) { |
|
|
|
if (!IS_NUMBER(this.start)) { |
|
|
|
throw TypeError('start must be a Number'); |
|
|
|
} |
|
|
|
if (this.end === undefined) { |
|
|
|
if (IS_UNDEFINED(this.end)) { |
|
|
|
this.end = Infinity; |
|
|
|
} else if ('number' !== typeof this.end) { |
|
|
|
} else if (!IS_NUMBER(this.end)) { |
|
|
|
throw TypeError('end must be a Number'); |
|
|
|
} |
|
|
|
|
|
|
@ -1449,7 +1446,7 @@ function ReadStream(path, options) { |
|
|
|
this.pos = this.start; |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof this.fd !== 'number') |
|
|
|
if (!IS_NUMBER(this.fd)) |
|
|
|
this.open(); |
|
|
|
|
|
|
|
this.on('end', function() { |
|
|
@ -1480,7 +1477,7 @@ ReadStream.prototype.open = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
ReadStream.prototype._read = function(n) { |
|
|
|
if (typeof this.fd !== 'number') |
|
|
|
if (!IS_NUMBER(this.fd)) |
|
|
|
return this.once('open', function() { |
|
|
|
this._read(n); |
|
|
|
}); |
|
|
@ -1501,7 +1498,7 @@ ReadStream.prototype._read = function(n) { |
|
|
|
var toRead = Math.min(pool.length - pool.used, n); |
|
|
|
var start = pool.used; |
|
|
|
|
|
|
|
if (this.pos !== undefined) |
|
|
|
if (!IS_UNDEFINED(this.pos)) |
|
|
|
toRead = Math.min(this.end - this.pos + 1, toRead); |
|
|
|
|
|
|
|
// already read everything we were supposed to read!
|
|
|
@ -1514,7 +1511,7 @@ ReadStream.prototype._read = function(n) { |
|
|
|
fs.read(this.fd, pool, pool.used, toRead, this.pos, onread); |
|
|
|
|
|
|
|
// move the pool positions, and internal position for reading.
|
|
|
|
if (this.pos !== undefined) |
|
|
|
if (!IS_UNDEFINED(this.pos)) |
|
|
|
this.pos += toRead; |
|
|
|
pool.used += toRead; |
|
|
|
|
|
|
@ -1540,7 +1537,7 @@ ReadStream.prototype.destroy = function() { |
|
|
|
return; |
|
|
|
this.destroyed = true; |
|
|
|
|
|
|
|
if ('number' === typeof this.fd) |
|
|
|
if (IS_NUMBER(this.fd)) |
|
|
|
this.close(); |
|
|
|
}; |
|
|
|
|
|
|
@ -1549,8 +1546,8 @@ ReadStream.prototype.close = function(cb) { |
|
|
|
var self = this; |
|
|
|
if (cb) |
|
|
|
this.once('close', cb); |
|
|
|
if (this.closed || 'number' !== typeof this.fd) { |
|
|
|
if ('number' !== typeof this.fd) { |
|
|
|
if (this.closed || !IS_NUMBER(this.fd)) { |
|
|
|
if (!IS_NUMBER(this.fd)) { |
|
|
|
this.once('open', close); |
|
|
|
return; |
|
|
|
} |
|
|
@ -1598,8 +1595,8 @@ function WriteStream(path, options) { |
|
|
|
this.pos = undefined; |
|
|
|
this.bytesWritten = 0; |
|
|
|
|
|
|
|
if (this.start !== undefined) { |
|
|
|
if ('number' !== typeof this.start) { |
|
|
|
if (!IS_UNDEFINED(this.start)) { |
|
|
|
if (!IS_NUMBER(this.start)) { |
|
|
|
throw TypeError('start must be a Number'); |
|
|
|
} |
|
|
|
if (this.start < 0) { |
|
|
@ -1609,7 +1606,7 @@ function WriteStream(path, options) { |
|
|
|
this.pos = this.start; |
|
|
|
} |
|
|
|
|
|
|
|
if ('number' !== typeof this.fd) |
|
|
|
if (!IS_NUMBER(this.fd)) |
|
|
|
this.open(); |
|
|
|
|
|
|
|
// dispose on finish.
|
|
|
@ -1634,10 +1631,10 @@ WriteStream.prototype.open = function() { |
|
|
|
|
|
|
|
|
|
|
|
WriteStream.prototype._write = function(data, encoding, cb) { |
|
|
|
if (!Buffer.isBuffer(data)) |
|
|
|
if (!IS_BUFFER(data)) |
|
|
|
return this.emit('error', new Error('Invalid data')); |
|
|
|
|
|
|
|
if (typeof this.fd !== 'number') |
|
|
|
if (!IS_NUMBER(this.fd)) |
|
|
|
return this.once('open', function() { |
|
|
|
this._write(data, encoding, cb); |
|
|
|
}); |
|
|
@ -1652,7 +1649,7 @@ WriteStream.prototype._write = function(data, encoding, cb) { |
|
|
|
cb(); |
|
|
|
}); |
|
|
|
|
|
|
|
if (this.pos !== undefined) |
|
|
|
if (!IS_UNDEFINED(this.pos)) |
|
|
|
this.pos += data.length; |
|
|
|
}; |
|
|
|
|
|
|
@ -1686,10 +1683,10 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) { |
|
|
|
|
|
|
|
// parse arguments
|
|
|
|
if (arg1) { |
|
|
|
if (typeof arg1 === 'string') { |
|
|
|
if (IS_STRING(arg1)) { |
|
|
|
encoding = arg1; |
|
|
|
cb = arg2; |
|
|
|
} else if (typeof arg1 === 'function') { |
|
|
|
} else if (IS_FUNCTION(arg1)) { |
|
|
|
cb = arg1; |
|
|
|
} else { |
|
|
|
throw new Error('bad arg'); |
|
|
@ -1698,7 +1695,7 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) { |
|
|
|
assertEncoding(encoding); |
|
|
|
|
|
|
|
// Change strings to buffers. SLOW
|
|
|
|
if (typeof data == 'string') { |
|
|
|
if (IS_STRING(data)) { |
|
|
|
data = new Buffer(data, encoding); |
|
|
|
} |
|
|
|
|
|
|
|