obfuscated_file_util_memory_delegate.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/containers/span.h"
  11. #include "base/files/file.h"
  12. #include "base/files/file_path.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/time/time.h"
  15. #include "storage/browser/file_system/native_file_util.h"
  16. #include "storage/browser/file_system/obfuscated_file_util_delegate.h"
  17. namespace storage {
  18. // This delegate performs all ObfuscatedFileUtil tasks that require touching
  19. // disk and peforms them in memory.
  20. // The given paths should be subpaths of the |file_system_directory| folder
  21. // passed to the constructor. No normalization is done on the this root folder
  22. // and it is expected that all input paths would start with it.
  23. // Directories and files are both stored in |Entry| structures, keeping the type
  24. // of the entry, creation, modification, and last access time, and
  25. // |file_content| or |directory_content| based on the type.
  26. // Directory tree is kept in a tree structure starting from |root_|. All API
  27. // functions that receive a |FilePath|, convert it to a |DecomposedPath| which
  28. // has separated normalized components, is ensured that it is under the root,
  29. // and has the respective |Entry| (if exists) and its parent in the directory
  30. // tree starting from |root_|.
  31. class COMPONENT_EXPORT(STORAGE_BROWSER) ObfuscatedFileUtilMemoryDelegate
  32. : public ObfuscatedFileUtilDelegate {
  33. public:
  34. ObfuscatedFileUtilMemoryDelegate(const base::FilePath& file_system_directory);
  35. ObfuscatedFileUtilMemoryDelegate(const ObfuscatedFileUtilMemoryDelegate&) =
  36. delete;
  37. ObfuscatedFileUtilMemoryDelegate& operator=(
  38. const ObfuscatedFileUtilMemoryDelegate&) = delete;
  39. ~ObfuscatedFileUtilMemoryDelegate() override;
  40. bool DirectoryExists(const base::FilePath& path) override;
  41. bool DeleteFileOrDirectory(const base::FilePath& path,
  42. bool recursive) override;
  43. bool IsLink(const base::FilePath& file_path) override;
  44. bool PathExists(const base::FilePath& path) override;
  45. NativeFileUtil::CopyOrMoveMode CopyOrMoveModeForDestination(
  46. const FileSystemURL& dest_url,
  47. bool copy) override;
  48. base::File CreateOrOpen(const base::FilePath& path,
  49. uint32_t file_flags) override;
  50. base::File::Error EnsureFileExists(const base::FilePath& path,
  51. bool* created) override;
  52. base::File::Error CreateDirectory(const base::FilePath& path,
  53. bool exclusive,
  54. bool recursive) override;
  55. base::File::Error GetFileInfo(const base::FilePath& path,
  56. base::File::Info* file_info) override;
  57. base::File::Error Touch(const base::FilePath& path,
  58. const base::Time& last_access_time,
  59. const base::Time& last_modified_time) override;
  60. base::File::Error Truncate(const base::FilePath& path,
  61. int64_t length) override;
  62. base::File::Error CopyOrMoveFile(
  63. const base::FilePath& src_path,
  64. const base::FilePath& dest_path,
  65. FileSystemOperation::CopyOrMoveOptionSet options,
  66. NativeFileUtil::CopyOrMoveMode mode) override;
  67. base::File::Error CopyInForeignFile(
  68. const base::FilePath& src_path,
  69. const base::FilePath& dest_path,
  70. FileSystemOperation::CopyOrMoveOptionSet options,
  71. NativeFileUtil::CopyOrMoveMode mode) override;
  72. base::File::Error DeleteFile(const base::FilePath& path) override;
  73. // Returns the total number of bytes used by all the files under |path|.
  74. // If the path does not exist or the total number of bytes doesn't fit in
  75. // |size_t|, the function returns 0.
  76. size_t ComputeDirectorySize(const base::FilePath& path) override;
  77. // Reads |buf_len| bytes from the file at |path|, starting from |offset|.
  78. // If successful, read bytes are written to |buf| and actual number of read
  79. // bytes are returned. Otherwise a net::Error value is returned.
  80. int ReadFile(const base::FilePath& path,
  81. int64_t offset,
  82. scoped_refptr<net::IOBuffer> buf,
  83. int buf_len);
  84. // Writes |buf_len| bytes to the file at |path|, starting from |offset|.
  85. // If successful, returns the actual number of written bytes, otherwise a
  86. // net::Error value is returned.
  87. int WriteFile(const base::FilePath& path,
  88. int64_t offset,
  89. scoped_refptr<net::IOBuffer> buf,
  90. int buf_len);
  91. base::File::Error CreateFileForTesting(const base::FilePath& path,
  92. base::span<const char> content);
  93. base::WeakPtr<ObfuscatedFileUtilMemoryDelegate> GetWeakPtr() {
  94. return weak_factory_.GetWeakPtr();
  95. }
  96. private:
  97. struct Entry;
  98. struct DecomposedPath;
  99. // Parses the given path into a decomposed path and performs validity checks
  100. // and normalization. Returns an empty value if checks fail.
  101. absl::optional<DecomposedPath> ParsePath(const base::FilePath& path);
  102. // Creates or opens a file specified in |dp|.
  103. void CreateOrOpenInternal(const DecomposedPath& dp, uint32_t file_flags);
  104. // Moves a directory from |src_dp| to |dest_dp|.
  105. bool MoveDirectoryInternal(const DecomposedPath& src_dp,
  106. const DecomposedPath& dest_dp);
  107. // Copies or moves a file from |src_dp| to |dest_dp|.
  108. bool CopyOrMoveFileInternal(const DecomposedPath& src_dp,
  109. const DecomposedPath& dest_dp,
  110. bool move);
  111. SEQUENCE_CHECKER(sequence_checker_);
  112. // The root of the directory tree.
  113. std::unique_ptr<Entry> root_;
  114. // The components of root path, kept for faster processing.
  115. std::vector<base::FilePath::StringType> root_path_components_;
  116. base::WeakPtrFactory<ObfuscatedFileUtilMemoryDelegate> weak_factory_{this};
  117. };
  118. } // namespace storage
  119. #endif // STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_