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_FILE_SYSTEM_FILE_UTIL_H_ | |
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/file_path.h" | |
10 #include "base/file_util.h" | |
11 #include "base/file_util_proxy.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/platform_file.h" | |
14 #include "base/tracked_objects.h" | |
15 #include "webkit/fileapi/file_system_types.h" | |
16 | |
17 namespace base { | |
18 struct PlatformFileInfo; | |
19 class MessageLoopProxy; | |
20 class Time; | |
21 } | |
22 | |
23 namespace fileapi { | |
24 | |
25 using base::PlatformFile; | |
26 using base::PlatformFileError; | |
27 class FileSystemOperationContext; | |
28 | |
29 // A large part of this implementation is taken from base::FileUtilProxy. | |
30 // TODO(dmikurube, kinuko): Clean up base::FileUtilProxy to factor out common | |
31 // routines. It includes dropping FileAPI-specific routines from FileUtilProxy. | |
32 // | |
33 // The default implementations of the virtual methods below (*1) assume the | |
34 // given paths are native ones for the host platform. The subclasses that | |
35 // perform local path translation/obfuscation must override them. | |
36 // (*1) All virtual methods which receive FilePath as their arguments: | |
37 // CreateOrOpen, EnsureFileExists, GetLocalFilePath, GetFileInfo, | |
38 // ReadDirectory, CreateDirectory, CopyOrMoveFile, DeleteFile, | |
39 // DeleteSingleDirectory, Touch, Truncate, PathExists, DirectoryExists, | |
40 // IsDirectoryEmpty and CreateFileEnumerator. | |
41 // | |
42 // The methods below (*2) assume the given paths may not be native ones for the | |
43 // host platform. The subclasses should not override them. They provide basic | |
44 // meta logic by using other virtual methods. | |
45 // (*2) All non-virtual methods: Copy, Move, Delete, DeleteDirectoryRecursive, | |
46 // PerformCommonCheckAndPreparationForMoveAndCopy and CopyOrMoveDirectory. | |
47 class FileSystemFileUtil { | |
48 public: | |
49 FileSystemFileUtil() {} | |
50 virtual ~FileSystemFileUtil() {} | |
51 | |
52 // Creates or opens a file with the given flags. It is invalid to pass NULL | |
53 // for the callback. | |
54 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create | |
55 // a new file at the given |file_path| and calls back with | |
56 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | |
57 virtual PlatformFileError CreateOrOpen( | |
58 FileSystemOperationContext* context, | |
59 const FilePath& file_path, | |
60 int file_flags, | |
61 PlatformFile* file_handle, | |
62 bool* created); | |
63 | |
64 // Close the given file handle. | |
65 virtual PlatformFileError Close( | |
66 FileSystemOperationContext* context, | |
67 PlatformFile); | |
68 | |
69 // Ensures that the given |file_path| exist. This creates a empty new file | |
70 // at |file_path| if the |file_path| does not exist. | |
71 // If a new file han not existed and is created at the |file_path|, | |
72 // |created| of the callback argument is set true and |error code| | |
73 // is set PLATFORM_FILE_OK. | |
74 // If the file already exists, |created| is set false and |error code| | |
75 // is set PLATFORM_FILE_OK. | |
76 // If the file hasn't existed but it couldn't be created for some other | |
77 // reasons, |created| is set false and |error code| indicates the error. | |
78 virtual PlatformFileError EnsureFileExists( | |
79 FileSystemOperationContext* context, | |
80 const FilePath& file_path, bool* created); | |
81 | |
82 // Maps |virtual_path| given |context| into |local_path| which represents | |
83 // physical file location on the host OS. This may not always make sense for | |
84 // all subclasses. | |
85 virtual PlatformFileError GetLocalFilePath( | |
86 FileSystemOperationContext* context, | |
87 const FilePath& virtual_path, | |
88 FilePath* local_path); | |
89 | |
90 // Retrieves the information about a file. It is invalid to pass NULL for the | |
91 // callback. | |
92 virtual PlatformFileError GetFileInfo( | |
93 FileSystemOperationContext* context, | |
94 const FilePath& file_, | |
95 base::PlatformFileInfo* file_info, | |
96 FilePath* platform_path); | |
97 | |
98 // Reads the filenames in |file_path|. | |
99 virtual PlatformFileError ReadDirectory( | |
100 FileSystemOperationContext* context, | |
101 const FilePath& file_path, | |
102 std::vector<base::FileUtilProxy::Entry>* entries); | |
103 | |
104 // Creates directory at given path. It's an error to create | |
105 // if |exclusive| is true and dir already exists. | |
106 virtual PlatformFileError CreateDirectory( | |
107 FileSystemOperationContext* context, | |
108 const FilePath& file_path, | |
109 bool exclusive, | |
110 bool recursive); | |
111 | |
112 // Copies or moves a single file. | |
113 virtual PlatformFileError CopyOrMoveFile( | |
114 FileSystemOperationContext* context, | |
115 const FilePath& src_file_path, | |
116 const FilePath& dest_file_path, | |
117 bool copy); | |
118 | |
119 // Copies in a single file from a different filesystem. The src_file_path is | |
120 // a true local platform path, regardless of which subclass of | |
121 // FileSystemFileUtil is being invoked. | |
122 virtual PlatformFileError CopyInForeignFile( | |
123 FileSystemOperationContext* context, | |
124 const FilePath& src_file_path, | |
125 const FilePath& dest_file_path); | |
126 | |
127 // Copies a file or a directory from |src_file_path| to |dest_file_path|. | |
128 // | |
129 // Error cases: | |
130 // If destination's parent doesn't exist. | |
131 // If source dir exists but destination path is an existing file. | |
132 // If source file exists but destination path is an existing directory. | |
133 // If source is a parent of destination. | |
134 // If source doesn't exist. | |
135 // | |
136 // This method calls one of the following methods depending on whether the | |
137 // target is a directory or not. | |
138 // - (virtual) CopyOrMoveFile or | |
139 // - (non-virtual) CopyOrMoveDirectory. | |
140 PlatformFileError Copy( | |
141 FileSystemOperationContext* context, | |
142 const FilePath& src_file_path, | |
143 const FilePath& dest_file_path); | |
144 | |
145 // Moves a file or a directory from src_file_path to dest_file_path. | |
146 // | |
147 // Error cases are similar to Copy method's error cases. | |
148 // | |
149 // This method calls one of the following methods depending on whether the | |
150 // target is a directory or not. | |
151 // - (virtual) CopyOrMoveFile or | |
152 // - (non-virtual) CopyOrMoveDirectory. | |
153 PlatformFileError Move( | |
154 FileSystemOperationContext* context, | |
155 const FilePath& src_file_path, | |
156 const FilePath& dest_file_path); | |
157 | |
158 // Deletes a file or a directory. | |
159 // It is an error to delete a non-empty directory with recursive=false. | |
160 // | |
161 // This method calls one of the following methods depending on whether the | |
162 // target is a directory or not, and whether the |recursive| flag is given or | |
163 // not. | |
164 // - (virtual) DeleteFile, | |
165 // - (virtual) DeleteSingleDirectory or | |
166 // - (non-virtual) DeleteDirectoryRecursive which calls two methods above. | |
167 PlatformFileError Delete( | |
168 FileSystemOperationContext* context, | |
169 const FilePath& file_path, | |
170 bool recursive); | |
171 | |
172 // Deletes a single file. | |
173 // It assumes the given path points a file. | |
174 // | |
175 // This method is called from DeleteDirectoryRecursive and Delete (both are | |
176 // non-virtual). | |
177 virtual PlatformFileError DeleteFile( | |
178 FileSystemOperationContext* unused, | |
179 const FilePath& file_path); | |
180 | |
181 // Deletes a single empty directory. | |
182 // It assumes the given path points an empty directory. | |
183 // | |
184 // This method is called from DeleteDirectoryRecursive and Delete (both are | |
185 // non-virtual). | |
186 virtual PlatformFileError DeleteSingleDirectory( | |
187 FileSystemOperationContext* unused, | |
188 const FilePath& file_path); | |
189 | |
190 // Touches a file. The callback can be NULL. | |
191 virtual PlatformFileError Touch( | |
192 FileSystemOperationContext* context, | |
193 const FilePath& file_path, | |
194 const base::Time& last_access_time, | |
195 const base::Time& last_modified_time); | |
196 | |
197 // Truncates a file to the given length. If |length| is greater than the | |
198 // current length of the file, the file will be extended with zeroes. | |
199 // The callback can be NULL. | |
200 virtual PlatformFileError Truncate( | |
201 FileSystemOperationContext* context, | |
202 const FilePath& path, | |
203 int64 length); | |
204 | |
205 virtual bool PathExists( | |
206 FileSystemOperationContext* unused, | |
207 const FilePath& file_path); | |
208 | |
209 virtual bool DirectoryExists( | |
210 FileSystemOperationContext* unused, | |
211 const FilePath& file_path); | |
212 | |
213 virtual bool IsDirectoryEmpty( | |
214 FileSystemOperationContext* unused, | |
215 const FilePath& file_path); | |
216 | |
217 // It will be implemented by each subclass such as FileSystemFileEnumerator. | |
218 class AbstractFileEnumerator { | |
219 public: | |
220 virtual ~AbstractFileEnumerator() {} | |
221 | |
222 // Returns an empty string if there are no more results. | |
223 virtual FilePath Next() = 0; | |
224 | |
225 virtual bool IsDirectory() = 0; | |
226 }; | |
227 | |
228 class EmptyFileEnumerator : public AbstractFileEnumerator { | |
229 virtual FilePath Next() { return FilePath(); } | |
230 virtual bool IsDirectory() { return false; } | |
231 }; | |
232 | |
233 // Returns a pointer to a new instance of AbstractFileEnumerator which is | |
234 // implemented for each FileUtil subclass. The instance needs to be freed | |
235 // by the caller, and its lifetime should not extend past when the current | |
236 // call returns to the main FILE message loop. | |
237 virtual AbstractFileEnumerator* CreateFileEnumerator( | |
238 FileSystemOperationContext* unused, | |
239 const FilePath& root_path); | |
240 | |
241 protected: | |
242 // Deletes a directory and all entries under the directory. | |
243 // | |
244 // This method is called from Delete. It internally calls two following | |
245 // virtual methods, | |
246 // - (virtual) DeleteFile to delete files, and | |
247 // - (virtual) DeleteSingleDirectory to delete empty directories after all | |
248 // the files are deleted. | |
249 PlatformFileError DeleteDirectoryRecursive( | |
250 FileSystemOperationContext* context, | |
251 const FilePath& file_path); | |
252 | |
253 // This also removes the destination directory if it's non-empty and all | |
254 // other checks are passed (so that the copy/move correctly overwrites the | |
255 // destination). | |
256 PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( | |
257 FileSystemOperationContext* unused, | |
258 const FilePath& src_file_path, | |
259 const FilePath& dest_file_path); | |
260 | |
261 // Performs recursive copy or move by calling CopyOrMoveFile for individual | |
262 // files. Operations for recursive traversal are encapsulated in this method. | |
263 // It assumes src_file_path and dest_file_path have passed | |
264 // PerformCommonCheckAndPreparationForMoveAndCopy(). | |
265 PlatformFileError CopyOrMoveDirectory( | |
266 FileSystemOperationContext* context, | |
267 const FilePath& src_file_path, | |
268 const FilePath& dest_file_path, | |
269 bool copy); | |
270 | |
271 // Determines whether a simple same-filesystem move or copy can be done. If | |
272 // so, it delegates to CopyOrMoveFile. Otherwise it looks up the true | |
273 // platform path of the source file, delegates to CopyInForeignFile, and [for | |
274 // move] calls DeleteFile on the source file. | |
275 PlatformFileError CopyOrMoveFileHelper( | |
276 FileSystemOperationContext* context, | |
277 const FilePath& src_file_path, | |
278 const FilePath& dest_file_path, | |
279 bool copy); | |
280 | |
281 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil); | |
282 }; | |
283 | |
284 } // namespace fileapi | |
285 | |
286 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ | |
OLD | NEW |