fs/StandardDirectory: use "if" with initializer

This commit is contained in:
Max Kellermann 2021-10-26 09:26:55 +02:00
parent 1bb22f118d
commit 225d85fd9b

View File

@ -228,13 +228,12 @@ GetUserConfigDir() noexcept
return GetStandardDir(CSIDL_LOCAL_APPDATA); return GetStandardDir(CSIDL_LOCAL_APPDATA);
#elif defined(USE_XDG) #elif defined(USE_XDG)
// Check for $XDG_CONFIG_HOME // Check for $XDG_CONFIG_HOME
auto config_home = getenv("XDG_CONFIG_HOME"); if (const auto config_home = getenv("XDG_CONFIG_HOME");
if (IsValidPathString(config_home) && IsValidDir(config_home)) IsValidPathString(config_home) && IsValidDir(config_home))
return AllocatedPath::FromFS(config_home); return AllocatedPath::FromFS(config_home);
// Check for $HOME/.config // Check for $HOME/.config
auto home = GetHomeDir(); if (const auto home = GetHomeDir(); !home.IsNull()) {
if (!home.IsNull()) {
auto fallback = home / Path::FromFS(".config"); auto fallback = home / Path::FromFS(".config");
if (IsValidDir(fallback.c_str())) if (IsValidDir(fallback.c_str()))
return fallback; return fallback;
@ -265,17 +264,15 @@ GetUserCacheDir() noexcept
{ {
#ifdef USE_XDG #ifdef USE_XDG
// Check for $XDG_CACHE_HOME // Check for $XDG_CACHE_HOME
auto cache_home = getenv("XDG_CACHE_HOME"); if (const auto cache_home = getenv("XDG_CACHE_HOME");
if (IsValidPathString(cache_home) && IsValidDir(cache_home)) IsValidPathString(cache_home) && IsValidDir(cache_home))
return AllocatedPath::FromFS(cache_home); return AllocatedPath::FromFS(cache_home);
// Check for $HOME/.cache // Check for $HOME/.cache
auto home = GetHomeDir(); if (const auto home = GetHomeDir(); !home.IsNull())
if (!home.IsNull()) { if (auto fallback = home / Path::FromFS(".cache");
auto fallback = home / Path::FromFS(".cache"); IsValidDir(fallback.c_str()))
if (IsValidDir(fallback.c_str()))
return fallback; return fallback;
}
return nullptr; return nullptr;
#elif defined(ANDROID) #elif defined(ANDROID)
@ -317,11 +314,11 @@ AllocatedPath
GetHomeDir() noexcept GetHomeDir() noexcept
{ {
#ifndef ANDROID #ifndef ANDROID
auto home = getenv("HOME"); if (const auto home = getenv("HOME");
if (IsValidPathString(home) && IsValidDir(home)) IsValidPathString(home) && IsValidDir(home))
return AllocatedPath::FromFS(home); return AllocatedPath::FromFS(home);
PasswdEntry pw;
if (pw.ReadByUid(getuid())) if (PasswdEntry pw; pw.ReadByUid(getuid()))
return SafePathFromFS(pw->pw_dir); return SafePathFromFS(pw->pw_dir);
#endif #endif
return nullptr; return nullptr;
@ -334,8 +331,8 @@ GetHomeDir(const char *user_name) noexcept
(void)user_name; (void)user_name;
#else #else
assert(user_name != nullptr); assert(user_name != nullptr);
PasswdEntry pw;
if (pw.ReadByName(user_name)) if (PasswdEntry pw; pw.ReadByName(user_name))
return SafePathFromFS(pw->pw_dir); return SafePathFromFS(pw->pw_dir);
#endif #endif
return nullptr; return nullptr;