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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // failing a CHECK if they do not. | 342 // failing a CHECK if they do not. |
344 void Check() const; | 343 void Check() const; |
345 | 344 |
346 void UpdateChecksum(); | 345 void UpdateChecksum(); |
347 | 346 |
348 ScopedFD file_; | 347 ScopedFD file_; |
349 unsigned int file_memory_checksum_; | 348 unsigned int file_memory_checksum_; |
350 }; | 349 }; |
351 #endif | 350 #endif |
352 | 351 |
353 // Creates or opens the given file. Only called if |name| has no traversal | 352 // Creates or opens the given file. Only called if |path_| has no traversal |
354 // ('..') components. | 353 // ('..') components. |
355 void DoInitialize(const FilePath& name, uint32 flags); | 354 void DoInitialize(uint32 flags); |
356 | 355 |
357 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, | 356 // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, |
358 // cf. issue 473337. | 357 // cf. issue 473337. |
359 bool DoFlush(); | 358 bool DoFlush(); |
360 | 359 |
361 void SetPlatformFile(PlatformFile file); | 360 void SetPlatformFile(PlatformFile file); |
362 | 361 |
363 #if defined(OS_WIN) | 362 #if defined(OS_WIN) |
364 win::ScopedHandle file_; | 363 win::ScopedHandle file_; |
365 #elif defined(OS_POSIX) | 364 #elif defined(OS_POSIX) |
366 MemoryCheckingScopedFD file_; | 365 MemoryCheckingScopedFD file_; |
367 #endif | 366 #endif |
368 | 367 |
| 368 // Path that |Initialize()| was called with. Can be unsafe (e.g. have '..'). |
| 369 FilePath path_; |
| 370 |
369 Error error_details_; | 371 Error error_details_; |
370 bool created_; | 372 bool created_; |
371 bool async_; | 373 bool async_; |
372 }; | 374 }; |
373 | 375 |
374 } // namespace base | 376 } // namespace base |
375 | 377 |
376 #endif // BASE_FILES_FILE_H_ | 378 #endif // BASE_FILES_FILE_H_ |
OLD | NEW |