softwarecontainer  0.18.0-739e8d7 2017-05-04
createdir.cpp
1 /*
2  * Copyright (C) 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 "createdir.h"
21 #include "softwarecontainererror.h"
22 
23 #include <sys/stat.h>
24 
25 namespace softwarecontainer {
26 
27 CreateDir::~CreateDir()
28 {
29  bool success = true;
30 
31  while (!m_rollbackCleaners.empty()) {
32  auto cuHandler = m_rollbackCleaners.back();
33  m_rollbackCleaners.pop_back();
34 
35  if (!cuHandler.clean()) {
36  success = false;
37  }
38  }
39 
40  if(!success) {
41  log_error() << "One or more cleanup handlers returned error status, please check the log";
42  }
43 }
44 
45 
46 bool CreateDir::createParentDirectory(const std::string path)
47 {
48  log_debug() << "Creating parent directories for " << path;
49  std::string parent = parentPath(path);
50 
51  if (!createDirectory(parent)) {
52  log_error() << "Could not create directory " << parent;
53  return false;
54  }
55 
56  return true;
57 }
58 
59 bool CreateDir::createDirectory(const std::string path)
60 {
61  if (isDirectory(path)) {
62  return true;
63  }
64 
65  if(!createParentDirectory(path)) {
66  log_error() << "Couldn't create parent directory for " << path;
67  return false;
68  }
69 
70  if (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == -1) {
71  log_error() << "Could not create directory " << path << ": " << strerror(errno);
72  return false;
73  }
74 
75  if (!pathInList(path)){
76  m_rollbackCleaners.emplace_back(path);
77  }
78  return true;
79 }
80 
81 std::string CreateDir::createTempDirectoryFromTemplate(std::string templ)
82 {
83  char *dir = const_cast<char*>(templ.c_str());
84  dir = mkdtemp(dir);
85  if (dir == nullptr) {
86  std::string message = "Failed to create directory from template: " + std::string(strerror(errno));
87  log_warning() << message;
88  throw SoftwareContainerError(message);
89  }
90 
91  m_rollbackCleaners.emplace_back(std::string(dir));
92  return std::string(dir);
93 }
94 
95 bool CreateDir::pathInList(const std::string path)
96 {
97  for (auto &element : m_rollbackCleaners) {
98  if (element.queryName() == path) {
99  return true;
100  }
101  }
102  return false;
103 }
104 
105 }
bool createDirectory(const std::string path)
createDirectory creates a directory according to given path.
Definition: createdir.cpp:59
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
std::string createTempDirectoryFromTemplate(std::string templatePath)
createTempDirectoryFromTemplate creates a uniquely named directory according to templatePath.
Definition: createdir.cpp:81
Developers guide to adding a config item: