ClickHouse/programs/disks/CommandWrite.cpp

59 lines
1.9 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"
2024-09-28 08:08:14 +00:00
#include <IO/ReadBufferFromEmptyFile.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/copyData.h>
2024-05-27 11:44:45 +00:00
#include <Common/TerminalSize.h>
namespace DB
{
class CommandWrite final : public ICommand
{
public:
CommandWrite()
{
command_name = "write";
2024-07-04 14:09:43 +00:00
description = "Write a file from `path-from` to `path-to`";
2024-06-26 15:59:15 +00:00
options_description.add_options()("path-from", po::value<String>(), "file from which we are reading, defaults to `stdin` (input from `stdin` is finished by Ctrl+D)")(
2024-05-27 11:44:45 +00:00
"path-to", po::value<String>(), "file to which we are writing (mandatory, positional)");
positional_options_description.add("path-to", 1);
}
2024-05-28 13:17:49 +00:00
2024-05-27 11:44:45 +00:00
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
2024-05-27 11:44:45 +00:00
auto disk = client.getCurrentDiskWithPath();
2024-05-27 11:44:45 +00:00
std::optional<String> path_from = getValueFromCommandLineOptionsWithOptional<String>(options, "path-from");
2024-05-27 11:44:45 +00:00
String path_to = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-to"));
2024-05-27 11:44:45 +00:00
auto in = [&]() -> std::unique_ptr<ReadBufferFromFileBase>
{
2024-05-27 11:44:45 +00:00
if (!path_from.has_value())
return std::make_unique<ReadBufferFromFileDescriptor>(STDIN_FILENO);
2024-10-16 02:18:59 +00:00
String relative_path_from = disk.getRelativeFromRoot(path_from.value());
auto res = disk.getDisk()->readFileIfExists(relative_path_from, getReadSettings());
if (res)
return res;
/// For backward compatibility.
return std::make_unique<ReadBufferFromEmptyFile>();
2024-05-27 11:44:45 +00:00
}();
2024-05-27 11:44:45 +00:00
auto out = disk.getDisk()->writeFile(path_to);
copyData(*in, *out);
out->finalize();
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandWrite()
{
2024-05-27 11:44:45 +00:00
return std::make_shared<DB::CommandWrite>();
}
}