Chromium Code Reviews| 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 "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 FileUtil { |
|
ericu
2011/08/03 22:59:26
It's a bit odd to call the file fileapi_file_util.
Dai Mikurube (NOT FULLTIME)
2011/08/04 03:54:48
Agreed. Renaming it to FileApiFileUtil.
| |
| 48 public: | 41 public: |
| 49 FileSystemFileUtil() {} | 42 FileUtil() {} |
| 50 virtual ~FileSystemFileUtil() {} | 43 virtual ~FileUtil() {} |
| 51 | 44 |
| 52 // Creates or opens a file with the given flags. It is invalid to pass NULL | 45 // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 53 // for the callback. | 46 // for the callback. |
| 54 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create | 47 // 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 | 48 // a new file at the given |file_path| and calls back with |
| 56 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | 49 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 57 virtual PlatformFileError CreateOrOpen( | 50 virtual PlatformFileError CreateOrOpen( |
| 58 FileSystemOperationContext* context, | 51 FileSystemOperationContext* context, |
| 59 const FilePath& file_path, | 52 const FilePath& file_path, |
| 60 int file_flags, | 53 int file_flags, |
| 61 PlatformFile* file_handle, | 54 PlatformFile* file_handle, |
| 62 bool* created); | 55 bool* created) = 0; |
| 63 | 56 |
| 64 // Close the given file handle. | 57 // Close the given file handle. |
| 65 virtual PlatformFileError Close( | 58 virtual PlatformFileError Close( |
| 66 FileSystemOperationContext* context, | 59 FileSystemOperationContext* context, |
| 67 PlatformFile); | 60 PlatformFile) = 0; |
| 68 | 61 |
| 69 // Ensures that the given |file_path| exist. This creates a empty new file | 62 // Ensures that the given |file_path| exist. This creates a empty new file |
| 70 // at |file_path| if the |file_path| does not exist. | 63 // 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|, | 64 // 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| | 65 // |created| of the callback argument is set true and |error code| |
| 73 // is set PLATFORM_FILE_OK. | 66 // is set PLATFORM_FILE_OK. |
| 74 // If the file already exists, |created| is set false and |error code| | 67 // If the file already exists, |created| is set false and |error code| |
| 75 // is set PLATFORM_FILE_OK. | 68 // is set PLATFORM_FILE_OK. |
| 76 // If the file hasn't existed but it couldn't be created for some other | 69 // 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. | 70 // reasons, |created| is set false and |error code| indicates the error. |
| 78 virtual PlatformFileError EnsureFileExists( | 71 virtual PlatformFileError EnsureFileExists( |
| 79 FileSystemOperationContext* context, | 72 FileSystemOperationContext* context, |
| 80 const FilePath& file_path, bool* created); | 73 const FilePath& file_path, bool* created) = 0; |
| 81 | 74 |
| 82 // Maps |virtual_path| given |context| into |local_path| which represents | 75 // 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 | 76 // physical file location on the host OS. This may not always make sense for |
| 84 // all subclasses. | 77 // all subclasses. |
| 85 virtual PlatformFileError GetLocalFilePath( | 78 virtual PlatformFileError GetLocalFilePath( |
| 86 FileSystemOperationContext* context, | 79 FileSystemOperationContext* context, |
| 87 const FilePath& virtual_path, | 80 const FilePath& virtual_path, |
| 88 FilePath* local_path); | 81 FilePath* local_path) = 0; |
| 89 | 82 |
| 90 // Retrieves the information about a file. It is invalid to pass NULL for the | 83 // Retrieves the information about a file. It is invalid to pass NULL for the |
| 91 // callback. | 84 // callback. |
| 92 virtual PlatformFileError GetFileInfo( | 85 virtual PlatformFileError GetFileInfo( |
| 93 FileSystemOperationContext* context, | 86 FileSystemOperationContext* context, |
| 94 const FilePath& file_, | 87 const FilePath& file_, |
| 95 base::PlatformFileInfo* file_info, | 88 base::PlatformFileInfo* file_info, |
| 96 FilePath* platform_path); | 89 FilePath* platform_path) = 0; |
| 97 | 90 |
| 98 // Reads the filenames in |file_path|. | 91 // Reads the filenames in |file_path|. |
| 99 virtual PlatformFileError ReadDirectory( | 92 virtual PlatformFileError ReadDirectory( |
| 100 FileSystemOperationContext* context, | 93 FileSystemOperationContext* context, |
| 101 const FilePath& file_path, | 94 const FilePath& file_path, |
| 102 std::vector<base::FileUtilProxy::Entry>* entries); | 95 std::vector<base::FileUtilProxy::Entry>* entries) = 0; |
| 103 | 96 |
| 104 // Creates directory at given path. It's an error to create | 97 // Creates directory at given path. It's an error to create |
| 105 // if |exclusive| is true and dir already exists. | 98 // if |exclusive| is true and dir already exists. |
| 106 virtual PlatformFileError CreateDirectory( | 99 virtual PlatformFileError CreateDirectory( |
| 107 FileSystemOperationContext* context, | 100 FileSystemOperationContext* context, |
| 108 const FilePath& file_path, | 101 const FilePath& file_path, |
| 109 bool exclusive, | 102 bool exclusive, |
| 110 bool recursive); | 103 bool recursive) = 0; |
| 111 | 104 |
| 112 // Copies or moves a single file. | 105 // Copies or moves a single file. |
| 113 virtual PlatformFileError CopyOrMoveFile( | 106 virtual PlatformFileError CopyOrMoveFile( |
| 114 FileSystemOperationContext* context, | 107 FileSystemOperationContext* context, |
| 115 const FilePath& src_file_path, | 108 const FilePath& src_file_path, |
| 116 const FilePath& dest_file_path, | 109 const FilePath& dest_file_path, |
| 117 bool copy); | 110 bool copy) = 0; |
| 118 | 111 |
| 119 // Copies in a single file from a different filesystem. The src_file_path is | 112 // 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 | 113 // a true local platform path, regardless of which subclass of FileUtil is |
| 121 // FileSystemFileUtil is being invoked. | 114 // being invoked. |
| 122 virtual PlatformFileError CopyInForeignFile( | 115 virtual PlatformFileError CopyInForeignFile( |
| 123 FileSystemOperationContext* context, | 116 FileSystemOperationContext* context, |
| 124 const FilePath& src_file_path, | 117 const FilePath& src_file_path, |
| 125 const FilePath& dest_file_path); | 118 const FilePath& dest_file_path) = 0; |
| 126 | 119 |
| 127 // Copies a file or a directory from |src_file_path| to |dest_file_path|. | 120 // Copies a file or a directory from |src_file_path| to |dest_file_path|. |
| 128 // | 121 // |
| 129 // Error cases: | 122 // Error cases: |
| 130 // If destination's parent doesn't exist. | 123 // If destination's parent doesn't exist. |
| 131 // If source dir exists but destination path is an existing file. | 124 // If source dir exists but destination path is an existing file. |
| 132 // If source file exists but destination path is an existing directory. | 125 // If source file exists but destination path is an existing directory. |
| 133 // If source is a parent of destination. | 126 // If source is a parent of destination. |
| 134 // If source doesn't exist. | 127 // If source doesn't exist. |
| 135 // | 128 // |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 const FilePath& file_path, | 162 const FilePath& file_path, |
| 170 bool recursive); | 163 bool recursive); |
| 171 | 164 |
| 172 // Deletes a single file. | 165 // Deletes a single file. |
| 173 // It assumes the given path points a file. | 166 // It assumes the given path points a file. |
| 174 // | 167 // |
| 175 // This method is called from DeleteDirectoryRecursive and Delete (both are | 168 // This method is called from DeleteDirectoryRecursive and Delete (both are |
| 176 // non-virtual). | 169 // non-virtual). |
| 177 virtual PlatformFileError DeleteFile( | 170 virtual PlatformFileError DeleteFile( |
| 178 FileSystemOperationContext* unused, | 171 FileSystemOperationContext* unused, |
| 179 const FilePath& file_path); | 172 const FilePath& file_path) = 0; |
| 180 | 173 |
| 181 // Deletes a single empty directory. | 174 // Deletes a single empty directory. |
| 182 // It assumes the given path points an empty directory. | 175 // It assumes the given path points an empty directory. |
| 183 // | 176 // |
| 184 // This method is called from DeleteDirectoryRecursive and Delete (both are | 177 // This method is called from DeleteDirectoryRecursive and Delete (both are |
| 185 // non-virtual). | 178 // non-virtual). |
| 186 virtual PlatformFileError DeleteSingleDirectory( | 179 virtual PlatformFileError DeleteSingleDirectory( |
| 187 FileSystemOperationContext* unused, | 180 FileSystemOperationContext* unused, |
| 188 const FilePath& file_path); | 181 const FilePath& file_path) = 0; |
| 189 | 182 |
| 190 // Touches a file. The callback can be NULL. | 183 // Touches a file. The callback can be NULL. |
| 191 virtual PlatformFileError Touch( | 184 virtual PlatformFileError Touch( |
| 192 FileSystemOperationContext* context, | 185 FileSystemOperationContext* context, |
| 193 const FilePath& file_path, | 186 const FilePath& file_path, |
| 194 const base::Time& last_access_time, | 187 const base::Time& last_access_time, |
| 195 const base::Time& last_modified_time); | 188 const base::Time& last_modified_time) = 0; |
| 196 | 189 |
| 197 // Truncates a file to the given length. If |length| is greater than the | 190 // 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. | 191 // current length of the file, the file will be extended with zeroes. |
| 199 // The callback can be NULL. | 192 // The callback can be NULL. |
| 200 virtual PlatformFileError Truncate( | 193 virtual PlatformFileError Truncate( |
| 201 FileSystemOperationContext* context, | 194 FileSystemOperationContext* context, |
| 202 const FilePath& path, | 195 const FilePath& path, |
| 203 int64 length); | 196 int64 length) = 0; |
| 204 | 197 |
| 205 virtual bool PathExists( | 198 virtual bool PathExists( |
| 206 FileSystemOperationContext* unused, | 199 FileSystemOperationContext* unused, |
| 207 const FilePath& file_path); | 200 const FilePath& file_path) = 0; |
| 208 | 201 |
| 209 virtual bool DirectoryExists( | 202 virtual bool DirectoryExists( |
| 210 FileSystemOperationContext* unused, | 203 FileSystemOperationContext* unused, |
| 211 const FilePath& file_path); | 204 const FilePath& file_path) = 0; |
| 212 | 205 |
| 213 virtual bool IsDirectoryEmpty( | 206 virtual bool IsDirectoryEmpty( |
| 214 FileSystemOperationContext* unused, | 207 FileSystemOperationContext* unused, |
| 215 const FilePath& file_path); | 208 const FilePath& file_path) = 0; |
| 216 | 209 |
| 217 // It will be implemented by each subclass such as FileSystemFileEnumerator. | 210 // It will be implemented by each subclass such as FileSystemFileEnumerator. |
| 218 class AbstractFileEnumerator { | 211 class AbstractFileEnumerator { |
| 219 public: | 212 public: |
| 220 virtual ~AbstractFileEnumerator() {} | 213 virtual ~AbstractFileEnumerator() {} |
| 221 | 214 |
| 222 // Returns an empty string if there are no more results. | 215 // Returns an empty string if there are no more results. |
| 223 virtual FilePath Next() = 0; | 216 virtual FilePath Next() = 0; |
| 224 | 217 |
| 225 virtual bool IsDirectory() = 0; | 218 virtual bool IsDirectory() = 0; |
| 226 }; | 219 }; |
| 227 | 220 |
| 228 class EmptyFileEnumerator : public AbstractFileEnumerator { | 221 class EmptyFileEnumerator : public AbstractFileEnumerator { |
| 229 virtual FilePath Next() { return FilePath(); } | 222 virtual FilePath Next() { return FilePath(); } |
| 230 virtual bool IsDirectory() { return false; } | 223 virtual bool IsDirectory() { return false; } |
| 231 }; | 224 }; |
| 232 | 225 |
| 233 // Returns a pointer to a new instance of AbstractFileEnumerator which is | 226 // Returns a pointer to a new instance of AbstractFileEnumerator which is |
| 234 // implemented for each FileUtil subclass. The instance needs to be freed | 227 // 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 | 228 // by the caller, and its lifetime should not extend past when the current |
| 236 // call returns to the main FILE message loop. | 229 // call returns to the main FILE message loop. |
| 237 virtual AbstractFileEnumerator* CreateFileEnumerator( | 230 virtual AbstractFileEnumerator* CreateFileEnumerator( |
| 238 FileSystemOperationContext* unused, | 231 FileSystemOperationContext* unused, |
| 239 const FilePath& root_path); | 232 const FilePath& root_path) = 0; |
| 240 | 233 |
| 241 protected: | 234 protected: |
| 242 // Deletes a directory and all entries under the directory. | 235 // Deletes a directory and all entries under the directory. |
| 243 // | 236 // |
| 244 // This method is called from Delete. It internally calls two following | 237 // This method is called from Delete. It internally calls two following |
| 245 // virtual methods, | 238 // virtual methods, |
| 246 // - (virtual) DeleteFile to delete files, and | 239 // - (virtual) DeleteFile to delete files, and |
| 247 // - (virtual) DeleteSingleDirectory to delete empty directories after all | 240 // - (virtual) DeleteSingleDirectory to delete empty directories after all |
| 248 // the files are deleted. | 241 // the files are deleted. |
| 249 PlatformFileError DeleteDirectoryRecursive( | 242 PlatformFileError DeleteDirectoryRecursive( |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 271 // Determines whether a simple same-filesystem move or copy can be done. If | 264 // 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 | 265 // so, it delegates to CopyOrMoveFile. Otherwise it looks up the true |
| 273 // platform path of the source file, delegates to CopyInForeignFile, and [for | 266 // platform path of the source file, delegates to CopyInForeignFile, and [for |
| 274 // move] calls DeleteFile on the source file. | 267 // move] calls DeleteFile on the source file. |
| 275 PlatformFileError CopyOrMoveFileHelper( | 268 PlatformFileError CopyOrMoveFileHelper( |
| 276 FileSystemOperationContext* context, | 269 FileSystemOperationContext* context, |
| 277 const FilePath& src_file_path, | 270 const FilePath& src_file_path, |
| 278 const FilePath& dest_file_path, | 271 const FilePath& dest_file_path, |
| 279 bool copy); | 272 bool copy); |
| 280 | 273 |
| 281 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil); | 274 DISALLOW_COPY_AND_ASSIGN(FileUtil); |
| 282 }; | 275 }; |
| 283 | 276 |
| 284 } // namespace fileapi | 277 } // namespace fileapi |
| 285 | 278 |
| 286 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ | 279 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ |
| OLD | NEW |