From c46c3555a1ea1f4a12cb6e4981a8fa4233e4e718 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 9 Dec 2022 11:15:17 -0500 Subject: [PATCH] LibCore: Add a standard path for runtime communication files This corresponds to XDG_RUNTIME_DIR, which is the path applications may place runtime communication files, like local sockets. --- Userland/Libraries/LibCore/StandardPaths.cpp | 22 ++++++++++++++++++++ Userland/Libraries/LibCore/StandardPaths.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/Userland/Libraries/LibCore/StandardPaths.cpp b/Userland/Libraries/LibCore/StandardPaths.cpp index 73d51d7302..feea2afa9e 100644 --- a/Userland/Libraries/LibCore/StandardPaths.cpp +++ b/Userland/Libraries/LibCore/StandardPaths.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -83,6 +84,27 @@ DeprecatedString StandardPaths::data_directory() return LexicalPath::canonicalized_path(builder.to_deprecated_string()); } +ErrorOr StandardPaths::runtime_directory() +{ + if (auto* data_directory = getenv("XDG_RUNTIME_DIR")) + return LexicalPath::canonicalized_path(data_directory); + + StringBuilder builder; + +#if defined(AK_OS_SERENITY) + auto sid = TRY(Core::System::getsid()); + builder.appendff("/tmp/session/{}", sid); +#elif defined(AK_OS_MACOS) + builder.append(home_directory()); + builder.append("/Library/Application Support"sv); +#else + auto uid = getuid(); + builder.appendff("/run/user/{}", uid); +#endif + + return LexicalPath::canonicalized_path(builder.to_deprecated_string()); +} + DeprecatedString StandardPaths::tempfile_directory() { return "/tmp"; diff --git a/Userland/Libraries/LibCore/StandardPaths.h b/Userland/Libraries/LibCore/StandardPaths.h index 78014ea279..ac6dd2b485 100644 --- a/Userland/Libraries/LibCore/StandardPaths.h +++ b/Userland/Libraries/LibCore/StandardPaths.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Core { @@ -19,6 +20,7 @@ public: static DeprecatedString tempfile_directory(); static DeprecatedString config_directory(); static DeprecatedString data_directory(); + static ErrorOr runtime_directory(); }; }