2016-10-25 06:49:24 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
2023-02-06 19:12:24 +00:00
|
|
|
#include <base/defines.h>
|
2022-05-08 17:01:47 +00:00
|
|
|
#include <cerrno>
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event FileOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
extern const int CANNOT_OPEN_FILE;
|
|
|
|
extern const int CANNOT_CLOSE_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReadBufferFromFile::ReadBufferFromFile(
|
|
|
|
const std::string & file_name_,
|
|
|
|
size_t buf_size,
|
|
|
|
int flags,
|
|
|
|
char * existing_memory,
|
2022-01-03 16:05:46 +00:00
|
|
|
size_t alignment,
|
2023-03-30 17:02:28 +00:00
|
|
|
std::optional<size_t> file_size_,
|
|
|
|
ThrottlerPtr throttler_)
|
|
|
|
: ReadBufferFromFileDescriptor(-1, buf_size, existing_memory, alignment, file_size_, throttler_)
|
|
|
|
, file_name(file_name_)
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::FileOpen);
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#ifdef OS_DARWIN
|
2016-10-26 22:27:38 +00:00
|
|
|
bool o_direct = (flags != -1) && (flags & O_DIRECT);
|
2018-01-10 00:04:08 +00:00
|
|
|
if (o_direct)
|
2016-10-26 22:27:38 +00:00
|
|
|
flags = flags & ~O_DIRECT;
|
|
|
|
#endif
|
2019-05-18 15:12:04 +00:00
|
|
|
fd = ::open(file_name.c_str(), flags == -1 ? O_RDONLY | O_CLOEXEC : flags | O_CLOEXEC);
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
if (-1 == fd)
|
2019-08-07 12:52:47 +00:00
|
|
|
throwFromErrnoWithPath("Cannot open file " + file_name, file_name,
|
|
|
|
errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
|
2022-06-10 08:22:31 +00:00
|
|
|
#ifdef OS_DARWIN
|
2016-10-26 22:27:38 +00:00
|
|
|
if (o_direct)
|
2016-11-29 21:56:00 +00:00
|
|
|
{
|
2016-10-26 22:27:38 +00:00
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1)
|
2019-08-21 00:35:41 +00:00
|
|
|
throwFromErrnoWithPath("Cannot set F_NOCACHE on file " + file_name, file_name, ErrorCodes::CANNOT_OPEN_FILE);
|
2016-11-29 21:56:00 +00:00
|
|
|
}
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReadBufferFromFile::ReadBufferFromFile(
|
2020-09-30 00:00:45 +00:00
|
|
|
int & fd_,
|
2017-02-09 10:10:13 +00:00
|
|
|
const std::string & original_file_name,
|
2016-10-25 06:49:24 +00:00
|
|
|
size_t buf_size,
|
|
|
|
char * existing_memory,
|
2022-01-03 16:05:46 +00:00
|
|
|
size_t alignment,
|
2023-03-30 17:02:28 +00:00
|
|
|
std::optional<size_t> file_size_,
|
|
|
|
ThrottlerPtr throttler_)
|
|
|
|
: ReadBufferFromFileDescriptor(fd_, buf_size, existing_memory, alignment, file_size_, throttler_)
|
2022-01-03 16:05:46 +00:00
|
|
|
, file_name(original_file_name.empty() ? "(fd = " + toString(fd_) + ")" : original_file_name)
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
2020-09-30 00:00:45 +00:00
|
|
|
fd_ = -1;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReadBufferFromFile::~ReadBufferFromFile()
|
|
|
|
{
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2023-02-06 19:12:24 +00:00
|
|
|
int err = ::close(fd);
|
|
|
|
chassert(!err || errno == EINTR);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ReadBufferFromFile::close()
|
|
|
|
{
|
2020-09-04 05:46:58 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
if (0 != ::close(fd))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_CLOSE_FILE, "Cannot close file");
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
fd = -1;
|
|
|
|
metric_increment.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|