18#if defined(__APPLE__) || defined(__linux__)
28 static std::shared_ptr<private_construction>
create(std::filesystem::path
const & root) {
31 result->lease_ =
acquire(result->path(),
true);
55 static std::
size_t recover(std::filesystem::
path const & root) {
56 auto location = std::filesystem::canonical(root);
59 std::size_t count = 0;
60 for (
auto const &
id : catalog.private_scopes()) {
63 catch (std::system_error
const & error) {
64 if (error.code() != std::errc::no_such_file_or_directory)
throw;
67 if (catalog.scope_state(
id) == state::active)
throw;
72 if (catalog.scope_state(
id) != state::active) {
83 for (
auto const & entry : std::filesystem::directory_iterator(location)) {
84 auto name = entry.path().filename().string();
85 if (name.size() != 46 || !name.starts_with(
".private-") || !name.ends_with(
".lock"))
continue;
86 auto hex = std::string_view(name).substr(9, 32);
87 if (hex.find_first_not_of(
"0123456789abcdef") != std::string_view::npos)
continue;
90 try { fd =
acquire(entry.path(),
false); }
91 catch (std::system_error
const & error) {
92 if (error.code() == std::errc::no_such_file_or_directory)
continue;
97 if (catalog.scope_state(
id) != state::active)
remove_name(location, entry.path());
107 :
root_(std::move(root)),
id_(std::move(id)) {}
108 static std::filesystem::path
path(std::filesystem::path
const & root,
object_id const &
id) {
109 return root / (
".private-" +
id.hex() +
".lock");
112 static void close(
int fd)
noexcept {
113#if defined(__APPLE__) || defined(__linux__)
120#if defined(__APPLE__) || defined(__linux__)
121 auto fd = ::open(
path.c_str(), O_RDWR | O_CLOEXEC | O_NOFOLLOW | (
create ? O_CREAT | O_EXCL : 0), 0600);
122 if (fd < 0)
throw std::system_error(errno, std::generic_category(),
"open private construction lease");
124 if (::fstat(fd, &info) || !S_ISREG(info.st_mode)) {
125 auto error = errno ? errno : EINVAL;
close(fd);
126 throw std::system_error(error, std::generic_category(),
"private construction lease is not regular");
128 if (::flock(fd, LOCK_EX | LOCK_NB)) {
129 auto error = errno;
close(fd);
130 if (!
create && (error == EWOULDBLOCK || error == EAGAIN))
return -1;
131 throw std::system_error(error, std::generic_category(),
"lock private construction lease");
135 if (::fstat(fd, &info) || info.st_nlink != 1) {
136 auto error = errno;
close(fd);
137 throw std::system_error(info.st_nlink != 1 ? ENOENT : error, std::generic_category(),
138 "private construction lease has no unique name");
143 throw std::system_error(std::make_error_code(std::errc::operation_not_supported),
"private construction leases require POSIX");
146 static void sync_name(std::filesystem::path
const & root,
int fd) {
147#if defined(__APPLE__) || defined(__linux__)
148 if (
::fsync(fd))
throw std::system_error(errno, std::generic_category(),
"sync private construction lease");
150 auto directory = ops.open_root(root);
151 if (directory < 0)
throw std::system_error(errno, std::generic_category(),
"open construction lease directory");
152 auto result = ops.sync_directory(directory);
auto error = errno;
153 ops.close(directory);
154 if (result)
throw std::system_error(error, std::generic_category(),
"sync construction lease directory");
156 (void)root; (void)fd;
159 static void remove_name(std::filesystem::path
const & root, std::filesystem::path
const &
path) {
160#if defined(__APPLE__) || defined(__linux__)
161 if (::unlink(
path.c_str()) && errno != ENOENT)
162 throw std::system_error(errno, std::generic_category(),
"remove released construction lease");
164 auto directory = ops.open_root(root);
165 if (directory < 0)
throw std::system_error(errno, std::generic_category(),
"open released lease directory");
166 auto result = ops.sync_directory(directory);
auto error = errno;
167 ops.close(directory);
168 if (result)
throw std::system_error(error, std::generic_category(),
"sync released lease directory");
170 (void)root; (void)
path;
Definition active_engine.h:18
Seals runtime frontiers and restores named sessions through immutable mappings.
Definition sqlite_catalog.h:39
Definition object_path.h:38
static object_id from_hex(std::string_view hex)
Definition object_path.h:45
Definition object_writer.h:100
Definition private_construction.h:105
int fd
Definition private_construction.h:105
~held()
Definition private_construction.h:105
Definition private_construction.h:27
static std::filesystem::path path(std::filesystem::path const &root, object_id const &id)
Definition private_construction.h:108
std::filesystem::path root_
Definition private_construction.h:102
private_construction & operator=(private_construction const &)=delete
std::filesystem::path path() const
Definition private_construction.h:111
static std::size_t recover(std::filesystem::path const &root)
Definition private_construction.h:55
private_construction(private_construction const &)=delete
static std::shared_ptr< private_construction > create(std::filesystem::path const &root)
Definition private_construction.h:28
catalog_options options() const
Definition private_construction.h:52
object_id id_
Definition private_construction.h:103
~private_construction()
Definition private_construction.h:39
private_construction(std::filesystem::path root, object_id id)
Definition private_construction.h:106
object_id const & identity() const &&=delete
static void sync_name(std::filesystem::path const &root, int fd)
Definition private_construction.h:146
static void close(int fd) noexcept
Definition private_construction.h:112
static void remove_name(std::filesystem::path const &root, std::filesystem::path const &path)
Definition private_construction.h:159
object_id const & identity() const &noexcept
Definition private_construction.h:53
int lease_
Definition private_construction.h:104
static int acquire(std::filesystem::path const &path, bool create)
Definition private_construction.h:119
Definition runtime_store.h:31
private_scope_state
Definition sqlite_catalog.h:435
static sqlite_catalog open(std::filesystem::path const &root, catalog_options options={}, Ops ops={})
Definition sqlite_catalog.h:379