8 #include <boost/filesystem.hpp>
9 #include <boost/optional.hpp>
11 #include "storage_config.h"
12 #include "storage_exception.h"
14 #include "uptane/tuf.h"
21 using store_data_t = void (
INvStorage::*)(
const std::string&);
22 using load_data_t = bool (
INvStorage::*)(std::string*);
24 typedef std::vector<std::pair<Uptane::EcuSerial, Uptane::HardwareIdentifier>> EcuSerials;
26 enum class EcuState { kOld = 0, kNotRegistered };
30 : serial(std::move(serial_in)), hardware_id(std::move(hardware_id_in)), state(state_in) {}
40 explicit WriteError(
const std::string& what) : std::runtime_error(what) {}
43 virtual size_t wfeed(
const uint8_t* buf,
size_t size) = 0;
44 virtual void wcommit() = 0;
45 virtual void wabort() = 0;
46 size_t getWrittenSize() {
return written_size_; }
49 std::array<uint8_t, 256> arr{};
51 is.read(reinterpret_cast<char*>(arr.data()), arr.size());
52 handle.wfeed(arr.data(), static_cast<size_t>(is.gcount()));
58 size_t written_size_{0};
65 explicit ReadError(
const std::string& what) : std::runtime_error(what) {}
68 virtual bool isPartial()
const = 0;
69 virtual std::unique_ptr<StorageTargetWHandle> toWriteHandle() = 0;
71 virtual size_t rsize()
const = 0;
72 virtual size_t rread(uint8_t* buf,
size_t size) = 0;
73 virtual void rclose() = 0;
75 void writeToFile(
const boost::filesystem::path& path) {
76 std::array<uint8_t, 1024> arr{};
78 std::ofstream file(path.c_str());
80 throw std::runtime_error(std::string(
"Error opening file ") + path.string());
82 while (written < rsize()) {
83 size_t nread = rread(arr.data(), arr.size());
84 file.write(reinterpret_cast<char*>(arr.data()), static_cast<std::streamsize>(nread));
92 std::array<uint8_t, 256> arr{};
94 while (written < handle.rsize()) {
95 size_t nread = handle.rread(arr.data(), arr.size());
97 os.write(reinterpret_cast<char*>(arr.data()), static_cast<std::streamsize>(nread));
105 enum class InstalledVersionUpdateMode { kNone, kCurrent, kPending };
113 virtual StorageType type() = 0;
114 virtual void storePrimaryKeys(
const std::string& public_key,
const std::string& private_key) = 0;
115 virtual bool loadPrimaryKeys(std::string* public_key, std::string* private_key) = 0;
116 virtual bool loadPrimaryPublic(std::string* public_key) = 0;
117 virtual bool loadPrimaryPrivate(std::string* private_key) = 0;
118 virtual void clearPrimaryKeys() = 0;
120 virtual void storeTlsCreds(
const std::string& ca,
const std::string& cert,
const std::string& pkey) = 0;
121 virtual void storeTlsCa(
const std::string& ca) = 0;
122 virtual void storeTlsCert(
const std::string& cert) = 0;
123 virtual void storeTlsPkey(
const std::string& pkey) = 0;
124 virtual bool loadTlsCreds(std::string* ca, std::string* cert, std::string* pkey) = 0;
125 virtual bool loadTlsCa(std::string* ca) = 0;
126 virtual bool loadTlsCert(std::string* cert) = 0;
127 virtual bool loadTlsPkey(std::string* cert) = 0;
128 virtual void clearTlsCreds() = 0;
138 virtual void clearMetadata() = 0;
139 virtual void storeDelegation(
const std::string&
data,
Uptane::Role role) = 0;
141 virtual bool loadAllDelegations(std::vector<std::pair<Uptane::Role, std::string>>&
data)
const = 0;
143 virtual void clearDelegations() = 0;
145 virtual void storeDeviceId(
const std::string& device_id) = 0;
146 virtual bool loadDeviceId(std::string* device_id) = 0;
147 virtual void clearDeviceId() = 0;
149 virtual void storeEcuSerials(
const EcuSerials& serials) = 0;
150 virtual bool loadEcuSerials(EcuSerials* serials) = 0;
151 virtual void clearEcuSerials() = 0;
153 virtual void storeMisconfiguredEcus(
const std::vector<MisconfiguredEcu>& ecus) = 0;
154 virtual bool loadMisconfiguredEcus(std::vector<MisconfiguredEcu>* ecus) = 0;
155 virtual void clearMisconfiguredEcus() = 0;
157 virtual void storeEcuRegistered() = 0;
158 virtual bool loadEcuRegistered() = 0;
159 virtual void clearEcuRegistered() = 0;
161 virtual void storeNeedReboot() = 0;
162 virtual bool loadNeedReboot(
bool* need_reboot) = 0;
163 virtual void clearNeedReboot() = 0;
165 virtual void saveInstalledVersion(
const std::string& ecu_serial,
const Uptane::Target& target,
166 InstalledVersionUpdateMode update_mode) = 0;
167 virtual bool loadInstalledVersions(
const std::string& ecu_serial, boost::optional<Uptane::Target>* current_version,
168 boost::optional<Uptane::Target>* pending_version) = 0;
169 virtual bool loadInstallationLog(
const std::string& ecu_serial, std::vector<Uptane::Target>* log,
170 bool only_installed) = 0;
171 virtual bool hasPendingInstall() = 0;
172 virtual void getPendingEcus(std::vector<std::pair<Uptane::EcuSerial, Uptane::Hash>>* pendingEcus) = 0;
173 virtual void clearInstalledVersions() = 0;
177 virtual bool loadEcuInstallationResults(
178 std::vector<std::pair<Uptane::EcuSerial, data::InstallationResult>>* results) = 0;
180 const std::string& correlation_id) = 0;
182 std::string* correlation_id) = 0;
183 virtual void clearInstallationResults() = 0;
185 virtual void saveEcuReportCounter(
const Uptane::EcuSerial& ecu_serial, int64_t counter) = 0;
186 virtual bool loadEcuReportCounter(std::vector<std::pair<Uptane::EcuSerial, int64_t>>* results) = 0;
188 virtual bool checkAvailableDiskSpace(uint64_t required_bytes)
const = 0;
189 virtual boost::optional<std::pair<size_t, std::string>> checkTargetFile(
const Uptane::Target& target)
const = 0;
192 virtual std::unique_ptr<StorageTargetWHandle> allocateTargetFile(
bool from_director,
195 virtual std::unique_ptr<StorageTargetRHandle> openTargetFile(
const Uptane::Target& target) = 0;
196 virtual std::vector<Uptane::Target> getTargetFiles() = 0;
197 virtual void removeTargetFile(
const std::string& target_name) = 0;
199 virtual void cleanUp() = 0;
202 static std::shared_ptr<INvStorage> newStorage(
const StorageConfig& config,
bool readonly =
false);
204 static bool fsReadInstalledVersions(
const boost::filesystem::path& filename,
205 std::vector<Uptane::Target>* installed_versions,
size_t* current_version);
209 bool loadPrimaryInstalledVersions(boost::optional<Uptane::Target>* current_version,
210 boost::optional<Uptane::Target>* pending_version) {
211 return loadInstalledVersions(
"", current_version, pending_version);
213 void savePrimaryInstalledVersion(
const Uptane::Target& target, InstalledVersionUpdateMode update_mode) {
214 return saveInstalledVersion(
"", target, update_mode);
216 bool loadPrimaryInstallationLog(std::vector<Uptane::Target>* log,
bool only_installed) {
217 return loadInstallationLog(
"", log, only_installed);
219 void importInstalledVersions(
const boost::filesystem::path& base_path);
222 void importSimple(
const boost::filesystem::path& base_path, store_data_t store_func, load_data_t load_func,
224 void importUpdateSimple(
const boost::filesystem::path& base_path, store_data_t store_func, load_data_t load_func,
226 void importPrimaryKeys(
const boost::filesystem::path& base_path,
const BasedPath& import_pubkey_path,
233 #endif // INVSTORAGE_H_