21 #include <sys/mount.h> 23 #include "filetoolkitwithundo.h" 25 #include "directorycleanuphandler.h" 26 #include "filecleanuphandler.h" 27 #include "mountcleanuphandler.h" 28 #include "overlaysynccleanuphandler.h" 30 #include "recursivecopy.h" 34 FileToolkitWithUndo::~FileToolkitWithUndo()
49 log_warning() <<
"One or more cleanup handlers returned error status, please check the log";
54 const std::string &dst,
55 const std::string &tmpContainerRoot,
57 bool writeBufferEnabled)
59 unsigned long flags = 0;
62 std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(
new CreateDir());
65 log_error() << src <<
" does not exist on the host, can not bindMount";
70 log_error() << dst <<
" does not exist on the host, can not bindMount";
74 log_debug() <<
"Bind-mounting " << src <<
" in " << dst <<
", flags: " << flags;
77 std::string upperDir , workDir;
81 if (tmpContainerRoot ==
"") {
82 upperDir = createDirInstance->createTempDirectoryFromTemplate(
"/tmp/sc-bindmount-upper-XXXXXX");
83 workDir = createDirInstance->createTempDirectoryFromTemplate(
"/tmp/sc-bindmount-work-XXXXXX");
85 upperDir = createDirInstance->createTempDirectoryFromTemplate(tmpContainerRoot +
"/bindmount-upper-XXXXXX");
86 workDir = createDirInstance->createTempDirectoryFromTemplate(tmpContainerRoot +
"/bindmount-work-XXXXXX");
88 fstype.assign(
"overlay");
90 std::ostringstream os;
91 os <<
"lowerdir=" << src <<
",upperdir=" << upperDir <<
",workdir=" << workDir;
93 log_debug() <<
"writeBufferEnabled, config: " << os.str();
95 mountRes = mount(
"overlay", dst.c_str(), fstype.c_str(), flags, os.str().c_str());
96 log_debug() <<
"mountRes: " << mountRes;
98 const void *data =
nullptr;
100 mountRes = mount(src.c_str(), dst.c_str(), fstype.c_str(), flags, data);
104 log_verbose() <<
"Bind-mounted folder " << src <<
" in " << dst;
106 log_error() <<
"Could not mount into container: src=" << src
107 <<
" , dst=" << dst <<
" err=" << strerror(errno);
111 if (readOnly && !writeBufferEnabled) {
112 const void *data =
nullptr;
114 flags = MS_REMOUNT | MS_RDONLY | MS_BIND;
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);
121 log_error() <<
"Could not re-mount " << src <<
" , read-only on " 122 << dst <<
" err=" << strerror(errno);
131 const std::string &upper,
132 const std::string &work,
133 const std::string &dst)
135 std::string fstype =
"overlay";
136 unsigned long flags = 0;
137 std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(
new CreateDir());
139 if (!createDirInstance->createDirectory(lower)
140 || !createDirInstance->createDirectory(upper)
141 || !createDirInstance->createDirectory(work)
142 || !createDirInstance->createDirectory(dst))
144 log_error() <<
"Failed to create lower/upper/work directory for overlayMount. lowerdir=" <<
145 lower <<
", upperdir=" << upper <<
", workdir=" << work;
149 std::string mountoptions = logging::StringBuilder() <<
"lowerdir=" << lower
150 <<
",upperdir=" << upper
151 <<
",workdir=" << work;
153 int mountRes = mount(
"overlay", dst.c_str(), fstype.c_str(), flags, mountoptions.c_str());
156 log_verbose() <<
"overlayMounted folder " << lower <<
" in " << dst;
160 log_error() <<
"Could not mount into container: upper=" << upper
161 <<
",lower=" << lower
163 <<
" at dst=" << dst <<
" err=" << strerror(errno);
172 const std::string &upper)
179 std::string fstype =
"tmpfs";
180 unsigned long flags = 0;
181 std::unique_ptr<CreateDir> createDirInstance = std::unique_ptr<CreateDir>(
new CreateDir());
183 if (!createDirInstance->createDirectory(dst)) {
184 log_error() <<
"Failed to create " << dst <<
" directory for tmpfs mount";
188 std::string mountoptions = logging::StringBuilder() <<
"size=" << maxSize;
192 int mountRes = mount(
"tmpfs", dst.c_str(), fstype.c_str(), flags, mountoptions.c_str());
195 log_verbose() <<
"tmpfs mounted in " << dst;
198 log_error() <<
"Could not mount tmpfs into directory: " << dst <<
" size=" << maxSize;
208 auto mountRes = mount(path.c_str(), path.c_str(),
"", MS_BIND,
nullptr);
210 log_error() <<
"Could not bind mount " << path <<
" to itself";
214 mountRes = mount(path.c_str(), path.c_str(),
"", MS_UNBINDABLE,
nullptr);
216 log_error() <<
"Could not make " << path <<
" unbindable";
220 mountRes = mount(path.c_str(), path.c_str(),
"", MS_SHARED,
nullptr);
222 log_error() <<
"Could not make " << path <<
" shared";
227 log_debug() <<
"Created shared mount point at " << path;
236 if (element->queryName() == path) {
243 bool FileToolkitWithUndo::writeToFile(
const std::string &path,
const std::string &content)
245 if (!softwarecontainer::writeToFile(path, content)) {
252 log_debug() <<
"Successfully wrote to " << path;
256 void FileToolkitWithUndo::markFileForDeletion(
const std::string &path)
bool isDirectoryEmpty(const std::string &path)
isDirectoryEmpty Check if path is empty
The FileCleanUpHandler class is a subclass of CleanUpHandler that deletes a file. ...
The CreateDir class is responsible for creating new directories and removing them when it is necessar...
The OverlaySyncCleanupHandler class is used to copy files on cleanup and can be added to the CleanupH...
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
bool existsInFileSystem(const std::string &path)
existsInFileSystem Check if path exists
static RecursiveCopy & getInstance()
getInstance Gets the RecursiveCopy instance.
bool copy(std::string src, std::string dst)
copy Copy files from src to dst
Developers guide to adding a config item: