diff --git a/dom/system/IOUtils.cpp b/dom/system/IOUtils.cpp index 2314a008b954..26183864f698 100644 --- a/dom/system/IOUtils.cpp +++ b/dom/system/IOUtils.cpp @@ -932,7 +932,7 @@ Result IOUtils::WriteSync( nsCOMPtr toMove; MOZ_ALWAYS_SUCCEEDS(aFile->Clone(getter_AddRefs(toMove))); - bool noOverwrite = aOptions.mMode != WriteMode::Create; + bool noOverwrite = aOptions.mMode == WriteMode::Create; if (MoveSync(toMove, backupFile, noOverwrite).isErr()) { return Err(IOError(NS_ERROR_FILE_COPY_OR_MOVE_FAILED) diff --git a/dom/system/tests/ioutils/test_ioutils_read_write.html b/dom/system/tests/ioutils/test_ioutils_read_write.html index 5b72a7b655ef..6726e8e25b5e 100644 --- a/dom/system/tests/ioutils/test_ioutils_read_write.html +++ b/dom/system/tests/ioutils/test_ioutils_read_write.html @@ -153,6 +153,7 @@ info("Test backup with tmp and backup file options, existing destination"); let newFileContents = new TextEncoder().encode("New file contents"); + ok(await fileExists(destFileName), `Expected ${destFileName} to exist`); bytesWritten = await IOUtils.write(destFileName, newFileContents, { backupFile: backupFileName, @@ -174,6 +175,31 @@ "IOUtils::write IOUtils::write can move tmp file to destination after performing a backup" ); + info("Test backup with tmp and backup file options, existing destination and backup"); + newFileContents = new TextEncoder().encode("Updated new file contents"); + ok(await fileExists(destFileName), `Expected ${destFileName} to exist`); + ok(await fileExists(backupFileName), `Expected ${backupFileName} to exist`); + bytesWritten = + await IOUtils.write(destFileName, newFileContents, { + backupFile: backupFileName, + tmpPath: tmpFileName, + }); + + ok(!await fileExists(tmpFileName), "IOUtils::write cleans up the tmpFile"); + ok( + await fileHasTextContents(backupFileName, "New file contents"), + "IOUtils::write can create a backup if the target file exists" + ); + ok( + await fileHasTextContents(destFileName, "Updated new file contents"), + "IOUtils::write can write to the destination when a temporary file is used" + ); + is( + bytesWritten, + newFileContents.length, + "IOUtils::write IOUtils::write can move tmp file to destination after performing a backup" + ); + await cleanup(destFileName, backupFileName); });