[Mac] Enable CrZombie for subprocesses in release builds.
[chromium-blink-merge.git] / webkit / fileapi / file_system_file_util.h
blob83a04c8538d932dceb24fd8f75f24f763f4718ec
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.
5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_
8 #include "base/file_path.h"
9 #include "base/file_util_proxy.h"
10 #include "base/platform_file.h"
12 namespace base {
13 class Time;
16 namespace fileapi {
18 using base::PlatformFile;
19 using base::PlatformFileError;
20 class FileSystemOperationContext;
22 // A large part of this implementation is taken from base::FileUtilProxy.
23 // TODO(dmikurube, kinuko): Clean up base::FileUtilProxy to factor out common
24 // routines. It includes dropping FileAPI-specific routines from FileUtilProxy.
26 // The default implementations of the virtual methods below (*1) assume the
27 // given paths are native ones for the host platform. The subclasses that
28 // perform local path translation/obfuscation must override them.
29 // (*1) All virtual methods which receive FilePath as their arguments:
30 // CreateOrOpen, EnsureFileExists, GetLocalFilePath, GetFileInfo,
31 // ReadDirectory, CreateDirectory, CopyOrMoveFile, DeleteFile,
32 // DeleteSingleDirectory, Touch, Truncate, PathExists, DirectoryExists,
33 // IsDirectoryEmpty and CreateFileEnumerator.
35 // The methods below (*2) assume the given paths may not be native ones for the
36 // host platform. The subclasses should not override them. They provide basic
37 // meta logic by using other virtual methods.
38 // (*2) All non-virtual methods: Copy, Move, Delete, DeleteDirectoryRecursive,
39 // PerformCommonCheckAndPreparationForMoveAndCopy and CopyOrMoveDirectory.
40 class FileSystemFileUtil {
41 public:
42 // It will be implemented by each subclass such as FileSystemFileEnumerator.
43 class AbstractFileEnumerator {
44 public:
45 virtual ~AbstractFileEnumerator() {}
47 // Returns an empty string if there are no more results.
48 virtual FilePath Next() = 0;
50 virtual int64 Size() = 0;
51 virtual bool IsDirectory() = 0;
54 class EmptyFileEnumerator : public AbstractFileEnumerator {
55 virtual FilePath Next() OVERRIDE { return FilePath(); }
56 virtual int64 Size() OVERRIDE { return 0; }
57 virtual bool IsDirectory() OVERRIDE { return false; }
60 virtual ~FileSystemFileUtil();
62 // Copies or moves a single file.
63 // Copies a file or a directory from |src_file_path| to |dest_file_path|.
65 // Error cases:
66 // If destination's parent doesn't exist.
67 // If source dir exists but destination path is an existing file.
68 // If source file exists but destination path is an existing directory.
69 // If source is a parent of destination.
70 // If source doesn't exist.
72 // This method calls one of the following methods depending on whether the
73 // target is a directory or not.
74 // - (virtual) CopyOrMoveFile or
75 // - (non-virtual) CopyOrMoveDirectory.
76 PlatformFileError Copy(
77 FileSystemOperationContext* context,
78 const FilePath& src_file_path,
79 const FilePath& dest_file_path);
81 // Moves a file or a directory from src_file_path to dest_file_path.
83 // Error cases are similar to Copy method's error cases.
85 // This method calls one of the following methods depending on whether the
86 // target is a directory or not.
87 // - (virtual) CopyOrMoveFile or
88 // - (non-virtual) CopyOrMoveDirectory.
89 PlatformFileError Move(
90 FileSystemOperationContext* context,
91 const FilePath& src_file_path,
92 const FilePath& dest_file_path);
94 // Deletes a file or a directory.
95 // It is an error to delete a non-empty directory with recursive=false.
97 // This method calls one of the following methods depending on whether the
98 // target is a directory or not, and whether the |recursive| flag is given or
99 // not.
100 // - (virtual) DeleteFile,
101 // - (virtual) DeleteSingleDirectory or
102 // - (non-virtual) DeleteDirectoryRecursive which calls two methods above.
103 PlatformFileError Delete(
104 FileSystemOperationContext* context,
105 const FilePath& file_path,
106 bool recursive);
108 // Creates or opens a file with the given flags. It is invalid to pass NULL
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);
120 // Close the given file handle.
121 virtual PlatformFileError Close(
122 FileSystemOperationContext* context,
123 PlatformFile);
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);
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);
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);
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);
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.
165 // The supplied context must remain valid at least lifetime of the enumerator
166 // instance.
167 virtual AbstractFileEnumerator* CreateFileEnumerator(
168 FileSystemOperationContext* context,
169 const FilePath& root_path);
171 // Maps |virtual_path| given |context| into |local_path| which represents
172 // physical file location on the host OS. This may not always make sense for
173 // all subclasses.
174 virtual PlatformFileError GetLocalFilePath(
175 FileSystemOperationContext* context,
176 const FilePath& virtual_path,
177 FilePath* local_path);
179 // Touches a file. The callback can be NULL.
180 // If the file doesn't exist, this fails with PLATFORM_FILE_ERROR_NOT_FOUND.
181 virtual PlatformFileError Touch(
182 FileSystemOperationContext* context,
183 const FilePath& file_path,
184 const base::Time& last_access_time,
185 const base::Time& last_modified_time);
187 // Truncates a file to the given length. If |length| is greater than the
188 // current length of the file, the file will be extended with zeroes.
189 // The callback can be NULL.
190 virtual PlatformFileError Truncate(
191 FileSystemOperationContext* context,
192 const FilePath& path,
193 int64 length);
195 virtual bool PathExists(
196 FileSystemOperationContext* context,
197 const FilePath& file_path);
199 virtual bool DirectoryExists(
200 FileSystemOperationContext* context,
201 const FilePath& file_path);
203 virtual bool IsDirectoryEmpty(
204 FileSystemOperationContext* context,
205 const FilePath& file_path);
207 virtual PlatformFileError CopyOrMoveFile(
208 FileSystemOperationContext* context,
209 const FilePath& src_file_path,
210 const FilePath& dest_file_path,
211 bool copy);
213 // Copies in a single file from a different filesystem. The src_file_path is
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);
221 // Deletes a single file.
222 // It assumes the given path points a file.
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);
230 // Deletes a single empty directory.
231 // It assumes the given path points an empty directory.
233 // This method is called from DeleteDirectoryRecursive and Delete (both are
234 // non-virtual).
235 virtual PlatformFileError DeleteSingleDirectory(
236 FileSystemOperationContext* context,
237 const FilePath& file_path);
239 protected:
240 FileSystemFileUtil();
241 explicit FileSystemFileUtil(FileSystemFileUtil* underlying_file_util);
243 // This also removes the destination directory if it's non-empty and all
244 // other checks are passed (so that the copy/move correctly overwrites the
245 // destination).
246 PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy(
247 FileSystemOperationContext* context,
248 const FilePath& src_file_path,
249 const FilePath& dest_file_path);
251 // Performs recursive copy or move by calling CopyOrMoveFile for individual
252 // files. Operations for recursive traversal are encapsulated in this method.
253 // It assumes src_file_path and dest_file_path have passed
254 // PerformCommonCheckAndPreparationForMoveAndCopy().
255 PlatformFileError CopyOrMoveDirectory(
256 FileSystemOperationContext* context,
257 const FilePath& src_file_path,
258 const FilePath& dest_file_path,
259 bool copy);
261 // Determines whether a simple same-filesystem move or copy can be done. If
262 // so, it delegates to CopyOrMoveFile. Otherwise it looks up the true
263 // platform path of the source file, delegates to CopyInForeignFile, and [for
264 // move] calls DeleteFile on the source file.
265 PlatformFileError CopyOrMoveFileHelper(
266 FileSystemOperationContext* context,
267 const FilePath& src_file_path,
268 const FilePath& dest_file_path,
269 bool copy);
271 // Deletes a directory and all entries under the directory.
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);
282 FileSystemFileUtil* underlying_file_util() const {
283 return underlying_file_util_.get();
286 private:
287 scoped_ptr<FileSystemFileUtil> underlying_file_util_;
289 DISALLOW_COPY_AND_ASSIGN(FileSystemFileUtil);
292 } // namespace fileapi
294 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_H_