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 <fcntl.h> | |
8 #include <errno.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/string_util.h" | |
12 | |
13 namespace disk_cache { | |
14 | |
15 OSFile CreateOSFile(const std::wstring& name, int flags, bool* created) { | |
16 int open_flags = 0; | |
17 if (flags & OS_FILE_CREATE) | |
18 open_flags = O_CREAT | O_EXCL; | |
19 | |
20 if (flags & OS_FILE_CREATE_ALWAYS) { | |
21 DCHECK(!open_flags); | |
22 open_flags = O_CREAT | O_TRUNC; | |
23 } | |
24 | |
25 if (!open_flags && !(flags & OS_FILE_OPEN) && | |
26 !(flags & OS_FILE_OPEN_ALWAYS)) { | |
27 NOTREACHED(); | |
28 errno = ENOTSUP; | |
29 return INVALID_HANDLE_VALUE; | |
30 } | |
31 | |
32 if (flags & OS_FILE_WRITE && flags & OS_FILE_READ) { | |
33 open_flags |= O_RDWR; | |
34 } else if (flags & OS_FILE_WRITE) { | |
35 open_flags |= O_WRONLY; | |
36 } else if (!(flags & OS_FILE_READ)) { | |
37 NOTREACHED(); | |
38 } | |
39 | |
40 DCHECK(O_RDONLY == 0); | |
41 | |
42 int descriptor = open(WideToUTF8(name).c_str(), open_flags, | |
43 S_IRUSR | S_IWUSR); | |
44 | |
45 if (flags & OS_FILE_OPEN_ALWAYS) { | |
46 if (descriptor > 0) { | |
47 if (created) | |
48 *created = false; | |
49 } else { | |
50 open_flags |= O_CREAT; | |
51 descriptor = open(WideToUTF8(name).c_str(), open_flags, | |
52 S_IRUSR | S_IWUSR); | |
53 if (created && descriptor > 0) | |
54 *created = true; | |
55 } | |
56 } | |
57 | |
58 return descriptor; | |
59 } | |
60 | |
61 } // namespace disk_cache | |
OLD | NEW |