blob: d74acddd02e3a882dd782ec50483ace5ffec95b2 [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 Gerey2e00abc2018-10-05 15:39:24 +020015#include "rtc_base/stringutils.h" // For ToUtf16
xians@webrtc.orge46bc772014-10-10 08:36:56 +000016#else
Viktor Palmkvist971eb272016-09-16 10:19:23 +020017#include <fcntl.h>
18#include <sys/stat.h>
19#include <sys/types.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) {
47 return ::DeleteFile(ToUtf16(path).c_str()) != 0;
48}
49
50PlatformFile OpenPlatformFile(const std::string& path) {
51 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
52 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
53}
54
Artem Titove62f6002018-03-19 15:40:00 +010055PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
56 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ, FILE_SHARE_READ,
57 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
58}
59
Viktor Palmkvist971eb272016-09-16 10:19:23 +020060PlatformFile CreatePlatformFile(const std::string& path) {
61 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
62 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
63}
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