libcruft-util/posix/flock.cpp

56 lines
1.2 KiB
C++

/*
* 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;
}