Use std::filesystem::path in more places (#10657)

Progress on #9205

Co-Authored-By: John Ericson <John.Ericson@Obsidian.Systems>

* Get rid of `PathNG`, just use `std::filesystem::path`
This commit is contained in:
Siddhant Kumar 2024-05-08 03:58:50 +05:30 committed by GitHub
parent 9ae6455b0e
commit fcbc36cf78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 104 additions and 82 deletions

View file

@ -108,7 +108,7 @@ struct MountedSSHStoreConfig : virtual SSHStoreConfig, virtual LocalFSStoreConfi
{
}
const std::string name() override { return "Experimental SSH Store with filesytem mounted"; }
const std::string name() override { return "Experimental SSH Store with filesystem mounted"; }
std::string doc() override
{

View file

@ -31,11 +31,11 @@ void SSHMaster::addCommonSSHOpts(Strings & args)
if (!keyFile.empty())
args.insert(args.end(), {"-i", keyFile});
if (!sshPublicHostKey.empty()) {
Path fileName = (Path) *state->tmpDir + "/host-key";
std::filesystem::path fileName = state->tmpDir->path() / "host-key";
auto p = host.rfind("@");
std::string thost = p != std::string::npos ? std::string(host, p + 1) : host;
writeFile(fileName, thost + " " + base64Decode(sshPublicHostKey) + "\n");
args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName});
writeFile(fileName.string(), thost + " " + base64Decode(sshPublicHostKey) + "\n");
args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName.string()});
}
if (compress)
args.push_back("-C");

View file

@ -1220,8 +1220,8 @@ StorePath LocalStore::addToStoreFromDump(
}
std::unique_ptr<AutoDelete> delTempDir;
Path tempPath;
Path tempDir;
std::filesystem::path tempPath;
std::filesystem::path tempDir;
AutoCloseFD tempDirFd;
bool methodsMatch = ContentAddressMethod(FileIngestionMethod(dumpMethod)) == hashMethod;
@ -1237,9 +1237,9 @@ StorePath LocalStore::addToStoreFromDump(
std::tie(tempDir, tempDirFd) = createTempDirInStore();
delTempDir = std::make_unique<AutoDelete>(tempDir);
tempPath = tempDir + "/x";
tempPath = tempDir / "x";
restorePath(tempPath, bothSource, dumpMethod);
restorePath(tempPath.string(), bothSource, dumpMethod);
dumpBuffer.reset();
dump = {};
@ -1252,7 +1252,7 @@ StorePath LocalStore::addToStoreFromDump(
methodsMatch
? dumpHash
: hashPath(
{getFSSourceAccessor(), CanonPath(tempPath)},
PosixSourceAccessor::createAtRoot(tempPath),
hashMethod.getFileIngestionMethod(), hashAlgo),
{
.others = references,
@ -1295,7 +1295,7 @@ StorePath LocalStore::addToStoreFromDump(
}
} else {
/* Move the temporary path we restored above. */
moveFile(tempPath, realPath);
moveFile(tempPath.string(), realPath);
}
/* For computing the nar hash. In recursive SHA-256 mode, this
@ -1330,9 +1330,9 @@ StorePath LocalStore::addToStoreFromDump(
/* Create a temporary directory in the store that won't be
garbage-collected until the returned FD is closed. */
std::pair<Path, AutoCloseFD> LocalStore::createTempDirInStore()
std::pair<std::filesystem::path, AutoCloseFD> LocalStore::createTempDirInStore()
{
Path tmpDirFn;
std::filesystem::path tmpDirFn;
AutoCloseFD tmpDirFd;
bool lockedByUs = false;
do {
@ -1345,7 +1345,7 @@ std::pair<Path, AutoCloseFD> LocalStore::createTempDirInStore()
continue;
}
lockedByUs = lockFile(tmpDirFd.get(), ltWrite, true);
} while (!pathExists(tmpDirFn) || !lockedByUs);
} while (!pathExists(tmpDirFn.string()) || !lockedByUs);
return {tmpDirFn, std::move(tmpDirFd)};
}

View file

@ -377,7 +377,7 @@ private:
void findRuntimeRoots(Roots & roots, bool censor);
std::pair<Path, AutoCloseFD> createTempDirInStore();
std::pair<std::filesystem::path, AutoCloseFD> createTempDirInStore();
typedef std::unordered_set<ino_t> InodeHash;

