blob: 09c23778a09bb49c8f49d8173d8800548398c72b [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
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090019namespace file_util_proxy {
kinuko@chromium.org83604d12010-09-03 07:28:49 +090020
21// Holds metadata for file or directory entry.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090022struct Entry {
23 FilePath::StringType name;
kinuko@chromium.org83604d12010-09-03 07:28:49 +090024 bool is_directory;
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090025};
kinuko@chromium.org83604d12010-09-03 07:28:49 +090026
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090027} // namespace file_util_proxy
28
dumi@chromium.orgc980e402010-08-21 07:42:50 +090029class MessageLoopProxy;
dumi@chromium.org97ae2612010-09-03 11:28:37 +090030class Time;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090031
32// This class provides asynchronous access to common file routines.
33class FileUtilProxy {
34 public:
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.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090038 typedef Callback1<base::PlatformFileError /* error code */
39 >::Type StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090040
41 // Creates or opens a file with the given flags. It is invalid to pass NULL
42 // for the callback.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090043 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
44 // a new file at the given |file_path| and calls back with
45 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090046 typedef Callback3<base::PlatformFileError /* error code */,
47 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090048 bool /* created */>::Type CreateOrOpenCallback;
49 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090050 const FilePath& file_path,
51 int file_flags,
52 CreateOrOpenCallback* callback);
53
54 // Creates a temporary file for writing. The path and an open file handle
55 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090056 typedef Callback3<base::PlatformFileError /* error code */,
57 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090058 FilePath>::Type CreateTemporaryCallback;
59 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090060 scoped_refptr<MessageLoopProxy> message_loop_proxy,
61 CreateTemporaryCallback* callback);
62
63 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090064 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090065 base::PlatformFile,
66 StatusCallback* callback);
67
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090068 // Ensures that the given |file_path| exist. This creates a empty new file
69 // at |file_path| if the |file_path| does not exist.
70 // If a new file han not existed and is created at the |file_path|,
71 // |created| of the callback argument is set true and |error code|
72 // is set PLATFORM_FILE_OK.
73 // If the file already exists, |created| is set false and |error code|
74 // is set PLATFORM_FILE_OK.
75 // If the file hasn't existed but it couldn't be created for some other
76 // reasons, |created| is set false and |error code| indicates the error.
77 typedef Callback2<base::PlatformFileError /* error code */,
78 bool /* created */>::Type EnsureFileExistsCallback;
79 static bool EnsureFileExists(
80 scoped_refptr<MessageLoopProxy> message_loop_proxy,
81 const FilePath& file_path,
82 EnsureFileExistsCallback* callback);
83
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090084 // Retrieves the information about a file. It is invalid to pass NULL for the
85 // callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090086 typedef Callback2<base::PlatformFileError /* error code */,
dumi@chromium.org97ae2612010-09-03 11:28:37 +090087 const base::PlatformFileInfo& /* file_info */
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090088 >::Type GetFileInfoCallback;
89 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,
96 base::PlatformFile file,
97 GetFileInfoCallback* callback);
98
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090099 typedef Callback2<base::PlatformFileError /* error code */,
100 const std::vector<base::file_util_proxy::Entry>&
101 >::Type ReadDirectoryCallback;
102 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
103 const FilePath& file_path,
104 ReadDirectoryCallback* callback);
105
106 // Copies a file or a directory from |src_file_path| to |dest_file_path|
107 // Error cases:
108 // If destination file doesn't exist or destination's parent
109 // doesn't exists.
110 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900111 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900112 // If source is a parent of destination.
113 // If source doesn't exists.
114 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
115 const FilePath& src_file_path,
116 const FilePath& dest_file_path,
117 StatusCallback* callback);
118
119 // Creates directory at given path. It's an error to create
120 // if |exclusive| is true and dir already exists.
121 static bool CreateDirectory(
122 scoped_refptr<MessageLoopProxy> message_loop_proxy,
123 const FilePath& file_path,
124 bool exclusive,
kinuko@chromium.orgc483d792010-09-08 09:50:41 +0900125 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900126 StatusCallback* callback);
127
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900128 // Deletes a file or a directory.
129 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900130 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
131 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900132 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900133 StatusCallback* callback);
134
135 // Moves a file or a directory from src_file_path to dest_file_path.
136 // Error cases are similar to Copy method's error cases.
137 static bool Move(
138 scoped_refptr<MessageLoopProxy> message_loop_proxy,
139 const FilePath& src_file_path,
140 const FilePath& dest_file_path,
141 StatusCallback* callback);
142
143 // Deletes a directory and all of its contents.
144 static bool RecursiveDelete(
145 scoped_refptr<MessageLoopProxy> message_loop_proxy,
146 const FilePath& file_path,
147 StatusCallback* callback);
148
dumi@chromium.org23915982010-09-10 12:01:14 +0900149 // Reads from a file. On success, the file pointer is moved to position
150 // |offset + bytes_to_read| in the file. The callback can be NULL.
151 typedef Callback2<base::PlatformFileError /* error code */,
152 int /* bytes read/written */>::Type ReadWriteCallback;
153 static bool Read(
154 scoped_refptr<MessageLoopProxy> message_loop_proxy,
155 base::PlatformFile file,
156 int64 offset,
157 char* buffer,
158 int bytes_to_read,
159 ReadWriteCallback* callback);
160
161 // Writes to a file. If |offset| is greater than the length of the file,
162 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900163 // |offset + bytes_to_write| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900164 static bool Write(
165 scoped_refptr<MessageLoopProxy> message_loop_proxy,
166 base::PlatformFile file,
167 int64 offset,
168 const char* buffer,
169 int bytes_to_write,
170 ReadWriteCallback* callback);
171
172 // Touches a file. The callback can be NULL.
173 static bool Touch(
174 scoped_refptr<MessageLoopProxy> message_loop_proxy,
175 base::PlatformFile file,
176 const base::Time& last_access_time,
177 const base::Time& last_modified_time,
178 StatusCallback* callback);
179
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900180 // Touches a file. The callback can be NULL.
181 static bool Touch(
182 scoped_refptr<MessageLoopProxy> message_loop_proxy,
183 const FilePath& file_path,
184 const base::Time& last_access_time,
185 const base::Time& last_modified_time,
186 StatusCallback* callback);
187
dumi@chromium.org23915982010-09-10 12:01:14 +0900188 // Truncates a file to the given length. If |length| is greater than the
189 // current length of the file, the file will be extended with zeroes.
190 // The callback can be NULL.
191 static bool Truncate(
192 scoped_refptr<MessageLoopProxy> message_loop_proxy,
193 base::PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900194 int64 length,
195 StatusCallback* callback);
196
197 // Truncates a file to the given length. If |length| is greater than the
198 // current length of the file, the file will be extended with zeroes.
199 // The callback can be NULL.
200 static bool Truncate(
201 scoped_refptr<MessageLoopProxy> message_loop_proxy,
202 const FilePath& path,
203 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900204 StatusCallback* callback);
205
206 // Flushes a file. The callback can be NULL.
207 static bool Flush(
208 scoped_refptr<MessageLoopProxy> message_loop_proxy,
209 base::PlatformFile file,
210 StatusCallback* callback);
211
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900212 private:
213 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
214};
215
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900216} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900217
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900218#endif // BASE_FILE_UTIL_PROXY_H_