Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix::SourceAccessor Struct Referenceabstract

#include <source-accessor.hh>

Inheritance diagram for nix::SourceAccessor:
nix::FilteringSourceAccessor nix::GitSourceAccessor nix::MemorySourceAccessor nix::MountedSourceAccessor nix::NarAccessor nix::RemoteFSAccessor nix::AllowListSourceAccessor nix::CachingFilteringSourceAccessor nix::AllowListSourceAccessorImpl nix::GitExportIgnoreSourceAccessor

Classes

struct  Stat

Public Types

enum  Type {
  tRegular , tSymlink , tDirectory , tChar ,
  tBlock , tSocket , tFifo , tUnknown
}
typedef std::optional< Type > DirEntry
typedef std::map< std::string, DirEntry > DirEntries

Public Member Functions

std::string readFile (const CanonPath &path)
virtual void readFile (const CanonPath &path, Sink &sink, fun< void(uint64_t)> sizeCallback=[](uint64_t size) {})=0
virtual bool pathExists (const CanonPath &path)
 Check whether a file exists at path.
virtual Stat lstat (const CanonPath &path)
virtual std::optional< Stat > maybeLstat (const CanonPath &path)=0
virtual DirEntries readDirectory (const CanonPath &path)=0
virtual void readDirectory (const CanonPath &dirPath, std::function< void(SourceAccessor &subdirAccessor, const CanonPath &subdirRelPath)> callback)
virtual std::string readLink (const CanonPath &path)=0
virtual void dumpPath (const CanonPath &path, Sink &sink, PathFilter &filter=defaultPathFilter)
Hash hashPath (const CanonPath &path, PathFilter &filter=defaultPathFilter, HashAlgorithm ha=HashAlgorithm::SHA256)
virtual std::optional< std::filesystem::path > getPhysicalPath (const CanonPath &path)
bool operator== (const SourceAccessor &x) const
auto operator<=> (const SourceAccessor &x) const
void setPathDisplay (std::string displayPrefix, std::string displaySuffix="")
virtual std::string showPath (const CanonPath &path)
 Render a human-readable representation of the accessor + path beneath it.
CanonPath resolveSymlinks (const CanonPath &path, SymlinkResolution mode=SymlinkResolution::Full)
virtual std::pair< CanonPath, std::optional< std::string > > getFingerprint (const CanonPath &path)
virtual std::optional< time_t > getLastModified ()
virtual void invalidateCache ()

Public Attributes

const size_t number
std::string displayPrefix
std::string displaySuffix
std::optional< std::string > fingerprint

Detailed Description

A read-only filesystem abstraction. This is used by the Nix evaluator and elsewhere for accessing sources in various filesystem-like entities (such as the real filesystem, tarballs or Git repositories).

Member Enumeration Documentation

◆ Type

Enumerator
tChar 

Any other node types that may be encountered on the file system, such as device nodes, sockets, named pipe, and possibly even more exotic things.

Responsible for "unknown" from builtins.readFileType "/dev/null".

Unlike DT_UNKNOWN, this must not be used for deferring the lookup of types.

Member Function Documentation

◆ getFingerprint()

virtual std::pair< CanonPath, std::optional< std::string > > nix::SourceAccessor::getFingerprint ( const CanonPath & path)
inlinevirtual

Return the fingerprint for path. This is usually the fingerprint of the current accessor, but for composite accessors (like MountedSourceAccessor), we want to return the fingerprint of the "inner" accessor if the current one lacks a fingerprint.

So this method is intended to return the most-outer accessor that has a fingerprint for path. It also returns the path that path corresponds to in that accessor.

For example: in a MountedSourceAccessor that has /nix/store/foo mounted, getFingerprint("/nix/store/foo/bar") will return the path /bar and the fingerprint of the /nix/store/foo accessor.

Reimplemented in nix::FilteringSourceAccessor.

◆ getLastModified()

virtual std::optional< time_t > nix::SourceAccessor::getLastModified ( )
inlinevirtual

Return the maximum last-modified time of the files in this tree, if available.

◆ getPhysicalPath()

