OLD | NEW |
| (Empty) |
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 "net/disk_cache/os_file.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace disk_cache { | |
10 | |
11 OSFile CreateOSFile(const std::wstring& name, int flags, bool* created) { | |
12 DWORD disposition = 0; | |
13 | |
14 if (flags & OS_FILE_OPEN) | |
15 disposition = OPEN_EXISTING; | |
16 | |
17 if (flags & OS_FILE_CREATE) { | |
18 DCHECK(!disposition); | |
19 disposition = CREATE_NEW; | |
20 } | |
21 | |
22 if (flags & OS_FILE_OPEN_ALWAYS) { | |
23 DCHECK(!disposition); | |
24 disposition = OPEN_ALWAYS; | |
25 } | |
26 | |
27 if (flags & OS_FILE_CREATE_ALWAYS) { | |
28 DCHECK(!disposition); | |
29 disposition = CREATE_ALWAYS; | |
30 } | |
31 | |
32 if (!disposition) { | |
33 NOTREACHED(); | |
34 return NULL; | |
35 } | |
36 | |
37 DWORD access = (flags & OS_FILE_READ) ? GENERIC_READ : 0; | |
38 if (flags & OS_FILE_WRITE) | |
39 access |= GENERIC_WRITE; | |
40 | |
41 DWORD sharing = (flags & OS_FILE_SHARE_READ) ? FILE_SHARE_READ : 0; | |
42 if (flags & OS_FILE_SHARE_WRITE) | |
43 access |= FILE_SHARE_WRITE; | |
44 | |
45 HANDLE file = CreateFile(name.c_str(), access, sharing, NULL, disposition, 0, | |
46 NULL); | |
47 | |
48 if ((flags & OS_FILE_OPEN_ALWAYS) && created && | |
49 INVALID_HANDLE_VALUE != file) { | |
50 *created = (ERROR_ALREADY_EXISTS != GetLastError()); | |
51 } | |
52 | |
53 return file; | |
54 } | |
55 | |
56 } // namespace disk_cache | |
57 | |
OLD | NEW |