Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
ostree_repo.cc
1 #include "ostree_repo.h"
2 
3 #include "logging/logging.h"
4 
5 // NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
6 OSTreeObject::ptr OSTreeRepo::GetObject(const uint8_t sha256[32], const OstreeObjectType type) const {
7  return GetObject(OSTreeHash(sha256), type);
8 }
9 
10 OSTreeObject::ptr OSTreeRepo::GetObject(const OSTreeHash hash, const OstreeObjectType type) const {
11  otable::const_iterator obj_it = ObjectTable.find(hash);
12  if (obj_it != ObjectTable.cend()) {
13  return obj_it->second;
14  }
15 
16  const std::map<OstreeObjectType, std::string> exts{{OstreeObjectType::OSTREE_OBJECT_TYPE_FILE, ".filez"},
17  {OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_TREE, ".dirtree"},
18  {OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META, ".dirmeta"},
19  {OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT, ".commit"}};
20  const std::string objpath = hash.string().insert(2, 1, '/');
21  OSTreeObject::ptr object;
22 
23  for (int i = 0; i < 3; ++i) {
24  if (i > 0) {
25  LOG_WARNING << "OSTree hash " << hash << " not found. Retrying (attempt " << i << " of 3)";
26  }
27  if (type != OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN) {
28  if (CheckForObject(hash, objpath + exts.at(type), object)) {
29  return object;
30  }
31  } else {
32  for (auto it = exts.cbegin(); it != exts.cend(); ++it) {
33  if (CheckForObject(hash, objpath + it->second, object)) {
34  return object;
35  }
36  }
37  }
38  }
39  throw OSTreeObjectMissing(hash);
40 }
41 
42 bool OSTreeRepo::CheckForObject(const OSTreeHash &hash, const std::string &path, OSTreeObject::ptr &object) const {
43  if (FetchObject(std::string("objects/") + path)) {
44  object = OSTreeObject::ptr(new OSTreeObject(*this, path));
45  ObjectTable[hash] = object;
46  LOG_DEBUG << "Fetched OSTree object " << path;
47  return true;
48  }
49  return false;
50 }
OSTreeObjectMissing
Thrown by GetObject when the object requested is not present in the repository.
Definition: ostree_repo.h:46
OSTreeHash
Definition: ostree_hash.h:10
OSTreeObject
Definition: ostree_object.h:30
OstreeObjectType
OstreeObjectType
Types of OSTree objects, borrowed from libostree/ostree-core.h.
Definition: garage_common.h:34