blob: 4f0222d44f0004ea01d829c6d1c977dc484cd898 [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
Patrik Höglunda8005cf2017-12-13 16:05:42 +010013#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
26#if defined(WEBRTC_WIN)
27const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
28
29FILE* FdopenPlatformFileForWriting(PlatformFile file) {
30 if (file == kInvalidPlatformFileValue)
deadbeef37f5ecf2017-02-27 14:06:41 -080031 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000032 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
33 if (fd < 0)
deadbeef37f5ecf2017-02-27 14:06:41 -080034 return nullptr;
xians@webrtc.orge46bc772014-10-10 08:36:56 +000035
36 return _fdopen(fd, "w");
37}
38
39bool ClosePlatformFile(PlatformFile file) {
40 return CloseHandle(file) != 0;
41}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020042
43bool RemoveFile(const std::string& path) {
44 return ::DeleteFile(ToUtf16(path).c_str()) != 0;
45}
46
47PlatformFile OpenPlatformFile(const std::string& path) {
48 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
49 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
50}
51
52PlatformFile CreatePlatformFile(const std::string& path) {
53 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
54 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
55}
56
57#else // defined(WEBRTC_WIN)
58
xians@webrtc.orge46bc772014-10-10 08:36:56 +000059const PlatformFile kInvalidPlatformFileValue = -1;
60
61FILE* FdopenPlatformFileForWriting(PlatformFile file) {
62 return fdopen(file, "w");
63}
64
65bool ClosePlatformFile(PlatformFile file) {
66 return close(file);
67}
Viktor Palmkvist971eb272016-09-16 10:19:23 +020068
69bool RemoveFile(const std::string& path) {
70 return ::unlink(path.c_str()) == 0;
71}
72
73PlatformFile OpenPlatformFile(const std::string& path) {
74 return ::open(path.c_str(), O_RDWR);
75}
76
77PlatformFile CreatePlatformFile(const std::string& path) {
78 return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
79}
80
xians@webrtc.orge46bc772014-10-10 08:36:56 +000081#endif
82
83} // namespace rtc