blob: 4778c434c1f2ff1cb3e2ec9b6d2141fe1a2cbac2 [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
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090011#include "base/callback.h"
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090012#include "base/file_path.h"
13#include "base/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090014#include "base/memory/ref_counted.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090015#include "base/platform_file.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090016#include "base/tracked_objects.h"
17
18namespace base {
19
20class MessageLoopProxy;
dumi@chromium.org97ae2612010-09-03 11:28:37 +090021class Time;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090022
23// This class provides asynchronous access to common file routines.
darin@chromium.orge585bed2011-08-06 00:34:00 +090024class BASE_EXPORT FileUtilProxy {
dumi@chromium.orgc980e402010-08-21 07:42:50 +090025 public:
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090026 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback.
27 struct Entry {
28 FilePath::StringType name;
29 bool is_directory;
tzik@chromium.org09987c32011-07-19 14:03:03 +090030 int64 size;
31 base::Time last_modified_time;
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090032 };
33
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090034 // This callback is used by methods that report only an error code. It is
jhawkins@chromium.org162de392011-10-18 06:33:35 +090035 // valid to pass a null callback to any function that takes a StatusCallback,
36 // in which case the operation will complete silently.
37 typedef base::Callback<void(PlatformFileError /* error code */)>
38 StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090039
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090040 typedef base::Callback<void(PlatformFileError /* error code */,
41 PassPlatformFile,
42 bool /* created */)> CreateOrOpenCallback;
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090043 typedef base::Callback<void(PlatformFileError /* error code */,
44 PassPlatformFile,
45 FilePath)> CreateTemporaryCallback;
jhawkins@chromium.org70bffc12011-10-18 04:39:59 +090046 typedef base::Callback<void(PlatformFileError /* error code */,
47 bool /* created */)> EnsureFileExistsCallback;
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090048 typedef base::Callback<void(PlatformFileError /* error code */,
49 const PlatformFileInfo& /* file_info */)>
50 GetFileInfoCallback;
jhawkins@chromium.orge13cc442011-10-18 05:34:19 +090051 typedef base::Callback<void(PlatformFileError /* error code */,
52 const std::vector<Entry>&)> ReadDirectoryCallback;
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090053 typedef base::Callback<void(PlatformFileError /* error code */,
54 const char* /* data */,
55 int /* bytes read/written */)> ReadCallback;
jhawkins@chromium.org553ef502011-10-18 05:56:52 +090056 typedef base::Callback<void(PlatformFileError /* error code */,
57 int /* bytes written */)> WriteCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090058
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090059 // Creates or opens a file with the given flags. It is invalid to pass a null
60 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
61 // create a new file at the given |file_path| and calls back with
62 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. Takes
63 // ownership of |callback| and will delete it even on failure.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090064 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090065 const FilePath& file_path,
66 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090067 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090068
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090069 // Creates a temporary file for writing. The path and an open file handle are
70 // returned. It is invalid to pass a null callback. The additional file flags
71 // will be added on top of the default file flags which are:
noelutz@google.comf56dab22011-06-14 05:29:50 +090072 // base::PLATFORM_FILE_CREATE_ALWAYS
73 // base::PLATFORM_FILE_WRITE
74 // base::PLATFORM_FILE_TEMPORARY.
75 // Set |additional_file_flags| to 0 for synchronous writes and set to
76 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090077 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090078 scoped_refptr<MessageLoopProxy> message_loop_proxy,
noelutz@google.comf56dab22011-06-14 05:29:50 +090079 int additional_file_flags,
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090080 const CreateTemporaryCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090081
82 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090083 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090084 PlatformFile,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090085 const StatusCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090086
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090087 // Ensures that the given |file_path| exist. This creates a empty new file
88 // at |file_path| if the |file_path| does not exist.
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090089 // If a new file does not exist and is created at the |file_path|, |created|
90 // of the callback argument is set true and |error code| is set
91 // PLATFORM_FILE_OK. If the file already exists, |created| is set false and
92 // |error code| is set PLATFORM_FILE_OK. If the file hasn't existed but it
93 // couldn't be created for some other reasons, |created| is set false and
94 // |error code| indicates the error.
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090095 static bool EnsureFileExists(
96 scoped_refptr<MessageLoopProxy> message_loop_proxy,
97 const FilePath& file_path,
jhawkins@chromium.org70bffc12011-10-18 04:39:59 +090098 const EnsureFileExistsCallback& callback);
kinuko@chromium.org850eb6d2010-10-15 09:37:34 +090099
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900100 // Retrieves the information about a file. It is invalid to pass a null
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900101 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900102 static bool GetFileInfo(
103 scoped_refptr<MessageLoopProxy> message_loop_proxy,
104 const FilePath& file_path,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +0900105 const GetFileInfoCallback& callback);
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +0900106
dumi@chromium.org23915982010-09-10 12:01:14 +0900107 static bool GetFileInfoFromPlatformFile(
108 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900109 PlatformFile file,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +0900110 const GetFileInfoCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900111
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900112 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
113 const FilePath& file_path,
jhawkins@chromium.orge13cc442011-10-18 05:34:19 +0900114 const ReadDirectoryCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900115
erg@google.com37c078e2011-01-11 09:50:59 +0900116 // Creates directory at given path. It's an error to create
117 // if |exclusive| is true and dir already exists.
118 static bool CreateDirectory(
119 scoped_refptr<MessageLoopProxy> message_loop_proxy,
120 const FilePath& file_path,
121 bool exclusive,
122 bool recursive,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900123 const StatusCallback& callback);
erg@google.com37c078e2011-01-11 09:50:59 +0900124
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900125 // Copies a file or a directory from |src_file_path| to |dest_file_path|
126 // Error cases:
127 // If destination file doesn't exist or destination's parent
128 // doesn't exists.
129 // If source dir exists but destination path is an existing file.
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900130 // If source file exists but destination path is an existing directory.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900131 // If source is a parent of destination.
132 // If source doesn't exists.
133 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
134 const FilePath& src_file_path,
135 const FilePath& dest_file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900136 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900137
erg@google.com37c078e2011-01-11 09:50:59 +0900138 // Moves a file or a directory from src_file_path to dest_file_path.
139 // Error cases are similar to Copy method's error cases.
140 static bool Move(
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900141 scoped_refptr<MessageLoopProxy> message_loop_proxy,
erg@google.com37c078e2011-01-11 09:50:59 +0900142 const FilePath& src_file_path,
143 const FilePath& dest_file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900144 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900145
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900146 // Deletes a file or a directory.
147 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900148 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
149 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900150 bool recursive,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900151 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900152
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900153 // Deletes a directory and all of its contents.
154 static bool RecursiveDelete(
155 scoped_refptr<MessageLoopProxy> message_loop_proxy,
156 const FilePath& file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900157 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900158
dumi@chromium.org23915982010-09-10 12:01:14 +0900159 // Reads from a file. On success, the file pointer is moved to position
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900160 // |offset + bytes_to_read| in the file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900161 static bool Read(
162 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900163 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900164 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900165 int bytes_to_read,
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900166 const ReadCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900167
168 // Writes to a file. If |offset| is greater than the length of the file,
169 // |false| is returned. On success, the file pointer is moved to position
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900170 // |offset + bytes_to_write| in the file. The callback can be null.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900171 // |bytes_to_write| must be greater than zero.
dumi@chromium.org23915982010-09-10 12:01:14 +0900172 static bool Write(
173 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900174 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900175 int64 offset,
176 const char* buffer,
177 int bytes_to_write,
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900178 const WriteCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900179
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900180 // Touches a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900181 static bool Touch(
182 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900183 PlatformFile file,
184 const Time& last_access_time,
185 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900186 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900187
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900188 // Touches a file. The callback can be null.
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900189 static bool Touch(
190 scoped_refptr<MessageLoopProxy> message_loop_proxy,
191 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900192 const Time& last_access_time,
193 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900194 const StatusCallback& callback);
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900195
dumi@chromium.org23915982010-09-10 12:01:14 +0900196 // Truncates a file to the given length. If |length| is greater than the
197 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900198 // The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900199 static bool Truncate(
200 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900201 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900202 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900203 const StatusCallback& callback);
ericu@google.com6a652222010-10-05 11:26:47 +0900204
205 // Truncates a file to the given length. If |length| is greater than the
206 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900207 // The callback can be null.
ericu@google.com6a652222010-10-05 11:26:47 +0900208 static bool Truncate(
209 scoped_refptr<MessageLoopProxy> message_loop_proxy,
210 const FilePath& path,
211 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900212 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900213
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900214 // Flushes a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900215 static bool Flush(
216 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900217 PlatformFile file,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900218 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900219
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900220 private:
221 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
222};
223
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900224} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900225
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900226#endif // BASE_FILE_UTIL_PROXY_H_