blob: 081c11b31739a29995932bded93cdf57d693e18f [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
51 // Creates a temporary file for writing. The path and an open file handle
52 // are returned. It is invalid to pass NULL for the callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090053 typedef Callback3<base::PlatformFileError /* error code */,
54 base::PassPlatformFile,
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090055 FilePath>::Type CreateTemporaryCallback;
56 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090057 scoped_refptr<MessageLoopProxy> message_loop_proxy,
58 CreateTemporaryCallback* callback);
59
60 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090061 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090062 base::PlatformFile,
63 StatusCallback* callback);
64
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090065 // Retrieves the information about a file. It is invalid to pass NULL for the
66 // callback.
dumi@chromium.org50f197d2010-09-01 04:30:27 +090067 typedef Callback2<base::PlatformFileError /* error code */,
dumi@chromium.org97ae2612010-09-03 11:28:37 +090068 const base::PlatformFileInfo& /* file_info */
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090069 >::Type GetFileInfoCallback;
70 static bool GetFileInfo(
71 scoped_refptr<MessageLoopProxy> message_loop_proxy,
72 const FilePath& file_path,
73 GetFileInfoCallback* callback);
74
dumi@chromium.org23915982010-09-10 12:01:14 +090075 static bool GetFileInfoFromPlatformFile(
76 scoped_refptr<MessageLoopProxy> message_loop_proxy,
77 base::PlatformFile file,
78 GetFileInfoCallback* callback);
79
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090080 typedef Callback2<base::PlatformFileError /* error code */,
81 const std::vector<base::file_util_proxy::Entry>&
82 >::Type ReadDirectoryCallback;
83 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
84 const FilePath& file_path,
85 ReadDirectoryCallback* callback);
86
87 // Copies a file or a directory from |src_file_path| to |dest_file_path|
88 // Error cases:
89 // If destination file doesn't exist or destination's parent
90 // doesn't exists.
91 // If source dir exists but destination path is an existing file.
92 // If source is a parent of destination.
93 // If source doesn't exists.
94 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
95 const FilePath& src_file_path,
96 const FilePath& dest_file_path,
97 StatusCallback* callback);
98
99 // Creates directory at given path. It's an error to create
100 // if |exclusive| is true and dir already exists.
101 static bool CreateDirectory(
102 scoped_refptr<MessageLoopProxy> message_loop_proxy,
103 const FilePath& file_path,
104 bool exclusive,
kinuko@chromium.orgc483d792010-09-08 09:50:41 +0900105 bool recursive,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900106 StatusCallback* callback);
107
108 // Deletes a file or empty directory.
109 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
110 const FilePath& file_path,
111 StatusCallback* callback);
112
113 // Moves a file or a directory from src_file_path to dest_file_path.
114 // Error cases are similar to Copy method's error cases.
115 static bool Move(
116 scoped_refptr<MessageLoopProxy> message_loop_proxy,
117 const FilePath& src_file_path,
118 const FilePath& dest_file_path,
119 StatusCallback* callback);
120
121 // Deletes a directory and all of its contents.
122 static bool RecursiveDelete(
123 scoped_refptr<MessageLoopProxy> message_loop_proxy,
124 const FilePath& file_path,
125 StatusCallback* callback);
126
dumi@chromium.org23915982010-09-10 12:01:14 +0900127 // Reads from a file. On success, the file pointer is moved to position
128 // |offset + bytes_to_read| in the file. The callback can be NULL.
129 typedef Callback2<base::PlatformFileError /* error code */,
130 int /* bytes read/written */>::Type ReadWriteCallback;
131 static bool Read(
132 scoped_refptr<MessageLoopProxy> message_loop_proxy,
133 base::PlatformFile file,
134 int64 offset,
135 char* buffer,
136 int bytes_to_read,
137 ReadWriteCallback* callback);
138
139 // Writes to a file. If |offset| is greater than the length of the file,
140 // |false| is returned. On success, the file pointer is moved to position
141 // |offset + bytes_to_write| in the file. If The callback can be NULL.
142 static bool Write(
143 scoped_refptr<MessageLoopProxy> message_loop_proxy,
144 base::PlatformFile file,
145 int64 offset,
146 const char* buffer,
147 int bytes_to_write,
148 ReadWriteCallback* callback);
149
150 // Touches a file. The callback can be NULL.
151 static bool Touch(
152 scoped_refptr<MessageLoopProxy> message_loop_proxy,
153 base::PlatformFile file,
154 const base::Time& last_access_time,
155 const base::Time& last_modified_time,
156 StatusCallback* callback);
157
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900158 // Touches a file. The callback can be NULL.
159 static bool Touch(
160 scoped_refptr<MessageLoopProxy> message_loop_proxy,
161 const FilePath& file_path,
162 const base::Time& last_access_time,
163 const base::Time& last_modified_time,
164 StatusCallback* callback);
165
dumi@chromium.org23915982010-09-10 12:01:14 +0900166 // Truncates a file to the given length. If |length| is greater than the
167 // current length of the file, the file will be extended with zeroes.
168 // The callback can be NULL.
169 static bool Truncate(
170 scoped_refptr<MessageLoopProxy> message_loop_proxy,
171 base::PlatformFile file,
172 long long length,
173 StatusCallback* callback);
174
175 // Flushes a file. The callback can be NULL.
176 static bool Flush(
177 scoped_refptr<MessageLoopProxy> message_loop_proxy,
178 base::PlatformFile file,
179 StatusCallback* callback);
180
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900181 private:
182 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
183};
184
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900185} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900186
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900187#endif // BASE_FILE_UTIL_PROXY_H_