4 #include <boost/filesystem.hpp>
9 #include <netinet/in.h>
11 #include "json/json.h"
14 static std::string fromBase64(std::string base64_string);
15 static std::string toBase64(
const std::string &tob64);
16 static std::string stripQuotes(
const std::string &value);
17 static std::string addQuotes(
const std::string &value);
18 static std::string extractField(
const std::string &in,
unsigned int field_id);
19 static Json::Value parseJSON(
const std::string &json_str);
20 static Json::Value parseJSONFile(
const boost::filesystem::path &filename);
21 static std::string jsonToStr(
const Json::Value &json);
22 static std::string jsonToCanonicalStr(
const Json::Value &json);
23 static std::string genPrettyName();
24 static std::string readFile(
const boost::filesystem::path &filename,
bool trim =
false);
26 static void writeFile(
const boost::filesystem::path &filename,
const char *content,
size_t size);
27 static void writeFile(
const boost::filesystem::path &filename,
const std::string &content,
28 bool create_directories =
true);
29 static void writeFile(
const boost::filesystem::path &filename,
const Json::Value &content,
30 bool create_directories =
true);
31 static void copyDir(
const boost::filesystem::path &from,
const boost::filesystem::path &to);
32 static std::string readFileFromArchive(std::istream &as,
const std::string &filename,
bool trim =
false);
33 static void writeArchive(
const std::map<std::string, std::string> &entries, std::ostream &as);
34 static Json::Value getHardwareInfo();
35 static Json::Value getNetworkInfo();
36 static std::string getHostname();
37 static std::string randomUuid();
38 static sockaddr_storage ipGetSockaddr(
int fd);
39 static std::string ipDisplayName(
const sockaddr_storage &saddr);
40 static int ipPort(
const sockaddr_storage &saddr);
41 static int shell(
const std::string &command, std::string *output,
bool include_stderr =
false);
42 static boost::filesystem::path absolutePath(
const boost::filesystem::path &root,
const boost::filesystem::path &file);
43 static void setSocketPort(sockaddr_storage *addr, in_port_t port);
44 static void createDirectories(
const boost::filesystem::path &path, mode_t mode);
45 static bool createSecureDirectory(
const boost::filesystem::path &path);
46 static std::string urlEncode(
const std::string &input);
47 static CURL *curlDupHandleWrapper(CURL *curl_in,
bool using_pkcs11);
48 static std::vector<boost::filesystem::path> getDirEntriesByExt(
const boost::filesystem::path &dir_path,
49 const std::string &ext);
50 static void setStorageRootPath(
const std::string &storage_root_path);
51 static boost::filesystem::path getStorageRootPath();
53 static void setUserAgent(std::string user_agent);
54 static const char *getUserAgent();
56 static void setCaPath(boost::filesystem::path path);
57 static const char *getCaPath();
60 static std::string storage_root_path_;
61 static std::string user_agent_;
62 static boost::filesystem::path ca_path_;
74 void PutContents(
const std::string &contents)
const;
75 boost::filesystem::path Path()
const;
76 std::string PathString()
const;
79 boost::filesystem::path tmp_name_;
88 boost::filesystem::path Path()
const;
89 std::string PathString()
const;
90 boost::filesystem::path operator/(
const boost::filesystem::path &subdir)
const;
93 boost::filesystem::path tmp_name_;
103 BasedPath(boost::filesystem::path p) : p_(std::move(p)) {}
105 boost::filesystem::path get(
const boost::filesystem::path &base)
const {
107 return Utils::absolutePath(base, p_);
110 bool empty()
const {
return p_.empty(); }
111 bool operator==(
const BasedPath &b)
const {
return p_ == b.p_; }
112 bool operator!=(
const BasedPath &b)
const {
return !(*
this == b); }
115 boost::filesystem::path p_;
122 template <
typename T>
123 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
125 bool operator<(
const sockaddr_storage &left,
const sockaddr_storage &right);
130 Socket(
int fd) : socket_fd_(fd) {}
136 int &operator*() {
return socket_fd_; }
137 std::string toString()
const;
140 void bind(in_port_t port,
bool reuse =
true)
const;
148 ConnectionSocket(
const std::string &ip, in_port_t port, in_port_t bind_port = 0);
155 struct sockaddr_in remote_sock_address_;
161 in_port_t port()
const {
return _port; }
172 CURL *get() {
return handle; }
178 template <
typename... T>
179 static void curlEasySetoptWrapper(CURL *curl_handle, CURLoption option, T &&... args) {
180 const CURLcode retval = curl_easy_setopt(curl_handle, option, std::forward<T>(args)...);
182 throw std::runtime_error(std::string(
"curl_easy_setopt error: ") + curl_easy_strerror(retval));
190 using _Single_object = std::unique_ptr<T>;
195 using _Unknown_bound = std::unique_ptr<T[]>;
198 template <
class T,
size_t N>
200 using _Known_bound = void;
203 template <
class T,
class... Args>
204 typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
205 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
209 typename _Unique_if<T>::_Unknown_bound make_unique(
size_t n) {
210 using U =
typename std::remove_extent<T>::type;
211 return std::unique_ptr<T>(
new U[n]());
214 template <
class T,
class... Args>
215 typename _Unique_if<T>::_Known_bound make_unique(Args &&...) =
delete;