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/file_path.h" |
22 #include "base/files/file_tracing.h" | 22 #include "base/files/file_tracing.h" |
23 #include "base/files/scoped_file.h" | 23 #include "base/files/scoped_file.h" |
24 #include "base/gtest_prod_util.h" | |
25 #include "base/move.h" | 24 #include "base/move.h" |
26 #include "base/time/time.h" | 25 #include "base/time/time.h" |
27 | 26 |
28 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
29 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
30 #endif | 29 #endif |
31 | 30 |
32 FORWARD_DECLARE_TEST(FileTest, MemoryCorruption); | |
33 | |
34 namespace base { | 31 namespace base { |
35 | 32 |
36 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
37 typedef HANDLE PlatformFile; | 34 typedef HANDLE PlatformFile; |
38 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
39 typedef int PlatformFile; | 36 typedef int PlatformFile; |
40 | 37 |
41 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 38 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
42 typedef struct stat stat_wrapper_t; | 39 typedef struct stat stat_wrapper_t; |
43 #else | 40 #else |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 #if defined(OS_WIN) | 296 #if defined(OS_WIN) |
300 static Error OSErrorToFileError(DWORD last_error); | 297 static Error OSErrorToFileError(DWORD last_error); |
301 #elif defined(OS_POSIX) | 298 #elif defined(OS_POSIX) |
302 static Error OSErrorToFileError(int saved_errno); | 299 static Error OSErrorToFileError(int saved_errno); |
303 #endif | 300 #endif |
304 | 301 |
305 // 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. |
306 static std::string ErrorToString(Error error); | 303 static std::string ErrorToString(Error error); |
307 | 304 |
308 private: | 305 private: |
309 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); | |
310 | |
311 friend class FileTracing::ScopedTrace; | 306 friend class FileTracing::ScopedTrace; |
312 | 307 |
313 #if defined(OS_POSIX) | |
314 // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum | |
315 // alongside it. This checksum is validated at every access, allowing early | |
316 // detection of memory corruption. | |
317 | |
318 // TODO(gavinp): This is in place temporarily to help us debug | |
319 // https://crbug.com/424562 , which can't be reproduced in valgrind. Remove | |
320 // this code after we have fixed this issue. | |
321 class MemoryCheckingScopedFD { | |
322 public: | |
323 MemoryCheckingScopedFD(); | |
324 MemoryCheckingScopedFD(int fd); | |
325 ~MemoryCheckingScopedFD(); | |
326 | |
327 bool is_valid() const { Check(); return file_.is_valid(); } | |
328 int get() const { Check(); return file_.get(); } | |
329 | |
330 void reset() { Check(); file_.reset(); UpdateChecksum(); } | |
331 void reset(int fd) { Check(); file_.reset(fd); UpdateChecksum(); } | |
332 int release() { | |
333 Check(); | |
334 int fd = file_.release(); | |
335 UpdateChecksum(); | |
336 return fd; | |
337 } | |
338 | |
339 private: | |
340 FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption); | |
341 | |
342 // Computes the checksum for the current value of |file_|. Returns via an | |
343 // out parameter to guard against implicit conversions of unsigned integral | |
344 // types. | |
345 void ComputeMemoryChecksum(unsigned int* out_checksum) const; | |
346 | |
347 // Confirms that the current |file_| and |file_memory_checksum_| agree, | |
348 // failing a CHECK if they do not. | |
349 void Check() const; | |
350 | |
351 void UpdateChecksum(); | |
352 | |
353 ScopedFD file_; | |
354 unsigned int file_memory_checksum_; | |
355 }; | |
356 #endif | |
357 | |
358 // 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 |
359 // traversal ('..') components. | 309 // traversal ('..') components. |
360 void DoInitialize(const FilePath& path, uint32 flags); | 310 void DoInitialize(const FilePath& path, uint32 flags); |
361 | 311 |
362 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, | 312 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, |
363 // cf. issue 473337. | 313 // cf. issue 473337. |
364 bool DoFlush(); | 314 bool DoFlush(); |
365 | 315 |
366 void SetPlatformFile(PlatformFile file); | 316 void SetPlatformFile(PlatformFile file); |
367 | 317 |
368 #if defined(OS_WIN) | 318 #if defined(OS_WIN) |
369 win::ScopedHandle file_; | 319 win::ScopedHandle file_; |
370 #elif defined(OS_POSIX) | 320 #elif defined(OS_POSIX) |
371 MemoryCheckingScopedFD file_; | 321 ScopedFD file_; |
372 #endif | 322 #endif |
373 | 323 |
374 // 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 |
375 // |Initialize()|. | 325 // |Initialize()|. |
376 FilePath tracing_path_; | 326 FilePath tracing_path_; |
377 | 327 |
378 // Object tied to the lifetime of |this| that enables/disables tracing. | 328 // Object tied to the lifetime of |this| that enables/disables tracing. |
379 FileTracing::ScopedEnabler trace_enabler_; | 329 FileTracing::ScopedEnabler trace_enabler_; |
380 | 330 |
381 Error error_details_; | 331 Error error_details_; |
382 bool created_; | 332 bool created_; |
383 bool async_; | 333 bool async_; |
384 }; | 334 }; |
385 | 335 |
386 } // namespace base | 336 } // namespace base |
387 | 337 |
388 #endif // BASE_FILES_FILE_H_ | 338 #endif // BASE_FILES_FILE_H_ |
| 339 |
OLD | NEW |