| 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_ |
| 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); |
| 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 virtual PlatformFileError Touch( | 177 virtual PlatformFileError Touch( |
| 192 FileSystemOperationContext* context, | 178 FileSystemOperationContext* context, |
| 193 const FilePath& file_path, | 179 const FilePath& file_path, |
| 194 const base::Time& last_access_time, | 180 const base::Time& last_access_time, |
| 195 const base::Time& last_modified_time); | 181 const base::Time& last_modified_time); |
| 196 | 182 |
| 197 // Truncates a file to the given length. If |length| is greater than the | 183 // 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. | 184 // current length of the file, the file will be extended with zeroes. |
| 199 // The callback can be NULL. | 185 // The callback can be NULL. |
| 200 virtual PlatformFileError Truncate( | 186 virtual PlatformFileError Truncate( |
| 201 FileSystemOperationContext* context, | 187 FileSystemOperationContext* context, |
| 202 const FilePath& path, | 188 const FilePath& path, |
| 203 int64 length); | 189 int64 length); |
| 204 | 190 |
| 205 virtual bool PathExists( | 191 virtual bool PathExists( |
| 206 FileSystemOperationContext* unused, | 192 FileSystemOperationContext* context, |
| 207 const FilePath& file_path); | 193 const FilePath& file_path); |
| 208 | 194 |
| 209 virtual bool DirectoryExists( | 195 virtual bool DirectoryExists( |
| 210 FileSystemOperationContext* unused, | 196 FileSystemOperationContext* context, |
| 211 const FilePath& file_path); | 197 const FilePath& file_path); |
| 212 | 198 |
| 213 virtual bool IsDirectoryEmpty( | 199 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, | 200 FileSystemOperationContext* context, |
| 251 const FilePath& file_path); | 201 const FilePath& file_path); |
| 252 | 202 |
| 203 virtual PlatformFileError CopyOrMoveFile( |
| 204 FileSystemOperationContext* context, |
| 205 const FilePath& src_file_path, |
| 206 const FilePath& dest_file_path, |
| 207 bool copy); |
| 208 |
| 209 // Copies in a single file from a different filesystem. The src_file_path is |
| 210 // a true local platform path, regardless of which subclass of |
| 211 // FileSystemFileUtil is being invoked. |
| 212 virtual PlatformFileError CopyInForeignFile( |
| 213 FileSystemOperationContext* context, |
| 214 const FilePath& src_file_path, |
| 215 const FilePath& dest_file_path); |
| 216 |
| 217 // Deletes a single file. |
| 218 // It assumes the given path points a file. |
| 219 // |
| 220 // This method is called from DeleteDirectoryRecursive and Delete (both are |
| 221 // non-virtual). |
| 222 virtual PlatformFileError DeleteFile( |
| 223 FileSystemOperationContext* context, |
| 224 const FilePath& file_path); |
| 225 |
| 226 // Deletes a single empty directory. |
| 227 // It assumes the given path points an empty directory. |
| 228 // |
| 229 // This method is called from DeleteDirectoryRecursive and Delete (both are |
| 230 // non-virtual). |
| 231 virtual PlatformFileError DeleteSingleDirectory( |
| 232 FileSystemOperationContext* context, |
| 233 const FilePath& file_path); |
| 234 |
| 235 protected: |
| 253 // This also removes the destination directory if it's non-empty and all | 236 // 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 | 237 // other checks are passed (so that the copy/move correctly overwrites the |
| 255 // destination). | 238 // destination). |
| 256 PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( | 239 PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( |
| 257 FileSystemOperationContext* unused, | 240 FileSystemOperationContext* context, |
| 258 const FilePath& src_file_path, | 241 const FilePath& src_file_path, |
| 259 const FilePath& dest_file_path); | 242 const FilePath& dest_file_path); |
| 260 | 243 |
| 261 // Performs recursive copy or move by calling CopyOrMoveFile for individual | 244 // Performs recursive copy or move by calling CopyOrMoveFile for individual |
| 262 // files. Operations for recursive traversal are encapsulated in this method. | 245 // files. Operations for recursive traversal are encapsulated in this method. |
| 263 // It assumes src_file_path and dest_file_path have passed | 246 // It assumes src_file_path and dest_file_path have passed |
| 264 // PerformCommonCheckAndPreparationForMoveAndCopy(). | 247 // PerformCommonCheckAndPreparationForMoveAndCopy(). |
| 265 PlatformFileError CopyOrMoveDirectory( | 248 PlatformFileError CopyOrMoveDirectory( |
| 266 FileSystemOperationContext* context, | 249 FileSystemOperationContext* context, |
| 267 const FilePath& src_file_path, | 250 const FilePath& src_file_path, |
| 268 const FilePath& dest_file_path, | 251 const FilePath& dest_file_path, |
| 269 bool copy); | 252 bool copy); |
| 270 | 253 |
| 271 // Determines whether a simple same-filesystem move or copy can be done. If | 254 // 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 | 255 // so, it delegates to CopyOrMoveFile. Otherwise it looks up the true |
| 273 // platform path of the source file, delegates to CopyInForeignFile, and [for | 256 // platform path of the source file, delegates to CopyInForeignFile, and [for |
| 274 // move] calls DeleteFile on the source file. | 257 // move] calls DeleteFile on the source file. |
| 275 PlatformFileError CopyOrMoveFileHelper( | 258 PlatformFileError CopyOrMoveFileHelper( |
| 276 FileSystemOperationContext* context, | 259 FileSystemOperationContext* context, |
| 277 const FilePath& src_file_path, | 260 const FilePath& src_file_path, |
| 278 const FilePath& dest_file_path, | 261 const FilePath& dest_file_path, |
| 279 bool copy); | 262 bool copy); |
| 280 | 263 |
| 264 // Deletes a directory and all entries under the directory. |
| 265 // |
| 266 // This method is called from Delete. It internally calls two following |
| 267 // virtual methods, |
| 268 // - (virtual) DeleteFile to delete files, and |
| 269 // - (virtual) DeleteSingleDirectory to delete empty directories after all |
| 270 // the files are deleted. |
| 271 PlatformFileError DeleteDirectoryRecursive( |
| 272 FileSystemOperationContext* context, |
| 273 const FilePath& file_path); |
| 274 |
| 275 FileSystemFileUtil* underlying_file_util() const { |
| 276 return underlying_file_util_.get(); |
| 277 } |
| 278 |
| 279 private: |
| 280 scoped_ptr<FileSystemFileUtil> underlying_file_util_; |
| 281 |
| 281 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil); | 282 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil); |
| 282 }; | 283 }; |
| 283 | 284 |
| 284 } // namespace fileapi | 285 } // namespace fileapi |
| 285 | 286 |
| 286 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_ | 287 #endif // WEBKIT_FILEAPI_FILE_UTIL_H_ |
| OLD | NEW |