rmdir should error on non-directories
This commit is contained in:
parent
635dd2e899
commit
656dfd242b
45
src/fs.js
45
src/fs.js
|
@ -332,13 +332,34 @@ define(function(require) {
|
||||||
var parentDirectoryNode;
|
var parentDirectoryNode;
|
||||||
var parentDirectoryData;
|
var parentDirectoryData;
|
||||||
|
|
||||||
function check_if_directory_exists(error, result) {
|
function read_parent_directory_data(error, result) {
|
||||||
|
if(error) {
|
||||||
|
callback(error);
|
||||||
|
} else {
|
||||||
|
parentDirectoryNode = result;
|
||||||
|
read_object(objectStore, parentDirectoryNode.data, check_if_node_exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_if_node_exists(error, result) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else if(ROOT_DIRECTORY_NAME == name) {
|
} else if(ROOT_DIRECTORY_NAME == name) {
|
||||||
callback(new EBusy());
|
callback(new EBusy());
|
||||||
} else if(!result) {
|
} else if(!_(result).has(name)) {
|
||||||
callback(new ENoEntry());
|
callback(new ENoEntry());
|
||||||
|
} else {
|
||||||
|
parentDirectoryData = result;
|
||||||
|
directoryNode = parentDirectoryData[name].id;
|
||||||
|
read_object(objectStore, directoryNode, check_if_node_is_directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_if_node_is_directory(error, result) {
|
||||||
|
if(error) {
|
||||||
|
callback(error);
|
||||||
|
} else if(result.mode != MODE_DIRECTORY) {
|
||||||
|
callback(new ENotDirectory());
|
||||||
} else {
|
} else {
|
||||||
directoryNode = result;
|
directoryNode = result;
|
||||||
read_object(objectStore, directoryNode.data, check_if_directory_is_empty);
|
read_object(objectStore, directoryNode.data, check_if_directory_is_empty);
|
||||||
|
@ -353,29 +374,15 @@ define(function(require) {
|
||||||
if(_(directoryData).size() > 0) {
|
if(_(directoryData).size() > 0) {
|
||||||
callback(new ENotEmpty());
|
callback(new ENotEmpty());
|
||||||
} else {
|
} else {
|
||||||
find_node(objectStore, parentPath, read_parent_directory_data);
|
remove_directory_entry_from_parent_directory_node();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function read_parent_directory_data(error, result) {
|
function remove_directory_entry_from_parent_directory_node() {
|
||||||
if(error) {
|
|
||||||
callback(error);
|
|
||||||
} else {
|
|
||||||
parentDirectoryNode = result;
|
|
||||||
read_object(objectStore, parentDirectoryNode.data, remove_directory_entry_from_parent_directory_node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function remove_directory_entry_from_parent_directory_node(error, result) {
|
|
||||||
if(error) {
|
|
||||||
callback(error);
|
|
||||||
} else {
|
|
||||||
parentDirectoryData = result;
|
|
||||||
delete parentDirectoryData[name];
|
delete parentDirectoryData[name];
|
||||||
write_object(objectStore, parentDirectoryData, parentDirectoryNode.data, remove_directory_node);
|
write_object(objectStore, parentDirectoryData, parentDirectoryNode.data, remove_directory_node);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function remove_directory_node(error) {
|
function remove_directory_node(error) {
|
||||||
if(error) {
|
if(error) {
|
||||||
|
@ -393,7 +400,7 @@ define(function(require) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
find_node(objectStore, path, check_if_directory_exists);
|
find_node(objectStore, parentPath, read_parent_directory_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function open_file(fs, objectStore, path, flags, callback) {
|
function open_file(fs, objectStore, path, flags, callback) {
|
||||||
|
|
|
@ -578,6 +578,56 @@ describe('fs.rmdir', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return an error if the path is not a directory', function() {
|
||||||
|
var complete = false;
|
||||||
|
var _error;
|
||||||
|
var that = this;
|
||||||
|
|
||||||
|
that.fs.mkdir('/tmp', function(error) {
|
||||||
|
that.fs.open('/tmp/myfile', 'w', function(error, fd) {
|
||||||
|
that.fs.close(fd, function(error) {
|
||||||
|
that.fs.rmdir('/tmp/myfile', function(error) {
|
||||||
|
_error = error;
|
||||||
|
|
||||||
|
complete = true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
waitsFor(function() {
|
||||||
|
return complete;
|
||||||
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
|
runs(function() {
|
||||||
|
expect(_error).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an error if the path is a symbolic link', function () {
|
||||||
|
var complete = false;
|
||||||
|
var _error;
|
||||||
|
var that = this;
|
||||||
|
|
||||||
|
that.fs.mkdir('/tmp', function (error) {
|
||||||
|
that.fs.symlink('/tmp', '/tmp/myfile', function (error) {
|
||||||
|
that.fs.rmdir('/tmp/myfile', function (error) {
|
||||||
|
_error = error;
|
||||||
|
|
||||||
|
complete = true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
waitsFor(function () {
|
||||||
|
return complete;
|
||||||
|
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(_error).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should remove an existing directory', function() {
|
it('should remove an existing directory', function() {
|
||||||
var complete = false;
|
var complete = false;
|
||||||
var _error, _stat;
|
var _error, _stat;
|
||||||
|
|
Loading…
Reference in New Issue