From 9e63838884aaf577c7591012c6a342b3b2043782 Mon Sep 17 00:00:00 2001 From: Tom Tung Date: Wed, 2 Oct 2019 10:38:55 +0000 Subject: [PATCH] Bug 1581067 - P5 - Only remove the temporary body files & body directories when it's in the initializing stage; r=asuth Differential Revision: https://phabricator.services.mozilla.com/D47770 --HG-- extra : moz-landing-system : lando --- dom/cache/FileUtils.cpp | 33 ++++++++++++++++++--------------- dom/cache/FileUtils.h | 6 ++++++ dom/cache/QuotaClient.cpp | 6 ++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/dom/cache/FileUtils.cpp b/dom/cache/FileUtils.cpp index ae1fe06ec634..f722047d6ed8 100644 --- a/dom/cache/FileUtils.cpp +++ b/dom/cache/FileUtils.cpp @@ -389,7 +389,9 @@ nsresult BodyDeleteFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir, fileDeleted = false; return NS_OK; }; - rv = BodyTraverseFiles(aQuotaInfo, bodyDir, removeFileForId); + rv = BodyTraverseFiles(aQuotaInfo, bodyDir, removeFileForId, + /* aCanRemoveFiles */ false, + /* aTrackQuota */ true); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -559,7 +561,9 @@ nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir, fileDeleted = false; return NS_OK; }; - rv = BodyTraverseFiles(aQuotaInfo, subdir, removeOrphanedFiles); + rv = BodyTraverseFiles(aQuotaInfo, subdir, removeOrphanedFiles, + /* aCanRemoveFiles */ true, + /* aTrackQuota */ true); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -574,7 +578,7 @@ nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir, template nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBodyDir, const Func& aHandleFileFunc, - const bool aTrackQuota) { + const bool aCanRemoveFiles, const bool aTrackQuota) { MOZ_DIAGNOSTIC_ASSERT(aBodyDir); nsresult rv; @@ -625,17 +629,16 @@ nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBodyDir, // Delete all tmp files regardless of known bodies. These are all // considered orphans. if (StringEndsWith(leafName, NS_LITERAL_CSTRING(".tmp"))) { - DebugOnly result = RemoveNsIFile(aQuotaInfo, file, aTrackQuota); - MOZ_ASSERT(NS_SUCCEEDED(result)); - continue; - } - - nsCString suffix(NS_LITERAL_CSTRING(".final")); - - // Otherwise, it must be a .final file. If its not, then try to remove it - // and move on - if (NS_WARN_IF(!StringEndsWith(leafName, suffix) || - leafName.Length() != NSID_LENGTH - 1 + suffix.Length())) { + if (aCanRemoveFiles) { + DebugOnly result = + RemoveNsIFile(aQuotaInfo, file, aTrackQuota); + MOZ_ASSERT(NS_SUCCEEDED(result)); + continue; + } + } else if (NS_WARN_IF( + !StringEndsWith(leafName, NS_LITERAL_CSTRING(".final")))) { + // Otherwise, it must be a .final file. If its not, then try to remove it + // and move on DebugOnly result = RemoveNsIFile(aQuotaInfo, file, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); @@ -657,7 +660,7 @@ nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBodyDir, return rv; } - if (isEmpty) { + if (isEmpty && aCanRemoveFiles) { DebugOnly result = RemoveNsIFileRecursively(aQuotaInfo, aBodyDir, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); diff --git a/dom/cache/FileUtils.h b/dom/cache/FileUtils.h index bf15b6f1f5e9..e4ddfe29b317 100644 --- a/dom/cache/FileUtils.h +++ b/dom/cache/FileUtils.h @@ -58,9 +58,15 @@ nsresult BodyDeleteFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir, nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBaseDir, nsTArray& aKnownBodyIdList); +// If aCanRemoveFiles is true, that means we are safe to touch the files which +// can be accessed in other threads. +// If it's not, that means we cannot remove the files which are possible to +// created by other threads. Note that if the files are not expected, we should +// be safe to remove them in any case. template nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile* aBodyDir, const Func& aHandleFileFunc, + const bool aCanRemoveFiles, const bool aTrackQuota = true); nsresult CreateMarkerFile(const QuotaInfo& aQuotaInfo); diff --git a/dom/cache/QuotaClient.cpp b/dom/cache/QuotaClient.cpp index 917fc1912f16..e1059a71f8dc 100644 --- a/dom/cache/QuotaClient.cpp +++ b/dom/cache/QuotaClient.cpp @@ -37,7 +37,7 @@ using mozilla::dom::quota::UsageInfo; using mozilla::ipc::AssertIsOnBackgroundThread; static nsresult GetBodyUsage(nsIFile* aMorgueDir, const Atomic& aCanceled, - UsageInfo* aUsageInfo) { + UsageInfo* aUsageInfo, const bool aInitializing) { AssertIsOnIOThread(); nsCOMPtr entries; @@ -88,6 +88,8 @@ static nsresult GetBodyUsage(nsIFile* aMorgueDir, const Atomic& aCanceled, return NS_OK; }; rv = mozilla::dom::cache::BodyTraverseFiles(dummy, bodyDir, getUsage, + /* aCanRemoveFiles */ + aInitializing, /* aTrackQuota */ false); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; @@ -474,7 +476,7 @@ class CacheQuotaClient final : public Client { if (isDir) { if (leafName.EqualsLiteral("morgue")) { - rv = GetBodyUsage(file, aCanceled, aUsageInfo); + rv = GetBodyUsage(file, aCanceled, aUsageInfo, aInitializing); if (NS_WARN_IF(NS_FAILED(rv))) { if (rv != NS_ERROR_ABORT) { REPORT_TELEMETRY_ERR_IN_INIT(aInitializing, kQuotaExternalError,