Bug 1303693 - OS.File.remove(filename, { ignoreAbsent: true }) throws when the parent directory doesn't exist. r?yoric draft
authorMark Hammond <mhammond@skippinet.com.au>
Tue, 20 Sep 2016 10:22:15 +1000
changeset 415244 5151ce8a5494a9d8afbd3b3ac08081c6241b92f2
parent 415058 c80b283448d3b1266f18f7ea0eaccb9692cc135c
child 531579 10b1785cd73a345100656648eb01757b9dac959b
push id29834
push userbmo:markh@mozilla.com
push dateTue, 20 Sep 2016 00:48:38 +0000
reviewersyoric
bugs1303693
milestone51.0a1
Bug 1303693 - OS.File.remove(filename, { ignoreAbsent: true }) throws when the parent directory doesn't exist. r?yoric MozReview-Commit-ID: HJml1cf430y
toolkit/components/osfile/modules/osfile_win_front.jsm
toolkit/components/osfile/tests/xpcshell/test_remove.js
--- a/toolkit/components/osfile/modules/osfile_win_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_win_front.jsm
@@ -421,17 +421,18 @@
       *
       * @throws {OS.File.Error} In case of I/O error.
       */
      File.remove = function remove(path, options = {}) {
        if (WinFile.DeleteFile(path)) {
          return;
        }
 
-       if (ctypes.winLastError == Const.ERROR_FILE_NOT_FOUND) {
+       if (ctypes.winLastError == Const.ERROR_FILE_NOT_FOUND ||
+           ctypes.winLastError == Const.ERROR_PATH_NOT_FOUND) {
          if ((!("ignoreAbsent" in options) || options.ignoreAbsent)) {
            return;
          }
        } else if (ctypes.winLastError == Const.ERROR_ACCESS_DENIED) {
          // Save winLastError before another ctypes call.
          let lastError = ctypes.winLastError;
          let attributes = WinFile.GetFileAttributes(path);
          if (attributes != Const.INVALID_FILE_ATTRIBUTES) {
--- a/toolkit/components/osfile/tests/xpcshell/test_remove.js
+++ b/toolkit/components/osfile/tests/xpcshell/test_remove.js
@@ -30,8 +30,27 @@ add_task(function* test_ignoreAbsent() {
   try {
     yield OS.File.remove(absent_file_name, {ignoreAbsent: true});
     yield OS.File.remove(absent_file_name);
   } catch (ex) {
     exception = ex;
   }
   Assert.ok(!exception, "OS.File.remove should not throw when not requested.");
 });
+
+add_task(function* test_ignoreAbsent_directory_missing() {
+  let absent_file_name = OS.Path.join("absent_parent", "test.tmp");
+
+  // Removing absent files should throw if "ignoreAbsent" is true.
+  yield Assert.rejects(OS.File.remove(absent_file_name, {ignoreAbsent: false}),
+                       "OS.File.remove throws if there is no such file.");
+
+  // Removing files from absent directories should not throw if "ignoreAbsent"
+  // is true or not defined.
+  let exception = null;
+  try {
+    yield OS.File.remove(absent_file_name, {ignoreAbsent: true});
+    yield OS.File.remove(absent_file_name);
+  } catch (ex) {
+    exception = ex;
+  }
+  Assert.ok(!exception, "OS.File.remove should not throw when not requested.");
+});