2022-07-20 20:30:16 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2024-07-01 11:11:16 +00:00
|
|
|
#include "Common/Exception.h"
|
2022-12-08 17:53:05 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2024-05-27 11:44:45 +00:00
|
|
|
#include "DisksClient.h"
|
|
|
|
#include "ICommand.h"
|
2022-04-08 07:52:16 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2024-07-01 13:24:13 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-08 17:20:54 +00:00
|
|
|
class CommandCopy final : public ICommand
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
public:
|
2024-05-27 11:44:45 +00:00
|
|
|
explicit CommandCopy() : ICommand()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
command_name = "copy";
|
2024-06-14 13:02:41 +00:00
|
|
|
description = "Recursively copy data from `path-from` to `path-to`";
|
|
|
|
options_description.add_options()(
|
|
|
|
"disk-from", po::value<String>(), "disk from which we copy is executed (default value is a current disk)")(
|
|
|
|
"disk-to", po::value<String>(), "disk to which copy is executed (default value is a current disk)")(
|
|
|
|
"path-from", po::value<String>(), "path from which copy is executed (mandatory, positional)")(
|
2024-07-01 11:11:16 +00:00
|
|
|
"path-to", po::value<String>(), "path to which copy is executed (mandatory, positional)")(
|
2024-07-04 14:09:43 +00:00
|
|
|
"recursive,r", "recursively copy the directory (required to remove a directory)");
|
2024-05-27 11:44:45 +00:00
|
|
|
positional_options_description.add("path-from", 1);
|
|
|
|
positional_options_description.add("path-to", 1);
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
auto disk_from = getDiskWithPath(client, options, "disk-from");
|
|
|
|
auto disk_to = getDiskWithPath(client, options, "disk-to");
|
|
|
|
String path_from = disk_from.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-from"));
|
|
|
|
String path_to = disk_to.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-to"));
|
2024-07-01 11:11:16 +00:00
|
|
|
bool recursive = options.count("recursive");
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-07-01 11:11:16 +00:00
|
|
|
if (!disk_from.getDisk()->exists(path_from))
|
|
|
|
{
|
2024-07-01 12:46:17 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"cannot stat '{}' on disk '{}': No such file or directory",
|
|
|
|
path_from,
|
|
|
|
disk_from.getDisk()->getName());
|
2024-07-01 11:11:16 +00:00
|
|
|
}
|
|
|
|
else if (disk_from.getDisk()->isFile(path_from))
|
|
|
|
{
|
|
|
|
auto target_location = getTargetLocation(path_from, disk_to, path_to);
|
|
|
|
if (!disk_to.getDisk()->exists(target_location) || disk_to.getDisk()->isFile(target_location))
|
|
|
|
{
|
|
|
|
disk_from.getDisk()->copyFile(
|
|
|
|
path_from,
|
|
|
|
*disk_to.getDisk(),
|
|
|
|
target_location,
|
|
|
|
/* read_settings= */ {},
|
|
|
|
/* write_settings= */ {},
|
|
|
|
/* cancellation_hook= */ {});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BAD_ARGUMENTS, "cannot overwrite directory {} with non-directory {}", target_location, path_from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (disk_from.getDisk()->isDirectory(path_from))
|
|
|
|
{
|
|
|
|
if (!recursive)
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "--recursive not specified; omitting directory {}", path_from);
|
|
|
|
}
|
|
|
|
auto target_location = getTargetLocation(path_from, disk_to, path_to);
|
|
|
|
|
|
|
|
if (disk_to.getDisk()->isFile(target_location))
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot overwrite non-directory {} with directory {}", path_to, target_location);
|
|
|
|
}
|
|
|
|
else if (!disk_to.getDisk()->exists(target_location))
|
|
|
|
{
|
|
|
|
disk_to.getDisk()->createDirectory(target_location);
|
|
|
|
}
|
|
|
|
disk_from.getDisk()->copyDirectoryContent(
|
|
|
|
path_from,
|
|
|
|
disk_to.getDisk(),
|
|
|
|
target_location,
|
|
|
|
/* read_settings= */ {},
|
|
|
|
/* write_settings= */ {},
|
|
|
|
/* cancellation_hook= */ {});
|
|
|
|
}
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
CommandPtr makeCommandCopy()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-07-01 17:35:07 +00:00
|
|
|
return std::make_shared<DB::CommandCopy>();
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
2024-07-04 13:52:05 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
}
|