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