This commit is contained in:
alesapin 2022-12-08 18:43:54 +01:00
parent 7a223f4956
commit 350bacec60
12 changed files with 44 additions and 60 deletions

View File

@ -55,10 +55,10 @@ public:
DiskPtr disk_from = global_context->getDisk(disk_name_from);
DiskPtr disk_to = global_context->getDisk(disk_name_to);
String full_path_from = validatePathAndGetAsRelative(path_from);
String full_path_to = validatePathAndGetAsRelative(path_to);
String relative_path_from = validatePathAndGetAsRelative(path_from);
String relative_path_to = validatePathAndGetAsRelative(path_to);
disk_from->copy(full_path_from, disk_to, full_path_to);
disk_from->copy(relative_path_from, disk_to, relative_path_to);
}
};
}

View File

@ -43,10 +43,10 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path_from = validatePathAndGetAsRelative(path_from);
String full_path_to = validatePathAndGetAsRelative(path_to);
String relative_path_from = validatePathAndGetAsRelative(path_from);
String relative_path_to = validatePathAndGetAsRelative(path_to);
disk->createHardLink(full_path_from, full_path_to);
disk->createHardLink(relative_path_from, relative_path_to);
}
};
}

View File

@ -48,32 +48,32 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path = validatePathAndGetAsRelative(path);
String relative_path = validatePathAndGetAsRelative(path);
bool recursive = config.getBool("recursive", false);
if (recursive)
listRecursive(disk, full_path);
listRecursive(disk, relative_path);
else
list(disk, full_path);
list(disk, relative_path);
}
private:
static void list(const DiskPtr & disk, const std::string & full_path)
static void list(const DiskPtr & disk, const std::string & relative_path)
{
std::vector<String> file_names;
disk->listFiles(full_path, file_names);
disk->listFiles(relative_path, file_names);
for (const auto & file_name : file_names)
std::cout << file_name << '\n';
}
static void listRecursive(const DiskPtr & disk, const std::string & full_path)
static void listRecursive(const DiskPtr & disk, const std::string & relative_path)
{
std::vector<String> file_names;
disk->listFiles(full_path, file_names);
disk->listFiles(relative_path, file_names);
std::cout << full_path << ":\n";
std::cout << relative_path << ":\n";
if (!file_names.empty())
{
@ -84,7 +84,7 @@ private:
for (const auto & file_name : file_names)
{
auto path = full_path + "/" + file_name;
auto path = relative_path + "/" + file_name;
if (disk->isDirectory(path))
listRecursive(disk, path);
}

View File

@ -48,13 +48,13 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path = validatePathAndGetAsRelative(path);
String relative_path = validatePathAndGetAsRelative(path);
bool recursive = config.getBool("recursive", false);
if (recursive)
disk->createDirectories(full_path);
disk->createDirectories(relative_path);
else
disk->createDirectory(full_path);
disk->createDirectory(relative_path);
}
};
}

View File

@ -42,13 +42,13 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path_from = validatePathAndGetAsRelative(path_from);
String full_path_to = validatePathAndGetAsRelative(path_to);
String relative_path_from = validatePathAndGetAsRelative(path_from);
String relative_path_to = validatePathAndGetAsRelative(path_to);
if (disk->isFile(full_path_from))
disk->moveFile(full_path_from, full_path_to);
if (disk->isFile(relative_path_from))
disk->moveFile(relative_path_from, relative_path_to);
else
disk->moveDirectory(full_path_from, full_path_to);
disk->moveDirectory(relative_path_from, relative_path_to);
}
};
}

View File

@ -49,23 +49,23 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path = validatePathAndGetAsRelative(command_arguments[0]);
String relative_path = validatePathAndGetAsRelative(command_arguments[0]);
String path_output = config.getString("output", "");
if (!path_output.empty())
{
String full_path_output = validatePathAndGetAsRelative(path_output);
String relative_path_output = validatePathAndGetAsRelative(path_output);
auto in = disk->readFile(full_path);
auto out = disk->writeFile(full_path_output);
auto in = disk->readFile(relative_path);
auto out = disk->writeFile(relative_path_output);
copyData(*in, *out);
out->finalize();
return;
}
else
{
auto in = disk->readFile(full_path);
auto in = disk->readFile(relative_path);
std::unique_ptr<WriteBufferFromFileBase> out = std::make_unique<WriteBufferFromFileDescriptor>(STDOUT_FILENO);
copyData(*in, *out);
}

View File

@ -41,9 +41,9 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path = validatePathAndGetAsRelative(path);
String relative_path = validatePathAndGetAsRelative(path);
disk->removeRecursive(full_path);
disk->removeRecursive(relative_path);
}
};
}

View File

@ -52,7 +52,7 @@ public:
DiskPtr disk = global_context->getDisk(disk_name);
String full_path = validatePathAndGetAsRelative(path);
String relative_path = validatePathAndGetAsRelative(path);
String path_input = config.getString("input", "");
std::unique_ptr<ReadBufferFromFileBase> in;
@ -62,11 +62,11 @@ public:
}
else
{
String full_path_input = validatePathAndGetAsRelative(path_input);
in = disk->readFile(full_path_input);
String relative_path_input = validatePathAndGetAsRelative(path_input);
in = disk->readFile(relative_path_input);
}
auto out = disk->writeFile(full_path);
auto out = disk->writeFile(relative_path);
copyData(*in, *out);
out->finalize();
}

View File

@ -32,18 +32,19 @@ void ICommand::addOptions(ProgramOptionsDescription & options_description)
String ICommand::validatePathAndGetAsRelative(const String & path)
{
/// If path contain non-normalized symbols like ../ or something like
/// this 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 them.
if (path.find("..") != std::string::npos || fs::path(path).lexically_normal().string() != path)
/// 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)
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.
if (fs::path(path).is_absolute())
return path.substr(1);
if (fs::path(lexically_normal_path).is_absolute())
return lexically_normal_path.substr(1);
return path;
return lexically_normal_path;
}
}

View File

@ -10,11 +10,6 @@ namespace fs = std::filesystem;
template <typename T>
DB::DiskPtr createDisk();
template <>
DB::DiskPtr createDisk<DB::DiskMemory>()
{
return std::make_shared<DB::DiskMemory>("memory_disk");
}
template <>
DB::DiskPtr createDisk<DB::DiskLocal>()
@ -30,11 +25,6 @@ void destroyDisk(DB::DiskPtr & disk)
disk.reset();
}
template <>
void destroyDisk<DB::DiskMemory>(DB::DiskPtr & disk)
{
disk.reset();
}
template <>
void destroyDisk<DB::DiskLocal>(DB::DiskPtr & disk)
@ -55,7 +45,7 @@ public:
};
using DiskImplementations = testing::Types<DB::DiskMemory, DB::DiskLocal>;
using DiskImplementations = testing::Types<DB::DiskLocal>;
TYPED_TEST_SUITE(DiskTest, DiskImplementations);

View File

@ -1,15 +1,11 @@
#pragma once
#include <Disks/DiskLocal.h>
#include <Disks/DiskMemory.h>
#include <Disks/IDisk.h>
template <typename T>
DB::DiskPtr createDisk();
template <>
DB::DiskPtr createDisk<DB::DiskMemory>();
template <>
DB::DiskPtr createDisk<DB::DiskLocal>();
@ -18,6 +14,3 @@ void destroyDisk(DB::DiskPtr & disk);
template <>
void destroyDisk<DB::DiskLocal>(DB::DiskPtr & disk);
template <>
void destroyDisk<DB::DiskMemory>(DB::DiskPtr & disk);

View File

@ -65,7 +65,7 @@ private:
};
using DiskImplementations = testing::Types<DB::DiskMemory, DB::DiskLocal>;
using DiskImplementations = testing::Types<DB::DiskLocal>;
TYPED_TEST_SUITE(StorageLogTest, DiskImplementations);
// Returns data written to table in Values format.