blob: d86f79786a1f3f89052584de353c9b0bb8594774 [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;
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090045 typedef base::Callback<void(PlatformFileError /* error code */,
46 PassPlatformFile,
47 FilePath)> CreateTemporaryCallback;
jhawkins@chromium.org70bffc12011-10-18 04:39:59 +090048 typedef base::Callback<void(PlatformFileError /* error code */,
49 bool /* created */)> EnsureFileExistsCallback;
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090050 typedef base::Callback<void(PlatformFileError /* error code */,
51 const PlatformFileInfo& /* file_info */)>
52 GetFileInfoCallback;
jhawkins@chromium.orge13cc442011-10-18 05:34:19 +090053 typedef base::Callback<void(PlatformFileError /* error code */,
54 const std::vector<Entry>&)> ReadDirectoryCallback;
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090055 typedef base::Callback<void(PlatformFileError /* error code */,
56 const char* /* data */,
57 int /* bytes read/written */)> 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
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090061 // Creates or opens a file with the given flags. It is invalid to pass a null
62 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
63 // create a new file at the given |file_path| and calls back with
64 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. Takes
65 // ownership of |callback| and will delete it even on failure.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090066 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090067 const FilePath& file_path,
68 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090069 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090070
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090071 // Creates a temporary file for writing. The path and an open file handle are
72 // returned. It is invalid to pass a null callback. The additional file flags
73 // will be added on top of the default file flags which are:
noelutz@google.comf56dab22011-06-14 05:29:50 +090074 // base::PLATFORM_FILE_CREATE_ALWAYS
75 // base::PLATFORM_FILE_WRITE
76 // base::PLATFORM_FILE_TEMPORARY.
77 // Set |additional_file_flags| to 0 for synchronous writes and set to
78 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090079 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090080 scoped_refptr<MessageLoopProxy> message_loop_proxy,
noelutz@google.comf56dab22011-06-14 05:29:50 +090081 int additional_file_flags,
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090082 const CreateTemporaryCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090083
84 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090085 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090086 PlatformFile,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090087 StatusCallback* callback);
88
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090089 // Ensures that the given |file_path| exist. This creates a empty new file
90 // at |file_path| if the |file_path| does not exist.
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090091 // If a new file does not exist and is created at the |file_path|, |created|
92 // of the callback argument is set true and |error code| is set
93 // PLATFORM_FILE_OK. If the file already exists, |created| is set false and
94 // |error code| is set PLATFORM_FILE_OK. If the file hasn't existed but it
95 // couldn't be created for some other reasons, |created| is set false and
96 // |error code| indicates the error.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090097 static bool EnsureFileExists(
98 scoped_refptr<MessageLoopProxy> message_loop_proxy,
99 const FilePath& file_path,
jhawkins@chromium.org70bffc12011-10-18 04:39:59 +0900100 const EnsureFileExistsCallback& callback);
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +0900101
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900102 // Retrieves the information about a file. It is invalid to pass a null
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900103 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900104 static bool GetFileInfo(
105 scoped_refptr<MessageLoopProxy> message_loop_proxy,
106 const FilePath& file_path,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +0900107 const GetFileInfoCallback& callback);
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900108
dumi@chromium.org23915982010-09-10 12:01:14 +0900109 static bool GetFileInfoFromPlatformFile(
110 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900111 PlatformFile file,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +0900112 const GetFileInfoCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900113
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900114 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
115 const FilePath& file_path,
jhawkins@chromium.orge13cc442011-10-18 05:34:19 +0900116 const ReadDirectoryCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900117
erg@google.com37c078e2011-01-11 09:50:59 +0900118 // Creates directory at given path. It's an error to create
119 // if |exclusive| is true and dir already exists.
120 static bool CreateDirectory(
121 scoped_refptr<MessageLoopProxy> message_loop_proxy,
122 const FilePath& file_path,
123 bool exclusive,
124 bool recursive,
125 StatusCallback* callback);
126
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900127 // Copies a file or a directory from |src_file_path| to |dest_file_path|
128 // Error cases:
129 // If destination file doesn't exist or destination's parent
130 // doesn't exists.
131 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900132 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900133 // If source is a parent of destination.
134 // If source doesn't exists.
135 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
136 const FilePath& src_file_path,
137 const FilePath& dest_file_path,
138 StatusCallback* callback);
139
erg@google.com37c078e2011-01-11 09:50:59 +0900140 // Moves a file or a directory from src_file_path to dest_file_path.
141 // Error cases are similar to Copy method's error cases.
142 static bool Move(
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900143 scoped_refptr<MessageLoopProxy> message_loop_proxy,
erg@google.com37c078e2011-01-11 09:50:59 +0900144 const FilePath& src_file_path,
145 const FilePath& dest_file_path,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900146 StatusCallback* callback);
147
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900148 // Deletes a file or a directory.
149 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900150 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
151 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900152 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900153 StatusCallback* callback);
154
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900155 // Deletes a directory and all of its contents.
156 static bool RecursiveDelete(
157 scoped_refptr<MessageLoopProxy> message_loop_proxy,
158 const FilePath& file_path,
159 StatusCallback* callback);
160
dumi@chromium.org23915982010-09-10 12:01:14 +0900161 // Reads from a file. On success, the file pointer is moved to position
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900162 // |offset + bytes_to_read| in the file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900163 static bool Read(
164 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900165 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900166 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900167 int bytes_to_read,
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900168 const ReadCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900169
170 // Writes to a file. If |offset| is greater than the length of the file,
171 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900172 // |offset + bytes_to_write| in the file. The callback can be NULL.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900173 // |bytes_to_write| must be greater than zero.
dumi@chromium.org23915982010-09-10 12:01:14 +0900174 static bool Write(
175 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900176 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900177 int64 offset,
178 const char* buffer,
179 int bytes_to_write,
darin@chromium.org44a99732011-02-04 09:39:34 +0900180 WriteCallback* callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900181
182 // Touches a file. The callback can be NULL.
183 static bool Touch(
184 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900185 PlatformFile file,
186 const Time& last_access_time,
187 const Time& last_modified_time,
dumi@chromium.org23915982010-09-10 12:01:14 +0900188 StatusCallback* callback);
189
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900190 // Touches a file. The callback can be NULL.
191 static bool Touch(
192 scoped_refptr<MessageLoopProxy> message_loop_proxy,
193 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900194 const Time& last_access_time,
195 const Time& last_modified_time,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900196 StatusCallback* callback);
197
dumi@chromium.org23915982010-09-10 12:01:14 +0900198 // Truncates a file to the given length. If |length| is greater than the
199 // current length of the file, the file will be extended with zeroes.
200 // The callback can be NULL.
201 static bool Truncate(
202 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900203 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900204 int64 length,
205 StatusCallback* callback);
206
207 // Truncates a file to the given length. If |length| is greater than the
208 // current length of the file, the file will be extended with zeroes.
209 // The callback can be NULL.
210 static bool Truncate(
211 scoped_refptr<MessageLoopProxy> message_loop_proxy,
212 const FilePath& path,
213 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900214 StatusCallback* callback);
215
216 // Flushes a file. The callback can be NULL.
217 static bool Flush(
218 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900219 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900220 StatusCallback* callback);
221
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900222 private:
223 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
224};
225
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900226} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900227
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900228#endif // BASE_FILE_UTIL_PROXY_H_