diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-12-01 17:43:30 -0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-12-01 17:43:30 -0800 |
commit | 09329cbb04a05d3b673eb8e84121b973b659c3e3 (patch) | |
tree | 7c81b38ff41d843ea7f3e8895a8e80f7c8efd4c7 /lib/fs.js | |
parent | f22c248e4c6f136981aa7afbd442a72b64c40b04 (diff) | |
download | node-new-09329cbb04a05d3b673eb8e84121b973b659c3e3.tar.gz |
more lint
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 283 |
1 files changed, 154 insertions, 129 deletions
@@ -10,54 +10,57 @@ var kPoolSize = 40 * 1024; fs.Stats = binding.Stats; -fs.Stats.prototype._checkModeProperty = function (property) { +fs.Stats.prototype._checkModeProperty = function(property) { return ((this.mode & constants.S_IFMT) === property); }; -fs.Stats.prototype.isDirectory = function () { +fs.Stats.prototype.isDirectory = function() { return this._checkModeProperty(constants.S_IFDIR); }; -fs.Stats.prototype.isFile = function () { +fs.Stats.prototype.isFile = function() { return this._checkModeProperty(constants.S_IFREG); }; -fs.Stats.prototype.isBlockDevice = function () { +fs.Stats.prototype.isBlockDevice = function() { return this._checkModeProperty(constants.S_IFBLK); }; -fs.Stats.prototype.isCharacterDevice = function () { +fs.Stats.prototype.isCharacterDevice = function() { return this._checkModeProperty(constants.S_IFCHR); }; -fs.Stats.prototype.isSymbolicLink = function () { +fs.Stats.prototype.isSymbolicLink = function() { return this._checkModeProperty(constants.S_IFLNK); }; -fs.Stats.prototype.isFIFO = function () { +fs.Stats.prototype.isFIFO = function() { return this._checkModeProperty(constants.S_IFIFO); }; -fs.Stats.prototype.isSocket = function () { +fs.Stats.prototype.isSocket = function() { return this._checkModeProperty(constants.S_IFSOCK); }; -fs.readFile = function (path, encoding_) { +fs.readFile = function(path, encoding_) { var encoding = typeof(encoding_) === 'string' ? encoding_ : null; - var callback = arguments[arguments.length-1]; + var callback = arguments[arguments.length - 1]; if (typeof(callback) !== 'function') callback = noop; var readStream = fs.createReadStream(path); var buffers = []; var nread = 0; - readStream.on("data", function (chunk) { + + readStream.on('data', function(chunk) { buffers.push(chunk); nread += chunk.length; }); - readStream.on("error", function (er) { + + readStream.on('error', function(er) { callback(er); readStream.destroy(); }); - readStream.on("end", function () { + + readStream.on('end', function() { // copy all the buffers into one var buffer; switch (buffers.length) { @@ -66,7 +69,7 @@ fs.readFile = function (path, encoding_) { default: // concat together buffer = new Buffer(nread); var n = 0; - buffers.forEach(function (b) { + buffers.forEach(function(b) { var l = b.length; b.copy(buffer, n, 0, l); n += l; @@ -84,7 +87,7 @@ fs.readFile = function (path, encoding_) { }); }; -fs.readFileSync = function (path, encoding) { +fs.readFileSync = function(path, encoding) { var fd = fs.openSync(path, constants.O_RDONLY, 0666); var buffer = new Buffer(4048); var buffers = []; @@ -107,13 +110,14 @@ fs.readFileSync = function (path, encoding) { var offset = 0; var i; buffer = new Buffer(nread); - buffers.forEach(function (i) { + buffers.forEach(function(i) { if (!i._bytesRead) return; - i.copy(buffer,offset,0,i._bytesRead); + i.copy(buffer, offset, 0, i._bytesRead); offset += i._bytesRead; }); } else if (buffers.length) { - //buffers has exactly 1 (possibly zero length) buffer, so this should be a shortcut + // buffers has exactly 1 (possibly zero length) buffer, so this should + // be a shortcut buffer = buffers[0].slice(0, buffers[0]._bytesRead); } else { buffer = new Buffer(0); @@ -131,30 +135,43 @@ function stringToFlags(flag) { return flag; } switch (flag) { - case "r": return constants.O_RDONLY; - case "r+": return constants.O_RDWR; - case "w": return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY; - case "w+": return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR; - case "a": return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY; - case "a+": return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR; - default: throw new Error("Unknown file open flag: " + flag); + case 'r': + return constants.O_RDONLY; + + case 'r+': + return constants.O_RDWR; + + case 'w': + return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY; + + case 'w+': + return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR; + + case 'a': + return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY; + + case 'a+': + return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR; + + default: + throw new Error('Unknown file open flag: ' + flag); } } -function noop () {} +function noop() {} // Yes, the follow could be easily DRYed up but I provide the explicit // list to make the arguments clear. -fs.close = function (fd, callback) { +fs.close = function(fd, callback) { binding.close(fd, callback || noop); }; -fs.closeSync = function (fd) { +fs.closeSync = function(fd) { return binding.close(fd); }; -fs.open = function (path, flags, mode_, callback) { +fs.open = function(path, flags, mode_, callback) { var mode = (typeof(mode_) == 'number' ? mode_ : 0666); var callback_ = arguments[arguments.length - 1]; var callback = (typeof(callback_) == 'function' ? callback_ : null); @@ -162,12 +179,12 @@ fs.open = function (path, flags, mode_, callback) { binding.open(path, stringToFlags(flags), mode, callback || noop); }; -fs.openSync = function (path, flags, mode) { +fs.openSync = function(path, flags, mode) { if (mode === undefined) { mode = 0666; } return binding.open(path, stringToFlags(flags), mode); }; -fs.read = function (fd, buffer, offset, length, position, callback) { +fs.read = function(fd, buffer, offset, length, position, callback) { if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, length, position, encoding, callback) var cb = arguments[4], @@ -180,9 +197,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) { callback = function(err, bytesRead) { if (!cb) return; - var str = (bytesRead > 0) - ? buffer.toString(encoding, 0, bytesRead) - : ''; + var str = (bytesRead > 0) ? buffer.toString(encoding, 0, bytesRead) : ''; (cb)(err, str, bytesRead); }; @@ -191,7 +206,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) { binding.read(fd, buffer, offset, length, position, callback || noop); }; -fs.readSync = function (fd, buffer, offset, length, position) { +fs.readSync = function(fd, buffer, offset, length, position) { var legacy = false; if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, length, position, encoding, callback) @@ -209,19 +224,17 @@ fs.readSync = function (fd, buffer, offset, length, position) { return r; } - var str = (r > 0) - ? buffer.toString(encoding, 0, r) - : ''; + var str = (r > 0) ? buffer.toString(encoding, 0, r) : ''; return [str, r]; }; -fs.write = function (fd, buffer, offset, length, position, callback) { +fs.write = function(fd, buffer, offset, length, position, callback) { if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, data, position, encoding, callback) callback = arguments[4]; position = arguments[2]; - buffer = new Buffer(''+arguments[1], arguments[3]); + buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; length = buffer.length; } @@ -238,145 +251,145 @@ fs.write = function (fd, buffer, offset, length, position, callback) { binding.write(fd, buffer, offset, length, position, callback || noop); }; -fs.writeSync = function (fd, buffer, offset, length, position) { +fs.writeSync = function(fd, buffer, offset, length, position) { if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, data, position, encoding) position = arguments[2]; - buffer = new Buffer(''+arguments[1], arguments[3]); + buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; length = buffer.length; } - if(!length) return 0; + if (!length) return 0; return binding.write(fd, buffer, offset, length, position); }; -fs.rename = function (oldPath, newPath, callback) { +fs.rename = function(oldPath, newPath, callback) { binding.rename(oldPath, newPath, callback || noop); }; -fs.renameSync = function (oldPath, newPath) { +fs.renameSync = function(oldPath, newPath) { return binding.rename(oldPath, newPath); }; -fs.truncate = function (fd, len, callback) { +fs.truncate = function(fd, len, callback) { binding.truncate(fd, len, callback || noop); }; -fs.truncateSync = function (fd, len) { +fs.truncateSync = function(fd, len) { return binding.truncate(fd, len); }; -fs.rmdir = function (path, callback) { +fs.rmdir = function(path, callback) { binding.rmdir(path, callback || noop); }; -fs.rmdirSync = function (path) { +fs.rmdirSync = function(path) { return binding.rmdir(path); }; -fs.fdatasync = function (fd, callback) { +fs.fdatasync = function(fd, callback) { binding.fdatasync(fd, callback || noop); }; -fs.fdatasyncSync = function (fd) { +fs.fdatasyncSync = function(fd) { return binding.fdatasync(fd); }; -fs.fsync = function (fd, callback) { +fs.fsync = function(fd, callback) { binding.fsync(fd, callback || noop); }; -fs.fsyncSync = function (fd) { +fs.fsyncSync = function(fd) { return binding.fsync(fd); }; -fs.mkdir = function (path, mode, callback) { +fs.mkdir = function(path, mode, callback) { binding.mkdir(path, mode, callback || noop); }; -fs.mkdirSync = function (path, mode) { +fs.mkdirSync = function(path, mode) { return binding.mkdir(path, mode); }; -fs.sendfile = function (outFd, inFd, inOffset, length, callback) { +fs.sendfile = function(outFd, inFd, inOffset, length, callback) { binding.sendfile(outFd, inFd, inOffset, length, callback || noop); }; -fs.sendfileSync = function (outFd, inFd, inOffset, length) { +fs.sendfileSync = function(outFd, inFd, inOffset, length) { return binding.sendfile(outFd, inFd, inOffset, length); }; -fs.readdir = function (path, callback) { +fs.readdir = function(path, callback) { binding.readdir(path, callback || noop); }; -fs.readdirSync = function (path) { +fs.readdirSync = function(path) { return binding.readdir(path); }; -fs.fstat = function (fd, callback) { +fs.fstat = function(fd, callback) { binding.fstat(fd, callback || noop); }; -fs.lstat = function (path, callback) { +fs.lstat = function(path, callback) { binding.lstat(path, callback || noop); }; -fs.stat = function (path, callback) { +fs.stat = function(path, callback) { binding.stat(path, callback || noop); }; -fs.fstatSync = function (fd) { +fs.fstatSync = function(fd) { return binding.fstat(fd); }; -fs.lstatSync = function (path) { +fs.lstatSync = function(path) { return binding.lstat(path); }; -fs.statSync = function (path) { +fs.statSync = function(path) { return binding.stat(path); }; -fs.readlink = function (path, callback) { +fs.readlink = function(path, callback) { binding.readlink(path, callback || noop); }; -fs.readlinkSync = function (path) { +fs.readlinkSync = function(path) { return binding.readlink(path); }; -fs.symlink = function (destination, path, callback) { +fs.symlink = function(destination, path, callback) { binding.symlink(destination, path, callback || noop); }; -fs.symlinkSync = function (destination, path) { +fs.symlinkSync = function(destination, path) { return binding.symlink(destination, path); }; -fs.link = function (srcpath, dstpath, callback) { +fs.link = function(srcpath, dstpath, callback) { binding.link(srcpath, dstpath, callback || noop); }; -fs.linkSync = function (srcpath, dstpath) { +fs.linkSync = function(srcpath, dstpath) { return binding.link(srcpath, dstpath); }; -fs.unlink = function (path, callback) { +fs.unlink = function(path, callback) { binding.unlink(path, callback || noop); }; -fs.unlinkSync = function (path) { +fs.unlinkSync = function(path) { return binding.unlink(path); }; -fs.chmod = function (path, mode, callback) { +fs.chmod = function(path, mode, callback) { binding.chmod(path, mode, callback || noop); }; -fs.chmodSync = function (path, mode) { +fs.chmodSync = function(path, mode) { return binding.chmod(path, mode); }; @@ -388,28 +401,28 @@ fs.chownSync = function(path, uid, gid) { return binding.chown(path, uid, gid); }; -function writeAll (fd, buffer, offset, length, callback) { +function writeAll(fd, buffer, offset, length, callback) { // write(fd, buffer, offset, length, position, callback) - fs.write(fd, buffer, offset, length, offset, function (writeErr, written) { + fs.write(fd, buffer, offset, length, offset, function(writeErr, written) { if (writeErr) { - fs.close(fd, function () { + fs.close(fd, function() { if (callback) callback(writeErr); }); } else { if (written === length) { fs.close(fd, callback); } else { - writeAll(fd, buffer, offset+written, length-written, callback); + writeAll(fd, buffer, offset + written, length - written, callback); } } }); } -fs.writeFile = function (path, data, encoding_, callback) { +fs.writeFile = function(path, data, encoding_, callback) { var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); var callback_ = arguments[arguments.length - 1]; var callback = (typeof(callback_) == 'function' ? callback_ : null); - fs.open(path, 'w', 0666, function (openErr, fd) { + fs.open(path, 'w', 0666, function(openErr, fd) { if (openErr) { if (callback) callback(openErr); } else { @@ -419,16 +432,16 @@ fs.writeFile = function (path, data, encoding_, callback) { }); }; -fs.writeFileSync = function (path, data, encoding) { - var fd = fs.openSync(path, "w"); +fs.writeFileSync = function(path, data, encoding) { + var fd = fs.openSync(path, 'w'); if (!Buffer.isBuffer(data)) { - data = new Buffer(data, encoding || "utf8") + data = new Buffer(data, encoding || 'utf8'); } var written = 0; var length = data.length; //writeSync(fd, buffer, offset, length, position) while (written < length) { - written += fs.writeSync(fd, data, written, length-written, written); + written += fs.writeSync(fd, data, written, length - written, written); } fs.closeSync(fd); }; @@ -437,12 +450,12 @@ fs.writeFileSync = function (path, data, encoding) { var statWatchers = {}; -fs.watchFile = function (filename) { +fs.watchFile = function(filename) { var stat; var options; var listener; - if ("object" == typeof arguments[1]) { + if ('object' == typeof arguments[1]) { options = arguments[1]; listener = arguments[2]; } else { @@ -460,11 +473,11 @@ fs.watchFile = function (filename) { stat = statWatchers[filename]; stat.start(filename, options.persistent, options.interval); } - stat.addListener("change", listener); + stat.addListener('change', listener); return stat; }; -fs.unwatchFile = function (filename) { +fs.unwatchFile = function(filename) { var stat; if (statWatchers[filename]) { stat = statWatchers[filename]; @@ -484,7 +497,7 @@ var normalizeArray = path.normalizeArray; // See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html fs.realpathSync = realpathSync; fs.realpath = realpath; -function realpathSync (p) { +function realpathSync(p) { if (p.charAt(0) !== '/') { p = path.join(process.cwd(), p); } @@ -496,26 +509,31 @@ function realpathSync (p) { // values, and pushing non-link path bits onto the buffer. // then return the buffer. // NB: path.length changes. - for (var i = 0; i < p.length; i ++) { + for (var i = 0; i < p.length; i++) { // skip over empty path parts. if (p[i] === '') continue; + var part = path.join.apply(path, buf.concat(p[i])); + if (knownHard[part]) { - buf.push( p[i] ); + buf.push(p[i]); continue; } + var stat = fs.lstatSync(part); if (!stat.isSymbolicLink()) { // not a symlink. easy. - knownHard[ part ] = true; + knownHard[part] = true; buf.push(p[i]); continue; } - var id = stat.dev.toString(32)+':'+stat.ino.toString(32); + + var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); if (!seenLinks[id]) { fs.statSync(part); seenLinks[id] = fs.readlinkSync(part); } + var target = seenLinks[id]; if (target.charAt(0) === '/') { // absolute. Start over. @@ -524,9 +542,10 @@ function realpathSync (p) { i = -1; continue; } + // not absolute. join and splice. - if (i === 0 && p[i].charAt(0) === "/") { - target = "/"+target; + if (i === 0 && p[i].charAt(0) === '/') { + target = '/' + target; } target = path.split(target); Array.prototype.splice.apply(p, [i, 1].concat(target)); @@ -538,7 +557,7 @@ function realpathSync (p) { } -function realpath (p, cb) { +function realpath(p, cb) { if (p.charAt(0) !== '/') { p = path.join(process.cwd(), p); } @@ -552,37 +571,41 @@ function realpath (p, cb) { // NB: path.length changes. var i = -1; var part; + LOOP(); - function LOOP () { - i ++; + function LOOP() { + i++; if (!(i < p.length)) return exit(); // skip over empty path parts. if (p[i] === '') return process.nextTick(LOOP); - part = path.join(buf.join('/')+'/'+p[i]); + part = path.join(buf.join('/') + '/' + p[i]); if (knownHard[part]) { - buf.push( p[i] ); + buf.push(p[i]); return process.nextTick(LOOP); } return fs.lstat(part, gotStat); } - function gotStat (er, stat) { + + function gotStat(er, stat) { if (er) return cb(er); if (!stat.isSymbolicLink()) { // not a symlink. easy. - knownHard[ part ] = true; + knownHard[part] = true; buf.push(p[i]); return process.nextTick(LOOP); } - var id = stat.dev.toString(32)+':'+stat.ino.toString(32); + + var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); if (seenLinks[id]) return gotTarget(null, seenLinks[id]); - fs.stat(part, function (er) { - if (er) return cb(er) - fs.readlink(part, function (er, target) { + fs.stat(part, function(er) { + if (er) return cb(er); + fs.readlink(part, function(er, target) { gotTarget(er, seenLinks[id] = target); }); - }) + }); } - function gotTarget (er, target) { + + function gotTarget(er, target) { if (er) return cb(er); if (target.charAt(0) === '/') { // absolute. Start over. @@ -592,8 +615,8 @@ function realpath (p, cb) { return process.nextTick(LOOP); } // not absolute. join and splice. - if (i === 0 && p[i].charAt(0) === "/") { - target = "/"+target; + if (i === 0 && p[i].charAt(0) === '/') { + target = '/' + target; } target = path.split(target); Array.prototype.splice.apply(p, [i, 1].concat(target)); @@ -602,13 +625,15 @@ function realpath (p, cb) { buf = []; return process.nextTick(LOOP); } - function exit () { + + function exit() { cb(null, path.join(buf.join('/') || '/')); } } var pool; -function allocNewPool () { + +function allocNewPool() { pool = new Buffer(kPoolSize); pool.used = 0; } @@ -644,12 +669,12 @@ var ReadStream = fs.ReadStream = function(path, options) { this[key] = options[key]; } - if(this.encoding) this.setEncoding(this.encoding); + if (this.encoding) this.setEncoding(this.encoding); if (this.start !== undefined || this.end !== undefined) { if (this.start === undefined || this.end === undefined) { - this.emit('error', - new Error('Both start and end are needed for range streaming.')); + this.emit('error', new Error('Both start and end are needed ' + + 'for range streaming.')); } else if (this.start > this.end) { this.emit('error', new Error('start must be <= end')); } else { @@ -677,13 +702,13 @@ util.inherits(ReadStream, Stream); fs.FileReadStream = fs.ReadStream; // support the legacy name -ReadStream.prototype.setEncoding = function (encoding) { - var StringDecoder = require("string_decoder").StringDecoder; // lazy load +ReadStream.prototype.setEncoding = function(encoding) { + var StringDecoder = require('string_decoder').StringDecoder; // lazy load this._decoder = new StringDecoder(encoding); }; -ReadStream.prototype._read = function () { +ReadStream.prototype._read = function() { var self = this; if (!self.readable || self.paused) return; @@ -710,7 +735,7 @@ ReadStream.prototype._read = function () { toRead = Math.min(this.end - this.pos + 1, toRead); } - function afterRead (err, bytesRead) { + function afterRead(err, bytesRead) { if (err) { self.emit('error', err); self.readable = false; @@ -723,7 +748,7 @@ ReadStream.prototype._read = function () { return; } - var b = thisPool.slice(start, start+bytesRead); + var b = thisPool.slice(start, start + bytesRead); // Possible optimizition here? // Reclaim some bytes if bytesRead < toRead? @@ -751,7 +776,7 @@ ReadStream.prototype._read = function () { }; -ReadStream.prototype._emitData = function (d) { +ReadStream.prototype._emitData = function(d) { if (this._decoder) { var string = this._decoder.write(d); if (string.length) this.emit('data', string); @@ -761,7 +786,7 @@ ReadStream.prototype._emitData = function (d) { }; -ReadStream.prototype.destroy = function (cb) { +ReadStream.prototype.destroy = function(cb) { var self = this; this.readable = false; @@ -842,7 +867,7 @@ util.inherits(WriteStream, Stream); fs.FileWriteStream = fs.WriteStream; // support the legacy name -WriteStream.prototype.flush = function () { +WriteStream.prototype.flush = function() { if (this.busy) return; var self = this; @@ -900,7 +925,7 @@ WriteStream.prototype.flush = function () { method.apply(this, args); }; -WriteStream.prototype.write = function (data) { +WriteStream.prototype.write = function(data) { if (!this.writable) { throw new Error('stream not writable'); } @@ -908,8 +933,8 @@ WriteStream.prototype.write = function (data) { this.drainable = true; var cb; - if (typeof(arguments[arguments.length-1]) == 'function') { - cb = arguments[arguments.length-1]; + if (typeof(arguments[arguments.length - 1]) == 'function') { + cb = arguments[arguments.length - 1]; } if (Buffer.isBuffer(data)) { @@ -926,13 +951,13 @@ WriteStream.prototype.write = function (data) { return false; }; -WriteStream.prototype.end = function (cb) { +WriteStream.prototype.end = function(cb) { this.writable = false; this._queue.push([fs.close, cb]); this.flush(); }; -WriteStream.prototype.destroy = function (cb) { +WriteStream.prototype.destroy = function(cb) { var self = this; this.writable = false; |