1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 05:01:48 +02:00

Use SourcePath in more places

Now that SourcePath uses a SourceAccessor instead of an InputAccessor,
we can use it in function signatures instead of passing a
SourceAccessor and CanonPath separately.
This commit is contained in:
Eelco Dolstra 2024-05-06 19:05:42 +02:00
parent 2926ef0e90
commit eab2919119
25 changed files with 101 additions and 109 deletions

View file

@ -8,6 +8,7 @@
#include "archive.hh"
#include "config.hh"
#include "posix-source-accessor.hh"
#include "source-path.hh"
#include "file-system.hh"
#include "signals.hh"
@ -110,9 +111,9 @@ void SourceAccessor::dumpPath(
time_t dumpPathAndGetMtime(const Path & path, Sink & sink, PathFilter & filter)
{
auto [accessor, canonPath] = PosixSourceAccessor::createAtRoot(path);
accessor.dumpPath(canonPath, sink, filter);
return accessor.mtime;
auto path2 = PosixSourceAccessor::createAtRoot(path);
path2.dumpPath(sink, filter);
return path2.accessor.dynamic_pointer_cast<PosixSourceAccessor>()->mtime;
}
void dumpPath(const Path & path, Sink & sink, PathFilter & filter)

View file

@ -1,6 +1,7 @@
#include "file-content-address.hh"
#include "archive.hh"
#include "git.hh"
#include "source-path.hh"
namespace nix {
@ -68,17 +69,17 @@ std::string_view renderFileIngestionMethod(FileIngestionMethod method)
void dumpPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
Sink & sink,
FileSerialisationMethod method,
PathFilter & filter)
{
switch (method) {
case FileSerialisationMethod::Flat:
accessor.readFile(path, sink);
path.readFile(sink);
break;
case FileSerialisationMethod::Recursive:
accessor.dumpPath(path, sink, filter);
path.dumpPath(sink, filter);
break;
}
}
@ -101,27 +102,27 @@ void restorePath(
HashResult hashPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
FileSerialisationMethod method, HashAlgorithm ha,
PathFilter & filter)
{
HashSink sink { ha };
dumpPath(accessor, path, sink, method, filter);
dumpPath(path, sink, method, filter);
return sink.finish();
}
Hash hashPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
FileIngestionMethod method, HashAlgorithm ht,
PathFilter & filter)
{
switch (method) {
case FileIngestionMethod::Flat:
case FileIngestionMethod::Recursive:
return hashPath(accessor, path, (FileSerialisationMethod) method, ht, filter).first;
return hashPath(path, (FileSerialisationMethod) method, ht, filter).first;
case FileIngestionMethod::Git:
return git::dumpHash(ht, accessor, path, filter).hash;
return git::dumpHash(ht, path, filter).hash;
}
assert(false);
}

View file

