ClickHouse/src/Common/filesystemHelpers.h

101 lines
2.9 KiB
C++
Raw Normal View History

2019-10-01 18:51:33 +00:00
#pragma once
2021-10-02 07:13:14 +00:00
#include <base/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>
#include <sys/statvfs.h>
#include <Poco/TemporaryFile.h>
2019-10-01 18:51:33 +00:00
2022-08-15 10:30:47 +00:00
namespace fs = std::filesystem;
2019-11-27 09:39:44 +00:00
2019-10-01 18:51:33 +00:00
namespace DB
{
2022-12-06 10:04:15 +00:00
using PocoTemporaryFile = Poco::TemporaryFile;
2019-10-01 18:51:33 +00:00
2019-10-02 12:06:34 +00:00
bool enoughSpaceInDirectory(const std::string & path, size_t data_size);
2022-12-06 10:04:15 +00:00
std::unique_ptr<PocoTemporaryFile> createTemporaryFile(const std::string & folder_path);
2022-08-15 18:04:25 +00:00
2019-10-01 18:51:33 +00:00
// Determine what block device is responsible for specified path
#if !defined(OS_LINUX)
[[noreturn]]
#endif
2022-02-15 10:39:45 +00:00
String getBlockDeviceId([[maybe_unused]] const String & path);
2022-08-19 14:58:30 +00:00
std::optional<String> tryGetBlockDeviceId([[maybe_unused]] const String & path);
2022-03-23 18:15:01 +00:00
enum class BlockDeviceType
{
UNKNOWN = 0, // we were unable to determine device type
NONROT = 1, // not a rotational device (SSD, NVME, etc)
ROT = 2 // rotational device (HDD)
};
// Try to determine block device type
#if !defined(OS_LINUX)
[[noreturn]]
#endif
2022-03-24 14:30:10 +00:00
BlockDeviceType getBlockDeviceType([[maybe_unused]] const String & device_id);
// Get size of read-ahead in bytes for specified block device
#if !defined(OS_LINUX)
[[noreturn]]
#endif
2022-03-24 14:30:10 +00:00
UInt64 getBlockDeviceReadAheadBytes([[maybe_unused]] const String & device_id);
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(OS_LINUX)
2019-11-27 09:39:44 +00:00
[[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
2020-10-03 00:02:47 +00:00
struct statvfs getStatVFS(const String & path);
2019-11-27 09:39:44 +00:00
/// Returns true if path starts with prefix path
bool pathStartsWith(const std::filesystem::path & path, const std::filesystem::path & prefix_path);
/// Returns true if path starts with prefix path
bool pathStartsWith(const String & path, const String & prefix_path);
2021-10-17 10:13:47 +00:00
/// Same as pathStartsWith, but without canonization, i.e. allowed to check symlinks.
2021-10-17 10:45:32 +00:00
/// (Path is made absolute and normalized.)
2021-10-17 08:42:36 +00:00
bool fileOrSymlinkPathStartsWith(const String & path, const String & prefix_path);
2021-08-18 11:28:22 +00:00
2022-05-25 14:49:40 +00:00
size_t getSizeFromFileDescriptor(int fd, const String & file_name = "");
2022-07-09 17:35:07 +00:00
std::optional<size_t> tryGetSizeFromFilePath(const String & path);
/// Get inode number for a file path.
/// Will not work correctly if filesystem does not support inodes.
Int64 getINodeNumberFromPath(const String & path);
2019-10-01 18:51:33 +00:00
}
2021-05-28 21:57:53 +00:00
namespace FS
{
bool createFile(const std::string & path);
bool exists(const std::string & path);
2021-05-28 21:57:53 +00:00
bool canRead(const std::string & path);
bool canWrite(const std::string & path);
bool canExecute(const std::string & path);
2021-05-28 21:57:53 +00:00
/// st_mtime
2021-05-28 21:57:53 +00:00
time_t getModificationTime(const std::string & path);
Poco::Timestamp getModificationTimestamp(const std::string & path);
void setModificationTime(const std::string & path, time_t time);
/// st_ctime
time_t getChangeTime(const std::string & path);
2022-08-15 10:30:47 +00:00
bool isSymlink(const fs::path & path);
fs::path readSymlink(const fs::path & path);
2021-05-28 21:57:53 +00:00
}