niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 143abd9 | 2012-02-08 19:39:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/file_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
| 15 | #ifdef _WIN32 |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 16 | #include <Windows.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | #else |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 22 | #include "webrtc/base/checks.h" |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 23 | #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h" |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 24 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | namespace webrtc { |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 26 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 27 | FileWrapper* FileWrapper::Create() { |
| 28 | return new FileWrapperImpl(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | FileWrapperImpl::FileWrapperImpl() |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 32 | : rw_lock_(RWLockWrapper::CreateRWLock()), |
| 33 | id_(NULL), |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 34 | managed_file_handle_(true), |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 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); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 43 | FileWrapperImpl::~FileWrapperImpl() { |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 44 | if (id_ != NULL && managed_file_handle_) { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 45 | fclose(id_); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int FileWrapperImpl::CloseFile() { |
| 50 | WriteLockScoped write(*rw_lock_); |
| 51 | return CloseFileImpl(); |
| 52 | } |
| 53 | |
| 54 | int 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 61 | } |
| 62 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 65 | int FileWrapperImpl::SetMaxFileSize(size_t bytes) { |
| 66 | WriteLockScoped write(*rw_lock_); |
| 67 | max_size_in_bytes_ = bytes; |
| 68 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 71 | int FileWrapperImpl::Flush() { |
| 72 | WriteLockScoped write(*rw_lock_); |
| 73 | return FlushImpl(); |
| 74 | } |
| 75 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 76 | int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 77 | ReadLockScoped read(*rw_lock_); |
| 78 | size_t length = strlen(file_name_utf8_); |
| 79 | if (length > kMaxFileNameSize) { |
| 80 | assert(false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | return -1; |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 82 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 96 | bool FileWrapperImpl::Open() const { |
| 97 | ReadLockScoped read(*rw_lock_); |
| 98 | return open_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 101 | int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only, |
| 102 | bool loop, bool text) { |
| 103 | WriteLockScoped write(*rw_lock_); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 104 | if (id_ != NULL && !managed_file_handle_) |
| 105 | return -1; |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 106 | size_t length = strlen(file_name_utf8); |
| 107 | if (length > kMaxFileNameSize - 1) { |
| 108 | return -1; |
| 109 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 111 | read_only_ = read_only; |
andrew@webrtc.org | 986fab1 | 2011-12-15 19:11:41 +0000 | [diff] [blame] | 112 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 113 | FILE* tmp_id = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | #if defined _WIN32 |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 115 | wchar_t wide_file_name[kMaxFileNameSize]; |
| 116 | wide_file_name[0] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 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"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | } else { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 128 | tmp_id = _wfopen(wide_file_name, L"wt"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | } |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 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 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | #else |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 138 | if (text) { |
| 139 | if (read_only) { |
| 140 | tmp_id = fopen(file_name_utf8, "rt"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } else { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 142 | tmp_id = fopen(file_name_utf8, "wt"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 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 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 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_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | } |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 159 | id_ = tmp_id; |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 160 | managed_file_handle_ = true; |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 161 | looping_ = loop; |
| 162 | open_ = true; |
| 163 | return 0; |
| 164 | } |
| 165 | return -1; |
| 166 | } |
| 167 | |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 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; |
| 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.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 191 | int FileWrapperImpl::Read(void* buf, size_t length) { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 192 | WriteLockScoped write(*rw_lock_); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 193 | if (id_ == NULL) |
| 194 | return -1; |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 195 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 196 | size_t bytes_read = fread(buf, 1, length, id_); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 197 | if (bytes_read != length && !looping_) { |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 198 | CloseFileImpl(); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 199 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 200 | return static_cast<int>(bytes_read); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int 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.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 227 | bool FileWrapperImpl::Write(const void* buf, size_t length) { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 228 | WriteLockScoped write(*rw_lock_); |
| 229 | if (buf == NULL) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | return false; |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 231 | |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 232 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 254 | |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 255 | int FileWrapperImpl::CloseFileImpl() { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 256 | if (id_ != NULL) { |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 257 | if (managed_file_handle_) |
| 258 | fclose(id_); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 259 | id_ = NULL; |
| 260 | } |
| 261 | memset(file_name_utf8_, 0, kMaxFileNameSize); |
| 262 | open_ = false; |
| 263 | return 0; |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int FileWrapperImpl::FlushImpl() { |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 267 | if (id_ != NULL) { |
| 268 | return fflush(id_); |
| 269 | } |
| 270 | return -1; |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 271 | } |
| 272 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 273 | int FileWrapper::Rewind() { |
| 274 | DCHECK(false); |
| 275 | return -1; |
| 276 | } |
| 277 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 278 | } // namespace webrtc |