fs/StandardDirectory: pass Path to IsValidDir()

This commit is contained in:
Max Kellermann 2022-11-28 18:23:10 +01:00
parent a38eabc8bc
commit cfbd751742

View File

@ -110,18 +110,21 @@ IsValidPathString(PathTraitsFS::const_pointer path) noexcept
[[gnu::pure]] [[gnu::pure]]
static inline bool static inline bool
IsValidDir(PathTraitsFS::const_pointer dir) noexcept IsValidDir(Path path) noexcept
{ {
return PathTraitsFS::IsAbsolute(dir) && return path.IsAbsolute() && DirectoryExists(path);
DirectoryExists(Path::FromFS(dir));
} }
[[gnu::pure]] [[gnu::pure]]
static inline AllocatedPath static inline AllocatedPath
SafePathFromFS(PathTraitsFS::const_pointer dir) noexcept SafePathFromFS(PathTraitsFS::const_pointer dir) noexcept
{ {
if (IsValidPathString(dir) && IsValidDir(dir)) if (!IsValidPathString(dir))
return AllocatedPath::FromFS(dir); return nullptr;
if (const Path path = Path::FromFS(dir); IsValidDir(path))
return AllocatedPath{path};
return nullptr; return nullptr;
} }
#endif #endif
@ -233,7 +236,7 @@ ParseConfigLine(std::string_view line, std::string_view dir_name,
result = home / result; result = home / result;
} }
if (IsValidDir(result.c_str())) { if (IsValidDir(result)) {
result_dir = std::move(result); result_dir = std::move(result);
return true; return true;
} }
@ -274,7 +277,7 @@ GetUserConfigDir() noexcept
// Check for $HOME/.config // Check for $HOME/.config
if (const auto home = GetHomeDir(); !home.IsNull()) { if (const auto home = GetHomeDir(); !home.IsNull()) {
auto fallback = home / Path::FromFS(".config"); auto fallback = home / Path::FromFS(".config");
if (IsValidDir(fallback.c_str())) if (IsValidDir(fallback))
return fallback; return fallback;
} }
@ -311,7 +314,7 @@ GetUserCacheDir() noexcept
// Check for $HOME/.cache // Check for $HOME/.cache
if (const auto home = GetHomeDir(); !home.IsNull()) if (const auto home = GetHomeDir(); !home.IsNull())
if (auto fallback = home / Path::FromFS(".cache"); if (auto fallback = home / Path::FromFS(".cache");
IsValidDir(fallback.c_str())) IsValidDir(fallback))
return fallback; return fallback;
return nullptr; return nullptr;
@ -406,9 +409,8 @@ AllocatedPath
GetHomeDir() noexcept GetHomeDir() noexcept
{ {
#ifndef ANDROID #ifndef ANDROID
if (const auto home = getenv("HOME"); if (const auto home = GetExistingEnvDirectory("HOME"); home != nullptr)
IsValidPathString(home) && IsValidDir(home)) return AllocatedPath{home};
return AllocatedPath::FromFS(home);
if (PasswdEntry pw; pw.ReadByUid(getuid())) if (PasswdEntry pw; pw.ReadByUid(getuid()))
return SafePathFromFS(pw->pw_dir); return SafePathFromFS(pw->pw_dir);