|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include "base/platform_file.h" |
|
6 |
|
7 #include "base/logging.h" |
|
8 |
|
9 namespace base { |
|
10 |
|
11 PlatformFile CreatePlatformFile(const std::wstring& name, |
|
12 int flags, |
|
13 bool* created) { |
|
14 DWORD disposition = 0; |
|
15 |
|
16 if (flags & PLATFORM_FILE_OPEN) |
|
17 disposition = OPEN_EXISTING; |
|
18 |
|
19 if (flags & PLATFORM_FILE_CREATE) { |
|
20 DCHECK(!disposition); |
|
21 disposition = CREATE_NEW; |
|
22 } |
|
23 |
|
24 if (flags & PLATFORM_FILE_OPEN_ALWAYS) { |
|
25 DCHECK(!disposition); |
|
26 disposition = OPEN_ALWAYS; |
|
27 } |
|
28 |
|
29 if (flags & PLATFORM_FILE_CREATE_ALWAYS) { |
|
30 DCHECK(!disposition); |
|
31 disposition = CREATE_ALWAYS; |
|
32 } |
|
33 |
|
34 if (!disposition) { |
|
35 NOTREACHED(); |
|
36 return NULL; |
|
37 } |
|
38 |
|
39 DWORD access = (flags & PLATFORM_FILE_READ) ? GENERIC_READ : 0; |
|
40 if (flags & PLATFORM_FILE_WRITE) |
|
41 access |= GENERIC_WRITE; |
|
42 |
|
43 DWORD sharing = (flags & PLATFORM_FILE_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; |
|
44 if (!(flags & PLATFORM_FILE_EXCLUSIVE_WRITE)) |
|
45 sharing |= FILE_SHARE_WRITE; |
|
46 |
|
47 DWORD create_flags = 0; |
|
48 if (flags & PLATFORM_FILE_ASYNC) |
|
49 create_flags |= FILE_FLAG_OVERLAPPED; |
|
50 |
|
51 HANDLE file = CreateFile(name.c_str(), access, sharing, NULL, disposition, |
|
52 create_flags, NULL); |
|
53 |
|
54 if ((flags & PLATFORM_FILE_OPEN_ALWAYS) && created && |
|
55 INVALID_HANDLE_VALUE != file) { |
|
56 *created = (ERROR_ALREADY_EXISTS != GetLastError()); |
|
57 } |
|
58 |
|
59 return file; |
|
60 } |
|
61 |
|
62 } // namespace disk_cache |