| 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 void 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; | |
| 69 } | 85 } |
| 70 | 86 |
| 71 int FileWrapperImpl::Flush() { | 87 int FileWrapper::Flush() { |
| 72 WriteLockScoped write(*rw_lock_); | 88 rtc::CritScope lock(&lock_); |
| 73 return FlushImpl(); | 89 return FlushImpl(); |
| 74 } | 90 } |
| 75 | 91 |
| 76 int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const { | 92 bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) { |
| 77 ReadLockScoped read(*rw_lock_); | 93 size_t length = strlen(file_name_utf8); |
| 78 size_t length = strlen(file_name_utf8_); | 94 if (length > kMaxFileNameSize - 1) |
| 79 if (length > kMaxFileNameSize) { | 95 return false; |
| 80 assert(false); | |
| 81 return -1; | |
| 82 } | |
| 83 if (length < 1) { | |
| 84 return -1; | |
| 85 } | |
| 86 | 96 |
| 87 // Make sure to NULL terminate | 97 rtc::CritScope lock(&lock_); |
| 88 if (size < length) { | 98 if (file_ != nullptr) |
| 89 length = size - 1; | 99 return false; |
| 90 } | 100 |
| 91 memcpy(file_name_utf8, file_name_utf8_, length); | 101 file_ = FileOpen(file_name_utf8, read_only); |
| 92 file_name_utf8[length] = 0; | 102 return file_ != nullptr; |
| 93 return 0; | |
| 94 } | 103 } |
| 95 | 104 |
| 96 bool FileWrapperImpl::Open() const { | 105 bool FileWrapper::OpenFromFileHandle(FILE* handle) { |
| 97 ReadLockScoped read(*rw_lock_); | 106 if (!handle) |
| 98 return open_; | 107 return false; |
| 108 rtc::CritScope lock(&lock_); |
| 109 CloseFileImpl(); |
| 110 file_ = handle; |
| 111 return true; |
| 99 } | 112 } |
| 100 | 113 |
| 101 int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only, | 114 int FileWrapper::Read(void* buf, size_t length) { |
| 102 bool loop, bool text) { | 115 rtc::CritScope lock(&lock_); |
| 103 WriteLockScoped write(*rw_lock_); | 116 if (file_ == nullptr) |
| 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; | 117 return -1; |
| 175 | 118 |
| 176 if (id_ != NULL) { | 119 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); | 120 return static_cast<int>(bytes_read); |
| 201 } | 121 } |
| 202 | 122 |
| 203 int FileWrapperImpl::WriteText(const char* format, ...) { | 123 bool FileWrapper::Write(const void* buf, size_t length) { |
| 204 WriteLockScoped write(*rw_lock_); | 124 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; | 125 return false; |
| 231 | 126 |
| 232 if (read_only_) | 127 rtc::CritScope lock(&lock_); |
| 233 return false; | |
| 234 | 128 |
| 235 if (id_ == NULL) | 129 if (file_ == nullptr) |
| 236 return false; | 130 return false; |
| 237 | 131 |
| 238 // Check if it's time to stop writing. | 132 // Check if it's time to stop writing. |
| 239 if (max_size_in_bytes_ > 0 && | 133 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; | 134 return false; |
| 243 } | |
| 244 | 135 |
| 245 size_t num_bytes = fwrite(buf, 1, length, id_); | 136 size_t num_bytes = fwrite(buf, 1, length, file_); |
| 246 size_in_bytes_ += num_bytes; | 137 position_ += num_bytes; |
| 247 if (num_bytes != length) { | 138 |
| 248 CloseFileImpl(); | 139 return num_bytes == length; |
| 249 return false; | |
| 250 } | |
| 251 return true; | |
| 252 } | 140 } |
| 253 | 141 |
| 254 int FileWrapperImpl::CloseFileImpl() { | 142 void FileWrapper::CloseFileImpl() { |
| 255 if (id_ != NULL) { | 143 if (file_ != nullptr) |
| 256 if (managed_file_handle_) | 144 fclose(file_); |
| 257 fclose(id_); | 145 file_ = nullptr; |
| 258 id_ = NULL; | |
| 259 } | |
| 260 memset(file_name_utf8_, 0, kMaxFileNameSize); | |
| 261 open_ = false; | |
| 262 return 0; | |
| 263 } | 146 } |
| 264 | 147 |
| 265 int FileWrapperImpl::FlushImpl() { | 148 int FileWrapper::FlushImpl() { |
| 266 if (id_ != NULL) { | 149 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 } | 150 } |
| 276 | 151 |
| 277 } // namespace webrtc | 152 } // namespace webrtc |
| OLD | NEW |