Bug 1548770 - File.createFile() should not assume that the file doesn't exist, r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D30534

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrea Marchesini 2019-05-09 19:26:53 +00:00
Родитель ad027dc05a
Коммит 3d080cfc18
3 изменённых файлов: 41 добавлений и 7 удалений

Просмотреть файл

@ -80,13 +80,13 @@ nsresult FileCreatorParent::CreateBlobImpl(
return rv;
}
if (aExistenceCheck) {
bool exists;
nsresult rv = file->Exists(&exists);
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) {
if (!exists) {
return NS_ERROR_FILE_NOT_FOUND;
}
@ -106,7 +106,9 @@ nsresult FileCreatorParent::CreateBlobImpl(
// 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();

Просмотреть файл

@ -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");
}

Просмотреть файл

@ -1,3 +1,4 @@
[DEFAULT]
[test_bloburi.js]
[test_createFile.js]