Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
path.hh
Go to the documentation of this file.
1#pragma once
3
4#include <string_view>
5
6#include "nix/util/types.hh"
9
10namespace nix {
11
12struct Hash;
13
14MakeError(BadStorePath, Error);
15MakeError(BadStorePathName, BadStorePath);
16
23void checkName(std::string_view name);
24
32class StorePath
33{
34 std::string baseName;
35
36public:
37
41 constexpr static size_t HashLen = 32; // i.e. 160 bits
42
48 constexpr static size_t MaxNameLen = 211;
49
53 constexpr static size_t MaxBasenameLen = HashLen + 1 + MaxNameLen;
54
55 StorePath() = delete;
56
58 StorePath(std::string_view baseName);
59
61 StorePath(const Hash & hash, std::string_view name);
62
63 std::string_view to_string() const noexcept
64 {
65 return baseName;
66 }
67
68 bool operator==(const StorePath & other) const noexcept = default;
69 auto operator<=>(const StorePath & other) const noexcept = default;
70
74 bool isDerivation() const noexcept;
75
79 void requireDerivation() const;
80
81 std::string_view name() const
82 {
83 return std::string_view(baseName).substr(HashLen + 1);
84 }
85
86 std::string_view hashPart() const
87 {
88 return std::string_view(baseName).substr(0, HashLen);
89 }
90
91 static const StorePath dummy;
92
93 static StorePath random(std::string_view name);
94};
95
96typedef std::set<StorePath> StorePathSet;
97typedef std::vector<StorePath> StorePaths;
98
102constexpr std::string_view drvExtension = ".drv";
103
104template<>
105struct json_avoids_null<StorePath> : std::true_type
106{};
107
108} // namespace nix
109
110namespace std {
111
112template<>
113struct hash<nix::StorePath>
114{
115 std::size_t operator()(const nix::StorePath & path) const noexcept
116 {
117 return *(std::size_t *) path.to_string().data();
118 }
119};
120
121} // namespace std
122
123namespace nix {
124
125inline std::size_t hash_value(const StorePath & path)
126{
127 return std::hash<StorePath>{}(path);
128}
129
130} // namespace nix
131
132JSON_IMPL(nix::StorePath)
Definition error.hh:310
Definition path.hh:33
static constexpr size_t MaxNameLen
Maximum length of the name, i.e. everything after <hash part>-.
Definition path.hh:48
static constexpr size_t MaxBasenameLen
Maximum length of the basename admitted by the constructor from a string.
Definition path.hh:53
static constexpr size_t HashLen
Definition path.hh:41
void requireDerivation() const
Definition path.cc:73
constexpr std::string_view drvExtension
Definition path.hh:102
void checkName(std::string_view name)
Definition path.cc:12
Definition hash.hh:58