refactor locks
This commit is contained in:
parent
e82fc08365
commit
ad7a557601
@ -84,5 +84,5 @@ public:
|
|||||||
const api_file &file) -> api_error;
|
const api_file &file) -> api_error;
|
||||||
} // namespace repertory
|
} // namespace repertory
|
||||||
|
|
||||||
#endif // _WIN32
|
#endif // !defined(_WIN32)
|
||||||
#endif // REPERTORY_INCLUDE_PLATFORM_UNIXPLATFORM_HPP_
|
#endif // REPERTORY_INCLUDE_PLATFORM_UNIXPLATFORM_HPP_
|
||||||
|
@ -150,23 +150,20 @@ auto lock_data::set_mount_state(bool active, const std::string &mount_location,
|
|||||||
get_lock_file());
|
get_lock_file());
|
||||||
}
|
}
|
||||||
if ((mount_state.find("Active") == mount_state.end()) ||
|
if ((mount_state.find("Active") == mount_state.end()) ||
|
||||||
(mount_state.get<bool>() != active) ||
|
(mount_state["Active"].get<bool>() != active) ||
|
||||||
(active && ((mount_state.find("Location") == mount_state.end()) ||
|
(active &&
|
||||||
(mount_state.get<std::string>() != mount_location)))) {
|
((mount_state.find("Location") == mount_state.end()) ||
|
||||||
auto lines = utils::file::read_file_lines(get_lock_data_file());
|
(mount_state["Location"].get<std::string>() != mount_location)))) {
|
||||||
auto txt = std::accumulate(
|
|
||||||
lines.begin(), lines.end(), std::string(),
|
|
||||||
[](auto &&val, auto &&line) -> auto { return val + line; });
|
|
||||||
auto json_data = json::parse(txt.empty() ? "{}" : txt);
|
|
||||||
json_data = {
|
|
||||||
{"Active", active},
|
|
||||||
{"Location", active ? mount_location : ""},
|
|
||||||
{"PID", active ? pid : -1},
|
|
||||||
};
|
|
||||||
if (mount_location.empty() && not active) {
|
if (mount_location.empty() && not active) {
|
||||||
ret = utils::file::file{get_lock_data_file()}.delete();
|
ret = utils::file::file{get_lock_data_file()}.delete();
|
||||||
} else {
|
} else {
|
||||||
ret = utils::file::write_json_file(get_lock_data_file(), json_data);
|
ret = utils::file::write_json_file(
|
||||||
|
get_lock_data_file(),
|
||||||
|
{
|
||||||
|
{"Active", active},
|
||||||
|
{"Location", active ? mount_location : ""},
|
||||||
|
{"PID", active ? pid : -1},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ret = true;
|
ret = true;
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#include "events/event_system.hpp"
|
#include "events/event_system.hpp"
|
||||||
#include "events/types/filesystem_item_added.hpp"
|
#include "events/types/filesystem_item_added.hpp"
|
||||||
#include "providers/i_provider.hpp"
|
#include "providers/i_provider.hpp"
|
||||||
|
#include "utils/config.hpp"
|
||||||
|
#include "utils/error_utils.hpp"
|
||||||
#include "utils/string.hpp"
|
#include "utils/string.hpp"
|
||||||
|
|
||||||
namespace repertory {
|
namespace repertory {
|
||||||
@ -42,28 +44,35 @@ lock_data::lock_data(provider_type prov, std::string unique_id)
|
|||||||
create_lock_id(prov, unique_id).c_str())) {}
|
create_lock_id(prov, unique_id).c_str())) {}
|
||||||
|
|
||||||
auto lock_data::get_current_mount_state(json &mount_state) -> bool {
|
auto lock_data::get_current_mount_state(json &mount_state) -> bool {
|
||||||
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
HKEY key{};
|
HKEY key{};
|
||||||
auto ret = (::RegCreateKeyExA(HKEY_CURRENT_USER,
|
if (::RegOpenKeyEx(HKEY_CURRENT_USER,
|
||||||
fmt::format(R"(SOFTWARE\{}\Mounts\\{})",
|
fmt::format(R"(SOFTWARE\{}\Mounts\{})",
|
||||||
REPERTORY_DATA_NAME, mutex_id_)
|
REPERTORY_DATA_NAME, mutex_id_)
|
||||||
.c_str(),
|
.c_str(),
|
||||||
0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &key,
|
0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS) {
|
||||||
nullptr) == ERROR_SUCCESS);
|
return true;
|
||||||
if (not ret) {
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string data;
|
std::string data;
|
||||||
DWORD data_size{};
|
DWORD data_size{};
|
||||||
|
|
||||||
DWORD type{REG_SZ};
|
DWORD type{REG_SZ};
|
||||||
::RegGetValueA(key, nullptr, nullptr, 0, &type, data.data(), &data_size);
|
::RegGetValueA(key, nullptr, nullptr, RRF_RT_REG_SZ, &type, nullptr,
|
||||||
|
&data_size);
|
||||||
|
|
||||||
data.resize(data_size);
|
data.resize(data_size);
|
||||||
ret = (::RegGetValueA(key, nullptr, nullptr, 0, &type, data.data(),
|
auto res = ::RegGetValueA(key, nullptr, nullptr, RRF_RT_REG_SZ, &type,
|
||||||
&data_size) == ERROR_SUCCESS);
|
data.data(), &data_size);
|
||||||
|
auto ret = res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND;
|
||||||
if (ret && data_size != 0U) {
|
if (ret && data_size != 0U) {
|
||||||
|
try {
|
||||||
mount_state = json::parse(data);
|
mount_state = json::parse(data);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
utils::error::raise_error(function_name, e, "failed to read mount state");
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
::RegCloseKey(key);
|
::RegCloseKey(key);
|
||||||
@ -133,15 +142,16 @@ auto lock_data::set_mount_state(bool active, std::string_view mount_location,
|
|||||||
json mount_state;
|
json mount_state;
|
||||||
[[maybe_unused]] auto success{get_mount_state(mount_state)};
|
[[maybe_unused]] auto success{get_mount_state(mount_state)};
|
||||||
if (not((mount_state.find("Active") == mount_state.end()) ||
|
if (not((mount_state.find("Active") == mount_state.end()) ||
|
||||||
(mount_state.get<bool>() != active) ||
|
(mount_state["Active"].get<bool>() != active) ||
|
||||||
(active && ((mount_state.find("Location") == mount_state.end()) ||
|
(active &&
|
||||||
(mount_state.get<std::string>() != mount_location))))) {
|
((mount_state.find("Location") == mount_state.end()) ||
|
||||||
|
(mount_state["Location"].get<std::string>() != mount_location))))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
HKEY key{};
|
HKEY key{};
|
||||||
if (::RegCreateKeyExA(HKEY_CURRENT_USER,
|
if (::RegCreateKeyExA(HKEY_CURRENT_USER,
|
||||||
fmt::format(R"(SOFTWARE\{}\Mounts\\{})",
|
fmt::format(R"(SOFTWARE\{}\Mounts\{})",
|
||||||
REPERTORY_DATA_NAME, mutex_id_)
|
REPERTORY_DATA_NAME, mutex_id_)
|
||||||
.c_str(),
|
.c_str(),
|
||||||
0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &key,
|
0, nullptr, 0, KEY_ALL_ACCESS, nullptr, &key,
|
||||||
@ -149,15 +159,6 @@ auto lock_data::set_mount_state(bool active, std::string_view mount_location,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto data{
|
|
||||||
json({
|
|
||||||
{"Active", active},
|
|
||||||
{"Location", active ? mount_location : ""},
|
|
||||||
{"PID", active ? pid : -1},
|
|
||||||
})
|
|
||||||
.dump(),
|
|
||||||
};
|
|
||||||
|
|
||||||
auto ret{false};
|
auto ret{false};
|
||||||
if (mount_location.empty() && not active) {
|
if (mount_location.empty() && not active) {
|
||||||
::RegCloseKey(key);
|
::RegCloseKey(key);
|
||||||
@ -172,6 +173,14 @@ auto lock_data::set_mount_state(bool active, std::string_view mount_location,
|
|||||||
|
|
||||||
ret = (::RegDeleteKeyA(key, mutex_id_.c_str()) == ERROR_SUCCESS);
|
ret = (::RegDeleteKeyA(key, mutex_id_.c_str()) == ERROR_SUCCESS);
|
||||||
} else {
|
} else {
|
||||||
|
auto data{
|
||||||
|
json({
|
||||||
|
{"Active", active},
|
||||||
|
{"Location", active ? mount_location : ""},
|
||||||
|
{"PID", active ? pid : -1},
|
||||||
|
})
|
||||||
|
.dump(),
|
||||||
|
};
|
||||||
ret = (::RegSetValueEx(key, nullptr, 0, REG_SZ,
|
ret = (::RegSetValueEx(key, nullptr, 0, REG_SZ,
|
||||||
reinterpret_cast<const BYTE *>(data.c_str()),
|
reinterpret_cast<const BYTE *>(data.c_str()),
|
||||||
static_cast<DWORD>(data.size())) == ERROR_SUCCESS);
|
static_cast<DWORD>(data.size())) == ERROR_SUCCESS);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user