michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/platform_file.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef ANDROID michael@0: #include michael@0: #endif michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/string_util.h" michael@0: michael@0: namespace base { michael@0: michael@0: // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here? michael@0: PlatformFile CreatePlatformFile(const std::wstring& name, michael@0: int flags, michael@0: bool* created) { michael@0: int open_flags = 0; michael@0: if (flags & PLATFORM_FILE_CREATE) michael@0: open_flags = O_CREAT | O_EXCL; michael@0: michael@0: if (flags & PLATFORM_FILE_CREATE_ALWAYS) { michael@0: DCHECK(!open_flags); michael@0: open_flags = O_CREAT | O_TRUNC; michael@0: } michael@0: michael@0: if (!open_flags && !(flags & PLATFORM_FILE_OPEN) && michael@0: !(flags & PLATFORM_FILE_OPEN_ALWAYS)) { michael@0: NOTREACHED(); michael@0: errno = ENOTSUP; michael@0: return kInvalidPlatformFileValue; michael@0: } michael@0: michael@0: if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) { michael@0: open_flags |= O_RDWR; michael@0: } else if (flags & PLATFORM_FILE_WRITE) { michael@0: open_flags |= O_WRONLY; michael@0: } else if (!(flags & PLATFORM_FILE_READ)) { michael@0: NOTREACHED(); michael@0: } michael@0: michael@0: DCHECK(O_RDONLY == 0); michael@0: michael@0: int descriptor = open(WideToUTF8(name).c_str(), open_flags, michael@0: S_IRUSR | S_IWUSR); michael@0: michael@0: if (flags & PLATFORM_FILE_OPEN_ALWAYS) { michael@0: if (descriptor > 0) { michael@0: if (created) michael@0: *created = false; michael@0: } else { michael@0: open_flags |= O_CREAT; michael@0: descriptor = open(WideToUTF8(name).c_str(), open_flags, michael@0: S_IRUSR | S_IWUSR); michael@0: if (created && descriptor > 0) michael@0: *created = true; michael@0: } michael@0: } michael@0: michael@0: return descriptor; michael@0: } michael@0: michael@0: } // namespace base