*/
void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth);
+/**
+ * @brief create the new directory.
+ * @param[in] source path
+ * @return return true when the directory was created.
+ */
bool createDir(const bf::path& path);
-bool copyDir(const bf::path& path, const bf::path& dst, FSFlag flags = FS_NONE, bool skip_symlink = false);
-bool copyFile(const bf::path& path, const bf::path& dst);
-bool moveFile(const bf::path& path, const bf::path& dst, bool force);
+
+/**
+ * @brief copy the directory.
+ * @param[in] path to the source directory
+ * @param[in] path to the target directory
+ * @param[in] filesystem flag
+ * @return return true when the directory was copied.
+ */
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags = FS_NONE);
+
+/**
+ * @brief copy the file.
+ * @param[in] path to the source file
+ * @param[in] path to the target file
+ * @return return true when the file was copied.
+ */
+bool copyFile(const bf::path& path1, const bf::path& path2);
+
+/**
+ * @brief moves of renames the file or directory.
+ * @param[in] path to the source file
+ * @param[in] path to the target file
+ * @return return true when the file was moved.
+ */
+bool moveFile(const bf::path& path1, const bf::path& path2);
+
+/**
+ * @brief removes the file or empty directory.
+ * @param[in] source path
+ * @return return true when the file was deleted.
+ */
bool removeFile(const bf::path& path);
+
+/**
+ * @brief removes the file or directory and all its contents.
+ * @param[in] source path
+ * @return return true when the file or directory was deleted.
+ */
bool removeAll(const bf::path& path);
#endif /* __UTILS_H__ */
for (auto& unp : updateTac) {
for (auto& bck : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet) / unp.substr(0, unp.find('/')))) {
if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
- if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())), FSFlag::FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
+ if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())))) {
_ERR("Failed to move %s to %s",
bck.path().c_str(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())).c_str());
return 0;
for (auto& np : tacDB) {
for (auto& bck : bf::recursive_directory_iterator(bf::path(mOptUsrDotnet) / np.substr(0, np.find('/')))) {
if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), mBackup.c_str()) != NULL) {
- if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())), FSFlag::FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
+ if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())))) {
_ERR("Failed to move %s to %s",
bck.path().c_str(), bck.path().string().substr(0, bck.path().string().rfind(mBackup.c_str())).c_str());
return 0;
return true;
}
-static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& dst) {
+static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) {
if (!bf::exists(path)) {
- _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), dst.c_str());
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
return false;
}
bf::perms permissions = bf::status(path).permissions();
if (stat(path.c_str(), &stats) != 0) {
return false;
}
- if (!setDirOwnershipAndPermissions(dst, permissions, stats.st_uid, stats.st_gid)) {
- _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), dst.c_str());
+ if (!setDirOwnershipAndPermissions(path2, permissions, stats.st_uid, stats.st_gid)) {
+ _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
return false;
}
return true;
return true;
}
-bool copyDir(const bf::path& path, const bf::path& dst, FSFlag flags, bool skip_symlink) {
+bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) {
try {
// Check whether the function call is valid
- if (!bf::exists(path) || !bf::is_directory(path)) {
- _ERR("Source directory %s does not exist or is not a directory", path.c_str());
+ if (!bf::exists(path1) || !bf::is_directory(path1)) {
+ _ERR("Source directory %s does not exist or is not a directory", path1.c_str());
return false;
}
- if (!bf::exists(dst)) {
+ if (!bf::exists(path2)) {
// Create the destination directory
- if (!createDir(dst)) {
- _ERR("Unable to create destination directory %s", dst.c_str());
+ if (!createDir(path2)) {
+ _ERR("Unable to create destination directory %s", path2.c_str());
return false;
}
if (flags & FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS) {
- copyOwnershipAndPermissions(path, dst);
+ copyOwnershipAndPermissions(path1, path2);
}
} else {
if (!(flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE))) {
- _ERR("Destination directory %s already exists", dst.c_str());
+ _ERR("Destination directory %s already exists", path2.c_str());
return false;
}
if (flags & (FS_MERGE_OVERWRITE | FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS)) {
- copyOwnershipAndPermissions(path, dst);
+ copyOwnershipAndPermissions(path1, path2);
}
}
} catch (const bf::filesystem_error& error) {
}
// Iterate through the source directory
- for (bf::directory_iterator file(path); file != bf::directory_iterator(); ++file) {
+ for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
try {
bf::path current(file->path());
- bf::path target = dst / current.filename();
+ bf::path target = path2 / current.filename();
if (bf::is_symlink(symlink_status(current))) {
- if (skip_symlink) {
- continue;
- }
if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
continue;
}
}
} else if (bf::is_directory(current)) {
// Found directory: Recursion
- if (!copyDir(current, target, flags, skip_symlink)) {
+ if (!copyDir(current, target, flags)) {
return false;
}
} else {
return true;
}
-bool copyFile(const bf::path& path, const bf::path& dst) {
+bool copyFile(const bf::path& path1, const bf::path& path2) {
bs::error_code error;
- bf::copy_file(path, dst, bf::copy_option::overwrite_if_exists, error);
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
if (error) {
- _ERR("copy file %s due to error [%s]", path.c_str(), error.message().c_str());
+ _ERR("copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
return false;
}
return true;
}
-bool moveFile(const bf::path& path, const bf::path& dst, bool force) {
- if (!force && bf::exists(dst)) {
+bool moveFile(const bf::path& path1, const bf::path& path2) {
+ if (bf::exists(path2)) {
return false;
}
bs::error_code error;
- bf::rename(path, dst, error);
+ bf::rename(path1, path2, error);
if (error) {
- _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path.c_str(), error.message().c_str());
- bf::copy_file(path, dst, bf::copy_option::overwrite_if_exists, error);
+ _ERR("Cannot move file: %s. Will copy/remove... with error [%s]", path1.c_str(), error.message().c_str());
+ bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
if (error) {
- _ERR("Cannot copy file %s due to error [%s]", path.c_str(), error.message().c_str());
+ _ERR("Cannot copy file %s due to error [%s]", path1.c_str(), error.message().c_str());
return false;
}
- bf::remove_all(path, error);
+ bf::remove_all(path1, error);
if (error) {
- _ERR("Cannot remove old file when coping: %s with error [%s]", path.c_str(), error.message().c_str());
+ _ERR("Cannot remove old file when coping: %s with error [%s]", path1.c_str(), error.message().c_str());
return false;
}
}