Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
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 Json::Value Package::toJson() const {
75  Json::Value json;
76  json["name"] = name;
77  json["version"] = version;
78  return json;
79 }
80 
81 Package Package::fromJson(const std::string &json_str) {
82  std::istringstream jsonss(json_str);
83  Json::Value json;
84  Json::parseFromStream(Json::CharReaderBuilder(), jsonss, &json, nullptr);
85  Package package;
86  package.name = json["name"].asString();
87  package.version = json["version"].asString();
88  return package;
89 }
90 
91 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
92  {ResultCode::Numeric::kOk, "OK"},
93  {ResultCode::Numeric::kAlreadyProcessed, "ALREADY_PROCESSED"},
94  {ResultCode::Numeric::kVerificationFailed, "VERIFICATION_FAILED"},
95  {ResultCode::Numeric::kInstallFailed, "INSTALL_FAILED"},
96  {ResultCode::Numeric::kDownloadFailed, "DOWNLOAD_FAILED"},
97  {ResultCode::Numeric::kInternalError, "INTERNAL_ERROR"},
98  {ResultCode::Numeric::kGeneralError, "GENERAL_ERROR"},
99  {ResultCode::Numeric::kNeedCompletion, "NEED_COMPLETION"},
100  {ResultCode::Numeric::kCustomError, "CUSTOM_ERROR"},
101  {ResultCode::Numeric::kUnknown, "UNKNOWN"},
102 };
103 
104 std::string data::ResultCode::toRepr() const {
105  std::string s = toString();
106 
107  if (s.find('\"') != std::string::npos) {
108  throw std::runtime_error("Result code cannot contain double quotes");
109  }
110 
111  return "\"" + s + "\"" + ":" + std::to_string(static_cast<int>(num_code));
112 }
113 
114 ResultCode data::ResultCode::fromRepr(const std::string &repr) {
115  size_t quote_n = repr.find('"');
116  std::string s;
117  size_t col_n;
118 
119  if (quote_n < repr.size() - 1) {
120  size_t end_quote_n = repr.find('"', quote_n + 1);
121  col_n = repr.find(':', end_quote_n + 1);
122  s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
123  } else {
124  // legacy format
125  col_n = repr.find(':');
126  s = repr.substr(0, col_n);
127  }
128 
129  if (col_n >= repr.size() - 1) {
130  return ResultCode(Numeric::kUnknown, s);
131  }
132 
133  int num = std::stoi(repr.substr(col_n + 1));
134 
135  return ResultCode(static_cast<Numeric>(num), s);
136 }
137 
138 Json::Value InstallationResult::toJson() const {
139  Json::Value json;
140  json["success"] = success;
141  json["code"] = result_code.toString();
142  json["description"] = description;
143  return json;
144 }
145 
146 std::ostream &operator<<(std::ostream &os, const ResultCode &result_code) {
147  os << result_code.toRepr();
148  return os;
149 }
150 
151 } // namespace data
152 
153 // vim: set tabstop=2 shiftwidth=2 expandtab:
154 
155 boost::filesystem::path utils::BasedPath::get(const boost::filesystem::path &base) const {
156  // note: BasedPath(bp.get() == bp)
157  return Utils::absolutePath(base, p_);
158 }
TimeStamp()
An invalid TimeStamp.
Definition: types.h:191
General data structures.
Definition: types.h:215