5 #include <boost/filesystem.hpp> 10 #include <netinet/in.h> 12 #include "json/json.h" 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);
58 void PutContents(
const std::string &contents);
59 boost::filesystem::path Path()
const;
60 std::string PathString()
const;
63 boost::filesystem::path tmp_name_;
72 boost::filesystem::path Path()
const;
73 std::string PathString()
const;
74 boost::filesystem::path operator/(
const boost::filesystem::path &subdir)
const;
77 boost::filesystem::path tmp_name_;
87 BasedPath(boost::filesystem::path p) : p_(std::move(p)) {}
89 boost::filesystem::path
get(
const boost::filesystem::path &base)
const {
91 return Utils::absolutePath(base, p_);
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); }
99 boost::filesystem::path p_;
106 template <
typename T>
107 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
111 void operator()(
const int *ptr)
const {
117 using SocketHandle = std::unique_ptr<int, SocketCloser>;
118 bool operator<(
const sockaddr_storage &left,
const sockaddr_storage &right);
124 handle = curl_easy_init();
125 if (handle ==
nullptr) {
126 throw std::runtime_error(
"Could not initialize curl handle");
130 if (handle !=
nullptr) {
131 curl_easy_cleanup(handle);
134 CURL *
get() {
return handle; }
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)...);
144 throw std::runtime_error(std::string(
"curl_easy_setopt error: ") + curl_easy_strerror(retval));
152 using _Single_object = std::unique_ptr<T>;
157 using _Unknown_bound = std::unique_ptr<T[]>;
160 template <
class T,
size_t N>
162 using _Known_bound = void;
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)...));
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]());
176 template <
class T,
class... Args>
177 typename _Unique_if<T>::_Known_bound make_unique(Args &&...) =
delete;
RAII Temporary file creation.