file_util_proxy.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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_FILE_UTIL_PROXY_H_ 6#define BASE_FILE_UTIL_PROXY_H_ 7 8#include <vector> 9 10#include "base/callback.h" 11#include "base/file_path.h" 12#include "base/file_util.h" 13#include "base/platform_file.h" 14#include "base/ref_counted.h" 15#include "base/tracked_objects.h" 16 17namespace base { 18 19class MessageLoopProxy; 20class Time; 21 22// This class provides asynchronous access to common file routines. 23class FileUtilProxy { 24 public: 25 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback. 26 struct Entry { 27 FilePath::StringType name; 28 bool is_directory; 29 }; 30 31 // This callback is used by methods that report only an error code. It is 32 // valid to pass NULL as the callback parameter to any function that takes a 33 // StatusCallback, in which case the operation will complete silently. 34 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback; 35 36 // Creates or opens a file with the given flags. It is invalid to pass NULL 37 // for the callback. 38 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create 39 // a new file at the given |file_path| and calls back with 40 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. 41 typedef Callback3<PlatformFileError /* error code */, 42 PassPlatformFile, 43 bool /* created */>::Type CreateOrOpenCallback; 44 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, 45 const FilePath& file_path, 46 int file_flags, 47 CreateOrOpenCallback* callback); 48 49 // Creates a temporary file for writing. The path and an open file handle 50 // are returned. It is invalid to pass NULL for the callback. 51 typedef Callback3<PlatformFileError /* error code */, 52 PassPlatformFile, 53 FilePath>::Type CreateTemporaryCallback; 54 static bool CreateTemporary( 55 scoped_refptr<MessageLoopProxy> message_loop_proxy, 56 CreateTemporaryCallback* callback); 57 58 // Close the given file handle. 59 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, 60 PlatformFile, 61 StatusCallback* callback); 62 63 // Ensures that the given |file_path| exist. This creates a empty new file 64 // at |file_path| if the |file_path| does not exist. 65 // If a new file han not existed and is created at the |file_path|, 66 // |created| of the callback argument is set true and |error code| 67 // is set PLATFORM_FILE_OK. 68 // If the file already exists, |created| is set false and |error code| 69 // is set PLATFORM_FILE_OK. 70 // If the file hasn't existed but it couldn't be created for some other 71 // reasons, |created| is set false and |error code| indicates the error. 72 typedef Callback2<PlatformFileError /* error code */, 73 bool /* created */>::Type EnsureFileExistsCallback; 74 static bool EnsureFileExists( 75 scoped_refptr<MessageLoopProxy> message_loop_proxy, 76 const FilePath& file_path, 77 EnsureFileExistsCallback* callback); 78 79 // Retrieves the information about a file. It is invalid to pass NULL for the 80 // callback. 81 typedef Callback2<PlatformFileError /* error code */, 82 const PlatformFileInfo& /* file_info */ 83 >::Type GetFileInfoCallback; 84 static bool GetFileInfo( 85 scoped_refptr<MessageLoopProxy> message_loop_proxy, 86 const FilePath& file_path, 87 GetFileInfoCallback* callback); 88 89 static bool GetFileInfoFromPlatformFile( 90 scoped_refptr<MessageLoopProxy> message_loop_proxy, 91 PlatformFile file, 92 GetFileInfoCallback* callback); 93 94 typedef Callback2<PlatformFileError /* error code */, 95 const std::vector<Entry>&>::Type ReadDirectoryCallback; 96 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy, 97 const FilePath& file_path, 98 ReadDirectoryCallback* callback); 99 100 // Copies a file or a directory from |src_file_path| to |dest_file_path| 101 // Error cases: 102 // If destination file doesn't exist or destination's parent 103 // doesn't exists. 104 // If source dir exists but destination path is an existing file. 105 // If source file exists but destination path is an existing directory. 106 // If source is a parent of destination. 107 // If source doesn't exists. 108 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, 109 const FilePath& src_file_path, 110 const FilePath& dest_file_path, 111 StatusCallback* callback); 112 113 // Creates directory at given path. It's an error to create 114 // if |exclusive| is true and dir already exists. 115 static bool CreateDirectory( 116 scoped_refptr<MessageLoopProxy> message_loop_proxy, 117 const FilePath& file_path, 118 bool exclusive, 119 bool recursive, 120 StatusCallback* callback); 121 122 // Deletes a file or a directory. 123 // It is an error to delete a non-empty directory with recursive=false. 124 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, 125 const FilePath& file_path, 126 bool recursive, 127 StatusCallback* callback); 128 129 // Moves a file or a directory from src_file_path to dest_file_path. 130 // Error cases are similar to Copy method's error cases. 131 static bool Move( 132 scoped_refptr<MessageLoopProxy> message_loop_proxy, 133 const FilePath& src_file_path, 134 const FilePath& dest_file_path, 135 StatusCallback* callback); 136 137 // Deletes a directory and all of its contents. 138 static bool RecursiveDelete( 139 scoped_refptr<MessageLoopProxy> message_loop_proxy, 140 const FilePath& file_path, 141 StatusCallback* callback); 142 143 // Reads from a file. On success, the file pointer is moved to position 144 // |offset + bytes_to_read| in the file. The callback can be NULL. 145 typedef Callback2<PlatformFileError /* error code */, 146 int /* bytes read/written */>::Type ReadWriteCallback; 147 static bool Read( 148 scoped_refptr<MessageLoopProxy> message_loop_proxy, 149 PlatformFile file, 150 int64 offset, 151 char* buffer, 152 int bytes_to_read, 153 ReadWriteCallback* callback); 154 155 // Writes to a file. If |offset| is greater than the length of the file, 156 // |false| is returned. On success, the file pointer is moved to position 157 // |offset + bytes_to_write| in the file. The callback can be NULL. 158 static bool Write( 159 scoped_refptr<MessageLoopProxy> message_loop_proxy, 160 PlatformFile file, 161 int64 offset, 162 const char* buffer, 163 int bytes_to_write, 164 ReadWriteCallback* callback); 165 166 // Touches a file. The callback can be NULL. 167 static bool Touch( 168 scoped_refptr<MessageLoopProxy> message_loop_proxy, 169 PlatformFile file, 170 const Time& last_access_time, 171 const Time& last_modified_time, 172 StatusCallback* callback); 173 174 // Touches a file. The callback can be NULL. 175 static bool Touch( 176 scoped_refptr<MessageLoopProxy> message_loop_proxy, 177 const FilePath& file_path, 178 const Time& last_access_time, 179 const Time& last_modified_time, 180 StatusCallback* callback); 181 182 // Truncates a file to the given length. If |length| is greater than the 183 // current length of the file, the file will be extended with zeroes. 184 // The callback can be NULL. 185 static bool Truncate( 186 scoped_refptr<MessageLoopProxy> message_loop_proxy, 187 PlatformFile file, 188 int64 length, 189 StatusCallback* callback); 190 191 // Truncates a file to the given length. If |length| is greater than the 192 // current length of the file, the file will be extended with zeroes. 193 // The callback can be NULL. 194 static bool Truncate( 195 scoped_refptr<MessageLoopProxy> message_loop_proxy, 196 const FilePath& path, 197 int64 length, 198 StatusCallback* callback); 199 200 // Flushes a file. The callback can be NULL. 201 static bool Flush( 202 scoped_refptr<MessageLoopProxy> message_loop_proxy, 203 PlatformFile file, 204 StatusCallback* callback); 205 206 private: 207 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); 208}; 209 210} // namespace base 211 212#endif // BASE_FILE_UTIL_PROXY_H_ 213