Skip to content

Commit f039075

Browse files
vinayakankugoyalEricson2314
authored andcommitted
Replace Path with std::filesystem::path in libfetchers.
1 parent e761a9f commit f039075

File tree

15 files changed

+72
-70
lines changed

15 files changed

+72
-70
lines changed

src/libexpr-tests/nix_api_expr.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ TEST_F(nix_api_expr_test, nix_eval_state_lookup_path)
1818
{
1919
auto tmpDir = nix::createTempDir();
2020
auto delTmpDir = std::make_unique<nix::AutoDelete>(tmpDir, true);
21-
auto nixpkgs = tmpDir + "/pkgs";
22-
auto nixos = tmpDir + "/cfg";
21+
auto nixpkgs = tmpDir / "pkgs";
22+
auto nixos = tmpDir / "cfg";
2323
std::filesystem::create_directories(nixpkgs);
2424
std::filesystem::create_directories(nixos);
2525

26-
std::string nixpkgsEntry = "nixpkgs=" + nixpkgs;
27-
std::string nixosEntry = "nixos-config=" + nixos;
26+
std::string nixpkgsEntry = "nixpkgs=" + nixpkgs.string();
27+
std::string nixosEntry = "nixos-config=" + nixos.string();
2828
const char * lookupPath[] = {nixpkgsEntry.c_str(), nixosEntry.c_str(), nullptr};
2929

3030
auto builder = nix_eval_state_builder_new(ctx, store);

src/libfetchers/git-lfs-fetch.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ void Fetch::fetch(
268268
return;
269269
}
270270

271-
Path cacheDir = getCacheDir() + "/git-lfs";
271+
std::filesystem::path cacheDir = getCacheDir() / "git-lfs";
272272
std::string key = hashString(HashAlgorithm::SHA256, pointerFilePath.rel()).to_string(HashFormat::Base16, false)
273273
+ "/" + pointer->oid;
274-
Path cachePath = cacheDir + "/" + key;
274+
std::filesystem::path cachePath = cacheDir / key;
275275
if (pathExists(cachePath)) {
276276
debug("using cache entry %s -> %s", key, cachePath);
277277
sink(readFile(cachePath));
@@ -302,8 +302,8 @@ void Fetch::fetch(
302302
downloadToSink(ourl, authHeader, sink, sha256, size);
303303

304304
debug("creating cache entry %s -> %s", key, cachePath);
305-
if (!pathExists(dirOf(cachePath)))
306-
createDirs(dirOf(cachePath));
305+
if (!pathExists(cachePath.parent_path()))
306+
createDirs(cachePath.parent_path());
307307
writeFile(cachePath, sink.s);
308308

309309
debug("%s fetched with git-lfs", pointerFilePath);

src/libfetchers/git-utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void initRepoAtomically(std::filesystem::path & path, bool bare)
206206
if (pathExists(path.string()))
207207
return;
208208

209-
Path tmpDir = createTempDir(os_string_to_string(PathViewNG{std::filesystem::path(path).parent_path()}));
209+
std::filesystem::path tmpDir = createTempDir(path.parent_path());
210210
AutoDelete delTmpDir(tmpDir, true);
211211
Repository tmpRepo;
212212

src/libfetchers/git.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ bool isCacheFileWithinTtl(time_t now, const struct stat & st)
4242
return st.st_mtime + static_cast<time_t>(settings.tarballTtl) > now;
4343
}
4444

45-
Path getCachePath(std::string_view key, bool shallow)
45+
std::filesystem::path getCachePath(std::string_view key, bool shallow)
4646
{
47-
return getCacheDir() + "/gitv3/" + hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false)
48-
+ (shallow ? "-shallow" : "");
47+
return getCacheDir() / "gitv3"
48+
/ (hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : ""));
4949
}
5050

