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 "mozilla/Attributes.h" 11 #include "mozilla/dom/cache/Types.h" 12 #include "CacheCommon.h" 13 #include "mozIStorageConnection.h" 14 #include "nsStreamUtils.h" 15 #include "nsTArrayForwardDeclare.h" 16 17 struct nsID; 18 class nsIFile; 19 20 namespace mozilla { 21 namespace dom { 22 namespace cache { 23 24 #define PADDING_FILE_NAME u".padding" 25 #define PADDING_TMP_FILE_NAME u".padding-tmp" 26 27 enum DirPaddingFile { FILE, TMP_FILE }; 28 29 nsresult BodyCreateDir(nsIFile& aBaseDir); 30 31 // Note that this function can only be used during the initialization of the 32 // database. We're unlikely to be able to delete the DB successfully past 33 // that point due to the file being in use. 34 nsresult BodyDeleteDir(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir); 35 36 // Returns a Result with a success value with the body id and, optionally, the 37 // copy context. 38 Result<std::pair<nsID, nsCOMPtr<nsISupports>>, nsresult> BodyStartWriteStream( 39 const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, nsIInputStream& aSource, 40 void* aClosure, nsAsyncCopyCallbackFun aCallback); 41 42 void BodyCancelWrite(nsISupports& aCopyContext); 43 44 nsresult BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId); 45 46 Result<NotNull<nsCOMPtr<nsIInputStream>>, nsresult> BodyOpen( 47 const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, const nsID& aId); 48 49 nsresult BodyMaybeUpdatePaddingSize(const QuotaInfo& aQuotaInfo, 50 nsIFile& aBaseDir, const nsID& aId, 51 uint32_t aPaddingInfo, 52 int64_t* aPaddingSizeInOut); 53 54 nsresult BodyDeleteFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, 55 const nsTArray<nsID>& aIdList); 56 57 nsresult BodyDeleteOrphanedFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBaseDir, 58 const nsTArray<nsID>& aKnownBodyIdList); 59 60 // If aCanRemoveFiles is true, that means we are safe to touch the files which 61 // can be accessed in other threads. 62 // If it's not, that means we cannot remove the files which are possible to 63 // created by other threads. Note that if the files are not expected, we should 64 // be safe to remove them in any case. 65 template <typename Func> 66 nsresult BodyTraverseFiles(const QuotaInfo& aQuotaInfo, nsIFile& aBodyDir, 67 const Func& aHandleFileFunc, bool aCanRemoveFiles, 68 bool aTrackQuota = true); 69 70 nsresult CreateMarkerFile(const QuotaInfo& aQuotaInfo); 71 72 nsresult DeleteMarkerFile(const QuotaInfo& aQuotaInfo); 73 74 bool MarkerFileExists(const QuotaInfo& aQuotaInfo); 75 76 nsresult RemoveNsIFileRecursively(const QuotaInfo& aQuotaInfo, nsIFile& aFile, 77 bool aTrackQuota = true); 78 79 // Delete a file that you think exists. If the file doesn't exist, an error 80 // will not be returned, but warning telemetry will be generated! So only call 81 // this on files that you know exist (idempotent usage, but it's not 82 // recommended). 83 nsresult RemoveNsIFile(const QuotaInfo& aQuotaInfo, nsIFile& aFile, 84 bool aTrackQuota = true); 85 86 void DecreaseUsageForQuotaInfo(const QuotaInfo& aQuotaInfo, 87 int64_t aUpdatingSize); 88 89 /** 90 * This function is used to check if the directory padding file is existed. 91 */ 92 bool DirectoryPaddingFileExists(nsIFile& aBaseDir, 93 DirPaddingFile aPaddingFileType); 94 95 /** 96 * 97 * The functions below are used to read/write/delete the directory padding file 98 * after acquiring the mutex lock. The mutex lock is held by 99 * CacheQuotaClient to prevent multi-thread accessing issue. To avoid deadlock, 100 * these functions should only access by static functions in 101 * dom/cache/QuotaClient.cpp. 102 * 103 */ 104 105 // Returns a Result with a success value denoting the padding size. 106 Result<int64_t, nsresult> DirectoryPaddingGet(nsIFile& aBaseDir); 107 108 nsresult DirectoryPaddingInit(nsIFile& aBaseDir); 109 110 nsresult UpdateDirectoryPaddingFile(nsIFile& aBaseDir, 111 mozIStorageConnection& aConn, 112 int64_t aIncreaseSize, 113 int64_t aDecreaseSize, 114 bool aTemporaryFileExist); 115 116 nsresult DirectoryPaddingFinalizeWrite(nsIFile& aBaseDir); 117 118 // Returns a Result with a success value denoting the padding size. 119 Result<int64_t, nsresult> DirectoryPaddingRestore(nsIFile& aBaseDir, 120 mozIStorageConnection& aConn, 121 bool aMustRestore); 122 123 nsresult DirectoryPaddingDeleteFile(nsIFile& aBaseDir, 124 DirPaddingFile aPaddingFileType); 125 } // namespace cache 126 } // namespace dom 127 } // namespace mozilla 128 129 #endif // mozilla_dom_cache_FileUtils_h 130