Initial support for $HOME and some XDG variables inside the configuration file

This commit tries to address issues #263 and #476. It enables the path expansion of HOME, XDG_CONFIG_HOME, XDG_MUSIC_DIR, XDG_CACHE_HOME and XDG_RUNTIME_DIR by using the glue functions already available in MPD.

Signed-off-by: Joan Vilardaga <github-91yu@joanvc.cat>
This commit is contained in:
JoanVC
2025-02-28 21:06:07 +01:00
parent da40483666
commit e8ce417150
5 changed files with 61 additions and 4 deletions

View File

@@ -5,13 +5,18 @@
#include "event/ServerSocket.hxx"
#include "Path.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/XDG.hxx"
void
ServerSocketAddGeneric(ServerSocket &server_socket, const char *address, unsigned int port)
{
if (address == nullptr || 0 == strcmp(address, "any")) {
server_socket.AddPort(port);
#ifdef USE_XDG
} else if (address[0] == '/' || address[0] == '~' || address[0] == '$') {
#else
} else if (address[0] == '/' || address[0] == '~') {
#endif
server_socket.AddPath(ParsePath(address));
} else if (address[0] == '@') {
server_socket.AddAbstract(address);

View File

@@ -5,6 +5,7 @@
#include "Data.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/Traits.hxx"
#include "fs/XDG.hxx"
#include "fs/glue/StandardDirectory.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "util/StringSplit.hxx"
@@ -90,6 +91,29 @@ ParsePath(const char *path)
return GetHome(std::string{user}.c_str())
/ AllocatedPath::FromUTF8Throw(rest);
}
#ifdef USE_XDG
} else if (path[0] == '$') {
++path;
const auto [env_var, rest] = Split(std::string_view{path}, '/');
AllocatedPath xdg_path(nullptr);
if (env_var == "HOME") {
xdg_path = GetConfiguredHome();
} else if (env_var == "XDG_CONFIG_HOME") {
xdg_path = GetUserConfigDir();
} else if (env_var == "XDG_MUSIC_DIR") {
xdg_path = GetUserMusicDir();
} else if (env_var == "XDG_CACHE_HOME") {
xdg_path = GetUserCacheDir();
} else if (env_var == "XDG_RUNTIME_DIR") {
xdg_path = GetUserRuntimeDir();
} else {
throw FmtRuntimeError("environment variable not supported: {:?}", env_var);
}
return xdg_path / AllocatedPath::FromUTF8Throw(rest);
#endif
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
throw FmtRuntimeError("not an absolute path: {:?}", path);
} else {