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