Fix fsync_part_directory (missing O_DIRECTORY)

On linux you should pass O_DIRECTORY, otherwise open(2) will fail
This commit is contained in:
Azat Khuzhin 2021-01-07 18:49:14 +03:00
parent 80d88a7b17
commit 8d3ec1cce9
2 changed files with 39 additions and 17 deletions

View File

@ -0,0 +1,32 @@
#include <Common/FileSyncGuard.h>
#include <Common/Exception.h>
#include <Disks/IDisk.h>
#include <fcntl.h> // O_RDWR
/// OSX does not have O_DIRECTORY
#ifndef O_DIRECTORY
#define O_DIRECTORY O_RDWR
#endif
namespace DB
{
FileSyncGuard::FileSyncGuard(const DiskPtr & disk_, const String & path)
: disk(disk_)
, fd(disk_->open(path, O_DIRECTORY))
{}
FileSyncGuard::~FileSyncGuard()
{
try
{
disk->sync(fd);
disk->close(fd);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
}

View File

@ -1,10 +1,14 @@
#pragma once
#include <Disks/IDisk.h>
#include <string>
#include <memory>
namespace DB
{
class IDisk;
using DiskPtr = std::shared_ptr<IDisk>;
/// Helper class, that receives file descriptor and does fsync for it in destructor.
/// It's used to keep descriptor open, while doing some operations with it, and do fsync at the end.
/// Guaranties of sequence 'close-reopen-fsync' may depend on kernel version.
@ -15,22 +19,8 @@ public:
/// NOTE: If you have already opened descriptor, it's preferred to use
/// this constructor instead of constructor with path.
FileSyncGuard(const DiskPtr & disk_, int fd_) : disk(disk_), fd(fd_) {}
FileSyncGuard(const DiskPtr & disk_, const String & path)
: disk(disk_), fd(disk_->open(path, O_RDWR)) {}
~FileSyncGuard()
{
try
{
disk->sync(fd);
disk->close(fd);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
FileSyncGuard(const DiskPtr & disk_, const std::string & path);
~FileSyncGuard();
private:
DiskPtr disk;