blob: 808c60e03fc1e9b0ee3bf3b1a4062d504c12cb73 [file] [log] [blame]
erg@google.comd5fffd42011-01-08 03:06:45 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
dumi@chromium.orgc980e402010-08-21 07:42:50 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +09005#ifndef BASE_FILE_UTIL_PROXY_H_
6#define BASE_FILE_UTIL_PROXY_H_
7
8#include <vector>
dumi@chromium.orgc980e402010-08-21 07:42:50 +09009
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090011#include "base/callback.h"
jhawkins@chromium.orgf7b5ad92011-05-10 09:37:40 +090012#include "base/callback_old.h"
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090013#include "base/file_path.h"
14#include "base/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090015#include "base/memory/ref_counted.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090016#include "base/platform_file.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090017#include "base/tracked_objects.h"
18
19namespace base {
20
21class MessageLoopProxy;
dumi@chromium.org97ae2612010-09-03 11:28:37 +090022class Time;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090023
24// This class provides asynchronous access to common file routines.
darin@chromium.orge585bed2011-08-06 00:34:00 +090025class BASE_EXPORT FileUtilProxy {
dumi@chromium.orgc980e402010-08-21 07:42:50 +090026 public:
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090027 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback.
28 struct Entry {
29 FilePath::StringType name;
30 bool is_directory;
tzik@chromium.org09987c32011-07-19 14:03:03 +090031 int64 size;
32 base::Time last_modified_time;
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090033 };
34
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090035 // This callback is used by methods that report only an error code. It is
dumi@chromium.orgc980e402010-08-21 07:42:50 +090036 // valid to pass NULL as the callback parameter to any function that takes a
37 // StatusCallback, in which case the operation will complete silently.
kinuko@chromium.org6e168d32011-08-19 19:14:27 +090038 // The ownership of |callback| is taken by the function and will always be
39 // deleted by the function even on failure.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090040 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090041
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090042 typedef base::Callback<void(PlatformFileError /* error code */,
43 PassPlatformFile,
44 bool /* created */)> CreateOrOpenCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090045 typedef Callback3<PlatformFileError /* error code */,
46 PassPlatformFile,
47 FilePath>::Type CreateTemporaryCallback;
48 typedef Callback2<PlatformFileError /* error code */,
49 bool /* created */>::Type EnsureFileExistsCallback;
50 typedef Callback2<PlatformFileError /* error code */,
51 const PlatformFileInfo& /* file_info */
52 >::Type GetFileInfoCallback;
53 typedef Callback2<PlatformFileError /* error code */,
54 const std::vector<Entry>&>::Type ReadDirectoryCallback;
darin@chromium.org44a99732011-02-04 09:39:34 +090055 typedef Callback3<PlatformFileError /* error code */,
56 const char* /* data */,
57 int /* bytes read/written */>::Type ReadCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090058 typedef Callback2<PlatformFileError /* error code */,
darin@chromium.org44a99732011-02-04 09:39:34 +090059 int /* bytes written */>::Type WriteCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090060
dumi@chromium.orgc980e402010-08-21 07:42:50 +090061 // Creates or opens a file with the given flags. It is invalid to pass NULL
62 // for the callback.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090063 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
64 // a new file at the given |file_path| and calls back with
65 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
kinuko@chromium.org6e168d32011-08-19 19:14:27 +090066 // Takes ownership of |callback| and will delete it even on failure.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090067 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090068 const FilePath& file_path,
69 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090070 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090071
72 // Creates a temporary file for writing. The path and an open file handle
noelutz@google.comf56dab22011-06-14 05:29:50 +090073 // are returned. It is invalid to pass NULL for the callback. The additional
74 // file flags will be added on top of the default file flags which are:
75 // base::PLATFORM_FILE_CREATE_ALWAYS
76 // base::PLATFORM_FILE_WRITE
77 // base::PLATFORM_FILE_TEMPORARY.
78 // Set |additional_file_flags| to 0 for synchronous writes and set to
79 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090080 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090081 scoped_refptr<MessageLoopProxy> message_loop_proxy,
noelutz@google.comf56dab22011-06-14 05:29:50 +090082 int additional_file_flags,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090083 CreateTemporaryCallback* callback);
84
85 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090086 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090087 PlatformFile,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090088 StatusCallback* callback);
89
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090090 // Ensures that the given |file_path| exist. This creates a empty new file
91 // at |file_path| if the |file_path| does not exist.
92 // If a new file han not existed and is created at the |file_path|,
93 // |created| of the callback argument is set true and |error code|
94 // is set PLATFORM_FILE_OK.
95 // If the file already exists, |created| is set false and |error code|
96 // is set PLATFORM_FILE_OK.
97 // If the file hasn't existed but it couldn't be created for some other
98 // reasons, |created| is set false and |error code| indicates the error.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090099 static bool EnsureFileExists(
100 scoped_refptr<MessageLoopProxy> message_loop_proxy,
101 const FilePath& file_path,
102 EnsureFileExistsCallback* callback);
103
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900104 // Retrieves the information about a file. It is invalid to pass NULL for the
105 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900106 static bool GetFileInfo(
107 scoped_refptr<MessageLoopProxy> message_loop_proxy,
108 const FilePath& file_path,
109 GetFileInfoCallback* callback);
110
dumi@chromium.org23915982010-09-10 12:01:14 +0900111 static bool GetFileInfoFromPlatformFile(
112 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900113 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900114 GetFileInfoCallback* callback);
115
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900116 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
117 const FilePath& file_path,
118 ReadDirectoryCallback* callback);
119
erg@google.com37c078e2011-01-11 09:50:59 +0900120 // Creates directory at given path. It's an error to create
121 // if |exclusive| is true and dir already exists.
122 static bool CreateDirectory(
123 scoped_refptr<MessageLoopProxy> message_loop_proxy,
124 const FilePath& file_path,
125 bool exclusive,
126 bool recursive,
127 StatusCallback* callback);
128
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900129 // Copies a file or a directory from |src_file_path| to |dest_file_path|
130 // Error cases:
131 // If destination file doesn't exist or destination's parent
132 // doesn't exists.
133 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900134 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900135 // If source is a parent of destination.
136 // If source doesn't exists.
137 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
138 const FilePath& src_file_path,
139 const FilePath& dest_file_path,
140 StatusCallback* callback);
141
erg@google.com37c078e2011-01-11 09:50:59 +0900142 // Moves a file or a directory from src_file_path to dest_file_path.
143 // Error cases are similar to Copy method's error cases.
144 static bool Move(
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900145 scoped_refptr<MessageLoopProxy> message_loop_proxy,
erg@google.com37c078e2011-01-11 09:50:59 +0900146 const FilePath& src_file_path,
147 const FilePath& dest_file_path,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900148 StatusCallback* callback);
149
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900150 // Deletes a file or a directory.
151 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900152 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
153 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900154 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900155 StatusCallback* callback);
156
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900157 // Deletes a directory and all of its contents.
158 static bool RecursiveDelete(
159 scoped_refptr<MessageLoopProxy> message_loop_proxy,
160 const FilePath& file_path,
161 StatusCallback* callback);
162
dumi@chromium.org23915982010-09-10 12:01:14 +0900163 // Reads from a file. On success, the file pointer is moved to position
164 // |offset + bytes_to_read| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900165 static bool Read(
166 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900167 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900168 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900169 int bytes_to_read,
darin@chromium.org44a99732011-02-04 09:39:34 +0900170 ReadCallback* callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900171
172 // Writes to a file. If |offset| is greater than the length of the file,
173 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900174 // |offset + bytes_to_write| in the file. The callback can be NULL.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900175 // |bytes_to_write| must be greater than zero.
dumi@chromium.org23915982010-09-10 12:01:14 +0900176 static bool Write(
177 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900178 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900179 int64 offset,
180 const char* buffer,
181 int bytes_to_write,
darin@chromium.org44a99732011-02-04 09:39:34 +0900182 WriteCallback* callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900183
184 // Touches a file. The callback can be NULL.
185 static bool Touch(
186 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900187 PlatformFile file,
188 const Time& last_access_time,
189 const Time& last_modified_time,
dumi@chromium.org23915982010-09-10 12:01:14 +0900190 StatusCallback* callback);
191
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900192 // Touches a file. The callback can be NULL.
193 static bool Touch(
194 scoped_refptr<MessageLoopProxy> message_loop_proxy,
195 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900196 const Time& last_access_time,
197 const Time& last_modified_time,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900198 StatusCallback* callback);
199
dumi@chromium.org23915982010-09-10 12:01:14 +0900200 // 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.
202 // The callback can be NULL.
203 static bool Truncate(
204 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900205 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900206 int64 length,
207 StatusCallback* callback);
208
209 // Truncates a file to the given length. If |length| is greater than the
210 // current length of the file, the file will be extended with zeroes.
211 // The callback can be NULL.
212 static bool Truncate(
213 scoped_refptr<MessageLoopProxy> message_loop_proxy,
214 const FilePath& path,
215 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900216 StatusCallback* callback);
217
218 // Flushes a file. The callback can be NULL.
219 static bool Flush(
220 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900221 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900222 StatusCallback* callback);
223
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900224 private:
225 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
226};
227
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900228} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900229
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900230#endif // BASE_FILE_UTIL_PROXY_H_