aboutsummaryrefslogtreecommitdiffstats
path: root/src/ConfigPath.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-09-12 10:30:00 +0200
committerMax Kellermann <max@duempel.org>2013-09-12 10:30:00 +0200
commit5f2705ab0755c9e1bb583223cf6ef27474813337 (patch)
tree72e04c675445cfab199f66ad9b9b3d8014fc2d58 /src/ConfigPath.cxx
parent9af620982c5f35f478484ad3a4ea236b133481c5 (diff)
downloadmpd-5f2705ab0755c9e1bb583223cf6ef27474813337.tar.gz
mpd-5f2705ab0755c9e1bb583223cf6ef27474813337.tar.xz
mpd-5f2705ab0755c9e1bb583223cf6ef27474813337.zip
ConfigPath: move code to GetHome()
Diffstat (limited to 'src/ConfigPath.cxx')
-rw-r--r--src/ConfigPath.cxx76
1 files changed, 48 insertions, 28 deletions
diff --git a/src/ConfigPath.cxx b/src/ConfigPath.cxx
index cc2292c51..1c6875bd4 100644
--- a/src/ConfigPath.cxx
+++ b/src/ConfigPath.cxx
@@ -30,6 +30,49 @@
#ifndef WIN32
#include <pwd.h>
+
+/**
+ * Determine a given user's home directory.
+ */
+static const char *
+GetHome(const char *user, Error &error)
+{
+ passwd *pw = getpwnam(user);
+ if (pw == nullptr) {
+ error.Format(path_domain,
+ "no such user: %s", user);
+ return nullptr;
+ }
+
+ return pw->pw_dir;
+}
+
+/**
+ * Determine the current user's home directory.
+ */
+static const char *
+GetHome(Error &error)
+{
+ const char *home = g_get_home_dir();
+ if (home == nullptr)
+ error.Set(path_domain,
+ "problems getting home for current user");
+
+ return home;
+}
+
+/**
+ * Determine the configured user's home directory.
+ */
+static const char *
+GetConfiguredHome(Error &error)
+{
+ const char *user = config_get_string(CONF_USER, nullptr);
+ return user != nullptr
+ ? GetHome(user, error)
+ : GetHome(error);
+}
+
#endif
Path
@@ -50,25 +93,7 @@ ParsePath(const char *path, Error &error)
const char *home;
if (path[1] == '/' || path[1] == '\0') {
- const char *user = config_get_string(CONF_USER, nullptr);
- if (user != nullptr) {
- struct passwd *passwd = getpwnam(user);
- if (!passwd) {
- error.Format(path_domain,
- "no such user: %s", user);
- return Path::Null();
- }
-
- home = passwd->pw_dir;
- } else {
- home = g_get_home_dir();
- if (home == nullptr) {
- error.Set(path_domain,
- "problems getting home "
- "for current user");
- return Path::Null();
- }
- }
+ home = GetConfiguredHome(error);
++path;
} else {
@@ -79,20 +104,15 @@ ParsePath(const char *path, Error &error)
? g_strndup(path, slash - path)
: g_strdup(path);
- struct passwd *passwd = getpwnam(user);
- if (!passwd) {
- error.Format(path_domain,
- "no such user: %s", user);
- g_free(user);
- return Path::Null();
- }
-
+ home = GetHome(user, error);
g_free(user);
- home = passwd->pw_dir;
path = slash;
}
+ if (home == nullptr)
+ return Path::Null();
+
return Path::Build(home, path2);
} else {
#endif