virtual std::optional< std::filesystem::path > nix::SourceAccessor::getPhysicalPath ( const CanonPath & path)
inlinevirtual

Return a corresponding path in the root filesystem, if possible. This is only possible for filesystems that are materialized in the root filesystem.

Reimplemented in nix::FilteringSourceAccessor.

◆ invalidateCache()

virtual void nix::SourceAccessor::invalidateCache ( )
inlinevirtual

Drop any cached state that could go stale across external filesystem mutation (e.g. cached directory fds).

Reimplemented in nix::FilteringSourceAccessor.

◆ pathExists()

bool nix::SourceAccessor::pathExists ( const CanonPath & path)
virtual

Check whether a file exists at path.

Todo
Consider making this non-virtual, since the evaluator uses maybeLstat as an indication that a file exists always (for positive caching purposes).

Reimplemented in nix::FilteringSourceAccessor, nix::GitSourceAccessor, and nix::MemorySourceAccessor.

◆ readDirectory() [1/2]

virtual void nix::SourceAccessor::readDirectory ( const CanonPath & dirPath,
std::function< void(SourceAccessor &subdirAccessor, const CanonPath &subdirRelPath)> callback )
inlinevirtual

Variation of readDirectory that receives a SourceAccessor possibly scoped to dirPath. Primary meant for recursive traversal functions that would benefit from *at-style syscalls relative to a particular directory.

Note
Like readFile, this method should not follow symlinks.
Parameters
callbackCaller-provided function invoked with a maximally deeply scoped SourceAccessor and the path that would have to be prepended to each path relative to dirPath to access a particular file with it.

◆ readDirectory() [2/2]

virtual DirEntries nix::SourceAccessor::readDirectory ( const CanonPath & path)
pure virtual
Note
Like readFile, this method should not follow symlinks.

Implemented in nix::FilteringSourceAccessor, nix::GitSourceAccessor, nix::MemorySourceAccessor, and nix::RemoteFSAccessor.

◆ readFile() [1/2]

std::string nix::SourceAccessor::readFile ( const CanonPath & path)

Return the contents of a file as a string.

Note
Unlike Unix, this method should not follow symlinks. Nix by default wants to manipulate symlinks explicitly, and not implicitly follow them, as they are frequently untrusted user data and thus may point to arbitrary locations. Acting on the targets targets of symlinks should only occasionally be done, and only with care.

◆ readFile() [2/2]

virtual void nix::SourceAccessor::readFile ( const CanonPath & path,
Sink & sink,
fun< void(uint64_t)> sizeCallback = [](uint64_t size) {} )
pure virtual

Write the contents of a file as a sink. sizeCallback must be called with the size of the file before any data is written to the sink.

Note
Like the other readFile, this method should not follow symlinks.
subclasses of SourceAccessor need to implement at least one of the readFile() variants.

Implemented in nix::FilteringSourceAccessor, nix::GitSourceAccessor, nix::MemorySourceAccessor, and nix::RemoteFSAccessor.

◆ resolveSymlinks()

CanonPath nix::SourceAccessor::resolveSymlinks ( const CanonPath & path,
SymlinkResolution mode = SymlinkResolution::Full )

Resolve any symlinks in path according to the given resolution mode.

Parameters
modemight only be a temporary solution for this. See the discussion in https://github.com/NixOS/nix/pull/9985.

◆ showPath()

std::string nix::SourceAccessor::showPath ( const CanonPath & path)
virtual

Render a human-readable representation of the accessor + path beneath it.

Warning
The output from this function is not considered stable and is primarily useful for error messages. In libexpr, we still have a single filesystem root (modulo builtin <nix/...> filesystem root). Prefer to render paths there directly from CanonPath when paths reside in the global root filesystem.

Reimplemented in nix::FilteringSourceAccessor.

Member Data Documentation

◆ fingerprint

std::optional<std::string> nix::SourceAccessor::fingerprint

A string that uniquely represents the contents of this accessor. This is used for caching lookups (see fetchToStore()).


The documentation for this struct was generated from the following files:
  • libutil/include/nix/util/source-accessor.hh
  • libutil/archive.cc
  • libutil/source-accessor.cc