aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/copy_cmd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zen/cmds/copy_cmd.cpp')
-rw-r--r--src/zen/cmds/copy_cmd.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/zen/cmds/copy_cmd.cpp b/src/zen/cmds/copy_cmd.cpp
index e5ddbfa85..d68a99616 100644
--- a/src/zen/cmds/copy_cmd.cpp
+++ b/src/zen/cmds/copy_cmd.cpp
@@ -45,6 +45,22 @@ CopyCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
FromPath = m_CopySource;
ToPath = m_CopyTarget;
+ std::error_code Ec;
+ std::filesystem::path FromCanonical = std::filesystem::canonical(FromPath, Ec);
+
+ if (!Ec)
+ {
+ std::filesystem::path ToCanonical = std::filesystem::canonical(ToPath, Ec);
+
+ if (!Ec)
+ {
+ if (FromCanonical == ToCanonical)
+ {
+ throw std::runtime_error("Target and source must be distinct files or directories");
+ }
+ }
+ }
+
const bool IsFileCopy = std::filesystem::is_regular_file(m_CopySource);
const bool IsDirCopy = std::filesystem::is_directory(m_CopySource);
@@ -76,6 +92,17 @@ CopyCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
std::filesystem::create_directories(ToPath);
}
+ std::filesystem::path ToCanonical = std::filesystem::canonical(ToPath, Ec);
+
+ if (!Ec)
+ {
+ if (!std::filesystem::relative(ToCanonical, FromCanonical).empty() ||
+ !std::filesystem::relative(FromCanonical, ToCanonical).empty())
+ {
+ throw std::runtime_error("Invalid parent/child relationship for source/target directories");
+ }
+ }
+
// Multi file copy
ZEN_CONSOLE("copying {} -> {}", FromPath, ToPath);