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