FileSystemDatabaseManager.h (7885B)
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 DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGER_H_ 8 #define DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGER_H_ 9 10 #include "ResultConnection.h" 11 #include "mozilla/dom/FileSystemTypes.h" 12 #include "mozilla/dom/quota/ForwardDecls.h" 13 #include "mozilla/dom/quota/UsageInfo.h" 14 #include "nsStringFwd.h" 15 16 template <class T> 17 class nsCOMPtr; 18 19 class nsIFile; 20 21 namespace mozilla { 22 23 template <typename V, typename E> 24 class Result; 25 26 namespace dom { 27 28 namespace quota { 29 30 struct OriginMetadata; 31 32 } // namespace quota 33 34 namespace fs { 35 36 struct FileId; 37 enum class FileMode; 38 class FileSystemChildMetadata; 39 class FileSystemEntryMetadata; 40 class FileSystemDirectoryListing; 41 class FileSystemEntryPair; 42 43 namespace data { 44 45 using FileSystemConnection = fs::ResultConnection; 46 47 class FileSystemDatabaseManager { 48 public: 49 /** 50 * @brief Updates stored usage data for all tracked files. 51 * 52 * @return nsresult error code 53 */ 54 static nsresult RescanUsages(const ResultConnection& aConnection, 55 const quota::OriginMetadata& aOriginMetadata); 56 57 /** 58 * @brief Obtains the current total usage for origin and connection. 59 * 60 * @return Result<quota::UsageInfo, QMResult> On success, 61 * - field UsageInfo::DatabaseUsage contains the sum of current 62 * total database and file usage, 63 * - field UsageInfo::FileUsage is not used and should be equal to Nothing. 64 * 65 * If the disk is inaccessible, various IO related errors may be returned. 66 */ 67 static Result<quota::UsageInfo, QMResult> GetUsage( 68 const ResultConnection& aConnection, 69 const quota::OriginMetadata& aOriginMetadata); 70 71 /** 72 * @brief Obtains the current total usage. 73 * 74 * @return Result<quota::UsageInfo, QMResult> On success, 75 * - field UsageInfo::DatabaseUsage contains the sum of current 76 * total database and file usage, 77 * - field UsageInfo::FileUsage is not used and should be equal to Nothing. 78 */ 79 virtual Result<quota::UsageInfo, QMResult> GetUsage() const = 0; 80 81 /** 82 * @brief Refreshes the stored file size. 83 * 84 * @param aEntry EntryId of the file whose size is refreshed. 85 */ 86 virtual nsresult UpdateUsage(const FileId& aFileId) = 0; 87 88 /** 89 * @brief Returns directory identifier, optionally creating it if it doesn't 90 * exist 91 * 92 * @param aHandle Current directory and filename 93 * @return Result<bool, QMResult> Directory identifier or error 94 */ 95 virtual Result<EntryId, QMResult> GetOrCreateDirectory( 96 const FileSystemChildMetadata& aHandle, bool aCreate) = 0; 97 98 /** 99 * @brief Returns file identifier, optionally creating it if it doesn't exist 100 * 101 * @param aHandle Current directory and filename 102 * @param aType Content type which is ignored if the file already exists 103 * @param aCreate true if file is to be created when it does not already exist 104 * @return Result<bool, QMResult> File identifier or error 105 */ 106 virtual Result<EntryId, QMResult> GetOrCreateFile( 107 const FileSystemChildMetadata& aHandle, bool aCreate) = 0; 108 109 /** 110 * @brief Returns the properties of a file corresponding to a file handle 111 */ 112 virtual nsresult GetFile(const EntryId& aEntryId, const FileId& aFileId, 113 const FileMode& aMode, ContentType& aType, 114 TimeStamp& lastModifiedMilliSeconds, Path& aPath, 115 nsCOMPtr<nsIFile>& aFile) const = 0; 116 117 virtual Result<FileSystemDirectoryListing, QMResult> GetDirectoryEntries( 118 const EntryId& aParent, PageNumber aPage) const = 0; 119 120 /** 121 * @brief Removes a directory 122 * 123 * @param aHandle Current directory and filename 124 * @return Result<bool, QMResult> False if file did not exist, otherwise true 125 * or error 126 */ 127 virtual Result<bool, QMResult> RemoveDirectory( 128 const FileSystemChildMetadata& aHandle, bool aRecursive) = 0; 129 130 /** 131 * @brief Removes a file 132 * 133 * @param aHandle Current directory and filename 134 * @return Result<bool, QMResult> False if file did not exist, otherwise true 135 * or error 136 */ 137 virtual Result<bool, QMResult> RemoveFile( 138 const FileSystemChildMetadata& aHandle) = 0; 139 140 /** 141 * @brief Rename a file/directory 142 * 143 * @param aHandle Source directory or file 144 * @param aNewName New entry name 145 * @return Result<EntryId, QMResult> The relevant entry id or error 146 */ 147 virtual Result<EntryId, QMResult> RenameEntry( 148 const FileSystemEntryMetadata& aHandle, const Name& aNewName) = 0; 149 150 /** 151 * @brief Move a file/directory 152 * 153 * @param aHandle Source directory or file 154 * @param aNewDesignation Destination directory and entry name 155 * @return Result<EntryId, QMResult> The relevant entry id or error 156 */ 157 virtual Result<EntryId, QMResult> MoveEntry( 158 const FileSystemEntryMetadata& aHandle, 159 const FileSystemChildMetadata& aNewDesignation) = 0; 160 161 /** 162 * @brief Tries to connect a parent directory to a file system item with a 163 * path, excluding the parent directory 164 * 165 * @param aHandle Pair of parent directory and child item candidates 166 * @return Result<Path, QMResult> Path or error if no it didn't exists 167 */ 168 virtual Result<Path, QMResult> Resolve( 169 const FileSystemEntryPair& aEndpoints) const = 0; 170 171 /** 172 * @brief Returns true only if a file with a given EntryId exists. 173 */ 174 virtual Result<bool, QMResult> DoesFileExist( 175 const EntryId& aEntryId) const = 0; 176 177 /** 178 * @brief Generates an EntryId for a given parent EntryId and filename. 179 */ 180 virtual Result<EntryId, QMResult> GetEntryId( 181 const FileSystemChildMetadata& aHandle) const = 0; 182 183 /** 184 * @brief To check if a file under a directory is locked, we need to map 185 * fileId's to entries. 186 * 187 * @param aFileId a FileId 188 * @return Result<EntryId, QMResult> Entry id of a temporary or main file 189 */ 190 virtual Result<EntryId, QMResult> GetEntryId(const FileId& aFileId) const = 0; 191 192 /** 193 * @brief Make sure EntryId maps to a FileId. This method should be called 194 * before exclusive locking is attempted. 195 */ 196 virtual Result<FileId, QMResult> EnsureFileId(const EntryId& aEntryId) = 0; 197 198 /** 199 * @brief Make sure EntryId maps to a temporary FileId. This method should be 200 * called before shared locking is attempted. 201 */ 202 virtual Result<FileId, QMResult> EnsureTemporaryFileId( 203 const EntryId& aEntryId) = 0; 204 205 /** 206 * @brief To support moves in metadata, the actual files on disk are tagged 207 * with file id's which are mapped to entry id's which represent paths. 208 * This function returns the main file corresponding to an entry. 209 * 210 * @param aEntryId An id of an entry 211 * @return Result<EntryId, QMResult> Main file id, used by exclusive locks 212 */ 213 virtual Result<FileId, QMResult> GetFileId(const EntryId& aEntryId) const = 0; 214 215 /** 216 * @brief Flag aFileId as the main file for aEntryId or abort. Removes the 217 * file which did not get flagged as the main file. 218 */ 219 virtual nsresult MergeFileId(const EntryId& aEntryId, const FileId& aFileId, 220 bool aAbort) = 0; 221 222 /** 223 * @brief Close database connection. 224 */ 225 virtual void Close() = 0; 226 227 /** 228 * @brief Start tracking file's usage. 229 */ 230 virtual nsresult BeginUsageTracking(const FileId& aFileId) = 0; 231 232 /** 233 * @brief Stop tracking file's usage. 234 */ 235 virtual nsresult EndUsageTracking(const FileId& aFileId) = 0; 236 237 virtual ~FileSystemDatabaseManager() = default; 238 }; 239 240 } // namespace data 241 } // namespace fs 242 } // namespace dom 243 } // namespace mozilla 244 245 #endif // DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGER_H_