Aktualizr
C++ SOTA Client
sqlstorage.h
1 #ifndef SQLSTORAGE_H_
2 #define SQLSTORAGE_H_
3 
4 #include <boost/filesystem.hpp>
5 
6 #include <sqlite3.h>
7 
8 #include "invstorage.h"
9 #include "sql_utils.h"
10 
11 extern const std::vector<std::string> schema_migrations;
12 extern const std::string current_schema;
13 extern const int current_schema_version;
14 
15 enum class SQLReqId { kGetSimple, kGetTable };
16 enum class DbVersion : int32_t { kEmpty = -1, kInvalid = -2 };
17 
18 class SQLStorage : public INvStorage {
19  public:
20  friend class SQLTargetWHandle;
21  friend class SQLTargetRHandle;
22  explicit SQLStorage(const StorageConfig& config, bool readonly);
23  ~SQLStorage() override = default;
24  void storePrimaryKeys(const std::string& public_key, const std::string& private_key) override;
25  bool loadPrimaryKeys(std::string* public_key, std::string* private_key) override;
26  bool loadPrimaryPublic(std::string* public_key) override;
27  bool loadPrimaryPrivate(std::string* private_key) override;
28  void clearPrimaryKeys() override;
29 
30  void storeTlsCreds(const std::string& ca, const std::string& cert, const std::string& pkey) override;
31  void storeTlsCa(const std::string& ca) override;
32  void storeTlsCert(const std::string& cert) override;
33  void storeTlsPkey(const std::string& pkey) override;
34  bool loadTlsCreds(std::string* ca, std::string* cert, std::string* pkey) override;
35  void clearTlsCreds() override;
36  bool loadTlsCa(std::string* ca) override;
37  bool loadTlsCert(std::string* cert) override;
38  bool loadTlsPkey(std::string* pkey) override;
39 
40  void storeRoot(const std::string& data, Uptane::RepositoryType repo, Uptane::Version version) override;
41  bool loadRoot(std::string* data, Uptane::RepositoryType repo, Uptane::Version version) override;
42  void storeNonRoot(const std::string& data, Uptane::RepositoryType repo, Uptane::Role role) override;
43  bool loadNonRoot(std::string* data, Uptane::RepositoryType repo, Uptane::Role role) override;
44  void clearNonRootMeta(Uptane::RepositoryType repo) override;
45  void clearMetadata() override;
46 
47  void storeDeviceId(const std::string& device_id) override;
48  bool loadDeviceId(std::string* device_id) override;
49  void clearDeviceId() override;
50  void storeEcuSerials(const EcuSerials& serials) override;
51  bool loadEcuSerials(EcuSerials* serials) override;
52  void clearEcuSerials() override;
53  void storeMisconfiguredEcus(const std::vector<MisconfiguredEcu>& ecus) override;
54  bool loadMisconfiguredEcus(std::vector<MisconfiguredEcu>* ecus) override;
55  void clearMisconfiguredEcus() override;
56  void storeEcuRegistered() override;
57  bool loadEcuRegistered() override;
58  void clearEcuRegistered() override;
59  void storeInstalledVersions(const std::vector<Uptane::Target>& installed_versions,
60  const std::string& current_hash) override;
61  std::string loadInstalledVersions(std::vector<Uptane::Target>* installed_versions) override;
62  void clearInstalledVersions() override;
63  void storeInstallationResult(const data::OperationResult& result) override;
64  bool loadInstallationResult(data::OperationResult* result) override;
65  void clearInstallationResult() override;
66 
67  std::unique_ptr<StorageTargetWHandle> allocateTargetFile(bool from_director, const std::string& filename,
68  size_t size) override;
69  std::unique_ptr<StorageTargetRHandle> openTargetFile(const std::string& filename) override;
70  void removeTargetFile(const std::string& filename) override;
71  void cleanUp() override;
72  StorageType type() override { return StorageType::kSqlite; };
73 
74  std::string getTableSchemaFromDb(const std::string& tablename);
75 
76  bool dbMigrate();
77  DbVersion getVersion(); // non-negative integer on success or -1 on error
78  boost::filesystem::path dbPath() const;
79 
80  private:
81  SQLite3Guard dbConnection();
82  // request info
83  void cleanMetaVersion(Uptane::RepositoryType repo, Uptane::Role role);
84  bool readonly_{false};
85 };
86 
87 #endif // SQLSTORAGE_H_
General data structures.
Definition: types.cc:44
Metadata version numbers.
Definition: tuf.h:58
TUF Roles.
Definition: tuf.h:26
RepositoryType
This must match the repo_type table in sqlstorage.
Definition: tuf.h:19