From 6aeec009d348a6bef2923f6eebb9997dfd363cdf Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 10 Jan 2025 15:53:34 +0100 Subject: clang format --- src/zenutil/service.cpp | 119 +++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 62 deletions(-) (limited to 'src/zenutil/service.cpp') diff --git a/src/zenutil/service.cpp b/src/zenutil/service.cpp index f85a8e7e3..cfb19e9a9 100644 --- a/src/zenutil/service.cpp +++ b/src/zenutil/service.cpp @@ -173,10 +173,7 @@ namespace { } } - std::string GetDaemonName(std::string_view ServiceName) - { - return fmt::format("com.epicgames.unreal.{}", ServiceName); - } + std::string GetDaemonName(std::string_view ServiceName) { return fmt::format("com.epicgames.unreal.{}", ServiceName); } std::filesystem::path GetPListPath(const std::string& DaemonName) { @@ -188,7 +185,7 @@ namespace { const std::filesystem::path& ExecutablePath, std::string_view CommandLineOptions, std::string_view DaemonName, - bool Debug) + bool Debug) { std::vector Arguments = SplitArguments(CommandLineOptions); ExtendableStringBuilder<256> ProgramArguments; @@ -253,36 +250,36 @@ namespace { #endif // ZEN_PLATFORM_MAC - #if ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX -std::pair ExecuteProgram(std::string_view Cmd) -{ - std::string data; - const int max_buffer = 256; - char buffer[max_buffer]; - std::string Command(Cmd); - Command.append(" 2>&1"); - - FILE * stream = popen(Command.c_str(), "r"); - if (stream) + std::pair ExecuteProgram(std::string_view Cmd) { - while (!feof(stream)) + std::string data; + const int max_buffer = 256; + char buffer[max_buffer]; + std::string Command(Cmd); + Command.append(" 2>&1"); + + FILE* stream = popen(Command.c_str(), "r"); + if (stream) { - if (fgets(buffer, max_buffer, stream) != NULL) + while (!feof(stream)) { - data.append(buffer); + if (fgets(buffer, max_buffer, stream) != NULL) + { + data.append(buffer); + } } - } - int Res = -1; - int st = pclose(stream); - if (WIFEXITED(st)) Res = WEXITSTATUS(st); - return {Res, data}; - } - return {errno, ""}; + int Res = -1; + int st = pclose(stream); + if (WIFEXITED(st)) + Res = WEXITSTATUS(st); + return {Res, data}; + } + return {errno, ""}; -#if 0 +# if 0 int in[2], out[2], n, pid; char buf[255]; @@ -350,11 +347,10 @@ std::pair ExecuteProgram(std::string_view Cmd) std::string Output(buf); return {0, Output}; -#endif // 0 -} - -#endif // ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX +# endif // 0 + } +#endif // ZEN_PLATFORM_MAC || ZEN_PLATFORM_LINUX } // namespace @@ -695,8 +691,7 @@ std::error_code InstallService(std::string_view ServiceName, const ServiceSpec& Spec) { const std::string DaemonName = GetDaemonName(ServiceName); - std::string PList = - BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true); + std::string PList = BuildPlist(ServiceName, Spec.ExecutablePath, Spec.CommandLineOptions, DaemonName, true); const std::filesystem::path PListPath = GetPListPath(DaemonName); ZEN_INFO("Writing launchd plist to {}", PListPath.string()); @@ -720,8 +715,8 @@ InstallService(std::string_view ServiceName, const ServiceSpec& Spec) std::error_code UninstallService(std::string_view ServiceName) { - const std::string DaemonName = GetDaemonName(ServiceName); - const std::filesystem::path PListPath = GetPListPath(DaemonName); + const std::string DaemonName = GetDaemonName(ServiceName); + const std::filesystem::path PListPath = GetPListPath(DaemonName); ZEN_INFO("Attempting to remove launchd plist from {}", PListPath.string()); std::error_code Ec; std::filesystem::remove(PListPath, Ec); @@ -733,7 +728,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) { ZEN_UNUSED(ServiceName, OutInfo); - OutInfo.Status = ServiceStatus::NotInstalled; + OutInfo.Status = ServiceStatus::NotInstalled; const std::string DaemonName = GetDaemonName(ServiceName); const std::filesystem::path PListPath = GetPListPath(DaemonName); @@ -743,8 +738,8 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) { // Parse plist :( - IoBuffer Buffer = ReadFile(PListPath).Flatten(); - MemoryView Data = Buffer.GetView(); + IoBuffer Buffer = ReadFile(PListPath).Flatten(); + MemoryView Data = Buffer.GetView(); std::string PList((const char*)Data.GetData(), Data.GetSize()); enum class ParseMode @@ -757,10 +752,10 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) ParseMode Mode = ParseMode::None; - ForEachStrTok(PList, '\n', [&](std::string_view Line){ - switch (Mode) - { - case ParseMode::None: + ForEachStrTok(PList, '\n', [&](std::string_view Line) { + switch (Mode) + { + case ParseMode::None: { if (Line.find("ProgramArguments") != std::string_view::npos) { @@ -769,7 +764,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) } } break; - case ParseMode::ExpectingProgramArgumentsArray: + case ParseMode::ExpectingProgramArgumentsArray: { if (Line.find("") != std::string_view::npos) { @@ -779,7 +774,7 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) Mode = ParseMode::None; } break; - case ParseMode::ExpectingProgramExecutablePath: + case ParseMode::ExpectingProgramExecutablePath: { if (std::string_view::size_type ArgStart = Line.find(""); ArgStart != std::string_view::npos) { @@ -787,15 +782,15 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) if (std::string_view::size_type ArgEnd = Line.find("", ArgStart); ArgEnd != std::string_view::npos) { std::string_view ProgramString = Line.substr(ArgStart, ArgEnd - ArgStart); - OutInfo.Spec.ExecutablePath = ProgramString; - Mode = ParseMode::ExpectingCommandLineOption; + OutInfo.Spec.ExecutablePath = ProgramString; + Mode = ParseMode::ExpectingCommandLineOption; return true; } } Mode = ParseMode::None; } break; - case ParseMode::ExpectingCommandLineOption: + case ParseMode::ExpectingCommandLineOption: { if (std::string_view::size_type ArgStart = Line.find(""); ArgStart != std::string_view::npos) { @@ -819,20 +814,20 @@ QueryInstalledService(std::string_view ServiceName, ServiceInfo& OutInfo) Mode = ParseMode::None; } break; - } - return true; + } + return true; }); } { - std::pair Res = ExecuteProgram(std::string("launchctl list ") + DaemonName); + std::pair Res = ExecuteProgram(std::string("launchctl list ") + DaemonName); if (Res.first == 0 && !Res.second.empty()) { - ForEachStrTok(Res.second, '\n', [&](std::string_view Line){ + ForEachStrTok(Res.second, '\n', [&](std::string_view Line) { if (Line.find("\"PID\"") != std::string_view::npos) { - std::string_view::size_type PidStart = Line.find('='); - std::string_view::size_type PidEnd = Line.find(';'); - std::string_view PidString = Line.substr(PidStart+2, PidEnd - (PidStart + 2)); + std::string_view::size_type PidStart = Line.find('='); + std::string_view::size_type PidEnd = Line.find(';'); + std::string_view PidString = Line.substr(PidStart + 2, PidEnd - (PidStart + 2)); if (ParseInt(PidString).has_value()) { OutInfo.Status = ServiceStatus::Running; @@ -854,15 +849,15 @@ StartService(std::string_view ServiceName) { ZEN_UNUSED(ServiceName); - const std::string DaemonName = GetDaemonName(ServiceName); - const std::filesystem::path PListPath = GetPListPath(DaemonName); + const std::string DaemonName = GetDaemonName(ServiceName); + const std::filesystem::path PListPath = GetPListPath(DaemonName); - std::pair Res = ExecuteProgram(std::string("launchctl bootstrap system ") + PListPath.string()); + std::pair Res = ExecuteProgram(std::string("launchctl bootstrap system ") + PListPath.string()); if (Res.first != 0) { return MakeErrorCode(Res.first); } - + return {}; } @@ -871,15 +866,15 @@ StopService(std::string_view ServiceName) { ZEN_UNUSED(ServiceName); - const std::string DaemonName = GetDaemonName(ServiceName); - const std::filesystem::path PListPath = GetPListPath(DaemonName); + const std::string DaemonName = GetDaemonName(ServiceName); + const std::filesystem::path PListPath = GetPListPath(DaemonName); - std::pair Res = ExecuteProgram(std::string("launchctl bootout system ") + PListPath.string()); + std::pair Res = ExecuteProgram(std::string("launchctl bootout system ") + PListPath.string()); if (Res.first != 0) { return MakeErrorCode(Res.first); } - + return {}; } -- cgit v1.2.3