Skip to content

Commit 31ce0c8

Browse files
authored
Merge pull request #14649 from vinayakankugoyal/path
Use std::filesystem::path instead of Path in libexpr.
2 parents 6cc44e4 + 697b068 commit 31ce0c8

File tree

7 files changed

+41
-37
lines changed

7 files changed

+41
-37
lines changed

src/libexpr/eval-settings.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ const std::string & EvalSettings::getCurrentSystem() const
103103
return evalSystem != "" ? evalSystem : settings.thisSystem.get();
104104
}
105105

106-
Path getNixDefExpr()
106+
std::filesystem::path getNixDefExpr()
107107
{
108-
return settings.useXDGBaseDirectories ? getStateDir() + "/defexpr" : getHome() + "/.nix-defexpr";
108+
return settings.useXDGBaseDirectories ? getStateDir() / "defexpr" : getHome() / ".nix-defexpr";
109109
}
110110

111111
} // namespace nix

src/libexpr/include/nix/expr/eval-settings.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,6 @@ struct EvalSettings : Config
366366
/**
367367
* Conventionally part of the default nix path in impure mode.
368368
*/
369-
Path getNixDefExpr();
369+
std::filesystem::path getNixDefExpr();
370370

371371
} // namespace nix

src/libutil/include/nix/util/users.hh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22
///@file
33

4+
#include <filesystem>
5+
46
#include "nix/util/types.hh"
57

68
#ifndef _WIN32
@@ -15,43 +17,43 @@ std::string getUserName();
1517
/**
1618
* @return the given user's home directory from /etc/passwd.
1719
*/
18-
Path getHomeOf(uid_t userId);
20+
std::filesystem::path getHomeOf(uid_t userId);
1921
#endif
2022

2123
/**
2224
* @return $HOME or the user's home directory from /etc/passwd.
2325
*/
24-
Path getHome();
26+
std::filesystem::path getHome();
2527

2628
/**
2729
* @return $NIX_CACHE_HOME or $XDG_CACHE_HOME/nix or $HOME/.cache/nix.
2830
*/
29-
Path getCacheDir();
31+
std::filesystem::path getCacheDir();
3032

3133
/**
3234
* @return $NIX_CONFIG_HOME or $XDG_CONFIG_HOME/nix or $HOME/.config/nix.
3335
*/
34-
Path getConfigDir();
36+
std::filesystem::path getConfigDir();
3537

3638
/**
3739
* @return the directories to search for user configuration files
3840
*/
39-
std::vector<Path> getConfigDirs();
41+
std::vector<std::filesystem::path> getConfigDirs();
4042

4143
/**
4244
* @return $NIX_DATA_HOME or $XDG_DATA_HOME/nix or $HOME/.local/share/nix.
4345
*/
44-
Path getDataDir();
46+
std::filesystem::path getDataDir();
4547

4648
/**
4749
* @return $NIX_STATE_HOME or $XDG_STATE_HOME/nix or $HOME/.local/state/nix.
4850
*/
49-
Path getStateDir();
51+
std::filesystem::path getStateDir();
5052

5153
/**
5254
* Create the Nix state directory and return the path to it.
5355
*/
54-
Path createNixStateDir();
56+
std::filesystem::path createNixStateDir();
5557