View file

@ -282,7 +282,7 @@ constexpr std::array<ExperimentalFeatureDetails, numXpFeatures> xpFeatureDetails
.tag = Xp::MountedSSHStore,
.name = "mounted-ssh-store",
.description = R"(
Allow the use of the [`mounted SSH store`](@docroot@/command-ref/new-cli/nix3-help-stores.html#experimental-ssh-store-with-filesytem-mounted).
Allow the use of the [`mounted SSH store`](@docroot@/command-ref/new-cli/nix3-help-stores.html#experimental-ssh-store-with-filesystem-mounted).
)",
.trackingUrl = "https://github.com/NixOS/nix/milestone/43",
},

View file

@ -13,9 +13,8 @@ namespace nix {
*
* @todo drop `NG` suffix and replace the ones in `types.hh`.
*/
typedef std::filesystem::path PathNG;
typedef std::list<Path> PathsNG;
typedef std::set<Path> PathSetNG;
typedef std::list<std::filesystem::path> PathsNG;
typedef std::set<std::filesystem::path> PathSetNG;
/**
* Stop gap until `std::filesystem::path_view` from P1030R6 exists in a
@ -23,18 +22,18 @@ typedef std::set<Path> PathSetNG;
*
* @todo drop `NG` suffix and replace the one in `types.hh`.
*/
struct PathViewNG : std::basic_string_view<PathNG::value_type>
struct PathViewNG : std::basic_string_view<std::filesystem::path::value_type>
{
using string_view = std::basic_string_view<PathNG::value_type>;
using string_view = std::basic_string_view<std::filesystem::path::value_type>;
using string_view::string_view;
PathViewNG(const PathNG & path)
: std::basic_string_view<PathNG::value_type>(path.native())
PathViewNG(const std::filesystem::path & path)
: std::basic_string_view<std::filesystem::path::value_type>(path.native())
{ }
PathViewNG(const PathNG::string_type & path)
: std::basic_string_view<PathNG::value_type>(path)
PathViewNG(const std::filesystem::path::string_type & path)
: std::basic_string_view<std::filesystem::path::value_type>(path)
{ }
const string_view & native() const { return *this; }
@ -43,10 +42,19 @@ struct PathViewNG : std::basic_string_view<PathNG::value_type>
std::string os_string_to_string(PathViewNG::string_view path);
PathNG::string_type string_to_os_string(std::string_view s);
std::filesystem::path::string_type string_to_os_string(std::string_view s);
std::optional<PathNG> maybePathNG(PathView path);
std::optional<std::filesystem::path> maybePath(PathView path);
PathNG pathNG(PathView path);
std::filesystem::path pathNG(PathView path);
/**
* Create string literals with the native character width of paths
*/
#ifndef _WIN32
# define PATHNG_LITERAL(s) s
#else
# define PATHNG_LITERAL(s) L ## s
#endif
}

View file

@ -228,9 +228,9 @@ bool isLink(const Path & path)
}
std::vector<std::filesystem::directory_entry> readDirectory(const Path & path)
std::vector<fs::directory_entry> readDirectory(const Path & path)
{
std::vector<std::filesystem::directory_entry> entries;
std::vector<fs::directory_entry> entries;
entries.reserve(64);
for (auto & entry : fs::directory_iterator{path}) {
@ -342,12 +342,12 @@ void syncParent(const Path & path)
}
static void _deletePath(Descriptor parentfd, const Path & path, uint64_t & bytesFreed)
static void _deletePath(Descriptor parentfd, const fs::path & path, uint64_t & bytesFreed)
{
#ifndef _WIN32
checkInterrupt();
std::string name(baseNameOf(path));
std::string name(baseNameOf(path.native()));
struct stat st;
if (fstatat(parentfd, name.c_str(), &st,
@ -416,9 +416,9 @@ static void _deletePath(Descriptor parentfd, const Path & path, uint64_t & bytes
#endif
}
static void _deletePath(const Path & path, uint64_t & bytesFreed)
static void _deletePath(const fs::path & path, uint64_t & bytesFreed)
{
Path dir = dirOf(path);
Path dir = dirOf(path.string());
if (dir == "")
dir = "/";
@ -432,7 +432,7 @@ static void _deletePath(const Path & path, uint64_t & bytesFreed)
}
void deletePath(const Path & path)
void deletePath(const fs::path & path)
{
uint64_t dummy;
deletePath(path, dummy);
@ -466,7 +466,7 @@ Paths createDirs(const Path & path)
}
void deletePath(const Path & path, uint64_t & bytesFreed)
void deletePath(const fs::path & path, uint64_t & bytesFreed)
{
//Activity act(*logger, lvlDebug, "recursively deleting path '%1%'", path);
bytesFreed = 0;
@ -478,7 +478,7 @@ void deletePath(const Path & path, uint64_t & bytesFreed)
AutoDelete::AutoDelete() : del{false} {}
AutoDelete::AutoDelete(const std::string & p, bool recursive) : path(p)
AutoDelete::AutoDelete(const fs::path & p, bool recursive) : _path(p)
{
del = true;
this->recursive = recursive;
@ -489,10 +489,9 @@ AutoDelete::~AutoDelete()
try {
if (del) {
if (recursive)
deletePath(path);
deletePath(_path);
else {
if (remove(path.c_str()) == -1)
throw SysError("cannot unlink '%1%'", path);
fs::remove(_path);
}
}
} catch (...) {
@ -505,8 +504,8 @@ void AutoDelete::cancel()
del = false;
}
void AutoDelete::reset(const Path & p, bool recursive) {
path = p;
void AutoDelete::reset(const fs::path & p, bool recursive) {
_path = p;
this->recursive = recursive;
del = true;
}

View file

@ -9,6 +9,7 @@
#include "error.hh"
#include "logging.hh"
#include "file-descriptor.hh"
#include "file-path.hh"
#include <sys/types.h>
#include <sys/stat.h>
@ -149,9 +150,9 @@ void syncParent(const Path & path);
* recursively. It's not an error if the path does not exist. The
* second variant returns the number of bytes and blocks freed.
*/
void deletePath(const Path & path);
void deletePath(const std::filesystem::path & path);
void deletePath(const Path & path, uint64_t & bytesFreed);
void deletePath(const std::filesystem::path & path, uint64_t & bytesFreed);
/**
* Create a directory and all its parents, if necessary. Returns the
@ -197,17 +198,23 @@ void copyFile(const Path & oldPath, const Path & newPath, bool andDelete);
*/
class AutoDelete
{
Path path;
std::filesystem::path _path;
bool del;
bool recursive;
public:
AutoDelete();
AutoDelete(const Path & p, bool recursive = true);
AutoDelete(const std::filesystem::path & p, bool recursive = true);
~AutoDelete();
void cancel();
void reset(const Path & p, bool recursive = true);
operator Path() const { return path; }
operator PathView() const { return path; }
void reset(const std::filesystem::path & p, bool recursive = true);
const std::filesystem::path & path() const { return _path; }
PathViewNG view() const { return _path; }
operator const std::filesystem::path & () const { return _path; }
operator PathViewNG () const { return _path; }
};

View file

@ -13,17 +13,17 @@ std::string os_string_to_string(PathViewNG::string_view path)
return std::string { path };
}
PathNG::string_type string_to_os_string(std::string_view s)
std::filesystem::path::string_type string_to_os_string(std::string_view s)
{
return std::string { s };
}
std::optional<PathNG> maybePathNG(PathView path)
std::optional<std::filesystem::path> maybePath(PathView path)
{
return { path };
}
PathNG pathNG(PathView path)
std::filesystem::path pathNG(PathView path)
{
return path;
}

View file

@ -12,35 +12,35 @@ namespace nix {
std::string os_string_to_string(PathViewNG::string_view path)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(PathNG::string_type { path });
return converter.to_bytes(std::filesystem::path::string_type { path });
}
PathNG::string_type string_to_os_string(std::string_view s)
std::filesystem::path::string_type string_to_os_string(std::string_view s)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(std::string { s });
}
std::optional<PathNG> maybePathNG(PathView path)
std::optional<std::filesystem::path> maybePath(PathView path)
{
if (path.length() >= 3 && (('A' <= path[0] && path[0] <= 'Z') || ('a' <= path[0] && path[0] <= 'z')) && path[1] == ':' && WindowsPathTrait<char>::isPathSep(path[2])) {
PathNG::string_type sw = string_to_os_string(
std::filesystem::path::string_type sw = string_to_os_string(
std::string { "\\\\?\\" } + path);
std::replace(sw.begin(), sw.end(), '/', '\\');
return sw;
}
if (path.length() >= 7 && path[0] == '\\' && path[1] == '\\' && (path[2] == '.' || path[2] == '?') && path[3] == '\\' &&
('A' <= path[4] && path[4] <= 'Z') && path[5] == ':' && WindowsPathTrait<char>::isPathSep(path[6])) {
PathNG::string_type sw = string_to_os_string(path);
std::filesystem::path::string_type sw = string_to_os_string(path);
std::replace(sw.begin(), sw.end(), '/', '\\');
return sw;
}
return std::optional<PathNG::string_type>();
return std::optional<std::filesystem::path::string_type>();
}
PathNG pathNG(PathView path)
std::filesystem::path pathNG(PathView path)
{
std::optional<PathNG::string_type> sw = maybePathNG(path);
std::optional<std::filesystem::path::string_type> sw = maybePath(path);
if (!sw) {
// FIXME why are we not using the regular error handling?
std::cerr << "invalid path for WinAPI call ["<<path<<"]"<<std::endl;

View file

@ -171,7 +171,7 @@ static void main_nix_build(int argc, char * * argv)
; // obsolete
else if (*arg == "--no-out-link" || *arg == "--no-link")
outLink = (Path) tmpDir + "/result";
outLink = (tmpDir.path() / "result").string();
else if (*arg == "--attr" || *arg == "-A")
attrPaths.push_back(getArg(*arg, arg, end));
@ -504,7 +504,7 @@ static void main_nix_build(int argc, char * * argv)
if (passAsFile.count(var.first)) {
keepTmp = true;
auto fn = ".attr-" + std::to_string(fileNr++);
Path p = (Path) tmpDir + "/" + fn;
Path p = (tmpDir.path() / fn).string();
writeFile(p, var.second);
env[var.first + "Path"] = p;
} else
@ -536,10 +536,10 @@ static void main_nix_build(int argc, char * * argv)
auto json = structAttrs.value();
structuredAttrsRC = writeStructuredAttrsShell(json);
auto attrsJSON = (Path) tmpDir + "/.attrs.json";
auto attrsJSON = (tmpDir.path() / ".attrs.json").string();
writeFile(attrsJSON, json.dump());
auto attrsSH = (Path) tmpDir + "/.attrs.sh";
auto attrsSH = (tmpDir.path() / ".attrs.sh").string();
writeFile(attrsSH, structuredAttrsRC);
env["NIX_ATTRS_SH_FILE"] = attrsSH;
@ -552,7 +552,7 @@ static void main_nix_build(int argc, char * * argv)
convenience, source $stdenv/setup to setup additional
environment variables and shell functions. Also don't
lose the current $PATH directories. */
auto rcfile = (Path) tmpDir + "/rc";
auto rcfile = (tmpDir.path() / "rc").string();
std::string rc = fmt(
R"(_nix_shell_clean_tmpdir() { command rm -rf %1%; }; )"s +
(keepTmp ?
@ -583,7 +583,7 @@ static void main_nix_build(int argc, char * * argv)
"unset TZ; %6%"
"shopt -s execfail;"
"%7%",
shellEscape(tmpDir),
shellEscape(tmpDir.path().string()),
(pure ? "" : "p=$PATH; "),
(pure ? "" : "PATH=$PATH:$p; unset p; "),
shellEscape(dirOf(*shell)),

View file

@ -336,8 +336,8 @@ struct Common : InstallableCommand, MixProfile
std::string makeRcScript(
ref<Store> store,
const BuildEnvironment & buildEnvironment,
const Path & tmpDir,
const Path & outputsDir = absPath(".") + "/outputs")
const std::filesystem::path & tmpDir,
const std::filesystem::path & outputsDir = std::filesystem::path { absPath(".") } / "outputs")
{
// A list of colon-separated environment variables that should be
// prepended to, rather than overwritten, in order to keep the shell usable.
@ -376,13 +376,19 @@ struct Common : InstallableCommand, MixProfile
StringMap rewrites;
if (buildEnvironment.providesStructuredAttrs()) {
for (auto & [outputName, from] : BuildEnvironment::getAssociative(outputs->second)) {
rewrites.insert({from, outputsDir + "/" + outputName});
rewrites.insert({
from,
(outputsDir / outputName).string()
});
}
} else {
for (auto & outputName : BuildEnvironment::getStrings(outputs->second)) {
auto from = buildEnvironment.vars.find(outputName);
assert(from != buildEnvironment.vars.end());
rewrites.insert({BuildEnvironment::getString(from->second), outputsDir + "/" + outputName});
rewrites.insert({
BuildEnvironment::getString(from->second),
(outputsDir / outputName).string(),
});
}
}
@ -405,7 +411,7 @@ struct Common : InstallableCommand, MixProfile
if (buildEnvironment.providesStructuredAttrs()) {
fixupStructuredAttrs(
"sh",
PATHNG_LITERAL("sh"),
"NIX_ATTRS_SH_FILE",
buildEnvironment.getAttrsSH(),
rewrites,
@ -413,7 +419,7 @@ struct Common : InstallableCommand, MixProfile
tmpDir
);
fixupStructuredAttrs(
"json",
PATHNG_LITERAL("json"),
"NIX_ATTRS_JSON_FILE",
buildEnvironment.getAttrsJSON(),
rewrites,
@ -430,19 +436,21 @@ struct Common : InstallableCommand, MixProfile
* that's accessible from the interactive shell session.
*/
void fixupStructuredAttrs(
const std::string & ext,
PathViewNG::string_view ext,
const std::string & envVar,
const std::string & content,
StringMap & rewrites,
const BuildEnvironment & buildEnvironment,
const Path & tmpDir)
const std::filesystem::path & tmpDir)
{
auto targetFilePath = tmpDir + "/.attrs." + ext;
writeFile(targetFilePath, content);
auto targetFilePath = tmpDir / PATHNG_LITERAL(".attrs.");
targetFilePath += ext;
writeFile(targetFilePath.string(), content);
auto fileInBuilderEnv = buildEnvironment.vars.find(envVar);
assert(fileInBuilderEnv != buildEnvironment.vars.end());
rewrites.insert({BuildEnvironment::getString(fileInBuilderEnv->second), targetFilePath});
rewrites.insert({BuildEnvironment::getString(fileInBuilderEnv->second), targetFilePath.string()});
}
Strings getDefaultFlakeAttrPaths() override
@ -578,7 +586,7 @@ struct CmdDevelop : Common, MixEnvironment
AutoDelete tmpDir(createTempDir("", "nix-develop"), true);
auto script = makeRcScript(store, buildEnvironment, (Path) tmpDir);
auto script = makeRcScript(store, buildEnvironment, tmpDir);
if (verbosity >= lvlDebug)
script += "set -x\n";

View file

@ -87,7 +87,7 @@ std::tuple<StorePath, Hash> prefetchFile(
if (!storePath) {
AutoDelete tmpDir(createTempDir(), true);
Path tmpFile = (Path) tmpDir + "/tmp";
std::filesystem::path tmpFile = tmpDir.path() / "tmp";
/* Download the file. */
{
@ -95,7 +95,7 @@ std::tuple<StorePath, Hash> prefetchFile(
if (executable)
mode = 0700;
AutoCloseFD fd = toDescriptor(open(tmpFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, mode));
AutoCloseFD fd = toDescriptor(open(tmpFile.string().c_str(), O_WRONLY | O_CREAT | O_EXCL, mode));
if (!fd) throw SysError("creating temporary file '%s'", tmpFile);
FdSink sink(fd.get());
@ -109,15 +109,15 @@ std::tuple<StorePath, Hash> prefetchFile(
if (unpack) {
Activity act(*logger, lvlChatty, actUnknown,
fmt("unpacking '%s'", url));
Path unpacked = (Path) tmpDir + "/unpacked";
auto unpacked = (tmpDir.path() / "unpacked").string();
createDirs(unpacked);
unpackTarfile(tmpFile, unpacked);
unpackTarfile(tmpFile.string(), unpacked);
/* If the archive unpacks to a single file/directory, then use
that as the top-level. */
auto entries = readDirectory(unpacked);
if (entries.size() == 1)
tmpFile = entries[0].path().string();
tmpFile = entries[0].path();
else
tmpFile = unpacked;
}