diff --git a/lib/fs.js b/lib/fs.js index 6ca74443a81a48..3315158f642628 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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) { @@ -301,8 +299,7 @@ function readFileAfterOpen(err, fd) { var context = this.context; if (err) { - var callback = context.callback; - callback(err); + context.callback(err); return; } @@ -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; @@ -425,17 +422,17 @@ fs.readFileSync = function(path, options) { } } - var done = false; + var done = false, 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)); } @@ -584,10 +581,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); @@ -622,6 +621,7 @@ 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') { @@ -629,7 +629,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) { position = null; } callback = maybeCallback(callback); - var req = new FSReqWrap(); req.oncomplete = strWrapper; return binding.writeBuffer(fd, buffer, offset, length, position, req); } @@ -646,7 +645,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); }; @@ -720,8 +718,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); } @@ -876,7 +876,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; @@ -967,9 +967,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; } @@ -1112,7 +1112,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' }; @@ -1625,8 +1625,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; @@ -1788,7 +1788,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;