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 6, 2024
1 parent b495040 commit d93d9cd
Show file tree
Hide file tree
Showing 39 changed files with 153 additions and 105 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);

}
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
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
4 changes: 3 additions & 1 deletion 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
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
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 @@ -343,12 +345,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;
};

}
6 changes: 3 additions & 3 deletions src/libstore/unix/lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct SimpleUserLock : UserLock
static std::unique_ptr<UserLock> acquire()
{
assert(settings.buildUsersGroup != "");
createDirs(settings.nixStateDir + "/userpool");
createDirs(settings.nixStateDir / "userpool");

/* Get the members of the build-users-group. */
struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());
Expand Down Expand Up @@ -139,14 +139,14 @@ struct AutoUserLock : UserLock
assert((uint64_t) settings.startId + (uint64_t) settings.uidCount <= std::numeric_limits<uid_t>::max());
assert(nrIds <= maxIdsPerBuild);

createDirs(settings.nixStateDir + "/userpool2");
createDirs(settings.nixStateDir / "userpool2");

size_t nrSlots = settings.uidCount / maxIdsPerBuild;

for (size_t i = 0; i < nrSlots; i++) {
debug("trying user slot '%d'", i);

createDirs(settings.nixStateDir + "/userpool2");
createDirs(settings.nixStateDir / "userpool2");

auto fnUserLock = fmt("%s/userpool2/slot-%d", settings.nixStateDir, i);

Expand Down
4 changes: 2 additions & 2 deletions src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static ArchiveSettings archiveSettings;

static GlobalConfig::Register rArchiveSettings(&archiveSettings);

PathFilter defaultPathFilter = [](const Path &) { return true; };
PathFilter defaultPathFilter = [](PathView) { return true; };


void SourceAccessor::dumpPath(
Expand Down Expand Up @@ -97,7 +97,7 @@ void SourceAccessor::dumpPath(
}

else if (st.type == tSymlink)
sink << "type" << "symlink" << "target" << readLink(path);
sink << "type" << "symlink" << "target" << readLink(path).string();

else throw Error("file '%s' has an unsupported type", path);

Expand Down
1 change: 0 additions & 1 deletion src/libutil/archive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace nix {


/**
* dumpPath creates a Nix archive of the specified path.
*
Expand Down
17 changes: 17 additions & 0 deletions src/libutil/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ protected:
, arity(1)
{ }

Handler(Path * dest)
: fun([=](std::vector<std::string> ss) { *dest = ss[0]; })
, arity(1)
{ }

template<class T>
Handler(T * dest, const T & val)
: fun([dest, val](std::vector<std::string> ss) { *dest = val; })
Expand Down Expand Up @@ -283,6 +288,18 @@ public:
});
}

/**
* Expect a path argument.
*/
void expectArg(const std::string & label, Path * dest, bool optional = false)
{
expectArgs({
.label = label,
.optional = optional,
.handler = {dest}
});
}

/**
* Expect 0 or more arguments.
*/
Expand Down
Loading

0 comments on commit d93d9cd

Please sign in to comment.