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