aboutsummaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fs.js')
-rw-r--r--lib/fs.js144
1 files changed, 72 insertions, 72 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 78e63c9c78..3a3fe56c2b 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -81,14 +81,14 @@ function rethrow() {
}
function maybeCallback(cb) {
- return IS_FUNCTION(cb) ? cb : rethrow();
+ return util.isFunction(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 (!IS_FUNCTION(cb)) {
+ if (!util.isFunction(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 (IS_FUNCTION(options) || !options) {
+ if (util.isFunction(options) || !options) {
options = { encoding: null, flag: 'r' };
- } else if (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, flag: 'r' };
} else if (!options) {
options = { encoding: null, flag: 'r' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(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 (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, flag: 'r' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(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 (!IS_STRING(flag)) {
+ if (!util.isString(flag)) {
return flag;
}
@@ -381,9 +381,9 @@ fs.closeSync = function(fd) {
};
function modeNum(m, def) {
- if (IS_NUMBER(m))
+ if (util.isNumber(m))
return m;
- if (IS_STRING(m))
+ if (util.isString(m))
return parseInt(m, 8);
if (def)
return modeNum(def);
@@ -408,7 +408,7 @@ fs.openSync = function(path, flags, mode) {
};
fs.read = function(fd, buffer, offset, length, position, callback) {
- if (!IS_BUFFER(buffer)) {
+ if (!util.isBuffer(buffer)) {
// legacy string interface (fd, length, position, encoding, callback)
var cb = arguments[4],
encoding = arguments[3];
@@ -439,7 +439,7 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
fs.readSync = function(fd, buffer, offset, length, position) {
var legacy = false;
- if (!IS_BUFFER(buffer)) {
+ if (!util.isBuffer(buffer)) {
// legacy string interface (fd, length, position, encoding, callback)
legacy = true;
var encoding = arguments[3];
@@ -467,9 +467,9 @@ fs.readSync = function(fd, buffer, offset, length, position) {
// OR
// fs.write(fd, string[, position[, encoding]], callback);
fs.write = function(fd, buffer, offset, length, position, callback) {
- if (IS_BUFFER(buffer)) {
+ if (util.isBuffer(buffer)) {
// if no position is passed then assume null
- if (IS_FUNCTION(position)) {
+ if (util.isFunction(position)) {
callback = position;
position = null;
}
@@ -481,10 +481,10 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
return binding.writeBuffer(fd, buffer, offset, length, position, wrapper);
}
- if (IS_STRING(buffer))
+ if (util.isString(buffer))
buffer += '';
- if (!IS_FUNCTION(position)) {
- if (IS_FUNCTION(offset)) {
+ if (!util.isFunction(position)) {
+ if (util.isFunction(offset)) {
position = offset;
offset = null;
} else {
@@ -505,14 +505,14 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
// OR
// fs.writeSync(fd, string[, position[, encoding]]);
fs.writeSync = function(fd, buffer, offset, length, position) {
- if (IS_BUFFER(buffer)) {
- if (IS_UNDEFINED(position))
+ if (util.isBuffer(buffer)) {
+ if (util.isUndefined(position))
position = null;
return binding.writeBuffer(fd, buffer, offset, length, position);
}
- if (!IS_STRING(buffer))
+ if (!util.isString(buffer))
buffer += '';
- if (IS_UNDEFINED(offset))
+ if (util.isUndefined(offset))
offset = null;
return binding.writeString(fd, buffer, offset, length, position);
};
@@ -534,14 +534,14 @@ fs.renameSync = function(oldPath, newPath) {
};
fs.truncate = function(path, len, callback) {
- if (IS_NUMBER(path)) {
+ if (util.isNumber(path)) {
// legacy
return fs.ftruncate(path, len, callback);
}
- if (IS_FUNCTION(len)) {
+ if (util.isFunction(len)) {
callback = len;
len = 0;
- } else if (IS_UNDEFINED(len)) {
+ } else if (util.isUndefined(len)) {
len = 0;
}
callback = maybeCallback(callback);
@@ -556,11 +556,11 @@ fs.truncate = function(path, len, callback) {
};
fs.truncateSync = function(path, len) {
- if (IS_NUMBER(path)) {
+ if (util.isNumber(path)) {
// legacy
return fs.ftruncateSync(path, len);
}
- if (IS_UNDEFINED(len)) {
+ if (util.isUndefined(len)) {
len = 0;
}
// allow error to be thrown, but still close fd.
@@ -574,17 +574,17 @@ fs.truncateSync = function(path, len) {
};
fs.ftruncate = function(fd, len, callback) {
- if (IS_FUNCTION(len)) {
+ if (util.isFunction(len)) {
callback = len;
len = 0;
- } else if (IS_UNDEFINED(len)) {
+ } else if (util.isUndefined(len)) {
len = 0;
}
binding.ftruncate(fd, len, makeCallback(callback));
};
fs.ftruncateSync = function(fd, len) {
- if (IS_UNDEFINED(len)) {
+ if (util.isUndefined(len)) {
len = 0;
}
return binding.ftruncate(fd, len);
@@ -618,7 +618,7 @@ fs.fsyncSync = function(fd) {
};
fs.mkdir = function(path, mode, callback) {
- if (IS_FUNCTION(mode)) callback = mode;
+ if (util.isFunction(mode)) callback = mode;
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
binding.mkdir(pathModule._makeLong(path),
@@ -698,7 +698,7 @@ function preprocessSymlinkDestination(path, type) {
}
fs.symlink = function(destination, path, type_, callback) {
- var type = (IS_STRING(type_) ? type_ : null);
+ var type = (util.isString(type_) ? type_ : null);
var callback = makeCallback(arguments[arguments.length - 1]);
if (!nullCheck(destination, callback)) return;
@@ -711,7 +711,7 @@ fs.symlink = function(destination, path, type_, callback) {
};
fs.symlinkSync = function(destination, path, type) {
- type = (IS_STRING(type) ? type : null);
+ type = (util.isString(type) ? type : null);
nullCheck(destination);
nullCheck(path);
@@ -849,10 +849,10 @@ fs.chownSync = function(path, uid, gid) {
// converts Date or number to a fractional UNIX timestamp
function toUnixTimestamp(time) {
- if (IS_NUMBER(time)) {
+ if (util.isNumber(time)) {
return time;
}
- if (IS_DATE(time)) {
+ if (util.isDate(time)) {
// convert to 123.456 UNIX timestamp
return time.getTime() / 1000;
}
@@ -915,13 +915,13 @@ function writeAll(fd, buffer, offset, length, position, callback) {
fs.writeFile = function(path, data, options, callback) {
var callback = maybeCallback(arguments[arguments.length - 1]);
- if (IS_FUNCTION(options) || !options) {
+ if (util.isFunction(options) || !options) {
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
- } else if (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, mode: 438, flag: 'w' };
} else if (!options) {
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'w' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments');
}
@@ -932,7 +932,7 @@ fs.writeFile = function(path, data, options, callback) {
if (openErr) {
if (callback) callback(openErr);
} else {
- var buffer = IS_BUFFER(data) ? data : new Buffer('' + data,
+ var buffer = util.isBuffer(data) ? data : new Buffer('' + data,
options.encoding || 'utf8');
var position = /a/.test(flag) ? null : 0;
writeAll(fd, buffer, 0, buffer.length, position, callback);
@@ -943,9 +943,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 (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, mode: 438, flag: 'w' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments');
}
@@ -953,7 +953,7 @@ fs.writeFileSync = function(path, data, options) {
var flag = options.flag || 'w';
var fd = fs.openSync(path, flag, options.mode);
- if (!IS_BUFFER(data)) {
+ if (!util.isBuffer(data)) {
data = new Buffer('' + data, options.encoding || 'utf8');
}
var written = 0;
@@ -972,13 +972,13 @@ fs.writeFileSync = function(path, data, options) {
fs.appendFile = function(path, data, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
- if (IS_FUNCTION(options) || !options) {
+ if (util.isFunction(options) || !options) {
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
- } else if (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, mode: 438, flag: 'a' };
} else if (!options) {
options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments');
}
@@ -990,9 +990,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 (IS_STRING(options)) {
+ } else if (util.isString(options)) {
options = { encoding: options, mode: 438, flag: 'a' };
- } else if (!IS_OBJECT(options)) {
+ } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments');
}
if (!options.flag)
@@ -1039,7 +1039,7 @@ fs.watch = function(filename) {
var options;
var listener;
- if (IS_OBJECT(arguments[1])) {
+ if (util.isObject(arguments[1])) {
options = arguments[1];
listener = arguments[2];
} else {
@@ -1047,7 +1047,7 @@ fs.watch = function(filename) {
listener = arguments[1];
}
- if (IS_UNDEFINED(options.persistent)) options.persistent = true;
+ if (util.isUndefined(options.persistent)) options.persistent = true;
watcher = new FSWatcher();
watcher.start(filename, options.persistent);
@@ -1119,7 +1119,7 @@ fs.watchFile = function(filename) {
persistent: true
};
- if (IS_OBJECT(arguments[1])) {
+ if (util.isObject(arguments[1])) {
options = util._extend(options, arguments[1]);
listener = arguments[2];
} else {
@@ -1146,7 +1146,7 @@ fs.unwatchFile = function(filename, listener) {
var stat = statWatchers[filename];
- if (IS_FUNCTION(listener)) {
+ if (util.isFunction(listener)) {
stat.removeListener('change', listener);
} else {
stat.removeAllListeners('change');
@@ -1255,7 +1255,7 @@ fs.realpathSync = function realpathSync(p, cache) {
linkTarget = seenLinks[id];
}
}
- if (IS_NULL(linkTarget)) {
+ if (util.isNull(linkTarget)) {
fs.statSync(base);
linkTarget = fs.readlinkSync(base);
}
@@ -1277,7 +1277,7 @@ fs.realpathSync = function realpathSync(p, cache) {
fs.realpath = function realpath(p, cache, cb) {
- if (!IS_FUNCTION(cb)) {
+ if (!util.isFunction(cb)) {
cb = maybeCallback(cache);
cache = null;
}
@@ -1438,13 +1438,13 @@ function ReadStream(path, options) {
options.autoClose : true;
this.pos = undefined;
- if (!IS_UNDEFINED(this.start)) {
- if (!IS_NUMBER(this.start)) {
+ if (!util.isUndefined(this.start)) {
+ if (!util.isNumber(this.start)) {
throw TypeError('start must be a Number');
}
- if (IS_UNDEFINED(this.end)) {
+ if (util.isUndefined(this.end)) {
this.end = Infinity;
- } else if (!IS_NUMBER(this.end)) {
+ } else if (!util.isNumber(this.end)) {
throw TypeError('end must be a Number');
}
@@ -1455,7 +1455,7 @@ function ReadStream(path, options) {
this.pos = this.start;
}
- if (!IS_NUMBER(this.fd))
+ if (!util.isNumber(this.fd))
this.open();
this.on('end', function() {
@@ -1486,7 +1486,7 @@ ReadStream.prototype.open = function() {
};
ReadStream.prototype._read = function(n) {
- if (!IS_NUMBER(this.fd))
+ if (!util.isNumber(this.fd))
return this.once('open', function() {
this._read(n);
});
@@ -1507,7 +1507,7 @@ ReadStream.prototype._read = function(n) {
var toRead = Math.min(pool.length - pool.used, n);
var start = pool.used;
- if (!IS_UNDEFINED(this.pos))
+ if (!util.isUndefined(this.pos))
toRead = Math.min(this.end - this.pos + 1, toRead);
// already read everything we were supposed to read!
@@ -1520,7 +1520,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 (!IS_UNDEFINED(this.pos))
+ if (!util.isUndefined(this.pos))
this.pos += toRead;
pool.used += toRead;
@@ -1546,7 +1546,7 @@ ReadStream.prototype.destroy = function() {
return;
this.destroyed = true;
- if (IS_NUMBER(this.fd))
+ if (util.isNumber(this.fd))
this.close();
};
@@ -1555,8 +1555,8 @@ ReadStream.prototype.close = function(cb) {
var self = this;
if (cb)
this.once('close', cb);
- if (this.closed || !IS_NUMBER(this.fd)) {
- if (!IS_NUMBER(this.fd)) {
+ if (this.closed || !util.isNumber(this.fd)) {
+ if (!util.isNumber(this.fd)) {
this.once('open', close);
return;
}
@@ -1604,8 +1604,8 @@ function WriteStream(path, options) {
this.pos = undefined;
this.bytesWritten = 0;
- if (!IS_UNDEFINED(this.start)) {
- if (!IS_NUMBER(this.start)) {
+ if (!util.isUndefined(this.start)) {
+ if (!util.isNumber(this.start)) {
throw TypeError('start must be a Number');
}
if (this.start < 0) {
@@ -1615,7 +1615,7 @@ function WriteStream(path, options) {
this.pos = this.start;
}
- if (!IS_NUMBER(this.fd))
+ if (!util.isNumber(this.fd))
this.open();
// dispose on finish.
@@ -1640,10 +1640,10 @@ WriteStream.prototype.open = function() {
WriteStream.prototype._write = function(data, encoding, cb) {
- if (!IS_BUFFER(data))
+ if (!util.isBuffer(data))
return this.emit('error', new Error('Invalid data'));
- if (!IS_NUMBER(this.fd))
+ if (!util.isNumber(this.fd))
return this.once('open', function() {
this._write(data, encoding, cb);
});
@@ -1658,7 +1658,7 @@ WriteStream.prototype._write = function(data, encoding, cb) {
cb();
});
- if (!IS_UNDEFINED(this.pos))
+ if (!util.isUndefined(this.pos))
this.pos += data.length;
};
@@ -1692,10 +1692,10 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) {
// parse arguments
if (arg1) {
- if (IS_STRING(arg1)) {
+ if (util.isString(arg1)) {
encoding = arg1;
cb = arg2;
- } else if (IS_FUNCTION(arg1)) {
+ } else if (util.isFunction(arg1)) {
cb = arg1;
} else {
throw new Error('bad arg');
@@ -1704,7 +1704,7 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) {
assertEncoding(encoding);
// Change strings to buffers. SLOW
- if (IS_STRING(data)) {
+ if (util.isString(data)) {
data = new Buffer(data, encoding);
}