ClickHouse/programs/disks/CommandRemove.cpp

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

55 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()->existsDirectory(path))
{
if (!recursive)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot remove '{}': Is a directory", path);
}
2024-09-19 11:51:02 +00:00
disk.getDisk()->removeRecursive(path);
}
else if (disk.getDisk()->existsFile(path))
{
disk.getDisk()->removeFileIfExists(path);
}
else
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path {} on disk {} doesn't exist", path, disk.getDisk()->getName());
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandRemove()
{
return std::make_shared<DB::CommandRemove>();
}
2024-05-27 11:44:45 +00:00
}