blob: f2665628051b0889bb12d5501840bf25b0612064 [file] [log] [blame]
dumi@chromium.orgc980e402010-08-21 07:42:50 +09001// Copyright (c) 2010 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.
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
36 // Creates or opens a file with the given flags. It is invalid to pass NULL
37 // for the callback.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090038 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
39 // a new file at the given |file_path| and calls back with
40 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090041 typedef Callback3<PlatformFileError /* error code */,
42 PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090043 bool /* created */>::Type CreateOrOpenCallback;
44 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090045 const FilePath& file_path,
46 int file_flags,
47 CreateOrOpenCallback* callback);
48
49 // Creates a temporary file for writing. The path and an open file handle
50 // are returned. It is invalid to pass NULL for the callback.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090051 typedef Callback3<PlatformFileError /* error code */,
52 PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090053 FilePath>::Type CreateTemporaryCallback;
54 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090055 scoped_refptr<MessageLoopProxy> message_loop_proxy,
56 CreateTemporaryCallback* callback);
57
58 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090059 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090060 PlatformFile,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090061 StatusCallback* callback);
62
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090063 // Ensures that the given |file_path| exist. This creates a empty new file
64 // at |file_path| if the |file_path| does not exist.
65 // If a new file han not existed and is created at the |file_path|,
66 // |created| of the callback argument is set true and |error code|
67 // is set PLATFORM_FILE_OK.
68 // If the file already exists, |created| is set false and |error code|
69 // is set PLATFORM_FILE_OK.
70 // If the file hasn't existed but it couldn't be created for some other
71 // reasons, |created| is set false and |error code| indicates the error.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090072 typedef Callback2<PlatformFileError /* error code */,
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090073 bool /* created */>::Type EnsureFileExistsCallback;
74 static bool EnsureFileExists(
75 scoped_refptr<MessageLoopProxy> message_loop_proxy,
76 const FilePath& file_path,
77 EnsureFileExistsCallback* callback);
78
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090079 // Retrieves the information about a file. It is invalid to pass NULL for the
80 // callback.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090081 typedef Callback2<PlatformFileError /* error code */,
82 const PlatformFileInfo& /* file_info */
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090083 >::Type GetFileInfoCallback;
84 static bool GetFileInfo(
85 scoped_refptr<MessageLoopProxy> message_loop_proxy,
86 const FilePath& file_path,
87 GetFileInfoCallback* callback);
88
dumi@chromium.org23915982010-09-10 12:01:14 +090089 static bool GetFileInfoFromPlatformFile(
90 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090091 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +090092 GetFileInfoCallback* callback);
93
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090094 typedef Callback2<PlatformFileError /* error code */,
95 const std::vector<Entry>&>::Type ReadDirectoryCallback;
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090096 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
97 const FilePath& file_path,
98 ReadDirectoryCallback* callback);
99
100 // Copies a file or a directory from |src_file_path| to |dest_file_path|
101 // Error cases:
102 // If destination file doesn't exist or destination's parent
103 // doesn't exists.
104 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900105 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900106 // If source is a parent of destination.
107 // If source doesn't exists.
108 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
109 const FilePath& src_file_path,
110 const FilePath& dest_file_path,
111 StatusCallback* callback);
112
113 // Creates directory at given path. It's an error to create
114 // if |exclusive| is true and dir already exists.
115 static bool CreateDirectory(
116 scoped_refptr<MessageLoopProxy> message_loop_proxy,
117 const FilePath& file_path,
118 bool exclusive,
kinuko@chromium.orgc483d792010-09-08 09:50:41 +0900119 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900120 StatusCallback* callback);
121
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900122 // Deletes a file or a directory.
123 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900124 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
125 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900126 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900127 StatusCallback* callback);
128
129 // Moves a file or a directory from src_file_path to dest_file_path.
130 // Error cases are similar to Copy method's error cases.
131 static bool Move(
132 scoped_refptr<MessageLoopProxy> message_loop_proxy,
133 const FilePath& src_file_path,
134 const FilePath& dest_file_path,
135 StatusCallback* callback);
136
137 // Deletes a directory and all of its contents.
138 static bool RecursiveDelete(
139 scoped_refptr<MessageLoopProxy> message_loop_proxy,
140 const FilePath& file_path,
141 StatusCallback* callback);
142
dumi@chromium.org23915982010-09-10 12:01:14 +0900143 // Reads from a file. On success, the file pointer is moved to position
144 // |offset + bytes_to_read| in the file. The callback can be NULL.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900145 typedef Callback2<PlatformFileError /* error code */,
dumi@chromium.org23915982010-09-10 12:01:14 +0900146 int /* bytes read/written */>::Type ReadWriteCallback;
147 static bool Read(
148 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900149 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900150 int64 offset,
151 char* buffer,
152 int bytes_to_read,
153 ReadWriteCallback* callback);
154
155 // Writes to a file. If |offset| is greater than the length of the file,
156 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900157 // |offset + bytes_to_write| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900158 static bool Write(
159 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900160 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900161 int64 offset,
162 const char* buffer,
163 int bytes_to_write,
164 ReadWriteCallback* callback);
165
166 // Touches a file. The callback can be NULL.
167 static bool Touch(
168 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900169 PlatformFile file,
170 const Time& last_access_time,
171 const Time& last_modified_time,
dumi@chromium.org23915982010-09-10 12:01:14 +0900172 StatusCallback* callback);
173
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900174 // Touches a file. The callback can be NULL.
175 static bool Touch(
176 scoped_refptr<MessageLoopProxy> message_loop_proxy,
177 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900178 const Time& last_access_time,
179 const Time& last_modified_time,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900180 StatusCallback* callback);
181
dumi@chromium.org23915982010-09-10 12:01:14 +0900182 // Truncates a file to the given length. If |length| is greater than the
183 // current length of the file, the file will be extended with zeroes.
184 // The callback can be NULL.
185 static bool Truncate(
186 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900187 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900188 int64 length,
189 StatusCallback* callback);
190
191 // Truncates a file to the given length. If |length| is greater than the
192 // current length of the file, the file will be extended with zeroes.
193 // The callback can be NULL.
194 static bool Truncate(
195 scoped_refptr<MessageLoopProxy> message_loop_proxy,
196 const FilePath& path,
197 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900198 StatusCallback* callback);
199
200 // Flushes a file. The callback can be NULL.
201 static bool Flush(
202 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900203 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900204 StatusCallback* callback);
205
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900206 private:
207 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
208};
209
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900210} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900211
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900212#endif // BASE_FILE_UTIL_PROXY_H_