From 22b013a59c4e0ad749017a3b13c0e10e73fdfd3e Mon Sep 17 00:00:00 2001 From: "j-h.choi" Date: Mon, 10 Jun 2019 15:04:05 +0900 Subject: [PATCH 1/1] Add comments for added function in utils.h --- NativeLauncher/inc/utils.h | 45 +++++++++++++++-- .../installer-plugin/prefer_nuget_cache_plugin.cc | 4 +- NativeLauncher/util/utils.cc | 57 ++++++++++------------ 3 files changed, 71 insertions(+), 35 deletions(-) diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index 9963e11..089f78c 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -114,11 +114,50 @@ typedef std::function FileReader; */ 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__ */ diff --git a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc index e0c9ffa..e5269a6 100644 --- a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc @@ -868,7 +868,7 @@ int unInstall_Undo() 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; @@ -892,7 +892,7 @@ int update_Undo() 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; diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index f93f7d1..5e15b43 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -248,9 +248,9 @@ static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permis 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(); @@ -258,8 +258,8 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& ds 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; @@ -278,29 +278,29 @@ bool createDir(const bf::path& path) { 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) { @@ -309,14 +309,11 @@ bool copyDir(const bf::path& path, const bf::path& dst, FSFlag flags, bool skip_ } // 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; } @@ -328,7 +325,7 @@ bool copyDir(const bf::path& path, const bf::path& dst, FSFlag flags, bool skip_ } } else if (bf::is_directory(current)) { // Found directory: Recursion - if (!copyDir(current, target, flags, skip_symlink)) { + if (!copyDir(current, target, flags)) { return false; } } else { @@ -362,32 +359,32 @@ bool copyDir(const bf::path& path, const bf::path& dst, FSFlag flags, bool skip_ 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; } } -- 2.7.4