Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
source-accessor.hh
1#pragma once
2
3#include <filesystem>
4#include <variant>
5
7#include "nix/util/fmt.hh"
9#include "nix/util/fun.hh"
10#include "nix/util/hash.hh"
11#include "nix/util/ref.hh"
12
13namespace nix {
14
15struct Sink;
16
21enum class SymlinkResolution {
27 Ancestors,
28
34 Full,
35};
36
37MakeError(SourceAccessorError, Error);
38MakeError(FileNotFound, SourceAccessorError);
39MakeError(NotASymlink, SourceAccessorError);
40MakeError(NotADirectory, SourceAccessorError);
41MakeError(NotARegularFile, SourceAccessorError);
42
49struct SourceAccessor : std::enable_shared_from_this<SourceAccessor>
50{
51private:
52 /* VTable anchor to avoid weak linkage of the vtable - it breaks
53 * dynamic_cast across shared libraries on Darwin. */
54 virtual void anchor() = 0;
55public:
56 const size_t number;
57
58 std::string displayPrefix, displaySuffix;
59
60 SourceAccessor();
61
62 virtual ~SourceAccessor() {}
63
74 std::string readFile(const CanonPath & path);
75
87 virtual void
88 readFile(const CanonPath & path, Sink & sink, fun<void(uint64_t)> sizeCallback = [](uint64_t size) {}) = 0;
89
97 virtual bool pathExists(const CanonPath & path);
98
99 enum Type {
100 tRegular,
101 tSymlink,
102 tDirectory,
112 tBlock,
113 tSocket,
114 tFifo,
115 tUnknown
116 };
117
118 struct Stat
119 {
120 Type type = tUnknown;
121
127 std::optional<uint64_t> fileSize;
128
132 bool isExecutable = false;
133
138 std::optional<uint64_t> narOffset;
139
140 bool isNotNARSerialisable();
141 std::string typeString();
142 };
143
144 virtual Stat lstat(const CanonPath & path);
145
146 virtual std::optional<Stat> maybeLstat(const CanonPath & path) = 0;
147
148 typedef std::optional<Type> DirEntry;
149
150 typedef std::map<std::string, DirEntry> DirEntries;
151
155 virtual DirEntries readDirectory(const CanonPath & path) = 0;
156
166 virtual void readDirectory(
167 const CanonPath & dirPath,
168 std::function<void(SourceAccessor & subdirAccessor, const CanonPath & subdirRelPath)> callback)
169 {
170 callback(*this, dirPath);
171 }
172
173 virtual std::string readLink(const CanonPath & path) = 0;
174
175 virtual void dumpPath(const CanonPath & path, Sink & sink, PathFilter & filter = defaultPathFilter);
176
177 Hash
178 hashPath(const CanonPath & path, PathFilter & filter = defaultPathFilter, HashAlgorithm ha = HashAlgorithm::SHA256);
179
185 virtual std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path)
186 {
187 return std::nullopt;
188 }
189
190 bool operator==(const SourceAccessor & x) const
191 {
192 return number == x.number;
193 }
194
195 auto operator<=>(const SourceAccessor & x) const
196 {
197 return number <=> x.number;
198 }
199
200 void setPathDisplay(std::string displayPrefix, std::string displaySuffix = "");
201
210 virtual std::string showPath(const CanonPath & path);
211
219 CanonPath resolveSymlinks(const CanonPath & path, SymlinkResolution mode = SymlinkResolution::Full);
220
225 std::optional<std::string> fingerprint;
226
243 virtual std::pair<CanonPath, std::optional<std::string>> getFingerprint(const CanonPath & path)
244 {
245 return {path, fingerprint};
246 }
247
252 virtual std::optional<time_t> getLastModified()
253 {
254 return std::nullopt;
255 }
256
261 virtual void invalidateCache() {}
262};
263
267ref<SourceAccessor> makeEmptySourceAccessor();
268
273MakeError(RestrictedPathError, Error);
274
275class SymlinkNotAllowed final : public CloneableError<SymlinkNotAllowed, Error>
276{
277 void anchor() override;
278
279public:
284 std::variant<CanonPath, OsCanonPath> path;
285
286 SymlinkNotAllowed(CanonPath path)
287 : CloneableError("relative path '%s' points to a symlink, which is not allowed", path.rel())
288 , path(std::move(path))
289 {
290 }
291
292 template<typename... Args>
293 SymlinkNotAllowed(CanonPath path, const std::string & fs, Args &&... args)
294 : CloneableError(fs, std::forward<Args>(args)...)
295 , path(std::move(path))
296 {
297 }
298
299 SymlinkNotAllowed(OsCanonPath path)
300 : CloneableError("relative path %s points to a symlink, which is not allowed", PathFmt(path.path()))
301 , path(std::move(path))
302 {
303 }
304
305 template<typename... Args>
306 SymlinkNotAllowed(OsCanonPath path, const std::string & fs, Args &&... args)
307 : CloneableError(fs, std::forward<Args>(args)...)
308 , path(std::move(path))
309 {
310 }
311};
312
316ref<SourceAccessor> getFSSourceAccessor();
317
332ref<SourceAccessor> makeFSSourceAccessor(
333 std::filesystem::path root, bool trackLastModified = false, FinalSymlink finalSymlink = FinalSymlink::DontFollow);
334
339ref<SourceAccessor> makeUnionSourceAccessor(std::vector<ref<SourceAccessor>> && accessors);
340
345ref<SourceAccessor> makeCachingSourceAccessor(ref<SourceAccessor> next);
346
347} // namespace nix
Definition args.hh:29
Definition canon-path.hh:50
Definition error.hh:279
Definition error.hh:310
Definition source-accessor.hh:276
std::variant< CanonPath, OsCanonPath > path
Definition source-accessor.hh:284
Definition fun.hh:19
Definition ref.hh:58
FinalSymlink
Definition file-system.hh:193
fun< bool(const std::string &path)> PathFilter
Definition file-system.hh:494
Definition hash.hh:58
Definition serialise.hh:18
Definition source-accessor.hh:119
std::optional< uint64_t > fileSize
Definition source-accessor.hh:127
bool isExecutable
Definition source-accessor.hh:132
std::optional< uint64_t > narOffset
Definition source-accessor.hh:138
Definition source-accessor.hh:50
virtual void invalidateCache()
Definition source-accessor.hh:261
virtual std::pair< CanonPath, std::optional< std::string > > getFingerprint(const CanonPath &path)
Definition source-accessor.hh:243
virtual std::optional< time_t > getLastModified()
Definition source-accessor.hh:252
virtual std::string showPath(const CanonPath &path)
Render a human-readable representation of the accessor + path beneath it.
Definition source-accessor.cc:95
virtual void readFile(const CanonPath &path, Sink &sink, fun< void(uint64_t)> sizeCallback=[](uint64_t size) {})=0
std::string readFile(const CanonPath &path)
Definition source-accessor.cc:62
virtual DirEntries readDirectory(const CanonPath &path)=0
std::optional< std::string > fingerprint
Definition source-accessor.hh:225
virtual void readDirectory(const CanonPath &dirPath, std::function< void(SourceAccessor &subdirAccessor, const CanonPath &subdirRelPath)> callback)
Definition source-accessor.hh:166
Type
Definition source-accessor.hh:99
@ tChar
Definition source-accessor.hh:111
virtual std::optional< std::filesystem::path > getPhysicalPath(const CanonPath &path)
Definition source-accessor.hh:185
CanonPath resolveSymlinks(const CanonPath &path, SymlinkResolution mode=SymlinkResolution::Full)
Definition source-accessor.cc:100