ClickHouse/programs/disks/ICommand.cpp

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

51 lines
1.6 KiB
C++
Raw Normal View History

#include "ICommand.h"
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
void ICommand::printHelpMessage() const
{
2022-06-16 17:38:33 +00:00
std::cout << "Command: " << command_name << '\n';
std::cout << "Description: " << description << '\n';
std::cout << "Usage: " << usage << '\n';
if (command_option_description)
{
auto options = *command_option_description;
if (!options.options().empty())
std::cout << options << '\n';
}
}
2022-06-21 14:40:22 +00:00
void ICommand::addOptions(ProgramOptionsDescription & options_description)
{
if (!command_option_description || command_option_description->options().empty())
return;
options_description.add(*command_option_description);
}
String ICommand::validatePathAndGetAsRelative(const String & path)
{
2022-12-08 17:43:54 +00:00
/// If path contain non-normalized symbols like . we will normalized them. If the resulting normalized path
/// still contain '..' it can be dangerous, disallow such paths. Also since clickhouse-disks
/// is not an interactive program (don't track you current path) it's OK to disallow .. paths.
String lexically_normal_path = fs::path(path).lexically_normal();
if (lexically_normal_path.find("..") != std::string::npos)
2022-06-21 14:40:22 +00:00
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Path {} is not normalized", path);
/// If path is absolute we should keep it as relative inside disk, so disk will look like
/// an ordinary filesystem with root.
2022-12-08 17:43:54 +00:00
if (fs::path(lexically_normal_path).is_absolute())
return lexically_normal_path.substr(1);
2022-12-08 17:43:54 +00:00
return lexically_normal_path;
}
}