1 /*
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
11 #include "rtc_base/platform_file.h"
12
13 #if defined(WEBRTC_WIN)
14 #define UNICODE
15 #include <io.h>
16
17 #include "rtc_base/stringutils.h" // For ToUtf16
18 #else
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #endif
23
24 namespace rtc {
25
FdopenPlatformFileForWriting(PlatformFile file)26 FILE* FdopenPlatformFileForWriting(PlatformFile file) {
27 return FdopenPlatformFile(file, "w");
28 }
29
30 #if defined(WEBRTC_WIN)
31 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
32
FdopenPlatformFile(PlatformFile file,const char * modes)33 FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
34 if (file == kInvalidPlatformFileValue)
35 return nullptr;
36 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
37 if (fd < 0)
38 return nullptr;
39
40 return _fdopen(fd, modes);
41 }
42
ClosePlatformFile(PlatformFile file)43 bool ClosePlatformFile(PlatformFile file) {
44 return CloseHandle(file) != 0;
45 }
46
RemoveFile(const std::string & path)47 bool RemoveFile(const std::string& path) {
48 #if defined(WEBRTC_UWP)
49 return kInvalidPlatformFileValue;
50 #else // defined(WEBRTC_WIN)
51 return ::DeleteFile(ToUtf16(path).c_str()) != 0;
52 #endif
53 }
54
OpenPlatformFile(const std::string & path)55 PlatformFile OpenPlatformFile(const std::string& path) {
56 #if defined(WEBRTC_UWP)
57 return kInvalidPlatformFileValue;
58 #else // defined(WEBRTC_WIN)
59 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
60 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
61 #endif // defined(WEBRTC_WIN)
62 }
63
OpenPlatformFileReadOnly(const std::string & path)64 PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
65 #if defined(WEBRTC_UWP)
66 return kInvalidPlatformFileValue;
67 #else // defined(WEBRTC_WIN)
68 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ, FILE_SHARE_READ,
69 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
70 #endif // defined(WEBRTC_WIN)
71 }
72
CreatePlatformFile(const std::string & path)73 PlatformFile CreatePlatformFile(const std::string& path) {
74 #if defined(WEBRTC_UWP)
75 return kInvalidPlatformFileValue;
76 #else // defined(WEBRTC_WIN)
77 return ::CreateFile(ToUtf16(path).c_str(), GENERIC_READ | GENERIC_WRITE, 0,
78 nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
79 #endif // defined(WEBRTC_WIN)
80 }
81
82 #else // defined(WEBRTC_WIN)
83
84 const PlatformFile kInvalidPlatformFileValue = -1;
85
FdopenPlatformFile(PlatformFile file,const char * modes)86 FILE* FdopenPlatformFile(PlatformFile file, const char* modes) {
87 return fdopen(file, modes);
88 }
89
ClosePlatformFile(PlatformFile file)90 bool ClosePlatformFile(PlatformFile file) {
91 return close(file) == 0;
92 }
93
RemoveFile(const std::string & path)94 bool RemoveFile(const std::string& path) {
95 return ::unlink(path.c_str()) == 0;
96 }
97
OpenPlatformFile(const std::string & path)98 PlatformFile OpenPlatformFile(const std::string& path) {
99 return ::open(path.c_str(), O_RDWR);
100 }
101
OpenPlatformFileReadOnly(const std::string & path)102 PlatformFile OpenPlatformFileReadOnly(const std::string& path) {
103 return ::open(path.c_str(), O_RDONLY);
104 }
105
CreatePlatformFile(const std::string & path)106 PlatformFile CreatePlatformFile(const std::string& path) {
107 return ::open(path.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
108 }
109
110 #endif
111
112 } // namespace rtc
113