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 #include "base/files/file.h" | 5 #include "base/files/file.h" |
6 | 6 |
7 #include <io.h> | 7 #include <io.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 // Make sure our Whence mappings match the system headers. | 16 // Make sure our Whence mappings match the system headers. |
17 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && | 17 COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && |
18 File::FROM_CURRENT == FILE_CURRENT && | 18 File::FROM_CURRENT == FILE_CURRENT && |
19 File::FROM_END == FILE_END, whence_matches_system); | 19 File::FROM_END == FILE_END, whence_matches_system); |
20 | 20 |
21 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { | |
Lei Zhang
2015/04/23 20:53:24
Just change the method name and don't reorder the
Dan Beam
2015/04/23 21:04:11
the .h order wouldn't match the .cc.
Lei Zhang
2015/04/23 21:10:43
If you don't mind being git blamed for all this co
| |
22 ThreadRestrictions::AssertIOAllowed(); | |
23 DCHECK(!IsValid()); | |
24 | |
25 DWORD disposition = 0; | |
26 | |
27 if (flags & FLAG_OPEN) | |
28 disposition = OPEN_EXISTING; | |
29 | |
30 if (flags & FLAG_CREATE) { | |
31 DCHECK(!disposition); | |
32 disposition = CREATE_NEW; | |
33 } | |
34 | |
35 if (flags & FLAG_OPEN_ALWAYS) { | |
36 DCHECK(!disposition); | |
37 disposition = OPEN_ALWAYS; | |
38 } | |
39 | |
40 if (flags & FLAG_CREATE_ALWAYS) { | |
41 DCHECK(!disposition); | |
42 DCHECK(flags & FLAG_WRITE); | |
43 disposition = CREATE_ALWAYS; | |
44 } | |
45 | |
46 if (flags & FLAG_OPEN_TRUNCATED) { | |
47 DCHECK(!disposition); | |
48 DCHECK(flags & FLAG_WRITE); | |
49 disposition = TRUNCATE_EXISTING; | |
50 } | |
51 | |
52 if (!disposition) { | |
53 NOTREACHED(); | |
54 return; | |
55 } | |
56 | |
57 DWORD access = 0; | |
58 if (flags & FLAG_WRITE) | |
59 access = GENERIC_WRITE; | |
60 if (flags & FLAG_APPEND) { | |
61 DCHECK(!access); | |
62 access = FILE_APPEND_DATA; | |
63 } | |
64 if (flags & FLAG_READ) | |
65 access |= GENERIC_READ; | |
66 if (flags & FLAG_WRITE_ATTRIBUTES) | |
67 access |= FILE_WRITE_ATTRIBUTES; | |
68 if (flags & FLAG_EXECUTE) | |
69 access |= GENERIC_EXECUTE; | |
70 | |
71 DWORD sharing = (flags & FLAG_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; | |
72 if (!(flags & FLAG_EXCLUSIVE_WRITE)) | |
73 sharing |= FILE_SHARE_WRITE; | |
74 if (flags & FLAG_SHARE_DELETE) | |
75 sharing |= FILE_SHARE_DELETE; | |
76 | |
77 DWORD create_flags = 0; | |
78 if (flags & FLAG_ASYNC) | |
79 create_flags |= FILE_FLAG_OVERLAPPED; | |
80 if (flags & FLAG_TEMPORARY) | |
81 create_flags |= FILE_ATTRIBUTE_TEMPORARY; | |
82 if (flags & FLAG_HIDDEN) | |
83 create_flags |= FILE_ATTRIBUTE_HIDDEN; | |
84 if (flags & FLAG_DELETE_ON_CLOSE) | |
85 create_flags |= FILE_FLAG_DELETE_ON_CLOSE; | |
86 if (flags & FLAG_BACKUP_SEMANTICS) | |
87 create_flags |= FILE_FLAG_BACKUP_SEMANTICS; | |
88 | |
89 file_.Set(CreateFile(name.value().c_str(), access, sharing, NULL, | |
90 disposition, create_flags, NULL)); | |
91 | |
92 if (file_.IsValid()) { | |
93 error_details_ = FILE_OK; | |
94 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | |
95 | |
96 if (flags & (FLAG_OPEN_ALWAYS)) | |
97 created_ = (ERROR_ALREADY_EXISTS != GetLastError()); | |
98 else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | |
99 created_ = true; | |
100 } else { | |
101 error_details_ = OSErrorToFileError(GetLastError()); | |
102 } | |
103 } | |
104 | |
105 bool File::IsValid() const { | 21 bool File::IsValid() const { |
106 return file_.IsValid(); | 22 return file_.IsValid(); |
107 } | 23 } |
108 | 24 |
109 PlatformFile File::GetPlatformFile() const { | 25 PlatformFile File::GetPlatformFile() const { |
110 return file_.Get(); | 26 return file_.Get(); |
111 } | 27 } |
112 | 28 |
113 PlatformFile File::TakePlatformFile() { | 29 PlatformFile File::TakePlatformFile() { |
114 return file_.Take(); | 30 return file_.Take(); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 case ERROR_FILE_CORRUPT: | 271 case ERROR_FILE_CORRUPT: |
356 case ERROR_DISK_CORRUPT: | 272 case ERROR_DISK_CORRUPT: |
357 return FILE_ERROR_IO; | 273 return FILE_ERROR_IO; |
358 default: | 274 default: |
359 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", | 275 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", |
360 last_error); | 276 last_error); |
361 return FILE_ERROR_FAILED; | 277 return FILE_ERROR_FAILED; |
362 } | 278 } |
363 } | 279 } |
364 | 280 |
281 void File::DoInitialize(const FilePath& name, uint32 flags) { | |
282 ThreadRestrictions::AssertIOAllowed(); | |
283 DCHECK(!IsValid()); | |
284 | |
285 DWORD disposition = 0; | |
286 | |
287 if (flags & FLAG_OPEN) | |
288 disposition = OPEN_EXISTING; | |
289 | |
290 if (flags & FLAG_CREATE) { | |
291 DCHECK(!disposition); | |
292 disposition = CREATE_NEW; | |
293 } | |
294 | |
295 if (flags & FLAG_OPEN_ALWAYS) { | |
296 DCHECK(!disposition); | |
297 disposition = OPEN_ALWAYS; | |
298 } | |
299 | |
300 if (flags & FLAG_CREATE_ALWAYS) { | |
301 DCHECK(!disposition); | |
302 DCHECK(flags & FLAG_WRITE); | |
303 disposition = CREATE_ALWAYS; | |
304 } | |
305 | |
306 if (flags & FLAG_OPEN_TRUNCATED) { | |
307 DCHECK(!disposition); | |
308 DCHECK(flags & FLAG_WRITE); | |
309 disposition = TRUNCATE_EXISTING; | |
310 } | |
311 | |
312 if (!disposition) { | |
313 NOTREACHED(); | |
314 return; | |
315 } | |
316 | |
317 DWORD access = 0; | |
318 if (flags & FLAG_WRITE) | |
319 access = GENERIC_WRITE; | |
320 if (flags & FLAG_APPEND) { | |
321 DCHECK(!access); | |
322 access = FILE_APPEND_DATA; | |
323 } | |
324 if (flags & FLAG_READ) | |
325 access |= GENERIC_READ; | |
326 if (flags & FLAG_WRITE_ATTRIBUTES) | |
327 access |= FILE_WRITE_ATTRIBUTES; | |
328 if (flags & FLAG_EXECUTE) | |
329 access |= GENERIC_EXECUTE; | |
330 | |
331 DWORD sharing = (flags & FLAG_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; | |
332 if (!(flags & FLAG_EXCLUSIVE_WRITE)) | |
333 sharing |= FILE_SHARE_WRITE; | |
334 if (flags & FLAG_SHARE_DELETE) | |
335 sharing |= FILE_SHARE_DELETE; | |
336 | |
337 DWORD create_flags = 0; | |
338 if (flags & FLAG_ASYNC) | |
339 create_flags |= FILE_FLAG_OVERLAPPED; | |
340 if (flags & FLAG_TEMPORARY) | |
341 create_flags |= FILE_ATTRIBUTE_TEMPORARY; | |
342 if (flags & FLAG_HIDDEN) | |
343 create_flags |= FILE_ATTRIBUTE_HIDDEN; | |
344 if (flags & FLAG_DELETE_ON_CLOSE) | |
345 create_flags |= FILE_FLAG_DELETE_ON_CLOSE; | |
346 if (flags & FLAG_BACKUP_SEMANTICS) | |
347 create_flags |= FILE_FLAG_BACKUP_SEMANTICS; | |
348 | |
349 file_.Set(CreateFile(name.value().c_str(), access, sharing, NULL, | |
350 disposition, create_flags, NULL)); | |
351 | |
352 if (file_.IsValid()) { | |
353 error_details_ = FILE_OK; | |
354 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); | |
355 | |
356 if (flags & (FLAG_OPEN_ALWAYS)) | |
357 created_ = (ERROR_ALREADY_EXISTS != GetLastError()); | |
358 else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) | |
359 created_ = true; | |
360 } else { | |
361 error_details_ = OSErrorToFileError(GetLastError()); | |
362 } | |
363 } | |
364 | |
365 bool File::DoFlush() { | 365 bool File::DoFlush() { |
366 ThreadRestrictions::AssertIOAllowed(); | 366 ThreadRestrictions::AssertIOAllowed(); |
367 DCHECK(IsValid()); | 367 DCHECK(IsValid()); |
368 return ::FlushFileBuffers(file_.Get()) != FALSE; | 368 return ::FlushFileBuffers(file_.Get()) != FALSE; |
369 } | 369 } |
370 | 370 |
371 void File::SetPlatformFile(PlatformFile file) { | 371 void File::SetPlatformFile(PlatformFile file) { |
372 file_.Set(file); | 372 file_.Set(file); |
373 } | 373 } |
374 | 374 |
375 } // namespace base | 375 } // namespace base |
OLD | NEW |