softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::FileToolkitWithUndo Class Reference
Inheritance diagram for softwarecontainer::FileToolkitWithUndo:
[legend]

Public Member Functions

bool bindMount (const std::string &src, const std::string &dst, const std::string &tmpContainerRoot, bool readOnly, bool writeBufferEnabled=false)
 bindMount Bind mount a src directory to another position dst. More...
 
bool tmpfsMount (const std::string dst, const int maxSize)
 tmpfsMount Mount a tmpfs in the dst path and limit size of the tmpfs to maxSize More...
 

Protected Member Functions

bool writeToFile (const std::string &path, const std::string &content)
 
void markFileForDeletion (const std::string &path)
 
bool overlayMount (const std::string &lower, const std::string &upper, const std::string &work, const std::string &dst)
 overlayMount Mount a directory with an overlay on top of it. More...
 
bool syncOverlayMount (const std::string &lower, const std::string &upper)
 syncOverlayMount Copy the directory structure from upper layer to the lower layer More...
 
bool createSharedMountPoint (const std::string &path)
 createSharedMountPoint Make the mount point shared, ie new mount points created in one bind mount will also be created in the other mount point. More...
 
bool pathInList (const std::string path)
 checks whether given path is already added to clean up handlers or not More...
 

Protected Attributes

std::vector< std::unique_ptr< CleanUpHandler > > m_cleanupHandlers
 m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo that will be run from the destructor. More...
 
std::vector< std::unique_ptr< CreateDir > > m_createDirList
 m_createDirList A vector of CreateDir classes. More...
 

Detailed Description

Definition at line 28 of file filetoolkitwithundo.h.

Member Function Documentation

bool softwarecontainer::FileToolkitWithUndo::bindMount ( const std::string &  src,
const std::string &  dst,
const std::string &  tmpContainerRoot,
bool  readOnly,
bool  writeBufferEnabled = false 
)

bindMount Bind mount a src directory to another position dst.

Parameters
srcPath to mount from
dstPath to mount to
readOnlyMake the bind mount destination read only
writeBufferEnabledEnable write buffers on the bind mount.
Returns
true on success, false on failure

Definition at line 53 of file filetoolkitwithundo.cpp.

References softwarecontainer::existsInFileSystem(), softwarecontainer::isDirectory(), and m_createDirList.

Referenced by softwarecontainer::Container::bindMountInContainer().

58 {
59  unsigned long flags = 0;
60  std::string fstype;
61  int mountRes;
62  std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(new CreateDir());
63 
64  if (!existsInFileSystem(src)) {
65  log_error() << src << " does not exist on the host, can not bindMount";
66  return false;
67  }
68 
69  if (!existsInFileSystem(dst)) {
70  log_error() << dst << " does not exist on the host, can not bindMount";
71  return false;
72  }
73 
74  log_debug() << "Bind-mounting " << src << " in " << dst << ", flags: " << flags;
75 
76  if(writeBufferEnabled && isDirectory(src)) {
77  std::string upperDir , workDir;
78 
79  // In case the tmpContainerRoot is set to nothing we need to create a
80  // good bindmount directory.
81  if (tmpContainerRoot == "") {
82  upperDir = createDirInstance->createTempDirectoryFromTemplate("/tmp/sc-bindmount-upper-XXXXXX");
83  workDir = createDirInstance->createTempDirectoryFromTemplate("/tmp/sc-bindmount-work-XXXXXX");
84  } else {
85  upperDir = createDirInstance->createTempDirectoryFromTemplate(tmpContainerRoot + "/bindmount-upper-XXXXXX");
86  workDir = createDirInstance->createTempDirectoryFromTemplate(tmpContainerRoot + "/bindmount-work-XXXXXX");
87  }
88  fstype.assign("overlay");
89 
90  std::ostringstream os;
91  os << "lowerdir=" << src << ",upperdir=" << upperDir << ",workdir=" << workDir;
92 
93  log_debug() << "writeBufferEnabled, config: " << os.str();
94 
95  mountRes = mount("overlay", dst.c_str(), fstype.c_str(), flags, os.str().c_str());
96  log_debug() << "mountRes: " << mountRes;
97  } else {
98  const void *data = nullptr;
99  flags = MS_BIND;
100  mountRes = mount(src.c_str(), dst.c_str(), fstype.c_str(), flags, data);
101  }
102 
103  if (mountRes == 0) {
104  log_verbose() << "Bind-mounted folder " << src << " in " << dst;
105  } else {
106  log_error() << "Could not mount into container: src=" << src
107  << " , dst=" << dst << " err=" << strerror(errno);
108  return false;
109  }
110 
111  if (readOnly && !writeBufferEnabled) {
112  const void *data = nullptr;
113 
114  flags = MS_REMOUNT | MS_RDONLY | MS_BIND;
115 
116  log_debug() << "Re-mounting read-only" << src << " in "
117  << dst << ", flags: " << flags;
118  mountRes = mount(src.c_str(), dst.c_str(), fstype.c_str(), flags, data);
119  if (mountRes != 0) {
120  // Failure
121  log_error() << "Could not re-mount " << src << " , read-only on "
122  << dst << " err=" << strerror(errno);
123  return false;
124  }
125  }
126  m_createDirList.push_back(std::move(createDirInstance));
127  return true;
128 }
std::vector< std::unique_ptr< CreateDir > > m_createDirList
m_createDirList A vector of CreateDir classes.
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
bool existsInFileSystem(const std::string &path)
existsInFileSystem Check if path exists

