Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
source-path.hh
Go to the documentation of this file.
1#pragma once
7
8#include "nix/util/ref.hh"
10#include "nix/util/source-accessor.hh"
11#include "nix/util/std-hash.hh"
12
13namespace nix {
14
21struct SourcePath
22{
23 ref<SourceAccessor> accessor;
24 CanonPath path;
25
26 SourcePath(ref<SourceAccessor> accessor, CanonPath path = CanonPath::root)
27 : accessor(std::move(accessor))
28 , path(std::move(path))
29 {
30 }
31
32 std::string_view baseName() const;
33
38 SourcePath parent() const;
39
44 std::string readFile() const;
45
46 void readFile(Sink & sink, fun<void(uint64_t)> sizeCallback = [](uint64_t size) {}) const
47 {
48 return accessor->readFile(path, sink, sizeCallback);
49 }
50
55 bool pathExists() const;
56
62
67 std::optional<SourceAccessor::Stat> maybeLstat() const;
68
73 SourceAccessor::DirEntries readDirectory() const;
74
79 std::string readLink() const;
80
84 void dumpPath(Sink & sink, PathFilter & filter = defaultPathFilter) const;
85
92 std::optional<std::filesystem::path> getPhysicalPath() const;
93
99 std::string to_string() const;
100
104 SourcePath operator/(const CanonPath & x) const;
105
111 SourcePath operator/(std::string_view c) const;
112
113 bool operator==(const SourcePath & x) const noexcept;
114 std::strong_ordering operator<=>(const SourcePath & x) const noexcept;
115
119 SourcePath resolveSymlinks(SymlinkResolution mode = SymlinkResolution::Full) const
120 {
121 return {accessor, accessor->resolveSymlinks(path, mode)};
122 }
123
124 friend class std::hash<nix::SourcePath>;
125};
126
127std::ostream & operator<<(std::ostream & str, const SourcePath & path);
128
129inline std::size_t hash_value(const SourcePath & path)
130{
131 std::size_t hash = 0;
132 boost::hash_combine(hash, path.accessor->number);
133 boost::hash_combine(hash, path.path);
134 return hash;
135}
136
137} // namespace nix
138
139template<>
140struct std::hash<nix::SourcePath>
141{
142 using is_avalanching = std::true_type;
143
144 std::size_t operator()(const nix::SourcePath & s) const noexcept
145 {
146 return nix::hash_value(s);
147 }
148};
Definition canon-path.hh:50
Definition fun.hh:19
Definition ref.hh:58
fun< bool(const std::string &path)> PathFilter
Definition file-system.hh:494
Definition serialise.hh:18
Definition source-accessor.hh:119
Definition source-path.hh:22
std::optional< std::filesystem::path > getPhysicalPath() const
Definition source-path.cc:52
SourceAccessor::Stat lstat() const
Definition source-path.cc:27
bool pathExists() const
Definition source-path.cc:22
void dumpPath(Sink &sink, PathFilter &filter=defaultPathFilter) const
Definition source-path.cc:47
std::string readFile() const
Definition source-path.cc:17
std::string readLink() const
Definition source-path.cc:42
SourceAccessor::DirEntries readDirectory() const
Definition source-path.cc:37
std::optional< SourceAccessor::Stat > maybeLstat() const
Definition source-path.cc:32
SourcePath resolveSymlinks(SymlinkResolution mode=SymlinkResolution::Full) const
Definition source-path.hh:119
std::string to_string() const
Render a human-readable representation of the accessor + path beneath it.
Definition source-path.cc:57
SourcePath operator/(const CanonPath &x) const
Definition source-path.cc:62
SourcePath parent() const
Definition source-path.cc:10