Bug 1548770 - File.createFile() should not assume that the file doesn't exist, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 09 May 2019 19:26:53 +0000
changeset 532170 47d40e2693bd341147547867b9458a603a7e9bd7
parent 532169 b0a2912f83a9bc268e8f1f0185608d1ccee22b55
child 532171 d4a9de6a57b71939eb1b155a14de17c58387f226
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1548770
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1548770 - File.createFile() should not assume that the file doesn't exist, r=smaug Differential Revision: https://phabricator.services.mozilla.com/D30534
dom/file/ipc/FileCreatorParent.cpp
dom/file/tests/test_createFile.js
dom/file/tests/xpcshell.ini
--- a/dom/file/ipc/FileCreatorParent.cpp
+++ b/dom/file/ipc/FileCreatorParent.cpp
@@ -75,23 +75,23 @@ nsresult FileCreatorParent::CreateBlobIm
   MOZ_ASSERT(!NS_IsMainThread());
 
   nsCOMPtr<nsIFile> file;
   nsresult rv = NS_NewLocalFile(aPath, true, getter_AddRefs(file));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
+  bool exists;
+  rv = file->Exists(&exists);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
   if (aExistenceCheck) {
-    bool exists;
-    nsresult rv = file->Exists(&exists);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
     if (!exists) {
       return NS_ERROR_FILE_NOT_FOUND;
     }
 
     bool isDir;
     rv = file->IsDirectory(&isDir);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
@@ -101,17 +101,19 @@ nsresult FileCreatorParent::CreateBlobIm
       return NS_ERROR_FILE_IS_DIRECTORY;
     }
   }
 
   RefPtr<FileBlobImpl> impl = new FileBlobImpl(file);
 
   // If the file doesn't exist, we cannot have its path, its size and so on.
   // Let's set them now.
-  if (!aExistenceCheck) {
+  if (!exists) {
+    MOZ_ASSERT(!aExistenceCheck);
+
     impl->SetMozFullPath(aPath);
     impl->SetLastModified(0);
     impl->SetEmptySize();
   }
 
   if (!aName.IsEmpty()) {
     impl->SetName(aName);
   }
new file mode 100644
--- /dev/null
+++ b/dom/file/tests/test_createFile.js
@@ -0,0 +1,31 @@
+async function run_test() {
+  const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+  do_get_profile();
+
+  let existingFile = Services.dirsvc.QueryInterface(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
+  existingFile.append("exists.js");
+  existingFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
+
+  var outStream = Cc["@mozilla.org/network/file-output-stream;1"]
+                      .createInstance(Ci.nsIFileOutputStream);
+  outStream.init(existingFile, 0x02 | 0x08 | 0x20, // write, create, truncate
+                 0666, 0);
+
+  var fileData = "Hello World!";
+  outStream.write(fileData, fileData.length);
+  outStream.close();
+
+  ok(existingFile.exists(), "exists.js exists");
+
+  let unknownFile = Services.dirsvc.QueryInterface(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
+  unknownFile.append("wow.txt");
+
+  ok(!unknownFile.exists(), unknownFile.path + " doesn't exist");
+
+  let a = await File.createFromNsIFile(existingFile, { existenceCheck: false });
+  ok(a.size != 0, "The size is correctly set");
+
+  let b = await File.createFromNsIFile(unknownFile, { existenceCheck: false });
+  ok(b.size == 0, "The size is 0 for unknown file");
+}
--- a/dom/file/tests/xpcshell.ini
+++ b/dom/file/tests/xpcshell.ini
@@ -1,3 +1,4 @@
 [DEFAULT]
 
 [test_bloburi.js]
+[test_createFile.js]