Bug 1443651: Don't return failure for access denied from createUnique() if file exists. r=Mossop

There are ways for Create() to return access denied for files which already
exist, particularly in the case of locked files. When it does, createUnique()
should check whether the file exists before considering the attempt a failure.

MozReview-Commit-ID: FyJTghk04jH

--HG--
extra : rebase_source : 10e6f2cb7da18e8e6d410b52a593b7455f0d76fa
This commit is contained in:
Kris Maglione 2018-03-06 14:32:40 -08:00
parent 2faddce683
commit afc3918d22

View File

@ -61,11 +61,19 @@ nsLocalFile::CreateUnique(uint32_t aType, uint32_t aAttributes)
return rv;
}
auto FailedBecauseExists = [&] (nsresult aRv) {
if (aRv == NS_ERROR_FILE_ACCESS_DENIED) {
bool exists;
return NS_SUCCEEDED(Exists(&exists)) && exists;
}
return aRv == NS_ERROR_FILE_ALREADY_EXISTS;
};
longName = (pathName.Length() + kMaxSequenceNumberLength >
kMaxFilenameLength);
if (!longName) {
rv = Create(aType, aAttributes);
if (rv != NS_ERROR_FILE_ALREADY_EXISTS) {
if (!FailedBecauseExists(rv)) {
return rv;
}
}
@ -128,7 +136,7 @@ nsLocalFile::CreateUnique(uint32_t aType, uint32_t aAttributes)
SetNativeLeafName(rootName + suffix);
#endif
nsresult rvCreate = Create(aType, aAttributes);
if (rvCreate != NS_ERROR_FILE_ALREADY_EXISTS) {
if (!FailedBecauseExists(rvCreate)) {
return rvCreate;
}
}
@ -143,7 +151,7 @@ nsLocalFile::CreateUnique(uint32_t aType, uint32_t aAttributes)
SetNativeLeafName(rootName + nsPrintfCString("-%d", indx) + suffix);
#endif
rv = Create(aType, aAttributes);
if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS) {
if (NS_SUCCEEDED(rv) || !FailedBecauseExists(rv)) {
return rv;
}
}