FileUtils.h (5877B)
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/. */ 6 7 #ifndef mozilla_dom_cache_FileUtils_h 8 #define mozilla_dom_cache_FileUtils_h 9 10 #include "CacheCipherKeyManager.h" 11 #include "CacheCommon.h" 12 #include "mozIStorageConnection.h" 13 #include "mozilla/dom/cache/Types.h" 14 #include "nsStreamUtils.h" 15 #include "nsTArrayForwardDeclare.h" 16 17 struct nsID; 18 class nsIFile; 19 20 namespace mozilla::dom::cache { 21 22 #define PADDING_FILE_NAME u".padding" 23 #define PADDING_TMP_FILE_NAME u".padding-tmp" 24 25 enum class DirPaddingFile { FILE, TMP_FILE }; 26 27 nsresult BodyCreateDir(nsIFile& aBaseDir); 28 29 // Note that this function can only be used during the initialization of the 30 // database. We're unlikely to be able to delete the DB successfully past 31 // that point due to the file being in use. 32 nsresult BodyDeleteDir(const CacheDirectoryMetadata& aDirectoryMetadata, 33 nsIFile& aBaseDir); 34 35 // Returns a Result with a success value with the body id and, optionally, the 36 // copy context. 37 Result<nsCOMPtr<nsISupports>, nsresult> BodyStartWriteStream( 38 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, 39 const nsID& aBodyId, Maybe<CipherKey> aMaybeCipherKey, 40 nsIInputStream& aSource, void* aClosure, nsAsyncCopyCallbackFun aCallback); 41 42 void BodyCancelWrite(nsISupports& aCopyContext); 43 44 Result<int64_t, nsresult> BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId); 45 46 Result<int64_t, nsresult> GetBodyDiskSize(nsIFile& aBaseDir, const nsID& aId); 47 48 Result<MovingNotNull<nsCOMPtr<nsIInputStream>>, nsresult> BodyOpen( 49 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, 50 const nsID& aId, Maybe<CipherKey> aMaybeCipherKey); 51 52 nsresult BodyMaybeUpdatePaddingSize( 53 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, 54 const nsID& aId, uint32_t aPaddingInfo, int64_t* aPaddingSizeInOut); 55 56 nsresult BodyDeleteFiles(const CacheDirectoryMetadata& aDirectoryMetadata, 57 nsIFile& aBaseDir, const nsTArray<nsID>& aIdList); 58 59 // Traverse all cache directorys and do a cleanup, leaving only files that 60 // belong to known body ids behind. 61 nsresult BodyDeleteOrphanedFiles( 62 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, 63 nsTHashSet<nsID>& aKnownBodyIds); 64 65 // Helper for BodyDeleteOrphanedFiles that must only be used on the cache 66 // thread. 67 template <typename Func> 68 nsresult BodyTraverseFilesForCleanup( 69 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, nsIFile& aBodyDir, 70 const Func& aHandleFileFunc); 71 72 nsresult CreateMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata); 73 74 nsresult DeleteMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata); 75 76 bool MarkerFileExists(const CacheDirectoryMetadata& aDirectoryMetadata); 77 78 nsresult RemoveNsIFileRecursively( 79 const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, nsIFile& aFile, 80 bool aTrackQuota = true); 81 82 // XXX Remove this method when all callers properly wrap aClientMetadata with 83 // Some/Nothing 84 inline nsresult RemoveNsIFileRecursively( 85 const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aFile, 86 bool aTrackQuota = true) { 87 return RemoveNsIFileRecursively(Some(aDirectoryMetadata), aFile, aTrackQuota); 88 } 89 90 // Delete a file that you think exists. If the file doesn't exist, an error 91 // will not be returned, but warning telemetry will be generated! So only call 92 // this on files that you know exist (idempotent usage, but it's not 93 // recommended). 94 nsresult RemoveNsIFile(const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata, 95 nsIFile& aFile, bool aTrackQuota = true); 96 97 // XXX Remove this method when all callers properly wrap aClientMetadata with 98 // Some/Nothing 99 inline nsresult RemoveNsIFile(const CacheDirectoryMetadata& aDirectoryMetadata, 100 nsIFile& aFile, bool aTrackQuota = true) { 101 return RemoveNsIFile(Some(aDirectoryMetadata), aFile, aTrackQuota); 102 } 103 104 void DecreaseUsageForDirectoryMetadata( 105 const CacheDirectoryMetadata& aDirectoryMetadata, int64_t aUpdatingSize); 106 107 /** 108 * This function is used to check if the directory padding file is existed. 109 */ 110 bool DirectoryPaddingFileExists(nsIFile& aBaseDir, 111 DirPaddingFile aPaddingFileType); 112 113 /** 114 * 115 * The functions below are used to read/write/delete the directory padding file 116 * after acquiring the mutex lock. The mutex lock is held by 117 * CacheQuotaClient to prevent multi-thread accessing issue. To avoid deadlock, 118 * these functions should only access by static functions in 119 * dom/cache/QuotaClient.cpp. 120 * 121 */ 122 123 // Returns a Result with a success value denoting the padding size. 124 Result<int64_t, nsresult> DirectoryPaddingGet(nsIFile& aBaseDir); 125 126 nsresult DirectoryPaddingInit(nsIFile& aBaseDir); 127 128 nsresult UpdateDirectoryPaddingFile(nsIFile& aBaseDir, 129 mozIStorageConnection& aConn, 130 int64_t aIncreaseSize, 131 int64_t aDecreaseSize, 132 bool aTemporaryFileExist); 133 134 nsresult DirectoryPaddingFinalizeWrite(nsIFile& aBaseDir); 135 136 // Returns a Result with a success value denoting the padding size. 137 Result<int64_t, nsresult> DirectoryPaddingRestore(nsIFile& aBaseDir, 138 mozIStorageConnection& aConn, 139 bool aMustRestore); 140 141 nsresult DirectoryPaddingDeleteFile(nsIFile& aBaseDir, 142 DirPaddingFile aPaddingFileType); 143 } // namespace mozilla::dom::cache 144 145 #endif // mozilla_dom_cache_FileUtils_h