blob: 871e75f86785c78c52b8a33a78b151341fa6b581 [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>
15#else
Viktor Palmkvist971eb272016-09-16 10:19:23 +020016#include <fcntl.h>
17#include <sys/stat.h>
18#include <sys/types.h>
xians@webrtc.orge46bc772014-10-10 08:36:56 +000019#include <unistd.h>
20#endif
21
22namespace rtc {
23
24#if defined(WEBRTC_WIN)
25const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
26
27FILE* FdopenPlatformFileForWriting(PlatformFile file) {
28 if (file == kInvalidPlatformFileValue)
deadbeef37f5ecf2017-02-27 14:06:41 -080029 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000030 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
31 if (fd < 0)
deadbeef37f5ecf2017-02-27 14:06:41 -080032 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000033
34 return _fdopen(fd, "w");
35}
36
37bool ClosePlatformFile(PlatformFile file) {
38 return CloseHandle(file) != 0;
39}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020040
41bool RemoveFile(const std::string& path) {
42 return ::DeleteFile(ToUtf16(path).c_str()) != 0;
43}
44
45PlatformFile OpenPlatformFile(const std::string& path) {
46 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
47 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
48}
49
50PlatformFile CreatePlatformFile(const std::string& path) {
51 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
52 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
53}
54
55#else // defined(WEBRTC_WIN)
56
xians@webrtc.orge46bc772014-10-10 08:36:56 +000057const PlatformFile kInvalidPlatformFileValue = -1;
58
59FILE* FdopenPlatformFileForWriting(PlatformFile file) {
60 return fdopen(file, "w");
61}
62
63bool ClosePlatformFile(PlatformFile file) {
64 return close(file);
65}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020066
67bool RemoveFile(const std::string& path) {
68 return ::unlink(path.c_str()) == 0;
69}
70
71PlatformFile OpenPlatformFile(const std::string& path) {
72 return ::open(path.c_str(), O_RDWR);
73}
74
75PlatformFile CreatePlatformFile(const std::string& path) {
76 return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
77}
78
xians@webrtc.orge46bc772014-10-10 08:36:56 +000079#endif
80
81} // namespace rtc