| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_FILES_FILE_H_ | 5 #ifndef BASE_FILES_FILE_H_ |
| 6 #define BASE_FILES_FILE_H_ | 6 #define BASE_FILES_FILE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/base_export.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_tracing.h" |
| 15 #include "base/files/scoped_file.h" |
| 16 #include "base/move.h" |
| 17 #include "base/time/time.h" |
| 8 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 |
| 9 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 10 #include <windows.h> | 21 #include <windows.h> |
| 22 #include "base/win/scoped_handle.h" |
| 11 #endif | 23 #endif |
| 12 | 24 |
| 13 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 14 #include <sys/stat.h> | 26 #include <sys/stat.h> |
| 15 #endif | 27 #endif |
| 16 | 28 |
| 17 #include <string> | |
| 18 | |
| 19 #include "base/base_export.h" | |
| 20 #include "base/basictypes.h" | |
| 21 #include "base/files/file_path.h" | |
| 22 #include "base/files/file_tracing.h" | |
| 23 #include "base/files/scoped_file.h" | |
| 24 #include "base/move.h" | |
| 25 #include "base/time/time.h" | |
| 26 | |
| 27 #if defined(OS_WIN) | |
| 28 #include "base/win/scoped_handle.h" | |
| 29 #endif | |
| 30 | |
| 31 namespace base { | 29 namespace base { |
| 32 | 30 |
| 33 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
| 34 typedef HANDLE PlatformFile; | 32 typedef HANDLE PlatformFile; |
| 35 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
| 36 typedef int PlatformFile; | 34 typedef int PlatformFile; |
| 37 | 35 |
| 38 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 36 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
| 39 typedef struct stat stat_wrapper_t; | 37 typedef struct stat stat_wrapper_t; |
| 40 #else | 38 #else |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // ipc/ipc_message_utils.cc. | 129 // ipc/ipc_message_utils.cc. |
| 132 struct BASE_EXPORT Info { | 130 struct BASE_EXPORT Info { |
| 133 Info(); | 131 Info(); |
| 134 ~Info(); | 132 ~Info(); |
| 135 #if defined(OS_POSIX) | 133 #if defined(OS_POSIX) |
| 136 // Fills this struct with values from |stat_info|. | 134 // Fills this struct with values from |stat_info|. |
| 137 void FromStat(const stat_wrapper_t& stat_info); | 135 void FromStat(const stat_wrapper_t& stat_info); |
| 138 #endif | 136 #endif |
| 139 | 137 |
| 140 // The size of the file in bytes. Undefined when is_directory is true. | 138 // The size of the file in bytes. Undefined when is_directory is true. |
| 141 int64 size; | 139 int64_t size; |
| 142 | 140 |
| 143 // True if the file corresponds to a directory. | 141 // True if the file corresponds to a directory. |
| 144 bool is_directory; | 142 bool is_directory; |
| 145 | 143 |
| 146 // True if the file corresponds to a symbolic link. For Windows currently | 144 // True if the file corresponds to a symbolic link. For Windows currently |
| 147 // not supported and thus always false. | 145 // not supported and thus always false. |
| 148 bool is_symbolic_link; | 146 bool is_symbolic_link; |
| 149 | 147 |
| 150 // The last modified time of a file. | 148 // The last modified time of a file. |
| 151 Time last_modified; | 149 Time last_modified; |
| 152 | 150 |
| 153 // The last accessed time of a file. | 151 // The last accessed time of a file. |
| 154 Time last_accessed; | 152 Time last_accessed; |
| 155 | 153 |
| 156 // The creation time of a file. | 154 // The creation time of a file. |
| 157 Time creation_time; | 155 Time creation_time; |
| 158 }; | 156 }; |
| 159 | 157 |
| 160 File(); | 158 File(); |
| 161 | 159 |
| 162 // Creates or opens the given file. This will fail with 'access denied' if the | 160 // Creates or opens the given file. This will fail with 'access denied' if the |
| 163 // |path| contains path traversal ('..') components. | 161 // |path| contains path traversal ('..') components. |
| 164 File(const FilePath& path, uint32 flags); | 162 File(const FilePath& path, uint32_t flags); |
| 165 | 163 |
| 166 // Takes ownership of |platform_file|. | 164 // Takes ownership of |platform_file|. |
| 167 explicit File(PlatformFile platform_file); | 165 explicit File(PlatformFile platform_file); |
| 168 | 166 |
| 169 // Creates an object with a specific error_details code. | 167 // Creates an object with a specific error_details code. |
| 170 explicit File(Error error_details); | 168 explicit File(Error error_details); |
| 171 | 169 |
| 172 File(File&& other); | 170 File(File&& other); |
| 173 | 171 |
| 174 ~File(); | 172 ~File(); |
| 175 | 173 |
| 176 // Takes ownership of |platform_file|. | 174 // Takes ownership of |platform_file|. |
| 177 static File CreateForAsyncHandle(PlatformFile platform_file); | 175 static File CreateForAsyncHandle(PlatformFile platform_file); |
| 178 | 176 |
| 179 File& operator=(File&& other); | 177 File& operator=(File&& other); |
| 180 | 178 |
| 181 // Creates or opens the given file. | 179 // Creates or opens the given file. |
| 182 void Initialize(const FilePath& path, uint32 flags); | 180 void Initialize(const FilePath& path, uint32_t flags); |
| 183 | 181 |
| 184 // Returns |true| if the handle / fd wrapped by this object is valid. This | 182 // Returns |true| if the handle / fd wrapped by this object is valid. This |
| 185 // method doesn't interact with the file system (and is safe to be called from | 183 // method doesn't interact with the file system (and is safe to be called from |
| 186 // ThreadRestrictions::SetIOAllowed(false) threads). | 184 // ThreadRestrictions::SetIOAllowed(false) threads). |
| 187 bool IsValid() const; | 185 bool IsValid() const; |
| 188 | 186 |
| 189 // Returns true if a new file was created (or an old one truncated to zero | 187 // Returns true if a new file was created (or an old one truncated to zero |
| 190 // length to simulate a new file, which can happen with | 188 // length to simulate a new file, which can happen with |
| 191 // FLAG_CREATE_ALWAYS), and false otherwise. | 189 // FLAG_CREATE_ALWAYS), and false otherwise. |
| 192 bool created() const { return created_; } | 190 bool created() const { return created_; } |
| 193 | 191 |
| 194 // Returns the OS result of opening this file. Note that the way to verify | 192 // Returns the OS result of opening this file. Note that the way to verify |
| 195 // the success of the operation is to use IsValid(), not this method: | 193 // the success of the operation is to use IsValid(), not this method: |
| 196 // File file(path, flags); | 194 // File file(path, flags); |
| 197 // if (!file.IsValid()) | 195 // if (!file.IsValid()) |
| 198 // return; | 196 // return; |
| 199 Error error_details() const { return error_details_; } | 197 Error error_details() const { return error_details_; } |
| 200 | 198 |
| 201 PlatformFile GetPlatformFile() const; | 199 PlatformFile GetPlatformFile() const; |
| 202 PlatformFile TakePlatformFile(); | 200 PlatformFile TakePlatformFile(); |
| 203 | 201 |
| 204 // Destroying this object closes the file automatically. | 202 // Destroying this object closes the file automatically. |
| 205 void Close(); | 203 void Close(); |
| 206 | 204 |
| 207 // Changes current position in the file to an |offset| relative to an origin | 205 // Changes current position in the file to an |offset| relative to an origin |
| 208 // defined by |whence|. Returns the resultant current position in the file | 206 // defined by |whence|. Returns the resultant current position in the file |
| 209 // (relative to the start) or -1 in case of error. | 207 // (relative to the start) or -1 in case of error. |
| 210 int64 Seek(Whence whence, int64 offset); | 208 int64_t Seek(Whence whence, int64_t offset); |
| 211 | 209 |
| 212 // Reads the given number of bytes (or until EOF is reached) starting with the | 210 // Reads the given number of bytes (or until EOF is reached) starting with the |
| 213 // given offset. Returns the number of bytes read, or -1 on error. Note that | 211 // given offset. Returns the number of bytes read, or -1 on error. Note that |
| 214 // this function makes a best effort to read all data on all platforms, so it | 212 // this function makes a best effort to read all data on all platforms, so it |
| 215 // is not intended for stream oriented files but instead for cases when the | 213 // is not intended for stream oriented files but instead for cases when the |
| 216 // normal expectation is that actually |size| bytes are read unless there is | 214 // normal expectation is that actually |size| bytes are read unless there is |
| 217 // an error. | 215 // an error. |
| 218 int Read(int64 offset, char* data, int size); | 216 int Read(int64_t offset, char* data, int size); |
| 219 | 217 |
| 220 // Same as above but without seek. | 218 // Same as above but without seek. |
| 221 int ReadAtCurrentPos(char* data, int size); | 219 int ReadAtCurrentPos(char* data, int size); |
| 222 | 220 |
| 223 // Reads the given number of bytes (or until EOF is reached) starting with the | 221 // Reads the given number of bytes (or until EOF is reached) starting with the |
| 224 // given offset, but does not make any effort to read all data on all | 222 // given offset, but does not make any effort to read all data on all |
| 225 // platforms. Returns the number of bytes read, or -1 on error. | 223 // platforms. Returns the number of bytes read, or -1 on error. |
| 226 int ReadNoBestEffort(int64 offset, char* data, int size); | 224 int ReadNoBestEffort(int64_t offset, char* data, int size); |
| 227 | 225 |
| 228 // Same as above but without seek. | 226 // Same as above but without seek. |
| 229 int ReadAtCurrentPosNoBestEffort(char* data, int size); | 227 int ReadAtCurrentPosNoBestEffort(char* data, int size); |
| 230 | 228 |
| 231 // Writes the given buffer into the file at the given offset, overwritting any | 229 // Writes the given buffer into the file at the given offset, overwritting any |
| 232 // data that was previously there. Returns the number of bytes written, or -1 | 230 // data that was previously there. Returns the number of bytes written, or -1 |
| 233 // on error. Note that this function makes a best effort to write all data on | 231 // on error. Note that this function makes a best effort to write all data on |
| 234 // all platforms. | 232 // all platforms. |
| 235 // Ignores the offset and writes to the end of the file if the file was opened | 233 // Ignores the offset and writes to the end of the file if the file was opened |
| 236 // with FLAG_APPEND. | 234 // with FLAG_APPEND. |
| 237 int Write(int64 offset, const char* data, int size); | 235 int Write(int64_t offset, const char* data, int size); |
| 238 | 236 |
| 239 // Save as above but without seek. | 237 // Save as above but without seek. |
| 240 int WriteAtCurrentPos(const char* data, int size); | 238 int WriteAtCurrentPos(const char* data, int size); |
| 241 | 239 |
| 242 // Save as above but does not make any effort to write all data on all | 240 // Save as above but does not make any effort to write all data on all |
| 243 // platforms. Returns the number of bytes written, or -1 on error. | 241 // platforms. Returns the number of bytes written, or -1 on error. |
| 244 int WriteAtCurrentPosNoBestEffort(const char* data, int size); | 242 int WriteAtCurrentPosNoBestEffort(const char* data, int size); |
| 245 | 243 |
| 246 // Returns the current size of this file, or a negative number on failure. | 244 // Returns the current size of this file, or a negative number on failure. |
| 247 int64 GetLength(); | 245 int64_t GetLength(); |
| 248 | 246 |
| 249 // Truncates the file to the given length. If |length| is greater than the | 247 // Truncates the file to the given length. If |length| is greater than the |
| 250 // current size of the file, the file is extended with zeros. If the file | 248 // current size of the file, the file is extended with zeros. If the file |
| 251 // doesn't exist, |false| is returned. | 249 // doesn't exist, |false| is returned. |
| 252 bool SetLength(int64 length); | 250 bool SetLength(int64_t length); |
| 253 | 251 |
| 254 // Instructs the filesystem to flush the file to disk. (POSIX: fsync, Windows: | 252 // Instructs the filesystem to flush the file to disk. (POSIX: fsync, Windows: |
| 255 // FlushFileBuffers). | 253 // FlushFileBuffers). |
| 256 bool Flush(); | 254 bool Flush(); |
| 257 | 255 |
| 258 // Updates the file times. | 256 // Updates the file times. |
| 259 bool SetTimes(Time last_access_time, Time last_modified_time); | 257 bool SetTimes(Time last_access_time, Time last_modified_time); |
| 260 | 258 |
| 261 // Returns some basic information for the given file. | 259 // Returns some basic information for the given file. |
| 262 bool GetInfo(Info* info); | 260 bool GetInfo(Info* info); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 #endif | 300 #endif |
| 303 | 301 |
| 304 // Converts an error value to a human-readable form. Used for logging. | 302 // Converts an error value to a human-readable form. Used for logging. |
| 305 static std::string ErrorToString(Error error); | 303 static std::string ErrorToString(Error error); |
| 306 | 304 |
| 307 private: | 305 private: |
| 308 friend class FileTracing::ScopedTrace; | 306 friend class FileTracing::ScopedTrace; |
| 309 | 307 |
| 310 // Creates or opens the given file. Only called if |path| has no | 308 // Creates or opens the given file. Only called if |path| has no |
| 311 // traversal ('..') components. | 309 // traversal ('..') components. |
| 312 void DoInitialize(const FilePath& path, uint32 flags); | 310 void DoInitialize(const FilePath& path, uint32_t flags); |
| 313 | 311 |
| 314 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, | 312 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, |
| 315 // cf. issue 473337. | 313 // cf. issue 473337. |
| 316 bool DoFlush(); | 314 bool DoFlush(); |
| 317 | 315 |
| 318 void SetPlatformFile(PlatformFile file); | 316 void SetPlatformFile(PlatformFile file); |
| 319 | 317 |
| 320 #if defined(OS_WIN) | 318 #if defined(OS_WIN) |
| 321 win::ScopedHandle file_; | 319 win::ScopedHandle file_; |
| 322 #elif defined(OS_POSIX) | 320 #elif defined(OS_POSIX) |
| 323 ScopedFD file_; | 321 ScopedFD file_; |
| 324 #endif | 322 #endif |
| 325 | 323 |
| 326 // A path to use for tracing purposes. Set if file tracing is enabled during | 324 // A path to use for tracing purposes. Set if file tracing is enabled during |
| 327 // |Initialize()|. | 325 // |Initialize()|. |
| 328 FilePath tracing_path_; | 326 FilePath tracing_path_; |
| 329 | 327 |
| 330 // Object tied to the lifetime of |this| that enables/disables tracing. | 328 // Object tied to the lifetime of |this| that enables/disables tracing. |
| 331 FileTracing::ScopedEnabler trace_enabler_; | 329 FileTracing::ScopedEnabler trace_enabler_; |
| 332 | 330 |
| 333 Error error_details_; | 331 Error error_details_; |
| 334 bool created_; | 332 bool created_; |
| 335 bool async_; | 333 bool async_; |
| 336 }; | 334 }; |
| 337 | 335 |
| 338 } // namespace base | 336 } // namespace base |
| 339 | 337 |
| 340 #endif // BASE_FILES_FILE_H_ | 338 #endif // BASE_FILES_FILE_H_ |
| 341 | 339 |
| OLD | NEW |