mergerfs/src/branches.hpp
Antonio S. Musumeci 0dcca0f1a3 branches: re-link default_minfreespace pointer on copy/move; atomically publish branch mode changes
Branches using the default minfreespace held a reference to their
owning Impl's _default_minfreespace. After a copy or move the
pointer still referenced the source Impl, which could become
invalid or diverge after further configuration changes. Switch to
a pointer and re-link it to the destination Impl on copy/move.

While here, convert set_branch_mode to do an atomic_store of a
newly constructed Impl rather than mutating the shared pointer
target in place. Readers holding an atomic_load snapshot are no
longer affected by a concurrent mode change.
2026-04-18 13:47:23 -05:00

103 lines
2.4 KiB
C++

/*
ISC License
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "branch.hpp"
#include "fs_path.hpp"
#include "strvec.hpp"
#include "tofrom_string.hpp"
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#define MINFREESPACE_DEFAULT (4294967295ULL)
class Branches final : public ToFromString
{
public:
class Impl final : public ToFromString,
public std::vector<Branch>
{
private:
const u64 *_default_minfreespace;
public:
using Ptr = std::shared_ptr<Impl>;
public:
Impl(const u64 *default_minfreespace);
public:
int from_string(const std::string_view str) final;
std::string to_string(void) const final;
public:
const u64 &minfreespace(void) const;
void to_paths(StrVec &strvec) const;
std::vector<fs::path> to_paths() const;
public:
Impl& operator=(Impl &impl_);
Impl& operator=(Impl &&impl_);
};
public:
using Ptr = Branches::Impl::Ptr;
public:
u64 minfreespace = MINFREESPACE_DEFAULT;
private:
mutable std::shared_ptr<Impl> _impl;
public:
Branches()
: _impl(std::make_shared<Impl>(&minfreespace))
{
}
public:
int from_string(const std::string_view str) final;
std::string to_string(void) const final;
public:
operator Ptr() const { return std::atomic_load(&_impl); }
Ptr operator->() const { return std::atomic_load(&_impl); }
public:
void find_and_set_mode_ro();
};
class SrcMounts : public ToFromString
{
public:
SrcMounts(Branches &b);
public:
int from_string(const std::string_view str) final;
std::string to_string(void) const final;
private:
Branches &_branches;
};