Bug 857077 - Add ignoreAbsent option to OS.File.remove. r=yoric

This commit is contained in:
Birunthan Mohanathas 2013-07-01 09:26:12 -04:00
parent 688f174edb
commit 22e4a95eb5
3 changed files with 48 additions and 7 deletions

View File

@ -268,11 +268,21 @@
* Remove an existing file.
*
* @param {string} path The name of the file.
* @param {*=} options Additional options.
* - {bool} ignoreAbsent If |false|, throw an error if the file does
* not exist. |true| by default.
*
* @throws {OS.File.Error} In case of I/O error.
*/
File.remove = function remove(path) {
throw_on_negative("remove",
UnixFile.unlink(path)
);
File.remove = function remove(path, options = {}) {
let result = UnixFile.unlink(path);
if (result == -1) {
if ((!("ignoreAbsent" in options) || options.ignoreAbsent) &&
ctypes.errno == Const.ENOENT) {
return;
}
throw new File.Error("remove");
}
};
/**

View File

@ -329,11 +329,21 @@
* Remove an existing file.
*
* @param {string} path The name of the file.
* @param {*=} options Additional options.
* - {bool} ignoreAbsent If |false|, throw an error if the file does
* not exist. |true| by default.
*
* @throws {OS.File.Error} In case of I/O error.
*/
File.remove = function remove(path) {
throw_on_zero("remove",
WinFile.DeleteFile(path));
File.remove = function remove(path, options = {}) {
let result = WinFile.DeleteFile(path);
if (!result) {
if ((!("ignoreAbsent" in options) || options.ignoreAbsent) &&
ctypes.winLastError == Const.ERROR_FILE_NOT_FOUND) {
return;
}
throw new File.Error("remove");
}
};
/**

View File

@ -31,6 +31,7 @@ self.onmessage = function onmessage_start(msg) {
test_info();
test_path();
test_exists_file();
test_remove_file();
} catch (x) {
log("Catching error: " + x);
log("Stack: " + x.stack);
@ -819,3 +820,23 @@ function test_exists_file()
info("test_exists_file: complete");
}
/**
* Test the file |remove| method.
*/
function test_remove_file()
{
let absent_file_name = "test_osfile_front_absent.tmp";
// Check that removing absent files is handled correctly
let exn = should_throw(function() {
OS.File.remove(absent_file_name, {ignoreAbsent: false});
});
ok(!!exn, "test_remove_file: throws if there is no such file");
exn = should_throw(function() {
OS.File.remove(absent_file_name, {ignoreAbsent: true});
OS.File.remove(absent_file_name);
});
ok(!exn, "test_remove_file: ignoreAbsent works");
}