2022-07-20 20:30:16 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2024-07-01 11:11:16 +00:00
|
|
|
#include "Common/Exception.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 CommandRemove final : public ICommand
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CommandRemove()
|
|
|
|
{
|
|
|
|
command_name = "remove";
|
2024-07-01 11:11:16 +00:00
|
|
|
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);
|
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-10-15 16:24:06 +00:00
|
|
|
const auto & disk = client.getCurrentDiskWithPath();
|
2024-05-27 11:44:45 +00:00
|
|
|
const String & path = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path"));
|
2024-07-01 11:11:16 +00:00
|
|
|
bool recursive = options.count("recursive");
|
2024-08-10 22:13:40 +00:00
|
|
|
if (disk.getDisk()->existsDirectory(path))
|
2024-07-01 11:11:16 +00:00
|
|
|
{
|
|
|
|
if (!recursive)
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "cannot remove '{}': Is a directory", path);
|
|
|
|
}
|
2024-09-19 11:51:02 +00:00
|
|
|
|
2024-07-01 11:11:16 +00:00
|
|
|
disk.getDisk()->removeRecursive(path);
|
|
|
|
}
|
2024-08-10 22:13:40 +00:00
|
|
|
else if (disk.getDisk()->existsFile(path))
|
2024-07-01 11:11:16 +00:00
|
|
|
{
|
|
|
|
disk.getDisk()->removeFileIfExists(path);
|
|
|
|
}
|
2024-08-10 22:13:40 +00:00
|
|
|
else
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path {} on disk {} doesn't exist", path, disk.getDisk()->getName());
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
CommandPtr makeCommandRemove()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-07-01 17:35:07 +00:00
|
|
|
return std::make_shared<DB::CommandRemove>();
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
2024-05-27 11:44:45 +00:00
|
|
|
|
|
|
|
}
|