fs: minor refactoring
1. Remove a few unnecessary variables to reduce LoC. 2. Remove redundant `var` definitions of variables in same function. 3. Refactor variables which are defined inside a block and used outside as well. 4. Refactor effect-less code. 5. In `rethrow` function, instead of assigning to `err` and throwing `err` directly throw `backtrace` object. 6. Reassign a defined parameter while also mentioning arguments in the body is one of the optimization killers. So, changing `callback` to `callback_` and declaring a new variable called `callback` in the body. PR-URL: https://github.com/nodejs/io.js/pull/1870 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
8841132f30
commit
a011c3243f
57
lib/fs.js
57
lib/fs.js
@ -49,8 +49,7 @@ function rethrow() {
|
|||||||
if (err) {
|
if (err) {
|
||||||
backtrace.stack = err.name + ': ' + err.message +
|
backtrace.stack = err.name + ': ' + err.message +
|
||||||
backtrace.stack.substr(backtrace.name.length);
|
backtrace.stack.substr(backtrace.name.length);
|
||||||
err = backtrace;
|
throw backtrace;
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -267,27 +266,25 @@ function ReadFileContext(callback, encoding) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ReadFileContext.prototype.read = function() {
|
ReadFileContext.prototype.read = function() {
|
||||||
var fd = this.fd;
|
|
||||||
var size = this.size;
|
|
||||||
var buffer;
|
var buffer;
|
||||||
var offset;
|
var offset;
|
||||||
var length;
|
var length;
|
||||||
|
|
||||||
if (size === 0) {
|
if (this.size === 0) {
|
||||||
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
|
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
|
||||||
offset = 0;
|
offset = 0;
|
||||||
length = kReadFileBufferLength;
|
length = kReadFileBufferLength;
|
||||||
} else {
|
} else {
|
||||||
buffer = this.buffer;
|
buffer = this.buffer;
|
||||||
offset = this.pos;
|
offset = this.pos;
|
||||||
length = size - this.pos;
|
length = this.size - this.pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
var req = new FSReqWrap();
|
var req = new FSReqWrap();
|
||||||
req.oncomplete = readFileAfterRead;
|
req.oncomplete = readFileAfterRead;
|
||||||
req.context = this;
|
req.context = this;
|
||||||
|
|
||||||
binding.read(fd, buffer, offset, length, -1, req);
|
binding.read(this.fd, buffer, offset, length, -1, req);
|
||||||
};
|
};
|
||||||
|
|
||||||
ReadFileContext.prototype.close = function(err) {
|
ReadFileContext.prototype.close = function(err) {
|
||||||
@ -302,8 +299,7 @@ function readFileAfterOpen(err, fd) {
|
|||||||
var context = this.context;
|
var context = this.context;
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
var callback = context.callback;
|
context.callback(err);
|
||||||
callback(err);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -417,7 +413,7 @@ fs.readFileSync = function(path, options) {
|
|||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
buffers = [];
|
buffers = [];
|
||||||
} else {
|
} else {
|
||||||
var threw = true;
|
threw = true;
|
||||||
try {
|
try {
|
||||||
buffer = new Buffer(size);
|
buffer = new Buffer(size);
|
||||||
threw = false;
|
threw = false;
|
||||||
@ -427,16 +423,18 @@ fs.readFileSync = function(path, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var done = false;
|
var done = false;
|
||||||
|
var bytesRead;
|
||||||
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
var threw = true;
|
threw = true;
|
||||||
try {
|
try {
|
||||||
if (size !== 0) {
|
if (size !== 0) {
|
||||||
var bytesRead = fs.readSync(fd, buffer, pos, size - pos);
|
bytesRead = fs.readSync(fd, buffer, pos, size - pos);
|
||||||
} else {
|
} else {
|
||||||
// the kernel lies about many files.
|
// the kernel lies about many files.
|
||||||
// Go ahead and try to read some bytes.
|
// Go ahead and try to read some bytes.
|
||||||
buffer = new Buffer(8192);
|
buffer = new Buffer(8192);
|
||||||
var bytesRead = fs.readSync(fd, buffer, 0, 8192);
|
bytesRead = fs.readSync(fd, buffer, 0, 8192);
|
||||||
if (bytesRead) {
|
if (bytesRead) {
|
||||||
buffers.push(buffer.slice(0, bytesRead));
|
buffers.push(buffer.slice(0, bytesRead));
|
||||||
}
|
}
|
||||||
@ -529,8 +527,8 @@ function modeNum(m, def) {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.open = function(path, flags, mode, callback) {
|
fs.open = function(path, flags, mode, callback_) {
|
||||||
callback = makeCallback(arguments[arguments.length - 1]);
|
var callback = makeCallback(arguments[arguments.length - 1]);
|
||||||
mode = modeNum(mode, 0o666);
|
mode = modeNum(mode, 0o666);
|
||||||
|
|
||||||
if (!nullCheck(path, callback)) return;
|
if (!nullCheck(path, callback)) return;
|
||||||
@ -585,10 +583,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
|
|||||||
|
|
||||||
fs.readSync = function(fd, buffer, offset, length, position) {
|
fs.readSync = function(fd, buffer, offset, length, position) {
|
||||||
var legacy = false;
|
var legacy = false;
|
||||||
|
var encoding;
|
||||||
|
|
||||||
if (!(buffer instanceof Buffer)) {
|
if (!(buffer instanceof Buffer)) {
|
||||||
// legacy string interface (fd, length, position, encoding, callback)
|
// legacy string interface (fd, length, position, encoding, callback)
|
||||||
legacy = true;
|
legacy = true;
|
||||||
var encoding = arguments[3];
|
encoding = arguments[3];
|
||||||
|
|
||||||
assertEncoding(encoding);
|
assertEncoding(encoding);
|
||||||
|
|
||||||
@ -623,6 +623,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
|
|||||||
callback(err, written || 0, buffer);
|
callback(err, written || 0, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var req = new FSReqWrap();
|
||||||
if (buffer instanceof Buffer) {
|
if (buffer instanceof Buffer) {
|
||||||
// if no position is passed then assume null
|
// if no position is passed then assume null
|
||||||
if (typeof position === 'function') {
|
if (typeof position === 'function') {
|
||||||
@ -630,7 +631,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
|
|||||||
position = null;
|
position = null;
|
||||||
}
|
}
|
||||||
callback = maybeCallback(callback);
|
callback = maybeCallback(callback);
|
||||||
var req = new FSReqWrap();
|
|
||||||
req.oncomplete = strWrapper;
|
req.oncomplete = strWrapper;
|
||||||
return binding.writeBuffer(fd, buffer, offset, length, position, req);
|
return binding.writeBuffer(fd, buffer, offset, length, position, req);
|
||||||
}
|
}
|
||||||
@ -647,7 +647,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
|
|||||||
length = 'utf8';
|
length = 'utf8';
|
||||||
}
|
}
|
||||||
callback = maybeCallback(position);
|
callback = maybeCallback(position);
|
||||||
var req = new FSReqWrap();
|
|
||||||
req.oncomplete = bufWrapper;
|
req.oncomplete = bufWrapper;
|
||||||
return binding.writeString(fd, buffer, offset, length, req);
|
return binding.writeString(fd, buffer, offset, length, req);
|
||||||
};
|
};
|
||||||
@ -721,8 +720,10 @@ fs.truncateSync = function(path, len) {
|
|||||||
}
|
}
|
||||||
// allow error to be thrown, but still close fd.
|
// allow error to be thrown, but still close fd.
|
||||||
var fd = fs.openSync(path, 'r+');
|
var fd = fs.openSync(path, 'r+');
|
||||||
|
var ret;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var ret = fs.ftruncateSync(fd, len);
|
ret = fs.ftruncateSync(fd, len);
|
||||||
} finally {
|
} finally {
|
||||||
fs.closeSync(fd);
|
fs.closeSync(fd);
|
||||||
}
|
}
|
||||||
@ -875,7 +876,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.symlink = function(destination, path, type_, callback) {
|
fs.symlink = function(destination, path, type_, callback_) {
|
||||||
var type = (typeof type_ === 'string' ? type_ : null);
|
var type = (typeof type_ === 'string' ? type_ : null);
|
||||||
var callback = makeCallback(arguments[arguments.length - 1]);
|
var callback = makeCallback(arguments[arguments.length - 1]);
|
||||||
|
|
||||||
@ -968,9 +969,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) {
|
|||||||
|
|
||||||
// prefer to return the chmod error, if one occurs,
|
// prefer to return the chmod error, if one occurs,
|
||||||
// but still try to close, and report closing errors if they occur.
|
// but still try to close, and report closing errors if they occur.
|
||||||
var err, err2;
|
var err, err2, ret;
|
||||||
try {
|
try {
|
||||||
var ret = fs.fchmodSync(fd, mode);
|
ret = fs.fchmodSync(fd, mode);
|
||||||
} catch (er) {
|
} catch (er) {
|
||||||
err = er;
|
err = er;
|
||||||
}
|
}
|
||||||
@ -1088,8 +1089,8 @@ fs.futimesSync = function(fd, atime, mtime) {
|
|||||||
binding.futimes(fd, atime, mtime);
|
binding.futimes(fd, atime, mtime);
|
||||||
};
|
};
|
||||||
|
|
||||||
function writeAll(fd, buffer, offset, length, position, callback) {
|
function writeAll(fd, buffer, offset, length, position, callback_) {
|
||||||
callback = maybeCallback(arguments[arguments.length - 1]);
|
var callback = maybeCallback(arguments[arguments.length - 1]);
|
||||||
|
|
||||||
// write(fd, buffer, offset, length, position, callback)
|
// write(fd, buffer, offset, length, position, callback)
|
||||||
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
|
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
|
||||||
@ -1112,7 +1113,7 @@ function writeAll(fd, buffer, offset, length, position, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile = function(path, data, options, callback) {
|
fs.writeFile = function(path, data, options, callback_) {
|
||||||
var callback = maybeCallback(arguments[arguments.length - 1]);
|
var callback = maybeCallback(arguments[arguments.length - 1]);
|
||||||
|
|
||||||
if (!options || typeof options === 'function') {
|
if (!options || typeof options === 'function') {
|
||||||
@ -1634,8 +1635,8 @@ function ReadStream(path, options) {
|
|||||||
this.flags = options.flags === undefined ? 'r' : options.flags;
|
this.flags = options.flags === undefined ? 'r' : options.flags;
|
||||||
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
||||||
|
|
||||||
this.start = options.start === undefined ? undefined : options.start;
|
this.start = options.start;
|
||||||
this.end = options.end === undefined ? undefined : options.end;
|
this.end = options.end;
|
||||||
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
|
||||||
this.pos = undefined;
|
this.pos = undefined;
|
||||||
|
|
||||||
@ -1804,7 +1805,7 @@ function WriteStream(path, options) {
|
|||||||
this.flags = options.flags === undefined ? 'w' : options.flags;
|
this.flags = options.flags === undefined ? 'w' : options.flags;
|
||||||
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
||||||
|
|
||||||
this.start = options.start === undefined ? undefined : options.start;
|
this.start = options.start;
|
||||||
this.pos = undefined;
|
this.pos = undefined;
|
||||||
this.bytesWritten = 0;
|
this.bytesWritten = 0;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user