20 #include "softwarecontainer-common.h" 21 #include "softwarecontainererror.h" 36 LOG_DECLARE_DEFAULT_CONTEXT(defaultLogContext,
"MAIN",
"Main context");
38 bool getStat(
const std::string &path,
struct stat &st)
40 return stat(path.c_str(), &st) == 0;
46 if (getStat(path, st)) {
47 return S_ISDIR(st.st_mode);
55 constexpr
int EMPTY_DIR_SIZE=2;
57 DIR *dir = opendir(path.c_str());
61 while ((d = readdir(dir)) != NULL) {
63 if(n > EMPTY_DIR_SIZE) {
68 if (n <= EMPTY_DIR_SIZE) {
78 if (getStat(path, st)) {
79 return S_ISREG(st.st_mode);
88 if (getStat(path, st)) {
89 return S_ISFIFO(st.st_mode);
98 if (getStat(path, st)) {
99 return S_ISSOCK(st.st_mode);
108 return getStat(path, st);
111 std::string parentPath(
const std::string &path_)
113 char *path = strdup(path_.c_str());
114 if (
nullptr == path) {
115 std::string message =
"Failed to do strdup on " + path_;
116 log_error() << message;
120 std::string parent = dirname(path);
125 std::string baseName(
const std::string &path)
127 return Glib::path_get_basename(path);
130 std::string buildPath(
const std::string &arg1,
const std::string &arg2)
132 return Glib::build_filename(arg1, arg2);
135 std::string buildPath(
const std::string &arg1,
const std::string &arg2,
const std::string &arg3)
137 return buildPath(buildPath(arg1, arg2), arg3);
140 bool touch(
const std::string &path)
142 int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK | O_LARGEFILE, 0666);
151 bool writeToFile(
const std::string &path,
const std::string &content)
154 log_verbose() <<
"writing to " << path <<
" : " << content;
155 std::ofstream out(path);
168 bool readFromFile(
const std::string &path, std::string &content)
171 std::ifstream t(path);
173 std::stringstream buffer;
175 content = buffer.str();
186 bool parseInt(
const char *arg,
int *result)
189 long value = strtol(arg, &end, 10);
190 if (end == arg || *end !=
'\0' || errno == ERANGE) {
bool isDirectoryEmpty(const std::string &path)
isDirectoryEmpty Check if path is empty
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
bool isPipe(const std::string &path)
isPipe Check if path is a pipe
bool existsInFileSystem(const std::string &path)
existsInFileSystem Check if path exists
bool isFile(const std::string &path)
isFile Check if path is a file
bool isSocket(const std::string &path)
isSocket Check if path is a socket
Developers guide to adding a config item: