niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 143abd9 | 2012-02-08 19:39:38 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 | |
Karl Wiberg | 6a4d411 | 2018-03-23 09:39:34 | [diff] [blame] | 11 | #include "rtc_base/system/file_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
Harald Alvestrand | ecc38d8 | 2023-10-24 09:04:31 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 15 | #include <cerrno> |
Harald Alvestrand | ecc38d8 | 2023-10-24 09:04:31 | [diff] [blame] | 16 | #include <cstdint> |
| 17 | #include <string> |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 18 | |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Harald Alvestrand | ecc38d8 | 2023-10-24 09:04:31 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_conversions.h" |
| 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 23 | #ifdef _WIN32 |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 | [diff] [blame] | 24 | #include <Windows.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 25 | #else |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 26 | #endif |
| 27 | |
Karl Wiberg | 6a4d411 | 2018-03-23 09:39:34 | [diff] [blame] | 28 | #include <utility> |
| 29 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 30 | namespace webrtc { |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 31 | namespace { |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 32 | FILE* FileOpen(absl::string_view file_name_utf8, bool read_only, int* error) { |
| 33 | RTC_CHECK_EQ(file_name_utf8.find_first_of('\0'), absl::string_view::npos) |
| 34 | << "Invalid filename, containing NUL character"; |
Ali Tofigh | 98bfd99 | 2022-07-22 20:10:49 | [diff] [blame] | 35 | std::string file_name(file_name_utf8); |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 36 | #if defined(_WIN32) |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 37 | int len = MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, nullptr, 0); |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 38 | std::wstring wstr(len, 0); |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 39 | MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, &wstr[0], len); |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 40 | FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 41 | #else |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 42 | FILE* file = fopen(file_name.c_str(), read_only ? "rb" : "wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 43 | #endif |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 44 | if (!file && error) { |
| 45 | *error = errno; |
| 46 | } |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 47 | return file; |
| 48 | } |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 49 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 50 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 51 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 52 | // static |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 53 | FileWrapper FileWrapper::OpenReadOnly(absl::string_view file_name_utf8) { |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 54 | return FileWrapper(FileOpen(file_name_utf8, true, nullptr)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 | [diff] [blame] | 55 | } |
| 56 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 57 | // static |
Ali Tofigh | 2ab914c | 2022-04-13 10:55:15 | [diff] [blame] | 58 | FileWrapper FileWrapper::OpenWriteOnly(absl::string_view file_name_utf8, |
Niels Möller | 23213d9 | 2019-01-22 10:01:24 | [diff] [blame] | 59 | int* error /*=nullptr*/) { |
| 60 | return FileWrapper(FileOpen(file_name_utf8, false, error)); |
| 61 | } |
| 62 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 63 | FileWrapper::FileWrapper(FileWrapper&& other) { |
| 64 | operator=(std::move(other)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 | [diff] [blame] | 65 | } |
| 66 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 67 | FileWrapper& FileWrapper::operator=(FileWrapper&& other) { |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 68 | Close(); |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 69 | file_ = other.file_; |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 70 | other.file_ = nullptr; |
| 71 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 72 | } |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 | [diff] [blame] | 73 | |
Niels Möller | 7ba3b81 | 2019-08-06 07:58:56 | [diff] [blame] | 74 | bool FileWrapper::SeekRelative(int64_t offset) { |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 75 | RTC_DCHECK(file_); |
Niels Möller | 7ba3b81 | 2019-08-06 07:58:56 | [diff] [blame] | 76 | return fseek(file_, rtc::checked_cast<long>(offset), SEEK_CUR) == 0; |
| 77 | } |
| 78 | |
| 79 | bool FileWrapper::SeekTo(int64_t position) { |
| 80 | RTC_DCHECK(file_); |
| 81 | return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0; |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 | [diff] [blame] | 82 | } |
| 83 | |
Björn Terelius | cbd6156 | 2021-03-25 14:52:16 | [diff] [blame] | 84 | long FileWrapper::FileSize() { |
| 85 | if (file_ == nullptr) |
| 86 | return -1; |
| 87 | long original_position = ftell(file_); |
| 88 | if (original_position < 0) |
| 89 | return -1; |
| 90 | int seek_error = fseek(file_, 0, SEEK_END); |
| 91 | if (seek_error) |
| 92 | return -1; |
| 93 | long file_size = ftell(file_); |
| 94 | seek_error = fseek(file_, original_position, SEEK_SET); |
| 95 | if (seek_error) |
| 96 | return -1; |
| 97 | return file_size; |
| 98 | } |
| 99 | |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 100 | bool FileWrapper::Flush() { |
| 101 | RTC_DCHECK(file_); |
| 102 | return fflush(file_) == 0; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 103 | } |
| 104 | |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 105 | size_t FileWrapper::Read(void* buf, size_t length) { |
| 106 | RTC_DCHECK(file_); |
| 107 | return fread(buf, 1, length, file_); |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 108 | } |
| 109 | |
Niels Möller | 7ba3b81 | 2019-08-06 07:58:56 | [diff] [blame] | 110 | bool FileWrapper::ReadEof() const { |
| 111 | RTC_DCHECK(file_); |
| 112 | return feof(file_); |
| 113 | } |
| 114 | |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 115 | bool FileWrapper::Write(const void* buf, size_t length) { |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 116 | RTC_DCHECK(file_); |
| 117 | return fwrite(buf, 1, length, file_) == length; |
| 118 | } |
Niels Moller | 4664727 | 2019-01-18 12:04:43 | [diff] [blame] | 119 | |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 120 | bool FileWrapper::Close() { |
Niels Moller | 4664727 | 2019-01-18 12:04:43 | [diff] [blame] | 121 | if (file_ == nullptr) |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 122 | return true; |
Niels Moller | 4664727 | 2019-01-18 12:04:43 | [diff] [blame] | 123 | |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 124 | bool success = fclose(file_) == 0; |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 125 | file_ = nullptr; |
Niels Möller | 5a6ae02 | 2019-01-21 10:59:10 | [diff] [blame] | 126 | return success; |
tommi | a6219cc | 2016-06-15 17:30:14 | [diff] [blame] | 127 | } |
| 128 | |
Per Åhgren | b8a9630 | 2020-05-07 13:58:55 | [diff] [blame] | 129 | FILE* FileWrapper::Release() { |
| 130 | FILE* file = file_; |
| 131 | file_ = nullptr; |
| 132 | return file; |
| 133 | } |
| 134 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 135 | } // namespace webrtc |