Commit 5f2705ab authored by Max Kellermann's avatar Max Kellermann

ConfigPath: move code to GetHome()

parent 9af62098
...@@ -30,6 +30,49 @@ ...@@ -30,6 +30,49 @@
#ifndef WIN32 #ifndef WIN32
#include <pwd.h> #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 #endif
Path Path
...@@ -50,25 +93,7 @@ ParsePath(const char *path, Error &error) ...@@ -50,25 +93,7 @@ ParsePath(const char *path, Error &error)
const char *home; const char *home;
if (path[1] == '/' || path[1] == '\0') { if (path[1] == '/' || path[1] == '\0') {
const char *user = config_get_string(CONF_USER, nullptr); home = GetConfiguredHome(error);
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();
}
}
++path; ++path;
} else { } else {
...@@ -79,20 +104,15 @@ ParsePath(const char *path, Error &error) ...@@ -79,20 +104,15 @@ ParsePath(const char *path, Error &error)
? g_strndup(path, slash - path) ? g_strndup(path, slash - path)
: g_strdup(path); : g_strdup(path);
struct passwd *passwd = getpwnam(user); home = GetHome(user, error);
if (!passwd) {
error.Format(path_domain,
"no such user: %s", user);
g_free(user);
return Path::Null();
}
g_free(user); g_free(user);
home = passwd->pw_dir;
path = slash; path = slash;
} }
if (home == nullptr)
return Path::Null();
return Path::Build(home, path2); return Path::Build(home, path2);
} else { } else {
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment