blob: e4f8b5e4924513c0d6a34512cb2b930a76257ac9 [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
13#if defined(WEBRTC_WIN)
14#include <io.h>
Yves Gerey988cc082018-10-23 12:03:01 +020015
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/string_utils.h" // For ToUtf16
xians@webrtc.orge46bc772014-10-10 08:36:56 +000017#else
Viktor Palmkvist971eb272016-09-16 10:19:23 +020018#include <fcntl.h>
19#include <sys/stat.h>
xians@webrtc.orge46bc772014-10-10 08:36:56 +000020#include <unistd.h>
21#endif
22
23namespace rtc {
24
Artem Titove62f6002018-03-19 15:40:00 +010025FILE* FdopenPlatformFileForWriting(PlatformFile file) {
26 return FdopenPlatformFile(file, "w");
27}
28
xians@webrtc.orge46bc772014-10-10 08:36:56 +000029#if defined(WEBRTC_WIN)
30const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
31
Artem Titove62f6002018-03-19 15:40:00 +010032FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
xians@webrtc.orge46bc772014-10-10 08:36:56 +000033 if (file == kInvalidPlatformFileValue)
deadbeef37f5ecf2017-02-27 14:06:41 -080034 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000035 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
36 if (fd < 0)
deadbeef37f5ecf2017-02-27 14:06:41 -080037 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000038
Artem Titove62f6002018-03-19 15:40:00 +010039 return _fdopen(fd, modes);
xians@webrtc.orge46bc772014-10-10 08:36:56 +000040}
41
42bool ClosePlatformFile(PlatformFile file) {
43 return CloseHandle(file) != 0;
44}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020045
46bool RemoveFile(const std::string& path) {
Mirko Bonadei673f7e52019-03-25 09:01:02 +010047 return ::DeleteFileW(ToUtf16(path).c_str()) != 0;
Viktor Palmkvist971eb272016-09-16 10:19:23 +020048}
49
50PlatformFile OpenPlatformFile(const std::string& path) {
Mirko Bonadei673f7e52019-03-25 09:01:02 +010051 return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
52 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
Viktor Palmkvist971eb272016-09-16 10:19:23 +020053}
54
Artem Titove62f6002018-03-19 15:40:00 +010055PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
Mirko Bonadei673f7e52019-03-25 09:01:02 +010056 return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ, FILE_SHARE_READ,
57 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
Artem Titove62f6002018-03-19 15:40:00 +010058}
59
Viktor Palmkvist971eb272016-09-16 10:19:23 +020060PlatformFile CreatePlatformFile(const std::string& path) {
Mirko Bonadei673f7e52019-03-25 09:01:02 +010061 return ::CreateFileW(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
62 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
Viktor Palmkvist971eb272016-09-16 10:19:23 +020063}
64
65#else // defined(WEBRTC_WIN)
66
xians@webrtc.orge46bc772014-10-10 08:36:56 +000067const PlatformFile kInvalidPlatformFileValue = -1;
68
Artem Titove62f6002018-03-19 15:40:00 +010069FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
70 return fdopen(file, modes);
xians@webrtc.orge46bc772014-10-10 08:36:56 +000071}
72
73bool ClosePlatformFile(PlatformFile file) {
Karl Wiberg04042252018-02-22 11:41:25 +010074 return close(file) == 0;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000075}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020076
77bool RemoveFile(const std::string& path) {
78 return ::unlink(path.c_str()) == 0;
79}
80
81PlatformFile OpenPlatformFile(const std::string& path) {
82 return ::open(path.c_str(), O_RDWR);
83}
84
Artem Titove62f6002018-03-19 15:40:00 +010085PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
86 return ::open(path.c_str(), O_RDONLY);
87}
88
Viktor Palmkvist971eb272016-09-16 10:19:23 +020089PlatformFile 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.orge46bc772014-10-10 08:36:56 +000093#endif
94
95} // namespace rtc