/* * Copyright 2018 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "file_handler.hpp" #include #include #include #include #include namespace ipmi_flash { bool FileHandler::open(const std::string& path, std::ios_base::openmode mode) { /* force binary mode */ mode |= std::ios::binary; this->path = path; if (file.is_open()) { return true; } file.open(filename, mode); return file.good(); } void FileHandler::close() { file.close(); } bool FileHandler::write(std::uint32_t offset, const std::vector& data) { file.seekp(offset); file.write(reinterpret_cast(data.data()), data.size()); return file.good(); } std::optional> FileHandler::read(std::uint32_t offset, std::uint32_t size) { uint32_t file_size = getSize(); if (offset > file_size) { return std::nullopt; } std::vector ret(std::min(file_size - offset, size)); file.seekg(offset); file.read(reinterpret_cast(ret.data()), ret.size()); if (!file.good()) { return std::nullopt; } return ret; } int FileHandler::getSize() { std::error_code ec; auto ret = std::filesystem::file_size(filename, ec); if (ec) { auto error = ec.message(); std::fprintf(stderr, "Failed to get filesize `%s`: %s\n", filename.c_str(), error.c_str()); return 0; } return ret; } } // namespace ipmi_flash