blob: 0b2cae5f770a1d3ba187e3a8db4bb6d301a4e2d6 [file] [log] [blame]
[email protected]f5ae4932012-01-12 01:59:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b90c93ff2010-08-20 22:42:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]61da1db2010-09-02 03:43:365#ifndef BASE_FILE_UTIL_PROXY_H_
6#define BASE_FILE_UTIL_PROXY_H_
7
8#include <vector>
[email protected]b90c93ff2010-08-20 22:42:509
[email protected]0bea7252011-08-05 15:34:0010#include "base/base_export.h"
[email protected]30c1eea2011-10-17 18:40:3011#include "base/callback.h"
[email protected]61da1db2010-09-02 03:43:3612#include "base/file_path.h"
13#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]b90c93ff2010-08-20 22:42:5015#include "base/platform_file.h"
[email protected]b90c93ff2010-08-20 22:42:5016#include "base/tracked_objects.h"
17
18namespace base {
19
20class MessageLoopProxy;
[email protected]2f0193c22010-09-03 02:28:3721class Time;
[email protected]b90c93ff2010-08-20 22:42:5022
23// This class provides asynchronous access to common file routines.
[email protected]0bea7252011-08-05 15:34:0024class BASE_EXPORT FileUtilProxy {
[email protected]b90c93ff2010-08-20 22:42:5025 public:
[email protected]0a3bd7e2011-10-19 07:21:5726 // Holds metadata for file or directory entry.
[email protected]841daf1d2010-11-02 20:36:5227 struct Entry {
28 FilePath::StringType name;
29 bool is_directory;
[email protected]f1ddaa42011-07-19 05:03:0330 int64 size;
31 base::Time last_modified_time;
[email protected]841daf1d2010-11-02 20:36:5232 };
33
[email protected]d480a3e2010-08-24 20:26:2334 // This callback is used by methods that report only an error code. It is
[email protected]01ce1052011-11-08 09:04:1635 // valid to pass a null callback to any function that takes a StatusCallback,
[email protected]aa0b33e2011-10-17 21:33:3536 // in which case the operation will complete silently.
[email protected]0a3bd7e2011-10-19 07:21:5737 typedef Callback<void(PlatformFileError)> StatusCallback;
[email protected]b90c93ff2010-08-20 22:42:5038
[email protected]0a3bd7e2011-10-19 07:21:5739 typedef Callback<void(PlatformFileError,
40 PassPlatformFile,
41 bool /* created */)> CreateOrOpenCallback;
42 typedef Callback<void(PlatformFileError,
43 PassPlatformFile,
[email protected]f5ae4932012-01-12 01:59:4744 const FilePath&)> CreateTemporaryCallback;
[email protected]0a3bd7e2011-10-19 07:21:5745 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;
[email protected]a502bbe72011-01-07 18:06:4553
[email protected]01ce1052011-11-08 09:04:1654 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask;
55 typedef Callback<PlatformFileError(PlatformFile)> CloseTask;
[email protected]4412cd02011-11-09 06:46:3956 typedef Callback<PlatformFileError(void)> FileTask;
[email protected]01ce1052011-11-08 09:04:1657
[email protected]d6c182212011-10-17 20:44:4758 // 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
[email protected]0a3bd7e2011-10-19 07:21:5761 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
[email protected]d480a3e2010-08-24 20:26:2362 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]b90c93ff2010-08-20 22:42:5063 const FilePath& file_path,
64 int file_flags,
[email protected]30c1eea2011-10-17 18:40:3065 const CreateOrOpenCallback& callback);
[email protected]b90c93ff2010-08-20 22:42:5066
[email protected]d6c182212011-10-17 20:44:4767 // 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:
[email protected]86ecf512011-06-13 20:29:5070 // 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.
[email protected]d480a3e2010-08-24 20:26:2375 static bool CreateTemporary(
[email protected]b90c93ff2010-08-20 22:42:5076 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]86ecf512011-06-13 20:29:5077 int additional_file_flags,
[email protected]7bf4a8e2011-10-17 19:29:2978 const CreateTemporaryCallback& callback);
[email protected]b90c93ff2010-08-20 22:42:5079
80 // Close the given file handle.
[email protected]d480a3e2010-08-24 20:26:2381 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:5282 PlatformFile,
[email protected]aa0b33e2011-10-17 21:33:3583 const StatusCallback& callback);
[email protected]b90c93ff2010-08-20 22:42:5084
[email protected]d6c182212011-10-17 20:44:4785 // Retrieves the information about a file. It is invalid to pass a null
[email protected]81070042010-08-31 02:42:3686 // callback.
[email protected]81070042010-08-31 02:42:3687 static bool GetFileInfo(
88 scoped_refptr<MessageLoopProxy> message_loop_proxy,
89 const FilePath& file_path,
[email protected]0f695a72011-10-17 20:12:0590 const GetFileInfoCallback& callback);
[email protected]81070042010-08-31 02:42:3691
[email protected]3fb43ed12010-09-10 03:01:1492 static bool GetFileInfoFromPlatformFile(
93 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:5294 PlatformFile file,
[email protected]0f695a72011-10-17 20:12:0595 const GetFileInfoCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:1496
[email protected]81b18ca2010-10-07 08:35:0997 // Deletes a file or a directory.
98 // It is an error to delete a non-empty directory with recursive=false.
[email protected]61da1db2010-09-02 03:43:3699 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
100 const FilePath& file_path,
[email protected]81b18ca2010-10-07 08:35:09101 bool recursive,
[email protected]aa0b33e2011-10-17 21:33:35102 const StatusCallback& callback);
[email protected]61da1db2010-09-02 03:43:36103
[email protected]61da1db2010-09-02 03:43:36104 // Deletes a directory and all of its contents.
105 static bool RecursiveDelete(
106 scoped_refptr<MessageLoopProxy> message_loop_proxy,
107 const FilePath& file_path,
[email protected]aa0b33e2011-10-17 21:33:35108 const StatusCallback& callback);
[email protected]61da1db2010-09-02 03:43:36109
[email protected]3fb43ed12010-09-10 03:01:14110 // Reads from a file. On success, the file pointer is moved to position
[email protected]d6c182212011-10-17 20:44:47111 // |offset + bytes_to_read| in the file. The callback can be null.
[email protected]3fb43ed12010-09-10 03:01:14112 static bool Read(
113 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:52114 PlatformFile file,
[email protected]3fb43ed12010-09-10 03:01:14115 int64 offset,
[email protected]3fb43ed12010-09-10 03:01:14116 int bytes_to_read,
[email protected]d6c182212011-10-17 20:44:47117 const ReadCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:14118
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
[email protected]4958f9f2011-10-17 20:56:52121 // |offset + bytes_to_write| in the file. The callback can be null.
[email protected]a88016d12011-08-17 16:45:48122 // |bytes_to_write| must be greater than zero.
[email protected]3fb43ed12010-09-10 03:01:14123 static bool Write(
124 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:52125 PlatformFile file,
[email protected]3fb43ed12010-09-10 03:01:14126 int64 offset,
127 const char* buffer,
128 int bytes_to_write,
[email protected]4958f9f2011-10-17 20:56:52129 const WriteCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:14130
[email protected]4958f9f2011-10-17 20:56:52131 // Touches a file. The callback can be null.
[email protected]3fb43ed12010-09-10 03:01:14132 static bool Touch(
133 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:52134 PlatformFile file,
135 const Time& last_access_time,
136 const Time& last_modified_time,
[email protected]aa0b33e2011-10-17 21:33:35137 const StatusCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:14138
[email protected]4958f9f2011-10-17 20:56:52139 // Touches a file. The callback can be null.
[email protected]9fae63af2010-09-24 01:09:32140 static bool Touch(
141 scoped_refptr<MessageLoopProxy> message_loop_proxy,
142 const FilePath& file_path,
[email protected]841daf1d2010-11-02 20:36:52143 const Time& last_access_time,
144 const Time& last_modified_time,
[email protected]aa0b33e2011-10-17 21:33:35145 const StatusCallback& callback);
[email protected]9fae63af2010-09-24 01:09:32146
[email protected]3fb43ed12010-09-10 03:01:14147 // 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.
[email protected]4958f9f2011-10-17 20:56:52149 // The callback can be null.
[email protected]3fb43ed12010-09-10 03:01:14150 static bool Truncate(
151 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:52152 PlatformFile file,
[email protected]7eb68a22010-10-05 02:26:47153 int64 length,
[email protected]aa0b33e2011-10-17 21:33:35154 const StatusCallback& callback);
[email protected]7eb68a22010-10-05 02:26:47155
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.
[email protected]4958f9f2011-10-17 20:56:52158 // The callback can be null.
[email protected]7eb68a22010-10-05 02:26:47159 static bool Truncate(
160 scoped_refptr<MessageLoopProxy> message_loop_proxy,
161 const FilePath& path,
162 int64 length,
[email protected]aa0b33e2011-10-17 21:33:35163 const StatusCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:14164
[email protected]4958f9f2011-10-17 20:56:52165 // Flushes a file. The callback can be null.
[email protected]3fb43ed12010-09-10 03:01:14166 static bool Flush(
167 scoped_refptr<MessageLoopProxy> message_loop_proxy,
[email protected]841daf1d2010-11-02 20:36:52168 PlatformFile file,
[email protected]aa0b33e2011-10-17 21:33:35169 const StatusCallback& callback);
[email protected]3fb43ed12010-09-10 03:01:14170
[email protected]01ce1052011-11-08 09:04:16171 // Relay helpers.
[email protected]4412cd02011-11-09 06:46:39172 static bool RelayFileTask(
173 scoped_refptr<MessageLoopProxy> message_loop_proxy,
174 const tracked_objects::Location& from_here,
175 const FileTask& task,
176 const StatusCallback& callback);
177
[email protected]01ce1052011-11-08 09:04:16178 static bool RelayCreateOrOpen(
179 scoped_refptr<MessageLoopProxy> message_loop_proxy,
180 const CreateOrOpenTask& open_task,
181 const CloseTask& close_task,
182 const CreateOrOpenCallback& callback);
183
184 static bool RelayClose(
185 scoped_refptr<MessageLoopProxy> message_loop_proxy,
186 const CloseTask& close_task,
187 PlatformFile,
188 const StatusCallback& callback);
189
[email protected]b90c93ff2010-08-20 22:42:50190 private:
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
192};
193
[email protected]61da1db2010-09-02 03:43:36194} // namespace base
[email protected]b90c93ff2010-08-20 22:42:50195
[email protected]61da1db2010-09-02 03:43:36196#endif // BASE_FILE_UTIL_PROXY_H_