Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : #include "base/platform_file.h"
8 :
9 : #include <sys/stat.h>
10 : #include <fcntl.h>
11 : #include <errno.h>
12 : #ifdef ANDROID
13 : #include <linux/stat.h>
14 : #endif
15 :
16 : #include "base/logging.h"
17 : #include "base/string_util.h"
18 :
19 : namespace base {
20 :
21 : // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
22 0 : PlatformFile CreatePlatformFile(const std::wstring& name,
23 : int flags,
24 : bool* created) {
25 0 : int open_flags = 0;
26 0 : if (flags & PLATFORM_FILE_CREATE)
27 0 : open_flags = O_CREAT | O_EXCL;
28 :
29 0 : if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
30 0 : DCHECK(!open_flags);
31 0 : open_flags = O_CREAT | O_TRUNC;
32 : }
33 :
34 0 : if (!open_flags && !(flags & PLATFORM_FILE_OPEN) &&
35 0 : !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
36 0 : NOTREACHED();
37 0 : errno = ENOTSUP;
38 0 : return kInvalidPlatformFileValue;
39 : }
40 :
41 0 : if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
42 0 : open_flags |= O_RDWR;
43 0 : } else if (flags & PLATFORM_FILE_WRITE) {
44 0 : open_flags |= O_WRONLY;
45 0 : } else if (!(flags & PLATFORM_FILE_READ)) {
46 0 : NOTREACHED();
47 : }
48 :
49 : DCHECK(O_RDONLY == 0);
50 :
51 0 : int descriptor = open(WideToUTF8(name).c_str(), open_flags,
52 0 : S_IRUSR | S_IWUSR);
53 :
54 0 : if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
55 0 : if (descriptor > 0) {
56 0 : if (created)
57 0 : *created = false;
58 : } else {
59 0 : open_flags |= O_CREAT;
60 0 : descriptor = open(WideToUTF8(name).c_str(), open_flags,
61 0 : S_IRUSR | S_IWUSR);
62 0 : if (created && descriptor > 0)
63 0 : *created = true;
64 : }
65 : }
66 :
67 0 : return descriptor;
68 : }
69 :
70 : } // namespace base
|