xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/platform_file.h" |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 12 | |
Oleh Prypin | 96a0f61 | 2018-10-05 13:13:38 +0000 | [diff] [blame] | 13 | #include "rtc_base/stringutils.h" |
| 14 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 15 | #if defined(WEBRTC_WIN) |
| 16 | #include <io.h> |
| 17 | #else |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #endif |
| 23 | |
| 24 | namespace rtc { |
| 25 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 26 | FILE* FdopenPlatformFileForWriting(PlatformFile file) { |
| 27 | return FdopenPlatformFile(file, "w"); |
| 28 | } |
| 29 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 30 | #if defined(WEBRTC_WIN) |
| 31 | const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 32 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 33 | FILE* FdopenPlatformFile(PlatformFile file, const char* modes) { |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 34 | if (file == kInvalidPlatformFileValue) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 35 | return nullptr; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 36 | int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0); |
| 37 | if (fd < 0) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 38 | return nullptr; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 39 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 40 | return _fdopen(fd, modes); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool ClosePlatformFile(PlatformFile file) { |
| 44 | return CloseHandle(file) != 0; |
| 45 | } |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 46 | |
| 47 | bool RemoveFile(const std::string& path) { |
| 48 | return ::DeleteFile(ToUtf16(path).c_str()) != 0; |
| 49 | } |
| 50 | |
| 51 | PlatformFile OpenPlatformFile(const std::string& path) { |
| 52 | return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 53 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 54 | } |
| 55 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 56 | PlatformFile OpenPlatformFileReadOnly(const std::string& path) { |
| 57 | return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 58 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 59 | } |
| 60 | |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 61 | PlatformFile CreatePlatformFile(const std::string& path) { |
| 62 | return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 63 | nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 64 | } |
| 65 | |
| 66 | #else // defined(WEBRTC_WIN) |
| 67 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 68 | const PlatformFile kInvalidPlatformFileValue = -1; |
| 69 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 70 | FILE* FdopenPlatformFile(PlatformFile file, const char* modes) { |
| 71 | return fdopen(file, modes); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool ClosePlatformFile(PlatformFile file) { |
Karl Wiberg | 0404225 | 2018-02-22 11:41:25 +0100 | [diff] [blame] | 75 | return close(file) == 0; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 76 | } |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 77 | |
| 78 | bool RemoveFile(const std::string& path) { |
| 79 | return ::unlink(path.c_str()) == 0; |
| 80 | } |
| 81 | |
| 82 | PlatformFile OpenPlatformFile(const std::string& path) { |
| 83 | return ::open(path.c_str(), O_RDWR); |
| 84 | } |
| 85 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 86 | PlatformFile OpenPlatformFileReadOnly(const std::string& path) { |
| 87 | return ::open(path.c_str(), O_RDONLY); |
| 88 | } |
| 89 | |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 90 | PlatformFile CreatePlatformFile(const std::string& path) { |
| 91 | return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); |
| 92 | } |
| 93 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
| 96 | } // namespace rtc |