File: obfuscated_file_util_memory_delegate.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 6,249 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_
#define STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_

#include <map>
#include <memory>
#include <vector>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "storage/browser/file_system/native_file_util.h"
#include "storage/browser/file_system/obfuscated_file_util_delegate.h"

namespace storage {

// This delegate performs all ObfuscatedFileUtil tasks that require touching
// disk and peforms them in memory.

// The given paths should be subpaths of the |file_system_directory| folder
// passed to the constructor. No normalization is done on the this root folder
// and it is expected that all input paths would start with it.

// Directories and files are both stored in |Entry| structures, keeping the type
// of the entry, creation, modification, and last access time, and
// |file_content| or |directory_content| based on the type.

// Directory tree is kept in a tree structure starting from |root_|. All API
// functions that receive a |FilePath|, convert it to a |DecomposedPath| which
// has separated normalized components, is ensured that it is under the root,
// and has the respective |Entry| (if exists) and its parent in the directory
// tree starting from |root_|.

class COMPONENT_EXPORT(STORAGE_BROWSER) ObfuscatedFileUtilMemoryDelegate
    : public ObfuscatedFileUtilDelegate {
 public:
  ObfuscatedFileUtilMemoryDelegate(const base::FilePath& file_system_directory);

  ObfuscatedFileUtilMemoryDelegate(const ObfuscatedFileUtilMemoryDelegate&) =
      delete;
  ObfuscatedFileUtilMemoryDelegate& operator=(
      const ObfuscatedFileUtilMemoryDelegate&) = delete;

  ~ObfuscatedFileUtilMemoryDelegate() override;

  bool DirectoryExists(const base::FilePath& path) override;
  bool DeleteFileOrDirectory(const base::FilePath& path,
                             bool recursive) override;
  bool IsLink(const base::FilePath& file_path) override;
  bool PathExists(const base::FilePath& path) override;

  NativeFileUtil::CopyOrMoveMode CopyOrMoveModeForDestination(
      const FileSystemURL& dest_url,
      bool copy) override;
  base::File CreateOrOpen(const base::FilePath& path,
                          uint32_t file_flags) override;
  base::File::Error EnsureFileExists(const base::FilePath& path,
                                     bool* created) override;
  base::File::Error CreateDirectory(const base::FilePath& path,
                                    bool exclusive,
                                    bool recursive) override;
  base::File::Error GetFileInfo(const base::FilePath& path,
                                base::File::Info* file_info) override;
  base::File::Error Touch(const base::FilePath& path,
                          const base::Time& last_access_time,
                          const base::Time& last_modified_time) override;
  base::File::Error Truncate(const base::FilePath& path,
                             int64_t length) override;
  base::File::Error CopyOrMoveFile(
      const base::FilePath& src_path,
      const base::FilePath& dest_path,
      FileSystemOperation::CopyOrMoveOptionSet options,
      NativeFileUtil::CopyOrMoveMode mode) override;
  base::File::Error CopyInForeignFile(
      const base::FilePath& src_path,
      const base::FilePath& dest_path,
      FileSystemOperation::CopyOrMoveOptionSet options,
      NativeFileUtil::CopyOrMoveMode mode) override;
  base::File::Error DeleteFile(const base::FilePath& path) override;

  // Returns the total number of bytes used by all the files under |path|.
  // If the path does not exist or the total number of bytes doesn't fit in
  // |size_t|, the function returns 0.
  size_t ComputeDirectorySize(const base::FilePath& path) override;

  // Reads |buf_len| bytes from the file at |path|, starting from |offset|.
  // If successful, read bytes are written to |buf| and actual number of read
  // bytes are returned. Otherwise a net::Error value is returned.
  int ReadFile(const base::FilePath& path,
               int64_t offset,
               scoped_refptr<net::IOBuffer> buf,
               int buf_len);

  // Writes |buf_len| bytes to the file at |path|, starting from |offset|.
  // If successful, returns the actual number of written bytes, otherwise a
  // net::Error value is returned.
  int WriteFile(const base::FilePath& path,
                int64_t offset,
                scoped_refptr<net::IOBuffer> buf,
                int buf_len);

  base::File::Error CreateFileForTesting(const base::FilePath& path,
                                         base::span<const char> content);

  base::WeakPtr<ObfuscatedFileUtilMemoryDelegate> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

 private:
  struct Entry;
  struct DecomposedPath;

  // Parses the given path into a decomposed path and performs validity checks
  // and normalization. Returns an empty value if checks fail.
  std::optional<DecomposedPath> ParsePath(const base::FilePath& path);

  // Creates or opens a file specified in |dp|.
  void CreateOrOpenInternal(const DecomposedPath& dp, uint32_t file_flags);

  // Moves a directory from |src_dp| to |dest_dp|.
  bool MoveDirectoryInternal(const DecomposedPath& src_dp,
                             const DecomposedPath& dest_dp);

  // Copies or moves a file from |src_dp| to |dest_dp|.
  bool CopyOrMoveFileInternal(const DecomposedPath& src_dp,
                              const DecomposedPath& dest_dp,
                              bool move);

  SEQUENCE_CHECKER(sequence_checker_);

  // The root of the directory tree.
  std::unique_ptr<Entry> root_;

  // The components of root path, kept for faster processing.
  std::vector<base::FilePath::StringType> root_path_components_;

  base::WeakPtrFactory<ObfuscatedFileUtilMemoryDelegate> weak_factory_{this};
};

}  // namespace storage

#endif  // STORAGE_BROWSER_FILE_SYSTEM_OBFUSCATED_FILE_UTIL_MEMORY_DELEGATE_H_