2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef ANBOX_UTILS_H_
19 #define ANBOX_UTILS_H_
21 #include <boost/format.hpp>
29 std::vector<std::string> collect_arguments(int argc, char **argv);
31 std::string read_file_if_exists_or_throw(const std::string &file_path);
33 bool write_to_file(const std::string &file_path,
34 const std::string &content = "");
35 int write_to_fd(int fd, const char *content, ssize_t len);
36 int write_file_at(int dirfd, const char *path, const char *content);
38 bool string_starts_with(const std::string &text, const std::string &prefix);
40 std::vector<std::string> string_split(const std::string &text, char sep);
42 std::string strip_surrounding_quotes(const std::string &text);
44 std::string hex_dump(const uint8_t *data, uint32_t size);
46 std::string get_env_value(const std::string &name,
47 const std::string &default_value = "");
48 bool is_env_set(const std::string &name);
50 void ensure_paths(const std::vector<std::string> &paths);
52 std::string prefix_dir_from_env(const std::string &path,
53 const std::string &env_var);
55 std::string process_get_exe_path(const pid_t &pid);
57 bool is_mounted(const std::string &path);
59 std::string find_program_on_path(const std::string &name);
61 template <typename... Types>
62 static std::string string_format(const std::string &fmt_str, Types &&... args);
67 // Base case, just return the passed in boost::format instance.
68 inline boost::format &string_format(boost::format &f) { return f; }
69 // Sprintf recursively walks the parameter pack at compile time.
70 template <typename Head, typename... Tail>
71 inline boost::format &string_format(boost::format &f, Head const &head,
73 return string_format(f % head, std::forward<Tail>(tail)...);
77 template <typename... Types>
78 inline std::string anbox::utils::string_format(const std::string &format,
80 boost::format f(format);
81 return impl::string_format(f, std::forward<Types>(args)...).str();