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