5151
// Returns the name of the HEAD branch.
@@ -55,7 +55,7 @@ Path getCachePath(std::string_view key, bool shallow)
5555
//
5656
// ref: refs/heads/main HEAD
5757
// ...
58-
std::optional<std::string> readHead(const Path & path)
58+
std::optional<std::string> readHead(const std::filesystem::path & path)
5959
{
6060
auto [status, output] = runProgram(
6161
RunOptions{
@@ -86,7 +86,7 @@ std::optional<std::string> readHead(const Path & path)
8686
// Persist the HEAD ref from the remote repo in the local cached repo.
8787
bool storeCachedHead(const std::string & actualUrl, bool shallow, const std::string & headRef)
8888
{
89-
Path cacheDir = getCachePath(actualUrl, shallow);
89+
std::filesystem::path cacheDir = getCachePath(actualUrl, shallow);
9090
try {
9191
runProgram("git", true, {"-C", cacheDir, "--git-dir", ".", "symbolic-ref", "--", "HEAD", headRef});
9292
} catch (ExecError & e) {
@@ -109,8 +109,8 @@ std::optional<std::string> readHeadCached(const std::string & actualUrl, bool sh
109109
{
110110
// Create a cache path to store the branch of the HEAD ref. Append something
111111
// in front of the URL to prevent collision with the repository itself.
112-
Path cacheDir = getCachePath(actualUrl, shallow);
113-
Path headRefFile = cacheDir + "/HEAD";
112+
std::filesystem::path cacheDir = getCachePath(actualUrl, shallow);
113+
std::filesystem::path headRefFile = cacheDir / "HEAD";
114114

115115
time_t now = time(0);
116116
struct stat st;

src/libfetchers/include/nix/fetchers/registry.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct Registry
3939

4040
static std::shared_ptr<Registry> read(const Settings & settings, const SourcePath & path, RegistryType type);
4141

42-
void write(const Path & path);
42+
void write(const std::filesystem::path & path);
4343

4444
void add(const Input & from, const Input & to, const Attrs & extraAttrs);
4545

@@ -50,9 +50,9 @@ typedef std::vector<std::shared_ptr<Registry>> Registries;
5050

5151
std::shared_ptr<Registry> getUserRegistry(const Settings & settings);
5252

53-
std::shared_ptr<Registry> getCustomRegistry(const Settings & settings, const Path & p);
53+
std::shared_ptr<Registry> getCustomRegistry(const Settings & settings, const std::filesystem::path & p);
5454

55-
Path getUserRegistryPath();
55+
std::filesystem::path getUserRegistryPath();
5656

5757
Registries getRegistries(const Settings & settings, Store & store);
5858

src/libfetchers/mercurial.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ struct MercurialInputScheme : InputScheme
213213
runHg({"status", "-R", actualUrl, "--clean", "--modified", "--added", "--no-status", "--print0"}),
214214
"\0"s);
215215

216-
Path actualPath(absPath(actualUrl));
216+
std::filesystem::path actualPath(absPath(actualUrl));
217217

218218
PathFilter filter = [&](const Path & p) -> bool {
219-
assert(hasPrefix(p, actualPath));
220-
std::string file(p, actualPath.size() + 1);
219+
assert(hasPrefix(p, actualPath.string()));
220+
std::string file(p, actualPath.string().size() + 1);
221221

222222
auto st = lstat(p);
223223

@@ -232,7 +232,7 @@ struct MercurialInputScheme : InputScheme
232232

233233
auto storePath = store.addToStore(
234234
input.getName(),
235-
{getFSSourceAccessor(), CanonPath(actualPath)},
235+
{getFSSourceAccessor(), CanonPath(actualPath.string())},
236236
ContentAddressMethod::Raw::NixArchive,
237237
HashAlgorithm::SHA256,
238238
{},
@@ -275,10 +275,8 @@ struct MercurialInputScheme : InputScheme
275275
return makeResult(res->value, res->storePath);
276276
}
277277

278-
Path cacheDir =
279-
fmt("%s/hg/%s",
280-
getCacheDir(),
281-
hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false));
278+
std::filesystem::path cacheDir =
279+
getCacheDir() / "hg" / hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false);
282280

283281
/* If this is a commit hash that we already have, we don't
284282
have to pull again. */
@@ -292,7 +290,7 @@ struct MercurialInputScheme : InputScheme
292290
try {
293291
runHg({"pull", "-R", cacheDir, "--", actualUrl});
294292
} catch (ExecError & e) {
295-
auto transJournal = cacheDir + "/.hg/store/journal";
293+
auto transJournal = cacheDir / ".hg" / "store" / "journal";
296294
/* hg throws "abandoned transaction" error only if this file exists */
297295
if (pathExists(transJournal)) {
298296
runHg({"recover", "-R", cacheDir});
@@ -302,7 +300,7 @@ struct MercurialInputScheme : InputScheme
302300
}
303301
}
304302
} else {
305-
createDirs(dirOf(cacheDir));
303+
createDirs(dirOf(cacheDir.string()));
306304
runHg({"clone", "--noupdate", "--", actualUrl, cacheDir});
307305
}
308306
}
@@ -328,14 +326,14 @@ struct MercurialInputScheme : InputScheme
328326
if (auto res = settings.getCache()->lookupStorePath(revInfoKey(rev), store))
329327
return makeResult(res->value, res->storePath);
330328

331-
Path tmpDir = createTempDir();
329+
std::filesystem::path tmpDir = createTempDir();
332330
AutoDelete delTmpDir(tmpDir, true);
333331

334332
runHg({"archive", "-R", cacheDir, "-r", rev.gitRev(), tmpDir});
335333

336-
deletePath(tmpDir + "/.hg_archival.txt");
334+
deletePath(tmpDir / ".hg_archival.txt");
337335

338-
auto storePath = store.addToStore(name, {getFSSourceAccessor(), CanonPath(tmpDir)});
336+
auto storePath = store.addToStore(name, {getFSSourceAccessor(), CanonPath(tmpDir.string())});
339337

340338
Attrs infoAttrs({
341339
{"revCount", (uint64_t) revCount},

src/libfetchers/registry.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ std::shared_ptr<Registry> Registry::read(const Settings & settings, const Source
5656
return registry;
5757
}
5858

59-
void Registry::write(const Path & path)
59+
void Registry::write(const std::filesystem::path & path)
6060
{
6161
nlohmann::json arr;
6262
for (auto & entry : entries) {
@@ -74,7 +74,7 @@ void Registry::write(const Path & path)
7474
json["version"] = 2;
7575
json["flakes"] = std::move(arr);
7676

77-
createDirs(dirOf(path));
77+
createDirs(path.parent_path());
7878
writeFile(path, json.dump(2));
7979
}
8080

@@ -90,38 +90,38 @@ void Registry::remove(const Input & input)
9090
entries.end());
9191
}
9292

93-
static Path getSystemRegistryPath()
93+
static std::filesystem::path getSystemRegistryPath()
9494
{
95-
return settings.nixConfDir + "/registry.json";
95+
return settings.nixConfDir / "registry.json";
9696
}
9797

9898
static std::shared_ptr<Registry> getSystemRegistry(const Settings & settings)
9999
{
100100
static auto systemRegistry = Registry::read(
101101
settings,
102-
SourcePath{getFSSourceAccessor(), CanonPath{getSystemRegistryPath()}}.resolveSymlinks(),
102+
SourcePath{getFSSourceAccessor(), CanonPath{getSystemRegistryPath().string()}}.resolveSymlinks(),
103103
Registry::System);
104104
return systemRegistry;
105105
}
106106

107-
Path getUserRegistryPath()
107+
std::filesystem::path getUserRegistryPath()
108108
{
109-
return getConfigDir() + "/registry.json";
109+
return getConfigDir() / "registry.json";
110110
}
111111

112112
std::shared_ptr<Registry> getUserRegistry(const Settings & settings)
113113
{
114114
static auto userRegistry = Registry::read(
115115
settings,
116-
SourcePath{getFSSourceAccessor(), CanonPath{getUserRegistryPath()}}.resolveSymlinks(),
116+
SourcePath{getFSSourceAccessor(), CanonPath{getUserRegistryPath().string()}}.resolveSymlinks(),
117117
Registry::User);
118118
return userRegistry;
119119
}
120120

121-
std::shared_ptr<Registry> getCustomRegistry(const Settings & settings, const Path & p)
121+
std::shared_ptr<Registry> getCustomRegistry(const Settings & settings, const std::filesystem::path & p)
122122
{
123-
static auto customRegistry =
124-
Registry::read(settings, SourcePath{getFSSourceAccessor(), CanonPath{p}}.resolveSymlinks(), Registry::Custom);
123+
static auto customRegistry = Registry::read(
124+
settings, SourcePath{getFSSourceAccessor(), CanonPath{p.string()}}.resolveSymlinks(), Registry::Custom);
125125
return customRegistry;
126126
}
127127

src/libflake-tests/nix_api_flake.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ TEST_F(nix_api_store_test, nix_api_load_flake)
8686
auto tmpDir = nix::createTempDir();
8787
nix::AutoDelete delTmpDir(tmpDir, true);
8888

89-
nix::writeFile(tmpDir + "/flake.nix", R"(
89+
nix::writeFile(tmpDir / "flake.nix", R"(
9090
{
9191
outputs = { ... }: {
9292
hello = "potato";
@@ -121,7 +121,8 @@ TEST_F(nix_api_store_test, nix_api_load_flake)
121121
assert_ctx_ok();
122122
ASSERT_NE(nullptr, parseFlags);
123123

124-
auto r0 = nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.size());
124+
auto r0 =
125+
nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.string().size());
125126
assert_ctx_ok();
126127
ASSERT_EQ(NIX_OK, r0);
127128

@@ -177,27 +178,27 @@ TEST_F(nix_api_store_test, nix_api_load_flake_with_flags)
177178
auto tmpDir = nix::createTempDir();
178179
nix::AutoDelete delTmpDir(tmpDir, true);
179180

180-
nix::createDirs(tmpDir + "/b");
181-
nix::writeFile(tmpDir + "/b/flake.nix", R"(
181+
nix::createDirs(tmpDir / "b");
182+
nix::writeFile(tmpDir / "b" / "flake.nix", R"(
182183
{
183184
outputs = { ... }: {
184185
hello = "BOB";
185186
};
186187
}
187188
)");
188189

189-
nix::createDirs(tmpDir + "/a");
190-
nix::writeFile(tmpDir + "/a/flake.nix", R"(
190+
nix::createDirs(tmpDir / "a");
191+
nix::writeFile(tmpDir / "a" / "flake.nix", R"(
191192
{
192-
inputs.b.url = ")" + tmpDir + R"(/b";
193+
inputs.b.url = ")" + tmpDir.string() + R"(/b";
193194
outputs = { b, ... }: {
194195
hello = b.hello;
195196
};
196197
}
197198
)");
198199

199-
nix::createDirs(tmpDir + "/c");
200-
nix::writeFile(tmpDir + "/c/flake.nix", R"(
200+
nix::createDirs(tmpDir / "c");
201+
nix::writeFile(tmpDir / "c" / "flake.nix", R"(
201202
{
202203
outputs = { ... }: {
203204
hello = "Claire";
@@ -230,7 +231,8 @@ TEST_F(nix_api_store_test, nix_api_load_flake_with_flags)
230231
assert_ctx_ok();
231232
ASSERT_NE(nullptr, parseFlags);
232233

233-
auto r0 = nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.size());
234+
auto r0 =
235+
nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.string().size());
234236
assert_ctx_ok();
235237
ASSERT_EQ(NIX_OK, r0);
236238

src/libstore-tests/nix_api_store.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ TEST_F(nix_api_store_test, nix_store_real_path)
212212
TEST_F(nix_api_util_context, nix_store_real_path_relocated)
213213
{
214214
auto tmp = nix::createTempDir();
215-
std::string storeRoot = tmp + "/store";
216-
std::string stateDir = tmp + "/state";
217-
std::string logDir = tmp + "/log";
215+
std::string storeRoot = tmp / "store";
216+
std::string stateDir = tmp / "state";
217+
std::string logDir = tmp / "log";
218218
const char * rootkv[] = {"root", storeRoot.c_str()};
219219
const char * statekv[] = {"state", stateDir.c_str()};
220220
const char * logkv[] = {"log", logDir.c_str()};
@@ -250,7 +250,8 @@ TEST_F(nix_api_util_context, nix_store_real_path_relocated)
250250

251251
TEST_F(nix_api_util_context, nix_store_real_path_binary_cache)
252252
{
253-
Store * store = nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir()).c_str(), nullptr);
253+
Store * store =
254+
nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir().string()).c_str(), nullptr);
254255
assert_ctx_ok();
255256
ASSERT_NE(store, nullptr);
256257

src/libstore/globals.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void initLibStore(bool loadConfig)
486486
/* On macOS, don't use the per-session TMPDIR (as set e.g. by
487487
sshd). This breaks build users because they don't have access
488488
to the TMPDIR, in particular in ‘nix-store --serve’. */
489-
if (hasPrefix(defaultTempDir(), "/var/folders/"))
489+
if (hasPrefix(defaultTempDir().string(), "/var/folders/"))
490490
unsetenv("TMPDIR");
491491
#endif
492492

0 commit comments

Comments
 (0)