Update Disks/*

This commit is contained in:
kssenii 2021-04-30 18:13:19 +03:00
parent 7f2f138d56
commit c609119daf
5 changed files with 31 additions and 25 deletions

View File

@ -8,10 +8,9 @@
#include <Common/quoteString.h>
#include <IO/createReadBufferFromFileBase.h>
#include <filesystem>
#include <fstream>
#include <unistd.h>
namespace fs = std::filesystem;
namespace DB
{
@ -25,6 +24,7 @@ namespace ErrorCodes
extern const int CANNOT_TRUNCATE_FILE;
extern const int CANNOT_UNLINK;
extern const int CANNOT_RMDIR;
extern const int CANNOT_OPEN_FILE;
}
std::mutex DiskLocal::reservation_mutex;
@ -268,12 +268,14 @@ void DiskLocal::listFiles(const String & path, std::vector<String> & file_names)
void DiskLocal::setLastModified(const String & path, const Poco::Timestamp & timestamp)
{
Poco::File(disk_path / path).setLastModified(timestamp);
fs::last_write_time(disk_path / path, static_cast<fs::file_time_type>(std::chrono::microseconds(timestamp.epochMicroseconds())));
}
Poco::Timestamp DiskLocal::getLastModified(const String & path)
{
return Poco::File(disk_path / path).getLastModified();
fs::file_time_type fs_time = fs::last_write_time(disk_path / path);
auto micro_sec = std::chrono::duration_cast<std::chrono::microseconds>(fs_time.time_since_epoch());
return Poco::Timestamp(micro_sec.count());
}
void DiskLocal::createHardLink(const String & src_path, const String & dst_path)
@ -290,12 +292,16 @@ void DiskLocal::truncateFile(const String & path, size_t size)
void DiskLocal::createFile(const String & path)
{
Poco::File(disk_path / path).createFile();
FILE * file = fopen((disk_path / path).string().data(), "a+");
if (file == nullptr)
throw Exception(ErrorCodes::CANNOT_OPEN_FILE, "Cannot create file {}", path);
}
void DiskLocal::setReadOnly(const String & path)
{
Poco::File(disk_path / path).setReadOnly(true);
fs::permissions(disk_path / path,
fs::perms::owner_read | fs::perms::group_read | fs::perms::others_read,
fs::perm_options::remove); /// bitwise AND
}
bool inline isSameDiskType(const IDisk & one, const IDisk & another)
@ -306,7 +312,7 @@ bool inline isSameDiskType(const IDisk & one, const IDisk & another)
void DiskLocal::copy(const String & from_path, const std::shared_ptr<IDisk> & to_disk, const String & to_path)
{
if (isSameDiskType(*this, *to_disk))
Poco::File(disk_path / from_path).copyTo(to_disk->getPath() + to_path); /// Use more optimal way.
fs::copy_file(disk_path / from_path, to_disk->getPath() + to_path);
else
IDisk::copy(from_path, to_disk, to_path); /// Copy files through buffers.
}
@ -384,10 +390,16 @@ void registerDiskLocal(DiskFactory & factory)
throw Exception("Disk path must end with /. Disk " + name, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
}
if (Poco::File disk{path}; !disk.canRead() || !disk.canWrite())
{
fs::path disk(path);
fs::perms p = fs::status(disk).permissions();
bool is_readable = (p & fs::perms::owner_read) != fs::perms::none
| (p & fs::perms::group_read) != fs::perms::none
| (p & fs::perms::others_read) != fs::perms::none;
bool is_writable = (p & fs::perms::owner_write) != fs::perms::none
| (p & fs::perms::group_write) != fs::perms::none
| (p & fs::perms::others_write) != fs::perms::none;
if (!is_readable || !is_writable)
throw Exception("There is no RW access to the disk " + name + " (" + path + ")", ErrorCodes::PATH_ACCESS_DENIED);
}
bool has_space_ratio = config.has(config_prefix + ".keep_free_space_ratio");

View File

@ -6,11 +6,6 @@
#include <IO/ReadBufferFromFileBase.h>
#include <IO/WriteBufferFromFile.h>
#include <Poco/DirectoryIterator.h>
#include <Poco/File.h>
#include <filesystem>
namespace fs = std::filesystem;
namespace DB
{

View File

@ -6,9 +6,7 @@
#include <IO/WriteBufferFromFileBase.h>
#include <IO/WriteBufferFromString.h>
#include <Interpreters/Context.h>
#include <filesystem>
namespace fs = std::filesystem;
namespace DB
{

View File

@ -48,10 +48,9 @@ void asyncCopy(IDisk & from_disk, String from_path, IDisk & to_disk, String to_p
}
else
{
Poco::Path path(from_path);
const String & dir_name = path.directory(path.depth() - 1);
const String dest = to_path + dir_name + "/";
to_disk.createDirectories(dest);
const String & dir_name = directoryPath(from_path);
fs::path dest(fs::path(to_path) / dir_name);
fs::create_directories(dest);
for (auto it = from_disk.iterateDirectory(from_path); it->isValid(); it->next())
asyncCopy(from_disk, it->path(), to_disk, dest, exec, results);

View File

@ -11,9 +11,10 @@
#include <mutex>
#include <utility>
#include <boost/noncopyable.hpp>
#include <Poco/Path.h>
#include <Poco/Timestamp.h>
#include <filesystem>
namespace fs = std::filesystem;
namespace CurrentMetrics
{
@ -288,19 +289,20 @@ inline String fullPath(const DiskPtr & disk, const String & path)
/// Return parent path for the specified path.
inline String parentPath(const String & path)
{
return Poco::Path(path).parent().toString();
auto fs_path = fs::path(path).parent_path() / "";
return fs_path.string();
}
/// Return file name for the specified path.
inline String fileName(const String & path)
{
return Poco::Path(path).getFileName();
return fs::path(path).filename();
}
/// Return directory path for the specified path.
inline String directoryPath(const String & path)
{
return Poco::Path(path).setFileName("").toString();
return fs::is_directory(path) ? path : fs::path(path).parent_path().string();
}
}