blob: 11fc988b74ce7cfffa79dd8add756b573ad51cce [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.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090043 typedef Callback3<base::PlatformFileError /* error code */,
44 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090045 bool /* created */>::Type CreateOrOpenCallback;
46 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090047 const FilePath& file_path,
48 int file_flags,
49 CreateOrOpenCallback* callback);
50
kinuko@chromium.orga122bcf2010-10-12 07:00:55 +090051 // Creates a file with the given flags. This one is a variation of
52 // CreateOrOpen but it doesn't return a file handle.
53 static bool Create(scoped_refptr<MessageLoopProxy> message_loop_proxy,
54 const FilePath& file_path,
55 int file_flags,
56 CreateOrOpenCallback* callback);
57
dumi@chromium.orgc980e402010-08-21 07:42:50 +090058 // Creates a temporary file for writing. The path and an open file handle
59 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090060 typedef Callback3<base::PlatformFileError /* error code */,
61 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090062 FilePath>::Type CreateTemporaryCallback;
63 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090064 scoped_refptr<MessageLoopProxy> message_loop_proxy,
65 CreateTemporaryCallback* callback);
66
67 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090068 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090069 base::PlatformFile,
70 StatusCallback* callback);
71
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090072 // Retrieves the information about a file. It is invalid to pass NULL for the
73 // callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090074 typedef Callback2<base::PlatformFileError /* error code */,
dumi@chromium.org97ae2612010-09-03 11:28:37 +090075 const base::PlatformFileInfo& /* file_info */
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090076 >::Type GetFileInfoCallback;
77 static bool GetFileInfo(
78 scoped_refptr<MessageLoopProxy> message_loop_proxy,
79 const FilePath& file_path,
80 GetFileInfoCallback* callback);
81
dumi@chromium.org23915982010-09-10 12:01:14 +090082 static bool GetFileInfoFromPlatformFile(
83 scoped_refptr<MessageLoopProxy> message_loop_proxy,
84 base::PlatformFile file,
85 GetFileInfoCallback* callback);
86
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090087 typedef Callback2<base::PlatformFileError /* error code */,
88 const std::vector<base::file_util_proxy::Entry>&
89 >::Type ReadDirectoryCallback;
90 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
91 const FilePath& file_path,
92 ReadDirectoryCallback* callback);
93
94 // Copies a file or a directory from |src_file_path| to |dest_file_path|
95 // Error cases:
96 // If destination file doesn't exist or destination's parent
97 // doesn't exists.
98 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +090099 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900100 // If source is a parent of destination.
101 // If source doesn't exists.
102 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
103 const FilePath& src_file_path,
104 const FilePath& dest_file_path,
105 StatusCallback* callback);
106
107 // Creates directory at given path. It's an error to create
108 // if |exclusive| is true and dir already exists.
109 static bool CreateDirectory(
110 scoped_refptr<MessageLoopProxy> message_loop_proxy,
111 const FilePath& file_path,
112 bool exclusive,
kinuko@chromium.orgc483d792010-09-08 09:50:41 +0900113 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900114 StatusCallback* callback);
115
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900116 // Deletes a file or a directory.
117 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900118 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
119 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900120 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900121 StatusCallback* callback);
122
123 // Moves a file or a directory from src_file_path to dest_file_path.
124 // Error cases are similar to Copy method's error cases.
125 static bool Move(
126 scoped_refptr<MessageLoopProxy> message_loop_proxy,
127 const FilePath& src_file_path,
128 const FilePath& dest_file_path,
129 StatusCallback* callback);
130
131 // Deletes a directory and all of its contents.
132 static bool RecursiveDelete(
133 scoped_refptr<MessageLoopProxy> message_loop_proxy,
134 const FilePath& file_path,
135 StatusCallback* callback);
136
dumi@chromium.org23915982010-09-10 12:01:14 +0900137 // Reads from a file. On success, the file pointer is moved to position
138 // |offset + bytes_to_read| in the file. The callback can be NULL.
139 typedef Callback2<base::PlatformFileError /* error code */,
140 int /* bytes read/written */>::Type ReadWriteCallback;
141 static bool Read(
142 scoped_refptr<MessageLoopProxy> message_loop_proxy,
143 base::PlatformFile file,
144 int64 offset,
145 char* buffer,
146 int bytes_to_read,
147 ReadWriteCallback* callback);
148
149 // Writes to a file. If |offset| is greater than the length of the file,
150 // |false| is returned. On success, the file pointer is moved to position
ericu@google.com6a652222010-10-05 11:26:47 +0900151 // |offset + bytes_to_write| in the file. The callback can be NULL.
dumi@chromium.org23915982010-09-10 12:01:14 +0900152 static bool Write(
153 scoped_refptr<MessageLoopProxy> message_loop_proxy,
154 base::PlatformFile file,
155 int64 offset,
156 const char* buffer,
157 int bytes_to_write,
158 ReadWriteCallback* callback);
159
160 // Touches a file. The callback can be NULL.
161 static bool Touch(
162 scoped_refptr<MessageLoopProxy> message_loop_proxy,
163 base::PlatformFile file,
164 const base::Time& last_access_time,
165 const base::Time& last_modified_time,
166 StatusCallback* callback);
167
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900168 // Touches a file. The callback can be NULL.
169 static bool Touch(
170 scoped_refptr<MessageLoopProxy> message_loop_proxy,
171 const FilePath& file_path,
172 const base::Time& last_access_time,
173 const base::Time& last_modified_time,
174 StatusCallback* callback);
175
dumi@chromium.org23915982010-09-10 12:01:14 +0900176 // Truncates a file to the given length. If |length| is greater than the
177 // current length of the file, the file will be extended with zeroes.
178 // The callback can be NULL.
179 static bool Truncate(
180 scoped_refptr<MessageLoopProxy> message_loop_proxy,
181 base::PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900182 int64 length,
183 StatusCallback* callback);
184
185 // Truncates a file to the given length. If |length| is greater than the
186 // current length of the file, the file will be extended with zeroes.
187 // The callback can be NULL.
188 static bool Truncate(
189 scoped_refptr<MessageLoopProxy> message_loop_proxy,
190 const FilePath& path,
191 int64 length,
dumi@chromium.org23915982010-09-10 12:01:14 +0900192 StatusCallback* callback);
193
194 // Flushes a file. The callback can be NULL.
195 static bool Flush(
196 scoped_refptr<MessageLoopProxy> message_loop_proxy,
197 base::PlatformFile file,
198 StatusCallback* callback);
199
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900200 private:
201 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
202};
203
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900204} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900205
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900206#endif // BASE_FILE_UTIL_PROXY_H_