/* * Copyright (C) 2026 oxmc / PawletOS * SPDX-License-Identifier: Apache-2.0 */ #include "state.h" #include #include #include namespace pawlet::hwupdate { State::State(std::string path) : path_(std::move(path)) {} void State::Load() { entries_.clear(); std::ifstream in(path_, std::ios::binary); if (!in) return; // first run Json::Value root; Json::CharReaderBuilder b; std::string errs; if (!Json::parseFromStream(b, in, &root, &errs)) { LOG(WARNING) << "hwupdate: state parse error: " << errs; return; } const Json::Value& comps = root["components"]; for (const auto& id : comps.getMemberNames()) { Entry e; e.installedVersion = comps[id].get("installed_version", "").asString(); e.retryCount = comps[id].get("retry_count", 0).asInt(); entries_[id] = e; } } bool State::Save() const { Json::Value root; root["schema_version"] = 1; Json::Value comps(Json::objectValue); for (const auto& [id, e] : entries_) { Json::Value c(Json::objectValue); c["installed_version"] = e.installedVersion; c["retry_count"] = e.retryCount; comps[id] = c; } root["components"] = comps; // Write atomically: temp file + rename. std::string tmp = path_ + ".tmp"; { std::ofstream out(tmp, std::ios::binary | std::ios::trunc); if (!out) { LOG(ERROR) << "hwupdate: cannot write state " << tmp; return false; } Json::StreamWriterBuilder w; out << Json::writeString(w, root); } if (rename(tmp.c_str(), path_.c_str()) != 0) { LOG(ERROR) << "hwupdate: cannot rename state into place"; return false; } return true; } std::string State::InstalledVersion(const std::string& componentId) const { auto it = entries_.find(componentId); return it == entries_.end() ? std::string() : it->second.installedVersion; } void State::SetInstalledVersion(const std::string& componentId, const std::string& version) { entries_[componentId].installedVersion = version; } int State::RetryCount(const std::string& componentId) const { auto it = entries_.find(componentId); return it == entries_.end() ? 0 : it->second.retryCount; } void State::SetRetryCount(const std::string& componentId, int count) { entries_[componentId].retryCount = count; } } // namespace pawlet::hwupdate