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

Loading…
Cancel
Save