tor-browser

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

FileSystemFileManager.h (6179B)


      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_FILESYSTEMFILEMANAGER_H_
      8 #define DOM_FS_PARENT_DATAMODEL_FILESYSTEMFILEMANAGER_H_
      9 
     10 #include "ErrorList.h"
     11 #include "mozilla/UniquePtr.h"
     12 #include "mozilla/dom/FileSystemTypes.h"
     13 #include "mozilla/dom/QMResult.h"
     14 #include "nsIFile.h"
     15 #include "nsString.h"
     16 
     17 template <class T>
     18 class nsCOMPtr;
     19 
     20 class nsIFileURL;
     21 
     22 namespace mozilla::dom {
     23 
     24 namespace quota {
     25 
     26 struct OriginMetadata;
     27 
     28 }  // namespace quota
     29 
     30 namespace fs {
     31 
     32 struct FileId;
     33 
     34 namespace data {
     35 
     36 /**
     37 * @brief Get the directory for file system items of specified origin.
     38 * Use this instead of constructing the path from quota manager's storage path.
     39 *
     40 * @param aOrigin Specified origin
     41 * @return Result<nsCOMPtr<nsIFile>, QMResult> Top file system directory
     42 */
     43 Result<nsCOMPtr<nsIFile>, QMResult> GetFileSystemDirectory(
     44    const quota::OriginMetadata& aOriginMetadata);
     45 
     46 /**
     47 * @brief Ensure that the origin-specific directory for file system exists.
     48 *
     49 * @param aOriginMetadata Specified origin metadata
     50 * @return nsresult Error if operation failed
     51 */
     52 nsresult EnsureFileSystemDirectory(
     53    const quota::OriginMetadata& aOriginMetadata);
     54 
     55 /**
     56 * @brief Get file system's database path for specified origin.
     57 * Use this to get the database path instead of constructing it from
     58 * quota manager's storage path - without the side effect of potentially
     59 * creating it.
     60 *
     61 * @param aOrigin Specified origin
     62 * @return Result<nsCOMPtr<nsIFile>, QMResult> Database file object
     63 */
     64 Result<nsCOMPtr<nsIFile>, QMResult> GetDatabaseFile(
     65    const quota::OriginMetadata& aOriginMetadata);
     66 
     67 /**
     68 * @brief Get file system's database url with directory lock parameter for
     69 * specified origin. Use this to open a database connection and have the quota
     70 * manager guard against its deletion or busy errors due to other connections.
     71 *
     72 * @param aOrigin Specified origin
     73 * @param aDirectoryLockId Directory lock id from the quota manager
     74 * @return Result<nsCOMPtr<nsIFileURL>, QMResult> Database file URL object
     75 */
     76 Result<nsCOMPtr<nsIFileURL>, QMResult> GetDatabaseFileURL(
     77    const quota::OriginMetadata& aOriginMetadata,
     78    const int64_t aDirectoryLockId);
     79 
     80 /**
     81 * @brief Creates and removes disk-backed representations of the file systems'
     82 * file entries for a specified origin.
     83 *
     84 * Other components should not depend on how the files are organized on disk
     85 * but instead rely on the entry id and have access to the local file using the
     86 * GetOrCreateFile result.
     87 *
     88 * The local paths are not necessarily stable in the long term and if they
     89 * absolutely must be cached, there should be a way to repopulate the cache
     90 * after an internal reorganization of the file entry represenations on disk,
     91 * for some currently unforeseen maintenance reason.
     92 *
     93 * Example: if GetOrCreateFile used to map entryId 'abc' to path '/c/u/1/123'
     94 * and now it maps it to '/d/u/1/12/123', the cache should either update all
     95 * paths at once through a migration, or purge them and save a new value
     96 * whenever a call to GetOrCreateFile is made.
     97 */
     98 class FileSystemFileManager {
     99 public:
    100  /**
    101   * @brief Create a File System File Manager object for a specified origin.
    102   *
    103   * @param aOrigin
    104   * @return Result<FileSystemFileManager, QMResult>
    105   */
    106  static Result<UniquePtr<FileSystemFileManager>, QMResult>
    107  CreateFileSystemFileManager(const quota::OriginMetadata& aOriginMetadata);
    108 
    109  /**
    110   * @brief Create a File System File Manager object which keeps file entries
    111   * under a specified directory instead of quota manager's storage path.
    112   * This should only be used for testing and preferably removed.
    113   *
    114   * @param topDirectory
    115   * @return Result<FileSystemFileManager, QMResult>
    116   */
    117  static Result<FileSystemFileManager, QMResult> CreateFileSystemFileManager(
    118      nsCOMPtr<nsIFile>&& topDirectory);
    119 
    120  /**
    121   * @brief Get a file object for a specified entry id. If a file for the entry
    122   * does not exist, returns an appropriate error.
    123   *
    124   * @param aEntryId Specified id of a file system entry
    125   * @return Result<nsCOMPtr<nsIFile>, QMResult> File or error.
    126   */
    127  Result<nsCOMPtr<nsIFile>, QMResult> GetFile(const FileId& aFileId) const;
    128 
    129  /**
    130   * @brief Get or create a disk-backed file object for a specified entry id.
    131   *
    132   * @param aFileId Specified id of a file system entry
    133   * @return Result<nsCOMPtr<nsIFile>, QMResult> File abstraction or IO error
    134   */
    135  Result<nsCOMPtr<nsIFile>, QMResult> GetOrCreateFile(const FileId& aFileId);
    136 
    137  /**
    138   * @brief Create a disk-backed file object as a copy.
    139   *
    140   * @param aDestinationFileId Specified id of file to be created
    141   * @param aSourceFileId Specified id of the file from which we make a copy
    142   * @return Result<nsCOMPtr<nsIFile>, QMResult> File abstraction or IO error
    143   */
    144  Result<nsCOMPtr<nsIFile>, QMResult> CreateFileFrom(
    145      const FileId& aDestinationFileId, const FileId& aSourceFileId);
    146 
    147  /**
    148   * @brief Remove the disk-backed file object for a specified entry id.
    149   * Note: The returned value is 0 in release builds.
    150   *
    151   * @param aFileId  Specified id of a file system entry
    152   * @return Result<Usage, QMResult> Error or file size
    153   */
    154  Result<Usage, QMResult> RemoveFile(const FileId& aFileId);
    155 
    156  /**
    157   * @brief This method can be used to try to delete a group of files from the
    158   * disk. In debug builds, the sum of the usages is provided ad return value,
    159   * in release builds the sum is not calculated.
    160   * The method attempts to remove all the files requested.
    161   */
    162  Result<DebugOnly<Usage>, QMResult> RemoveFiles(
    163      const nsTArray<FileId>& aFileIds, nsTArray<FileId>& aFailedRemovals);
    164 
    165 private:
    166  explicit FileSystemFileManager(nsCOMPtr<nsIFile>&& aTopDirectory);
    167 
    168  nsCOMPtr<nsIFile> mTopDirectory;
    169 };
    170 
    171 }  // namespace data
    172 }  // namespace fs
    173 }  // namespace mozilla::dom
    174 
    175 #endif  // DOM_FS_PARENT_DATAMODEL_FILESYSTEMFILEMANAGER_H_