From 3d085c4de72ea6541c1517ef5b6af3a0cfd62e72 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 19 Jan 2022 06:43:59 +1000 Subject: [PATCH] posix: add flock wrapper --- CMakeLists.txt | 2 ++ posix/flock.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ posix/flock.hpp | 32 ++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 posix/flock.cpp create mode 100644 posix/flock.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4baa4c87..8be9bc7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/posix/flock.cpp b/posix/flock.cpp new file mode 100644 index 00000000..d3fa0f43 --- /dev/null +++ b/posix/flock.cpp @@ -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 + */ + +#include "./flock.hpp" +#include "./except.hpp" + +#include + +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; +} + diff --git a/posix/flock.hpp b/posix/flock.hpp new file mode 100644 index 00000000..4fb6282e --- /dev/null +++ b/posix/flock.hpp @@ -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 + */ + +#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; + }; +} \ No newline at end of file