2015-03-02 16:20:00 +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/. */
|
|
|
|
|
2020-04-15 02:51:44 +03:00
|
|
|
#include "FileUtilsImpl.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "DBSchema.h"
|
2017-07-10 12:02:44 +03:00
|
|
|
#include "mozilla/dom/InternalResponse.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/quota/FileStreams.h"
|
2017-05-23 13:33:47 +03:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "mozilla/dom/quota/QuotaObject.h"
|
2021-01-14 19:42:32 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/SnappyCompressOutputStream.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-10-05 06:06:28 +03:00
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
2017-07-18 13:57:54 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom::cache {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2019-08-22 16:06:48 +03:00
|
|
|
using mozilla::dom::quota::Client;
|
2020-11-17 12:07:03 +03:00
|
|
|
using mozilla::dom::quota::CloneFileAndAppend;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::dom::quota::FileInputStream;
|
|
|
|
using mozilla::dom::quota::FileOutputStream;
|
2021-02-08 16:15:50 +03:00
|
|
|
using mozilla::dom::quota::GetDirEntryKind;
|
|
|
|
using mozilla::dom::quota::nsIFileKind;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::dom::quota::PERSISTENCE_TYPE_DEFAULT;
|
2017-05-23 13:33:47 +03:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
2017-07-10 12:02:44 +03:00
|
|
|
using mozilla::dom::quota::QuotaObject;
|
2015-04-16 22:00:16 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-07-10 12:03:24 +03:00
|
|
|
// Const variable for generate padding size.
|
|
|
|
// XXX This will be tweaked to something more meaningful in Bug 1383656.
|
|
|
|
const int64_t kRoundUpNumber = 20480;
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
enum BodyFileType { BODY_FILE_FINAL, BODY_FILE_TMP };
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<NotNull<nsCOMPtr<nsIFile>>, nsresult> BodyIdToFile(nsIFile& aBaseDir,
|
|
|
|
const nsID& aId,
|
|
|
|
BodyFileType aType);
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
int64_t RoundUp(int64_t aX, int64_t aY);
|
2017-07-10 12:03:24 +03:00
|
|
|
|
|
|
|
// The alogrithm for generating padding refers to the mitigation approach in
|
|
|
|
// https://github.com/whatwg/storage/issues/31.
|
|
|
|
// First, generate a random number between 0 and 100kB.
|
|
|
|
// Next, round up the sum of random number and response size to the nearest
|
|
|
|
// 20kB.
|
|
|
|
// Finally, the virtual padding size will be the result minus the response size.
|
2021-01-14 19:42:32 +03:00
|
|
|
int64_t BodyGeneratePadding(int64_t aBodyFileSize, uint32_t aPaddingInfo);
|
2017-07-10 12:02:44 +03:00
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult DirectoryPaddingWrite(nsIFile& aBaseDir,
|
|
|
|
DirPaddingFile aPaddingFileType,
|
|
|
|
int64_t aPaddingSize);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
const auto kMorgueDirectory = u"morgue"_ns;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
template <typename SuccessType = Ok>
|
|
|
|
Result<SuccessType, nsresult> MapNotFoundToDefault(const nsresult aRv) {
|
|
|
|
return (aRv == NS_ERROR_FILE_NOT_FOUND ||
|
|
|
|
aRv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
|
|
|
|
? Result<SuccessType, nsresult>{std::in_place}
|
|
|
|
: Err(aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<Ok, nsresult> MapAlreadyExistsToDefault(const nsresult aRv) {
|
|
|
|
return (aRv == NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
? Result<Ok, nsresult>{std::in_place}
|
|
|
|
: Err(aRv);
|
|
|
|
}
|
2015-03-05 01:05:37 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<NotNull<nsCOMPtr<nsIFile>>, nsresult> BodyGetCacheDir(nsIFile& aBaseDir,
|
|
|
|
const nsID& aId) {
|
|
|
|
CACHE_TRY_UNWRAP(auto cacheDir,
|
|
|
|
CloneFileAndAppend(aBaseDir, kMorgueDirectory));
|
2015-03-05 01:05:37 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
// Some file systems have poor performance when there are too many files
|
|
|
|
// in a single directory. Mitigate this issue by spreading the body
|
|
|
|
// files out into sub-directories. We use the last byte of the ID for
|
|
|
|
// the name of the sub-directory.
|
|
|
|
CACHE_TRY(cacheDir->Append(IntToString(aId.m3[7])));
|
2015-03-05 01:05:37 +03:00
|
|
|
|
2021-04-24 11:58:31 +03:00
|
|
|
// Callers call this function without checking if the directory already
|
|
|
|
// exists (idempotent usage). QM_OR_ELSE_WARN is not used here since we want
|
|
|
|
// to ignore NS_ERROR_FILE_ALREADY_EXISTS completely.
|
|
|
|
QM_TRY(ToResult(cacheDir->Create(nsIFile::DIRECTORY_TYPE, 0755))
|
|
|
|
.orElse(ErrToDefaultOkOrErr<NS_ERROR_FILE_ALREADY_EXISTS>));
|
2015-03-05 01:05:37 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return WrapNotNullUnchecked(std::move(cacheDir));
|
2015-03-05 01:05:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
} // namespace
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyCreateDir(nsIFile& aBaseDir) {
|
|
|
|
CACHE_TRY_INSPECT(const auto& bodyDir,
|
|
|
|
CloneFileAndAppend(aBaseDir, kMorgueDirectory));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-04-24 11:58:32 +03:00
|
|
|
// Callers call this function without checking if the directory already
|
|
|
|
// exists (idempotent usage). QM_OR_ELSE_WARN is not used here since we want
|
|
|
|
// to ignore NS_ERROR_FILE_ALREADY_EXISTS completely.
|
|
|
|
QM_TRY(ToResult(bodyDir->Create(nsIFile::DIRECTORY_TYPE, 0755))
|
|
|
|
.orElse(ErrToDefaultOkOrErr<NS_ERROR_FILE_ALREADY_EXISTS>));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyDeleteDir(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir) {
|
|
|
|
CACHE_TRY_INSPECT(const auto& bodyDir,
|
|
|
|
CloneFileAndAppend(aBaseDir, kMorgueDirectory));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(RemoveNsIFileRecursively(aQuotaInfo, *bodyDir));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<std::pair<nsID, nsCOMPtr<nsISupports>>, nsresult> BodyStartWriteStream(
|
|
|
|
const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, nsIInputStream& aSource,
|
|
|
|
void* aClosure, nsAsyncCopyCallbackFun aCallback) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aClosure);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aCallback);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& idGen, ToResultGet<nsCOMPtr<nsIUUIDGenerator>>(
|
|
|
|
MOZ_SELECT_OVERLOAD(do_GetService),
|
|
|
|
"@mozilla.org/uuid-generator;1"));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsID id;
|
|
|
|
CACHE_TRY(idGen->GenerateUUIDInPlace(&id));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& finalFile,
|
|
|
|
BodyIdToFile(aBaseDir, id, BODY_FILE_FINAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
{
|
|
|
|
CACHE_TRY_INSPECT(const bool& exists,
|
|
|
|
MOZ_TO_RESULT_INVOKE(*finalFile, Exists));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(OkIf(!exists), Err(NS_ERROR_FILE_ALREADY_EXISTS));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& tmpFile,
|
|
|
|
BodyIdToFile(aBaseDir, id, BODY_FILE_TMP));
|
|
|
|
|
2021-01-28 14:12:19 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& fileStream,
|
|
|
|
CreateFileOutputStream(PERSISTENCE_TYPE_DEFAULT, aQuotaInfo,
|
|
|
|
Client::DOMCACHE, tmpFile.get()));
|
2021-01-14 19:42:32 +03:00
|
|
|
|
2021-01-28 14:12:19 +03:00
|
|
|
const auto compressed =
|
|
|
|
MakeRefPtr<SnappyCompressOutputStream>(fileStream.get());
|
2021-01-14 19:42:32 +03:00
|
|
|
|
|
|
|
const nsCOMPtr<nsIEventTarget> target =
|
2015-03-02 16:20:00 +03:00
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsCOMPtr<nsISupports> copyContext;
|
|
|
|
CACHE_TRY(NS_AsyncCopy(&aSource, compressed, target,
|
|
|
|
NS_ASYNCCOPY_VIA_WRITESEGMENTS,
|
|
|
|
compressed->BlockSize(), aCallback, aClosure, true,
|
|
|
|
true, // close streams
|
|
|
|
getter_AddRefs(copyContext)));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return std::make_pair(id, std::move(copyContext));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
void BodyCancelWrite(nsISupports& aCopyContext) {
|
2021-03-24 15:27:53 +03:00
|
|
|
QM_WARNONLY_TRY(NS_CancelAsyncCopy(&aCopyContext, NS_ERROR_ABORT));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
// TODO The partially written file must be cleaned up after the async copy
|
2015-03-02 16:20:00 +03:00
|
|
|
// makes its callback.
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId) {
|
|
|
|
CACHE_TRY_INSPECT(const auto& tmpFile,
|
|
|
|
BodyIdToFile(aBaseDir, aId, BODY_FILE_TMP));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& finalFile,
|
|
|
|
BodyIdToFile(aBaseDir, aId, BODY_FILE_FINAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsAutoString finalFileName;
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(finalFile->GetLeafName(finalFileName));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2017-05-23 13:33:47 +03:00
|
|
|
// It's fine to not notify the QuotaManager that the path has been changed,
|
|
|
|
// because its path will be updated and its size will be recalculated when
|
|
|
|
// opening file next time.
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(tmpFile->RenameTo(nullptr, finalFileName));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<NotNull<nsCOMPtr<nsIInputStream>>, nsresult> BodyOpen(
|
|
|
|
const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, const nsID& aId) {
|
|
|
|
CACHE_TRY_INSPECT(const auto& finalFile,
|
|
|
|
BodyIdToFile(aBaseDir, aId, BODY_FILE_FINAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-28 14:12:19 +03:00
|
|
|
CACHE_TRY_RETURN(CreateFileInputStream(PERSISTENCE_TYPE_DEFAULT, aQuotaInfo,
|
|
|
|
Client::DOMCACHE, finalFile.get())
|
|
|
|
.map([](NotNull<RefPtr<FileInputStream>>&& stream) {
|
|
|
|
return WrapNotNullUnchecked(
|
|
|
|
nsCOMPtr<nsIInputStream>{stream.get()});
|
|
|
|
}));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 12:02:44 +03:00
|
|
|
nsresult BodyMaybeUpdatePaddingSize(const QuotaInfo& aQuotaInfo,
|
2021-01-14 19:42:32 +03:00
|
|
|
nsIFile& aBaseDir, const nsID& aId,
|
2017-07-10 12:03:24 +03:00
|
|
|
const uint32_t aPaddingInfo,
|
2021-01-14 19:42:32 +03:00
|
|
|
int64_t* aPaddingSizeInOut) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPaddingSizeInOut);
|
2017-07-10 12:02:44 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& bodyFile,
|
|
|
|
BodyIdToFile(aBaseDir, aId, BODY_FILE_TMP));
|
2017-07-10 12:02:44 +03:00
|
|
|
|
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(quotaManager);
|
|
|
|
|
|
|
|
int64_t fileSize = 0;
|
2021-01-14 19:42:32 +03:00
|
|
|
RefPtr<QuotaObject> quotaObject = quotaManager->GetQuotaObject(
|
|
|
|
PERSISTENCE_TYPE_DEFAULT, aQuotaInfo, Client::DOMCACHE, bodyFile.get(),
|
|
|
|
-1, &fileSize);
|
2017-07-10 12:02:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(quotaObject);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
|
2017-12-06 05:12:24 +03:00
|
|
|
// XXXtt: bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1422815
|
|
|
|
if (!quotaObject) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2017-07-10 12:02:44 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
if (*aPaddingSizeInOut == InternalResponse::UNKNOWN_PADDING_SIZE) {
|
|
|
|
*aPaddingSizeInOut = BodyGeneratePadding(fileSize, aPaddingInfo);
|
2017-07-10 12:02:44 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(*aPaddingSizeInOut >= 0);
|
2017-07-10 12:02:44 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
if (!quotaObject->IncreaseSize(*aPaddingSizeInOut)) {
|
2017-07-10 12:02:44 +03:00
|
|
|
return NS_ERROR_FILE_NO_DEVICE_SPACE;
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-10 12:02:44 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyDeleteFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir,
|
2017-05-23 13:33:47 +03:00
|
|
|
const nsTArray<nsID>& aIdList) {
|
2019-09-27 19:54:45 +03:00
|
|
|
for (const auto id : aIdList) {
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& bodyDir, BodyGetCacheDir(aBaseDir, id));
|
2019-09-27 19:54:22 +03:00
|
|
|
|
2021-01-05 12:27:18 +03:00
|
|
|
const auto removeFileForId =
|
|
|
|
[&aQuotaInfo, &id](
|
2021-01-14 19:42:32 +03:00
|
|
|
nsIFile& bodyFile,
|
2021-01-05 12:27:18 +03:00
|
|
|
const nsACString& leafName) -> Result<bool, nsresult> {
|
2019-09-27 19:54:45 +03:00
|
|
|
nsID fileId;
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(OkIf(fileId.Parse(leafName.BeginReading())), true,
|
|
|
|
([&aQuotaInfo, &bodyFile](const auto) {
|
|
|
|
DebugOnly<nsresult> result = RemoveNsIFile(
|
|
|
|
aQuotaInfo, bodyFile, /* aTrackQuota */ false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
}));
|
2019-09-27 19:54:22 +03:00
|
|
|
|
2019-09-27 19:54:45 +03:00
|
|
|
if (id.Equals(fileId)) {
|
|
|
|
DebugOnly<nsresult> result = RemoveNsIFile(aQuotaInfo, bodyFile);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
2021-01-05 12:27:18 +03:00
|
|
|
return true;
|
2019-09-27 19:54:22 +03:00
|
|
|
}
|
2019-09-27 19:54:45 +03:00
|
|
|
|
2021-01-05 12:27:18 +03:00
|
|
|
return false;
|
2019-09-27 19:54:45 +03:00
|
|
|
};
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(BodyTraverseFiles(aQuotaInfo, *bodyDir, removeFileForId,
|
|
|
|
/* aCanRemoveFiles */ false,
|
|
|
|
/* aTrackQuota */ true));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
namespace {
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<NotNull<nsCOMPtr<nsIFile>>, nsresult> BodyIdToFile(
|
|
|
|
nsIFile& aBaseDir, const nsID& aId, const BodyFileType aType) {
|
|
|
|
CACHE_TRY_UNWRAP(auto bodyFile, BodyGetCacheDir(aBaseDir, aId));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
char idString[NSID_LENGTH];
|
|
|
|
aId.ToProvidedString(idString);
|
|
|
|
|
|
|
|
NS_ConvertASCIItoUTF16 fileName(idString);
|
|
|
|
|
|
|
|
if (aType == BODY_FILE_FINAL) {
|
|
|
|
fileName.AppendLiteral(".final");
|
|
|
|
} else {
|
|
|
|
fileName.AppendLiteral(".tmp");
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(bodyFile->Append(fileName));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return bodyFile;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2017-07-10 12:03:24 +03:00
|
|
|
int64_t RoundUp(const int64_t aX, const int64_t aY) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aX >= 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aY > 0);
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - ((aX - 1) / aY) * aY >= aY);
|
|
|
|
return aY + ((aX - 1) / aY) * aY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t BodyGeneratePadding(const int64_t aBodyFileSize,
|
|
|
|
const uint32_t aPaddingInfo) {
|
|
|
|
// Generate padding
|
|
|
|
int64_t randomSize = static_cast<int64_t>(aPaddingInfo);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - aBodyFileSize >= randomSize);
|
|
|
|
randomSize += aBodyFileSize;
|
|
|
|
|
|
|
|
return RoundUp(randomSize, kRoundUpNumber) - aBodyFileSize;
|
2017-07-10 12:02:44 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult DirectoryPaddingWrite(nsIFile& aBaseDir,
|
|
|
|
DirPaddingFile aPaddingFileType,
|
|
|
|
int64_t aPaddingSize) {
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPaddingSize >= 0);
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& file,
|
|
|
|
CloneFileAndAppend(aBaseDir, aPaddingFileType == DirPaddingFile::TMP_FILE
|
|
|
|
? nsLiteralString(PADDING_TMP_FILE_NAME)
|
|
|
|
: nsLiteralString(PADDING_FILE_NAME)));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& outputStream,
|
|
|
|
NS_NewLocalFileOutputStream(file));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2017-10-05 06:06:28 +03:00
|
|
|
nsCOMPtr<nsIObjectOutputStream> objectStream =
|
|
|
|
NS_NewObjectOutputStream(outputStream);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(objectStream->Write64(aPaddingSize));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-16 22:00:16 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir,
|
|
|
|
const nsTArray<nsID>& aKnownBodyIdList) {
|
2015-06-26 08:22:46 +03:00
|
|
|
// body files are stored in a directory structure like:
|
|
|
|
//
|
|
|
|
// /morgue/01/{01fdddb2-884d-4c3d-95ba-0c8062f6c325}.final
|
|
|
|
// /morgue/02/{02fdddb2-884d-4c3d-95ba-0c8062f6c325}.tmp
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& dir,
|
|
|
|
CloneFileAndAppend(aBaseDir, kMorgueDirectory));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
|
|
|
// Iterate over all the intermediate morgue subdirs
|
2021-01-05 12:27:18 +03:00
|
|
|
CACHE_TRY(quota::CollectEachFile(
|
|
|
|
*dir,
|
|
|
|
[&aQuotaInfo, &aKnownBodyIdList](
|
|
|
|
const nsCOMPtr<nsIFile>& subdir) -> Result<Ok, nsresult> {
|
2021-02-08 16:15:50 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& dirEntryKind, GetDirEntryKind(*subdir));
|
|
|
|
|
|
|
|
switch (dirEntryKind) {
|
|
|
|
case nsIFileKind::ExistsAsDirectory: {
|
|
|
|
const auto removeOrphanedFiles =
|
|
|
|
[&aQuotaInfo, &aKnownBodyIdList](
|
|
|
|
nsIFile& bodyFile,
|
|
|
|
const nsACString& leafName) -> Result<bool, nsresult> {
|
|
|
|
// Finally, parse the uuid out of the name. If it fails to parse,
|
|
|
|
// then ignore the file.
|
|
|
|
auto cleanup = MakeScopeExit([&aQuotaInfo, &bodyFile] {
|
|
|
|
DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFile(aQuotaInfo, bodyFile);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
});
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
CACHE_TRY(OkIf(id.Parse(leafName.BeginReading())), true);
|
|
|
|
|
|
|
|
if (!aKnownBodyIdList.Contains(id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup.release();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
2021-04-28 10:33:55 +03:00
|
|
|
|
|
|
|
// QM_OR_ELSE_WARN is not used here since we want ignore
|
|
|
|
// NS_ERROR_FILE_FS_CORRUPTED completely (even a warning is not
|
|
|
|
// desired).
|
2021-02-12 12:14:21 +03:00
|
|
|
CACHE_TRY(
|
|
|
|
ToResult(BodyTraverseFiles(aQuotaInfo, *subdir,
|
|
|
|
removeOrphanedFiles,
|
|
|
|
/* aCanRemoveFiles */ true,
|
|
|
|
/* aTrackQuota */ true))
|
|
|
|
.orElse([](const nsresult rv) -> Result<Ok, nsresult> {
|
2021-04-28 10:33:54 +03:00
|
|
|
// We treat NS_ERROR_FILE_FS_CORRUPTED as if the
|
|
|
|
// directory did not exist at all.
|
|
|
|
if (rv == NS_ERROR_FILE_FS_CORRUPTED) {
|
2021-02-12 12:14:21 +03:00
|
|
|
return Ok{};
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(rv);
|
2021-04-28 10:33:54 +03:00
|
|
|
}));
|
2021-02-08 16:15:50 +03:00
|
|
|
break;
|
2019-09-27 19:54:45 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
case nsIFileKind::ExistsAsFile: {
|
|
|
|
// If a file got in here somehow, try to remove it and move on
|
|
|
|
DebugOnly<nsresult> result =
|
|
|
|
RemoveNsIFile(aQuotaInfo, *subdir, /* aTrackQuota */ false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
|
|
|
break;
|
|
|
|
}
|
2021-02-08 16:52:49 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
case nsIFileKind::DoesNotExist:
|
|
|
|
// Ignore files that got removed externally while iterating.
|
|
|
|
break;
|
|
|
|
}
|
2020-12-21 21:13:56 +03:00
|
|
|
|
2021-01-05 12:27:18 +03:00
|
|
|
return Ok{};
|
|
|
|
}));
|
|
|
|
|
|
|
|
return NS_OK;
|
2019-09-27 19:54:45 +03:00
|
|
|
}
|
2019-09-27 19:54:22 +03:00
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
namespace {
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
Result<nsCOMPtr<nsIFile>, nsresult> GetMarkerFileHandle(
|
|
|
|
const QuotaInfo& aQuotaInfo) {
|
|
|
|
CACHE_TRY_UNWRAP(auto marker,
|
|
|
|
CloneFileAndAppend(*aQuotaInfo.mDir, u"cache"_ns));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(marker->Append(u"context_open.marker"_ns));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return marker;
|
2015-06-26 08:22:46 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-26 08:22:46 +03:00
|
|
|
|
|
|
|
nsresult CreateMarkerFile(const QuotaInfo& aQuotaInfo) {
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aQuotaInfo));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
2021-03-24 15:27:53 +03:00
|
|
|
QM_TRY(
|
|
|
|
QM_OR_ELSE_WARN(ToResult(marker->Create(nsIFile::NORMAL_FILE_TYPE, 0644)),
|
|
|
|
MapAlreadyExistsToDefault));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
|
|
|
// Note, we don't need to fsync here. We only care about actually
|
|
|
|
// writing the marker if later modifications to the Cache are
|
|
|
|
// actually flushed to the disk. If the OS crashes before the marker
|
|
|
|
// is written then we are ensured no other changes to the Cache were
|
|
|
|
// flushed either.
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2015-06-26 08:22:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult DeleteMarkerFile(const QuotaInfo& aQuotaInfo) {
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aQuotaInfo));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
2019-09-27 19:54:22 +03:00
|
|
|
DebugOnly<nsresult> result =
|
2021-01-14 19:42:32 +03:00
|
|
|
RemoveNsIFile(aQuotaInfo, *marker, /* aTrackQuota */ false);
|
2019-09-27 19:54:22 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(result));
|
2015-06-26 08:22:46 +03:00
|
|
|
|
|
|
|
// Again, no fsync is necessary. If the OS crashes before the file
|
|
|
|
// removal is flushed, then the Cache will search for stale data on
|
|
|
|
// startup. This will cause the next Cache access to be a bit slow, but
|
|
|
|
// it seems appropriate after an OS crash.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
bool MarkerFileExists(const QuotaInfo& aQuotaInfo) {
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aQuotaInfo), false);
|
2015-06-26 08:22:46 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_RETURN(MOZ_TO_RESULT_INVOKE(marker, Exists), false);
|
2015-06-26 08:22:46 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult RemoveNsIFileRecursively(const QuotaInfo& aQuotaInfo, nsIFile& aFile,
|
2019-09-27 19:54:22 +03:00
|
|
|
const bool aTrackQuota) {
|
2021-02-08 16:15:50 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& dirEntryKind, GetDirEntryKind(aFile));
|
2017-05-23 13:33:47 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
switch (dirEntryKind) {
|
|
|
|
case nsIFileKind::ExistsAsDirectory:
|
|
|
|
// Unfortunately, we need to traverse all the entries and delete files one
|
|
|
|
// by
|
|
|
|
// one to update their usages to the QuotaManager.
|
|
|
|
CACHE_TRY(quota::CollectEachFile(
|
|
|
|
aFile,
|
|
|
|
[&aQuotaInfo, &aTrackQuota](
|
|
|
|
const nsCOMPtr<nsIFile>& file) -> Result<Ok, nsresult> {
|
|
|
|
CACHE_TRY(RemoveNsIFileRecursively(aQuotaInfo, *file, aTrackQuota));
|
2017-05-23 13:33:47 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
return Ok{};
|
|
|
|
}));
|
2021-01-05 12:27:18 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
// In the end, remove the folder
|
|
|
|
CACHE_TRY(aFile.Remove(/* recursive */ false));
|
2017-05-23 13:33:47 +03:00
|
|
|
|
2021-02-08 16:15:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIFileKind::ExistsAsFile:
|
|
|
|
return RemoveNsIFile(aQuotaInfo, aFile, aTrackQuota);
|
|
|
|
|
|
|
|
case nsIFileKind::DoesNotExist:
|
|
|
|
// Ignore files that got removed externally while iterating.
|
|
|
|
break;
|
|
|
|
}
|
2017-05-23 13:33:47 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-05-23 13:33:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
nsresult RemoveNsIFile(const QuotaInfo& aQuotaInfo, nsIFile& aFile,
|
2019-09-27 19:54:22 +03:00
|
|
|
const bool aTrackQuota) {
|
2017-05-23 13:33:47 +03:00
|
|
|
int64_t fileSize = 0;
|
2019-09-27 19:54:22 +03:00
|
|
|
if (aTrackQuota) {
|
2021-03-24 15:27:53 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& maybeFileSize,
|
|
|
|
QM_OR_ELSE_WARN(
|
|
|
|
MOZ_TO_RESULT_INVOKE(aFile, GetFileSize).map(Some<int64_t>),
|
|
|
|
MapNotFoundToDefault<Maybe<int64_t>>));
|
2021-01-14 19:42:32 +03:00
|
|
|
|
|
|
|
if (!maybeFileSize) {
|
2019-09-27 19:54:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-05-23 13:33:47 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
fileSize = *maybeFileSize;
|
2017-05-23 13:33:47 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 15:27:53 +03:00
|
|
|
QM_TRY(QM_OR_ELSE_WARN(ToResult(aFile.Remove(/* recursive */ false)),
|
|
|
|
MapNotFoundToDefault<>));
|
2021-01-14 19:42:32 +03:00
|
|
|
|
|
|
|
if (fileSize > 0) {
|
|
|
|
MOZ_ASSERT(aTrackQuota);
|
2017-07-10 12:02:44 +03:00
|
|
|
DecreaseUsageForQuotaInfo(aQuotaInfo, fileSize);
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-10 12:02:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecreaseUsageForQuotaInfo(const QuotaInfo& aQuotaInfo,
|
2021-01-14 19:42:32 +03:00
|
|
|
const int64_t aUpdatingSize) {
|
2017-07-10 12:02:44 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aUpdatingSize > 0);
|
|
|
|
|
2017-05-23 13:33:47 +03:00
|
|
|
QuotaManager* quotaManager = QuotaManager::Get();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(quotaManager);
|
|
|
|
|
2021-03-17 09:56:39 +03:00
|
|
|
quotaManager->DecreaseUsageForClient(
|
|
|
|
quota::ClientMetadata{aQuotaInfo, Client::DOMCACHE}, aUpdatingSize);
|
2017-05-23 13:33:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
bool DirectoryPaddingFileExists(nsIFile& aBaseDir,
|
2017-07-18 13:57:54 +03:00
|
|
|
DirPaddingFile aPaddingFileType) {
|
2020-11-17 14:36:34 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& file,
|
2021-01-14 19:42:32 +03:00
|
|
|
CloneFileAndAppend(aBaseDir, aPaddingFileType == DirPaddingFile::TMP_FILE
|
|
|
|
? nsLiteralString(PADDING_TMP_FILE_NAME)
|
|
|
|
: nsLiteralString(PADDING_FILE_NAME)),
|
2020-11-17 14:36:34 +03:00
|
|
|
false);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 14:36:34 +03:00
|
|
|
CACHE_TRY_RETURN(MOZ_TO_RESULT_INVOKE(file, Exists), false);
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
Result<int64_t, nsresult> DirectoryPaddingGet(nsIFile& aBaseDir) {
|
2020-11-17 12:07:03 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
2021-01-14 19:42:32 +03:00
|
|
|
!DirectoryPaddingFileExists(aBaseDir, DirPaddingFile::TMP_FILE));
|
2020-11-17 12:07:03 +03:00
|
|
|
|
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& file,
|
|
|
|
CloneFileAndAppend(aBaseDir, nsLiteralString(PADDING_FILE_NAME)));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 22:30:05 +03:00
|
|
|
CACHE_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 22:30:05 +03:00
|
|
|
CACHE_TRY_INSPECT(const auto& bufferedStream,
|
|
|
|
NS_NewBufferedInputStream(stream.forget(), 512));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 22:30:05 +03:00
|
|
|
const nsCOMPtr<nsIObjectInputStream> objectStream =
|
2017-10-05 06:06:28 +03:00
|
|
|
NS_NewObjectInputStream(bufferedStream);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 12:07:03 +03:00
|
|
|
CACHE_TRY_RETURN(
|
|
|
|
MOZ_TO_RESULT_INVOKE(objectStream, Read64).map([](const uint64_t val) {
|
|
|
|
return int64_t(val);
|
|
|
|
}));
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult DirectoryPaddingInit(nsIFile& aBaseDir) {
|
|
|
|
CACHE_TRY(DirectoryPaddingWrite(aBaseDir, DirPaddingFile::FILE, 0));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult UpdateDirectoryPaddingFile(nsIFile& aBaseDir,
|
|
|
|
mozIStorageConnection& aConn,
|
|
|
|
const int64_t aIncreaseSize,
|
|
|
|
const int64_t aDecreaseSize,
|
|
|
|
const bool aTemporaryFileExist) {
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aIncreaseSize >= 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDecreaseSize >= 0);
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
const auto directoryPaddingGetResult =
|
|
|
|
aTemporaryFileExist ? Maybe<int64_t>{} : [&aBaseDir] {
|
2021-03-24 15:27:53 +03:00
|
|
|
CACHE_TRY_RETURN(
|
2021-04-16 04:09:10 +03:00
|
|
|
QM_OR_ELSE_WARN(DirectoryPaddingGet(aBaseDir).map(Some<int64_t>),
|
|
|
|
MapNotFoundToDefault<Maybe<int64_t>>),
|
2021-03-24 15:27:53 +03:00
|
|
|
Maybe<int64_t>{});
|
2021-01-14 19:42:32 +03:00
|
|
|
}();
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const int64_t& currentPaddingSize,
|
|
|
|
([directoryPaddingGetResult, &aBaseDir, &aConn, aIncreaseSize,
|
|
|
|
aDecreaseSize]() -> Result<int64_t, nsresult> {
|
|
|
|
if (!directoryPaddingGetResult) {
|
|
|
|
// Fail to read padding size from the dir padding file, so try to
|
|
|
|
// restore.
|
|
|
|
|
|
|
|
// Not delete the temporary padding file here, because we're going
|
|
|
|
// to overwrite it below anyway.
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::FILE));
|
2021-01-14 19:42:32 +03:00
|
|
|
|
|
|
|
// We don't need to add the aIncreaseSize or aDecreaseSize here,
|
|
|
|
// because it's already encompassed within the database.
|
|
|
|
CACHE_TRY_RETURN(db::FindOverallPaddingSize(aConn));
|
|
|
|
}
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
int64_t currentPaddingSize = directoryPaddingGetResult.value();
|
|
|
|
bool shouldRevise = false;
|
2017-10-06 10:18:06 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
if (aIncreaseSize > 0) {
|
|
|
|
if (INT64_MAX - currentPaddingSize < aDecreaseSize) {
|
|
|
|
shouldRevise = true;
|
|
|
|
} else {
|
|
|
|
currentPaddingSize += aIncreaseSize;
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 10:18:06 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
if (aDecreaseSize > 0) {
|
|
|
|
if (currentPaddingSize < aDecreaseSize) {
|
|
|
|
shouldRevise = true;
|
|
|
|
} else if (!shouldRevise) {
|
|
|
|
currentPaddingSize -= aDecreaseSize;
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 10:18:06 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
if (shouldRevise) {
|
|
|
|
// If somehow runing into this condition, the tracking padding size is
|
|
|
|
// incorrect.
|
|
|
|
// Delete padding file to indicate the padding size is incorrect for
|
|
|
|
// avoiding error happening in the following lines.
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::FILE));
|
2021-01-14 19:42:32 +03:00
|
|
|
|
|
|
|
CACHE_TRY_UNWRAP(currentPaddingSize,
|
|
|
|
db::FindOverallPaddingSize(aConn));
|
|
|
|
|
|
|
|
// XXXtt: we should have an easy way to update (increase or
|
|
|
|
// recalulate) padding size in the QM. For now, only correct the
|
|
|
|
// padding size in padding file and make QM be able to get the correct
|
|
|
|
// size in the next QM initialization. We still want to catch this in
|
|
|
|
// the debug build.
|
|
|
|
MOZ_ASSERT(false, "The padding size is unsync with QM");
|
|
|
|
}
|
2017-07-18 13:58:56 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2021-01-14 19:42:32 +03:00
|
|
|
const int64_t lastPaddingSize = currentPaddingSize;
|
|
|
|
CACHE_TRY_UNWRAP(currentPaddingSize, db::FindOverallPaddingSize(aConn));
|
2017-07-18 13:58:56 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(currentPaddingSize == lastPaddingSize);
|
2017-07-18 13:58:56 +03:00
|
|
|
#endif // DEBUG
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return currentPaddingSize;
|
|
|
|
}()));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(currentPaddingSize >= 0);
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingWrite(aBaseDir, DirPaddingFile::TMP_FILE,
|
|
|
|
currentPaddingSize));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult DirectoryPaddingFinalizeWrite(nsIFile& aBaseDir) {
|
2017-07-18 13:57:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
DirectoryPaddingFileExists(aBaseDir, DirPaddingFile::TMP_FILE));
|
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& file,
|
|
|
|
CloneFileAndAppend(aBaseDir, nsLiteralString(PADDING_TMP_FILE_NAME)));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY(file->RenameTo(nullptr, nsLiteralString(PADDING_FILE_NAME)));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
Result<int64_t, nsresult> DirectoryPaddingRestore(nsIFile& aBaseDir,
|
|
|
|
mozIStorageConnection& aConn,
|
|
|
|
const bool aMustRestore) {
|
2017-07-18 13:57:54 +03:00
|
|
|
// The content of padding file is untrusted, so remove it here.
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::FILE));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 12:04:35 +03:00
|
|
|
CACHE_TRY_INSPECT(const int64_t& paddingSize,
|
|
|
|
db::FindOverallPaddingSize(aConn));
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(paddingSize >= 0);
|
2017-07-18 13:58:56 +03:00
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingWrite(aBaseDir, DirPaddingFile::FILE, paddingSize),
|
|
|
|
(aMustRestore ? Err(tryTempError)
|
|
|
|
: Result<int64_t, nsresult>{paddingSize}));
|
2017-10-05 12:30:40 +03:00
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
CACHE_TRY(DirectoryPaddingDeleteFile(aBaseDir, DirPaddingFile::TMP_FILE));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2020-11-17 12:04:35 +03:00
|
|
|
return paddingSize;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:09:10 +03:00
|
|
|
nsresult DirectoryPaddingDeleteFile(nsIFile& aBaseDir,
|
|
|
|
DirPaddingFile aPaddingFileType) {
|
2021-01-14 19:42:32 +03:00
|
|
|
CACHE_TRY_INSPECT(
|
|
|
|
const auto& file,
|
|
|
|
CloneFileAndAppend(aBaseDir, aPaddingFileType == DirPaddingFile::TMP_FILE
|
|
|
|
? nsLiteralString(PADDING_TMP_FILE_NAME)
|
|
|
|
: nsLiteralString(PADDING_FILE_NAME)));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-03-24 15:27:53 +03:00
|
|
|
QM_TRY(QM_OR_ELSE_WARN(ToResult(file->Remove(/* recursive */ false)),
|
|
|
|
MapNotFoundToDefault<>));
|
2017-07-18 13:57:54 +03:00
|
|
|
|
2021-01-14 19:42:32 +03:00
|
|
|
return NS_OK;
|
2017-07-18 13:57:54 +03:00
|
|
|
}
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom::cache
|