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 "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: PlatformFile CreatePlatformFile(const std::wstring& name, michael@0: int flags, michael@0: bool* created) { michael@0: DWORD disposition = 0; michael@0: michael@0: if (flags & PLATFORM_FILE_OPEN) michael@0: disposition = OPEN_EXISTING; michael@0: michael@0: if (flags & PLATFORM_FILE_CREATE) { michael@0: DCHECK(!disposition); michael@0: disposition = CREATE_NEW; michael@0: } michael@0: michael@0: if (flags & PLATFORM_FILE_OPEN_ALWAYS) { michael@0: DCHECK(!disposition); michael@0: disposition = OPEN_ALWAYS; michael@0: } michael@0: michael@0: if (flags & PLATFORM_FILE_CREATE_ALWAYS) { michael@0: DCHECK(!disposition); michael@0: disposition = CREATE_ALWAYS; michael@0: } michael@0: michael@0: if (!disposition) { michael@0: NOTREACHED(); michael@0: return NULL; michael@0: } michael@0: michael@0: DWORD access = (flags & PLATFORM_FILE_READ) ? GENERIC_READ : 0; michael@0: if (flags & PLATFORM_FILE_WRITE) michael@0: access |= GENERIC_WRITE; michael@0: michael@0: DWORD sharing = (flags & PLATFORM_FILE_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; michael@0: if (!(flags & PLATFORM_FILE_EXCLUSIVE_WRITE)) michael@0: sharing |= FILE_SHARE_WRITE; michael@0: michael@0: DWORD create_flags = 0; michael@0: if (flags & PLATFORM_FILE_ASYNC) michael@0: create_flags |= FILE_FLAG_OVERLAPPED; michael@0: michael@0: HANDLE file = CreateFile(name.c_str(), access, sharing, NULL, disposition, michael@0: create_flags, NULL); michael@0: michael@0: if ((flags & PLATFORM_FILE_OPEN_ALWAYS) && created && michael@0: INVALID_HANDLE_VALUE != file) { michael@0: *created = (ERROR_ALREADY_EXISTS != GetLastError()); michael@0: } michael@0: michael@0: return file; michael@0: } michael@0: michael@0: } // namespace disk_cache