@ -7,6 +7,8 @@
namespace nix {
struct SourcePath;
/**
* An enumeration of the ways we can serialize file system
* objects.
@ -45,7 +47,7 @@ std::string_view renderFileSerialisationMethod(FileSerialisationMethod method);
* Dump a serialization of the given file system object.
*/
void dumpPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
Sink & sink,
FileSerialisationMethod method,
PathFilter & filter = defaultPathFilter);
@ -72,7 +74,7 @@ void restorePath(
* ```
*/
HashResult hashPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
FileSerialisationMethod method, HashAlgorithm ha,
PathFilter & filter = defaultPathFilter);
@ -138,7 +140,7 @@ std::string_view renderFileIngestionMethod(FileIngestionMethod method);
* useful defined for a merkle format.
*/
Hash hashPath(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
FileIngestionMethod method, HashAlgorithm ha,
PathFilter & filter = defaultPathFilter);

View file

@ -8,7 +8,6 @@
#include "signals.hh"
#include "config.hh"
#include "hash.hh"
#include "posix-source-accessor.hh"
#include "git.hh"
#include "serialise.hh"
@ -269,18 +268,18 @@ void dumpTree(const Tree & entries, Sink & sink,
Mode dump(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
Sink & sink,
std::function<DumpHook> hook,
PathFilter & filter,
const ExperimentalFeatureSettings & xpSettings)
{
auto st = accessor.lstat(path);
auto st = path.lstat();
switch (st.type) {
case SourceAccessor::tRegular:
{
accessor.readFile(path, sink, [&](uint64_t size) {
path.readFile(sink, [&](uint64_t size) {
dumpBlobPrefix(size, sink, xpSettings);
});
return st.isExecutable
@ -291,9 +290,9 @@ Mode dump(
case SourceAccessor::tDirectory:
{
Tree entries;
for (auto & [name, _] : accessor.readDirectory(path)) {
for (auto & [name, _] : path.readDirectory()) {
auto child = path / name;
if (!filter(child.abs())) continue;
if (!filter(child.path.abs())) continue;
auto entry = hook(child);
@ -309,7 +308,7 @@ Mode dump(
case SourceAccessor::tSymlink:
{
auto target = accessor.readLink(path);
auto target = path.readLink();
dumpBlobPrefix(target.size(), sink, xpSettings);
sink(target);
return Mode::Symlink;
@ -323,13 +322,14 @@ Mode dump(
TreeEntry dumpHash(
HashAlgorithm ha,
SourceAccessor & accessor, const CanonPath & path, PathFilter & filter)
HashAlgorithm ha,
const SourcePath & path,
PathFilter & filter)
{
std::function<DumpHook> hook;
hook = [&](const CanonPath & path) -> TreeEntry {
hook = [&](const SourcePath & path) -> TreeEntry {
auto hashSink = HashSink(ha);
auto mode = dump(accessor, path, hashSink, hook, filter);
auto mode = dump(path, hashSink, hook, filter);
auto hash = hashSink.finish().first;
return {
.mode = mode,

View file

@ -8,7 +8,7 @@
#include "types.hh"
#include "serialise.hh"
#include "hash.hh"
#include "source-accessor.hh"
#include "source-path.hh"
#include "fs-sink.hh"
namespace nix::git {
@ -125,7 +125,7 @@ std::optional<Mode> convertMode(SourceAccessor::Type type);
* Given a `Hash`, return a `SourceAccessor` and `CanonPath` pointing to
* the file system object with that path.
*/
using RestoreHook = std::pair<SourceAccessor *, CanonPath>(Hash);
using RestoreHook = SourcePath(Hash);
/**
* Wrapper around `parse` and `RestoreSink`
@ -157,10 +157,10 @@ void dumpTree(
* Note that if the child is a directory, its child in must also be so
* processed in order to compute this information.
*/
using DumpHook = TreeEntry(const CanonPath & path);
using DumpHook = TreeEntry(const SourcePath & path);
Mode dump(
SourceAccessor & accessor, const CanonPath & path,
const SourcePath & path,
Sink & sink,
std::function<DumpHook> hook,
PathFilter & filter = defaultPathFilter,
@ -172,9 +172,9 @@ Mode dump(
* A smaller wrapper around `dump`.
*/
TreeEntry dumpHash(
HashAlgorithm ha,
SourceAccessor & accessor, const CanonPath & path,
PathFilter & filter = defaultPathFilter);
HashAlgorithm ha,
const SourcePath & path,
PathFilter & filter = defaultPathFilter);
/**
* A line from the output of `git ls-remote --symref`.

View file

@ -1,4 +1,5 @@
#include "posix-source-accessor.hh"
#include "source-path.hh"
#include "signals.hh"
#include "sync.hh"
@ -17,11 +18,11 @@ PosixSourceAccessor::PosixSourceAccessor()
: PosixSourceAccessor(std::filesystem::path {})
{ }
std::pair<PosixSourceAccessor, CanonPath> PosixSourceAccessor::createAtRoot(const std::filesystem::path & path)
SourcePath PosixSourceAccessor::createAtRoot(const std::filesystem::path & path)
{
std::filesystem::path path2 = absPath(path.string());
return {
PosixSourceAccessor { path2.root_path() },
make_ref<PosixSourceAccessor>(path2.root_path()),
CanonPath { path2.relative_path().string() },
};
}

View file

@ -4,6 +4,8 @@
namespace nix {
struct SourcePath;
/**
* A source accessor that uses the Unix filesystem.
*/
@ -53,7 +55,7 @@ struct PosixSourceAccessor : virtual SourceAccessor
* and
* [`std::filesystem::path::relative_path`](https://en.cppreference.com/w/cpp/filesystem/path/relative_path).
*/
static std::pair<PosixSourceAccessor, CanonPath> createAtRoot(const std::filesystem::path & path);
static SourcePath createAtRoot(const std::filesystem::path & path);
private:

View file

@ -41,6 +41,11 @@ struct SourcePath
*/
std::string readFile() const;
void readFile(
Sink & sink,
std::function<void(uint64_t)> sizeCallback = [](uint64_t size){}) const
{ return accessor->readFile(path, sink, sizeCallback); }
/**
* Return whether this `SourcePath` denotes a file (of any type)
* that exists