2022-07-20 20:30:16 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2024-05-27 11:44:45 +00:00
|
|
|
#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 CommandMove final : public ICommand
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CommandMove()
|
|
|
|
{
|
|
|
|
command_name = "move";
|
2023-08-29 12:25:04 +00:00
|
|
|
description = "Move file or directory from `from_path` to `to_path`";
|
2024-05-27 11:44:45 +00:00
|
|
|
options_description.add_options()("path-from", po::value<String>(), "path from which we copy (mandatory, positional)")(
|
2024-06-14 13:54:12 +00:00
|
|
|
"path-to", po::value<String>(), "path to which we copy (mandatory, positional)");
|
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-06-14 13:54:12 +00:00
|
|
|
auto disk = client.getCurrentDiskWithPath();
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
String path_from = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-from"));
|
|
|
|
String path_to = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-to"));
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
if (disk.getDisk()->isFile(path_from))
|
2024-07-01 11:11:16 +00:00
|
|
|
{
|
2024-05-28 13:17:49 +00:00
|
|
|
disk.getDisk()->moveFile(path_from, path_to);
|
2024-07-01 11:11:16 +00:00
|
|
|
}
|
|
|
|
else if (disk.getDisk()->isDirectory(path_from))
|
|
|
|
{
|
|
|
|
auto target_location = getTargetLocation(path_from, disk, path_to);
|
|
|
|
if (!disk.getDisk()->exists(target_location))
|
|
|
|
{
|
|
|
|
disk.getDisk()->createDirectory(target_location);
|
|
|
|
disk.getDisk()->moveDirectory(path_from, target_location);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (disk.getDisk()->isFile(target_location))
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::BAD_ARGUMENTS, "cannot overwrite non-directory '{}' with directory '{}'", target_location, path_from);
|
|
|
|
}
|
|
|
|
if (!disk.getDisk()->isDirectoryEmpty(target_location))
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot move '{}' to '{}': Directory not empty", path_from, target_location);
|
|
|
|
}
|
2024-09-19 11:51:02 +00:00
|
|
|
|
2024-07-01 11:11:16 +00:00
|
|
|
disk.getDisk()->moveDirectory(path_from, target_location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!disk.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.getDisk()->getName());
|
2024-07-01 11:11:16 +00:00
|
|
|
}
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
CommandPtr makeCommandMove()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-07-01 17:35:07 +00:00
|
|
|
return std::make_shared<DB::CommandMove>();
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
2024-05-27 11:44:45 +00:00
|
|
|
|
|
|
|
}
|