Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Change target autocleaner to be more selective
Browse files Browse the repository at this point in the history
Only deletes until the second last version.

Signed-off-by: Laurent Bonnans <[email protected]>
  • Loading branch information
lbonn committed Sep 4, 2019
1 parent e09506a commit e40b89e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/libaktualizr/primary/aktualizr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ boost::signals2::connection Aktualizr::SetSignalHandler(
return sig_->connect(handler);
}

Aktualizr::InstallationLog Aktualizr::GetInstallationLog() {
Aktualizr::InstallationLog ilog;

EcuSerials serials;
if (!storage_->loadEcuSerials(&serials)) {
throw std::runtime_error("Could not load ecu serials");
}

for (const auto &s : serials) {
ilog.ecus.push_back(s.first);

std::vector<Uptane::Target> installed_versions;
std::vector<Uptane::Target*> log;
storage_->loadInstallationLog(s.first.ToString(), &installed_versions, &log);

std::vector<Uptane::Target> iv;
for (const auto &i : log) {
iv.push_back(*i);
}
ilog.installs.at(s.first) = std::move(iv);
}

return ilog;
}

std::vector<Uptane::Target> Aktualizr::GetStoredTargets() { return storage_->getTargetFiles(); }

void Aktualizr::DeleteStoredTarget(const Uptane::Target &target) { storage_->removeTargetFile(target.filename()); }
Expand Down
10 changes: 10 additions & 0 deletions src/libaktualizr/primary/aktualizr.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ class Aktualizr {
*/
std::future<result::Download> Download(const std::vector<Uptane::Target>& updates);

/**
* Get log of installations
* @return installation log
*/
struct InstallationLog {
std::vector<Uptane::EcuSerial> ecus;
std::map<Uptane::EcuSerial, std::vector<Uptane::Target>> installs;
};
InstallationLog GetInstallationLog();

/**
* Get list of targets currently in storage. This is intended to be used with
* DeleteStoredTarget and targets are not guaranteed to be verified and
Expand Down
36 changes: 23 additions & 13 deletions src/libaktualizr/primary/aktualizr_helpers.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
#include <set>

#include "aktualizr_helpers.h"

void targets_autoclean_cb(Aktualizr &aktualizr, const std::shared_ptr<event::BaseEvent> &event) {
if (!event->isTypeOf<event::AllInstallsComplete>()) {
return;
}

const auto targets_event = dynamic_cast<event::AllInstallsComplete *>(event.get());

std::vector<Uptane::Target> installed_targets = aktualizr.GetStoredTargets();
std::vector<bool> to_remove(installed_targets.size(), true);

// do not remove targets that were just installed
for (const result::Install::EcuReport &er : targets_event->result.ecu_reports) {
const Uptane::Target t = er.update;
auto it = std::find_if(installed_targets.begin(), installed_targets.end(),
[&t](const Uptane::Target &t2) { return t.sha256Hash() == t2.sha256Hash(); });
if (it == installed_targets.end()) {
continue;
std::set<Uptane::EcuSerial> affected_ecus;
for (const Uptane::Target &t : installed_targets) {
Uptane::EcuMap em = t.ecus();
for (const auto &ecu : em) {
affected_ecus.insert(ecu.first);
}

size_t rem_idx = static_cast<size_t>(it - installed_targets.begin());
to_remove[rem_idx] = false;
}

// TODO: more specific check
for (const Uptane::EcuSerial &ecu : affected_ecus) {
Aktualizr::InstallationLog log = aktualizr.GetInstallationLog();

auto ecu_log = log.installs.at(ecu);
for (auto it = ecu_log.end() - 2; it != ecu_log.end(); it++) {
auto fit = std::find_if(installed_targets.begin(), installed_targets.end(),
[&it](const Uptane::Target &t2) { return it->sha256Hash() == t2.sha256Hash(); });

if (fit == installed_targets.end()) {
continue;
}

size_t rem_idx = static_cast<size_t>(fit - installed_targets.begin());
to_remove[rem_idx] = false;
}
}

for (size_t k = 0; k < installed_targets.size(); k++) {
if (to_remove[k]) {
Expand Down

0 comments on commit e40b89e

Please sign in to comment.