5658
/**
5759
* Perform tilde expansion on a path, replacing tilde with the user's

src/libutil/include/nix/util/util.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "nix/util/logging.hh"
77
#include "nix/util/strings.hh"
88

9+
#include <filesystem>
910
#include <functional>
1011
#include <map>
1112
#include <sstream>

src/libutil/unix/users.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ std::string getUserName()
1818
return name;
1919
}
2020

21-
Path getHomeOf(uid_t userId)
21+
std::filesystem::path getHomeOf(uid_t userId)
2222
{
2323
std::vector<char> buf(16384);
2424
struct passwd pwbuf;
@@ -28,9 +28,9 @@ Path getHomeOf(uid_t userId)
2828
return pw->pw_dir;
2929
}
3030

31-
Path getHome()
31+
std::filesystem::path getHome()
3232
{
33-
static Path homeDir = []() {
33+
static std::filesystem::path homeDir = []() {
3434
std::optional<std::string> unownedUserHomeDir = {};
3535
auto homeDir = getEnv("HOME");
3636
if (homeDir) {

src/libutil/users.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,82 @@
55

66
namespace nix {
77

8-
Path getCacheDir()
8+
std::filesystem::path getCacheDir()
99
{
1010
auto dir = getEnv("NIX_CACHE_HOME");
1111
if (dir) {
1212
return *dir;
1313
} else {
1414
auto xdgDir = getEnv("XDG_CACHE_HOME");
1515
if (xdgDir) {
16-
return *xdgDir + "/nix";
16+
return std::filesystem::path{*xdgDir} / "nix";
1717
} else {
18-
return getHome() + "/.cache/nix";
18+
return getHome() / ".cache" / "nix";
1919
}
2020
}
2121
}
2222

23-
Path getConfigDir()
23+
std::filesystem::path getConfigDir()
2424
{
2525
auto dir = getEnv("NIX_CONFIG_HOME");
2626
if (dir) {
2727
return *dir;
2828
} else {
2929
auto xdgDir = getEnv("XDG_CONFIG_HOME");
3030
if (xdgDir) {
31-
return *xdgDir + "/nix";
31+
return std::filesystem::path{*xdgDir} / "nix";
3232
} else {
33-
return getHome() + "/.config/nix";
33+
return getHome() / ".config" / "nix";
3434
}
3535
}
3636
}
3737

38-
std::vector<Path> getConfigDirs()
38+
std::vector<std::filesystem::path> getConfigDirs()
3939
{
40-
Path configHome = getConfigDir();
40+
std::filesystem::path configHome = getConfigDir();
4141
auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg");
42-
std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":");
43-
for (auto & p : result) {
44-
p += "/nix";
42+
auto tokens = tokenizeString<std::vector<std::string>>(configDirs, ":");
43+
std::vector<std::filesystem::path> result;
44+
result.push_back(configHome);
45+
for (auto & token : tokens) {
46+
result.push_back(std::filesystem::path{token} / "nix");
4547
}
46-
result.insert(result.begin(), configHome);
4748
return result;
4849
}
4950

50-
Path getDataDir()
51+
std::filesystem::path getDataDir()
5152
{
5253
auto dir = getEnv("NIX_DATA_HOME");
5354
if (dir) {
5455
return *dir;
5556
} else {
5657
auto xdgDir = getEnv("XDG_DATA_HOME");
5758
if (xdgDir) {
58-
return *xdgDir + "/nix";
59+
return std::filesystem::path{*xdgDir} / "nix";
5960
} else {
60-
return getHome() + "/.local/share/nix";
61+
return getHome() / ".local" / "share" / "nix";
6162
}
6263
}
6364
}
6465

65-
Path getStateDir()
66+
std::filesystem::path getStateDir()
6667
{
6768
auto dir = getEnv("NIX_STATE_HOME");
6869
if (dir) {
6970
return *dir;
7071
} else {
7172
auto xdgDir = getEnv("XDG_STATE_HOME");
7273
if (xdgDir) {
73-
return *xdgDir + "/nix";
74+
return std::filesystem::path{*xdgDir} / "nix";
7475
} else {
75-
return getHome() + "/.local/state/nix";
76+
return getHome() / ".local" / "state" / "nix";
7677
}
7778
}
7879
}
7980

80-
Path createNixStateDir()
81+
std::filesystem::path createNixStateDir()
8182
{
82-
Path dir = getStateDir();
83+
std::filesystem::path dir = getStateDir();
8384
createDirs(dir);
8485
return dir;
8586
}

src/libutil/windows/users.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ std::string getUserName()
3535
return name;
3636
}
3737

38-
Path getHome()
38+
std::filesystem::path getHome()
3939
{
40-
static Path homeDir = []() {
41-
Path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
40+
static std::filesystem::path homeDir = []() {
41+
std::filesystem::path homeDir = getEnv("USERPROFILE").value_or("C:\\Users\\Default");
4242
assert(!homeDir.empty());
4343
return canonPath(homeDir);
4444
}();

0 commit comments

Comments
 (0)