Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
types.cc
1 #include "utilities/types.h"
2 
3 #include <stdexcept>
4 #include <utility>
5 
6 TimeStamp TimeStamp::Now() {
7  time_t raw_time;
8  struct tm time_struct {};
9  time(&raw_time);
10  gmtime_r(&raw_time, &time_struct);
11  char formatted[22];
12  strftime(formatted, 22, "%Y-%m-%dT%H:%M:%SZ", &time_struct);
13  return TimeStamp(formatted);
14 }
15 
16 TimeStamp::TimeStamp(std::string rfc3339) {
17  if (rfc3339.length() != 20 || rfc3339[19] != 'Z') {
19  }
20  time_ = rfc3339;
21 }
22 
23 bool TimeStamp::IsValid() const { return time_.length() != 0; }
24 
25 bool TimeStamp::IsExpiredAt(const TimeStamp &now) const {
26  if (!IsValid()) {
27  return true;
28  }
29  if (!now.IsValid()) {
30  return true;
31  }
32  return *this < now;
33 }
34 
35 bool TimeStamp::operator<(const TimeStamp &other) const { return IsValid() && other.IsValid() && time_ < other.time_; }
36 
37 bool TimeStamp::operator>(const TimeStamp &other) const { return (other < *this); }
38 
39 std::ostream &operator<<(std::ostream &os, const TimeStamp &t) {
40  os << t.time_;
41  return os;
42 }
43 
44 namespace data {
45 Json::Value Package::toJson() {
46  Json::Value json;
47  json["name"] = name;
48  json["version"] = version;
49  return json;
50 }
51 
52 Package Package::fromJson(const std::string &json_str) {
53  std::istringstream jsonss(json_str);
54  Json::Value json;
55  Json::parseFromStream(Json::CharReaderBuilder(), jsonss, &json, nullptr);
56  Package package;
57  package.name = json["name"].asString();
58  package.version = json["version"].asString();
59  return package;
60 }
61 
62 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
63  {ResultCode::Numeric::kOk, "OK"},
64  {ResultCode::Numeric::kAlreadyProcessed, "ALREADY_PROCESSED"},
65  {ResultCode::Numeric::kVerificationFailed, "VERIFICATION_FAILED"},
66  {ResultCode::Numeric::kInstallFailed, "INSTALL_FAILED"},
67  {ResultCode::Numeric::kDownloadFailed, "DOWNLOAD_FAILED"},
68  {ResultCode::Numeric::kInternalError, "INTERNAL_ERROR"},
69  {ResultCode::Numeric::kGeneralError, "GENERAL_ERROR"},
70  {ResultCode::Numeric::kNeedCompletion, "NEED_COMPLETION"},
71  {ResultCode::Numeric::kCustomError, "CUSTOM_ERROR"},
72  {ResultCode::Numeric::kUnknown, "UNKNOWN"},
73 };
74 
75 std::string data::ResultCode::toRepr() const {
76  std::string s = toString();
77 
78  if (s.find('\"') != std::string::npos) {
79  throw std::runtime_error("Result code cannot contain double quotes");
80  }
81 
82  return "\"" + s + "\"" + ":" + std::to_string(static_cast<int>(num_code));
83 }
84 
85 ResultCode data::ResultCode::fromRepr(const std::string &repr) {
86  size_t quote_n = repr.find('"');
87  std::string s;
88  size_t col_n;
89 
90  if (quote_n < repr.size() - 1) {
91  size_t end_quote_n = repr.find('"', quote_n + 1);
92  col_n = repr.find(':', end_quote_n + 1);
93  s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
94  } else {
95  // legacy format
96  col_n = repr.find(':');
97  s = repr.substr(0, col_n);
98  }
99 
100  if (col_n >= repr.size() - 1) {
101  return ResultCode(Numeric::kUnknown, s);
102  }
103 
104  int num = std::stoi(repr.substr(col_n + 1));
105 
106  return ResultCode(static_cast<Numeric>(num), s);
107 }
108 
109 Json::Value InstallationResult::toJson() const {
110  Json::Value json;
111  json["success"] = success;
112  json["code"] = result_code.toString();
113  json["description"] = description;
114  return json;
115 }
116 
117 std::ostream &operator<<(std::ostream &os, const ResultCode &result_code) {
118  os << result_code.toRepr();
119  return os;
120 }
121 
122 } // namespace data
123 
124 // vim: set tabstop=2 shiftwidth=2 expandtab:
data::ResultCode
Definition: types.h:122
data::ResultCode::Numeric::kVerificationFailed
Metadata verification failed.
data::ResultCode::Numeric::kGeneralError
Other error.
data::ResultCode::Numeric::kAlreadyProcessed
Operation has already been processed.
types.h
TimeStamp::InvalidTimeStamp
Definition: types.h:99
TimeStamp::TimeStamp
TimeStamp()
An invalid TimeStamp.
Definition: types.h:89
data
General data structures.
Definition: types.cc:44
data::Package
Definition: types.h:115
TimeStamp
Definition: types.h:85
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
data::ResultCode::Numeric::kDownloadFailed
Package download failed.
data::ResultCode::Numeric::kInstallFailed
Package installation failed.