fix mount state
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

This commit is contained in:
Scott E. Graves 2025-03-24 20:42:56 -05:00
parent ed59cbb91e
commit c9904603df
5 changed files with 28 additions and 42 deletions

View File

@ -30,16 +30,20 @@ class i_provider;
class lock_data final { class lock_data final {
public: public:
explicit lock_data(const provider_type &pt, std::string unique_id /*= ""*/); explicit lock_data(const provider_type &prov, std::string unique_id /*= ""*/);
lock_data(); lock_data(const lock_data &) = delete;
lock_data(lock_data &&) = delete;
auto operator=(const lock_data &) -> lock_data & = delete;
auto operator=(lock_data &&) -> lock_data & = delete;
~lock_data(); ~lock_data();
private: private:
const provider_type pt_; provider_type prov_;
const std::string unique_id_; std::string unique_id_;
const std::string mutex_id_; std::string mutex_id_;
int lock_fd_; int lock_fd_;
int lock_status_{EWOULDBLOCK}; int lock_status_{EWOULDBLOCK};

View File

@ -36,17 +36,14 @@
#include "utils/unix.hpp" #include "utils/unix.hpp"
namespace repertory { namespace repertory {
lock_data::lock_data(const provider_type &pt, std::string unique_id /*= ""*/) lock_data::lock_data(const provider_type &prov, std::string unique_id /*= ""*/)
: pt_(pt), : prov_(prov),
unique_id_(std::move(unique_id)), unique_id_(std::move(unique_id)),
mutex_id_("repertory_" + app_config::get_provider_name(pt) + "_" + mutex_id_("repertory_" + app_config::get_provider_name(prov) + "_" +
unique_id_) { unique_id_) {
lock_fd_ = open(get_lock_file().c_str(), O_CREAT | O_RDWR, S_IWUSR | S_IRUSR); lock_fd_ = open(get_lock_file().c_str(), O_CREAT | O_RDWR, S_IWUSR | S_IRUSR);
} }
lock_data::lock_data()
: pt_(provider_type::sia), unique_id_(""), mutex_id_(""), lock_fd_(-1) {}
lock_data::~lock_data() { release(); } lock_data::~lock_data() { release(); }
auto lock_data::get_lock_data_file() -> std::string { auto lock_data::get_lock_data_file() -> std::string {
@ -105,9 +102,6 @@ auto lock_data::grab_lock(std::uint8_t retry_count) -> lock_result {
lock_status_ = wait_for_lock(lock_fd_, retry_count); lock_status_ = wait_for_lock(lock_fd_, retry_count);
switch (lock_status_) { switch (lock_status_) {
case 0: case 0:
if (not set_mount_state(false, "", -1)) {
utils::error::raise_error(function_name, "failed to set mount state");
}
return lock_result::success; return lock_result::success;
case EWOULDBLOCK: case EWOULDBLOCK:
return lock_result::locked; return lock_result::locked;
@ -140,7 +134,7 @@ auto lock_data::set_mount_state(bool active, const std::string &mount_location,
if (handle != -1) { if (handle != -1) {
if (wait_for_lock(handle) == 0) { if (wait_for_lock(handle) == 0) {
const auto mount_id = const auto mount_id =
app_config::get_provider_display_name(pt_) + unique_id_; app_config::get_provider_display_name(prov_) + unique_id_;
json mount_state; json mount_state;
if (not utils::file::read_json_file(get_lock_data_file(), mount_state)) { if (not utils::file::read_json_file(get_lock_data_file(), mount_state)) {
utils::error::raise_error(function_name, utils::error::raise_error(function_name,

View File

@ -89,23 +89,9 @@ auto lock_data::grab_lock(std::uint8_t retry_count) -> lock_result {
} }
switch (mutex_state_) { switch (mutex_state_) {
case WAIT_OBJECT_0: { case WAIT_OBJECT_0:
ret = lock_result::success; ret = lock_result::success;
auto should_reset = true; break;
json mount_state;
if (get_mount_state(pt_, mount_state)) {
if (mount_state["Active"].get<bool>() &&
mount_state["Location"] == "elevating") {
should_reset = false;
}
}
if (should_reset) {
if (not set_mount_state(false, "", -1)) {
utils::error::raise_error(function_name, "failed to set mount state");
}
}
} break;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
ret = lock_result::locked; ret = lock_result::locked;

View File

@ -28,13 +28,13 @@
#include "providers/provider.hpp" #include "providers/provider.hpp"
#include "types/repertory.hpp" #include "types/repertory.hpp"
#include "utils/cli_utils.hpp" #include "utils/cli_utils.hpp"
#include "utils/com_init_wrapper.hpp"
#include "utils/file.hpp" #include "utils/file.hpp"
#if defined(_WIN32) #if defined(_WIN32)
#include "drives/winfsp/remotewinfsp/remote_client.hpp" #include "drives/winfsp/remotewinfsp/remote_client.hpp"
#include "drives/winfsp/remotewinfsp/remote_winfsp_drive.hpp" #include "drives/winfsp/remotewinfsp/remote_winfsp_drive.hpp"
#include "drives/winfsp/winfsp_drive.hpp" #include "drives/winfsp/winfsp_drive.hpp"
#include "utils/com_init_wrapper.hpp"
using repertory_drive = repertory::winfsp_drive; using repertory_drive = repertory::winfsp_drive;
using remote_client = repertory::remote_winfsp::remote_client; using remote_client = repertory::remote_winfsp::remote_client;
@ -56,6 +56,15 @@ namespace repertory::cli::actions {
mount(std::vector<const char *> args, std::string data_directory, mount(std::vector<const char *> args, std::string data_directory,
int &mount_result, provider_type prov, const std::string &remote_host, int &mount_result, provider_type prov, const std::string &remote_host,
std::uint16_t remote_port, const std::string &unique_id) -> exit_code { std::uint16_t remote_port, const std::string &unique_id) -> exit_code {
lock_data global_lock(provider_type::unknown, "global");
{
auto lock_result = global_lock.grab_lock(100U);
if (lock_result != lock_result::success) {
std::cerr << "FATAL: Unable to get global lock" << std::endl;
return exit_code::lock_failed;
}
}
lock_data lock(prov, unique_id); lock_data lock(prov, unique_id);
auto lock_result = lock.grab_lock(); auto lock_result = lock.grab_lock();
if (lock_result == lock_result::locked) { if (lock_result == lock_result::locked) {
@ -97,13 +106,6 @@ mount(std::vector<const char *> args, std::string data_directory,
} }
#endif // defined(_WIN32) #endif // defined(_WIN32)
lock_data global_lock(provider_type::unknown, "global");
lock_result = global_lock.grab_lock(100U);
if (lock_result != lock_result::success) {
std::cerr << "FATAL: Unable to get global lock" << std::endl;
return exit_code::lock_failed;
}
auto drive_args = utils::cli::parse_drive_options(args, prov, data_directory); auto drive_args = utils::cli::parse_drive_options(args, prov, data_directory);
app_config config(prov, data_directory); app_config config(prov, data_directory);
{ {

View File

@ -168,7 +168,7 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
: http_error_codes::internal_error; : http_error_codes::internal_error;
}); });
server->Get("/api/v1/locations", [this](auto && /* req */, auto &&res) { server->Get("/api/v1/locations", [](auto && /* req */, auto &&res) {
handle_get_available_locations(res); handle_get_available_locations(res);
}); });
@ -567,12 +567,12 @@ void handlers::handle_post_mount(const httplib::Request &req,
return; return;
} }
config_->set_mount_location(prov, name, location);
static std::mutex mount_mtx; static std::mutex mount_mtx;
mutex_lock lock(mount_mtx); mutex_lock lock(mount_mtx);
launch_process(prov, name, {location}, true); launch_process(prov, name, {location}, true);
config_->set_mount_location(prov, name, location);
launch_process(prov, name, {"-status"}); launch_process(prov, name, {"-status"});
} }