ipc/chromium/src/base/platform_file_posix.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial