Skip to content

Commit 979d5a7

Browse files
committed
Drop fs alias in favour of std::filesystem
Since we dropped fs::symlink_exists, we no longer have a need for the fs namespace. Having less abstractions makes it easier to lookup the functions in reference documentations.
1 parent 5b59be9 commit 979d5a7

File tree

20 files changed

+129
-160
lines changed

20 files changed

+129
-160
lines changed

src/libcmd/common-eval-args.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
namespace nix {
2020

21-
namespace fs { using namespace std::filesystem; }
2221

2322
fetchers::Settings fetchSettings;
2423

@@ -123,8 +122,8 @@ MixEvalArgs::MixEvalArgs()
123122
.category = category,
124123
.labels = {"original-ref", "resolved-ref"},
125124
.handler = {[&](std::string _from, std::string _to) {
126-
auto from = parseFlakeRef(fetchSettings, _from, fs::current_path().string());
127-
auto to = parseFlakeRef(fetchSettings, _to, fs::current_path().string());
125+
auto from = parseFlakeRef(fetchSettings, _from, std::filesystem::current_path().string());
126+
auto to = parseFlakeRef(fetchSettings, _to, std::filesystem::current_path().string());
128127
fetchers::Attrs extraAttrs;
129128
if (to.subdir != "") extraAttrs["dir"] = to.subdir;
130129
fetchers::overrideRegistry(from.input, to.input, extraAttrs);

src/libcmd/installables.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
namespace nix {
3333

34-
namespace fs { using namespace std::filesystem; }
35-
3634
void completeFlakeInputAttrPath(
3735
AddCompletions & completions,
3836
ref<EvalState> evalState,
@@ -343,7 +341,7 @@ void completeFlakeRefWithFragment(
343341
auto flakeRefS = std::string(prefix.substr(0, hash));
344342

345343
// TODO: ideally this would use the command base directory instead of assuming ".".
346-
auto flakeRef = parseFlakeRef(fetchSettings, expandTilde(flakeRefS), fs::current_path().string());
344+
auto flakeRef = parseFlakeRef(fetchSettings, expandTilde(flakeRefS), std::filesystem::current_path().string());
347345

348346
auto evalCache = openEvalCache(*evalState,
349347
std::make_shared<flake::LockedFlake>(lockFlake(

src/libfetchers-tests/git-utils.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@
1111

1212
namespace nix {
1313

14-
namespace fs {
15-
using namespace std::filesystem;
16-
}
17-
1814
class GitUtilsTest : public ::testing::Test
1915
{
2016
// We use a single repository for all tests.
21-
fs::path tmpDir;
17+
std::filesystem::path tmpDir;
2218
std::unique_ptr<AutoDelete> delTmpDir;
2319

2420
public:

src/libstore-test-support/include/nix/store/tests/nix_api_store.hh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <filesystem>
1212
#include <gtest/gtest.h>
1313

14-
namespace fs { using namespace std::filesystem; }
15-
1614
namespace nixC {
1715
class nix_api_store_test : public nix_api_util_context
1816
{
@@ -27,10 +25,10 @@ public:
2725
{
2826
nix_store_free(store);
2927

30-
for (auto & path : fs::recursive_directory_iterator(nixDir)) {
31-
fs::permissions(path, fs::perms::owner_all);
28+
for (auto & path : std::filesystem::recursive_directory_iterator(nixDir)) {
29+
std::filesystem::permissions(path, std::filesystem::perms::owner_all);
3230
}
33-
fs::remove_all(nixDir);
31+
std::filesystem::remove_all(nixDir);
3432
}
3533

3634
Store * store;
@@ -45,7 +43,7 @@ protected:
4543
auto tmpl = nix::defaultTempDir() + "/tests_nix-store.";
4644
for (size_t i = 0; true; ++i) {
4745
nixDir = tmpl + std::string { i };
48-
if (fs::create_directory(nixDir)) break;
46+
if (std::filesystem::create_directory(nixDir)) break;
4947
}
5048
#else
5149
// resolve any symlinks in i.e. on macOS /tmp -> /private/tmp

src/libstore-tests/machines.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ TEST(machines, getMachinesWithIncorrectFormat) {
163163
}
164164

165165
TEST(machines, getMachinesWithCorrectFileReference) {
166-
auto path = fs::weakly_canonical(getUnitTestData() / "machines/valid");
167-
ASSERT_TRUE(fs::exists(path));
166+
auto path = std::filesystem::weakly_canonical(getUnitTestData() / "machines/valid");
167+
ASSERT_TRUE(std::filesystem::exists(path));
168168

169169
auto actual = Machine::parseConfig({}, "@" + path.string());
170170
ASSERT_THAT(actual, SizeIs(3));
@@ -174,22 +174,22 @@ TEST(machines, getMachinesWithCorrectFileReference) {
174174
}
175175

176176
TEST(machines, getMachinesWithCorrectFileReferenceToEmptyFile) {
177-
fs::path path = "/dev/null";
178-
ASSERT_TRUE(fs::exists(path));
177+
std::filesystem::path path = "/dev/null";
178+
ASSERT_TRUE(std::filesystem::exists(path));
179179

180180
auto actual = Machine::parseConfig({}, "@" + path.string());
181181
ASSERT_THAT(actual, SizeIs(0));
182182
}
183183

184184
TEST(machines, getMachinesWithIncorrectFileReference) {
185-
auto path = fs::weakly_canonical("/not/a/file");
186-
ASSERT_TRUE(!fs::exists(path));
185+
auto path = std::filesystem::weakly_canonical("/not/a/file");
186+
ASSERT_TRUE(!std::filesystem::exists(path));
187187
auto actual = Machine::parseConfig({}, "@" + path.string());
188188
ASSERT_THAT(actual, SizeIs(0));
189189
}
190190

191191
TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) {
192192
EXPECT_THROW(
193-
Machine::parseConfig({}, "@" + fs::weakly_canonical(getUnitTestData() / "machines" / "bad_format").string()),
193+
Machine::parseConfig({}, "@" + std::filesystem::weakly_canonical(getUnitTestData() / "machines" / "bad_format").string()),
194194
FormatError);
195195
}

src/libstore/builtins/unpack-channel.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace nix {
55

6-
namespace fs { using namespace std::filesystem; }
7-
86
void builtinUnpackChannel(
97
const BasicDerivation & drv,
108
const std::map<std::string, Path> & outputs)
@@ -15,11 +13,11 @@ void builtinUnpackChannel(
1513
return i->second;
1614
};
1715

18-
fs::path out{outputs.at("out")};
16+
std::filesystem::path out{outputs.at("out")};
1917
auto & channelName = getAttr("channelName");
2018
auto & src = getAttr("src");
2119

22-
if (fs::path{channelName}.filename().string() != channelName) {
20+
if (std::filesystem::path{channelName}.filename().string() != channelName) {
2321
throw Error("channelName is not allowed to contain filesystem separators, got %1%", channelName);
2422
}
2523

@@ -38,8 +36,8 @@ void builtinUnpackChannel(
3836

3937
auto target = out / channelName;
4038
try {
41-
fs::rename(fileName, target);
42-
} catch (fs::filesystem_error &) {
39+
std::filesystem::rename(fileName, target);
40+
} catch (std::filesystem::filesystem_error &) {
4341
throw SysError("failed to rename %1% to %2%", fileName, target.string());
4442
}
4543
}

src/libutil/executable-path.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
namespace nix {
88

9-
namespace fs {
10-
using namespace std::filesystem;
11-
}
12-
139
constexpr static const OsStringView path_var_separator{
1410
&ExecutablePath::separator,
1511
1,
@@ -28,15 +24,15 @@ ExecutablePath ExecutablePath::parse(const OsString & path)
2824
auto strings = path.empty() ? (std::list<OsString>{})
2925
: basicSplitString<std::list<OsString>, OsChar>(path, path_var_separator);
3026

31-
std::vector<fs::path> ret;
27+
std::vector<std::filesystem::path> ret;
3228
ret.reserve(strings.size());
3329

3430
std::transform(
3531
std::make_move_iterator(strings.begin()),
3632
std::make_move_iterator(strings.end()),
3733
std::back_inserter(ret),
3834
[](OsString && str) {
39-
return fs::path{
35+
return std::filesystem::path{
4036
str.empty()
4137
// "A zero-length prefix is a legacy feature that
4238
// indicates the current working directory. It
@@ -62,13 +58,13 @@ OsString ExecutablePath::render() const
6258
return basicConcatStringsSep(path_var_separator, path2);
6359
}
6460

65-
std::optional<fs::path>
66-
ExecutablePath::findName(const OsString & exe, std::function<bool(const fs::path &)> isExecutable) const
61+
std::optional<std::filesystem::path>
62+
ExecutablePath::findName(const OsString & exe, std::function<bool(const std::filesystem::path &)> isExecutable) const
6763
{
6864
// "If the pathname being sought contains a <slash>, the search
6965
// through the path prefixes shall not be performed."
7066
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
71-
assert(OsPathTrait<fs::path::value_type>::rfindPathSep(exe) == exe.npos);
67+
assert(OsPathTrait<std::filesystem::path::value_type>::rfindPathSep(exe) == exe.npos);
7268

7369
for (auto & dir : directories) {
7470
auto candidate = dir / exe;
@@ -79,7 +75,8 @@ ExecutablePath::findName(const OsString & exe, std::function<bool(const fs::path
7975
return std::nullopt;
8076
}
8177

82-
fs::path ExecutablePath::findPath(const fs::path & exe, std::function<bool(const fs::path &)> isExecutable) const
78+
std::filesystem::path ExecutablePath::findPath(
79+
const std::filesystem::path & exe, std::function<bool(const std::filesystem::path &)> isExecutable) const
8380
{
8481
// "If the pathname being sought contains a <slash>, the search
8582
// through the path prefixes shall not be performed."

0 commit comments

Comments
 (0)