Fix in readFile only, stop leaking descriptors in readFile

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-08-18 11:03:46 -04:00
parent 1a2774b152
commit 2590a886ac
1 changed files with 28 additions and 11 deletions

View File

@ -573,7 +573,11 @@ function open_file(context, path, flags, callback) {
var followedCount = 0; var followedCount = 0;
if(ROOT_DIRECTORY_NAME == name) { if(ROOT_DIRECTORY_NAME == name) {
callback(new Errors.EISDIR('the named file is the root directory')); if(_(flags).contains(O_WRITE)) {
callback(new Errors.EISDIR('the named file is a directory and O_WRITE is set'));
} else {
find_node(context, path, set_file_node);
}
} else { } else {
find_node(context, parentPath, read_directory_data); find_node(context, parentPath, read_directory_data);
} }
@ -599,8 +603,8 @@ function open_file(context, path, flags, callback) {
callback(new Errors.ENOENT('O_CREATE and O_EXCLUSIVE are set, and the named file exists')); callback(new Errors.ENOENT('O_CREATE and O_EXCLUSIVE are set, and the named file exists'));
} else { } else {
directoryEntry = directoryData[name]; directoryEntry = directoryData[name];
if(directoryEntry.type == MODE_DIRECTORY) { if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) {
callback(new Errors.EISDIR('the named file is a directory')); callback(new Errors.EISDIR('the named file is a directory and O_WRITE is set'));
} else { } else {
context.getObject(directoryEntry.id, check_if_symbolic_link); context.getObject(directoryEntry.id, check_if_symbolic_link);
} }
@ -1687,7 +1691,7 @@ function readFile(fs, context, path, options, callback) {
var flags = validate_flags(options.flag || 'r'); var flags = validate_flags(options.flag || 'r');
if(!flags) { if(!flags) {
callback(new Errors.EINVAL('flags is not valid')); return callback(new Errors.EINVAL('flags is not valid'));
} }
open_file(context, path, flags, function(err, fileNode) { open_file(context, path, flags, function(err, fileNode) {
@ -1697,21 +1701,34 @@ function readFile(fs, context, path, options, callback) {
var ofd = new OpenFileDescription(path, fileNode.id, flags, 0); var ofd = new OpenFileDescription(path, fileNode.id, flags, 0);
var fd = fs.allocDescriptor(ofd); var fd = fs.allocDescriptor(ofd);
fstat_file(context, ofd, function(err2, fstatResult) { function cleanup() {
if(err2) { fs.releaseDescriptor(fd);
return callback(err2); ofd = null;
}
fstat_file(context, ofd, function(err, fstatResult) {
if(err) {
cleanup();
return callback(err);
} }
var stats = new Stats(fstatResult, fs.name); var stats = new Stats(fstatResult, fs.name);
if(stats.isDirectory()) {
cleanup();
return callback(new Errors.EISDIR('illegal operation on directory'));
}
var size = stats.size; var size = stats.size;
var buffer = new Buffer(size); var buffer = new Buffer(size);
buffer.fill(0); buffer.fill(0);
read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) { read_data(context, ofd, buffer, 0, size, 0, function(err, nbytes) {
if(err3) { cleanup();
return callback(err3);
if(err) {
return callback(err);
} }
fs.releaseDescriptor(fd);
var data; var data;
if(options.encoding === 'utf8') { if(options.encoding === 'utf8') {