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 c371070 commit c1c1774
Show file tree
Hide file tree
Showing 56 changed files with 255 additions and 259 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
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
2 changes: 1 addition & 1 deletion 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
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/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 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
2 changes: 1 addition & 1 deletion src/libstore/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ std::optional<StorePath> StoreDirConfig::maybeParseStorePath(std::string_view pa
}
}

bool StoreDirConfig::isStorePath(std::string_view path) const
bool StoreDirConfig::isStorePath(PathView path) const
{
return (bool) maybeParseStorePath(path);
}
Expand Down
1 change: 1 addition & 0 deletions src/libstore/pathlocks.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
///@file

#include "file-descriptor.hh"
#include "file-path.hh"

namespace nix {

Expand Down
20 changes: 11 additions & 9 deletions src/libstore/profiles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path pro

for (auto & i : readDirectory(profileDir)) {
if (auto n = parseName(profileName, i.name)) {
auto path = profileDir + "/" + i.name;
auto path = profileDir / i.name;
gens.push_back({
.number = *n,
.path = path,
Expand Down Expand Up @@ -255,7 +255,7 @@ time_t parseOlderThanTimeSpec(std::string_view timeSpec)
void switchLink(Path link, Path target)
{
/* Hacky. */
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
if (dirOf(target) == dirOf(link)) target = baseNameOf(target).native();

replaceSymlink(target, link);
}
Expand Down Expand Up @@ -310,28 +310,30 @@ Path profilesDir()
auto profileRoot =
isRootUser()
? rootProfilesDir()
: createNixStateDir() + "/profiles";
: createNixStateDir() / "profiles";
createDirs(profileRoot);
return profileRoot;
}

Path rootProfilesDir()
{
return settings.nixStateDir + "/profiles/per-user/root";
return settings.nixStateDir / "profiles/per-user/root";
}


Path getDefaultProfile()
{
Path profileLink = settings.useXDGBaseDirectories ? createNixStateDir() + "/profile" : getHome() + "/.nix-profile";
Path profileLink = settings.useXDGBaseDirectories
? createNixStateDir() / "profile"
: getHome() / ".nix-profile";
try {
auto profile = profilesDir() + "/profile";
auto profile = profilesDir() / "profile";
if (!pathExists(profileLink)) {
replaceSymlink(profile, profileLink);
}
// Backwards compatibiliy measure: Make root's profile available as
// `.../default` as it's what NixOS and most of the init scripts expect
Path globalProfileLink = settings.nixStateDir + "/profiles/default";
Path globalProfileLink = settings.nixStateDir / "profiles/default";
if (isRootUser() && !pathExists(globalProfileLink)) {
replaceSymlink(profile, globalProfileLink);
}
Expand All @@ -345,12 +347,12 @@ Path getDefaultProfile()

Path defaultChannelsDir()
{
return profilesDir() + "/channels";
return profilesDir() / "channels";
}

Path rootChannelsDir()
{
return rootProfilesDir() + "/channels";
return rootProfilesDir() / "channels";
}

}
2 changes: 1 addition & 1 deletion src/libstore/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ SQLite::SQLite(const Path & path, SQLiteOpenMode mode)
bool immutable = mode == SQLiteOpenMode::Immutable;
int flags = immutable ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
if (mode == SQLiteOpenMode::Normal) flags |= SQLITE_OPEN_CREATE;
auto uri = "file:" + percentEncode(path) + "?immutable=" + (immutable ? "1" : "0");
auto uri = "file:" + percentEncode(path.native()) + "?immutable=" + (immutable ? "1" : "0");
int ret = sqlite3_open_v2(uri.c_str(), &db, SQLITE_OPEN_URI | flags, vfs);
if (ret != SQLITE_OK) {
const char * err = sqlite3_errstr(ret);
Expand Down
1 change: 1 addition & 0 deletions src/libstore/sqlite.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

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

struct sqlite3;
struct sqlite3_stmt;
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/ssh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void SSHMaster::addCommonSSHOpts(Strings & args)
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});
args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName.native()});
}
if (compress)
args.push_back("-C");
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ public:
/**
* Follow symlinks until we end up with a path in the Nix store.
*/
Path followLinksToStore(std::string_view path) const;
Path followLinksToStore(PathView path) const;

/**
* Same as followLinksToStore(), but apply toStorePath() to the
* result.
*/
StorePath followLinksToStorePath(std::string_view path) const;
StorePath followLinksToStorePath(PathView path) const;

/**
* Check whether a path is valid.
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/store-dir-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct StoreDirConfig : public Config
* @return true if ‘path’ is a store path, i.e. a direct child of the
* Nix store.
*/
bool isStorePath(std::string_view path) const;
bool isStorePath(PathView path) const;

/**
* Split a path like /nix/store/<hash>-<name>/<bla> into
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/uds-remote-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ UDSRemoteStore::UDSRemoteStore(
std::string UDSRemoteStore::getUri()
{
if (path) {
return std::string("unix://") + *path;
return std::string { "unix://" } + path->native();
} else {
return "daemon";
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/uds-remote-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private:
};

ref<RemoteStore::Connection> openConnection() override;
std::optional<std::string> path;
std::optional<Path> path;
};

}
2 changes: 1 addition & 1 deletion src/libstore/unix/local-overlay-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ R"(

This store type is a variation of the [local store] designed to leverage Linux's [Overlay Filesystem](https://docs.kernel.org/filesystems/overlayfs.html) (OverlayFS for short).
Just as OverlayFS combines a lower and upper filesystem by treating the upper one as a patch against the lower, the local overlay store combines a lower store with an upper almost-[local store].
("almost" because while the upper fileystems for OverlayFS is valid on its own, the upper almost-store is not a valid local store on its own because some references will dangle.)
("almost" because while the upper filesystems for OverlayFS is valid on its own, the upper almost-store is not a valid local store on its own because some references will dangle.)
To use this store, you will first need to configure an OverlayFS mountpoint [appropriately](#example-filesystem-layout) as Nix will not do this for you (though it will verify the mountpoint is configured correctly).

### Conceptual parts of a local overlay store
Expand Down
Loading

0 comments on commit c1c1774

Please sign in to comment.