2020-04-15 02:51:44 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_cache_FileUtilsImpl_h
|
|
|
|
#define mozilla_dom_cache_FileUtilsImpl_h
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
#include "mozilla/dom/FlippedOnce.h"
|
2020-04-15 02:51:44 +03:00
|
|
|
#include "mozilla/dom/cache/FileUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
|
|
|
template <typename Func>
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBodyDir,
|
2020-04-15 02:51:44 +03:00
|
|
|
const Func& aHandleFileFunc,
|
|
|
|
const bool aCanRemoveFiles, const bool aTrackQuota) {
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2021-01-11 12:20:25 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> parentFile;
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult rv = aBodyDir.GetParent(getter_AddRefs(parentFile));
|
2021-01-11 12:20:25 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(parentFile);
|
2020-04-15 02:51:44 +03:00
|
|
|
|
2021-01-11 12:20:25 +03:00
|
|
|
nsAutoCString nativeLeafName;
|
|
|
|
rv = parentFile->GetNativeLeafName(nativeLeafName);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2020-04-15 02:51:44 +03:00
|
|
|
|
2021-01-11 12:20:25 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(StringEndsWith(nativeLeafName, "morgue"_ns));
|
|
|
|
}
|
2020-04-15 02:51:44 +03:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
FlippedOnce<true> isEmpty;
|
2021-05-03 22:44:39 +03:00
|
|
|
QM_TRY(quota::CollectEachFile(
|
2021-01-14 19:42:32 +03:00
|
|
|
aBodyDir,
|
2021-01-05 12:27:18 +03:00
|
|
|
[&isEmpty, &aQuotaInfo, aTrackQuota, &aHandleFileFunc,
|
|
|
|
aCanRemoveFiles](const nsCOMPtr<nsIFile>& file) -> Result<Ok, nsresult> {
|
2021-05-03 22:44:39 +03:00
|
|
|
QM_TRY_INSPECT(const auto& dirEntryKind, quota::GetDirEntryKind(*file));
|
2021-02-08 16:15:50 +03:00
|
|
|
|
|
|
|
switch (dirEntryKind) {
|
|
|
|
case quota::nsIFileKind::ExistsAsDirectory: {
|
|
|
|
// If it's a directory somehow, try to remove it and move on
|
|
|
|
DebugOnly<nsresult> result = RemoveNsIFileRecursively(
|
|
|
|
aQuotaInfo, *file, /* aTrackQuota */ false);
|
2021-01-05 12:27:18 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
2021-02-08 16:15:50 +03:00
|
|
|
break;
|
2021-01-05 12:27:18 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
case quota::nsIFileKind::ExistsAsFile: {
|
|
|
|
nsAutoCString leafName;
|
2021-05-03 22:44:39 +03:00
|
|
|
QM_TRY(file->GetNativeLeafName(leafName));
|
2021-02-08 16:15:50 +03:00
|
|
|
|
|
|
|
// Delete all tmp files regardless of known bodies. These are all
|
|
|
|
// considered orphans.
|
|
|
|
if (StringEndsWith(leafName, ".tmp"_ns)) {
|
|
|
|
if (aCanRemoveFiles) {
|
|
|
|
DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFile(aQuotaInfo, *file, aTrackQuota);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
return Ok{};
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-17 16:39:51 +03:00
|
|
|
// Otherwise, it must be a .final file.
|
|
|
|
QM_WARNONLY_TRY_UNWRAP(
|
|
|
|
const auto maybeEndingOk,
|
|
|
|
OkIf(StringEndsWith(leafName, ".final"_ns)));
|
|
|
|
|
|
|
|
// If its not, try to remove it and move on.
|
|
|
|
if (!maybeEndingOk) {
|
|
|
|
DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFile(aQuotaInfo, *file, /* aTrackQuota */ false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
return Ok{};
|
|
|
|
}
|
2021-02-08 16:15:50 +03:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:44:39 +03:00
|
|
|
QM_TRY_INSPECT(const bool& fileDeleted,
|
|
|
|
aHandleFileFunc(*file, leafName));
|
2021-02-08 16:15:50 +03:00
|
|
|
if (fileDeleted) {
|
|
|
|
return Ok{};
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty.EnsureFlipped();
|
|
|
|
break;
|
|
|
|
}
|
2021-01-05 12:27:18 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
case quota::nsIFileKind::DoesNotExist:
|
|
|
|
// Ignore files that got removed externally while iterating.
|
|
|
|
break;
|
|
|
|
}
|
2021-01-05 12:27:18 +03:00
|
|
|
|
|
|
|
return Ok{};
|
|
|
|
}));
|
2020-04-15 02:51:44 +03:00
|
|
|
|
|
|
|
if (isEmpty && aCanRemoveFiles) {
|
|
|
|
DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFileRecursively(aQuotaInfo, aBodyDir, /* aTrackQuota */ false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:20:25 +03:00
|
|
|
return NS_OK;
|
2020-04-15 02:51:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_cache_FileUtilsImpl_h
|