ClickHouse/programs/disks/CommandMove.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.5 KiB
C++
Raw Normal View History

2022-07-20 20:30:16 +00:00
#include <Interpreters/Context.h>
2024-05-27 11:44:45 +00:00
#include "ICommand.h"
namespace DB
{
2024-07-01 13:24:13 +00:00
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
class CommandMove final : public ICommand
{
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);
}
2024-05-27 11:44:45 +00:00
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
2024-06-14 13:54:12 +00:00
auto disk = client.getCurrentDiskWithPath();
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"));
2024-05-27 11:44:45 +00:00
if (disk.getDisk()->isFile(path_from))
{
2024-05-28 13:17:49 +00:00
disk.getDisk()->moveFile(path_from, path_to);
}
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
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-05-27 11:44:45 +00:00
CommandPtr makeCommandMove()
{
return std::make_shared<DB::CommandMove>();
}
2024-05-27 11:44:45 +00:00
}