2011-10-24 12:10:59 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
2013-06-21 20:34:19 +00:00
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Работает с готовым файловым дескриптором. Не открывает и не закрывает файл.
|
|
|
|
|
*/
|
|
|
|
|
class WriteBufferFromFileDescriptor : public BufferWithOwnMemory<WriteBuffer>
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
void nextImpl()
|
|
|
|
|
{
|
|
|
|
|
if (!offset())
|
|
|
|
|
return;
|
2011-12-26 07:07:30 +00:00
|
|
|
|
|
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
|
while (bytes_written != offset())
|
|
|
|
|
{
|
|
|
|
|
ssize_t res = ::write(fd, working_buffer.begin() + bytes_written, offset() - bytes_written);
|
|
|
|
|
|
|
|
|
|
if ((-1 == res || 0 == res) && errno != EINTR)
|
|
|
|
|
throwFromErrno("Cannot write to file " + getFileName(), ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
|
|
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
|
bytes_written += res;
|
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Имя или описание файла
|
|
|
|
|
virtual std::string getFileName()
|
|
|
|
|
{
|
2013-06-21 20:34:19 +00:00
|
|
|
|
return "(fd = " + toString(fd) + ")";
|
2011-10-24 12:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2014-01-03 06:31:32 +00:00
|
|
|
|
WriteBufferFromFileDescriptor(int fd_ = -1, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = NULL, size_t alignment = 0)
|
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment), fd(fd_) {}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
2012-02-24 19:21:38 +00:00
|
|
|
|
/** Можно вызывать для инициализации, если нужный fd не был передан в конструктор.
|
|
|
|
|
* Менять fd во время работы нельзя.
|
|
|
|
|
*/
|
|
|
|
|
void setFD(int fd_)
|
|
|
|
|
{
|
|
|
|
|
fd = fd_;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 23:14:08 +00:00
|
|
|
|
~WriteBufferFromFileDescriptor()
|
2011-10-24 12:10:59 +00:00
|
|
|
|
{
|
2013-11-18 17:17:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2011-12-12 01:06:13 +00:00
|
|
|
|
next();
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2013-11-18 19:18:03 +00:00
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
}
|
2011-12-28 20:01:41 +00:00
|
|
|
|
|
|
|
|
|
int getFD()
|
|
|
|
|
{
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
off_t seek(off_t offset, int whence = SEEK_SET)
|
|
|
|
|
{
|
|
|
|
|
off_t res = lseek(fd, offset, whence);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2011-12-30 17:57:02 +00:00
|
|
|
|
|
|
|
|
|
void truncate(off_t length = 0)
|
|
|
|
|
{
|
|
|
|
|
int res = ftruncate(fd, length);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot truncate file " + getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
|
|
|
|
|
}
|
2013-09-15 01:10:16 +00:00
|
|
|
|
|
|
|
|
|
void sync()
|
|
|
|
|
{
|
|
|
|
|
/// Если в буфере ещё остались данные - запишем их.
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
|
/// Попросим ОС сбросить данные на диск.
|
|
|
|
|
int res = fsync(fd);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
|
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|