Aktualizr
C++ SOTA Client
types.cc
1 #include <array>
2 #include <sstream>
3 #include <stdexcept>
4 #include <utility>
5 
6 #include "libaktualizr/types.h"
7 #include "utilities/utils.h"
8 
9 std::ostream &operator<<(std::ostream &os, const StorageType stype) {
10  std::string stype_str;
11  switch (stype) {
12  case StorageType::kFileSystem:
13  stype_str = "filesystem";
14  break;
15  case StorageType::kSqlite:
16  stype_str = "sqlite";
17  break;
18  default:
19  stype_str = "unknown";
20  break;
21  }
22  os << '"' << stype_str << '"';
23  return os;
24 }
25 
26 std::string TimeToString(struct tm time) {
27  std::array<char, 22> formatted{};
28  strftime(formatted.data(), 22, "%Y-%m-%dT%H:%M:%SZ", &time);
29  return std::string(formatted.data());
30 }
31 
32 TimeStamp TimeStamp::Now() { return TimeStamp(CurrentTime()); }
33 
34 struct tm TimeStamp::CurrentTime() {
35  time_t raw_time;
36  struct tm time_struct {};
37  time(&raw_time);
38  gmtime_r(&raw_time, &time_struct);
39 
40  return time_struct;
41 }
42 
43 TimeStamp::TimeStamp(std::string rfc3339) {
44  if (rfc3339.length() != 20 || rfc3339[19] != 'Z') {
46  }
47  time_ = rfc3339;
48 }
49 
50 TimeStamp::TimeStamp(struct tm time) : TimeStamp(TimeToString(time)) {}
51 
52 bool TimeStamp::IsValid() const { return time_.length() != 0; }
53 
54 bool TimeStamp::IsExpiredAt(const TimeStamp &now) const {
55  if (!IsValid()) {
56  return true;
57  }
58  if (!now.IsValid()) {
59  return true;
60  }
61  return *this < now;
62 }
63 
64 bool TimeStamp::operator<(const TimeStamp &other) const { return IsValid() && other.IsValid() && time_ < other.time_; }
65 
66 bool TimeStamp::operator>(const TimeStamp &other) const { return (other < *this); }
67 
68 std::ostream &operator<<(std::ostream &os, const TimeStamp &t) {
69  os << t.time_;
70  return os;
71 }
72 
73 namespace data {
74 
75 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
76  {ResultCode::Numeric::kOk, "OK"},
77  {ResultCode::Numeric::kAlreadyProcessed, "ALREADY_PROCESSED"},
78  {ResultCode::Numeric::kVerificationFailed, "VERIFICATION_FAILED"},
79  {ResultCode::Numeric::kInstallFailed, "INSTALL_FAILED"},
80  {ResultCode::Numeric::kDownloadFailed, "DOWNLOAD_FAILED"},
81  {ResultCode::Numeric::kInternalError, "INTERNAL_ERROR"},
82  {ResultCode::Numeric::kGeneralError, "GENERAL_ERROR"},
83  {ResultCode::Numeric::kNeedCompletion, "NEED_COMPLETION"},
84  {ResultCode::Numeric::kCustomError, "CUSTOM_ERROR"},
85  {ResultCode::Numeric::kUnknown, "UNKNOWN"},
86 };
87 
88 std::string data::ResultCode::toRepr() const {
89  std::string s = toString();
90 
91  if (s.find('\"') != std::string::npos) {
92  throw std::runtime_error("Result code cannot contain double quotes");
93  }
94 
95  return "\"" + s + "\"" + ":" + std::to_string(static_cast<int>(num_code));
96 }
97 
98 ResultCode data::ResultCode::fromRepr(const std::string &repr) {
99  size_t quote_n = repr.find('"');
100  std::string s;
101  size_t col_n;
102 
103  if (quote_n < repr.size() - 1) {
104  size_t end_quote_n = repr.find('"', quote_n + 1);
105  col_n = repr.find(':', end_quote_n + 1);
106  s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
107  } else {
108  // legacy format
109  col_n = repr.find(':');
110  s = repr.substr(0, col_n);
111  }
112 
113  if (col_n >= repr.size() - 1) {
114  return ResultCode(Numeric::kUnknown, s);
115  }
116 
117  int num = std::stoi(repr.substr(col_n + 1));
118 
119  return ResultCode(static_cast<Numeric>(num), s);
120 }
121 
122 Json::Value InstallationResult::toJson() const {
123  Json::Value json;
124  json["success"] = success;
125  json["code"] = result_code.toString();
126  json["description"] = description;
127  return json;
128 }
129 
130 std::ostream &operator<<(std::ostream &os, const ResultCode &result_code) {
131  os << result_code.toRepr();
132  return os;
133 }
134 
135 } // namespace data
136 
137 // vim: set tabstop=2 shiftwidth=2 expandtab:
138 
139 boost::filesystem::path utils::BasedPath::get(const boost::filesystem::path &base) const {
140  // note: BasedPath(bp.get() == bp)
141  return Utils::absolutePath(base, p_);
142 }
data::ResultCode::Numeric::kVerificationFailed
@ kVerificationFailed
Metadata verification failed.
data::ResultCode::Numeric::kGeneralError
@ kGeneralError
Other error.
data::ResultCode::Numeric::kAlreadyProcessed
@ kAlreadyProcessed
Operation has already been processed.
types.h
TimeStamp::InvalidTimeStamp
Definition: types.h:204
TimeStamp::TimeStamp
TimeStamp()
An invalid TimeStamp.
Definition: types.h:193
data
General data structures.
Definition: types.h:217
TimeStamp
Definition: types.h:188
data::ResultCode::Numeric::kInternalError
@ kInternalError
SWM Internal integrity error.
data::ResultCode::Numeric::kDownloadFailed
@ kDownloadFailed
Package download failed.
data::ResultCode::Numeric::kInstallFailed
@ kInstallFailed
Package installation failed.