file_util_proxy.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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. 4 5#ifndef BASE_FILES_FILE_UTIL_PROXY_H_ 6#define BASE_FILES_FILE_UTIL_PROXY_H_ 7 8#include "base/base_export.h" 9#include "base/callback_forward.h" 10#include "base/files/file_path.h" 11#include "base/memory/ref_counted.h" 12#include "base/platform_file.h" 13 14namespace tracked_objects { 15class Location; 16}; 17 18namespace base { 19 20class TaskRunner; 21class Time; 22 23// This class provides asynchronous access to common file routines. 24class BASE_EXPORT FileUtilProxy { 25 public: 26 // Holds metadata for file or directory entry. 27 struct Entry { 28 FilePath::StringType name; 29 bool is_directory; 30 int64 size; 31 base::Time last_modified_time; 32 }; 33 34 // This callback is used by methods that report only an error code. It is 35 // valid to pass a null callback to any function that takes a StatusCallback, 36 // in which case the operation will complete silently. 37 typedef Callback<void(PlatformFileError)> StatusCallback; 38 39 typedef Callback<void(PlatformFileError, 40 PassPlatformFile, 41 bool /* created */)> CreateOrOpenCallback; 42 typedef Callback<void(PlatformFileError, 43 PassPlatformFile, 44 const FilePath&)> CreateTemporaryCallback; 45 typedef Callback<void(PlatformFileError, 46 const PlatformFileInfo&)> GetFileInfoCallback; 47 typedef Callback<void(PlatformFileError, 48 const char* /* data */, 49 int /* bytes read */)> ReadCallback; 50 typedef Callback<void(PlatformFileError, 51 int /* bytes written */)> WriteCallback; 52 53 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask; 54 typedef Callback<PlatformFileError(PlatformFile)> CloseTask; 55 typedef Callback<PlatformFileError(void)> FileTask; 56 57 // 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 60 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. 61 // 62 // This returns false if task posting to |task_runner| has failed. 63 static bool CreateOrOpen(TaskRunner* task_runner, 64 const FilePath& file_path, 65 int file_flags, 66 const CreateOrOpenCallback& callback); 67 68 // Creates a temporary file for writing. The path and an open file handle are 69 // returned. It is invalid to pass a null callback. The additional file flags 70 // will be added on top of the default file flags which are: 71 // base::PLATFORM_FILE_CREATE_ALWAYS 72 // base::PLATFORM_FILE_WRITE 73 // base::PLATFORM_FILE_TEMPORARY. 74 // Set |additional_file_flags| to 0 for synchronous writes and set to 75 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. 76 // 77 // This returns false if task posting to |task_runner| has failed. 78 static bool CreateTemporary( 79 TaskRunner* task_runner, 80 int additional_file_flags, 81 const CreateTemporaryCallback& callback); 82 83 // Close the given file handle. 84 // This returns false if task posting to |task_runner| has failed. 85 static bool Close(TaskRunner* task_runner, 86 PlatformFile, 87 const StatusCallback& callback); 88 89 // Retrieves the information about a file. It is invalid to pass a null 90 // callback. 91 // This returns false if task posting to |task_runner| has failed. 92 static bool GetFileInfo( 93 TaskRunner* task_runner, 94 const FilePath& file_path, 95 const GetFileInfoCallback& callback); 96 97 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath. 98 // This returns false if task posting to |task_runner| has failed. 99 static bool GetFileInfoFromPlatformFile( 100 TaskRunner* task_runner, 101 PlatformFile file, 102 const GetFileInfoCallback& callback); 103 104 // Deletes a file or a directory. 105 // It is an error to delete a non-empty directory with recursive=false. 106 // This returns false if task posting to |task_runner| has failed. 107 static bool Delete(TaskRunner* task_runner, 108 const FilePath& file_path, 109 bool recursive, 110 const StatusCallback& callback); 111 112 // Deletes a directory and all of its contents. 113 // This returns false if task posting to |task_runner| has failed. 114 static bool RecursiveDelete( 115 TaskRunner* task_runner, 116 const FilePath& file_path, 117 const StatusCallback& callback); 118 119 // Reads from a file. On success, the file pointer is moved to position 120 // |offset + bytes_to_read| in the file. The callback can be null. 121 // 122 // This returns false if |bytes_to_read| is less than zero, or 123 // if task posting to |task_runner| has failed. 124 static bool Read( 125 TaskRunner* task_runner, 126 PlatformFile file, 127 int64 offset, 128 int bytes_to_read, 129 const ReadCallback& callback); 130 131 // Writes to a file. If |offset| is greater than the length of the file, 132 // |false| is returned. On success, the file pointer is moved to position 133 // |offset + bytes_to_write| in the file. The callback can be null. 134 // |bytes_to_write| must be greater than zero. 135 // 136 // This returns false if |bytes_to_write| is less than or equal to zero, 137 // if |buffer| is NULL, or if task posting to |task_runner| has failed. 138 static bool Write( 139 TaskRunner* task_runner, 140 PlatformFile file, 141 int64 offset, 142 const char* buffer, 143 int bytes_to_write, 144 const WriteCallback& callback); 145 146 // Touches a file. The callback can be null. 147 // This returns false if task posting to |task_runner| has failed. 148 static bool Touch( 149 TaskRunner* task_runner, 150 PlatformFile file, 151 const Time& last_access_time, 152 const Time& last_modified_time, 153 const StatusCallback& callback); 154 155 // Touches a file. The callback can be null. 156 // This returns false if task posting to |task_runner| has failed. 157 static bool Touch( 158 TaskRunner* task_runner, 159 const FilePath& file_path, 160 const Time& last_access_time, 161 const Time& last_modified_time, 162 const StatusCallback& callback); 163 164 // Truncates a file to the given length. If |length| is greater than the 165 // current length of the file, the file will be extended with zeroes. 166 // The callback can be null. 167 // This returns false if task posting to |task_runner| has failed. 168 static bool Truncate( 169 TaskRunner* task_runner, 170 PlatformFile file, 171 int64 length, 172 const StatusCallback& callback); 173 174 // Truncates a file to the given length. If |length| is greater than the 175 // current length of the file, the file will be extended with zeroes. 176 // The callback can be null. 177 // This returns false if task posting to |task_runner| has failed. 178 static bool Truncate( 179 TaskRunner* task_runner, 180 const FilePath& path, 181 int64 length, 182 const StatusCallback& callback); 183 184 // Flushes a file. The callback can be null. 185 // This returns false if task posting to |task_runner| has failed. 186 static bool Flush( 187 TaskRunner* task_runner, 188 PlatformFile file, 189 const StatusCallback& callback); 190 191 // Relay helpers. 192 // They return false if posting a given task to |task_runner| has failed. 193 static bool RelayCreateOrOpen( 194 TaskRunner* task_runner, 195 const CreateOrOpenTask& open_task, 196 const CloseTask& close_task, 197 const CreateOrOpenCallback& callback); 198 static bool RelayClose( 199 TaskRunner* task_runner, 200 const CloseTask& close_task, 201 PlatformFile, 202 const StatusCallback& callback); 203 204 private: 205 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); 206}; 207 208} // namespace base 209 210#endif // BASE_FILES_FILE_UTIL_PROXY_H_ 211