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