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 | |
| 13 | #if defined(WEBRTC_WIN) |
| 14 | #include <io.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/string_utils.h" // For ToUtf16 |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 17 | #else |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <sys/stat.h> |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 25 | FILE* FdopenPlatformFileForWriting(PlatformFile file) { |
| 26 | return FdopenPlatformFile(file, "w"); |
| 27 | } |
| 28 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 29 | #if defined(WEBRTC_WIN) |
| 30 | const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 31 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 32 | FILE* FdopenPlatformFile(PlatformFile file, const char* modes) { |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 33 | if (file == kInvalidPlatformFileValue) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 34 | return nullptr; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 35 | int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0); |
| 36 | if (fd < 0) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 37 | return nullptr; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 38 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 39 | return _fdopen(fd, modes); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool ClosePlatformFile(PlatformFile file) { |
| 43 | return CloseHandle(file) != 0; |
| 44 | } |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 45 | |
| 46 | bool RemoveFile(const std::string& path) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame^] | 47 | return ::DeleteFileW(ToUtf16(path).c_str()) != 0; |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | PlatformFile OpenPlatformFile(const std::string& path) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame^] | 51 | return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 52 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 55 | PlatformFile OpenPlatformFileReadOnly(const std::string& path) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame^] | 56 | return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 57 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 60 | PlatformFile CreatePlatformFile(const std::string& path) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame^] | 61 | return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 62 | nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | #else // defined(WEBRTC_WIN) |
| 66 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 67 | const PlatformFile kInvalidPlatformFileValue = -1; |
| 68 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 69 | FILE* FdopenPlatformFile(PlatformFile file, const char* modes) { |
| 70 | return fdopen(file, modes); |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool ClosePlatformFile(PlatformFile file) { |
Karl Wiberg | 0404225 | 2018-02-22 11:41:25 +0100 | [diff] [blame] | 74 | return close(file) == 0; |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 75 | } |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 76 | |
| 77 | bool RemoveFile(const std::string& path) { |
| 78 | return ::unlink(path.c_str()) == 0; |
| 79 | } |
| 80 | |
| 81 | PlatformFile OpenPlatformFile(const std::string& path) { |
| 82 | return ::open(path.c_str(), O_RDWR); |
| 83 | } |
| 84 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 85 | PlatformFile OpenPlatformFileReadOnly(const std::string& path) { |
| 86 | return ::open(path.c_str(), O_RDONLY); |
| 87 | } |
| 88 | |
Viktor Palmkvist | 971eb27 | 2016-09-16 10:19:23 +0200 | [diff] [blame] | 89 | PlatformFile CreatePlatformFile(const std::string& path) { |
| 90 | return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); |
| 91 | } |
| 92 | |
xians@webrtc.org | e46bc77 | 2014-10-10 08:36:56 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | } // namespace rtc |