1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 20:41:47 +02:00

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

@ -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;