Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
source-accessor.hh
1#pragma once
2
3#include <filesystem>
4
5#include "canon-path.hh"
6#include "hash.hh"
7#include "ref.hh"
8
9namespace nix {
10
11struct Sink;
12
17enum class SymlinkResolution {
23 Ancestors,
24
30 Full,
31};
32
33MakeError(FileNotFound, Error);
34
41struct SourceAccessor : std::enable_shared_from_this<SourceAccessor>
42{
43 const size_t number;
44
45 std::string displayPrefix, displaySuffix;
46
47 SourceAccessor();
48
49 virtual ~SourceAccessor()
50 { }
51
62 virtual std::string readFile(const CanonPath & path);
63
75 virtual void readFile(
76 const CanonPath & path,
77 Sink & sink,
78 std::function<void(uint64_t)> sizeCallback = [](uint64_t size){});
79
80 virtual bool pathExists(const CanonPath & path);
81
82 enum Type {
83 tRegular, tSymlink, tDirectory,
91 tChar, tBlock, tSocket, tFifo,
92 tUnknown
93 };
94
95 struct Stat
96 {
97 Type type = tUnknown;
98
104 std::optional<uint64_t> fileSize;
105
109 bool isExecutable = false;
110
115 std::optional<uint64_t> narOffset;
116
117 bool isNotNARSerialisable();
118 std::string typeString();
119 };
120
121 Stat lstat(const CanonPath & path);
122
123 virtual std::optional<Stat> maybeLstat(const CanonPath & path) = 0;
124
125 typedef std::optional<Type> DirEntry;
126
127 typedef std::map<std::string, DirEntry> DirEntries;
128
132 virtual DirEntries readDirectory(const CanonPath & path) = 0;
133
134 virtual std::string readLink(const CanonPath & path) = 0;
135
136 virtual void dumpPath(
137 const CanonPath & path,
138 Sink & sink,
139 PathFilter & filter = defaultPathFilter);
140
141 Hash hashPath(
142 const CanonPath & path,
143 PathFilter & filter = defaultPathFilter,
144 HashAlgorithm ha = HashAlgorithm::SHA256);
145
151 virtual std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path)
152 { return std::nullopt; }
153
154 bool operator == (const SourceAccessor & x) const
155 {
156 return number == x.number;
157 }
158
159 auto operator <=> (const SourceAccessor & x) const
160 {
161 return number <=> x.number;
162 }
163
164 void setPathDisplay(std::string displayPrefix, std::string displaySuffix = "");
165
166 virtual std::string showPath(const CanonPath & path);
167
176 const CanonPath & path,
177 SymlinkResolution mode = SymlinkResolution::Full);
178
183 std::optional<std::string> fingerprint;
184
189 virtual std::optional<time_t> getLastModified()
190 { return std::nullopt; }
191};
192
196ref<SourceAccessor> makeEmptySourceAccessor();
197
202MakeError(RestrictedPathError, Error);
203
207ref<SourceAccessor> getFSSourceAccessor();
208
215ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root);
216
217}
Definition canon-path.hh:41
std::function< bool(const Path &path)> PathFilter
Definition file-system.hh:365
const size_t number
Definition lexer.l:4439
std::optional< SourceAccessor::Stat > maybeLstat() const
T x
Definition lexer.l:2648
CanonPath(std::string_view raw)
SourceAccessor::Stat lstat() const
std::string readFile() const
std::string readLink() const
bool pathExists() const
Definition hash.hh:45
Definition serialise.hh:20
Definition source-accessor.hh:96
std::optional< uint64_t > fileSize
Definition source-accessor.hh:104
bool isExecutable
Definition source-accessor.hh:109
std::optional< uint64_t > narOffset
Definition source-accessor.hh:115
Definition source-accessor.hh:42
virtual std::optional< time_t > getLastModified()
Definition source-accessor.hh:189
virtual DirEntries readDirectory(const CanonPath &path)=0
std::optional< std::string > fingerprint
Definition source-accessor.hh:183
Type
Definition source-accessor.hh:82
@ tChar
Definition source-accessor.hh:91
virtual std::optional< std::filesystem::path > getPhysicalPath(const CanonPath &path)
Definition source-accessor.hh:151
CanonPath resolveSymlinks(const CanonPath &path, SymlinkResolution mode=SymlinkResolution::Full)
Definition source-accessor.cc:90