Files
scylladb/utils/file_lock.cc
Avi Kivity fcb8d040e8 treewide: use Software Package Data Exchange (SPDX) license identifiers
Instead of lengthy blurbs, switch to single-line, machine-readable
standardized (https://spdx.dev) license identifiers. The Linux kernel
switched long ago, so there is strong precedent.

Three cases are handled: AGPL-only, Apache-only, and dual licensed.
For the latter case, I chose (AGPL-3.0-or-later and Apache-2.0),
reasoning that our changes are extensive enough to apply our license.

The changes we applied mechanically with a script, except to
licenses/README.md.

Closes #9937
2022-01-18 12:15:18 +01:00

69 lines
1.7 KiB
C++

/*
* Copyright (C) 2014-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <seastar/core/seastar.hh>
#include <seastar/core/posix.hh>
#include <unistd.h>
#include <fcntl.h>
#include "file_lock.hh"
class utils::file_lock::impl {
public:
impl(fs::path path)
: _path(std::move(path)), _fd(
file_desc::open(_path.native(), O_RDWR | O_CREAT | O_CLOEXEC,
S_IRWXU)) {
if (::lockf(_fd.get(), F_TLOCK, 0) != 0) {
throw std::system_error(errno, std::system_category(),
"Could not acquire lock: " + _path.native());
}
}
impl(impl&&) = default;
~impl() {
if (!_path.empty()) {
::unlink(_path.c_str());
}
assert(_fd.get() != -1);
auto r = ::lockf(_fd.get(), F_ULOCK, 0);
assert(r == 0);
}
fs::path _path;
file_desc _fd;
};
utils::file_lock::file_lock(fs::path path)
: _impl(std::make_unique<impl>(std::move(path)))
{}
utils::file_lock::file_lock(file_lock&& f) noexcept
: _impl(std::move(f._impl))
{}
utils::file_lock::~file_lock()
{}
fs::path utils::file_lock::path() const {
return _impl ? _impl->_path : "";
}
future<utils::file_lock> utils::file_lock::acquire(fs::path path) {
// meh. not really any future stuff here. but pretend, for the
// day when a future version of lock etc is added.
try {
return make_ready_future<file_lock>(file_lock(path));
} catch (...) {
return make_exception_future<utils::file_lock>(std::current_exception());
}
}
std::ostream& utils::operator<<(std::ostream& out, const file_lock& f) {
return out << "file lock '" << f.path() << "'";
}