Aktualizr
C++ SOTA Client
utils.h
1 #ifndef UTILS_H_
2 #define UTILS_H_
3 
4 #include <glob.h>
5 #include <boost/filesystem.hpp>
6 #include <memory>
7 #include <string>
8 
9 #include <curl/curl.h>
10 #include <netinet/in.h>
11 
12 #include "json/json.h"
13 
14 struct Utils {
15  static std::string fromBase64(std::string base64_string);
16  static std::string toBase64(const std::string &tob64);
17  static std::string stripQuotes(const std::string &value);
18  static std::string addQuotes(const std::string &value);
19  static std::string extractField(const std::string &in, unsigned int field_id);
20  static Json::Value parseJSON(const std::string &json_str);
21  static Json::Value parseJSONFile(const boost::filesystem::path &filename);
22  static std::string jsonToStr(const Json::Value &json);
23  static std::string jsonToCanonicalStr(const Json::Value &json);
24  static std::string genPrettyName();
25  static std::string readFile(const boost::filesystem::path &filename, bool trim = false);
26  static void writeFile(const boost::filesystem::path &filename, const std::string &content,
27  bool create_directories = true);
28  static void writeFile(const boost::filesystem::path &filename, const Json::Value &content,
29  bool create_directories = true);
30  static void copyDir(const boost::filesystem::path &from, const boost::filesystem::path &to);
31  static std::string readFileFromArchive(std::istream &as, const std::string &filename, bool trim = false);
32  static void writeArchive(const std::map<std::string, std::string> &entries, std::ostream &as);
33  static Json::Value getHardwareInfo();
34  static Json::Value getNetworkInfo();
35  static std::string getHostname();
36  static std::string randomUuid();
37  static sockaddr_storage ipGetSockaddr(int fd);
38  static std::string ipDisplayName(const sockaddr_storage &saddr);
39  static int ipPort(const sockaddr_storage &saddr);
40  static void clearUbootCounter();
41  static void setUbootUpgraded();
42  static int shell(const std::string &command, std::string *output, bool include_stderr = false);
43  static boost::filesystem::path absolutePath(const boost::filesystem::path &root, const boost::filesystem::path &file);
44  static void setSocketPort(sockaddr_storage *addr, in_port_t port);
45  static std::vector<boost::filesystem::path> glob(const std::string &pat);
46  static void createDirectories(const boost::filesystem::path &path, mode_t mode);
47 };
48 
49 /**
50  * RAII Temporary file creation
51  */
53  public:
54  explicit TemporaryFile(const std::string &hint = "file");
55  TemporaryFile(const TemporaryFile &) = delete;
56  TemporaryFile operator=(const TemporaryFile &) = delete;
57  ~TemporaryFile();
58  void PutContents(const std::string &contents);
59  boost::filesystem::path Path() const;
60  std::string PathString() const;
61 
62  private:
63  boost::filesystem::path tmp_name_;
64 };
65 
67  public:
68  explicit TemporaryDirectory(const std::string &hint = "dir");
69  TemporaryDirectory(const TemporaryDirectory &) = delete;
70  TemporaryDirectory operator=(TemporaryDirectory &) = delete;
72  boost::filesystem::path Path() const;
73  std::string PathString() const;
74  boost::filesystem::path operator/(const boost::filesystem::path &subdir) const;
75 
76  private:
77  boost::filesystem::path tmp_name_;
78 };
79 
80 // Can represent an absolute or relative path, only readable through the
81 // `.get()` method
82 //
83 // The intent is to avoid unintentional use of the "naked" relative path by
84 // mandating a base directory for each instantiation
85 class BasedPath {
86  public:
87  BasedPath(boost::filesystem::path p) : p_(std::move(p)) {}
88 
89  boost::filesystem::path get(const boost::filesystem::path &base) const {
90  // note: BasedPath(bp.get()) == bp
91  return Utils::absolutePath(base, p_);
92  }
93 
94  bool empty() const { return p_.empty(); }
95  bool operator==(const BasedPath &b) const { return p_ == b.p_; }
96  bool operator!=(const BasedPath &b) const { return !(*this == b); }
97 
98  private:
99  boost::filesystem::path p_;
100 };
101 
102 // helper template for C (mostly openssl) data structured
103 // user should still take care about the order of destruction
104 // by instantiating StructGuard<> in a right order.
105 // BTW local variables are destructed in reverse order of instantiation
106 template <typename T>
107 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
108 
109 // helper object for RAII socket management
110 struct SocketCloser {
111  void operator()(const int *ptr) const {
112  close(*ptr);
113  delete ptr;
114  }
115 };
116 
117 using SocketHandle = std::unique_ptr<int, SocketCloser>;
118 bool operator<(const sockaddr_storage &left, const sockaddr_storage &right); // required by std::map
119 
120 // wrapper for curl handles
122  public:
123  CurlEasyWrapper() {
124  handle = curl_easy_init();
125  if (handle == nullptr) {
126  throw std::runtime_error("Could not initialize curl handle");
127  }
128  }
129  ~CurlEasyWrapper() {
130  if (handle != nullptr) {
131  curl_easy_cleanup(handle);
132  }
133  }
134  CURL *get() { return handle; }
135 
136  private:
137  CURL *handle;
138 };
139 
140 template <typename... T>
141 static void curlEasySetoptWrapper(CURL *curl_handle, CURLoption option, T &&... args) {
142  const CURLcode retval = curl_easy_setopt(curl_handle, option, std::forward<T>(args)...);
143  if (retval != 0u) {
144  throw std::runtime_error(std::string("curl_easy_setopt error: ") + curl_easy_strerror(retval));
145  }
146 }
147 
148 // this is reference implementation of make_unique which is not yet included to C++11
149 namespace std_ {
150 template <class T>
151 struct _Unique_if {
152  using _Single_object = std::unique_ptr<T>;
153 };
154 
155 template <class T>
156 struct _Unique_if<T[]> {
157  using _Unknown_bound = std::unique_ptr<T[]>;
158 };
159 
160 template <class T, size_t N>
161 struct _Unique_if<T[N]> {
162  using _Known_bound = void;
163 };
164 
165 template <class T, class... Args>
166 typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
167  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
168 }
169 
170 template <class T>
171 typename _Unique_if<T>::_Unknown_bound make_unique(size_t n) {
172  using U = typename std::remove_extent<T>::type;
173  return std::unique_ptr<T>(new U[n]());
174 }
175 
176 template <class T, class... Args>
177 typename _Unique_if<T>::_Known_bound make_unique(Args &&...) = delete;
178 } // namespace std_
179 
180 #endif // UTILS_H_
Definition: utils.h:149
Definition: utils.h:14
RAII Temporary file creation.
Definition: utils.h:52