blob: 7df51451f20c982592db81e4a0ea9109c6c69da3 [file] [log] [blame]
groby@chromium.orgdcdfe7f2012-01-12 10:59:47 +09001// Copyright (c) 2012 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
darin@chromium.orge585bed2011-08-06 00:34:00 +09008#include "base/base_export.h"
thestig@chromium.org98e70672012-04-24 06:23:04 +09009#include "base/callback_forward.h"
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +090010#include "base/file_path.h"
11#include "base/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090012#include "base/memory/ref_counted.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090013#include "base/platform_file.h"
thestig@chromium.org98e70672012-04-24 06:23:04 +090014
15namespace tracked_objects {
16class Location;
17};
dumi@chromium.orgc980e402010-08-21 07:42:50 +090018
19namespace base {
20
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090021class TaskRunner;
dumi@chromium.org97ae2612010-09-03 11:28:37 +090022class Time;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090023
24// This class provides asynchronous access to common file routines.
darin@chromium.orge585bed2011-08-06 00:34:00 +090025class BASE_EXPORT FileUtilProxy {
dumi@chromium.orgc980e402010-08-21 07:42:50 +090026 public:
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090027 // Holds metadata for file or directory entry.
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090028 struct Entry {
29 FilePath::StringType name;
30 bool is_directory;
tzik@chromium.org09987c32011-07-19 14:03:03 +090031 int64 size;
32 base::Time last_modified_time;
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090033 };
34
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090035 // This callback is used by methods that report only an error code. It is
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090036 // valid to pass a null callback to any function that takes a StatusCallback,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090037 // in which case the operation will complete silently.
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090038 typedef Callback<void(PlatformFileError)> StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090039
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090040 typedef Callback<void(PlatformFileError,
41 PassPlatformFile,
42 bool /* created */)> CreateOrOpenCallback;
43 typedef Callback<void(PlatformFileError,
44 PassPlatformFile,
groby@chromium.orgdcdfe7f2012-01-12 10:59:47 +090045 const FilePath&)> CreateTemporaryCallback;
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090046 typedef Callback<void(PlatformFileError,
thestig@chromium.org98e70672012-04-24 06:23:04 +090047 const PlatformFileInfo&)> GetFileInfoCallback;
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090048 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;
kinuko@chromium.org9c033e22011-11-09 15:46:39 +090056 typedef Callback<PlatformFileError(void)> FileTask;
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090057
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090058 // Creates or opens a file with the given flags. It is invalid to pass a null
59 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
60 // create a new file at the given |file_path| and calls back with
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090061 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090062 static bool CreateOrOpen(TaskRunner* task_runner,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090063 const FilePath& file_path,
64 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090065 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090066
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090067 // Creates a temporary file for writing. The path and an open file handle are
68 // returned. It is invalid to pass a null callback. The additional file flags
69 // will be added on top of the default file flags which are:
noelutz@google.comf56dab22011-06-14 05:29:50 +090070 // base::PLATFORM_FILE_CREATE_ALWAYS
71 // base::PLATFORM_FILE_WRITE
72 // base::PLATFORM_FILE_TEMPORARY.
73 // Set |additional_file_flags| to 0 for synchronous writes and set to
74 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090075 static bool CreateTemporary(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090076 TaskRunner* task_runner,
noelutz@google.comf56dab22011-06-14 05:29:50 +090077 int additional_file_flags,
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090078 const CreateTemporaryCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090079
80 // Close the given file handle.
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090081 static bool Close(TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090082 PlatformFile,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090083 const StatusCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090084
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090085 // Retrieves the information about a file. It is invalid to pass a null
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090086 // callback.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090087 static bool GetFileInfo(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090088 TaskRunner* task_runner,
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090089 const FilePath& file_path,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090090 const GetFileInfoCallback& callback);
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090091
dumi@chromium.org23915982010-09-10 12:01:14 +090092 static bool GetFileInfoFromPlatformFile(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090093 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090094 PlatformFile file,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090095 const GetFileInfoCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +090096
kinuko@chromium.org1a078042010-10-07 17:35:09 +090097 // Deletes a file or a directory.
98 // It is an error to delete a non-empty directory with recursive=false.
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090099 static bool Delete(TaskRunner* task_runner,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900100 const FilePath& file_path,
kinuko@chromium.org1a078042010-10-07 17:35:09 +0900101 bool recursive,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900102 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900103
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900104 // Deletes a directory and all of its contents.
105 static bool RecursiveDelete(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900106 TaskRunner* task_runner,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900107 const FilePath& file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900108 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900109
dumi@chromium.org23915982010-09-10 12:01:14 +0900110 // Reads from a file. On success, the file pointer is moved to position
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900111 // |offset + bytes_to_read| in the file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900112 static bool Read(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900113 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900114 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900115 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900116 int bytes_to_read,
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900117 const ReadCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900118
119 // Writes to a file. If |offset| is greater than the length of the file,
120 // |false| is returned. On success, the file pointer is moved to position
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900121 // |offset + bytes_to_write| in the file. The callback can be null.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900122 // |bytes_to_write| must be greater than zero.
dumi@chromium.org23915982010-09-10 12:01:14 +0900123 static bool Write(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900124 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900125 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900126 int64 offset,
127 const char* buffer,
128 int bytes_to_write,
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900129 const WriteCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900130
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900131 // Touches a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900132 static bool Touch(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900133 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900134 PlatformFile file,
135 const Time& last_access_time,
136 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900137 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900138
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900139 // Touches a file. The callback can be null.
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900140 static bool Touch(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900141 TaskRunner* task_runner,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900142 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900143 const Time& last_access_time,
144 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900145 const StatusCallback& callback);
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900146
dumi@chromium.org23915982010-09-10 12:01:14 +0900147 // Truncates a file to the given length. If |length| is greater than the
148 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900149 // The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900150 static bool Truncate(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900151 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900152 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900153 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900154 const StatusCallback& callback);
ericu@google.com6a652222010-10-05 11:26:47 +0900155
156 // Truncates a file to the given length. If |length| is greater than the
157 // current length of the file, the file will be extended with zeroes.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900158 // The callback can be null.
ericu@google.com6a652222010-10-05 11:26:47 +0900159 static bool Truncate(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900160 TaskRunner* task_runner,
ericu@google.com6a652222010-10-05 11:26:47 +0900161 const FilePath& path,
162 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900163 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900164
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900165 // Flushes a file. The callback can be null.
dumi@chromium.org23915982010-09-10 12:01:14 +0900166 static bool Flush(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900167 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900168 PlatformFile file,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900169 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900170
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900171 // Relay helpers.
kinuko@chromium.org9c033e22011-11-09 15:46:39 +0900172 static bool RelayFileTask(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900173 TaskRunner* task_runner,
kinuko@chromium.org9c033e22011-11-09 15:46:39 +0900174 const tracked_objects::Location& from_here,
175 const FileTask& task,
176 const StatusCallback& callback);
177
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900178 static bool RelayCreateOrOpen(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900179 TaskRunner* task_runner,
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900180 const CreateOrOpenTask& open_task,
181 const CloseTask& close_task,
182 const CreateOrOpenCallback& callback);
183
184 static bool RelayClose(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900185 TaskRunner* task_runner,
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900186 const CloseTask& close_task,
187 PlatformFile,
188 const StatusCallback& callback);
189
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900190 private:
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
192};
193
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900194} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900195
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900196#endif // BASE_FILE_UTIL_PROXY_H_