<commit_before>#include "fsstorage.h"

#include <iostream>

#include <boost/filesystem.hpp>
#include <boost/scoped_array.hpp>

#include "logger.h"
#include "utils.h"

FSStorage::FSStorage(const Config& config) : config_(config) {
  boost::filesystem::create_directories(config_.tls.certificates_directory);
  boost::filesystem::create_directories(config_.uptane.metadata_path / "repo");
  boost::filesystem::create_directories(config_.uptane.metadata_path / "director");

  // migrate from old aktualizr behavior if we already provisioned
  boost::filesystem::path public_key_path = config_.tls.certificates_directory / config_.uptane.public_key_path;
  boost::filesystem::path private_key_path = config_.tls.certificates_directory / config_.uptane.private_key_path;
  boost::filesystem::path ca_path(config_.tls.ca_file());
  boost::filesystem::path cert_path(config_.tls.client_certificate());
  boost::filesystem::path pkey_path(config_.tls.pkey_file());
  boost::filesystem::path device_id = config_.tls.certificates_directory / "device_id";
  boost::filesystem::path primary_ecu_serial = config_.tls.certificates_directory / "primary_ecu_serial";
  if (boost::filesystem::exists(public_key_path) && boost::filesystem::exists(private_key_path) &&
      boost::filesystem::exists(ca_path) && boost::filesystem::exists(cert_path) &&
      boost::filesystem::exists(pkey_path) && boost::filesystem::exists(device_id) &&
      boost::filesystem::exists(primary_ecu_serial)) {
    Utils::writeFile((config_.tls.certificates_directory / "is_registered").string(), std::string("1"));
  }
}
FSStorage::~FSStorage() {
  // TODO: clear director_files, image_files
}

void FSStorage::storePrimaryKeys(const std::string& public_key, const std::string& private_key) {
  boost::filesystem::path public_key_path = config_.tls.certificates_directory / config_.uptane.public_key_path;
  boost::filesystem::path private_key_path = config_.tls.certificates_directory / config_.uptane.private_key_path;

  boost::filesystem::remove(public_key_path);
  boost::filesystem::remove(private_key_path);

  Utils::writeFile(public_key_path.string(), public_key);
  Utils::writeFile(private_key_path.string(), private_key);

  sync();
}

bool FSStorage::loadPrimaryKeys(std::string* public_key, std::string* private_key) {
  boost::filesystem::path public_key_path = config_.tls.certificates_directory / config_.uptane.public_key_path;
  boost::filesystem::path private_key_path = config_.tls.certificates_directory / config_.uptane.private_key_path;

  if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
    return false;
  }<commit_msg>Don't set is_register if the device really isn't<commit_after>#include "fsstorage.h"

#include <iostream>

#include <boost/filesystem.hpp>
#include <boost/scoped_array.hpp>

#include "logger.h"
#include "utils.h"

FSStorage::FSStorage(const Config& config) : config_(config) {
  boost::filesystem::create_directories(config_.tls.certificates_directory);
  boost::filesystem::create_directories(config_.uptane.metadata_path / "repo");
  boost::filesystem::create_directories(config_.uptane.metadata_path / "director");
}

FSStorage::~FSStorage() {
  // TODO: clear director_files, image_files
}

void FSStorage::storePrimaryKeys(const std::string& public_key, const std::string& private_key) {
  boost::filesystem::path public_key_path = config_.tls.certificates_directory / config_.uptane.public_key_path;
  boost::filesystem::path private_key_path = config_.tls.certificates_directory / config_.uptane.private_key_path;

  boost::filesystem::remove(public_key_path);
  boost::filesystem::remove(private_key_path);

  Utils::writeFile(public_key_path.string(), public_key);
  Utils::writeFile(private_key_path.string(), private_key);

  sync();
}

bool FSStorage::loadPrimaryKeys(std::string* public_key, std::string* private_key) {
  boost::filesystem::path public_key_path = config_.tls.certificates_directory / config_.uptane.public_key_path;
  boost::filesystem::path private_key_path = config_.tls.certificates_directory / config_.uptane.private_key_path;

  if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
    return false;
  }
{'fraction_non_alphanumeric': 0.13542157324540743, 'fraction_numerical': 0.00023551577955723034, 'mean_word_length': 7.847916666666666, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '717411', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1324, 'n_words': 181}