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