From 57ad2e97735f9b93a1f5c55ccc7d3f048463d2dc Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Thu, 11 Sep 2025 07:57:53 -0500 Subject: [PATCH] remove launchd entry on unmount --- .../src/drives/fuse/fuse_base.cpp | 11 ++++++---- .../repertory/src/ui/mgmt_app_config.cpp | 8 ++------ support/include/utils/unix.hpp | 6 ++++++ support/src/utils/unix.cpp | 20 +++++++++++++++++++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/repertory/librepertory/src/drives/fuse/fuse_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_base.cpp index 4095719a..71066e03 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_base.cpp @@ -936,11 +936,14 @@ auto fuse_base::unlink_(const char *path) -> int { } auto fuse_base::unmount(const std::string &mount_location) -> int { + REPERTORY_USES_FUNCTION_NAME(); + #if defined(__APPLE__) - if (utils::file::file{ - utils::path::combine("~", {"/Library/LaunchAgents", label_})} - .exists()) { - return utils::launchctl_command(label_, utils::launchctl_type::bootout); + if (not utils::remove_launchd_plist( + utils::path::combine("~", {"/Library/LaunchAgents"}), label_, true)) { + utils::error::raise_error( + function_name, + fmt::format("failed to remove launchd entry|label|{}", label_)); } auto cmd = "umount \"" + mount_location + "\" >/dev/null 2>&1"; diff --git a/repertory/repertory/src/ui/mgmt_app_config.cpp b/repertory/repertory/src/ui/mgmt_app_config.cpp index 0ab969c8..c292878f 100644 --- a/repertory/repertory/src/ui/mgmt_app_config.cpp +++ b/repertory/repertory/src/ui/mgmt_app_config.cpp @@ -304,9 +304,6 @@ void mgmt_app_config::set_auto_start(bool auto_start) { #if defined(__APPLE__) const auto *label = "com.fifthgrid.blockstorage.repertory.ui"; auto plist_path = utils::path::combine("~", {"/Library/LaunchAgents"}); - auto file = utils::file::file{ - utils::path::combine(plist_path, {label}), - }; if (auto_start) { utils::plist_cfg cfg{}; cfg.args = { @@ -330,13 +327,12 @@ void mgmt_app_config::set_auto_start(bool auto_start) { function_name, utils::get_last_error_code(), "failed to create auto-start entry|name|repertory"); } - } else if (file.remove()) { + } else if (utils::remove_launchd_plist(plist_path, label, false)) { utils::error::handle_info(function_name, "removed auto-start entry|name|repertory"); } else { utils::error::raise_error( - function_name, utils::get_last_error_code(), - "failed to remove auto-start entry|name|repertory"); + function_name, "failed to remove auto-start entry|name|repertory"); } #endif // defined(__APPLE__) } else { diff --git a/support/include/utils/unix.hpp b/support/include/utils/unix.hpp index 1d172450..623decbb 100644 --- a/support/include/utils/unix.hpp +++ b/support/include/utils/unix.hpp @@ -99,8 +99,14 @@ void set_last_error_code(int error_code); -> bool; #endif // defined(PROJECT_ENABLE_PUGIXML) +#if defined(PROJECT_ENABLE_FMT) [[nodiscard]] auto launchctl_command(std::string_view label, launchctl_type type) -> int; + +[[nodiscard]] auto remove_launchd_plist(std::string_view plist_path, + std::string_view label, + bool should_bootout) -> bool; +#endif // defined(PROJECT_ENABLE_FMT) #endif // defined(__APPLE__) // template implementations diff --git a/support/src/utils/unix.cpp b/support/src/utils/unix.cpp index 759da41c..6b161587 100644 --- a/support/src/utils/unix.cpp +++ b/support/src/utils/unix.cpp @@ -368,6 +368,7 @@ auto generate_launchd_plist(const plist_cfg &cfg, bool overwrite_existing) } #endif // defined(PROJECT_ENABLE_PUGIXML) +#if defined(PROJECT_ENABLE_FMT) auto launchctl_command(std::string_view label, launchctl_type type) -> int { switch (type) { case launchctl_type::bootout: @@ -398,6 +399,25 @@ auto launchctl_command(std::string_view label, launchctl_type type) -> int { return -1; } + +auto remove_launchd_plist(std::string_view plist_path, std::string_view label, + bool should_bootout) -> bool { + auto file = utils::file::file{ + utils::path::combine(plist_path, {label + ".plist"}), + }; + if (not file.exists()) { + return true; + } + + auto res = + should_bootout ? launchctl_command(label, launchctl_type::bootout) : 0; + if (not file.remove()) { + return false; + } + + return res == 0; +} +#endif // defined(PROJECT_ENABLE_FMT) #endif // defined(__APPLE__) } // namespace repertory::utils