Here is the call graph for this function:

Here is the caller graph for this function:

bool softwarecontainer::FileToolkitWithUndo::tmpfsMount ( const std::string  dst,
const int  maxSize 
)

tmpfsMount Mount a tmpfs in the dst path and limit size of the tmpfs to maxSize

Parameters
dstThe destination to mount a tmpfs on
maxSizeThe max size of the tmpfs being mounted
Returns

Definition at line 177 of file filetoolkitwithundo.cpp.

References softwarecontainer::isDirectoryEmpty(), m_cleanupHandlers, and m_createDirList.

Referenced by softwarecontainer::SoftwareContainer::SoftwareContainer().

178 {
179  std::string fstype = "tmpfs";
180  unsigned long flags = 0;
181  std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(new CreateDir());
182 
183  if (!createDirInstance->createDirectory(dst)) {
184  log_error() << "Failed to create " << dst << " directory for tmpfs mount";
185  return false;
186  }
187 
188  std::string mountoptions = logging::StringBuilder() << "size=" << maxSize;
189 
190  bool directoryIsEmpty = isDirectoryEmpty(dst);
191 
192  int mountRes = mount("tmpfs", dst.c_str(), fstype.c_str(), flags, mountoptions.c_str());
193 
194  if (0 == mountRes) {
195  log_verbose() << "tmpfs mounted in " << dst;
196  m_cleanupHandlers.emplace_back(new MountCleanUpHandler(dst));
197  } else {
198  log_error() << "Could not mount tmpfs into directory: " << dst << " size=" << maxSize;
199  return false;
200  }
201 
202  m_createDirList.push_back(std::move(createDirInstance));
203  return true;
204 }
std::vector< std::unique_ptr< CleanUpHandler > > m_cleanupHandlers
m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo th...
bool isDirectoryEmpty(const std::string &path)
isDirectoryEmpty Check if path is empty
std::vector< std::unique_ptr< CreateDir > > m_createDirList
m_createDirList A vector of CreateDir classes.

Here is the call graph for this function:

Here is the caller graph for this function:

bool softwarecontainer::FileToolkitWithUndo::overlayMount ( const std::string &  lower,
const std::string &  upper,
const std::string &  work,
const std::string &  dst 
)
protected

overlayMount Mount a directory with an overlay on top of it.

An overlay protects the lower filesystem from writes by writing to the upper file system through the work directory.

Parameters
lowerThe lower file system, this will be read only.
upperThe upper file system, this can be a tmpfs/ramfs of some kind. This is where final writes wind up
workThis is a work directory, preferably a tmpfs/ramfs of some kind. This is where writes wind up temporarily.
dstWhere the overlay filesystem will be mounted.
Returns
true on success, false on failure

Definition at line 130 of file filetoolkitwithundo.cpp.

References m_cleanupHandlers, and m_createDirList.

Referenced by softwarecontainer::Container::create().

134 {
135  std::string fstype = "overlay";
136  unsigned long flags = 0;
137  std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(new CreateDir());
138 
139  if (!createDirInstance->createDirectory(lower)
140  || !createDirInstance->createDirectory(upper)
141  || !createDirInstance->createDirectory(work)
142  || !createDirInstance->createDirectory(dst))
143  {
144  log_error() << "Failed to create lower/upper/work directory for overlayMount. lowerdir=" <<
145  lower << ", upperdir=" << upper << ", workdir=" << work;
146  return false;
147  }
148 
149  std::string mountoptions = logging::StringBuilder() << "lowerdir=" << lower
150  << ",upperdir=" << upper
151  << ",workdir=" << work;
152 
153  int mountRes = mount("overlay", dst.c_str(), fstype.c_str(), flags, mountoptions.c_str());
154 
155  if (mountRes == 0) {
156  log_verbose() << "overlayMounted folder " << lower << " in " << dst;
157  m_cleanupHandlers.emplace_back(new MountCleanUpHandler(dst));
158  m_cleanupHandlers.emplace_back(new OverlaySyncCleanupHandler(upper, lower));
159  } else {
160  log_error() << "Could not mount into container: upper=" << upper
161  << ",lower=" << lower
162  << ",work=" << work
163  << " at dst=" << dst << " err=" << strerror(errno);
164  return false;
165  }
166 
167  m_createDirList.push_back(std::move(createDirInstance));
168  return true;
169 }
std::vector< std::unique_ptr< CleanUpHandler > > m_cleanupHandlers
m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo th...
std::vector< std::unique_ptr< CreateDir > > m_createDirList
m_createDirList A vector of CreateDir classes.

