| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/platform_file.h" | 5 #include "base/platform_file.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 | 10 |
| 11 #include "base/file_path.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here? | 17 // TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here? |
| 17 PlatformFile CreatePlatformFile(const std::wstring& name, | 18 PlatformFile CreatePlatformFile(const FilePath& name, int flags, |
| 18 int flags, | |
| 19 bool* created) { | 19 bool* created) { |
| 20 int open_flags = 0; | 20 int open_flags = 0; |
| 21 if (flags & PLATFORM_FILE_CREATE) | 21 if (flags & PLATFORM_FILE_CREATE) |
| 22 open_flags = O_CREAT | O_EXCL; | 22 open_flags = O_CREAT | O_EXCL; |
| 23 | 23 |
| 24 if (flags & PLATFORM_FILE_CREATE_ALWAYS) { | 24 if (flags & PLATFORM_FILE_CREATE_ALWAYS) { |
| 25 DCHECK(!open_flags); | 25 DCHECK(!open_flags); |
| 26 open_flags = O_CREAT | O_TRUNC; | 26 open_flags = O_CREAT | O_TRUNC; |
| 27 } | 27 } |
| 28 | 28 |
| 29 if (!open_flags && !(flags & PLATFORM_FILE_OPEN) && | 29 if (!open_flags && !(flags & PLATFORM_FILE_OPEN) && |
| 30 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) { | 30 !(flags & PLATFORM_FILE_OPEN_ALWAYS)) { |
| 31 NOTREACHED(); | 31 NOTREACHED(); |
| 32 errno = EOPNOTSUPP; | 32 errno = EOPNOTSUPP; |
| 33 return kInvalidPlatformFileValue; | 33 return kInvalidPlatformFileValue; |
| 34 } | 34 } |
| 35 | 35 |
| 36 if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) { | 36 if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) { |
| 37 open_flags |= O_RDWR; | 37 open_flags |= O_RDWR; |
| 38 } else if (flags & PLATFORM_FILE_WRITE) { | 38 } else if (flags & PLATFORM_FILE_WRITE) { |
| 39 open_flags |= O_WRONLY; | 39 open_flags |= O_WRONLY; |
| 40 } else if (!(flags & PLATFORM_FILE_READ)) { | 40 } else if (!(flags & PLATFORM_FILE_READ)) { |
| 41 NOTREACHED(); | 41 NOTREACHED(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DCHECK(O_RDONLY == 0); | 44 DCHECK(O_RDONLY == 0); |
| 45 | 45 |
| 46 int descriptor = open(WideToUTF8(name).c_str(), open_flags, | 46 int descriptor = open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR); |
| 47 S_IRUSR | S_IWUSR); | |
| 48 | 47 |
| 49 if (flags & PLATFORM_FILE_OPEN_ALWAYS) { | 48 if (flags & PLATFORM_FILE_OPEN_ALWAYS) { |
| 50 if (descriptor > 0) { | 49 if (descriptor > 0) { |
| 51 if (created) | 50 if (created) |
| 52 *created = false; | 51 *created = false; |
| 53 } else { | 52 } else { |
| 54 open_flags |= O_CREAT; | 53 open_flags |= O_CREAT; |
| 55 if (flags & PLATFORM_FILE_EXCLUSIVE_READ || | 54 if (flags & PLATFORM_FILE_EXCLUSIVE_READ || |
| 56 flags & PLATFORM_FILE_EXCLUSIVE_WRITE) { | 55 flags & PLATFORM_FILE_EXCLUSIVE_WRITE) { |
| 57 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW | 56 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW |
| 58 } | 57 } |
| 59 descriptor = open(WideToUTF8(name).c_str(), open_flags, | 58 descriptor = open(name.value().c_str(), open_flags, S_IRUSR | S_IWUSR); |
| 60 S_IRUSR | S_IWUSR); | |
| 61 if (created && descriptor > 0) | 59 if (created && descriptor > 0) |
| 62 *created = true; | 60 *created = true; |
| 63 } | 61 } |
| 64 } | 62 } |
| 65 | 63 |
| 66 if ((descriptor > 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) { | 64 if ((descriptor > 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) { |
| 67 unlink(WideToUTF8(name).c_str()); | 65 unlink(name.value().c_str()); |
| 68 } | 66 } |
| 69 | 67 |
| 70 return descriptor; | 68 return descriptor; |
| 71 } | 69 } |
| 72 | 70 |
| 71 PlatformFile CreatePlatformFile(const std::wstring& name, int flags, |
| 72 bool* created) { |
| 73 return CreatePlatformFile(FilePath::FromWStringHack(name), flags, created); |
| 74 } |
| 75 |
| 73 bool ClosePlatformFile(PlatformFile file) { | 76 bool ClosePlatformFile(PlatformFile file) { |
| 74 return close(file); | 77 return close(file); |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace base | 80 } // namespace base |
| OLD | NEW |