Skip to content

Commit

Permalink
fs: minor refactoring
Browse files Browse the repository at this point in the history
1. Removed a few unnecessary variables to reduce LoC

2. Removed redundant `var` definitions of variables in same function

3. Refactored variables which are defined inside a block and used
outside as well

4. Refactored effect-less code
  • Loading branch information
thefourtheye committed Jun 2, 2015
1 parent 8eafbfe commit d725865
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,25 @@ function ReadFileContext(callback, encoding) {
}

ReadFileContext.prototype.read = function() {
var fd = this.fd;
var size = this.size;
var buffer;
var offset;
var length;

if (size === 0) {
if (this.size === 0) {
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
offset = 0;
length = kReadFileBufferLength;
} else {
buffer = this.buffer;
offset = this.pos;
length = size - this.pos;
length = this.size - this.pos;
}

var req = new FSReqWrap();
req.oncomplete = readFileAfterRead;
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) {
Expand All @@ -301,8 +299,7 @@ function readFileAfterOpen(err, fd) {
var context = this.context;

if (err) {
var callback = context.callback;
callback(err);
context.callback(err);
return;
}

Expand Down Expand Up @@ -416,7 +413,7 @@ fs.readFileSync = function(path, options) {
if (size === 0) {
buffers = [];
} else {
var threw = true;
threw = true;
try {
buffer = new Buffer(size);
threw = false;
Expand All @@ -426,16 +423,18 @@ fs.readFileSync = function(path, options) {
}

var done = false;
var bytesRead;

while (!done) {
var threw = true;
threw = true;
try {
if (size !== 0) {
var bytesRead = fs.readSync(fd, buffer, pos, size - pos);
bytesRead = fs.readSync(fd, buffer, pos, size - pos);
} else {
// the kernel lies about many files.
// Go ahead and try to read some bytes.
buffer = new Buffer(8192);
var bytesRead = fs.readSync(fd, buffer, 0, 8192);
bytesRead = fs.readSync(fd, buffer, 0, 8192);
if (bytesRead) {
buffers.push(buffer.slice(0, bytesRead));
}
Expand Down Expand Up @@ -584,10 +583,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {

fs.readSync = function(fd, buffer, offset, length, position) {
var legacy = false;
var encoding;

if (!(buffer instanceof Buffer)) {
// legacy string interface (fd, length, position, encoding, callback)
legacy = true;
var encoding = arguments[3];
encoding = arguments[3];

assertEncoding(encoding);

Expand Down Expand Up @@ -622,14 +623,14 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
callback(err, written || 0, buffer);
}

var req = new FSReqWrap();
if (buffer instanceof Buffer) {
// if no position is passed then assume null
if (typeof position === 'function') {
callback = position;
position = null;
}
callback = maybeCallback(callback);
var req = new FSReqWrap();
req.oncomplete = strWrapper;
return binding.writeBuffer(fd, buffer, offset, length, position, req);
}
Expand All @@ -646,7 +647,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
length = 'utf8';
}
callback = maybeCallback(position);
var req = new FSReqWrap();
req.oncomplete = bufWrapper;
return binding.writeString(fd, buffer, offset, length, req);
};
Expand Down Expand Up @@ -720,8 +720,10 @@ fs.truncateSync = function(path, len) {
}
// allow error to be thrown, but still close fd.
var fd = fs.openSync(path, 'r+');
var ret;

try {
var ret = fs.ftruncateSync(fd, len);
ret = fs.ftruncateSync(fd, len);
} finally {
fs.closeSync(fd);
}
Expand Down Expand Up @@ -876,7 +878,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {

fs.symlink = function(destination, path, type_, callback) {
var type = (typeof type_ === 'string' ? type_ : null);
var callback = makeCallback(arguments[arguments.length - 1]);
callback = makeCallback(arguments[arguments.length - 1]);

if (!nullCheck(destination, callback)) return;
if (!nullCheck(path, callback)) return;
Expand Down Expand Up @@ -967,9 +969,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) {

// prefer to return the chmod error, if one occurs,
// but still try to close, and report closing errors if they occur.
var err, err2;
var err, err2, ret;
try {
var ret = fs.fchmodSync(fd, mode);
ret = fs.fchmodSync(fd, mode);
} catch (er) {
err = er;
}
Expand Down Expand Up @@ -1112,7 +1114,7 @@ function writeAll(fd, buffer, offset, length, position, callback) {
}

fs.writeFile = function(path, data, options, callback) {
var callback = maybeCallback(arguments[arguments.length - 1]);
callback = maybeCallback(arguments[arguments.length - 1]);

if (!options || typeof options === 'function') {
options = { encoding: 'utf8', mode: 0o666, flag: 'w' };
Expand Down Expand Up @@ -1625,8 +1627,8 @@ function ReadStream(path, options) {
this.flags = options.flags === undefined ? 'r' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;

this.start = options.start === undefined ? undefined : options.start;
this.end = options.end === undefined ? undefined : options.end;
this.start = options.start;
this.end = options.end;
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
this.pos = undefined;

Expand Down Expand Up @@ -1788,7 +1790,7 @@ function WriteStream(path, options) {
this.flags = options.flags === undefined ? 'w' : options.flags;
this.mode = options.mode === undefined ? 0o666 : options.mode;

this.start = options.start === undefined ? undefined : options.start;
this.start = options.start;
this.pos = undefined;
this.bytesWritten = 0;

Expand Down

0 comments on commit d725865

Please sign in to comment.