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