blob: 89a918514ac4967841c8a879deaa15870058e302 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrike@webrtc.org143abd92012-02-08 19:39:38 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
phoglund@webrtc.org740be442012-12-12 12:52:15 +000011#include "webrtc/system_wrappers/source/file_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
andrew@webrtc.org114c7902011-12-10 02:33:33 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15#ifdef _WIN32
andrew@webrtc.org114c7902011-12-10 02:33:33 +000016#include <Windows.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017#else
andrew@webrtc.org114c7902011-12-10 02:33:33 +000018#include <stdarg.h>
19#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000020#endif
21
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000022#include "webrtc/base/checks.h"
phoglund@webrtc.org740be442012-12-12 12:52:15 +000023#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000026
phoglund@webrtc.org740be442012-12-12 12:52:15 +000027FileWrapper* FileWrapper::Create() {
28 return new FileWrapperImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000029}
30
31FileWrapperImpl::FileWrapperImpl()
phoglund@webrtc.org740be442012-12-12 12:52:15 +000032 : rw_lock_(RWLockWrapper::CreateRWLock()),
33 id_(NULL),
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000034 managed_file_handle_(true),
phoglund@webrtc.org740be442012-12-12 12:52:15 +000035 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
phoglund@webrtc.org740be442012-12-12 12:52:15 +000043FileWrapperImpl::~FileWrapperImpl() {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000044 if (id_ != NULL && managed_file_handle_) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000045 fclose(id_);
46 }
47}
48
49int FileWrapperImpl::CloseFile() {
50 WriteLockScoped write(*rw_lock_);
51 return CloseFileImpl();
52}
53
54int FileWrapperImpl::Rewind() {
55 WriteLockScoped write(*rw_lock_);
56 if (looping_ || !read_only_) {
57 if (id_ != NULL) {
58 size_in_bytes_ = 0;
59 return fseek(id_, 0, SEEK_SET);
niklase@google.com470e71d2011-07-07 08:21:25 +000060 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +000061 }
62 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
phoglund@webrtc.org740be442012-12-12 12:52:15 +000065int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
66 WriteLockScoped write(*rw_lock_);
67 max_size_in_bytes_ = bytes;
68 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
phoglund@webrtc.org740be442012-12-12 12:52:15 +000071int FileWrapperImpl::Flush() {
72 WriteLockScoped write(*rw_lock_);
73 return FlushImpl();
74}
75
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000076int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000077 ReadLockScoped read(*rw_lock_);
78 size_t length = strlen(file_name_utf8_);
79 if (length > kMaxFileNameSize) {
80 assert(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000081 return -1;
phoglund@webrtc.org740be442012-12-12 12:52:15 +000082 }
83 if (length < 1) {
84 return -1;
85 }
86
87 // Make sure to NULL terminate
88 if (size < length) {
89 length = size - 1;
90 }
91 memcpy(file_name_utf8, file_name_utf8_, length);
92 file_name_utf8[length] = 0;
93 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
phoglund@webrtc.org740be442012-12-12 12:52:15 +000096bool FileWrapperImpl::Open() const {
97 ReadLockScoped read(*rw_lock_);
98 return open_;
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000101int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
102 bool loop, bool text) {
103 WriteLockScoped write(*rw_lock_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000104 if (id_ != NULL && !managed_file_handle_)
105 return -1;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000106 size_t length = strlen(file_name_utf8);
107 if (length > kMaxFileNameSize - 1) {
108 return -1;
109 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000111 read_only_ = read_only;
andrew@webrtc.org986fab12011-12-15 19:11:41 +0000112
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000113 FILE* tmp_id = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114#if defined _WIN32
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000115 wchar_t wide_file_name[kMaxFileNameSize];
116 wide_file_name[0] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000118 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");
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 } else {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000128 tmp_id = _wfopen(wide_file_name, L"wt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000130 } 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000137#else
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000138 if (text) {
139 if (read_only) {
140 tmp_id = fopen(file_name_utf8, "rt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 } else {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000142 tmp_id = fopen(file_name_utf8, "wt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000144 } 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000151#endif
152
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000153 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_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000159 id_ = tmp_id;
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000160 managed_file_handle_ = true;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000161 looping_ = loop;
162 open_ = true;
163 return 0;
164 }
165 return -1;
166}
167
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000168int 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;
175
176 if (id_ != NULL) {
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
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000191int FileWrapperImpl::Read(void* buf, size_t length) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000192 WriteLockScoped write(*rw_lock_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000193 if (id_ == NULL)
194 return -1;
andrew@webrtc.org114c7902011-12-10 02:33:33 +0000195
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000196 size_t bytes_read = fread(buf, 1, length, id_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000197 if (bytes_read != length && !looping_) {
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000198 CloseFileImpl();
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000199 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000200 return static_cast<int>(bytes_read);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000201}
202
203int FileWrapperImpl::WriteText(const char* format, ...) {
204 WriteLockScoped write(*rw_lock_);
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
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000227bool FileWrapperImpl::Write(const void* buf, size_t length) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000228 WriteLockScoped write(*rw_lock_);
229 if (buf == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 return false;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000231
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000232 if (read_only_)
233 return false;
234
235 if (id_ == NULL)
236 return false;
237
238 // Check if it's time to stop writing.
239 if (max_size_in_bytes_ > 0 &&
240 (size_in_bytes_ + length) > max_size_in_bytes_) {
241 FlushImpl();
242 return false;
243 }
244
245 size_t num_bytes = fwrite(buf, 1, length, id_);
246 if (num_bytes > 0) {
247 size_in_bytes_ += num_bytes;
248 return true;
249 }
250
251 CloseFileImpl();
252 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000254
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000255int FileWrapperImpl::CloseFileImpl() {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000256 if (id_ != NULL) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000257 if (managed_file_handle_)
258 fclose(id_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000259 id_ = NULL;
260 }
261 memset(file_name_utf8_, 0, kMaxFileNameSize);
262 open_ = false;
263 return 0;
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000264}
265
266int FileWrapperImpl::FlushImpl() {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000267 if (id_ != NULL) {
268 return fflush(id_);
269 }
270 return -1;
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000271}
272
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000273int FileWrapper::Rewind() {
henrikg91d6ede2015-09-17 00:24:34 -0700274 RTC_DCHECK(false);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000275 return -1;
276}
277
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000278} // namespace webrtc