ClickHouse/programs/disks/CommandRemove.cpp

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

59 lines
1.6 KiB
C++
Raw Normal View History

2022-07-20 20:30:16 +00:00
#include <Interpreters/Context.h>
#include "Common/Exception.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 CommandRemove final : public ICommand
{
public:
CommandRemove()
{
command_name = "remove";
description = "Remove file or directory. Throws exception if file doesn't exists";
2024-07-04 14:09:43 +00:00
options_description.add_options()("path", po::value<String>(), "path that is going to be deleted (mandatory, positional)")(
2024-07-01 12:46:17 +00:00
"recursive,r", "recursively removes the directory (required to remove a directory)");
2024-05-27 11:44:45 +00:00
positional_options_description.add("path", 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
const String & path = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path"));
bool recursive = options.count("recursive");
if (!disk.getDisk()->exists(path))
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path {} on disk {} doesn't exist", path, disk.getDisk()->getName());
}
else if (disk.getDisk()->isDirectory(path))
{
if (!recursive)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot remove '{}': Is a directory", path);
}
else
{
disk.getDisk()->removeRecursive(path);
}
}
else
{
disk.getDisk()->removeFileIfExists(path);
}
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandRemove()
{
return std::make_shared<DB::CommandRemove>();
}
2024-05-27 11:44:45 +00:00
}