Tweak/Wontfix a few tests that we could care less about.
[chromium-blink-merge.git] / base / platform_file_posix.cc
blob46039b980ffe2ba788786ada360ba0c21e3f49b3
1 // Copyright (c) 2009 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.
5 #include "base/platform_file.h"
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <sys/stat.h>
11 #include "base/file_path.h"
12 #include "base/logging.h"
13 #include "base/utf_string_conversions.h"
15 namespace base {
17 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
18 PlatformFile CreatePlatformFile(const FilePath& name, int flags,
19 bool* created) {
20 int open_flags = 0;
21 if (flags & PLATFORM_FILE_CREATE)
22 open_flags = O_CREAT | O_EXCL;
24 if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
25 DCHECK(!open_flags);
26 open_flags = O_CREAT | O_TRUNC;
29 if (!open_flags && !(flags & PLATFORM_FILE_OPEN) &&
30 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
31 NOTREACHED();
32 errno = EOPNOTSUPP;
33 return kInvalidPlatformFileValue;
36 if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
37 open_flags |= O_RDWR;
38 } else if (flags & PLATFORM_FILE_WRITE) {
39 open_flags |= O_WRONLY;
40 } else if (!(flags & PLATFORM_FILE_READ)) {
41 NOTREACHED();
44 DCHECK(O_RDONLY == 0);
46 int descriptor = open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR);
48 if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
49 if (descriptor > 0) {
50 if (created)
51 *created = false;
52 } else {
53 open_flags |= O_CREAT;
54 if (flags & PLATFORM_FILE_EXCLUSIVE_READ ||
55 flags & PLATFORM_FILE_EXCLUSIVE_WRITE) {
56 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
58 descriptor = open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR);
59 if (created && descriptor > 0)
60 *created = true;
64 if ((descriptor > 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) {
65 unlink(name.value().c_str());
68 return descriptor;
71 PlatformFile CreatePlatformFile(const std::wstring& name, int flags,
72 bool* created) {
73 return CreatePlatformFile(FilePath::FromWStringHack(name), flags, created);
76 bool ClosePlatformFile(PlatformFile file) {
77 return close(file);
80 } // namespace base