blob: 56d1f772c29d327a178dcd26a6cd09781712ecdf [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
brettw@chromium.org06a553b2013-01-26 09:21:58 +09005#ifndef BASE_FILES_FILE_UTIL_PROXY_H_
6#define BASE_FILES_FILE_UTIL_PROXY_H_
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +09007
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"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090011#include "base/memory/ref_counted.h"
dumi@chromium.orgc980e402010-08-21 07:42:50 +090012#include "base/platform_file.h"
thestig@chromium.org98e70672012-04-24 06:23:04 +090013
14namespace tracked_objects {
15class Location;
16};
dumi@chromium.orgc980e402010-08-21 07:42:50 +090017
18namespace base {
19
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090020class TaskRunner;
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:
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090026 // This callback is used by methods that report only an error code. It is
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090027 // valid to pass a null callback to any function that takes a StatusCallback,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090028 // in which case the operation will complete silently.
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090029 typedef Callback<void(PlatformFileError)> StatusCallback;
dumi@chromium.orgc980e402010-08-21 07:42:50 +090030
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090031 typedef Callback<void(PlatformFileError,
32 PassPlatformFile,
33 bool /* created */)> CreateOrOpenCallback;
34 typedef Callback<void(PlatformFileError,
35 PassPlatformFile,
groby@chromium.orgdcdfe7f2012-01-12 10:59:47 +090036 const FilePath&)> CreateTemporaryCallback;
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090037 typedef Callback<void(PlatformFileError,
thestig@chromium.org98e70672012-04-24 06:23:04 +090038 const PlatformFileInfo&)> GetFileInfoCallback;
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090039 typedef Callback<void(PlatformFileError,
40 const char* /* data */,
41 int /* bytes read */)> ReadCallback;
42 typedef Callback<void(PlatformFileError,
43 int /* bytes written */)> WriteCallback;
erg@google.comd5fffd42011-01-08 03:06:45 +090044
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090045 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask;
46 typedef Callback<PlatformFileError(PlatformFile)> CloseTask;
kinuko@chromium.org9c033e22011-11-09 15:46:39 +090047 typedef Callback<PlatformFileError(void)> FileTask;
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +090048
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090049 // Creates or opens a file with the given flags. It is invalid to pass a null
50 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
51 // create a new file at the given |file_path| and calls back with
kinuko@chromium.org4f65c992011-10-19 16:21:57 +090052 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090053 //
54 // This returns false if task posting to |task_runner| has failed.
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090055 static bool CreateOrOpen(TaskRunner* task_runner,
dumi@chromium.orgc980e402010-08-21 07:42:50 +090056 const FilePath& file_path,
57 int file_flags,
jhawkins@chromium.org3a6573d2011-10-18 03:40:30 +090058 const CreateOrOpenCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090059
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090060 // Creates a temporary file for writing. The path and an open file handle are
61 // returned. It is invalid to pass a null callback. The additional file flags
62 // will be added on top of the default file flags which are:
noelutz@google.comf56dab22011-06-14 05:29:50 +090063 // base::PLATFORM_FILE_CREATE_ALWAYS
64 // base::PLATFORM_FILE_WRITE
65 // base::PLATFORM_FILE_TEMPORARY.
66 // Set |additional_file_flags| to 0 for synchronous writes and set to
67 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090068 //
69 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org6dedd6d2010-08-25 05:26:23 +090070 static bool CreateTemporary(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090071 TaskRunner* task_runner,
noelutz@google.comf56dab22011-06-14 05:29:50 +090072 int additional_file_flags,
jhawkins@chromium.orgd90c4f52011-10-18 04:29:29 +090073 const CreateTemporaryCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090074
75 // Close the given file handle.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090076 // This returns false if task posting to |task_runner| has failed.
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090077 static bool Close(TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +090078 PlatformFile,
jhawkins@chromium.org162de392011-10-18 06:33:35 +090079 const StatusCallback& callback);
dumi@chromium.orgc980e402010-08-21 07:42:50 +090080
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +090081 // Retrieves the information about a file. It is invalid to pass a null
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090082 // callback.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090083 // This returns false if task posting to |task_runner| has failed.
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090084 static bool GetFileInfo(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090085 TaskRunner* task_runner,
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090086 const FilePath& file_path,
jhawkins@chromium.org56771f02011-10-18 05:12:05 +090087 const GetFileInfoCallback& callback);
jianli@chromium.org9ed1f9d2010-08-31 11:42:36 +090088
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090089 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath.
90 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +090091 static bool GetFileInfoFromPlatformFile(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +090092 TaskRunner* task_runner,
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.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +090098 // This returns false if task posting to |task_runner| has failed.
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.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900105 // This returns false if task posting to |task_runner| has failed.
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900106 static bool RecursiveDelete(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900107 TaskRunner* task_runner,
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900108 const FilePath& file_path,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900109 const StatusCallback& callback);
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900110
dumi@chromium.org23915982010-09-10 12:01:14 +0900111 // Reads from a file. On success, the file pointer is moved to position
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900112 // |offset + bytes_to_read| in the file. The callback can be null.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900113 //
114 // This returns false if |bytes_to_read| is less than zero, or
115 // if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +0900116 static bool Read(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900117 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900118 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900119 int64 offset,
dumi@chromium.org23915982010-09-10 12:01:14 +0900120 int bytes_to_read,
jhawkins@chromium.orgd08a33e2011-10-18 05:44:47 +0900121 const ReadCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900122
123 // Writes to a file. If |offset| is greater than the length of the file,
124 // |false| is returned. On success, the file pointer is moved to position
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900125 // |offset + bytes_to_write| in the file. The callback can be null.
sanga@chromium.org21d251f2011-08-18 01:45:48 +0900126 // |bytes_to_write| must be greater than zero.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900127 //
128 // This returns false if |bytes_to_write| is less than or equal to zero,
129 // if |buffer| is NULL, or if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +0900130 static bool Write(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900131 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900132 PlatformFile file,
dumi@chromium.org23915982010-09-10 12:01:14 +0900133 int64 offset,
134 const char* buffer,
135 int bytes_to_write,
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900136 const WriteCallback& 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.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900139 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +0900140 static bool Touch(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900141 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900142 PlatformFile file,
143 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.org23915982010-09-10 12:01:14 +0900146
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900147 // Touches a file. The callback can be null.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900148 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900149 static bool Touch(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900150 TaskRunner* task_runner,
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900151 const FilePath& file_path,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900152 const Time& last_access_time,
153 const Time& last_modified_time,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900154 const StatusCallback& callback);
dumi@chromium.org12c2e2b2010-09-24 10:09:32 +0900155
dumi@chromium.org23915982010-09-10 12:01:14 +0900156 // 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.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900159 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +0900160 static bool Truncate(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900161 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900162 PlatformFile file,
ericu@google.com6a652222010-10-05 11:26:47 +0900163 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900164 const StatusCallback& callback);
ericu@google.com6a652222010-10-05 11:26:47 +0900165
166 // 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.
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900168 // The callback can be null.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900169 // This returns false if task posting to |task_runner| has failed.
ericu@google.com6a652222010-10-05 11:26:47 +0900170 static bool Truncate(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900171 TaskRunner* task_runner,
ericu@google.com6a652222010-10-05 11:26:47 +0900172 const FilePath& path,
173 int64 length,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900174 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900175
jhawkins@chromium.org553ef502011-10-18 05:56:52 +0900176 // Flushes a file. The callback can be null.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900177 // This returns false if task posting to |task_runner| has failed.
dumi@chromium.org23915982010-09-10 12:01:14 +0900178 static bool Flush(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900179 TaskRunner* task_runner,
kkanetkar@chromium.org48710682010-11-03 05:36:52 +0900180 PlatformFile file,
jhawkins@chromium.org162de392011-10-18 06:33:35 +0900181 const StatusCallback& callback);
dumi@chromium.org23915982010-09-10 12:01:14 +0900182
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900183 // Relay helpers.
kinuko@chromium.org6cce7092013-01-21 13:26:59 +0900184 // They return false if posting a given task to |task_runner| has failed.
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900185 static bool RelayCreateOrOpen(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900186 TaskRunner* task_runner,
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900187 const CreateOrOpenTask& open_task,
188 const CloseTask& close_task,
189 const CreateOrOpenCallback& callback);
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900190 static bool RelayClose(
kinuko@chromium.org20e2efc2012-04-24 03:40:57 +0900191 TaskRunner* task_runner,
kinuko@chromium.org42e9d8c2011-11-08 18:04:16 +0900192 const CloseTask& close_task,
193 PlatformFile,
194 const StatusCallback& callback);
195
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900196 private:
197 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
198};
199
kkanetkar@chromium.org3a3bdea2010-09-02 12:43:36 +0900200} // namespace base
dumi@chromium.orgc980e402010-08-21 07:42:50 +0900201
brettw@chromium.org06a553b2013-01-26 09:21:58 +0900202#endif // BASE_FILES_FILE_UTIL_PROXY_H_