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"
11 #include "base/file_path.h"
12 #include "base/logging.h"
13 #include "base/utf_string_conversions.h"
17 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
18 PlatformFile
CreatePlatformFile(const FilePath
& name
, int flags
,
21 if (flags
& PLATFORM_FILE_CREATE
)
22 open_flags
= O_CREAT
| O_EXCL
;
24 if (flags
& PLATFORM_FILE_CREATE_ALWAYS
) {
26 open_flags
= O_CREAT
| O_TRUNC
;
29 if (!open_flags
&& !(flags
& PLATFORM_FILE_OPEN
) &&
30 !(flags
& PLATFORM_FILE_OPEN_ALWAYS
)) {
33 return kInvalidPlatformFileValue
;
36 if (flags
& PLATFORM_FILE_WRITE
&& flags
& PLATFORM_FILE_READ
) {
38 } else if (flags
& PLATFORM_FILE_WRITE
) {
39 open_flags
|= O_WRONLY
;
40 } else if (!(flags
& PLATFORM_FILE_READ
)) {
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
) {
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)
64 if ((descriptor
> 0) && (flags
& PLATFORM_FILE_DELETE_ON_CLOSE
)) {
65 unlink(name
.value().c_str());
71 PlatformFile
CreatePlatformFile(const std::wstring
& name
, int flags
,
73 return CreatePlatformFile(FilePath::FromWStringHack(name
), flags
, created
);
76 bool ClosePlatformFile(PlatformFile file
) {