blob: b5e28c0b838ede81ab0e536b119a259b8f914322 [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
10#include "base/callback.h"
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090011#include "base/file_path.h"
12#include "base/file_util.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090013#include "base/platform_file.h"
14#include "base/ref_counted.h"
15#include "base/tracked_objects.h"
16
17namespace base {
18
19class MessageLoopProxy;
dumi@chromium.org97ae2612010-09-03 11:28:37 +090020class Time;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090021
22// This class provides asynchronous access to common file routines.
23class FileUtilProxy {
24 public:
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090025 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback.
26 struct Entry {
27 FilePath::StringType name;
28 bool is_directory;
29 };
30
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090031 // This callback is used by methods that report only an error code. It is
dumi@chromium.orgc980e402010-08-21 07:42:50 +090032 // valid to pass NULL as the callback parameter to any function that takes a
33 // StatusCallback, in which case the operation will complete silently.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090034 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090035
erg@google.comd5fffd42011-01-08 03:06:45 +090036 typedef Callback3<PlatformFileError /* error code */,
37 PassPlatformFile,
38 bool /* created */>::Type CreateOrOpenCallback;
39 typedef Callback3<PlatformFileError /* error code */,
40 PassPlatformFile,
41 FilePath>::Type CreateTemporaryCallback;
42 typedef Callback2<PlatformFileError /* error code */,
43 bool /* created */>::Type EnsureFileExistsCallback;
44 typedef Callback2<PlatformFileError /* error code */,
45 const PlatformFileInfo& /* file_info */
46 >::Type GetFileInfoCallback;
47 typedef Callback2<PlatformFileError /* error code */,
48 const std::vector<Entry>&>::Type ReadDirectoryCallback;
49 typedef Callback2<PlatformFileError /* error code */,
50 int /* bytes read/written */>::Type ReadWriteCallback;
51
dumi@chromium.orgc980e402010-08-21 07:42:50 +090052 // Creates or opens a file with the given flags. It is invalid to pass NULL
53 // for the callback.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090054 // 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.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090057 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090058 const FilePath& file_path,
59 int file_flags,
60 CreateOrOpenCallback* callback);
61
62 // Creates a temporary file for writing. The path and an open file handle
63 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090064 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090065 scoped_refptr<MessageLoopProxy> message_loop_proxy,
66 CreateTemporaryCallback* callback);
67
68 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090069 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090070 PlatformFile,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090071 StatusCallback* callback);
72
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090073 // Ensures that the given |file_path| exist. This creates a empty new file
74 // at |file_path| if the |file_path| does not exist.
75 // If a new file han not existed and is created at the |file_path|,
76 // |created| of the callback argument is set true and |error code|
77 // is set PLATFORM_FILE_OK.
78 // If the file already exists, |created| is set false and |error code|
79 // is set PLATFORM_FILE_OK.
80 // If the file hasn't existed but it couldn't be created for some other
81 // reasons, |created| is set false and |error code| indicates the error.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090082 static bool EnsureFileExists(
83 scoped_refptr<MessageLoopProxy> message_loop_proxy,
84 const FilePath& file_path,
85 EnsureFileExistsCallback* callback);
86
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090087 // Retrieves the information about a file. It is invalid to pass NULL for the
88 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090089 static bool GetFileInfo(
90 scoped_refptr<MessageLoopProxy> message_loop_proxy,
91 const FilePath& file_path,
92 GetFileInfoCallback* callback);
93
dumi@chromium.org23915982010-09-10 12:01:14 +090094 static bool GetFileInfoFromPlatformFile(
95 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090096 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +090097 GetFileInfoCallback* callback);
98
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090099 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
100 const FilePath& file_path,
101 ReadDirectoryCallback* callback);
102
erg@google.com37c078e2011-01-11 09:50:59 +0900103 // Creates directory at given path. It's an error to create
104 // if |exclusive| is true and dir already exists.
105 static bool CreateDirectory(
106 scoped_refptr<MessageLoopProxy> message_loop_proxy,
107 const FilePath& file_path,
108 bool exclusive,
109 bool recursive,
110 StatusCallback* callback);
111
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900112 // Copies a file or a directory from |src_file_path| to |dest_file_path|
113 // Error cases:
114 // If destination file doesn't exist or destination's parent
115 // doesn't exists.
116 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900117 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900118 // If source is a parent of destination.
119 // If source doesn't exists.
120 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
121 const FilePath& src_file_path,
122 const FilePath& dest_file_path,
123 StatusCallback* callback);
124
erg@google.com37c078e2011-01-11 09:50:59 +0900125 // Moves a file or a directory from src_file_path to dest_file_path.
126 // Error cases are similar to Copy method's error cases.
127 static bool Move(
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900128 scoped_refptr<MessageLoopProxy> message_loop_proxy,
erg@google.com37c078e2011-01-11 09:50:59 +0900129 const FilePath& src_file_path,
130 const FilePath& dest_file_path,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900131 StatusCallback* callback);
132
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900133 // Deletes a file or a directory.
134 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900135 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
136 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900137 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900138 StatusCallback* callback);
139
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900140 // Deletes a directory and all of its contents.
141 static bool RecursiveDelete(
142 scoped_refptr<MessageLoopProxy> message_loop_proxy,
143 const FilePath& file_path,
144 StatusCallback* callback);
145
dumi@chromium.org23915982010-09-10 12:01:14 +0900146 // Reads from a file. On success, the file pointer is moved to position
147 // |offset + bytes_to_read| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900148 static bool Read(
149 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900150 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900151 int64 offset,
152 char* buffer,
153 int bytes_to_read,
154 ReadWriteCallback* callback);
155
156 // Writes to a file. If |offset| is greater than the length of the file,
157 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900158 // |offset + bytes_to_write| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900159 static bool Write(
160 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900161 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900162 int64 offset,
163 const char* buffer,
164 int bytes_to_write,
165 ReadWriteCallback* callback);
166
167 // Touches a file. The callback can be NULL.
168 static bool Touch(
169 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900170 PlatformFile file,
171 const Time& last_access_time,
172 const Time& last_modified_time,
dumi@chromium.org23915982010-09-10 12:01:14 +0900173 StatusCallback* callback);
174
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900175 // Touches a file. The callback can be NULL.
176 static bool Touch(
177 scoped_refptr<MessageLoopProxy> message_loop_proxy,
178 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900179 const Time& last_access_time,
180 const Time& last_modified_time,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900181 StatusCallback* callback);
182
dumi@chromium.org23915982010-09-10 12:01:14 +0900183 // Truncates a file to the given length. If |length| is greater than the
184 // current length of the file, the file will be extended with zeroes.
185 // The callback can be NULL.
186 static bool Truncate(
187 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900188 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900189 int64 length,
190 StatusCallback* callback);
191
192 // Truncates a file to the given length. If |length| is greater than the
193 // current length of the file, the file will be extended with zeroes.
194 // The callback can be NULL.
195 static bool Truncate(
196 scoped_refptr<MessageLoopProxy> message_loop_proxy,
197 const FilePath& path,
198 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900199 StatusCallback* callback);
200
201 // Flushes a file. The callback can be NULL.
202 static bool Flush(
203 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900204 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900205 StatusCallback* callback);
206
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900207 private:
208 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
209};
210
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900211} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900212
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900213#endif // BASE_FILE_UTIL_PROXY_H_