posix: add flock wrapper

This commit is contained in:
Danny Robson 2022-01-19 06:43:59 +10:00
parent acbfa674ff
commit 3d085c4de7
3 changed files with 89 additions and 0 deletions

View File

@ -93,6 +93,8 @@ list (
posix/except.hpp
posix/fd.cpp
posix/fd.hpp
posix/flock.cpp
posix/flock.hpp
posix/ostream.cpp
posix/ostream.hpp
posix/util.cpp

55
posix/flock.cpp Normal file
View File

@ -0,0 +1,55 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2021-2022, Danny Robson <danny@nerdcruft.net>
*/
#include "./flock.hpp"
#include "./except.hpp"
#include <sys/file.h>
using flock_ = cruft::posix::flock;
///////////////////////////////////////////////////////////////////////////////
flock_::flock (
cruft::posix::fd &&_fd,
int operation
)
: m_fd (std::move (_fd))
{
cruft::posix::error::try_code (
::flock (m_fd.native (), operation)
);
}
//-----------------------------------------------------------------------------
flock_::flock (flock_ &&rhs) noexcept
: m_fd (std::move (rhs.m_fd))
{ ; }
//-----------------------------------------------------------------------------
flock_::~flock ()
{
if (m_fd.native () < 0)
return;
cruft::posix::error::try_code (
::flock (m_fd.native (), LOCK_UN)
);
}
//-----------------------------------------------------------------------------
cruft::posix::fd const&
flock_::native (void) const
{
return m_fd;
}

32
posix/flock.hpp Normal file
View File

@ -0,0 +1,32 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2021-2022, Danny Robson <danny@nerdcruft.net>
*/
#include "./fd.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace cruft::posix {
class flock {
public:
flock (cruft::posix::fd const&, int operation);
flock (cruft::posix::fd &&, int operation);
flock (int fd, int operation);
~flock ();
flock (flock&&) noexcept;
flock& operator= (flock&&) noexcept;
flock (flock const&) = delete;
flock& operator= (flock const&) = delete;
cruft::posix::fd const& native (void) const;
private:
cruft::posix::fd m_fd;
};
}