Skip to content

Commit

Permalink
Make Path std::filesystem::path not std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed May 7, 2024
1 parent fcbc36c commit 2124ad7
Show file tree
Hide file tree
Showing 72 changed files with 419 additions and 348 deletions.
6 changes: 3 additions & 3 deletions src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ static int main_build_remote(int argc, char * * argv)

/* It would be more appropriate to use $XDG_RUNTIME_DIR, since
that gets cleared on reboot, but it wouldn't work on macOS. */
auto currentLoadName = "/current-load";
PathView currentLoadName = "current-load";
if (auto localStore = store.dynamic_pointer_cast<LocalFSStore>())
currentLoad = std::string { localStore->stateDir } + currentLoadName;
currentLoad = localStore->stateDir / currentLoadName;
else
currentLoad = settings.nixStateDir + currentLoadName;
currentLoad = settings.nixStateDir / currentLoadName.native();

std::shared_ptr<Store> sshStore;
AutoCloseFD bestSlotLock;
Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
return res.finish();
}

SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir)
SourcePath lookupFileArg(EvalState & state, PathView s, const Path * baseDir)
{
if (EvalSettings::isPseudoUrl(s)) {
auto accessor = fetchers::downloadTarball(
Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/common-eval-args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ private:
/**
* @param baseDir Optional [base directory](https://nixos.org/manual/nix/unstable/glossary#gloss-base-directory)
*/
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);
SourcePath lookupFileArg(EvalState & state, PathView s, const Path * baseDir = nullptr);

}
1 change: 1 addition & 0 deletions src/libexpr/eval-error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "error.hh"
#include "pos-idx.hh"
#include "file-path.hh"

namespace nix {

Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Strings EvalSettings::getDefaultNixPath()
if (s.empty()) {
res.push_back(p);
} else {
res.push_back(s + "=" + p);
res.push_back(s + "=" + p.native());
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
.program = "git",
.args = {
"-c",
"gpg.ssh.allowedSignersFile=" + allowedSignersFile,
"gpg.ssh.allowedSignersFile=" + allowedSignersFile.string(),
"-C", path.string(),
"verify-commit",
rev.gitRev()
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void BinaryCacheStore::init()

auto cacheInfo = getFile(cacheInfoFile);
if (!cacheInfo) {
upsertFile(cacheInfoFile, "StoreDir: " + storeDir + "\n", "text/x-nix-cache-info");
upsertFile(cacheInfoFile, "StoreDir: " + storeDir.string() + "\n", "text/x-nix-cache-info");
} else {
for (auto & line : tokenizeString<Strings>(*cacheInfo, "\n")) {
size_t colon= line.find(':');
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
auto & localFSStore = require<LocalFSStore>(*store);
localFSStore.addPermRoot(storePath, gcRoot);
logger->stopWork();
to << gcRoot;
to << gcRoot.string();
break;
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ Derivation parseDerivation(
expect(str, ")");
}

expect(str, ","); drv.inputSrcs = store.parseStorePathSet(parseStrings(str, true));
expect(str, ",");
for (auto & i : parseStrings(str, true)) drv.inputSrcs.insert(i);
expect(str, ","); drv.platform = parseString(str).toOwned();
expect(str, ","); drv.builder = parseString(str).toOwned();

Expand Down
7 changes: 6 additions & 1 deletion src/libstore/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ struct BasicDerivation
*/
StorePathSet inputSrcs;
std::string platform;
Path builder;
/**
* This is a path, but we don't want to use `std::filesystem::path`,
* because it might not be for the same OS (or OS with the same type
* of native paths) as (this) Nix is built for.
*/
std::string builder;
Strings args;
StringPairs env;
std::string name;
Expand Down
6 changes: 4 additions & 2 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ Settings::Settings()
}

#if defined(__linux__) && defined(SANDBOX_SHELL)
sandboxPaths = tokenizeString<StringSet>("/bin/sh=" SANDBOX_SHELL);
sandboxPaths = std::set {
Path { "/bin/sh=" SANDBOX_SHELL }
};
#endif

/* chroot-like behavior from Apple's sandbox */
Expand Down Expand Up @@ -154,7 +156,7 @@ std::vector<Path> getUserConfigFiles()
// Use the paths specified in NIX_USER_CONF_FILES if it has been defined
auto nixConfFiles = getEnv("NIX_USER_CONF_FILES");
if (nixConfFiles.has_value()) {
return tokenizeString<std::vector<std::string>>(nixConfFiles.value(), ":");
return tokenizeString<std::vector<Path>>(nixConfFiles.value(), ":");
}

// Use the paths specified by the XDG spec
Expand Down
8 changes: 4 additions & 4 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public:
)"};

Setting<std::string> builders{
this, "@" + nixConfDir + "/machines", "builders",
this, "@" + (nixConfDir / "machines").native(), "builders",
R"(
A semicolon- or newline-separated list of build machines.
Expand Down Expand Up @@ -1036,8 +1036,8 @@ public:
Nix to use for downloads.
)"};

Setting<std::string> netrcFile{
this, fmt("%s/%s", nixConfDir, "netrc"), "netrc-file",
PathSetting netrcFile{
this, nixConfDir/ "netrc", "netrc-file",
R"(
If set to an absolute path to a `netrc` file, Nix will use the HTTP
authentication credentials in this file when trying to download from
Expand All @@ -1061,7 +1061,7 @@ public:
> `.netrc`.
)"};

Setting<Path> caFile{
PathSetting caFile{
this, getDefaultSSLCertFile(), "ssl-cert-file",
R"(
The path of a file containing CA certificates used to
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/http-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HttpBinaryCacheStore : public virtual HttpBinaryCacheStoreConfig, public v
{
private:

Path cacheUri;
std::string cacheUri;

struct State
{
Expand All @@ -40,7 +40,7 @@ class HttpBinaryCacheStore : public virtual HttpBinaryCacheStoreConfig, public v

HttpBinaryCacheStore(
const std::string & scheme,
const Path & _cacheUri,
const std::string & _cacheUri,
const Params & params)
: StoreConfig(params)
, BinaryCacheStoreConfig(params)
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void LegacySSHStore::queryPathInfoUncached(const StorePath & path,

debug("querying remote host '%s' for info on '%s'", host, printStorePath(path));

conn->to << ServeProto::Command::QueryPathInfos << PathSet{printStorePath(path)};
conn->to << ServeProto::Command::QueryPathInfos << StringSet{printStorePath(path)};
conn->to.flush();

auto p = readString(conn->from);
Expand Down
18 changes: 9 additions & 9 deletions src/libstore/local-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public

std::string getUri() override
{
return "file://" + binaryCacheDir;
return "file://" + binaryCacheDir.native();
}

static std::set<std::string> uriSchemes();
Expand All @@ -58,7 +58,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
std::shared_ptr<std::basic_iostream<char>> istream,
const std::string & mimeType) override
{
auto path2 = binaryCacheDir + "/" + path;
auto path2 = binaryCacheDir / path;
static std::atomic<int> counter{0};
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
AutoDelete del(tmp, false);
Expand All @@ -71,7 +71,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
void getFile(const std::string & path, Sink & sink) override
{
try {
readFile(binaryCacheDir + "/" + path, sink);
readFile(binaryCacheDir / path, sink);
} catch (SysError & e) {
if (e.errNo == ENOENT)
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
Expand All @@ -89,7 +89,7 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public
!hasSuffix(name, ".narinfo"))
continue;
paths.insert(parseStorePath(
storeDir + "/" + name.substr(0, name.size() - 8)
storeDir / name.substr(0, name.size() - 8)
+ "-" + MissingName));
}

Expand All @@ -104,17 +104,17 @@ class LocalBinaryCacheStore : public virtual LocalBinaryCacheStoreConfig, public

void LocalBinaryCacheStore::init()
{
createDirs(binaryCacheDir + "/nar");
createDirs(binaryCacheDir + "/" + realisationsPrefix);
createDirs(binaryCacheDir / "nar");
createDirs(binaryCacheDir / realisationsPrefix);
if (writeDebugInfo)
createDirs(binaryCacheDir + "/debuginfo");
createDirs(binaryCacheDir + "/log");
createDirs(binaryCacheDir / "debuginfo");
createDirs(binaryCacheDir / "log");
BinaryCacheStore::init();
}

bool LocalBinaryCacheStore::fileExists(const std::string & path)
{
return pathExists(binaryCacheDir + "/" + path);
return pathExists(binaryCacheDir / path);
}

std::set<std::string> LocalBinaryCacheStore::uriSchemes()
Expand Down
7 changes: 4 additions & 3 deletions src/libstore/local-fs-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ struct LocalStoreAccessor : PosixSourceAccessor
bool requireValidPath;

LocalStoreAccessor(ref<LocalFSStore> store, bool requireValidPath)
: store(store)
: PosixSourceAccessor(store->getRealStoreDir())
, store(store)
, requireValidPath(requireValidPath)
{ }

Expand All @@ -28,7 +29,7 @@ struct LocalStoreAccessor : PosixSourceAccessor
auto [storePath, rest] = store->toStorePath(path.abs());
if (requireValidPath && !store->isValidPath(storePath))
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
return CanonPath(store->getRealStoreDir()) / storePath.to_string() / CanonPath(rest);
return CanonPath(storePath.to_string()) / rest;
}

std::optional<Stat> maybeLstat(const CanonPath & path) override
Expand Down Expand Up @@ -70,7 +71,7 @@ void LocalFSStore::narFromPath(const StorePath & path, Sink & sink)
{
if (!isValidPath(path))
throw Error("path '%s' is not valid", printStorePath(path));
dumpPath(getRealStoreDir() + std::string(printStorePath(path), storeDir.size()), sink);
dumpPath(getRealStoreDir() / path.to_string(), sink);
}

const std::string LocalFSStore::drvsLogDir = "drvs";
Expand Down
8 changes: 4 additions & 4 deletions src/libstore/local-fs-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ struct LocalFSStoreConfig : virtual StoreConfig
"Directory prefixed to all other paths."};

const PathSetting stateDir{this,
rootDir.get() ? *rootDir.get() + "/nix/var/nix" : settings.nixStateDir,
rootDir.get() ? *rootDir.get() / "nix/var/nix" : settings.nixStateDir,
"state",
"Directory where Nix will store state."};

const PathSetting logDir{this,
rootDir.get() ? *rootDir.get() + "/nix/var/log/nix" : settings.nixLogDir,
rootDir.get() ? *rootDir.get() / "nix/var/log/nix" : settings.nixLogDir,
"log",
"directory where Nix will store log files."};

const PathSetting realStoreDir{this,
rootDir.get() ? *rootDir.get() + "/nix/store" : storeDir, "real",
rootDir.get() ? *rootDir.get() / "nix/store" : storeDir, "real",
"Physical path of the Nix store."};
};

Expand Down Expand Up @@ -66,7 +66,7 @@ public:
Path toRealPath(const Path & storePath) override
{
assert(isInStore(storePath));
return getRealStoreDir() + "/" + std::string(storePath, storeDir.size() + 1);
return getRealStoreDir() / Path::string_type { storePath, storeDir.native().size() + 1 };
}

std::optional<std::string> getBuildLogExact(const StorePath & path) override;
Expand Down
18 changes: 11 additions & 7 deletions src/libstore/nar-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct NarMember

std::string target;

/* If this is a directory, all the children of the directory. */
/**
* If this is a directory, all the children of the directory.
*/
std::map<std::string, NarMember> children;
};

Expand Down Expand Up @@ -71,9 +73,11 @@ struct NarAccessor : public SourceAccessor
: acc(acc), source(source)
{ }

NarMember & createMember(const Path & path, NarMember member)
NarMember & createMember(const CanonPath & path, NarMember member)
{
size_t level = std::count(path.begin(), path.end(), '/');
size_t level = 0;
for (auto _ : path) ++level;

while (parents.size() > level) parents.pop();

if (parents.empty()) {
Expand All @@ -83,14 +87,14 @@ struct NarAccessor : public SourceAccessor
} else {
if (parents.top()->stat.type != Type::tDirectory)
throw Error("NAR file missing parent directory of path '%s'", path);
auto result = parents.top()->children.emplace(baseNameOf(path), std::move(member));
auto result = parents.top()->children.emplace(*path.baseName(), std::move(member));
auto & ref = result.first->second;
parents.push(&ref);
return ref;
}
}

void createDirectory(const Path & path) override
void createDirectory(const CanonPath & path) override
{
createMember(path, NarMember{ .stat = {
.type = Type::tDirectory,
Expand All @@ -100,7 +104,7 @@ struct NarAccessor : public SourceAccessor
} });
}

void createRegularFile(const Path & path, std::function<void(CreateRegularFileSink &)> func) override
void createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func) override
{
auto & nm = createMember(path, NarMember{ .stat = {
.type = Type::tRegular,
Expand All @@ -112,7 +116,7 @@ struct NarAccessor : public SourceAccessor
func(nmc);
}

void createSymlink(const Path & path, const std::string & target) override
void createSymlink(const CanonPath & path, const std::string & target) override
{
createMember(path,
NarMember{
Expand Down
5 changes: 4 additions & 1 deletion src/libstore/nar-info-disk-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
};

{
auto r(state->insertCache.use()(uri)(time(0))(storeDir)(wantMassQuery)(priority));
auto r {
state->insertCache.use()
(uri)(time(0))(storeDir.native())(wantMassQuery)(priority)
};
if (!r.next()) { abort(); }
ret.id = (int) r.getInt(0);
}
Expand Down
6 changes: 5 additions & 1 deletion src/libstore/path-info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ std::string ValidPathInfo::fingerprint(const Store & store) const
if (narSize == 0)
throw Error("cannot calculate fingerprint of path '%s' because its size is not known",
store.printStorePath(path));

StringSet refs;
for (auto & i : references) refs.insert(store.printStorePath(i));

return
"1;" + store.printStorePath(path) + ";"
+ narHash.to_string(HashFormat::Nix32, true) + ";"
+ std::to_string(narSize) + ";"
+ concatStringsSep(",", store.printStorePathSet(references));
+ concatStringsSep(",", refs);
}


Expand Down
2 changes: 1 addition & 1 deletion src/libstore/path-with-outputs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ StorePathWithOutputs parsePathWithOutputs(const StoreDirConfig & store, std::str
StorePathWithOutputs followLinksToStorePathWithOutputs(const Store & store, std::string_view pathWithOutputs)
{
auto [path, outputs] = parsePathWithOutputs(pathWithOutputs);
return StorePathWithOutputs { store.followLinksToStorePath(path), std::move(outputs) };
return StorePathWithOutputs { store.followLinksToStorePath(PathView{path}), std::move(outputs) };
}

}
Loading

0 comments on commit 2124ad7

Please sign in to comment.