ClickHouse/src/Common/filesystemHelpers.h

48 lines
1.1 KiB
C++
Raw Normal View History

2019-10-01 18:51:33 +00:00
#pragma once
2020-09-15 09:55:57 +00:00
#include <common/types.h>
2019-11-27 09:39:44 +00:00
#include <Common/Exception.h>
2019-10-01 18:51:33 +00:00
2019-11-27 09:39:44 +00:00
#include <filesystem>
#include <memory>
#include <string>
2020-10-03 00:01:32 +00:00
#include <cerrno>
2019-11-27 09:39:44 +00:00
#include <sys/statvfs.h>
2019-10-01 18:51:33 +00:00
#include <Poco/TemporaryFile.h>
2019-11-27 09:39:44 +00:00
2019-10-01 18:51:33 +00:00
namespace DB
{
2019-11-27 09:39:44 +00:00
namespace ErrorCodes
{
extern const int CANNOT_STATVFS;
}
2019-10-01 18:51:33 +00:00
using TemporaryFile = Poco::TemporaryFile;
2019-10-02 12:06:34 +00:00
bool enoughSpaceInDirectory(const std::string & path, size_t data_size);
2019-10-01 18:51:33 +00:00
std::unique_ptr<TemporaryFile> createTemporaryFile(const std::string & path);
2020-06-27 19:05:00 +00:00
/// Returns mount point of filesystem where absolute_path (must exist) is located
2019-11-27 09:39:44 +00:00
std::filesystem::path getMountPoint(std::filesystem::path absolute_path);
/// Returns name of filesystem mounted to mount_point
#if !defined(__linux__)
[[noreturn]]
#endif
2019-12-03 13:37:40 +00:00
String getFilesystemName([[maybe_unused]] const String & mount_point);
2019-11-27 09:39:44 +00:00
inline struct statvfs getStatVFS(const String & path)
{
struct statvfs fs;
2020-10-03 00:01:32 +00:00
while (statvfs(path.c_str(), &fs) != 0)
{
if (errno == EINTR)
continue;
2019-11-27 09:39:44 +00:00
throwFromErrnoWithPath("Could not calculate available disk space (statvfs)", path, ErrorCodes::CANNOT_STATVFS);
2020-10-03 00:01:32 +00:00
}
2019-11-27 09:39:44 +00:00
return fs;
}
2019-10-01 18:51:33 +00:00
}