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);
27 static void writeFile(
const boost::filesystem::path &filename,
const char *content,
size_t size);
28 static void writeFile(
const boost::filesystem::path &filename,
const std::string &content,
29 bool create_directories =
true);
30 static void writeFile(
const boost::filesystem::path &filename,
const Json::Value &content,
31 bool create_directories =
true);
32 static void copyDir(
const boost::filesystem::path &from,
const boost::filesystem::path &to);
33 static std::string readFileFromArchive(std::istream &as,
const std::string &filename,
bool trim =
false);
34 static void writeArchive(
const std::map<std::string, std::string> &entries, std::ostream &as);
35 static Json::Value getHardwareInfo();
36 static Json::Value getNetworkInfo();
37 static std::string getHostname();
38 static std::string randomUuid();
39 static sockaddr_storage ipGetSockaddr(
int fd);
40 static std::string ipDisplayName(
const sockaddr_storage &saddr);
41 static int ipPort(
const sockaddr_storage &saddr);
42 static void clearUbootCounter();
43 static void setUbootUpgraded();
44 static int shell(
const std::string &command, std::string *output,
bool include_stderr =
false);
45 static boost::filesystem::path absolutePath(
const boost::filesystem::path &root,
const boost::filesystem::path &file);
46 static void setSocketPort(sockaddr_storage *addr, in_port_t port);
47 static std::vector<boost::filesystem::path> glob(
const std::string &pat);
48 static void createDirectories(
const boost::filesystem::path &path, mode_t mode);
49 static std::string urlEncode(
const std::string &input);
50 static CURL *curlDupHandleWrapper(CURL *curl_in,
bool using_pkcs11);
62 void PutContents(
const std::string &contents);
63 boost::filesystem::path Path()
const;
64 std::string PathString()
const;
67 boost::filesystem::path tmp_name_;
76 boost::filesystem::path Path()
const;
77 std::string PathString()
const;
78 boost::filesystem::path operator/(
const boost::filesystem::path &subdir)
const;
81 boost::filesystem::path tmp_name_;
91 BasedPath(boost::filesystem::path p) : p_(std::move(p)) {}
93 boost::filesystem::path
get(
const boost::filesystem::path &base)
const {
95 return Utils::absolutePath(base, p_);
98 bool empty()
const {
return p_.empty(); }
99 bool operator==(
const BasedPath &b)
const {
return p_ == b.p_; }
100 bool operator!=(
const BasedPath &b)
const {
return !(*
this == b); }
103 boost::filesystem::path p_;
110 template <
typename T>
111 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
115 void operator()(
const int *ptr)
const {
121 using SocketHandle = std::unique_ptr<int, SocketCloser>;
122 bool operator<(
const sockaddr_storage &left,
const sockaddr_storage &right);
128 handle = curl_easy_init();
129 if (handle ==
nullptr) {
130 throw std::runtime_error(
"Could not initialize curl handle");
134 if (handle !=
nullptr) {
135 curl_easy_cleanup(handle);
138 CURL *
get() {
return handle; }
144 template <
typename... T>
145 static void curlEasySetoptWrapper(CURL *curl_handle, CURLoption option, T &&... args) {
146 const CURLcode retval = curl_easy_setopt(curl_handle, option, std::forward<T>(args)...);
148 throw std::runtime_error(std::string(
"curl_easy_setopt error: ") + curl_easy_strerror(retval));
156 using _Single_object = std::unique_ptr<T>;
161 using _Unknown_bound = std::unique_ptr<T[]>;
164 template <
class T,
size_t N>
166 using _Known_bound = void;
169 template <
class T,
class... Args>
170 typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
171 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
176 using U =
typename std::remove_extent<T>::type;
177 return std::unique_ptr<T>(
new U[n]());
180 template <
class T,
class... Args>
RAII Temporary file creation.