aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/copy_cmd.cpp
blob: 530661607f1456fa5f7883bc4379780601515f90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright Epic Games, Inc. All Rights Reserved.

#include "copy_cmd.h"

#include <zencore/filesystem.h>
#include <zencore/fmtutils.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_options()("must-clone",
							"Always perform block clone (fails if clone is not possible)",
							cxxopts::value(m_MustClone)->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.parse_positional({"source", "target"});
}

CopyCommand::~CopyCommand() = default;

void
CopyCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	ZEN_UNUSED(GlobalOptions);

	if (!ParseOptions(argc, argv))
	{
		return;
	}

	// Validate arguments

	if (m_CopySource.empty())
		throw OptionParseException("'--source' is required", m_Options.help());

	if (m_CopyTarget.empty())
		throw OptionParseException("'--target' is required", m_Options.help());

	std::filesystem::path FromPath = m_CopySource;
	std::filesystem::path 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 = IsFile(m_CopySource);
	const bool IsDirCopy  = IsDir(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 (IsFile(ToPath))
		{
			throw std::runtime_error("Attempted copy of directory into file");
		}

		if (!IsDir(ToPath))
		{
			CreateDirectories(ToPath);
		}

		std::filesystem::path ToCanonical = std::filesystem::canonical(ToPath, Ec);

		if (!Ec)
		{
			if (ToCanonical.generic_string().starts_with(FromCanonical.generic_string()) ||
				FromCanonical.generic_string().starts_with(ToCanonical.generic_string()))
			{
				throw std::runtime_error("Invalid parent/child relationship for source/target directories");
			}
		}

		// Multi file copy

		ZEN_CONSOLE("copying {} -> {}", FromPath, ToPath);

		zen::Stopwatch Timer;

		struct CopyVisitor : public FileSystemTraversal::TreeVisitor
		{
			CopyVisitor(std::filesystem::path InBasePath, zen::CopyFileOptions InCopyOptions)
			: BasePath(InBasePath)
			, CopyOptions(InCopyOptions)
			{
			}

			virtual void VisitFile(const std::filesystem::path& Parent,
								   const path_view&				File,
								   uint64_t						FileSize,
								   uint32_t,
								   uint64_t) override
			{
				ZEN_UNUSED(FileSize);
				std::error_code				Ec;
				const std::filesystem::path Relative = std::filesystem::relative(Parent, BasePath, Ec);

				if (Ec)
				{
					FailedFileCount++;
				}
				else
				{
					const std::filesystem::path FromPath = Parent / File;
					const std::filesystem::path ToPath	 = TargetPath / Relative / File;

					try
					{
						zen::CreateDirectories(TargetPath / Relative);
						if (zen::CopyFile(FromPath, ToPath, CopyOptions))
						{
							++FileCount;
							ByteCount += FileSize;
						}
						else
						{
							throw std::logic_error("CopyFile failed in an unexpected way");
						}
					}
					catch (const std::exception& Ex)
					{
						++FailedFileCount;

						ZEN_CONSOLE_ERROR("Failed to copy '{}' to '{}': '{}'", FromPath, ToPath, Ex.what());
					}
				}
			}

			virtual bool VisitDirectory(const std::filesystem::path&, const path_view&, uint32_t) override { return true; }

			std::filesystem::path BasePath;
			std::filesystem::path TargetPath;
			zen::CopyFileOptions  CopyOptions;
			int					  FileCount		  = 0;
			uint64_t			  ByteCount		  = 0;
			int					  FailedFileCount = 0;
		};

		zen::CopyFileOptions CopyOptions;
		CopyOptions.EnableClone = !m_NoClone;
		CopyOptions.MustClone	= m_MustClone;

		CopyVisitor Visitor{FromPath, CopyOptions};
		Visitor.TargetPath = ToPath;

		FileSystemTraversal Traversal;
		Traversal.TraverseFileSystem(FromPath, Visitor);

		ZEN_CONSOLE("Copy of {} files ({}) completed in {} ({})",
					Visitor.FileCount,
					NiceBytes(Visitor.ByteCount),
					zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs()),
					zen::NiceRate(Visitor.ByteCount, (uint32_t)Timer.GetElapsedTimeMs()));

		if (Visitor.FailedFileCount)
		{
			throw std::runtime_error(fmt::format("{} file copy operations FAILED", Visitor.FailedFileCount));
		}
	}
	else
	{
		// Single file copy

		zen::Stopwatch Timer;

		zen::CopyFileOptions CopyOptions;
		CopyOptions.EnableClone = !m_NoClone;

		zen::CreateDirectories(ToPath.parent_path());
		if (zen::CopyFile(FromPath, ToPath, CopyOptions))
		{
			ZEN_CONSOLE("Copy completed in {}", zen::NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
		}
		else
		{
			throw std::runtime_error(fmt::format("Failed to copy '{}' to '{}'", FromPath, ToPath));
		}
	}
}

}  // namespace zen