2015-03-02 13:20:00 +00: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_FileUtils_h
|
|
|
|
#define mozilla_dom_cache_FileUtils_h
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/dom/cache/Types.h"
|
2020-10-21 07:59:44 +00:00
|
|
|
#include "CacheCommon.h"
|
2017-10-26 11:35:08 +00:00
|
|
|
#include "mozIStorageConnection.h"
|
2015-03-02 13:20:00 +00:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsTArrayForwardDeclare.h"
|
|
|
|
|
|
|
|
struct nsID;
|
|
|
|
class nsIFile;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
2017-10-26 11:35:08 +00:00
|
|
|
#define PADDING_FILE_NAME u".padding"
|
|
|
|
#define PADDING_TMP_FILE_NAME u".padding-tmp"
|
|
|
|
|
2017-07-18 10:57:54 +00:00
|
|
|
enum DirPaddingFile { FILE, TMP_FILE };
|
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyCreateDir(nsIFile& aBaseDir);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-04-16 19:00:16 +00:00
|
|
|
// Note that this function can only be used during the initialization of the
|
|
|
|
// database. We're unlikely to be able to delete the DB successfully past
|
|
|
|
// that point due to the file being in use.
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyDeleteDir(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
// Returns a Result with a success value with the body id and, optionally, the
|
|
|
|
// copy context.
|
|
|
|
Result<std::pair<nsID, nsCOMPtr<nsISupports>>, nsresult> BodyStartWriteStream(
|
|
|
|
const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, nsIInputStream& aSource,
|
|
|
|
void* aClosure, nsAsyncCopyCallbackFun aCallback);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
void BodyCancelWrite(nsISupports& aCopyContext);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
Result<NotNull<nsCOMPtr<nsIInputStream>>, nsresult> BodyOpen(
|
|
|
|
const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, const nsID& aId);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2017-07-10 09:02:44 +00:00
|
|
|
nsresult BodyMaybeUpdatePaddingSize(const QuotaInfo& aQuotaInfo,
|
2021-01-14 16:42:32 +00:00
|
|
|
nsIFile& aBaseDir, const nsID& aId,
|
|
|
|
uint32_t aPaddingInfo,
|
|
|
|
int64_t* aPaddingSizeInOut);
|
2017-07-10 09:02:44 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyDeleteFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir,
|
2017-05-23 10:33:47 +00:00
|
|
|
const nsTArray<nsID>& aIdList);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir,
|
|
|
|
const nsTArray<nsID>& aKnownBodyIdList);
|
2015-06-26 05:22:46 +00:00
|
|
|
|
2019-10-02 10:38:55 +00:00
|
|
|
// 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.
|
2019-09-27 16:54:45 +00:00
|
|
|
template <typename Func>
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBodyDir,
|
|
|
|
const Func& aHandleFileFunc, bool aCanRemoveFiles,
|
|
|
|
bool aTrackQuota = true);
|
2019-09-27 16:54:45 +00:00
|
|
|
|
2015-06-26 05:22:46 +00:00
|
|
|
nsresult CreateMarkerFile(const QuotaInfo& aQuotaInfo);
|
|
|
|
|
|
|
|
nsresult DeleteMarkerFile(const QuotaInfo& aQuotaInfo);
|
|
|
|
|
2015-06-26 05:22:46 +00:00
|
|
|
bool MarkerFileExists(const QuotaInfo& aQuotaInfo);
|
|
|
|
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult RemoveNsIFileRecursively(const QuotaInfo& aQuotaInfo, nsIFile& aFile,
|
|
|
|
bool aTrackQuota = true);
|
2017-05-23 10:33:47 +00:00
|
|
|
|
2021-05-21 09:30:30 +00:00
|
|
|
// Delete a file that you think exists. If the file doesn't exist, an error
|
|
|
|
// will not be returned, but warning telemetry will be generated! So only call
|
|
|
|
// this on files that you know exist (idempotent usage, but it's not
|
|
|
|
// recommended).
|
2021-01-14 16:42:32 +00:00
|
|
|
nsresult RemoveNsIFile(const QuotaInfo& aQuotaInfo, nsIFile& aFile,
|
|
|
|
bool aTrackQuota = true);
|
2017-05-23 10:33:47 +00:00
|
|
|
|
2017-07-10 09:02:44 +00:00
|
|
|
void DecreaseUsageForQuotaInfo(const QuotaInfo& aQuotaInfo,
|
2021-01-14 16:42:32 +00:00
|
|
|
int64_t aUpdatingSize);
|
2017-07-18 10:57:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used to check if the directory padding file is existed.
|
|
|
|
*/
|
2021-01-14 16:42:32 +00:00
|
|
|
bool DirectoryPaddingFileExists(nsIFile& aBaseDir,
|
2017-07-18 10:57:54 +00:00
|
|
|
DirPaddingFile aPaddingFileType);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The functions below are used to read/write/delete the directory padding file
|
|
|
|
* after acquiring the mutex lock. The mutex lock is held by
|
|
|
|
* CacheQuotaClient to prevent multi-thread accessing issue. To avoid deadlock,
|
|
|
|
* these functions should only access by static functions in
|
|
|
|
* dom/cache/QuotaClient.cpp.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-11-17 09:07:03 +00:00
|
|
|
// Returns a Result with a success value denoting the padding size.
|
2021-04-16 01:09:10 +00:00
|
|
|
Result<int64_t, nsresult> DirectoryPaddingGet(nsIFile& aBaseDir);
|
2020-11-17 09:07:03 +00:00
|
|
|
|
2021-04-16 01:09:10 +00:00
|
|
|
nsresult DirectoryPaddingInit(nsIFile& aBaseDir);
|
2017-07-18 10:57:54 +00:00
|
|
|
|
2021-04-16 01:09:10 +00:00
|
|
|
nsresult UpdateDirectoryPaddingFile(nsIFile& aBaseDir,
|
|
|
|
mozIStorageConnection& aConn,
|
|
|
|
int64_t aIncreaseSize,
|
|
|
|
int64_t aDecreaseSize,
|
|
|
|
bool aTemporaryFileExist);
|
2017-07-18 10:57:54 +00:00
|
|
|
|
2021-04-16 01:09:10 +00:00
|
|
|
nsresult DirectoryPaddingFinalizeWrite(nsIFile& aBaseDir);
|
2017-07-18 10:57:54 +00:00
|
|
|
|
2020-11-17 09:04:35 +00:00
|
|
|
// Returns a Result with a success value denoting the padding size.
|
2021-04-16 01:09:10 +00:00
|
|
|
Result<int64_t, nsresult> DirectoryPaddingRestore(nsIFile& aBaseDir,
|
|
|
|
mozIStorageConnection& aConn,
|
|
|
|
bool aMustRestore);
|
2017-07-18 10:57:54 +00:00
|
|
|
|
2021-04-16 01:09:10 +00:00
|
|
|
nsresult DirectoryPaddingDeleteFile(nsIFile& aBaseDir,
|
|
|
|
DirPaddingFile aPaddingFileType);
|
2015-03-02 13:20:00 +00:00
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_cache_FileUtils_h
|