Here is the caller graph for this function:

bool softwarecontainer::FileToolkitWithUndo::syncOverlayMount ( const std::string &  lower,
const std::string &  upper 
)
protected

syncOverlayMount Copy the directory structure from upper layer to the lower layer

Parameters
lowerThe lower layer used in an overlay file system.
upperThe upper layer in an overlay file system.
Returns
true on success, false on failure

Definition at line 171 of file filetoolkitwithundo.cpp.

References softwarecontainer::RecursiveCopy::copy(), and softwarecontainer::RecursiveCopy::getInstance().

173 {
174  return RecursiveCopy::getInstance().copy(upper, lower);
175 }
static RecursiveCopy & getInstance()
getInstance Gets the RecursiveCopy instance.
bool copy(std::string src, std::string dst)
copy Copy files from src to dst

Here is the call graph for this function:

bool softwarecontainer::FileToolkitWithUndo::createSharedMountPoint ( const std::string &  path)
protected

createSharedMountPoint Make the mount point shared, ie new mount points created in one bind mount will also be created in the other mount point.

Parameters
pathThe mount path to make shared.
Returns
true on success, false on failure

Definition at line 206 of file filetoolkitwithundo.cpp.

References m_cleanupHandlers.

Referenced by softwarecontainer::Container::initialize().

207 {
208  auto mountRes = mount(path.c_str(), path.c_str(), "", MS_BIND, nullptr);
209  if (mountRes != 0) {
210  log_error() << "Could not bind mount " << path << " to itself";
211  return false;
212  }
213 
214  mountRes = mount(path.c_str(), path.c_str(), "", MS_UNBINDABLE, nullptr);
215  if (mountRes != 0) {
216  log_error() << "Could not make " << path << " unbindable";
217  return false;
218  }
219 
220  mountRes = mount(path.c_str(), path.c_str(), "", MS_SHARED, nullptr);
221  if (mountRes != 0) {
222  log_error() << "Could not make " << path << " shared";
223  return false;
224  }
225 
226  m_cleanupHandlers.emplace_back(new MountCleanUpHandler(path));
227  log_debug() << "Created shared mount point at " << path;
228 
229  return true;
230 }
std::vector< std::unique_ptr< CleanUpHandler > > m_cleanupHandlers
m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo th...

Here is the caller graph for this function:

bool softwarecontainer::FileToolkitWithUndo::pathInList ( const std::string  path)
protected

checks whether given path is already added to clean up handlers or not

This function will be called only before adding new CleanUpHandler for FileCleanUpHandler and DirectoryCleanUpHandler. The reason behind this, only indicated CleanUpHandlers are related with file/directory removal operations.

Parameters
astring path name to check
Returns
true if the path is already exist, false otherwise

Definition at line 232 of file filetoolkitwithundo.cpp.

References m_cleanupHandlers.

Referenced by softwarecontainer::Container::bindMountInContainer().

233 {
234 
235  for (auto &element : m_cleanupHandlers) {
236  if (element->queryName() == path) {
237  return true;
238  }
239  }
240  return false;
241 }
std::vector< std::unique_ptr< CleanUpHandler > > m_cleanupHandlers
m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo th...

Here is the caller graph for this function:

Field Documentation

std::vector<std::unique_ptr<CleanUpHandler> > softwarecontainer::FileToolkitWithUndo::m_cleanupHandlers
protected

m_cleanupHandlers A vector of cleanupHandlers added during the lifetime of the FileToolKitWithUndo that will be run from the destructor.

Definition at line 121 of file filetoolkitwithundo.h.

Referenced by softwarecontainer::Container::bindMountInContainer(), createSharedMountPoint(), overlayMount(), pathInList(), and tmpfsMount().

std::vector<std::unique_ptr<CreateDir> > softwarecontainer::FileToolkitWithUndo::m_createDirList
protected

m_createDirList A vector of CreateDir classes.

This class handles directory cleaning operations when either interfered errors or when its destructor runs.

Definition at line 127 of file filetoolkitwithundo.h.

Referenced by bindMount(), softwarecontainer::Container::bindMountInContainer(), softwarecontainer::Container::initialize(), overlayMount(), softwarecontainer::SoftwareContainer::previouslyConfigured(), and tmpfsMount().


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