diff --git a/src/libcmd/repl-interacter.cc b/src/libcmd/repl-interacter.cc index cf174069947..8eebeec25b5 100644 --- a/src/libcmd/repl-interacter.cc +++ b/src/libcmd/repl-interacter.cc @@ -125,7 +125,7 @@ ReadlineLikeInteracter::Guard ReadlineLikeInteracter::init(detail::ReplCompleter #if !USE_READLINE el_hist_size = 1000; #endif - read_history(historyFile.c_str()); + read_history(historyFile.string().c_str()); auto oldRepl = curRepl; curRepl = repl; Guard restoreRepl([oldRepl] { curRepl = oldRepl; }); @@ -208,7 +208,7 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT ReadlineLikeInteracter::~ReadlineLikeInteracter() { - write_history(historyFile.c_str()); + write_history(historyFile.string().c_str()); } }; // namespace nix diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 83945ae136c..c702df8f039 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -560,7 +560,7 @@ ProcessLineResult NixRepl::processLine(std::string line) auto localStore = state->store.dynamic_pointer_cast(); if (localStore && command == ":bl") { std::string symlink = "repl-result-" + outputName; - localStore->addPermRoot(outputPath, absPath(symlink)); + localStore->addPermRoot(outputPath, absPath(symlink).string()); logger->cout(" ./%s -> %s", symlink, state->store->printStorePath(outputPath)); } else { logger->cout(" %s -> %s", outputName, state->store->printStorePath(outputPath)); diff --git a/src/libstore/builtins/buildenv.cc b/src/libstore/builtins/buildenv.cc index 49ea657551a..514a3f3a38e 100644 --- a/src/libstore/builtins/buildenv.cc +++ b/src/libstore/builtins/buildenv.cc @@ -76,7 +76,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, createLinks(state, srcFile, dstFile, priority); continue; } else if (S_ISLNK(dstSt.st_mode)) { - auto target = canonPath(dstFile, true); + auto target = canonPath(dstFile, true).string(); if (!S_ISDIR(lstat(target).st_mode)) throw Error("collision between %1% and non-directory %2%", PathFmt(srcFile), PathFmt(target)); if (unlink(dstFile.c_str()) == -1) @@ -104,7 +104,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir, if (S_ISLNK(dstSt.st_mode)) { auto prevPriority = state.priorities[dstFile]; if (prevPriority == priority) - throw BuildEnvFileConflictError(readLink(dstFile), srcFile, priority); + throw BuildEnvFileConflictError(readLink(dstFile).string(), srcFile, priority); if (prevPriority < priority) continue; if (unlink(dstFile.c_str()) == -1) diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 155fe2432bb..4d10d336210 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -680,7 +680,7 @@ static void performOp( "you are not privileged to create perm roots\n\n" "hint: you can just do this client-side without special privileges, and probably want to do that instead."); auto storePath = WorkerProto::Serialise::read(*store, rconn); - Path gcRoot = absPath(readString(conn.from)); + Path gcRoot = absPath(readString(conn.from)).string(); logger->startWork(); auto & localFSStore = require(*store); localFSStore.addPermRoot(storePath, gcRoot); @@ -690,7 +690,7 @@ static void performOp( } case WorkerProto::Op::AddIndirectRoot: { - Path path = absPath(readString(conn.from)); + Path path = absPath(readString(conn.from)).string(); logger->startWork(); auto & indirectRootStore = require(*store); diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 10f91543fc0..d644b3c6070 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -41,7 +41,7 @@ static std::string gcRootsDir = "gcroots"; void LocalStore::addIndirectRoot(const Path & path) { std::string hash = hashString(HashAlgorithm::SHA1, path).to_string(HashFormat::Nix32, false); - Path realRoot = canonPath((config->stateDir.get() / gcRootsDir / "auto" / hash).string()); + Path realRoot = canonPath((config->stateDir.get() / gcRootsDir / "auto" / hash).string()).string(); makeSymlink(realRoot, path); } @@ -57,7 +57,7 @@ void LocalStore::createTempRootsFile() if (pathExists(fnTempRoots)) /* It *must* be stale, since there can be no two processes with the same pid. */ - unlink(fnTempRoots.c_str()); + unlink(fnTempRoots.string().c_str()); *fdTempRoots = openLockFile(fnTempRoots, true); @@ -236,14 +236,14 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R } else if (type == std::filesystem::file_type::symlink) { - Path target = readLink(path); + Path target = readLink(path).string(); if (isInStore(target)) foundRoot(path, target); /* Handle indirect roots. */ else { auto parentPath = std::filesystem::path(path).parent_path(); - target = absPath(target, &parentPath); + target = absPath(target, &parentPath).string(); if (!pathExists(target)) { if (isInDir(path, std::filesystem::path{config->stateDir.get()} / gcRootsDir / "auto")) { printInfo("removing stale link from '%1%' to '%2%'", path, target); @@ -252,7 +252,7 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R } else { if (!std::filesystem::is_symlink(target)) return; - Path target2 = readLink(target); + Path target2 = readLink(target).string(); if (isInStore(target2)) foundRoot(target, target2); } @@ -408,7 +408,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) throw UnimplementedError("External GC client not implemented yet"); #else if (fcntl(fdServer.get(), F_SETFL, fcntl(fdServer.get(), F_GETFL) | O_NONBLOCK) == -1) - throw SysError("making socket '%s' non-blocking", PathFmt(socketPath)); + throw SysError("making socket %s non-blocking", PathFmt(socketPath)); Pipe shutdownPipe; shutdownPipe.create(); @@ -539,6 +539,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) /* Helper function that deletes a path from the store and throws GCLimitReached if we've deleted enough garbage. */ auto deleteFromStore = [&](std::string_view baseName, bool isKnownPath) { + assert(!std::filesystem::path(baseName).is_absolute()); Path path = storeDir + "/" + std::string(baseName); auto realPath = config->realStoreDir.get() / std::string(baseName); @@ -548,7 +549,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) if (baseName.find("tmp-", 0) == 0) { AutoCloseFD tmpDirFd = openDirectory(realPath); if (!tmpDirFd || !lockFile(tmpDirFd.get(), ltWrite, false)) { - debug("skipping locked tempdir '%s'", PathFmt(realPath)); + debug("skipping locked tempdir %s", PathFmt(realPath)); return; } } @@ -713,7 +714,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) printInfo("determining live/dead paths..."); try { - AutoCloseDir dir(opendir(config->realStoreDir.get().c_str())); + AutoCloseDir dir(opendir(config->realStoreDir.get().string().c_str())); if (!dir) throw SysError("opening directory %1%", PathFmt(config->realStoreDir.get())); @@ -757,7 +758,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) if (options.action == GCOptions::gcDeleteDead || options.action == GCOptions::gcDeleteSpecific) { printInfo("deleting unused links..."); - AutoCloseDir dir(opendir(linksDir.c_str())); + AutoCloseDir dir(opendir(linksDir.string().c_str())); if (!dir) throw SysError("opening directory %1%", PathFmt(linksDir)); @@ -769,7 +770,7 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) std::string name = dirent->d_name; if (name == "." || name == "..") continue; - Path path = linksDir + "/" + name; + Path path = (linksDir / name).string(); auto st = lstat(path); diff --git a/src/libstore/include/nix/store/local-fs-store.hh b/src/libstore/include/nix/store/local-fs-store.hh index 7e6801f6397..f78f6fd33d8 100644 --- a/src/libstore/include/nix/store/local-fs-store.hh +++ b/src/libstore/include/nix/store/local-fs-store.hh @@ -53,18 +53,24 @@ public: Setting stateDir{ this, - rootDir.get() ? *rootDir.get() + "/nix/var/nix" : getDefaultStateDir(), + rootDir.get() ? *rootDir.get() / "nix/var/nix" : std::filesystem::path(getDefaultStateDir()), "state", - "Directory where Nix stores state."}; + "Directory where Nix stores state.", + }; Setting logDir{ this, - rootDir.get() ? *rootDir.get() + "/nix/var/log/nix" : getDefaultLogDir(), + rootDir.get() ? *rootDir.get() / "nix/var/log/nix" : std::filesystem::path(getDefaultLogDir()), "log", - "directory where Nix stores log files."}; + "directory where Nix stores log files.", + }; Setting realStoreDir{ - this, rootDir.get() ? *rootDir.get() + "/nix/store" : storeDir, "real", "Physical path of the Nix store."}; + this, + rootDir.get() ? *rootDir.get() / "nix/store" : std::filesystem::path(storeDir), + "real", + "Physical path of the Nix store.", + }; }; struct alignas(8) /* Work around ASAN failures on i686-linux. */ @@ -94,7 +100,7 @@ struct alignas(8) /* Work around ASAN failures on i686-linux. */ * @param gcRoot The location of the symlink. * * @param storePath The store object being rooted. The symlink will - * point to `toRealPath(store.printStorePath(storePath))`. + * point to `toRealPath(storePath)`. * * How the permanent GC root corresponding to this symlink is * managed is implementation-specific. @@ -108,13 +114,7 @@ struct alignas(8) /* Work around ASAN failures on i686-linux. */ Path toRealPath(const StorePath & storePath) { - return toRealPath(printStorePath(storePath)); - } - - Path toRealPath(const Path & storePath) - { - assert(isInStore(storePath)); - return getRealStoreDir() + "/" + std::string(storePath, storeDir.size() + 1); + return (getRealStoreDir() / storePath.to_string()).string(); } std::optional getBuildLogExact(const StorePath & path) override; diff --git a/src/libstore/indirect-root-store.cc b/src/libstore/indirect-root-store.cc index 97035c3f22d..ae56ea311d8 100644 --- a/src/libstore/indirect-root-store.cc +++ b/src/libstore/indirect-root-store.cc @@ -17,7 +17,7 @@ void IndirectRootStore::makeSymlink(const Path & link, const Path & target) Path IndirectRootStore::addPermRoot(const StorePath & storePath, const Path & _gcRoot) { - Path gcRoot(canonPath(_gcRoot)); + Path gcRoot(canonPath(_gcRoot).string()); if (isInStore(gcRoot)) throw Error( diff --git a/src/libstore/local-fs-store.cc b/src/libstore/local-fs-store.cc index b5d276c716e..e37dbc0d8c0 100644 --- a/src/libstore/local-fs-store.cc +++ b/src/libstore/local-fs-store.cc @@ -30,7 +30,8 @@ LocalFSStoreConfig::LocalFSStoreConfig(PathView rootDir, const Params & params) */ , rootDir{makeRootDirSetting( *this, - !rootDir.empty() && params.count("root") == 0 ? std::optional{canonPath(rootDir)} : std::nullopt)} + !rootDir.empty() && params.count("root") == 0 ? std::optional{canonPath(rootDir).string()} + : std::nullopt)} { } diff --git a/src/libstore/local-overlay-store.cc b/src/libstore/local-overlay-store.cc index 0e8655152f1..6242f9e006c 100644 --- a/src/libstore/local-overlay-store.cc +++ b/src/libstore/local-overlay-store.cc @@ -207,7 +207,7 @@ void LocalOverlayStore::collectGarbage(const GCOptions & options, GCResults & re void LocalOverlayStore::deleteStorePath(const Path & path, uint64_t & bytesFreed, bool isKnownPath) { - auto mergedDir = config->realStoreDir.get() + "/"; + auto mergedDir = config->realStoreDir.get().string() + "/"; if (path.substr(0, mergedDir.length()) != mergedDir) { warn("local-overlay: unexpected gc path '%s' ", path); return; @@ -261,7 +261,7 @@ LocalStore::VerificationResult LocalOverlayStore::verifyAllValidPaths(RepairFlag StorePathSet done; auto existsInStoreDir = [&](const StorePath & storePath) { - return pathExists(config->realStoreDir.get() + "/" + storePath.to_string()); + return pathExists((config->realStoreDir.get() / storePath.to_string()).string()); }; bool errors = false; diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 0fd95b43dbc..ada70f3b63e 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -175,7 +175,7 @@ LocalStore::LocalStore(ref config) if (st.st_uid != 0 || st.st_gid != gr->gr_gid || (st.st_mode & ~S_IFMT) != perm) { if (chown(config->realStoreDir.get().c_str(), 0, gr->gr_gid) == -1) - throw SysError("changing ownership of path '%s'", PathFmt(config->realStoreDir.get())); + throw SysError("changing ownership of path %s", PathFmt(config->realStoreDir.get())); chmod(config->realStoreDir.get(), perm); } } @@ -204,7 +204,7 @@ LocalStore::LocalStore(ref config) auto st = maybeStat(reservedPath); if (!st || st->st_size != gcSettings.reservedSize) { AutoCloseFD fd = toDescriptor(open( - reservedPath.c_str(), + reservedPath.string().c_str(), O_WRONLY | O_CREAT #ifndef _WIN32 | O_CLOEXEC @@ -397,7 +397,7 @@ AutoCloseFD LocalStore::openGCLock() { auto fnGCLock = config->stateDir.get() / "gc.lock"; auto fdGCLock = open( - fnGCLock.c_str(), + fnGCLock.string().c_str(), O_RDWR | O_CREAT #ifndef _WIN32 | O_CLOEXEC @@ -405,7 +405,7 @@ AutoCloseFD LocalStore::openGCLock() , 0600); if (!fdGCLock) - throw SysError("opening global GC lock '%1%'", PathFmt(fnGCLock)); + throw SysError("opening global GC lock %1%", PathFmt(fnGCLock)); return toDescriptor(fdGCLock); } @@ -450,7 +450,7 @@ LocalStore::~LocalStore() auto fdTempRoots(_fdTempRoots.lock()); if (*fdTempRoots) { fdTempRoots->close(); - unlink(fnTempRoots.c_str()); + unlink(fnTempRoots.string().c_str()); } } catch (...) { ignoreExceptionInDestructor(); @@ -502,7 +502,7 @@ void LocalStore::openDB(State & state, bool create) throw Error("cannot create database while in read-only mode"); } - if (access(dbDir.c_str(), R_OK | (config->readOnly ? 0 : W_OK))) + if (access(dbDir.string().c_str(), R_OK | (config->readOnly ? 0 : W_OK))) throw SysError("Nix database directory %1% is not writable", PathFmt(dbDir)); /* Open the Nix database. */ diff --git a/src/libstore/optimise-store.cc b/src/libstore/optimise-store.cc index 6895535a4ed..79736cd47b8 100644 --- a/src/libstore/optimise-store.cc +++ b/src/libstore/optimise-store.cc @@ -50,7 +50,7 @@ LocalStore::InodeHash LocalStore::loadInodeHash() debug("loading hash inodes in memory"); InodeHash inodeHash; - AutoCloseDir dir(opendir(linksDir.c_str())); + AutoCloseDir dir(opendir(linksDir.string().c_str())); if (!dir) throw SysError("opening directory %1%", PathFmt(linksDir)); @@ -72,9 +72,9 @@ Strings LocalStore::readDirectoryIgnoringInodes(const std::filesystem::path & pa { Strings names; - AutoCloseDir dir(opendir(path.c_str())); + AutoCloseDir dir(opendir(path.string().c_str())); if (!dir) - throw SysError("opening directory '%s'", PathFmt(path)); + throw SysError("opening directory %s", PathFmt(path)); struct dirent * dirent; while (errno = 0, dirent = readdir(dir.get())) { /* sic */ @@ -91,7 +91,7 @@ Strings LocalStore::readDirectoryIgnoringInodes(const std::filesystem::path & pa names.push_back(name); } if (errno) - throw SysError("reading directory '%s'", PathFmt(path)); + throw SysError("reading directory %s", PathFmt(path)); return names; } @@ -111,7 +111,7 @@ void LocalStore::optimisePath_( https://github.com/NixOS/nix/pull/2230 for more discussion. */ if (std::regex_search(path.string(), std::regex("\\.app/Contents/.+$"))) { - debug("'%s' is not allowed to be linked in macOS", PathFmt(path)); + debug("%s is not allowed to be linked in macOS", PathFmt(path)); return; } #endif @@ -142,7 +142,7 @@ void LocalStore::optimisePath_( /* This can still happen on top-level files. */ if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) { - debug("'%s' is already linked, with %d other file(s)", PathFmt(path), st.st_nlink - 2); + debug("%s is already linked, with %d other file(s)", PathFmt(path), st.st_nlink - 2); return; } @@ -162,7 +162,7 @@ void LocalStore::optimisePath_( HashAlgorithm::SHA256) .hash; }); - debug("'%s' has hash '%s'", PathFmt(path), hash.to_string(HashFormat::Nix32, true)); + debug("%s has hash '%s'", PathFmt(path), hash.to_string(HashFormat::Nix32, true)); /* Check if this is a known hash. */ std::filesystem::path linkPath = std::filesystem::path{linksDir} / hash.to_string(HashFormat::Nix32, false); diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 9f123be7aaa..13f0d61c2c5 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -84,7 +84,7 @@ std::filesystem::path Store::followLinksToStore(std::string_view _path) const } if (!isInStore(path.string())) - throw BadStorePath("path '%1%' is not in the Nix store", PathFmt(path)); + throw BadStorePath("path %1% is not in the Nix store", PathFmt(path)); return path; } diff --git a/src/libstore/store-dir-config.cc b/src/libstore/store-dir-config.cc index d54a1859008..0e835e178c8 100644 --- a/src/libstore/store-dir-config.cc +++ b/src/libstore/store-dir-config.cc @@ -15,7 +15,7 @@ StorePath StoreDirConfig::parseStorePath(std::string_view path) const // Windows <-> Unix ssh-ing). auto p = #ifdef _WIN32 - path + std::filesystem::path(path) #else canonPath(std::string(path)) #endif diff --git a/src/libstore/unix/build/chroot-derivation-builder.cc b/src/libstore/unix/build/chroot-derivation-builder.cc index f48d7fdce4b..c677383ef1e 100644 --- a/src/libstore/unix/build/chroot-derivation-builder.cc +++ b/src/libstore/unix/build/chroot-derivation-builder.cc @@ -130,7 +130,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl for (auto & i : inputPaths) { auto p = store.printStorePath(i); - pathsInChroot.insert_or_assign(p, ChrootPath{.source = store.toRealPath(p)}); + pathsInChroot.insert_or_assign(p, ChrootPath{.source = store.toRealPath(i)}); } /* If we're repairing, checking or rebuilding part of a @@ -159,7 +159,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl { // FIXME: why the needsHashRewrite() conditional? return !needsHashRewrite() ? chrootRootDir / p.relative_path() - : std::filesystem::path(store.toRealPath(p.native())); + : std::filesystem::path(store.toRealPath(store.parseStorePath(p.native()))); } void cleanupBuild(bool force) override diff --git a/src/libstore/unix/build/derivation-builder.cc b/src/libstore/unix/build/derivation-builder.cc index 84d3a10eb10..4a0abe9019e 100644 --- a/src/libstore/unix/build/derivation-builder.cc +++ b/src/libstore/unix/build/derivation-builder.cc @@ -321,7 +321,7 @@ class DerivationBuilderImpl : public DerivationBuilder, public DerivationBuilder virtual std::filesystem::path realPathInHost(const std::filesystem::path & p) { - return store.toRealPath(p.native()); + return store.toRealPath(store.parseStorePath(p.native())); } /** @@ -1831,14 +1831,14 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() auto optFixedPath = output->path(store, drv.name, outputName); if (!optFixedPath || store.printStorePath(*optFixedPath) != finalDestPath) { assert(newInfo.ca); - dynamicOutputLock.lockPaths({store.toRealPath(finalDestPath)}); + dynamicOutputLock.lockPaths({store.toRealPath(newInfo.path)}); } /* Move files, if needed */ - if (store.toRealPath(finalDestPath) != actualPath) { + if (store.toRealPath(newInfo.path) != actualPath) { if (buildMode == bmRepair) { /* Path already exists, need to replace it */ - replaceValidPath(store.toRealPath(finalDestPath), actualPath); + replaceValidPath(store.toRealPath(newInfo.path), actualPath); } else if (buildMode == bmCheck) { /* Path already exists, and we want to compare, so we leave out new path in place. */ @@ -1849,7 +1849,7 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() /* Can delete our scratch copy now. */ deletePath(actualPath); } else { - auto destPath = store.toRealPath(finalDestPath); + auto destPath = store.toRealPath(newInfo.path); deletePath(destPath); movePath(actualPath, destPath); } @@ -1863,7 +1863,7 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() if (newInfo.narHash != oldInfo.narHash) { auto * diffHook = localSettings.getDiffHook(); if (diffHook || settings.keepFailed) { - auto dst = store.toRealPath(finalDestPath + ".check"); + auto dst = store.toRealPath(newInfo.path) + ".check"; deletePath(dst); movePath(actualPath, dst); @@ -1881,13 +1881,13 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() throw NotDeterministic( "derivation '%s' may not be deterministic: output '%s' differs from '%s'", store.printStorePath(drvPath), - store.toRealPath(finalDestPath), + store.toRealPath(newInfo.path), dst); } else throw NotDeterministic( "derivation '%s' may not be deterministic: output '%s' differs", store.printStorePath(drvPath), - store.toRealPath(finalDestPath)); + store.toRealPath(newInfo.path)); } /* Since we verified the build, it's now ultimately trusted. */ @@ -1909,8 +1909,7 @@ SingleDrvOutputs DerivationBuilderImpl::registerOutputs() } if (!store.isValidPath(newInfo.path)) - store.optimisePath( - store.toRealPath(finalDestPath), NoRepair); // FIXME: combine with scanForReferences() + store.optimisePath(store.toRealPath(newInfo.path), NoRepair); // FIXME: combine with scanForReferences() newInfo.deriver = drvPath; newInfo.ultimate = true; diff --git a/src/libutil/executable-path.cc b/src/libutil/executable-path.cc index 7b684249ad8..ec3520fd0ca 100644 --- a/src/libutil/executable-path.cc +++ b/src/libutil/executable-path.cc @@ -55,7 +55,7 @@ void ExecutablePath::parseAppend(const OsString & path) OsString ExecutablePath::render() const { - std::vector path2; + std::vector path2; path2.reserve(directories.size()); for (auto & p : directories) path2.push_back(p.native()); diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 84055acd25f..23cfb7aba6e 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -595,29 +595,25 @@ AutoCloseFD createAnonymousTempFile() std::pair createTempFile(const std::filesystem::path & prefix) { - std::filesystem::path tmpl(defaultTempDir() / (prefix.string() + ".XXXXXX")); - // Strictly speaking, this is UB, but who cares... + assert(!prefix.is_absolute()); + auto tmpl = (defaultTempDir() / (prefix.string() + ".XXXXXX")).string(); // FIXME: use O_TMPFILE. - AutoCloseFD fd = toDescriptor( -#ifdef _WIN32 - _wmkstemp(tmpl.c_str()) -#else - mkstemp(const_cast(tmpl.c_str())) -#endif - ); + // `mkstemp` modifies the string to contain the actual filename. + AutoCloseFD fd = toDescriptor(mkstemp(tmpl.data())); if (!fd) - throw SysError("creating temporary file %s", PathFmt(tmpl)); + throw SysError("creating temporary file '%s'", tmpl); #ifndef _WIN32 unix::closeOnExec(fd.get()); #endif - return {std::move(fd), tmpl}; + return {std::move(fd), std::filesystem::path(std::move(tmpl))}; } 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{}()); + assert(!std::filesystem::path(suffix).is_absolute()); auto tmpRoot = canonPath(root.empty() ? defaultTempDir().string() : root.string(), true); return tmpRoot / fmt("%s-%s-%s", suffix, getpid(), counter.fetch_add(1, std::memory_order_relaxed)); } diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 969c9ec8ee9..79c24197a53 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -182,7 +182,7 @@ std::string PosixSourceAccessor::readLink(const CanonPath & path) { if (auto parent = path.parent()) assertNoSymlinks(*parent); - return nix::readLink(makeAbsPath(path).string()); + return nix::readLink(makeAbsPath(path).string()).string(); } std::optional PosixSourceAccessor::getPhysicalPath(const CanonPath & path) diff --git a/src/libutil/unix-domain-socket.cc b/src/libutil/unix-domain-socket.cc index 5ea1e249e9d..5c8ff944b43 100644 --- a/src/libutil/unix-domain-socket.cc +++ b/src/libutil/unix-domain-socket.cc @@ -36,7 +36,7 @@ AutoCloseFD createUnixDomainSocket(const std::filesystem::path & path, mode_t mo { auto fdSocket = nix::createUnixDomainSocket(); - bind(fdSocket.get(), path); + bind(toSocket(fdSocket.get()), path); chmod(path, mode); @@ -72,7 +72,7 @@ bindConnectProcHelper(std::string_view operationName, auto && operation, Socket try { pipe.readSide.close(); auto dir = std::filesystem::path(path).parent_path(); - if (chdir(dir.c_str()) == -1) + if (chdir(dir.string().c_str()) == -1) throw SysError("chdir to %s failed", PathFmt(dir)); std::string base(baseNameOf(path)); if (base.size() + 1 >= sizeof(addr.sun_path)) @@ -105,7 +105,7 @@ bindConnectProcHelper(std::string_view operationName, auto && operation, Socket void bind(Socket fd, const std::filesystem::path & path) { - unlink(path.c_str()); + unlink(path.string().c_str()); bindConnectProcHelper("bind", ::bind, fd, path.string()); } diff --git a/src/libutil/windows/file-path.cc b/src/libutil/windows/file-path.cc index 10469d9700c..7913b3d5d28 100644 --- a/src/libutil/windows/file-path.cc +++ b/src/libutil/windows/file-path.cc @@ -9,7 +9,7 @@ namespace nix { -static std::optional maybePath(PathView path) +std::optional maybePath(PathView path) { if (path.length() >= 3 && (('A' <= path[0] && path[0] <= 'Z') || ('a' <= path[0] && path[0] <= 'z')) && path[1] == ':' && WindowsPathTrait::isPathSep(path[2])) { diff --git a/src/nix/nix-build/nix-build.cc b/src/nix/nix-build/nix-build.cc index acbf76fa743..d09cb677aa1 100644 --- a/src/nix/nix-build/nix-build.cc +++ b/src/nix/nix-build/nix-build.cc @@ -735,7 +735,7 @@ static void main_nix_build(int argc, char ** argv) std::string symlink = drvPrefix; if (outputName != "out") symlink += "-" + outputName; - store2->addPermRoot(outputPath, absPath(symlink)); + store2->addPermRoot(outputPath, absPath(symlink).string()); } outPaths.push_back(outputPath); diff --git a/src/nix/nix-collect-garbage/nix-collect-garbage.cc b/src/nix/nix-collect-garbage/nix-collect-garbage.cc index 66030c78035..3d84ca13cd9 100644 --- a/src/nix/nix-collect-garbage/nix-collect-garbage.cc +++ b/src/nix/nix-collect-garbage/nix-collect-garbage.cc @@ -38,7 +38,7 @@ void removeOldGenerations(std::filesystem::path dir) if (type == std::filesystem::file_type::symlink && canWrite) { std::string link; try { - link = readLink(path); + link = readLink(path).string(); } catch (SystemError & e) { if (e.is(std::errc::no_such_file_or_directory)) continue; diff --git a/src/nix/run.cc b/src/nix/run.cc index be46319364c..38dbc42b681 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -81,11 +81,15 @@ void execProgramInStore( if (store->storeDir != store2->getRealStoreDir()) { Strings helperArgs = { - chrootHelperName, store->storeDir, store2->getRealStoreDir(), std::string(system.value_or("")), program}; + chrootHelperName, + store->storeDir, + store2->getRealStoreDir().string(), + std::string(system.value_or("")), + program}; for (auto & arg : args) helperArgs.push_back(arg); - execve(getSelfExe().value_or("nix").c_str(), stringsToCharPtrs(helperArgs).data(), envp); + execve(getSelfExe().value_or("nix").string().c_str(), stringsToCharPtrs(helperArgs).data(), envp); throw SysError("could not execute chroot helper"); }