Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 17 | #include "file_handler.hpp" |
| 18 | |
Patrick Venture | 2bd7021 | 2019-03-08 13:14:08 -0800 | [diff] [blame] | 19 | #include <filesystem> |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 20 | #include <ios> |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 21 | #include <optional> |
| 22 | #include <utility> |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 25 | namespace ipmi_flash |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 26 | { |
| 27 | |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 28 | bool FileHandler::open(const std::string& path, std::ios_base::openmode mode) |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 29 | { |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 30 | /* force binary mode */ |
| 31 | mode |= std::ios::binary; |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 32 | this->path = path; |
| 33 | |
| 34 | if (file.is_open()) |
| 35 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 36 | return true; |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 37 | } |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 38 | file.open(filename, mode); |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 39 | return file.good(); |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void FileHandler::close() |
| 43 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 44 | file.close(); |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool FileHandler::write(std::uint32_t offset, |
| 48 | const std::vector<std::uint8_t>& data) |
| 49 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 50 | file.seekp(offset); |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 51 | file.write(reinterpret_cast<const char*>(data.data()), data.size()); |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 52 | return file.good(); |
Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 55 | std::optional<std::vector<uint8_t>> |
| 56 | FileHandler::read(std::uint32_t offset, std::uint32_t size) |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 57 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 58 | uint32_t file_size = getSize(); |
| 59 | if (offset > file_size) |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 60 | { |
| 61 | return std::nullopt; |
| 62 | } |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 63 | std::vector<uint8_t> ret(std::min(file_size - offset, size)); |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 64 | file.seekg(offset); |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 65 | file.read(reinterpret_cast<char*>(ret.data()), ret.size()); |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 66 | if (!file.good()) |
| 67 | { |
| 68 | return std::nullopt; |
| 69 | } |
Willy Tu | df82a46 | 2021-10-25 09:49:40 -0700 | [diff] [blame] | 70 | return ret; |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 73 | int FileHandler::getSize() |
| 74 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 75 | std::error_code ec; |
| 76 | auto ret = std::filesystem::file_size(filename, ec); |
| 77 | if (ec) |
Patrick Venture | 86b49e2 | 2018-11-15 14:29:00 -0800 | [diff] [blame] | 78 | { |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 79 | auto error = ec.message(); |
| 80 | std::fprintf(stderr, "Failed to get filesize `%s`: %s\n", |
| 81 | filename.c_str(), error.c_str()); |
| 82 | return 0; |
Patrick Venture | 86b49e2 | 2018-11-15 14:29:00 -0800 | [diff] [blame] | 83 | } |
William A. Kennington III | ba90fd7 | 2021-10-22 00:33:52 -0700 | [diff] [blame] | 84 | return ret; |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 87 | } // namespace ipmi_flash |