OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/system_wrappers/source/file_impl.h" | 11 #include "webrtc/system_wrappers/include/file_wrapper.h" |
12 | |
13 #include <assert.h> | |
14 | 12 |
15 #ifdef _WIN32 | 13 #ifdef _WIN32 |
16 #include <Windows.h> | 14 #include <Windows.h> |
17 #else | 15 #else |
18 #include <stdarg.h> | 16 #include <stdarg.h> |
19 #include <string.h> | 17 #include <string.h> |
20 #endif | 18 #endif |
21 | 19 |
22 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
23 #include "webrtc/system_wrappers/include/rw_lock_wrapper.h" | |
24 | 21 |
25 namespace webrtc { | 22 namespace webrtc { |
| 23 namespace { |
| 24 FILE* FileOpen(const char* file_name_utf8, bool read_only) { |
| 25 #if defined(_WIN32) |
| 26 int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0); |
| 27 std::wstring wstr(len, 0); |
| 28 MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len); |
| 29 FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb"); |
| 30 #else |
| 31 FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb"); |
| 32 #endif |
| 33 return file; |
| 34 } |
| 35 } // namespace |
26 | 36 |
| 37 // static |
27 FileWrapper* FileWrapper::Create() { | 38 FileWrapper* FileWrapper::Create() { |
28 return new FileWrapperImpl(); | 39 return new FileWrapper(); |
29 } | 40 } |
30 | 41 |
31 FileWrapperImpl::FileWrapperImpl() | 42 // static |
32 : rw_lock_(RWLockWrapper::CreateRWLock()), | 43 FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) { |
33 id_(NULL), | 44 return FileWrapper(FileOpen(file_name_utf8, read_only), 0); |
34 managed_file_handle_(true), | |
35 open_(false), | |
36 looping_(false), | |
37 read_only_(false), | |
38 max_size_in_bytes_(0), | |
39 size_in_bytes_(0) { | |
40 memset(file_name_utf8_, 0, kMaxFileNameSize); | |
41 } | 45 } |
42 | 46 |
43 FileWrapperImpl::~FileWrapperImpl() { | 47 FileWrapper::FileWrapper() {} |
44 if (id_ != NULL && managed_file_handle_) { | 48 |
45 fclose(id_); | 49 FileWrapper::FileWrapper(FILE* file, size_t max_size) |
46 } | 50 : file_(file), max_size_in_bytes_(max_size) {} |
| 51 |
| 52 FileWrapper::~FileWrapper() { |
| 53 CloseFileImpl(); |
47 } | 54 } |
48 | 55 |
49 int FileWrapperImpl::CloseFile() { | 56 FileWrapper::FileWrapper(FileWrapper&& other) { |
50 WriteLockScoped write(*rw_lock_); | 57 operator=(std::move(other)); |
51 return CloseFileImpl(); | |
52 } | 58 } |
53 | 59 |
54 int FileWrapperImpl::Rewind() { | 60 FileWrapper& FileWrapper::operator=(FileWrapper&& other) { |
55 WriteLockScoped write(*rw_lock_); | 61 file_ = other.file_; |
56 if (looping_ || !read_only_) { | 62 max_size_in_bytes_ = other.max_size_in_bytes_; |
57 if (id_ != NULL) { | 63 position_ = other.position_; |
58 size_in_bytes_ = 0; | 64 other.file_ = nullptr; |
59 return fseek(id_, 0, SEEK_SET); | 65 return *this; |
60 } | 66 } |
| 67 |
| 68 void FileWrapper::CloseFile() { |
| 69 rtc::CritScope lock(&lock_); |
| 70 CloseFileImpl(); |
| 71 } |
| 72 |
| 73 int FileWrapper::Rewind() { |
| 74 rtc::CritScope lock(&lock_); |
| 75 if (file_ != nullptr) { |
| 76 position_ = 0; |
| 77 return fseek(file_, 0, SEEK_SET); |
61 } | 78 } |
62 return -1; | 79 return -1; |
63 } | 80 } |
64 | 81 |
65 int FileWrapperImpl::SetMaxFileSize(size_t bytes) { | 82 int FileWrapper::SetMaxFileSize(size_t bytes) { |
66 WriteLockScoped write(*rw_lock_); | 83 rtc::CritScope lock(&lock_); |
67 max_size_in_bytes_ = bytes; | 84 max_size_in_bytes_ = bytes; |
68 return 0; | 85 return 0; |
69 } | 86 } |
70 | 87 |
71 int FileWrapperImpl::Flush() { | 88 int FileWrapper::Flush() { |
72 WriteLockScoped write(*rw_lock_); | 89 rtc::CritScope lock(&lock_); |
73 return FlushImpl(); | 90 return FlushImpl(); |
74 } | 91 } |
75 | 92 |
76 int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const { | 93 int FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) { |
77 ReadLockScoped read(*rw_lock_); | 94 size_t length = strlen(file_name_utf8); |
78 size_t length = strlen(file_name_utf8_); | 95 if (length > kMaxFileNameSize - 1) |
79 if (length > kMaxFileNameSize) { | |
80 assert(false); | |
81 return -1; | 96 return -1; |
82 } | 97 |
83 if (length < 1) { | 98 rtc::CritScope lock(&lock_); |
| 99 if (file_ != nullptr) |
84 return -1; | 100 return -1; |
85 } | |
86 | 101 |
87 // Make sure to NULL terminate | 102 file_ = FileOpen(file_name_utf8, read_only); |
88 if (size < length) { | 103 return file_ == nullptr ? -1 : 0; |
89 length = size - 1; | 104 } |
90 } | 105 |
91 memcpy(file_name_utf8, file_name_utf8_, length); | 106 int FileWrapper::OpenFromFileHandle(FILE* handle) { |
92 file_name_utf8[length] = 0; | 107 if (!handle) |
| 108 return -1; |
| 109 rtc::CritScope lock(&lock_); |
| 110 CloseFileImpl(); |
| 111 file_ = handle; |
93 return 0; | 112 return 0; |
94 } | 113 } |
95 | 114 |
96 bool FileWrapperImpl::Open() const { | 115 int FileWrapper::Read(void* buf, size_t length) { |
97 ReadLockScoped read(*rw_lock_); | 116 rtc::CritScope lock(&lock_); |
98 return open_; | 117 if (file_ == nullptr) |
99 } | |
100 | |
101 int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only, | |
102 bool loop, bool text) { | |
103 WriteLockScoped write(*rw_lock_); | |
104 if (id_ != NULL && !managed_file_handle_) | |
105 return -1; | |
106 size_t length = strlen(file_name_utf8); | |
107 if (length > kMaxFileNameSize - 1) { | |
108 return -1; | |
109 } | |
110 | |
111 read_only_ = read_only; | |
112 | |
113 FILE* tmp_id = NULL; | |
114 #if defined _WIN32 | |
115 wchar_t wide_file_name[kMaxFileNameSize]; | |
116 wide_file_name[0] = 0; | |
117 | |
118 MultiByteToWideChar(CP_UTF8, | |
119 0, // UTF8 flag | |
120 file_name_utf8, | |
121 -1, // Null terminated string | |
122 wide_file_name, | |
123 kMaxFileNameSize); | |
124 if (text) { | |
125 if (read_only) { | |
126 tmp_id = _wfopen(wide_file_name, L"rt"); | |
127 } else { | |
128 tmp_id = _wfopen(wide_file_name, L"wt"); | |
129 } | |
130 } else { | |
131 if (read_only) { | |
132 tmp_id = _wfopen(wide_file_name, L"rb"); | |
133 } else { | |
134 tmp_id = _wfopen(wide_file_name, L"wb"); | |
135 } | |
136 } | |
137 #else | |
138 if (text) { | |
139 if (read_only) { | |
140 tmp_id = fopen(file_name_utf8, "rt"); | |
141 } else { | |
142 tmp_id = fopen(file_name_utf8, "wt"); | |
143 } | |
144 } else { | |
145 if (read_only) { | |
146 tmp_id = fopen(file_name_utf8, "rb"); | |
147 } else { | |
148 tmp_id = fopen(file_name_utf8, "wb"); | |
149 } | |
150 } | |
151 #endif | |
152 | |
153 if (tmp_id != NULL) { | |
154 // +1 comes from copying the NULL termination character. | |
155 memcpy(file_name_utf8_, file_name_utf8, length + 1); | |
156 if (id_ != NULL) { | |
157 fclose(id_); | |
158 } | |
159 id_ = tmp_id; | |
160 managed_file_handle_ = true; | |
161 looping_ = loop; | |
162 open_ = true; | |
163 return 0; | |
164 } | |
165 return -1; | |
166 } | |
167 | |
168 int FileWrapperImpl::OpenFromFileHandle(FILE* handle, | |
169 bool manage_file, | |
170 bool read_only, | |
171 bool loop) { | |
172 WriteLockScoped write(*rw_lock_); | |
173 if (!handle) | |
174 return -1; | 118 return -1; |
175 | 119 |
176 if (id_ != NULL) { | 120 size_t bytes_read = fread(buf, 1, length, file_); |
177 if (managed_file_handle_) | |
178 fclose(id_); | |
179 else | |
180 return -1; | |
181 } | |
182 | |
183 id_ = handle; | |
184 managed_file_handle_ = manage_file; | |
185 read_only_ = read_only; | |
186 looping_ = loop; | |
187 open_ = true; | |
188 return 0; | |
189 } | |
190 | |
191 int FileWrapperImpl::Read(void* buf, size_t length) { | |
192 WriteLockScoped write(*rw_lock_); | |
193 if (id_ == NULL) | |
194 return -1; | |
195 | |
196 size_t bytes_read = fread(buf, 1, length, id_); | |
197 if (bytes_read != length && !looping_) { | |
198 CloseFileImpl(); | |
199 } | |
200 return static_cast<int>(bytes_read); | 121 return static_cast<int>(bytes_read); |
201 } | 122 } |
202 | 123 |
203 int FileWrapperImpl::WriteText(const char* format, ...) { | 124 bool FileWrapper::Write(const void* buf, size_t length) { |
204 WriteLockScoped write(*rw_lock_); | 125 if (buf == nullptr) |
205 if (format == NULL) | |
206 return -1; | |
207 | |
208 if (read_only_) | |
209 return -1; | |
210 | |
211 if (id_ == NULL) | |
212 return -1; | |
213 | |
214 va_list args; | |
215 va_start(args, format); | |
216 int num_chars = vfprintf(id_, format, args); | |
217 va_end(args); | |
218 | |
219 if (num_chars >= 0) { | |
220 return num_chars; | |
221 } else { | |
222 CloseFileImpl(); | |
223 return -1; | |
224 } | |
225 } | |
226 | |
227 bool FileWrapperImpl::Write(const void* buf, size_t length) { | |
228 WriteLockScoped write(*rw_lock_); | |
229 if (buf == NULL) | |
230 return false; | 126 return false; |
231 | 127 |
232 if (read_only_) | 128 rtc::CritScope lock(&lock_); |
233 return false; | |
234 | 129 |
235 if (id_ == NULL) | 130 if (file_ == nullptr) |
236 return false; | 131 return false; |
237 | 132 |
238 // Check if it's time to stop writing. | 133 // Check if it's time to stop writing. |
239 if (max_size_in_bytes_ > 0 && | 134 if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_) |
240 (size_in_bytes_ + length) > max_size_in_bytes_) { | |
241 FlushImpl(); | |
242 return false; | 135 return false; |
243 } | |
244 | 136 |
245 size_t num_bytes = fwrite(buf, 1, length, id_); | 137 size_t num_bytes = fwrite(buf, 1, length, file_); |
246 size_in_bytes_ += num_bytes; | 138 position_ += num_bytes; |
247 if (num_bytes != length) { | 139 |
248 CloseFileImpl(); | 140 return num_bytes == length; |
249 return false; | |
250 } | |
251 return true; | |
252 } | 141 } |
253 | 142 |
254 int FileWrapperImpl::CloseFileImpl() { | 143 void FileWrapper::CloseFileImpl() { |
255 if (id_ != NULL) { | 144 if (file_ != nullptr) |
256 if (managed_file_handle_) | 145 fclose(file_); |
257 fclose(id_); | 146 file_ = nullptr; |
258 id_ = NULL; | |
259 } | |
260 memset(file_name_utf8_, 0, kMaxFileNameSize); | |
261 open_ = false; | |
262 return 0; | |
263 } | 147 } |
264 | 148 |
265 int FileWrapperImpl::FlushImpl() { | 149 int FileWrapper::FlushImpl() { |
266 if (id_ != NULL) { | 150 return (file_ != nullptr) ? fflush(file_) : -1; |
267 return fflush(id_); | |
268 } | |
269 return -1; | |
270 } | |
271 | |
272 int FileWrapper::Rewind() { | |
273 RTC_DCHECK(false); | |
274 return -1; | |
275 } | 151 } |
276 | 152 |
277 } // namespace webrtc | 153 } // namespace webrtc |
OLD | NEW |