softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::CreateDir Class Reference

The CreateDir class is responsible for creating new directories and removing them when it is necessary. More...

#include <createdir.h>

Public Member Functions

bool createDirectory (const std::string path)
 createDirectory creates a directory according to given path. More...
 
std::string createTempDirectoryFromTemplate (std::string templatePath)
 createTempDirectoryFromTemplate creates a uniquely named directory according to templatePath. More...
 

Detailed Description

The CreateDir class is responsible for creating new directories and removing them when it is necessary.

This class is created as a helper to FileToolkitWithUndo class. It adds value to FileToolkitWithUndo class by creating rollback option on failures.

Definition at line 33 of file createdir.h.

Member Function Documentation

bool softwarecontainer::CreateDir::createDirectory ( const std::string  path)

createDirectory creates a directory according to given path.

The directory will be removed when the object is destroyed.

Parameters
pathPath of directory to be created
Returns
true on success, false on failure

Definition at line 59 of file createdir.cpp.

References softwarecontainer::isDirectory().

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 }
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory

Here is the call graph for this function:

std::string softwarecontainer::CreateDir::createTempDirectoryFromTemplate ( std::string  templatePath)

createTempDirectoryFromTemplate creates a uniquely named directory according to templatePath.

Warning
The created directory will be destroyed when the object is destroyed
Parameters
templatePathis used to create the path of the temporary directory. The last six characters of template must be XXXXXX and these are replaced with a string that makes the directory name unique.
Exceptions
SoftwareContainerErrorif template path is not appropriate.
Returns
A string path pointing to the newly created temporary directory.

Definition at line 81 of file createdir.cpp.

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 }

The documentation for this class was generated from the following files: