SlHelpers
Toggle main menu visibility
Loading...
Searching...
No Matches
HomeDir.h
1
// SPDX-License-Identifier: GPL-2.0-only
2
3
#pragma once
4
5
#include <filesystem>
6
#include <pwd.h>
7
#include <sys/types.h>
8
#include <unistd.h>
9
10
namespace
SlHelpers {
11
15
class
HomeDir {
16
public
:
17
HomeDir() =
delete
;
18
23
static
std::filesystem::path
get
() noexcept {
24
std::filesystem::path dir;
25
26
if
(
const
auto
homeDir = std::getenv(
"HOME"
)) {
27
dir = homeDir;
28
if
(std::filesystem::exists(dir))
29
return
dir;
30
}
31
32
dir = ::getpwuid(::getuid())->pw_dir;
33
if
(std::filesystem::exists(dir))
34
return
dir;
35
36
return
{};
37
}
38
43
static
std::filesystem::path
getCacheDir
() noexcept {
44
if
(
const
auto
xdgCacheDir = std::getenv(
"XDG_CACHE_HOME"
))
45
return
xdgCacheDir;
46
47
const
auto
home_dir =
get
();
48
if
(home_dir.empty())
49
return
{};
50
51
return
std::filesystem::path(home_dir) /
".cache"
;
52
}
53
59
static
std::filesystem::path
createCacheDir
(
const
std::filesystem::path &subdir)
noexcept
{
60
auto
cache =
getCacheDir
();
61
if
(cache.empty())
62
return
{};
63
cache /= subdir;
64
65
std::error_code ec;
66
std::filesystem::create_directories(cache, ec);
67
if
(ec)
68
return
{};
69
70
return
cache;
71
}
72
};
73
74
}
SlHelpers::HomeDir::getCacheDir
static std::filesystem::path getCacheDir() noexcept
Obtains directory for caching.
Definition
HomeDir.h:43
SlHelpers::HomeDir::get
static std::filesystem::path get() noexcept
Obtains home directory.
Definition
HomeDir.h:23
SlHelpers::HomeDir::createCacheDir
static std::filesystem::path createCacheDir(const std::filesystem::path &subdir) noexcept
Creates (if not existing) and returns getCacheDir() / subdir.
Definition
HomeDir.h:59
include
helpers
HomeDir.h
Generated by
1.17.0