1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_FILE_UTIL_PROXY_H_
6 #define BASE_FILE_UTIL_PROXY_H_
8 #include "base/base_export.h"
9 #include "base/callback_forward.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/platform_file.h"
15 namespace tracked_objects
{
24 // This class provides asynchronous access to common file routines.
25 class BASE_EXPORT FileUtilProxy
{
27 // Holds metadata for file or directory entry.
29 FilePath::StringType name
;
32 base::Time last_modified_time
;
35 // This callback is used by methods that report only an error code. It is
36 // valid to pass a null callback to any function that takes a StatusCallback,
37 // in which case the operation will complete silently.
38 typedef Callback
<void(PlatformFileError
)> StatusCallback
;
40 typedef Callback
<void(PlatformFileError
,
42 bool /* created */)> CreateOrOpenCallback
;
43 typedef Callback
<void(PlatformFileError
,
45 const FilePath
&)> CreateTemporaryCallback
;
46 typedef Callback
<void(PlatformFileError
,
47 const PlatformFileInfo
&)> 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
;
54 typedef Callback
<PlatformFileError(PlatformFile
*, bool*)> CreateOrOpenTask
;
55 typedef Callback
<PlatformFileError(PlatformFile
)> CloseTask
;
56 typedef Callback
<PlatformFileError(void)> FileTask
;
58 // 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
61 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
62 static bool CreateOrOpen(TaskRunner
* task_runner
,
63 const FilePath
& file_path
,
65 const CreateOrOpenCallback
& callback
);
67 // 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:
70 // 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.
75 static bool CreateTemporary(
76 TaskRunner
* task_runner
,
77 int additional_file_flags
,
78 const CreateTemporaryCallback
& callback
);
80 // Close the given file handle.
81 static bool Close(TaskRunner
* task_runner
,
83 const StatusCallback
& callback
);
85 // Retrieves the information about a file. It is invalid to pass a null
87 static bool GetFileInfo(
88 TaskRunner
* task_runner
,
89 const FilePath
& file_path
,
90 const GetFileInfoCallback
& callback
);
92 static bool GetFileInfoFromPlatformFile(
93 TaskRunner
* task_runner
,
95 const GetFileInfoCallback
& callback
);
97 // Deletes a file or a directory.
98 // It is an error to delete a non-empty directory with recursive=false.
99 static bool Delete(TaskRunner
* task_runner
,
100 const FilePath
& file_path
,
102 const StatusCallback
& callback
);
104 // Deletes a directory and all of its contents.
105 static bool RecursiveDelete(
106 TaskRunner
* task_runner
,
107 const FilePath
& file_path
,
108 const StatusCallback
& callback
);
110 // Reads from a file. On success, the file pointer is moved to position
111 // |offset + bytes_to_read| in the file. The callback can be null.
113 TaskRunner
* task_runner
,
117 const ReadCallback
& callback
);
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
121 // |offset + bytes_to_write| in the file. The callback can be null.
122 // |bytes_to_write| must be greater than zero.
124 TaskRunner
* task_runner
,
129 const WriteCallback
& callback
);
131 // Touches a file. The callback can be null.
133 TaskRunner
* task_runner
,
135 const Time
& last_access_time
,
136 const Time
& last_modified_time
,
137 const StatusCallback
& callback
);
139 // Touches a file. The callback can be null.
141 TaskRunner
* task_runner
,
142 const FilePath
& file_path
,
143 const Time
& last_access_time
,
144 const Time
& last_modified_time
,
145 const StatusCallback
& callback
);
147 // 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.
149 // The callback can be null.
150 static bool Truncate(
151 TaskRunner
* task_runner
,
154 const StatusCallback
& callback
);
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.
158 // The callback can be null.
159 static bool Truncate(
160 TaskRunner
* task_runner
,
161 const FilePath
& path
,
163 const StatusCallback
& callback
);
165 // Flushes a file. The callback can be null.
167 TaskRunner
* task_runner
,
169 const StatusCallback
& callback
);
172 static bool RelayFileTask(
173 TaskRunner
* task_runner
,
174 const tracked_objects::Location
& from_here
,
175 const FileTask
& task
,
176 const StatusCallback
& callback
);
178 static bool RelayCreateOrOpen(
179 TaskRunner
* task_runner
,
180 const CreateOrOpenTask
& open_task
,
181 const CloseTask
& close_task
,
182 const CreateOrOpenCallback
& callback
);
184 static bool RelayClose(
185 TaskRunner
* task_runner
,
186 const CloseTask
& close_task
,
188 const StatusCallback
& callback
);
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy
);
196 #endif // BASE_FILE_UTIL_PROXY_H_