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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "system_wrappers/include/file_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #ifdef _WIN32 |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 14 | #include <Windows.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #else |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 16 | #include <stdarg.h> |
| 17 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 20 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | namespace webrtc { |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 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"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | #else |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 31 | FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | #endif |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 33 | return file; |
| 34 | } |
| 35 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 37 | // static |
| 38 | FileWrapper* FileWrapper::Create() { |
| 39 | return new FileWrapper(); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 42 | // static |
| 43 | FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) { |
| 44 | return FileWrapper(FileOpen(file_name_utf8, read_only), 0); |
henrikg@webrtc.org | 863b536 | 2013-12-06 16:05:17 +0000 | [diff] [blame] | 45 | } |
| 46 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 47 | FileWrapper::FileWrapper() {} |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 48 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 49 | FileWrapper::FileWrapper(FILE* file, size_t max_size) |
| 50 | : file_(file), max_size_in_bytes_(max_size) {} |
| 51 | |
| 52 | FileWrapper::~FileWrapper() { |
| 53 | CloseFileImpl(); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 54 | } |
| 55 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 56 | FileWrapper::FileWrapper(FileWrapper&& other) { |
| 57 | operator=(std::move(other)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 58 | } |
| 59 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 60 | FileWrapper& FileWrapper::operator=(FileWrapper&& other) { |
| 61 | file_ = other.file_; |
| 62 | max_size_in_bytes_ = other.max_size_in_bytes_; |
| 63 | position_ = other.position_; |
| 64 | other.file_ = nullptr; |
| 65 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 67 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 68 | void FileWrapper::CloseFile() { |
| 69 | rtc::CritScope lock(&lock_); |
| 70 | CloseFileImpl(); |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 71 | } |
| 72 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 73 | int FileWrapper::Rewind() { |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 74 | rtc::CritScope lock(&lock_); |
| 75 | if (file_ != nullptr) { |
| 76 | position_ = 0; |
| 77 | return fseek(file_, 0, SEEK_SET); |
| 78 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 82 | void FileWrapper::SetMaxFileSize(size_t bytes) { |
| 83 | rtc::CritScope lock(&lock_); |
| 84 | max_size_in_bytes_ = bytes; |
| 85 | } |
| 86 | |
| 87 | int FileWrapper::Flush() { |
| 88 | rtc::CritScope lock(&lock_); |
| 89 | return FlushImpl(); |
| 90 | } |
| 91 | |
| 92 | bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) { |
| 93 | size_t length = strlen(file_name_utf8); |
| 94 | if (length > kMaxFileNameSize - 1) |
| 95 | return false; |
| 96 | |
| 97 | rtc::CritScope lock(&lock_); |
| 98 | if (file_ != nullptr) |
| 99 | return false; |
| 100 | |
| 101 | file_ = FileOpen(file_name_utf8, read_only); |
| 102 | return file_ != nullptr; |
| 103 | } |
| 104 | |
| 105 | bool FileWrapper::OpenFromFileHandle(FILE* handle) { |
| 106 | if (!handle) |
| 107 | return false; |
| 108 | rtc::CritScope lock(&lock_); |
| 109 | CloseFileImpl(); |
| 110 | file_ = handle; |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | int FileWrapper::Read(void* buf, size_t length) { |
| 115 | rtc::CritScope lock(&lock_); |
| 116 | if (file_ == nullptr) |
| 117 | return -1; |
| 118 | |
| 119 | size_t bytes_read = fread(buf, 1, length, file_); |
| 120 | return static_cast<int>(bytes_read); |
| 121 | } |
| 122 | |
| 123 | bool FileWrapper::Write(const void* buf, size_t length) { |
| 124 | if (buf == nullptr) |
| 125 | return false; |
| 126 | |
| 127 | rtc::CritScope lock(&lock_); |
| 128 | |
| 129 | if (file_ == nullptr) |
| 130 | return false; |
| 131 | |
| 132 | // Check if it's time to stop writing. |
| 133 | if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_) |
| 134 | return false; |
| 135 | |
| 136 | size_t num_bytes = fwrite(buf, 1, length, file_); |
| 137 | position_ += num_bytes; |
| 138 | |
| 139 | return num_bytes == length; |
| 140 | } |
| 141 | |
| 142 | void FileWrapper::CloseFileImpl() { |
| 143 | if (file_ != nullptr) |
| 144 | fclose(file_); |
| 145 | file_ = nullptr; |
| 146 | } |
| 147 | |
| 148 | int FileWrapper::FlushImpl() { |
| 149 | return (file_ != nullptr) ? fflush(file_) : -1; |
| 150 | } |
| 151 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 152 | } // namespace webrtc |