ClickHouse/dbms/include/DB/IO/ReadBufferFromFile.h

43 lines
995 B
C
Raw Normal View History

2011-10-24 12:10:59 +00:00
#pragma once
#include <fcntl.h>
#include <DB/IO/ReadBufferFromFileDescriptor.h>
namespace DB
{
/** Принимает имя файла. Самостоятельно открывает и закрывает файл.
*/
class ReadBufferFromFile : public ReadBufferFromFileDescriptor
{
private:
std::string file_name;
public:
2014-01-04 04:52:22 +00:00
ReadBufferFromFile(const std::string & file_name_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, int flags = -1,
2014-04-08 07:31:51 +00:00
char * existing_memory = nullptr, size_t alignment = 0)
: ReadBufferFromFileDescriptor(-1, buf_size, existing_memory, alignment), file_name(file_name_)
2011-10-24 12:10:59 +00:00
{
ProfileEvents::increment(ProfileEvents::FileOpen);
2014-01-04 04:52:22 +00:00
fd = open(file_name.c_str(), flags == -1 ? O_RDONLY : flags);
2011-10-24 12:10:59 +00:00
if (-1 == fd)
throwFromErrno("Cannot open file " + file_name, errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
2011-10-24 12:10:59 +00:00
}
virtual ~ReadBufferFromFile()
2011-10-24 12:10:59 +00:00
{
close(fd);
2011-10-24 12:10:59 +00:00
}
virtual std::string getFileName()
2011-10-24 12:10:59 +00:00
{
return file_name;
}
};
}