tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FileSystemDatabaseManagerVersion001.h (8401B)


      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_FILESYSTEMDATABASEMANAGERVERSION001_H_
      8 #define DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGERVERSION001_H_
      9 
     10 #include "FileSystemDatabaseManager.h"
     11 #include "mozilla/dom/quota/CommonMetadata.h"
     12 #include "mozilla/dom/quota/ResultExtensions.h"
     13 #include "nsString.h"
     14 
     15 namespace mozilla::dom::fs {
     16 
     17 struct FileId;
     18 
     19 namespace data {
     20 
     21 class FileSystemDataManager;
     22 class FileSystemFileManager;
     23 
     24 /**
     25 * @brief Versioned implementation of database interface enables backwards
     26 * support after the schema has changed. Version number 0 refers to
     27 * uninitialized database, and versions after that are sequential upgrades.
     28 *
     29 * To change the schema to the next version x,
     30 * - a new implementation FileSystemDatabaseManagerVersion00x is derived from
     31 * the previous version and the required methods are overridden
     32 * - a new apppropriate schema initialization class SchemaVersion00x is created
     33 * or derived
     34 * - the factory method of FileSystemDatabaseManager is extended to try to
     35 * migrate the data from the previous version to version x, and to return
     36 * FileSystemDatabaseManagerVersion00x implementation if the database version
     37 * after the migrations is x
     38 * - note that if the migration fails at some old version, the corresponding
     39 * old implementation should be returned: this way the users whose migrations
     40 * fail systematically due to hardware or other issues will not get locked out
     41 */
     42 class FileSystemDatabaseManagerVersion001 : public FileSystemDatabaseManager {
     43 public:
     44  FileSystemDatabaseManagerVersion001(
     45      FileSystemDataManager* aDataManager, FileSystemConnection&& aConnection,
     46      UniquePtr<FileSystemFileManager>&& aFileManager,
     47      const EntryId& aRootEntry);
     48 
     49  /* Static to allow use by quota client without instantiation */
     50  static nsresult RescanTrackedUsages(
     51      const FileSystemConnection& aConnection,
     52      const quota::OriginMetadata& aOriginMetadata);
     53 
     54  /* Static to allow use by quota client without instantiation */
     55  static Result<Usage, QMResult> GetFileUsage(
     56      const FileSystemConnection& aConnection);
     57 
     58  Result<quota::UsageInfo, QMResult> GetUsage() const override;
     59 
     60  nsresult UpdateUsage(const FileId& aFileId) override;
     61 
     62  Result<EntryId, QMResult> GetOrCreateDirectory(
     63      const FileSystemChildMetadata& aHandle, bool aCreate) override;
     64 
     65  Result<EntryId, QMResult> GetOrCreateFile(
     66      const FileSystemChildMetadata& aHandle, bool aCreate) override;
     67 
     68  nsresult GetFile(const EntryId& aEntryId, const FileId& aFileId,
     69                   const FileMode& aMode, ContentType& aType,
     70                   TimeStamp& lastModifiedMilliSeconds, Path& aPath,
     71                   nsCOMPtr<nsIFile>& aFile) const override;
     72 
     73  Result<FileSystemDirectoryListing, QMResult> GetDirectoryEntries(
     74      const EntryId& aParent, PageNumber aPage) const override;
     75 
     76  Result<bool, QMResult> RemoveDirectory(const FileSystemChildMetadata& aHandle,
     77                                         bool aRecursive) override;
     78 
     79  Result<bool, QMResult> RemoveFile(
     80      const FileSystemChildMetadata& aHandle) override;
     81 
     82  Result<EntryId, QMResult> RenameEntry(const FileSystemEntryMetadata& aHandle,
     83                                        const Name& aNewName) override;
     84 
     85  Result<EntryId, QMResult> MoveEntry(
     86      const FileSystemEntryMetadata& aHandle,
     87      const FileSystemChildMetadata& aNewDesignation) override;
     88 
     89  Result<Path, QMResult> Resolve(
     90      const FileSystemEntryPair& aEndpoints) const override;
     91 
     92  Result<bool, QMResult> DoesFileExist(const EntryId& aEntryId) const override;
     93 
     94  Result<EntryId, QMResult> GetEntryId(
     95      const FileSystemChildMetadata& aHandle) const override;
     96 
     97  Result<EntryId, QMResult> GetEntryId(const FileId& aFileId) const override;
     98 
     99  Result<FileId, QMResult> EnsureFileId(const EntryId& aEntryId) override;
    100 
    101  Result<FileId, QMResult> EnsureTemporaryFileId(
    102      const EntryId& aEntryId) override;
    103 
    104  Result<FileId, QMResult> GetFileId(const EntryId& aEntryId) const override;
    105 
    106  nsresult MergeFileId(const EntryId& aEntryId, const FileId& aFileId,
    107                       bool aAbort) override;
    108 
    109  void Close() override;
    110 
    111  nsresult BeginUsageTracking(const FileId& aFileId) override;
    112 
    113  nsresult EndUsageTracking(const FileId& aFileId) override;
    114 
    115  virtual ~FileSystemDatabaseManagerVersion001() = default;
    116 
    117 protected:
    118  virtual Result<bool, QMResult> DoesFileIdExist(const FileId& aFileId) const;
    119 
    120  virtual nsresult RemoveFileId(const FileId& aFileId);
    121 
    122  virtual Result<std::pair<nsTArray<FileId>, Usage>, QMResult>
    123  FindFilesWithoutDeprecatedLocksUnderEntry(const EntryId& aEntryId) const;
    124 
    125  virtual Result<nsTArray<std::pair<EntryId, FileId>>, QMResult>
    126  FindFileEntriesUnderDirectory(const EntryId& aEntryId) const;
    127 
    128  nsresult SetUsageTracking(const FileId& aFileId, bool aTracked);
    129 
    130  nsresult UpdateUsageInDatabase(const FileId& aFileId, Usage aNewDiskUsage);
    131 
    132  Result<Ok, QMResult> EnsureUsageIsKnown(const FileId& aFileId);
    133 
    134  void DecreaseCachedQuotaUsage(int64_t aDelta);
    135 
    136  nsresult UpdateCachedQuotaUsage(const FileId& aFileId, Usage aOldUsage,
    137                                  Usage aNewUsage) const;
    138 
    139  Result<bool, QMResult> RemoveDirectoryImpl(const EntryId& aEntryId);
    140 
    141  nsresult ClearDestinationIfNotLocked(
    142      const FileSystemConnection& aConnection,
    143      const FileSystemDataManager* const aDataManager,
    144      const FileSystemEntryMetadata& aHandle,
    145      const FileSystemChildMetadata& aNewDesignation);
    146 
    147  nsresult PrepareRenameEntry(const FileSystemConnection& aConnection,
    148                              const FileSystemDataManager* const aDataManager,
    149                              const FileSystemEntryMetadata& aHandle,
    150                              const Name& aNewName, bool aIsFile);
    151 
    152  nsresult PrepareMoveEntry(const FileSystemConnection& aConnection,
    153                            const FileSystemDataManager* const aDataManager,
    154                            const FileSystemEntryMetadata& aHandle,
    155                            const FileSystemChildMetadata& aNewDesignation,
    156                            bool aIsFile);
    157 
    158  // This is a raw pointer since we're owned by the FileSystemDataManager.
    159  FileSystemDataManager* MOZ_NON_OWNING_REF mDataManager;
    160 
    161  FileSystemConnection mConnection;
    162 
    163  UniquePtr<FileSystemFileManager> mFileManager;
    164 
    165  const EntryId mRootEntry;
    166 
    167  const quota::ClientMetadata mClientMetadata;
    168 
    169  int32_t mFilesOfUnknownUsage;
    170 };
    171 
    172 inline auto toNSResult = [](const auto& aRv) { return ToNSResult(aRv); };
    173 
    174 Result<bool, QMResult> ApplyEntryExistsQuery(
    175    const FileSystemConnection& aConnection, const nsACString& aQuery,
    176    const FileSystemChildMetadata& aHandle);
    177 
    178 Result<bool, QMResult> ApplyEntryExistsQuery(
    179    const FileSystemConnection& aConnection, const nsACString& aQuery,
    180    const EntryId& aEntry);
    181 
    182 Result<bool, QMResult> IsFile(const FileSystemConnection& aConnection,
    183                              const EntryId& aEntryId);
    184 
    185 Result<EntryId, QMResult> FindEntryId(const FileSystemConnection& aConnection,
    186                                      const FileSystemChildMetadata& aHandle,
    187                                      bool aIsFile);
    188 
    189 Result<EntryId, QMResult> FindParent(const FileSystemConnection& aConnection,
    190                                     const EntryId& aEntryId);
    191 
    192 Result<bool, QMResult> IsSame(const FileSystemConnection& aConnection,
    193                              const FileSystemEntryMetadata& aHandle,
    194                              const FileSystemChildMetadata& aNewHandle,
    195                              bool aIsFile);
    196 
    197 Result<Path, QMResult> ResolveReversedPath(
    198    const FileSystemConnection& aConnection,
    199    const FileSystemEntryPair& aEndpoints);
    200 
    201 nsresult GetFileAttributes(const FileSystemConnection& aConnection,
    202                           const EntryId& aEntryId, ContentType& aType);
    203 
    204 void TryRemoveDuringIdleMaintenance(const nsTArray<FileId>& aItemToRemove);
    205 
    206 ContentType DetermineContentType(const Name& aName);
    207 
    208 }  // namespace data
    209 }  // namespace mozilla::dom::fs
    210 
    211 #endif  // DOM_FS_PARENT_DATAMODEL_FILESYSTEMDATABASEMANAGERVERSION001_H_