[ui] Add auto-mount on first launch functionality #52

This commit is contained in:
2025-09-06 18:19:41 -05:00
parent f4a7e0e187
commit f473d5c855
8 changed files with 131 additions and 100 deletions

View File

@@ -43,6 +43,17 @@ enum class launchctl_type : std::uint8_t {
bootstrap,
kickstart,
};
struct plist_cfg final {
std::vector<std::string> args;
bool keep_alive{false};
std::string label;
std::string plist_path;
bool run_at_load{false};
std::string stderr_log{"/tmp/stderr.log"};
std::string stdout_log{"/tmp/stdout.log"};
std::string working_dir{"/tmp"};
};
#endif // defined(__APPLE__)
[[nodiscard]] auto create_daemon(std::function<int()> main_func) -> int;
@@ -57,12 +68,9 @@ void windows_create_to_unix(const UINT32 &create_options,
const UINT32 &granted_access, std::uint32_t &flags,
remote::file_mode &mode);
#if defined(__APPLE__)
[[nodiscard]] auto generate_launchd_plist(
const std::string &label, std::string plist_path,
const std::vector<std::string> &args, bool run_at_load = false,
bool keep_alive = false, const std::string &working_dir = "/tmp",
const std::string &stdout_log = "/tmp/stdout.log",
const std::string &stderr_log = "/tmp/stderr.log") -> bool;
[[nodiscard]] auto generate_launchd_plist(const plist_cfg &cfg,
bool overwrite_existing = true)
-> bool;
[[nodiscard]] auto launchctl_command(std::string_view label,
launchctl_type type) -> int;

View File

@@ -448,13 +448,19 @@ auto fuse_base::mount([[maybe_unused]] std::vector<std::string> orig_args,
orig_args[0U] = utils::path::combine(".", {"repertory"});
orig_args.insert(std::next(orig_args.begin()), "-f");
if (not utils::generate_launchd_plist(
label_, utils::path::combine("~", {"/Library/LaunchAgents"}),
orig_args, false, false, utils::path::absolute("."),
fmt::format("/tmp/repertory_{}_{}.out",
provider_type_to_string(prov), unique_id),
fmt::format("/tmp/repertory_{}_{}.err",
provider_type_to_string(prov), unique_id))) {
utils::plist_cfg cfg{};
cfg.args = orig_args;
cfg.keep_alive = false;
cfg.label = label_;
cfg.plist_path = utils::path::combine("~", {"/Library/LaunchAgents"});
cfg.run_at_load = false;
cfg.stderr_log = fmt::format("/tmp/repertory_{}_{}.err",
provider_type_to_string(prov), unique_id);
cfg.stdout_log = fmt::format("/tmp/repertory_{}_{}.out",
provider_type_to_string(prov), unique_id);
cfg.working_dir = utils::path::absolute(".");
if (not utils::generate_launchd_plist(cfg, true)) {
std::cerr << fmt::format("FATAL: Failed to generate plist|{}", label_)
<< std::endl;
return -1;

View File

@@ -308,12 +308,13 @@ auto create_daemon(std::function<int()> main_func) -> int {
}
#if defined(__APPLE__)
auto generate_launchd_plist(const std::string &label, std::string plist_path,
const std::vector<std::string> &args,
bool run_at_load, bool keep_alive,
const std::string &working_dir,
const std::string &stdout_log,
const std::string &stderr_log) -> bool {
auto generate_launchd_plist(const plist_cfg &cfg, bool overwrite_existing)
-> bool {
auto file = utils::path::combine(cfg.plist_path, {cfg.label + ".plist"});
if (utils::file::file{file}.exists() && not overwrite_existing) {
return true;
}
pugi::xml_document doc;
auto decl = doc.append_child(pugi::node_declaration);
decl.append_attribute("version") = "1.0";
@@ -325,11 +326,11 @@ auto generate_launchd_plist(const std::string &label, std::string plist_path,
auto dict = plist.append_child("dict");
dict.append_child("key").text().set("Label");
dict.append_child("string").text().set(label.c_str());
dict.append_child("string").text().set(cfg.label.c_str());
dict.append_child("key").text().set("ProgramArguments");
auto array = dict.append_child("array");
for (const auto &arg : args) {
for (const auto &arg : cfg.args) {
array.append_child("string").text().set(arg.c_str());
}
@@ -347,23 +348,22 @@ auto generate_launchd_plist(const std::string &label, std::string plist_path,
}
dict.append_child("key").text().set("WorkingDirectory");
dict.append_child("string").text().set(working_dir.c_str());
dict.append_child("string").text().set(cfg.working_dir.c_str());
dict.append_child("key").text().set("KeepAlive");
dict.append_child(keep_alive ? "true" : "false");
dict.append_child(cfg.keep_alive ? "true" : "false");
dict.append_child("key").text().set("RunAtLoad");
dict.append_child(run_at_load ? "true" : "false");
dict.append_child(cfg.run_at_load ? "true" : "false");
dict.append_child("key").text().set("StandardOutPath");
dict.append_child("string").text().set(stdout_log.c_str());
dict.append_child("string").text().set(cfg.stdout_log.c_str());
dict.append_child("key").text().set("StandardErrorPath");
dict.append_child("string").text().set(stderr_log.c_str());
dict.append_child("string").text().set(cfg.stderr_log.c_str());
return doc.save_file(
utils::path::combine(plist_path, {label + ".plist"}).c_str(), " ",
pugi::format_indent | pugi::format_write_bom);
return doc.save_file(file.c_str(), " ",
pugi::format_indent | pugi::format_write_bom);
}
auto launchctl_command(std::string_view label, launchctl_type type) -> int {