blob: a4c906a1ed028adf6db0bff2528407b90b9bccc9 [file] [log] [blame]
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/platform_file.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000012
Oleh Prypin96a0f612018-10-05 13:13:38 +000013#include "rtc_base/stringutils.h"
14
xians@webrtc.orge46bc772014-10-10 08:36:56 +000015#if defined(WEBRTC_WIN)
16#include <io.h>
17#else
Viktor Palmkvist971eb272016-09-16 10:19:23 +020018#include <fcntl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
xians@webrtc.orge46bc772014-10-10 08:36:56 +000021#include <unistd.h>
22#endif
23
24namespace rtc {
25
Artem Titove62f6002018-03-19 15:40:00 +010026FILE* FdopenPlatformFileForWriting(PlatformFile file) {
27 return FdopenPlatformFile(file, "w");
28}
29
xians@webrtc.orge46bc772014-10-10 08:36:56 +000030#if defined(WEBRTC_WIN)
31const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
32
Artem Titove62f6002018-03-19 15:40:00 +010033FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
xians@webrtc.orge46bc772014-10-10 08:36:56 +000034 if (file == kInvalidPlatformFileValue)
deadbeef37f5ecf2017-02-27 14:06:41 -080035 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000036 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
37 if (fd < 0)
deadbeef37f5ecf2017-02-27 14:06:41 -080038 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000039
Artem Titove62f6002018-03-19 15:40:00 +010040 return _fdopen(fd, modes);
xians@webrtc.orge46bc772014-10-10 08:36:56 +000041}
42
43bool ClosePlatformFile(PlatformFile file) {
44 return CloseHandle(file) != 0;
45}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020046
47bool RemoveFile(const std::string& path) {
48 return ::DeleteFile(ToUtf16(path).c_str()) != 0;
49}
50
51PlatformFile 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 Titove62f6002018-03-19 15:40:00 +010056PlatformFile 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 Palmkvist971eb272016-09-16 10:19:23 +020061PlatformFile 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.orge46bc772014-10-10 08:36:56 +000068const PlatformFile kInvalidPlatformFileValue = -1;
69
Artem Titove62f6002018-03-19 15:40:00 +010070FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
71 return fdopen(file, modes);
xians@webrtc.orge46bc772014-10-10 08:36:56 +000072}
73
74bool ClosePlatformFile(PlatformFile file) {
Karl Wiberg04042252018-02-22 11:41:25 +010075 return close(file) == 0;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000076}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020077
78bool RemoveFile(const std::string& path) {
79 return ::unlink(path.c_str()) == 0;
80}
81
82PlatformFile OpenPlatformFile(const std::string& path) {
83 return ::open(path.c_str(), O_RDWR);
84}
85
Artem Titove62f6002018-03-19 15:40:00 +010086PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
87 return ::open(path.c_str(), O_RDONLY);
88}
89
Viktor Palmkvist971eb272016-09-16 10:19:23 +020090PlatformFile 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.orge46bc772014-10-10 08:36:56 +000094#endif
95
96} // namespace rtc