| 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 "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 #include <string> | 17 #include <string> |
| 18 | 18 |
| 19 #include "base/base_export.h" | 19 #include "base/base_export.h" |
| 20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 21 #include "base/files/file_path.h" |
| 21 #include "base/files/scoped_file.h" | 22 #include "base/files/scoped_file.h" |
| 22 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
| 23 #include "base/move.h" | 24 #include "base/move.h" |
| 24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 25 | 26 |
| 26 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 27 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 FORWARD_DECLARE_TEST(FileTest, MemoryCorruption); | 31 FORWARD_DECLARE_TEST(FileTest, MemoryCorruption); |
| 31 | 32 |
| 32 namespace base { | 33 namespace base { |
| 33 | 34 |
| 34 class FilePath; | |
| 35 | |
| 36 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 37 typedef HANDLE PlatformFile; | 36 typedef HANDLE PlatformFile; |
| 38 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
| 39 typedef int PlatformFile; | 38 typedef int PlatformFile; |
| 40 | 39 |
| 41 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 40 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
| 42 typedef struct stat stat_wrapper_t; | 41 typedef struct stat stat_wrapper_t; |
| 43 #else | 42 #else |
| 44 typedef struct stat64 stat_wrapper_t; | 43 typedef struct stat64 stat_wrapper_t; |
| 45 #endif | 44 #endif |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // The last accessed time of a file. | 154 // The last accessed time of a file. |
| 156 Time last_accessed; | 155 Time last_accessed; |
| 157 | 156 |
| 158 // The creation time of a file. | 157 // The creation time of a file. |
| 159 Time creation_time; | 158 Time creation_time; |
| 160 }; | 159 }; |
| 161 | 160 |
| 162 File(); | 161 File(); |
| 163 | 162 |
| 164 // Creates or opens the given file. This will fail with 'access denied' if the | 163 // Creates or opens the given file. This will fail with 'access denied' if the |
| 165 // |name| contains path traversal ('..') components. | 164 // |path| contains path traversal ('..') components. |
| 166 File(const FilePath& name, uint32 flags); | 165 File(const FilePath& path, uint32 flags); |
| 167 | 166 |
| 168 // Takes ownership of |platform_file|. | 167 // Takes ownership of |platform_file|. |
| 169 explicit File(PlatformFile platform_file); | 168 explicit File(PlatformFile platform_file); |
| 170 | 169 |
| 171 // Creates an object with a specific error_details code. | 170 // Creates an object with a specific error_details code. |
| 172 explicit File(Error error_details); | 171 explicit File(Error error_details); |
| 173 | 172 |
| 174 // Move constructor for C++03 move emulation of this type. | 173 // Move constructor for C++03 move emulation of this type. |
| 175 File(RValue other); | 174 File(RValue other); |
| 176 | 175 |
| 177 ~File(); | 176 ~File(); |
| 178 | 177 |
| 179 // Move operator= for C++03 move emulation of this type. | 178 // Move operator= for C++03 move emulation of this type. |
| 180 File& operator=(RValue other); | 179 File& operator=(RValue other); |
| 181 | 180 |
| 182 // Creates or opens the given file. | 181 // Creates or opens the given file. |
| 183 void Initialize(const FilePath& name, uint32 flags); | 182 void Initialize(const FilePath& path, uint32 flags); |
| 184 | 183 |
| 185 bool IsValid() const; | 184 bool IsValid() const; |
| 186 | 185 |
| 187 // Returns true if a new file was created (or an old one truncated to zero | 186 // Returns true if a new file was created (or an old one truncated to zero |
| 188 // length to simulate a new file, which can happen with | 187 // length to simulate a new file, which can happen with |
| 189 // FLAG_CREATE_ALWAYS), and false otherwise. | 188 // FLAG_CREATE_ALWAYS), and false otherwise. |
| 190 bool created() const { return created_; } | 189 bool created() const { return created_; } |
| 191 | 190 |
| 192 // Returns the OS result of opening this file. Note that the way to verify | 191 // Returns the OS result of opening this file. Note that the way to verify |
| 193 // the success of the operation is to use IsValid(), not this method: | 192 // the success of the operation is to use IsValid(), not this method: |
| 194 // File file(name, flags); | 193 // File file(path, flags); |
| 195 // if (!file.IsValid()) | 194 // if (!file.IsValid()) |
| 196 // return; | 195 // return; |
| 197 Error error_details() const { return error_details_; } | 196 Error error_details() const { return error_details_; } |
| 198 | 197 |
| 199 PlatformFile GetPlatformFile() const; | 198 PlatformFile GetPlatformFile() const; |
| 200 PlatformFile TakePlatformFile(); | 199 PlatformFile TakePlatformFile(); |
| 201 | 200 |
| 202 // Destroying this object closes the file automatically. | 201 // Destroying this object closes the file automatically. |
| 203 void Close(); | 202 void Close(); |
| 204 | 203 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 #elif defined(OS_POSIX) | 297 #elif defined(OS_POSIX) |
| 299 static Error OSErrorToFileError(int saved_errno); | 298 static Error OSErrorToFileError(int saved_errno); |
| 300 #endif | 299 #endif |
| 301 | 300 |
| 302 // Converts an error value to a human-readable form. Used for logging. | 301 // Converts an error value to a human-readable form. Used for logging. |
| 303 static std::string ErrorToString(Error error); | 302 static std::string ErrorToString(Error error); |
| 304 | 303 |
| 305 private: | 304 private: |
| 306 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); | 305 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); |
| 307 | 306 |
| 307 friend class ScopedFileTrace; |
| 308 |
| 308 #if defined(OS_POSIX) | 309 #if defined(OS_POSIX) |
| 309 // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum | 310 // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum |
| 310 // alongside it. This checksum is validated at every access, allowing early | 311 // alongside it. This checksum is validated at every access, allowing early |
| 311 // detection of memory corruption. | 312 // detection of memory corruption. |
| 312 | 313 |
| 313 // TODO(gavinp): This is in place temporarily to help us debug | 314 // TODO(gavinp): This is in place temporarily to help us debug |
| 314 // https://crbug.com/424562 , which can't be reproduced in valgrind. Remove | 315 // https://crbug.com/424562 , which can't be reproduced in valgrind. Remove |
| 315 // this code after we have fixed this issue. | 316 // this code after we have fixed this issue. |
| 316 class MemoryCheckingScopedFD { | 317 class MemoryCheckingScopedFD { |
| 317 public: | 318 public: |
| (...skipping 25 matching lines...) Expand all Loading... |
| 343 // failing a CHECK if they do not. | 344 // failing a CHECK if they do not. |
| 344 void Check() const; | 345 void Check() const; |
| 345 | 346 |
| 346 void UpdateChecksum(); | 347 void UpdateChecksum(); |
| 347 | 348 |
| 348 ScopedFD file_; | 349 ScopedFD file_; |
| 349 unsigned int file_memory_checksum_; | 350 unsigned int file_memory_checksum_; |
| 350 }; | 351 }; |
| 351 #endif | 352 #endif |
| 352 | 353 |
| 353 // Creates or opens the given file. Only called if |name| has no traversal | 354 // Creates or opens the given file. Only called if |path_| has no |
| 354 // ('..') components. | 355 // traversal ('..') components. |
| 355 void DoInitialize(const FilePath& name, uint32 flags); | 356 void DoInitialize(uint32 flags); |
| 356 | 357 |
| 357 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, | 358 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, |
| 358 // cf. issue 473337. | 359 // cf. issue 473337. |
| 359 bool DoFlush(); | 360 bool DoFlush(); |
| 360 | 361 |
| 361 void SetPlatformFile(PlatformFile file); | 362 void SetPlatformFile(PlatformFile file); |
| 362 | 363 |
| 363 #if defined(OS_WIN) | 364 #if defined(OS_WIN) |
| 364 win::ScopedHandle file_; | 365 win::ScopedHandle file_; |
| 365 #elif defined(OS_POSIX) | 366 #elif defined(OS_POSIX) |
| 366 MemoryCheckingScopedFD file_; | 367 MemoryCheckingScopedFD file_; |
| 367 #endif | 368 #endif |
| 368 | 369 |
| 370 // Path that |Initialize()| was called with. Only set if safe (i.e. no '..'). |
| 371 FilePath path_; |
| 372 |
| 373 class TraceEnabler { |
| 374 public: |
| 375 TraceEnabler(); |
| 376 ~TraceEnabler(); |
| 377 }; |
| 378 |
| 379 // Object tied to the lifetime of this File that enables/disables tracing. |
| 380 TraceEnabler trace_enabler_; |
| 381 |
| 369 Error error_details_; | 382 Error error_details_; |
| 370 bool created_; | 383 bool created_; |
| 371 bool async_; | 384 bool async_; |
| 372 }; | 385 }; |
| 373 | 386 |
| 374 } // namespace base | 387 } // namespace base |
| 375 | 388 |
| 376 #endif // BASE_FILES_FILE_H_ | 389 #endif // BASE_FILES_FILE_H_ |
| OLD | NEW |