OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 | |
5 #ifndef WEBKIT_FILEAPI_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | |
6 #define WEBKIT_FILEAPI_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | |
7 | |
8 #include <map> | |
9 #include <vector> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/file_util_proxy.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/platform_file.h" | |
15 #include "base/timer.h" | |
16 #include "webkit/fileapi/file_system_directory_database.h" | |
17 #include "webkit/fileapi/file_system_file_util.h" | |
18 #include "webkit/fileapi/file_system_origin_database.h" | |
19 #include "webkit/fileapi/file_system_types.h" | |
20 | |
21 namespace base { | |
22 struct PlatformFileInfo; | |
23 class Time; | |
24 } | |
25 | |
26 class GURL; | |
27 | |
28 namespace fileapi { | |
29 | |
30 class FileSystemOperationContext; | |
31 | |
32 // The overall implementation philosophy of this class is that partial failures | |
33 // should leave us with an intact database; we'd prefer to leak the occasional | |
34 // backing file than have a database entry whose backing file is missing. When | |
35 // doing FSCK operations, if you find a loose backing file with no reference, | |
36 // you may safely delete it. | |
37 // | |
38 // This class is RefCountedThreadSafe because it may gain a reference on the IO | |
39 // thread, but must be deleted on the FILE thread because that's where | |
40 // DropDatabases needs to be called. References will be held by the | |
41 // SandboxMountPointProvider [and the task it uses to drop the reference] and | |
42 // SandboxMountPointProvider::GetFileSystemRootPathTask. Without that last one, | |
43 // we wouldn't need ref counting. | |
44 // | |
45 // TODO(ericu): We don't ever update directory mtimes; which operations should | |
46 // do that? | |
47 class ObfuscatedFileSystemFileUtil : public FileSystemFileUtil, | |
48 public base::RefCountedThreadSafe<ObfuscatedFileSystemFileUtil> { | |
49 public: | |
50 // |underlying_file_util| is owned by the instance. It will be deleted by | |
51 // the owner instance. For example, it can be instanciated as follows: | |
52 // FileSystemFileUtil* file_system_file_util = | |
53 // new ObfuscatedFileSystemFileUtil(new FileSystemFileUtil()); | |
54 ObfuscatedFileSystemFileUtil( | |
55 const FilePath& file_system_directory, | |
56 FileSystemFileUtil* underlying_file_util); | |
57 virtual ~ObfuscatedFileSystemFileUtil(); | |
58 | |
59 virtual base::PlatformFileError CreateOrOpen( | |
60 FileSystemOperationContext* context, | |
61 const FilePath& file_path, | |
62 int file_flags, | |
63 base::PlatformFile* file_handle, | |
64 bool* created) OVERRIDE; | |
65 | |
66 virtual base::PlatformFileError EnsureFileExists( | |
67 FileSystemOperationContext* context, | |
68 const FilePath& file_path, bool* created) OVERRIDE; | |
69 | |
70 virtual base::PlatformFileError GetLocalFilePath( | |
71 FileSystemOperationContext* context, | |
72 const FilePath& virtual_file, | |
73 FilePath* local_path) OVERRIDE; | |
74 | |
75 virtual base::PlatformFileError GetFileInfo( | |
76 FileSystemOperationContext* context, | |
77 const FilePath& file, | |
78 base::PlatformFileInfo* file_info, | |
79 FilePath* platform_file) OVERRIDE; | |
80 | |
81 virtual base::PlatformFileError ReadDirectory( | |
82 FileSystemOperationContext* context, | |
83 const FilePath& file_path, | |
84 std::vector<base::FileUtilProxy::Entry>* entries) OVERRIDE; | |
85 | |
86 virtual base::PlatformFileError CreateDirectory( | |
87 FileSystemOperationContext* context, | |
88 const FilePath& file_path, | |
89 bool exclusive, | |
90 bool recursive) OVERRIDE; | |
91 | |
92 virtual base::PlatformFileError CopyOrMoveFile( | |
93 FileSystemOperationContext* context, | |
94 const FilePath& src_file_path, | |
95 const FilePath& dest_file_path, | |
96 bool copy) OVERRIDE; | |
97 | |
98 virtual PlatformFileError CopyInForeignFile( | |
99 FileSystemOperationContext* context, | |
100 const FilePath& src_file_path, | |
101 const FilePath& dest_file_path) OVERRIDE; | |
102 | |
103 virtual base::PlatformFileError DeleteFile( | |
104 FileSystemOperationContext* context, | |
105 const FilePath& file_path) OVERRIDE; | |
106 | |
107 virtual base::PlatformFileError DeleteSingleDirectory( | |
108 FileSystemOperationContext* context, | |
109 const FilePath& file_path) OVERRIDE; | |
110 | |
111 virtual base::PlatformFileError Touch( | |
112 FileSystemOperationContext* context, | |
113 const FilePath& file_path, | |
114 const base::Time& last_access_time, | |
115 const base::Time& last_modified_time) OVERRIDE; | |
116 | |
117 virtual base::PlatformFileError Truncate( | |
118 FileSystemOperationContext* context, | |
119 const FilePath& path, | |
120 int64 length) OVERRIDE; | |
121 | |
122 virtual bool PathExists( | |
123 FileSystemOperationContext* context, | |
124 const FilePath& file_path) OVERRIDE; | |
125 | |
126 virtual bool DirectoryExists( | |
127 FileSystemOperationContext* context, | |
128 const FilePath& file_path) OVERRIDE; | |
129 | |
130 virtual bool IsDirectoryEmpty( | |
131 FileSystemOperationContext* context, | |
132 const FilePath& file_path) OVERRIDE; | |
133 | |
134 // Gets the topmost directory specific to this origin and type. This will | |
135 // contain both the directory database's files and all the backing file | |
136 // subdirectories. | |
137 FilePath GetDirectoryForOriginAndType( | |
138 const GURL& origin, FileSystemType type, bool create); | |
139 | |
140 // Deletes the topmost directory specific to this origin and type. This will | |
141 // delete its directory database. | |
142 bool DeleteDirectoryForOriginAndType(const GURL& origin, FileSystemType type); | |
143 | |
144 // This will migrate a filesystem from the old passthrough sandbox into the | |
145 // new obfuscated one. It won't obfuscate the old filenames [it will maintain | |
146 // the old structure, but move it to a new root], but any new files created | |
147 // will go into the new standard locations. This will be completely | |
148 // transparent to the user. This migration is atomic in that it won't alter | |
149 // the source data until it's done, and that will be with a single directory | |
150 // move [the directory with the unguessable name will move into the new | |
151 // filesystem storage directory]. However, if this fails partway through, it | |
152 // might leave a seemingly-valid database for this origin. When it starts up, | |
153 // it will clear any such database, just in case. | |
154 bool MigrateFromOldSandbox( | |
155 const GURL& origin, FileSystemType type, const FilePath& root); | |
156 | |
157 // TODO(ericu): This doesn't really feel like it belongs in this class. | |
158 // The previous version lives in FileSystemPathManager, but perhaps | |
159 // SandboxMountPointProvider would be better? | |
160 static FilePath::StringType GetDirectoryNameForType(FileSystemType type); | |
161 | |
162 // Origin enumerator interface. | |
163 // An instance of this interface is assumed to be called on the file thread. | |
164 class AbstractOriginEnumerator { | |
165 public: | |
166 virtual ~AbstractOriginEnumerator() {} | |
167 | |
168 // Returns the next origin. Returns empty if there are no more origins. | |
169 virtual GURL Next() = 0; | |
170 | |
171 // Returns the current origin's information. | |
172 virtual bool HasFileSystemType(FileSystemType type) const = 0; | |
173 }; | |
174 | |
175 // This method and all methods of its returned class must be called only on | |
176 // the FILE thread. The caller is responsible for deleting the returned | |
177 // object. | |
178 AbstractOriginEnumerator* CreateOriginEnumerator(); | |
179 | |
180 virtual AbstractFileEnumerator* CreateFileEnumerator( | |
181 FileSystemOperationContext* context, | |
182 const FilePath& root_path) OVERRIDE; | |
183 | |
184 // Deletes a directory database from the database list in the ObfuscatedFSFU | |
185 // and destroys the database on the disk. | |
186 bool DestroyDirectoryDatabase(const GURL& origin, FileSystemType type); | |
187 | |
188 // Computes a cost for storing a given file in the obfuscated FSFU. | |
189 // As the cost of a file is independent of the cost of its parent directories, | |
190 // this ignores all but the BaseName of the supplied path. In order to | |
191 // compute the cost of adding a multi-segment directory recursively, call this | |
192 // on each path segment and add the results. | |
193 static int64 ComputeFilePathCost(const FilePath& path); | |
194 | |
195 private: | |
196 typedef FileSystemDirectoryDatabase::FileId FileId; | |
197 typedef FileSystemDirectoryDatabase::FileInfo FileInfo; | |
198 | |
199 base::PlatformFileError GetFileInfoInternal( | |
200 FileSystemDirectoryDatabase* db, | |
201 FileSystemOperationContext* context, | |
202 FileId file_id, | |
203 FileInfo* local_info, | |
204 base::PlatformFileInfo* file_info, | |
205 FilePath* platform_file_path); | |
206 | |
207 // Creates a new file, both the underlying backing file and the entry in the | |
208 // database. file_info is an in-out parameter. Supply the name and | |
209 // parent_id; data_path is ignored. On success, data_path will | |
210 // always be set to the relative path [from the root of the type-specific | |
211 // filesystem directory] of a NEW backing file, and handle, if supplied, will | |
212 // hold open PlatformFile for the backing file, which the caller is | |
213 // responsible for closing. If you supply a path in source_path, it will be | |
214 // used as a source from which to COPY data. | |
215 // Caveat: do not supply handle if you're also supplying a data path. It was | |
216 // easier not to support this, and no code has needed it so far, so it will | |
217 // DCHECK and handle will hold base::kInvalidPlatformFileValue. | |
218 base::PlatformFileError CreateFile( | |
219 FileSystemOperationContext* context, | |
220 const GURL& origin_url, FileSystemType type, | |
221 const FilePath& source_path, FileInfo* file_info, | |
222 int file_flags, base::PlatformFile* handle); | |
223 // Given the filesystem's root URL and a virtual path, produces a real, full | |
224 // local path to the underlying data file. This does a database lookup, and | |
225 // verifies that the file exists. | |
226 FilePath GetLocalPath( | |
227 const GURL& origin_url, | |
228 FileSystemType type, | |
229 const FilePath& virtual_path); | |
230 // This converts from a relative path [as is stored in the FileInfo.data_path | |
231 // field] to an absolute local path that can be given to the operating system. | |
232 // It does no checks as to whether the file actually exists; it's pure path | |
233 // manipulation. | |
234 FilePath DataPathToLocalPath( | |
235 const GURL& origin, FileSystemType type, const FilePath& data_path); | |
236 // This does the reverse of DataPathToLocalPath. | |
237 FilePath LocalPathToDataPath( | |
238 const GURL& origin, FileSystemType type, const FilePath& local_path); | |
239 // This returns NULL if |create| flag is false and a filesystem does not | |
240 // exist for the given |origin_url| and |type|. | |
241 // For read operations |create| should be false. | |
242 FileSystemDirectoryDatabase* GetDirectoryDatabase( | |
243 const GURL& origin_url, FileSystemType type, bool create); | |
244 // Gets the topmost directory specific to this origin. This will | |
245 // contain both the filesystem type subdirectories. | |
246 FilePath GetDirectoryForOrigin(const GURL& origin, bool create); | |
247 void MarkUsed(); | |
248 void DropDatabases(); | |
249 bool InitOriginDatabase(bool create); | |
250 | |
251 typedef std::map<std::string, FileSystemDirectoryDatabase*> DirectoryMap; | |
252 DirectoryMap directories_; | |
253 scoped_ptr<FileSystemOriginDatabase> origin_database_; | |
254 FilePath file_system_directory_; | |
255 base::OneShotTimer<ObfuscatedFileSystemFileUtil> timer_; | |
256 scoped_ptr<FileSystemFileUtil> underlying_file_util_; | |
257 | |
258 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtil); | |
259 }; | |
260 | |
261 } // namespace fileapi | |
262 | |
263 #endif // WEBKIT_FILEAPI_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | |
OLD | NEW |