blob: 15a3e82f9946c252d4cc52ddbcc7494354516337 [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:
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090026 // Holds metadata for file or directory entry.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090027 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
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090035 // valid to pass a null callback to any function that takes a StatusCallback,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090036 // in which case the operation will complete silently.
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090037 typedef Callback<void(PlatformFileError)> StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090038
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090039 typedef Callback<void(PlatformFileError,
40 PassPlatformFile,
41 bool /* created */)> CreateOrOpenCallback;
42 typedef Callback<void(PlatformFileError,
43 PassPlatformFile,
44 FilePath)> CreateTemporaryCallback;
45 typedef Callback<void(PlatformFileError,
46 const PlatformFileInfo&
47 )> GetFileInfoCallback;
48 typedef Callback<void(PlatformFileError,
49 const char* /* data */,
50 int /* bytes read */)> ReadCallback;
51 typedef Callback<void(PlatformFileError,
52 int /* bytes written */)> WriteCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090053
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090054 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask;
55 typedef Callback<PlatformFileError(PlatformFile)> CloseTask;
56
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090057 // Creates or opens a file with the given flags. It is invalid to pass a null
58 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
59 // create a new file at the given |file_path| and calls back with
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090060 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090061 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090062 const FilePath& file_path,
63 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090064 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090065
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090066 // Creates a temporary file for writing. The path and an open file handle are
67 // returned. It is invalid to pass a null callback. The additional file flags
68 // will be added on top of the default file flags which are:
noelutz@google.comf56dab22011-06-14 05:29:50 +090069 // base::PLATFORM_FILE_CREATE_ALWAYS
70 // base::PLATFORM_FILE_WRITE
71 // base::PLATFORM_FILE_TEMPORARY.
72 // Set |additional_file_flags| to 0 for synchronous writes and set to
73 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090074 static bool CreateTemporary(
dumi@chromium.orgc980e402010-08-21 07:42:50 +090075 scoped_refptr<MessageLoopProxy> message_loop_proxy,
noelutz@google.comf56dab22011-06-14 05:29:50 +090076 int additional_file_flags,
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090077 const CreateTemporaryCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090078
79 // Close the given file handle.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090080 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090081 PlatformFile,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090082 const StatusCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090083
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090084 // Retrieves the information about a file. It is invalid to pass a null
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090085 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090086 static bool GetFileInfo(
87 scoped_refptr<MessageLoopProxy> message_loop_proxy,
88 const FilePath& file_path,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090089 const GetFileInfoCallback& callback);
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090090
dumi@chromium.org23915982010-09-10 12:01:14 +090091 static bool GetFileInfoFromPlatformFile(
92 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090093 PlatformFile file,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090094 const GetFileInfoCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +090095
kinuko@chromium.org1a078042010-10-07 17:35:09 +090096 // Deletes a file or a directory.
97 // It is an error to delete a non-empty directory with recursive=false.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090098 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
99 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900100 bool recursive,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900101 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900102
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900103 // Deletes a directory and all of its contents.
104 static bool RecursiveDelete(
105 scoped_refptr<MessageLoopProxy> message_loop_proxy,
106 const FilePath& file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900107 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900108
dumi@chromium.org23915982010-09-10 12:01:14 +0900109 // Reads from a file. On success, the file pointer is moved to position
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900110 // |offset + bytes_to_read| in the file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900111 static bool Read(
112 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900113 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900114 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900115 int bytes_to_read,
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900116 const ReadCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900117
118 // Writes to a file. If |offset| is greater than the length of the file,
119 // |false| is returned. On success, the file pointer is moved to position
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900120 // |offset + bytes_to_write| in the file. The callback can be null.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900121 // |bytes_to_write| must be greater than zero.
dumi@chromium.org23915982010-09-10 12:01:14 +0900122 static bool Write(
123 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900124 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900125 int64 offset,
126 const char* buffer,
127 int bytes_to_write,
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900128 const WriteCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900129
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900130 // Touches a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900131 static bool Touch(
132 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900133 PlatformFile file,
134 const Time& last_access_time,
135 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900136 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900137
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900138 // Touches a file. The callback can be null.
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900139 static bool Touch(
140 scoped_refptr<MessageLoopProxy> message_loop_proxy,
141 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900142 const Time& last_access_time,
143 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900144 const StatusCallback& callback);
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900145
dumi@chromium.org23915982010-09-10 12:01:14 +0900146 // Truncates a file to the given length. If |length| is greater than the
147 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900148 // The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900149 static bool Truncate(
150 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900151 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900152 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900153 const StatusCallback& callback);
ericu@google.com6a652222010-10-05 11:26:47 +0900154
155 // Truncates a file to the given length. If |length| is greater than the
156 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900157 // The callback can be null.
ericu@google.com6a652222010-10-05 11:26:47 +0900158 static bool Truncate(
159 scoped_refptr<MessageLoopProxy> message_loop_proxy,
160 const FilePath& path,
161 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900162 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900163
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900164 // Flushes a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900165 static bool Flush(
166 scoped_refptr<MessageLoopProxy> message_loop_proxy,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900167 PlatformFile file,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900168 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900169
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900170 // Relay helpers.
171 static bool RelayCreateOrOpen(
172 scoped_refptr<MessageLoopProxy> message_loop_proxy,
173 const CreateOrOpenTask& open_task,
174 const CloseTask& close_task,
175 const CreateOrOpenCallback& callback);
176
177 static bool RelayClose(
178 scoped_refptr<MessageLoopProxy> message_loop_proxy,
179 const CloseTask& close_task,
180 PlatformFile,
181 const StatusCallback& callback);
182
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900183 private:
184 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
185};
186
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900187} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900188
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900189#endif // BASE_FILE_UTIL_PROXY_H_