Browse Source

Rename binding reference in fs.js

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
3819920d77
  1. 231
      lib/fs.js

231
lib/fs.js

@ -1,9 +1,10 @@
var sys = require('sys'), var sys = require('sys'),
events = require('events'); events = require('events');
var fs = process.binding('fs'); var binding = process.binding('fs');
var fs = exports;
exports.Stats = fs.Stats; fs.Stats = binding.Stats;
fs.Stats.prototype._checkModeProperty = function (property) { fs.Stats.prototype._checkModeProperty = function (property) {
return ((this.mode & property) === property); return ((this.mode & property) === property);
@ -39,7 +40,7 @@ fs.Stats.prototype.isSocket = function () {
function readAll (fd, pos, content, encoding, callback) { function readAll (fd, pos, content, encoding, callback) {
fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) { binding.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) {
if (err) { if (err) {
if (callback) callback(err); if (callback) callback(err);
} else if (chunk) { } else if (chunk) {
@ -47,18 +48,18 @@ function readAll (fd, pos, content, encoding, callback) {
pos += bytesRead; pos += bytesRead;
readAll(fd, pos, content, encoding, callback); readAll(fd, pos, content, encoding, callback);
} else { } else {
fs.close(fd, function (err) { binding.close(fd, function (err) {
if (callback) callback(err, content); if (callback) callback(err, content);
}); });
} }
}); });
} }
exports.readFile = function (path, encoding_, callback) { fs.readFile = function (path, encoding_, callback) {
var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8'; var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8';
var callback_ = arguments[arguments.length - 1]; var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null); var callback = (typeof(callback_) == 'function' ? callback_ : null);
fs.open(path, process.O_RDONLY, 0666, function (err, fd) { binding.open(path, process.O_RDONLY, 0666, function (err, fd) {
if (err) { if (err) {
if (callback) callback(err); if (callback) callback(err);
} else { } else {
@ -67,26 +68,26 @@ exports.readFile = function (path, encoding_, callback) {
}); });
}; };
exports.readFileSync = function (path, encoding) { fs.readFileSync = function (path, encoding) {
encoding = encoding || "utf8"; // default to utf8 encoding = encoding || "utf8"; // default to utf8
var fd = fs.open(path, process.O_RDONLY, 0666); var fd = binding.open(path, process.O_RDONLY, 0666);
var content = ''; var content = '';
var pos = 0; var pos = 0;
var r; var r;
while ((r = fs.read(fd, 4*1024, pos, encoding)) && r[0]) { while ((r = binding.read(fd, 4*1024, pos, encoding)) && r[0]) {
content += r[0]; content += r[0];
pos += r[1] pos += r[1]
} }
fs.close(fd); binding.close(fd);
return content; return content;
}; };
// Used by fs.open and friends // Used by binding.open and friends
function stringToFlags(flag) { function stringToFlags(flag) {
// Only mess with strings // Only mess with strings
if (typeof flag !== 'string') { if (typeof flag !== 'string') {
@ -108,157 +109,157 @@ function noop () {}
// Yes, the follow could be easily DRYed up but I provide the explicit // Yes, the follow could be easily DRYed up but I provide the explicit
// list to make the arguments clear. // list to make the arguments clear.
exports.close = function (fd, callback) { fs.close = function (fd, callback) {
fs.close(fd, callback || noop); binding.close(fd, callback || noop);
}; };
exports.closeSync = function (fd) { fs.closeSync = function (fd) {
return fs.close(fd); return binding.close(fd);
}; };
exports.open = function (path, flags, mode, callback) { fs.open = function (path, flags, mode, callback) {
if (mode === undefined) { mode = 0666; } if (mode === undefined) { mode = 0666; }
fs.open(path, stringToFlags(flags), mode, callback || noop); binding.open(path, stringToFlags(flags), mode, callback || noop);
}; };
exports.openSync = function (path, flags, mode) { fs.openSync = function (path, flags, mode) {
if (mode === undefined) { mode = 0666; } if (mode === undefined) { mode = 0666; }
return fs.open(path, stringToFlags(flags), mode); return binding.open(path, stringToFlags(flags), mode);
}; };
exports.read = function (fd, length, position, encoding, callback) { fs.read = function (fd, length, position, encoding, callback) {
encoding = encoding || "binary"; encoding = encoding || "binary";
fs.read(fd, length, position, encoding, callback || noop); binding.read(fd, length, position, encoding, callback || noop);
}; };
exports.readSync = function (fd, length, position, encoding) { fs.readSync = function (fd, length, position, encoding) {
encoding = encoding || "binary"; encoding = encoding || "binary";
return fs.read(fd, length, position, encoding); return binding.read(fd, length, position, encoding);
}; };
exports.write = function (fd, data, position, encoding, callback) { fs.write = function (fd, data, position, encoding, callback) {
encoding = encoding || "binary"; encoding = encoding || "binary";
fs.write(fd, data, position, encoding, callback || noop); binding.write(fd, data, position, encoding, callback || noop);
}; };
exports.writeSync = function (fd, data, position, encoding) { fs.writeSync = function (fd, data, position, encoding) {
encoding = encoding || "binary"; encoding = encoding || "binary";
return fs.write(fd, data, position, encoding); return binding.write(fd, data, position, encoding);
}; };
exports.rename = function (oldPath, newPath, callback) { fs.rename = function (oldPath, newPath, callback) {
fs.rename(oldPath, newPath, callback || noop); binding.rename(oldPath, newPath, callback || noop);
}; };
exports.renameSync = function (oldPath, newPath) { fs.renameSync = function (oldPath, newPath) {
return fs.rename(oldPath, newPath); return binding.rename(oldPath, newPath);
}; };
exports.truncate = function (fd, len, callback) { fs.truncate = function (fd, len, callback) {
fs.truncate(fd, len, callback || noop); binding.truncate(fd, len, callback || noop);
}; };
exports.truncateSync = function (fd, len) { fs.truncateSync = function (fd, len) {
return fs.truncate(fd, len); return binding.truncate(fd, len);
}; };
exports.rmdir = function (path, callback) { fs.rmdir = function (path, callback) {
fs.rmdir(path, callback || noop); binding.rmdir(path, callback || noop);
}; };
exports.rmdirSync = function (path) { fs.rmdirSync = function (path) {
return fs.rmdir(path); return binding.rmdir(path);
}; };
exports.mkdir = function (path, mode, callback) { fs.mkdir = function (path, mode, callback) {
fs.mkdir(path, mode, callback || noop); binding.mkdir(path, mode, callback || noop);
}; };
exports.mkdirSync = function (path, mode) { fs.mkdirSync = function (path, mode) {
return fs.mkdir(path, mode); return binding.mkdir(path, mode);
}; };
exports.sendfile = function (outFd, inFd, inOffset, length, callback) { fs.sendfile = function (outFd, inFd, inOffset, length, callback) {
fs.sendfile(outFd, inFd, inOffset, length, callback || noop); binding.sendfile(outFd, inFd, inOffset, length, callback || noop);
}; };
exports.sendfileSync = function (outFd, inFd, inOffset, length) { fs.sendfileSync = function (outFd, inFd, inOffset, length) {
return fs.sendfile(outFd, inFd, inOffset, length); return binding.sendfile(outFd, inFd, inOffset, length);
}; };
exports.readdir = function (path, callback) { fs.readdir = function (path, callback) {
fs.readdir(path, callback || noop); binding.readdir(path, callback || noop);
}; };
exports.readdirSync = function (path) { fs.readdirSync = function (path) {
return fs.readdir(path); return binding.readdir(path);
}; };
exports.lstat = function (path, callback) { fs.lstat = function (path, callback) {
fs.lstat(path, callback || noop); binding.lstat(path, callback || noop);
}; };
exports.stat = function (path, callback) { fs.stat = function (path, callback) {
fs.stat(path, callback || noop); binding.stat(path, callback || noop);
}; };
exports.lstatSync = function (path) { fs.lstatSync = function (path) {
return fs.lstat(path); return binding.lstat(path);
}; };
exports.statSync = function (path) { fs.statSync = function (path) {
return fs.stat(path); return binding.stat(path);
}; };
exports.readlink = function (path, callback) { fs.readlink = function (path, callback) {
fs.readlink(path, callback || noop); binding.readlink(path, callback || noop);
}; };
exports.readlinkSync = function (path) { fs.readlinkSync = function (path) {
return fs.readlink(path); return binding.readlink(path);
}; };
exports.symlink = function (destination, path, callback) { fs.symlink = function (destination, path, callback) {
fs.symlink(destination, path, callback || noop); binding.symlink(destination, path, callback || noop);
}; };
exports.symlinkSync = function (destination, path) { fs.symlinkSync = function (destination, path) {
return fs.symlink(destination, path); return binding.symlink(destination, path);
}; };
exports.link = function (srcpath, dstpath, callback) { fs.link = function (srcpath, dstpath, callback) {
fs.link(srcpath, dstpath, callback || noop); binding.link(srcpath, dstpath, callback || noop);
}; };
exports.linkSync = function (srcpath, dstpath) { fs.linkSync = function (srcpath, dstpath) {
return fs.link(srcpath, dstpath); return binding.link(srcpath, dstpath);
}; };
exports.unlink = function (path, callback) { fs.unlink = function (path, callback) {
fs.unlink(path, callback || noop); binding.unlink(path, callback || noop);
}; };
exports.unlinkSync = function (path) { fs.unlinkSync = function (path) {
return fs.unlink(path); return binding.unlink(path);
}; };
exports.chmod = function (path, mode, callback) { fs.chmod = function (path, mode, callback) {
fs.chmod(path, mode, callback || noop); binding.chmod(path, mode, callback || noop);
}; };
exports.chmodSync = function (path, mode) { fs.chmodSync = function (path, mode) {
return fs.chmod(path, mode); return binding.chmod(path, mode);
}; };
function writeAll (fd, data, encoding, callback) { function writeAll (fd, data, encoding, callback) {
exports.write(fd, data, 0, encoding, function (writeErr, written) { fs.write(fd, data, 0, encoding, function (writeErr, written) {
if (writeErr) { if (writeErr) {
exports.close(fd, function () { fs.close(fd, function () {
if (callback) callback(writeErr); if (callback) callback(writeErr);
}); });
} else { } else {
if (written === data.length) { if (written === data.length) {
exports.close(fd, callback); fs.close(fd, callback);
} else { } else {
writeAll(fd, data.slice(written), encoding, callback); writeAll(fd, data.slice(written), encoding, callback);
} }
@ -266,11 +267,11 @@ function writeAll (fd, data, encoding, callback) {
}); });
} }
exports.writeFile = function (path, data, encoding_, callback) { fs.writeFile = function (path, data, encoding_, callback) {
var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
var callback_ = arguments[arguments.length - 1]; var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null); var callback = (typeof(callback_) == 'function' ? callback_ : null);
exports.open(path, 'w', 0666, function (openErr, fd) { fs.open(path, 'w', 0666, function (openErr, fd) {
if (openErr) { if (openErr) {
if (callback) callback(openErr); if (callback) callback(openErr);
} else { } else {
@ -279,23 +280,23 @@ exports.writeFile = function (path, data, encoding_, callback) {
}); });
}; };
exports.writeFileSync = function (path, data, encoding) { fs.writeFileSync = function (path, data, encoding) {
encoding = encoding || "utf8"; // default to utf8 encoding = encoding || "utf8"; // default to utf8
var fd = exports.openSync(path, "w"); var fd = fs.openSync(path, "w");
var written = 0; var written = 0;
while (written < data.length) { while (written < data.length) {
written += exports.writeSync(fd, data, 0, encoding); written += fs.writeSync(fd, data, 0, encoding);
data = data.slice(written); data = data.slice(written);
} }
exports.closeSync(fd); fs.closeSync(fd);
}; };
exports.cat = function () { fs.cat = function () {
throw new Error("fs.cat is deprecated. Please use fs.readFile instead."); throw new Error("fs.cat is deprecated. Please use fs.readFile instead.");
}; };
exports.catSync = function () { fs.catSync = function () {
throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead."); throw new Error("fs.catSync is deprecated. Please use fs.readFileSync instead.");
}; };
@ -303,7 +304,7 @@ exports.catSync = function () {
var statWatchers = {}; var statWatchers = {};
exports.watchFile = function (filename) { fs.watchFile = function (filename) {
var stat; var stat;
var options; var options;
var listener; var listener;
@ -322,7 +323,7 @@ exports.watchFile = function (filename) {
if (statWatchers[filename]) { if (statWatchers[filename]) {
stat = statWatchers[filename]; stat = statWatchers[filename];
} else { } else {
statWatchers[filename] = new fs.StatWatcher(); statWatchers[filename] = new binding.StatWatcher();
stat = statWatchers[filename]; stat = statWatchers[filename];
stat.start(filename, options.persistent, options.interval); stat.start(filename, options.persistent, options.interval);
} }
@ -330,7 +331,7 @@ exports.watchFile = function (filename) {
return stat; return stat;
}; };
exports.unwatchFile = function (filename) { fs.unwatchFile = function (filename) {
if (statWatchers[filename]) { if (statWatchers[filename]) {
stat = statWatchers[filename]; stat = statWatchers[filename];
stat.stop(); stat.stop();
@ -344,7 +345,7 @@ var path = require('path');
var normalize = path.normalize var normalize = path.normalize
normalizeArray = path.normalizeArray; normalizeArray = path.normalizeArray;
exports.realpathSync = function (path) { fs.realpathSync = function (path) {
var seen_links = {}, knownHards = {}, buf, i = 0, part, x, stats; var seen_links = {}, knownHards = {}, buf, i = 0, part, x, stats;
if (path.charAt(0) !== '/') { if (path.charAt(0) !== '/') {
var cwd = process.cwd().split('/'); var cwd = process.cwd().split('/');
@ -362,13 +363,13 @@ exports.realpathSync = function (path) {
if (part in knownHards) { if (part in knownHards) {
buf.push(path[i]); buf.push(path[i]);
} else { } else {
stats = exports.lstatSync(part); stats = fs.lstatSync(part);
if (stats.isSymbolicLink()) { if (stats.isSymbolicLink()) {
x = stats.dev.toString(32)+":"+stats.ino.toString(32); x = stats.dev.toString(32)+":"+stats.ino.toString(32);
if (x in seen_links) if (x in seen_links)
throw new Error("cyclic link at "+part); throw new Error("cyclic link at "+part);
seen_links[x] = true; seen_links[x] = true;
part = exports.readlinkSync(part); part = fs.readlinkSync(part);
if (part.charAt(0) === '/') { if (part.charAt(0) === '/') {
// absolute // absolute
path = normalizeArray(part.split('/')); path = normalizeArray(part.split('/'));
@ -400,7 +401,7 @@ exports.realpathSync = function (path) {
} }
exports.realpath = function (path, callback) { fs.realpath = function (path, callback) {
var seen_links = {}, knownHards = {}, buf = [''], i = 0, part, x; var seen_links = {}, knownHards = {}, buf = [''], i = 0, part, x;
if (path.charAt(0) !== '/') { if (path.charAt(0) !== '/') {
// assumes cwd is canonical // assumes cwd is canonical
@ -426,14 +427,14 @@ exports.realpath = function (path, callback) {
buf.push(path[i]); buf.push(path[i]);
next(); next();
} else { } else {
exports.lstat(part, function(err, stats){ fs.lstat(part, function(err, stats){
if (err) return done(err); if (err) return done(err);
if (stats.isSymbolicLink()) { if (stats.isSymbolicLink()) {
x = stats.dev.toString(32)+":"+stats.ino.toString(32); x = stats.dev.toString(32)+":"+stats.ino.toString(32);
if (x in seen_links) if (x in seen_links)
return done(new Error("cyclic link at "+part)); return done(new Error("cyclic link at "+part));
seen_links[x] = true; seen_links[x] = true;
exports.readlink(part, function(err, npart){ fs.readlink(part, function(err, npart){
if (err) return done(err); if (err) return done(err);
part = npart; part = npart;
if (part.charAt(0) === '/') { if (part.charAt(0) === '/') {
@ -458,24 +459,24 @@ exports.realpath = function (path, callback) {
} }
} }
next(); next();
}); // fs.readlink }); // binding.readlink
} }
else { else {
buf.push(path[i]); buf.push(path[i]);
knownHards[buf.join('/')] = true; knownHards[buf.join('/')] = true;
next(); next();
} }
}); // fs.lstat }); // binding.lstat
} }
} }
next(); next();
} }
exports.createReadStream = function(path, options) { fs.createReadStream = function(path, options) {
return new FileReadStream(path, options); return new FileReadStream(path, options);
}; };
var FileReadStream = exports.FileReadStream = function(path, options) { var FileReadStream = fs.FileReadStream = function(path, options) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.path = path; this.path = path;
@ -500,7 +501,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
return; return;
} }
exports.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) { fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
self.readable = false; self.readable = false;
@ -529,7 +530,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
}); });
} }
exports.open(this.path, this.flags, this.mode, function(err, fd) { fs.open(this.path, this.flags, this.mode, function(err, fd) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
self.readable = false; self.readable = false;
@ -545,7 +546,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
this.readable = false; this.readable = false;
function close() { function close() {
exports.close(self.fd, function(err) { fs.close(self.fd, function(err) {
if (err) { if (err) {
if (cb) { if (cb) {
cb(err); cb(err);
@ -585,11 +586,11 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
}; };
sys.inherits(FileReadStream, events.EventEmitter); sys.inherits(FileReadStream, events.EventEmitter);
exports.createWriteStream = function(path, options) { fs.createWriteStream = function(path, options) {
return new FileWriteStream(path, options); return new FileWriteStream(path, options);
}; };
var FileWriteStream = exports.FileWriteStream = function(path, options) { var FileWriteStream = fs.FileWriteStream = function(path, options) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
this.path = path; this.path = path;
@ -608,7 +609,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
queue = [], queue = [],
busy = false; busy = false;
queue.push([exports.open, this.path, this.flags, this.mode, undefined]); queue.push([fs.open, this.path, this.flags, this.mode, undefined]);
function flush() { function flush() {
if (busy) { if (busy) {
@ -639,7 +640,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
} }
// stop flushing after close // stop flushing after close
if (method === exports.close) { if (method === fs.close) {
if (cb) { if (cb) {
cb(null); cb(null);
} }
@ -648,7 +649,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
} }
// save reference for file pointer // save reference for file pointer
if (method === exports.open) { if (method === fs.open) {
self.fd = arguments[1]; self.fd = arguments[1];
self.emit('open', self.fd); self.emit('open', self.fd);
} else if (cb) { } else if (cb) {
@ -660,7 +661,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
}); });
// Inject the file pointer // Inject the file pointer
if (method !== exports.open) { if (method !== fs.open) {
args.unshift(self.fd); args.unshift(self.fd);
} }
@ -672,20 +673,20 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
throw new Error('stream not writeable'); throw new Error('stream not writeable');
} }
queue.push([exports.write, data, undefined, this.encoding, cb]); queue.push([fs.write, data, undefined, this.encoding, cb]);
flush(); flush();
return false; return false;
}; };
this.close = function(cb) { this.close = function(cb) {
this.writeable = false; this.writeable = false;
queue.push([exports.close, cb]); queue.push([fs.close, cb]);
flush(); flush();
}; };
this.forceClose = function(cb) { this.forceClose = function(cb) {
this.writeable = false; this.writeable = false;
exports.close(self.fd, function(err) { fs.close(self.fd, function(err) {
if (err) { if (err) {
if (cb) { if (cb) {
cb(err); cb(err);

Loading…
Cancel
Save