Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Use std::filesystem::path in more places (NixOS#10657)
Browse files Browse the repository at this point in the history
Progress on NixOS#9205

Co-Authored-By: John Ericson <[email protected]>

* Get rid of `PathNG`, just use `std::filesystem::path`
  • Loading branch information
siddhantk232 authored and pull[bot] committed May 9, 2024
1 parent 593f684 commit 40924db
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions src/libstore/ssh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
18 changes: 9 additions & 9 deletions src/libstore/unix/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {};
Expand All @@ -1252,7 +1252,7 @@ StorePath LocalStore::addToStoreFromDump(
methodsMatch
? dumpHash
: hashPath(
{getFSSourceAccessor(), CanonPath(tempPath)},
PosixSourceAccessor::createAtRoot(tempPath),
hashMethod.getFileIngestionMethod(), hashAlgo),
{
.others = references,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)};
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/unix/local-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/libutil/experimental-features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
32 changes: 20 additions & 12 deletions src/libutil/file-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@ 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
* future C++ standard.
*
* @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; }
Expand All @@ -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

}
27 changes: 13 additions & 14 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = "/";

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 (...) {
Expand All @@ -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;
}
Expand Down
21 changes: 14 additions & 7 deletions src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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; }
};


Expand Down
6 changes: 3 additions & 3 deletions src/libutil/unix/file-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/libutil/windows/file-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 40924db

Please sign in to comment.