diff --git a/src/libexpr-tests/nix_api_expr.cc b/src/libexpr-tests/nix_api_expr.cc index 3e5cd0d43..026c89598 100644 --- a/src/libexpr-tests/nix_api_expr.cc +++ b/src/libexpr-tests/nix_api_expr.cc @@ -18,13 +18,13 @@ TEST_F(nix_api_expr_test, nix_eval_state_lookup_path) { auto tmpDir = nix::createTempDir(); auto delTmpDir = std::make_unique(tmpDir, true); - auto nixpkgs = tmpDir + "/pkgs"; - auto nixos = tmpDir + "/cfg"; + auto nixpkgs = tmpDir / "pkgs"; + auto nixos = tmpDir / "cfg"; std::filesystem::create_directories(nixpkgs); std::filesystem::create_directories(nixos); - std::string nixpkgsEntry = "nixpkgs=" + nixpkgs; - std::string nixosEntry = "nixos-config=" + nixos; + std::string nixpkgsEntry = "nixpkgs=" + nixpkgs.string(); + std::string nixosEntry = "nixos-config=" + nixos.string(); const char * lookupPath[] = {nixpkgsEntry.c_str(), nixosEntry.c_str(), nullptr}; auto builder = nix_eval_state_builder_new(ctx, store); diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 520086e28..c9ad30407 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -384,7 +384,7 @@ path_start std::string_view($1.p, $1.l) ); } - Path path(getHome() + std::string($1.p + 1, $1.l - 1)); + Path path(getHome().string() + std::string($1.p + 1, $1.l - 1)); $$ = state->exprs.add(state->exprs.alloc, ref(state->rootFS), path); } ; diff --git a/src/libfetchers/git-lfs-fetch.cc b/src/libfetchers/git-lfs-fetch.cc index f1982c314..e2b2c2e7d 100644 --- a/src/libfetchers/git-lfs-fetch.cc +++ b/src/libfetchers/git-lfs-fetch.cc @@ -268,10 +268,10 @@ void Fetch::fetch( return; } - Path cacheDir = getCacheDir() + "/git-lfs"; + std::filesystem::path cacheDir = getCacheDir() / "git-lfs"; std::string key = hashString(HashAlgorithm::SHA256, pointerFilePath.rel()).to_string(HashFormat::Base16, false) + "/" + pointer->oid; - Path cachePath = cacheDir + "/" + key; + std::filesystem::path cachePath = cacheDir / key; if (pathExists(cachePath)) { debug("using cache entry %s -> %s", key, cachePath); sink(readFile(cachePath)); @@ -302,8 +302,8 @@ void Fetch::fetch( downloadToSink(ourl, authHeader, sink, sha256, size); debug("creating cache entry %s -> %s", key, cachePath); - if (!pathExists(dirOf(cachePath))) - createDirs(dirOf(cachePath)); + if (!pathExists(cachePath.parent_path())) + createDirs(cachePath.parent_path()); writeFile(cachePath, sink.s); debug("%s fetched with git-lfs", pointerFilePath); diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc index 4a7fc3d0d..37f776b11 100644 --- a/src/libfetchers/git-utils.cc +++ b/src/libfetchers/git-utils.cc @@ -206,7 +206,7 @@ static void initRepoAtomically(std::filesystem::path & path, bool bare) if (pathExists(path.string())) return; - Path tmpDir = createTempDir(os_string_to_string(PathViewNG{std::filesystem::path(path).parent_path()})); + std::filesystem::path tmpDir = createTempDir(path.parent_path()); AutoDelete delTmpDir(tmpDir, true); Repository tmpRepo; diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index 75e3f1214..0e6a6cb69 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -42,10 +42,10 @@ bool isCacheFileWithinTtl(time_t now, const struct stat & st) return st.st_mtime + static_cast(settings.tarballTtl) > now; } -Path getCachePath(std::string_view key, bool shallow) +std::filesystem::path getCachePath(std::string_view key, bool shallow) { - return getCacheDir() + "/gitv3/" + hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) - + (shallow ? "-shallow" : ""); + return getCacheDir() / "gitv3" + / (hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : "")); } // Returns the name of the HEAD branch. @@ -55,7 +55,7 @@ Path getCachePath(std::string_view key, bool shallow) // // ref: refs/heads/main HEAD // ... -std::optional readHead(const Path & path) +std::optional readHead(const std::filesystem::path & path) { auto [status, output] = runProgram( RunOptions{ @@ -86,7 +86,7 @@ std::optional readHead(const Path & path) // Persist the HEAD ref from the remote repo in the local cached repo. bool storeCachedHead(const std::string & actualUrl, bool shallow, const std::string & headRef) { - Path cacheDir = getCachePath(actualUrl, shallow); + std::filesystem::path cacheDir = getCachePath(actualUrl, shallow); try { runProgram("git", true, {"-C", cacheDir, "--git-dir", ".", "symbolic-ref", "--", "HEAD", headRef}); } catch (ExecError & e) { @@ -109,8 +109,8 @@ std::optional readHeadCached(const std::string & actualUrl, bool sh { // Create a cache path to store the branch of the HEAD ref. Append something // in front of the URL to prevent collision with the repository itself. - Path cacheDir = getCachePath(actualUrl, shallow); - Path headRefFile = cacheDir + "/HEAD"; + std::filesystem::path cacheDir = getCachePath(actualUrl, shallow); + std::filesystem::path headRefFile = cacheDir / "HEAD"; time_t now = time(0); struct stat st; diff --git a/src/libfetchers/include/nix/fetchers/registry.hh b/src/libfetchers/include/nix/fetchers/registry.hh index c9c8b162f..dc7e3edb5 100644 --- a/src/libfetchers/include/nix/fetchers/registry.hh +++ b/src/libfetchers/include/nix/fetchers/registry.hh @@ -39,7 +39,7 @@ struct Registry static std::shared_ptr read(const Settings & settings, const SourcePath & path, RegistryType type); - void write(const Path & path); + void write(const std::filesystem::path & path); void add(const Input & from, const Input & to, const Attrs & extraAttrs); @@ -50,9 +50,9 @@ typedef std::vector> Registries; std::shared_ptr getUserRegistry(const Settings & settings); -std::shared_ptr getCustomRegistry(const Settings & settings, const Path & p); +std::shared_ptr getCustomRegistry(const Settings & settings, const std::filesystem::path & p); -Path getUserRegistryPath(); +std::filesystem::path getUserRegistryPath(); Registries getRegistries(const Settings & settings, Store & store); diff --git a/src/libfetchers/mercurial.cc b/src/libfetchers/mercurial.cc index 87e18133d..65999497c 100644 --- a/src/libfetchers/mercurial.cc +++ b/src/libfetchers/mercurial.cc @@ -213,11 +213,11 @@ struct MercurialInputScheme : InputScheme runHg({"status", "-R", actualUrl, "--clean", "--modified", "--added", "--no-status", "--print0"}), "\0"s); - Path actualPath(absPath(actualUrl)); + std::filesystem::path actualPath(absPath(actualUrl)); PathFilter filter = [&](const Path & p) -> bool { - assert(hasPrefix(p, actualPath)); - std::string file(p, actualPath.size() + 1); + assert(hasPrefix(p, actualPath.string())); + std::string file(p, actualPath.string().size() + 1); auto st = lstat(p); @@ -232,7 +232,7 @@ struct MercurialInputScheme : InputScheme auto storePath = store.addToStore( input.getName(), - {getFSSourceAccessor(), CanonPath(actualPath)}, + {getFSSourceAccessor(), CanonPath(actualPath.string())}, ContentAddressMethod::Raw::NixArchive, HashAlgorithm::SHA256, {}, @@ -275,10 +275,8 @@ struct MercurialInputScheme : InputScheme return makeResult(res->value, res->storePath); } - Path cacheDir = - fmt("%s/hg/%s", - getCacheDir(), - hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false)); + std::filesystem::path cacheDir = + getCacheDir() / "hg" / hashString(HashAlgorithm::SHA256, actualUrl).to_string(HashFormat::Nix32, false); /* If this is a commit hash that we already have, we don't have to pull again. */ @@ -292,7 +290,7 @@ struct MercurialInputScheme : InputScheme try { runHg({"pull", "-R", cacheDir, "--", actualUrl}); } catch (ExecError & e) { - auto transJournal = cacheDir + "/.hg/store/journal"; + auto transJournal = cacheDir / ".hg" / "store" / "journal"; /* hg throws "abandoned transaction" error only if this file exists */ if (pathExists(transJournal)) { runHg({"recover", "-R", cacheDir}); @@ -302,7 +300,7 @@ struct MercurialInputScheme : InputScheme } } } else { - createDirs(dirOf(cacheDir)); + createDirs(dirOf(cacheDir.string())); runHg({"clone", "--noupdate", "--", actualUrl, cacheDir}); } } @@ -328,14 +326,14 @@ struct MercurialInputScheme : InputScheme if (auto res = settings.getCache()->lookupStorePath(revInfoKey(rev), store)) return makeResult(res->value, res->storePath); - Path tmpDir = createTempDir(); + std::filesystem::path tmpDir = createTempDir(); AutoDelete delTmpDir(tmpDir, true); runHg({"archive", "-R", cacheDir, "-r", rev.gitRev(), tmpDir}); - deletePath(tmpDir + "/.hg_archival.txt"); + deletePath(tmpDir / ".hg_archival.txt"); - auto storePath = store.addToStore(name, {getFSSourceAccessor(), CanonPath(tmpDir)}); + auto storePath = store.addToStore(name, {getFSSourceAccessor(), CanonPath(tmpDir.string())}); Attrs infoAttrs({ {"revCount", (uint64_t) revCount}, diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index cb360f03c..c81eb6b53 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -56,7 +56,7 @@ std::shared_ptr Registry::read(const Settings & settings, const Source return registry; } -void Registry::write(const Path & path) +void Registry::write(const std::filesystem::path & path) { nlohmann::json arr; for (auto & entry : entries) { @@ -74,7 +74,7 @@ void Registry::write(const Path & path) json["version"] = 2; json["flakes"] = std::move(arr); - createDirs(dirOf(path)); + createDirs(path.parent_path()); writeFile(path, json.dump(2)); } @@ -90,38 +90,38 @@ void Registry::remove(const Input & input) entries.end()); } -static Path getSystemRegistryPath() +static std::filesystem::path getSystemRegistryPath() { - return settings.nixConfDir + "/registry.json"; + return settings.nixConfDir / "registry.json"; } static std::shared_ptr getSystemRegistry(const Settings & settings) { static auto systemRegistry = Registry::read( settings, - SourcePath{getFSSourceAccessor(), CanonPath{getSystemRegistryPath()}}.resolveSymlinks(), + SourcePath{getFSSourceAccessor(), CanonPath{getSystemRegistryPath().string()}}.resolveSymlinks(), Registry::System); return systemRegistry; } -Path getUserRegistryPath() +std::filesystem::path getUserRegistryPath() { - return getConfigDir() + "/registry.json"; + return getConfigDir() / "registry.json"; } std::shared_ptr getUserRegistry(const Settings & settings) { static auto userRegistry = Registry::read( settings, - SourcePath{getFSSourceAccessor(), CanonPath{getUserRegistryPath()}}.resolveSymlinks(), + SourcePath{getFSSourceAccessor(), CanonPath{getUserRegistryPath().string()}}.resolveSymlinks(), Registry::User); return userRegistry; } -std::shared_ptr getCustomRegistry(const Settings & settings, const Path & p) +std::shared_ptr getCustomRegistry(const Settings & settings, const std::filesystem::path & p) { - static auto customRegistry = - Registry::read(settings, SourcePath{getFSSourceAccessor(), CanonPath{p}}.resolveSymlinks(), Registry::Custom); + static auto customRegistry = Registry::read( + settings, SourcePath{getFSSourceAccessor(), CanonPath{p.string()}}.resolveSymlinks(), Registry::Custom); return customRegistry; } diff --git a/src/libflake-tests/nix_api_flake.cc b/src/libflake-tests/nix_api_flake.cc index da7f01401..84ac8eb40 100644 --- a/src/libflake-tests/nix_api_flake.cc +++ b/src/libflake-tests/nix_api_flake.cc @@ -86,7 +86,7 @@ TEST_F(nix_api_store_test, nix_api_load_flake) auto tmpDir = nix::createTempDir(); nix::AutoDelete delTmpDir(tmpDir, true); - nix::writeFile(tmpDir + "/flake.nix", R"( + nix::writeFile(tmpDir / "flake.nix", R"( { outputs = { ... }: { hello = "potato"; @@ -121,7 +121,8 @@ TEST_F(nix_api_store_test, nix_api_load_flake) assert_ctx_ok(); ASSERT_NE(nullptr, parseFlags); - auto r0 = nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.size()); + auto r0 = + nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.string().size()); assert_ctx_ok(); ASSERT_EQ(NIX_OK, r0); @@ -177,8 +178,8 @@ TEST_F(nix_api_store_test, nix_api_load_flake_with_flags) auto tmpDir = nix::createTempDir(); nix::AutoDelete delTmpDir(tmpDir, true); - nix::createDirs(tmpDir + "/b"); - nix::writeFile(tmpDir + "/b/flake.nix", R"( + nix::createDirs(tmpDir / "b"); + nix::writeFile(tmpDir / "b" / "flake.nix", R"( { outputs = { ... }: { hello = "BOB"; @@ -186,18 +187,18 @@ TEST_F(nix_api_store_test, nix_api_load_flake_with_flags) } )"); - nix::createDirs(tmpDir + "/a"); - nix::writeFile(tmpDir + "/a/flake.nix", R"( + nix::createDirs(tmpDir / "a"); + nix::writeFile(tmpDir / "a" / "flake.nix", R"( { - inputs.b.url = ")" + tmpDir + R"(/b"; + inputs.b.url = ")" + tmpDir.string() + R"(/b"; outputs = { b, ... }: { hello = b.hello; }; } )"); - nix::createDirs(tmpDir + "/c"); - nix::writeFile(tmpDir + "/c/flake.nix", R"( + nix::createDirs(tmpDir / "c"); + nix::writeFile(tmpDir / "c" / "flake.nix", R"( { outputs = { ... }: { hello = "Claire"; @@ -230,7 +231,8 @@ TEST_F(nix_api_store_test, nix_api_load_flake_with_flags) assert_ctx_ok(); ASSERT_NE(nullptr, parseFlags); - auto r0 = nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.size()); + auto r0 = + nix_flake_reference_parse_flags_set_base_directory(ctx, parseFlags, tmpDir.c_str(), tmpDir.string().size()); assert_ctx_ok(); ASSERT_EQ(NIX_OK, r0); diff --git a/src/libstore-tests/nix_api_store.cc b/src/libstore-tests/nix_api_store.cc index a7fa4d8d8..142f648d3 100644 --- a/src/libstore-tests/nix_api_store.cc +++ b/src/libstore-tests/nix_api_store.cc @@ -212,9 +212,9 @@ TEST_F(nix_api_store_test, nix_store_real_path) TEST_F(nix_api_util_context, nix_store_real_path_relocated) { auto tmp = nix::createTempDir(); - std::string storeRoot = tmp + "/store"; - std::string stateDir = tmp + "/state"; - std::string logDir = tmp + "/log"; + std::string storeRoot = tmp / "store"; + std::string stateDir = tmp / "state"; + std::string logDir = tmp / "log"; const char * rootkv[] = {"root", storeRoot.c_str()}; const char * statekv[] = {"state", stateDir.c_str()}; const char * logkv[] = {"log", logDir.c_str()}; @@ -250,7 +250,8 @@ TEST_F(nix_api_util_context, nix_store_real_path_relocated) TEST_F(nix_api_util_context, nix_store_real_path_binary_cache) { - Store * store = nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir()).c_str(), nullptr); + Store * store = + nix_store_open(ctx, nix::fmt("file://%s/binary-cache", nix::createTempDir().string()).c_str(), nullptr); assert_ctx_ok(); ASSERT_NE(store, nullptr); diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 8c542b686..9e72f8577 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -486,7 +486,7 @@ void initLibStore(bool loadConfig) /* On macOS, don't use the per-session TMPDIR (as set e.g. by sshd). This breaks build users because they don't have access to the TMPDIR, in particular in ‘nix-store --serve’. */ - if (hasPrefix(defaultTempDir(), "/var/folders/")) + if (hasPrefix(defaultTempDir().string(), "/var/folders/")) unsetenv("TMPDIR"); #endif diff --git a/src/libstore/include/nix/store/globals.hh b/src/libstore/include/nix/store/globals.hh index 5ddfbee30..fa4f7abbd 100644 --- a/src/libstore/include/nix/store/globals.hh +++ b/src/libstore/include/nix/store/globals.hh @@ -101,7 +101,7 @@ public: /** * The directory where system configuration files are stored. */ - Path nixConfDir; + std::filesystem::path nixConfDir; /** * A list of user configuration files to load. @@ -292,7 +292,7 @@ public: Setting builders{ this, - "@" + nixConfDir + "/machines", + "@" + nixConfDir.string() + "/machines", "builders", R"( A semicolon- or newline-separated list of build machines. diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index a849576f6..ab242bd84 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -1332,7 +1332,7 @@ std::pair LocalStore::createTempDirInStore() /* There is a slight possibility that `tmpDir' gets deleted by the GC between createTempDir() and when we acquire a lock on it. We'll repeat until 'tmpDir' exists and we've locked it. */ - tmpDirFn = createTempDir(config->realStoreDir, "tmp"); + tmpDirFn = createTempDir(std::filesystem::path{config->realStoreDir.get()}, "tmp"); tmpDirFd = openDirectory(tmpDirFn); if (!tmpDirFd) { continue; diff --git a/src/libstore/unix/build/darwin-derivation-builder.cc b/src/libstore/unix/build/darwin-derivation-builder.cc index 613ec6d54..24329bffc 100644 --- a/src/libstore/unix/build/darwin-derivation-builder.cc +++ b/src/libstore/unix/build/darwin-derivation-builder.cc @@ -174,7 +174,7 @@ struct DarwinDerivationBuilder : DerivationBuilderImpl /* The tmpDir in scope points at the temporary build directory for our derivation. Some packages try different mechanisms to find temporary directories, so we want to open up a broader place for them to put their files, if needed. */ - Path globalTmpDir = canonPath(defaultTempDir(), true); + Path globalTmpDir = canonPath(defaultTempDir().string(), true); /* They don't like trailing slashes on subpath directives */ while (!globalTmpDir.empty() && globalTmpDir.back() == '/') diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 758173f41..6b145b343 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -676,16 +676,16 @@ void AutoUnmount::cancel() ////////////////////////////////////////////////////////////////////// -std::string defaultTempDir() +std::filesystem::path defaultTempDir() { return getEnvNonEmpty("TMPDIR").value_or("/tmp"); } -Path createTempDir(const Path & tmpRoot, const Path & prefix, mode_t mode) +std::filesystem::path createTempDir(const std::filesystem::path & tmpRoot, const std::string & prefix, mode_t mode) { while (1) { checkInterrupt(); - Path tmpDir = makeTempPath(tmpRoot, prefix); + std::filesystem::path tmpDir = makeTempPath(tmpRoot, prefix); if (mkdir( tmpDir.c_str() #ifndef _WIN32 // TODO abstract mkdir perms for Windows @@ -727,11 +727,11 @@ std::pair createTempFile(const Path & prefix) return {std::move(fd), tmpl}; } -Path makeTempPath(const Path & root, const Path & suffix) +std::filesystem::path makeTempPath(const std::filesystem::path & root, const std::string & suffix) { // start the counter at a random value to minimize issues with preexisting temp paths static std::atomic counter(std::random_device{}()); - auto tmpRoot = canonPath(root.empty() ? defaultTempDir() : root, true); + auto tmpRoot = canonPath(root.empty() ? defaultTempDir().string() : root.string(), true); return fmt("%1%/%2%-%3%-%4%", tmpRoot, suffix, getpid(), counter.fetch_add(1, std::memory_order_relaxed)); } diff --git a/src/libutil/include/nix/util/file-system.hh b/src/libutil/include/nix/util/file-system.hh index a203af2aa..7673c24fd 100644 --- a/src/libutil/include/nix/util/file-system.hh +++ b/src/libutil/include/nix/util/file-system.hh @@ -334,7 +334,8 @@ typedef std::unique_ptr AutoCloseDir; /** * Create a temporary directory. */ -Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix", mode_t mode = 0755); +std::filesystem::path +createTempDir(const std::filesystem::path & tmpRoot = "", const std::string & prefix = "nix", mode_t mode = 0755); /** * Create a temporary file, returning a file handle and its path. @@ -344,7 +345,7 @@ std::pair createTempFile(const Path & prefix = "nix"); /** * Return `TMPDIR`, or the default temporary directory if unset or empty. */ -Path defaultTempDir(); +std::filesystem::path defaultTempDir(); /** * Interpret `exe` as a location in the ambient file system and return @@ -358,7 +359,7 @@ bool isExecutableFileAmbient(const std::filesystem::path & exe); * The constructed path looks like `--`. To create a * path nested in a directory, provide a suffix starting with `/`. */ -Path makeTempPath(const Path & root, const Path & suffix = ".tmp"); +std::filesystem::path makeTempPath(const std::filesystem::path & root, const std::string & suffix = ".tmp"); /** * Used in various places.