Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / cache / FileUtils.h
blob98ef21c8c31868643f2ee36ebbf9253f523650e8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_cache_FileUtils_h
8 #define mozilla_dom_cache_FileUtils_h
10 #include "CacheCommon.h"
11 #include "CacheCipherKeyManager.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/cache/Types.h"
14 #include "mozIStorageConnection.h"
15 #include "nsStreamUtils.h"
16 #include "nsTArrayForwardDeclare.h"
18 struct nsID;
19 class nsIFile;
21 namespace mozilla::dom::cache {
23 #define PADDING_FILE_NAME u".padding"
24 #define PADDING_TMP_FILE_NAME u".padding-tmp"
26 enum class DirPaddingFile { FILE, TMP_FILE };
28 nsresult BodyCreateDir(nsIFile& aBaseDir);
30 // Note that this function can only be used during the initialization of the
31 // database. We're unlikely to be able to delete the DB successfully past
32 // that point due to the file being in use.
33 nsresult BodyDeleteDir(const CacheDirectoryMetadata& aDirectoryMetadata,
34 nsIFile& aBaseDir);
36 // Returns a Result with a success value with the body id and, optionally, the
37 // copy context.
38 Result<nsCOMPtr<nsISupports>, nsresult> BodyStartWriteStream(
39 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir,
40 const nsID& aBodyId, Maybe<CipherKey> aMaybeCipherKey,
41 nsIInputStream& aSource, void* aClosure, nsAsyncCopyCallbackFun aCallback);
43 void BodyCancelWrite(nsISupports& aCopyContext);
45 Result<int64_t, nsresult> BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId);
47 Result<int64_t, nsresult> GetBodyDiskSize(nsIFile& aBaseDir, const nsID& aId);
49 Result<MovingNotNull<nsCOMPtr<nsIInputStream>>, nsresult> BodyOpen(
50 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir,
51 const nsID& aId, Maybe<CipherKey> aMaybeCipherKey);
53 nsresult BodyMaybeUpdatePaddingSize(
54 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir,
55 const nsID& aId, uint32_t aPaddingInfo, int64_t* aPaddingSizeInOut);
57 nsresult BodyDeleteFiles(const CacheDirectoryMetadata& aDirectoryMetadata,
58 nsIFile& aBaseDir, const nsTArray<nsID>& aIdList);
60 nsresult BodyDeleteOrphanedFiles(
61 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir,
62 const nsTArray<nsID>& aKnownBodyIdList);
64 // If aCanRemoveFiles is true, that means we are safe to touch the files which
65 // can be accessed in other threads.
66 // If it's not, that means we cannot remove the files which are possible to
67 // created by other threads. Note that if the files are not expected, we should
68 // be safe to remove them in any case.
69 template <typename Func>
70 nsresult BodyTraverseFiles(
71 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, nsIFile& aBodyDir,
72 const Func& aHandleFileFunc, bool aCanRemoveFiles, bool aTrackQuota = true);
74 // XXX Remove this method when all callers properly wrap aClientMetadata with
75 // Some/Nothing
76 template <typename Func>
77 nsresult BodyTraverseFiles(const CacheDirectoryMetadata& aDirectoryMetadata,
78 nsIFile& aBodyDir, const Func& aHandleFileFunc,
79 bool aCanRemoveFiles, bool aTrackQuota = true) {
80 return BodyTraverseFiles(Some(aDirectoryMetadata), aBodyDir, aHandleFileFunc,
81 aCanRemoveFiles, aTrackQuota);
84 nsresult CreateMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata);
86 nsresult DeleteMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata);
88 bool MarkerFileExists(const CacheDirectoryMetadata& aDirectoryMetadata);
90 nsresult RemoveNsIFileRecursively(
91 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, nsIFile& aFile,
92 bool aTrackQuota = true);
94 // XXX Remove this method when all callers properly wrap aClientMetadata with
95 // Some/Nothing
96 inline nsresult RemoveNsIFileRecursively(
97 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aFile,
98 bool aTrackQuota = true) {
99 return RemoveNsIFileRecursively(Some(aDirectoryMetadata), aFile, aTrackQuota);
102 // Delete a file that you think exists. If the file doesn't exist, an error
103 // will not be returned, but warning telemetry will be generated! So only call
104 // this on files that you know exist (idempotent usage, but it's not
105 // recommended).
106 nsresult RemoveNsIFile(const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata,
107 nsIFile& aFile, bool aTrackQuota = true);
109 // XXX Remove this method when all callers properly wrap aClientMetadata with
110 // Some/Nothing
111 inline nsresult RemoveNsIFile(const CacheDirectoryMetadata& aDirectoryMetadata,
112 nsIFile& aFile, bool aTrackQuota = true) {
113 return RemoveNsIFile(Some(aDirectoryMetadata), aFile, aTrackQuota);
116 void DecreaseUsageForDirectoryMetadata(
117 const CacheDirectoryMetadata& aDirectoryMetadata, int64_t aUpdatingSize);
120 * This function is used to check if the directory padding file is existed.
122 bool DirectoryPaddingFileExists(nsIFile& aBaseDir,
123 DirPaddingFile aPaddingFileType);
127 * The functions below are used to read/write/delete the directory padding file
128 * after acquiring the mutex lock. The mutex lock is held by
129 * CacheQuotaClient to prevent multi-thread accessing issue. To avoid deadlock,
130 * these functions should only access by static functions in
131 * dom/cache/QuotaClient.cpp.
135 // Returns a Result with a success value denoting the padding size.
136 Result<int64_t, nsresult> DirectoryPaddingGet(nsIFile& aBaseDir);
138 nsresult DirectoryPaddingInit(nsIFile& aBaseDir);
140 nsresult UpdateDirectoryPaddingFile(nsIFile& aBaseDir,
141 mozIStorageConnection& aConn,
142 int64_t aIncreaseSize,
143 int64_t aDecreaseSize,
144 bool aTemporaryFileExist);
146 nsresult DirectoryPaddingFinalizeWrite(nsIFile& aBaseDir);
148 // Returns a Result with a success value denoting the padding size.
149 Result<int64_t, nsresult> DirectoryPaddingRestore(nsIFile& aBaseDir,
150 mozIStorageConnection& aConn,
151 bool aMustRestore);
153 nsresult DirectoryPaddingDeleteFile(nsIFile& aBaseDir,
154 DirPaddingFile aPaddingFileType);
155 } // namespace mozilla::dom::cache
157 #endif // mozilla_dom_cache_FileUtils_h