diff options
| author | Stefan Boberg <[email protected]> | 2023-05-02 10:01:47 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-02 10:01:47 +0200 |
| commit | 075d17f8ada47e990fe94606c3d21df409223465 (patch) | |
| tree | e50549b766a2f3c354798a54ff73404217b4c9af /src/zen/cmds/copy.cpp | |
| parent | fix: bundle shouldn't append content zip to zen (diff) | |
| download | archived-zen-075d17f8ada47e990fe94606c3d21df409223465.tar.xz archived-zen-075d17f8ada47e990fe94606c3d21df409223465.zip | |
moved source directories into `/src` (#264)
* moved source directories into `/src`
* updated bundle.lua for new `src` path
* moved some docs, icon
* removed old test trees
Diffstat (limited to 'src/zen/cmds/copy.cpp')
| -rw-r--r-- | src/zen/cmds/copy.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/zen/cmds/copy.cpp b/src/zen/cmds/copy.cpp new file mode 100644 index 000000000..6f6c078d4 --- /dev/null +++ b/src/zen/cmds/copy.cpp @@ -0,0 +1,95 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "copy.h" + +#include <zencore/filesystem.h> +#include <zencore/logging.h> +#include <zencore/string.h> +#include <zencore/timer.h> + +namespace zen { + +CopyCommand::CopyCommand() +{ + m_Options.add_options()("h,help", "Print help"); + m_Options.add_options()("no-clone", "Do not perform block clone", cxxopts::value(m_NoClone)->default_value("false")); + m_Options.add_option("", "s", "source", "Copy source", cxxopts::value(m_CopySource), "<file/directory>"); + m_Options.add_option("", "t", "target", "Copy target", cxxopts::value(m_CopyTarget), "<file/directory>"); + m_Options.add_option("", "", "positional", "Positional arguments", cxxopts::value(m_Positional), ""); + m_Options.parse_positional({"source", "target", "positional"}); +} + +CopyCommand::~CopyCommand() = default; + +int +CopyCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) +{ + ZEN_UNUSED(GlobalOptions); + + if (!ZenCmdBase::ParseOptions(argc, argv)) + { + return 0; + } + + // Validate arguments + + if (m_CopySource.empty()) + throw std::runtime_error("No source specified"); + + if (m_CopyTarget.empty()) + throw std::runtime_error("No target specified"); + + std::filesystem::path FromPath; + std::filesystem::path ToPath; + + FromPath = m_CopySource; + ToPath = m_CopyTarget; + + const bool IsFileCopy = std::filesystem::is_regular_file(m_CopySource); + const bool IsDirCopy = std::filesystem::is_directory(m_CopySource); + + if (!IsFileCopy && !IsDirCopy) + { + throw std::runtime_error("Invalid source specification (neither directory nor file)"); + } + + if (IsFileCopy && IsDirCopy) + { + throw std::runtime_error("Invalid source specification (both directory AND file!?)"); + } + + if (IsDirCopy) + { + if (std::filesystem::exists(ToPath)) + { + const bool IsTargetDir = std::filesystem::is_directory(ToPath); + if (!IsTargetDir) + { + if (std::filesystem::is_regular_file(ToPath)) + { + throw std::runtime_error("Attempted copy of directory into file"); + } + } + } + else + { + std::filesystem::create_directories(ToPath); + } + } + else + { + // Single file copy + + zen::Stopwatch Timer; + + zen::CopyFileOptions CopyOptions; + CopyOptions.EnableClone = !m_NoClone; + zen::CopyFile(FromPath, ToPath, CopyOptions); + + ZEN_CONSOLE("Copy completed in {}", zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs())); + } + + return 0; +} + +} // namespace zen |