softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer-common.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include "softwarecontainer-common.h"
21 #include "softwarecontainererror.h"
22 
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 #include <fstream>
27 #include <cstdarg>
28 
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <libgen.h>
34 
35 namespace softwarecontainer {
36  LOG_DECLARE_DEFAULT_CONTEXT(defaultLogContext, "MAIN", "Main context");
37 
38 bool getStat(const std::string &path, struct stat &st)
39 {
40  return stat(path.c_str(), &st) == 0;
41 }
42 
43 bool isDirectory(const std::string &path)
44 {
45  struct stat st;
46  if (getStat(path, st)) {
47  return S_ISDIR(st.st_mode);
48  }
49 
50  return false;
51 }
52 
53 bool isDirectoryEmpty(const std::string &path) {
54  int n = 0;
55  constexpr int EMPTY_DIR_SIZE=2;
56  struct dirent *d;
57  DIR *dir = opendir(path.c_str());
58  if (dir == NULL) { //Not a directory or doesn't exist
59  return true;
60  }
61  while ((d = readdir(dir)) != NULL) {
62  ++n;
63  if(n > EMPTY_DIR_SIZE) {
64  break;
65  }
66  }
67  closedir(dir);
68  if (n <= EMPTY_DIR_SIZE) { //Directory Empty
69  return true;
70  } else {
71  return false;
72  }
73 }
74 
75 bool isFile(const std::string &path)
76 {
77  struct stat st;
78  if (getStat(path, st)) {
79  return S_ISREG(st.st_mode);
80  }
81 
82  return false;
83 }
84 
85 bool isPipe(const std::string &path)
86 {
87  struct stat st;
88  if (getStat(path, st)) {
89  return S_ISFIFO(st.st_mode);
90  }
91 
92  return false;
93 }
94 
95 bool isSocket(const std::string &path)
96 {
97  struct stat st;
98  if (getStat(path, st)) {
99  return S_ISSOCK(st.st_mode);
100  }
101 
102  return false;
103 }
104 
105 bool existsInFileSystem(const std::string &path)
106 {
107  struct stat st;
108  return getStat(path, st);
109 }
110 
111 std::string parentPath(const std::string &path_)
112 {
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;
117  throw SoftwareContainerError(message);
118  }
119 
120  std::string parent = dirname(path);
121  free(path);
122  return parent;
123 }
124 
125 std::string baseName(const std::string &path)
126 {
127  return Glib::path_get_basename(path);
128 }
129 
130 std::string buildPath(const std::string &arg1, const std::string &arg2)
131 {
132  return Glib::build_filename(arg1, arg2);
133 }
134 
135 std::string buildPath(const std::string &arg1, const std::string &arg2, const std::string &arg3)
136 {
137  return buildPath(buildPath(arg1, arg2), arg3);
138 }
139 
140 bool touch(const std::string &path)
141 {
142  int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK | O_LARGEFILE, 0666);
143  if (fd != -1) {
144  close(fd);
145  return true;
146  } else {
147  return false;
148  }
149 }
150 
151 bool writeToFile(const std::string &path, const std::string &content)
152 {
153  bool ret = true;
154  log_verbose() << "writing to " << path << " : " << content;
155  std::ofstream out(path);
156  if (out.is_open()) {
157  out << content;
158  if (!out.good()) {
159  ret = false;
160  }
161  out.close();
162  } else {
163  ret = false;
164  }
165  return ret;
166 }
167 
168 bool readFromFile(const std::string &path, std::string &content)
169 {
170  bool ret = true;
171  std::ifstream t(path);
172  if (t.is_open()) {
173  std::stringstream buffer;
174  buffer << t.rdbuf();
175  content = buffer.str();
176  if (!t.good()) {
177  ret = false;
178  }
179  t.close();
180  } else {
181  ret = false;
182  }
183  return ret;
184 }
185 
186 bool parseInt(const char *arg, int *result)
187 {
188  char *end;
189  long value = strtol(arg, &end, 10);
190  if (end == arg || *end != '\0' || errno == ERANGE) {
191  return false;
192  }
193 
194  *result = value;
195  return true;
196 }
197 
198 } // namespace softwarecontainer
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: