ClickHouse/src/Functions/FunctionFile.cpp

103 lines
3.6 KiB
C++
Raw Normal View History

#include <Columns/ColumnString.h>
#include <Columns/IColumn.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeString.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/WriteBufferFromVector.h>
#include <IO/copyData.h>
2021-01-16 03:27:31 +00:00
#include <Interpreters/Context.h>
#include <unistd.h>
2021-04-28 20:48:34 +00:00
#include <filesystem>
2021-04-28 20:48:34 +00:00
namespace fs = std::filesystem;
namespace DB
{
2021-02-16 12:37:49 +00:00
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int NOT_IMPLEMENTED;
extern const int DATABASE_ACCESS_DENIED;
}
/// A function to read file as a string.
2021-06-01 12:20:52 +00:00
class FunctionFile : public IFunction, WithContext
2021-02-16 12:37:49 +00:00
{
public:
static constexpr auto name = "file";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionFile>(context_); }
explicit FunctionFile(ContextPtr context_) : WithContext(context_) {}
2021-02-16 12:37:49 +00:00
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
2021-02-16 12:37:49 +00:00
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
2021-02-16 12:37:49 +00:00
if (!isString(arguments[0].type))
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "{} is only implemented for type String", getName());
2021-02-16 12:37:49 +00:00
return std::make_shared<DataTypeString>();
}
2021-02-16 12:37:49 +00:00
bool useDefaultImplementationForConstants() const override { return true; }
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
2021-02-16 12:37:49 +00:00
const ColumnPtr column = arguments[0].column;
const ColumnString * column_src = checkAndGetColumn<ColumnString>(column.get());
if (!column_src)
2021-02-16 12:37:49 +00:00
throw Exception(
fmt::format("Illegal column {} of argument of function {}", arguments[0].column->getName(), getName()),
ErrorCodes::ILLEGAL_COLUMN);
2021-02-16 12:37:49 +00:00
auto result = ColumnString::create();
auto & res_chars = result->getChars();
auto & res_offsets = result->getOffsets();
2021-02-16 12:37:49 +00:00
res_offsets.resize(input_rows_count);
2021-01-14 12:09:13 +00:00
fs::path user_files_absolute_path = fs::canonical(fs::path(getContext()->getUserFilesPath()));
std::string user_files_absolute_path_string = user_files_absolute_path.string();
// If run in Local mode, no need for path checking.
bool need_check = getContext()->getApplicationType() != Context::ApplicationType::LOCAL;
2021-02-16 12:37:49 +00:00
for (size_t row = 0; row < input_rows_count; ++row)
{
StringRef filename = column_src->getDataAt(row);
fs::path file_path(filename.data, filename.data + filename.size);
2021-02-16 12:37:49 +00:00
2021-04-28 20:48:34 +00:00
if (file_path.is_relative())
file_path = user_files_absolute_path / file_path;
2021-02-16 12:37:49 +00:00
/// Do not use fs::canonical or fs::weakly_canonical.
/// Otherwise it will not allow to work with symlinks in `user_files_path` directory.
file_path = fs::absolute(file_path).lexically_normal();
2021-02-16 12:37:49 +00:00
if (need_check && file_path.string().find(user_files_absolute_path_string) != 0)
throw Exception(ErrorCodes::DATABASE_ACCESS_DENIED, "File is not inside {}", user_files_absolute_path.string());
2021-02-16 12:37:49 +00:00
ReadBufferFromFile in(file_path);
WriteBufferFromVector out(res_chars, AppendModeTag{});
copyData(in, out);
out.finalize();
2021-02-16 12:37:49 +00:00
res_chars.push_back(0);
res_offsets[row] = res_chars.size();
}
2021-02-16 12:37:49 +00:00
return result;
}
};
void registerFunctionFile(FunctionFactory & factory)
{
factory.registerFunction<FunctionFile>();
}
}