blob: 8005b8ff0fb0d94ebc0bb354c820cc58081a227e [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
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 Venture64919ec2018-11-15 11:52:07 -080017#include "file_handler.hpp"
18
Patrick Venture2bd70212019-03-08 13:14:08 -080019#include <filesystem>
Jason Ling56a22732020-10-23 19:53:17 -070020#include <ios>
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070021#include <optional>
22#include <utility>
Patrick Venture64919ec2018-11-15 11:52:07 -080023#include <vector>
24
Patrick Venture1d5a31c2019-05-20 11:38:22 -070025namespace ipmi_flash
Patrick Venture64919ec2018-11-15 11:52:07 -080026{
27
Jason Ling56a22732020-10-23 19:53:17 -070028bool FileHandler::open(const std::string& path, std::ios_base::openmode mode)
Patrick Venture64919ec2018-11-15 11:52:07 -080029{
Jason Ling56a22732020-10-23 19:53:17 -070030 /* force binary mode */
31 mode |= std::ios::binary;
Patrick Venture64919ec2018-11-15 11:52:07 -080032 this->path = path;
33
34 if (file.is_open())
35 {
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070036 return true;
Patrick Venture64919ec2018-11-15 11:52:07 -080037 }
Jason Ling56a22732020-10-23 19:53:17 -070038 file.open(filename, mode);
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070039 return file.good();
Patrick Venture64919ec2018-11-15 11:52:07 -080040}
41
42void FileHandler::close()
43{
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070044 file.close();
Patrick Venture64919ec2018-11-15 11:52:07 -080045}
46
47bool FileHandler::write(std::uint32_t offset,
48 const std::vector<std::uint8_t>& data)
49{
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070050 file.seekp(offset);
Patrick Venture64919ec2018-11-15 11:52:07 -080051 file.write(reinterpret_cast<const char*>(data.data()), data.size());
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070052 return file.good();
Patrick Venture64919ec2018-11-15 11:52:07 -080053}
54
Patrick Williams42a44c22024-08-16 15:21:32 -040055std::optional<std::vector<uint8_t>>
56 FileHandler::read(std::uint32_t offset, std::uint32_t size)
Jason Ling56a22732020-10-23 19:53:17 -070057{
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070058 uint32_t file_size = getSize();
59 if (offset > file_size)
Jason Ling56a22732020-10-23 19:53:17 -070060 {
61 return std::nullopt;
62 }
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070063 std::vector<uint8_t> ret(std::min(file_size - offset, size));
Jason Ling56a22732020-10-23 19:53:17 -070064 file.seekg(offset);
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070065 file.read(reinterpret_cast<char*>(ret.data()), ret.size());
Jason Ling56a22732020-10-23 19:53:17 -070066 if (!file.good())
67 {
68 return std::nullopt;
69 }
Willy Tudf82a462021-10-25 09:49:40 -070070 return ret;
Jason Ling56a22732020-10-23 19:53:17 -070071}
72
Patrick Venturecc7d1602018-11-15 13:58:33 -080073int FileHandler::getSize()
74{
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070075 std::error_code ec;
76 auto ret = std::filesystem::file_size(filename, ec);
77 if (ec)
Patrick Venture86b49e22018-11-15 14:29:00 -080078 {
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070079 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 Venture86b49e22018-11-15 14:29:00 -080083 }
William A. Kennington IIIba90fd72021-10-22 00:33:52 -070084 return ret;
Patrick Venturecc7d1602018-11-15 13:58:33 -080085}
86
Patrick Venture1d5a31c2019-05-20 11:38:22 -070087} // namespace ipmi_flash