Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
local-store.hh
Go to the documentation of this file.
1#pragma once
3
4#include "sqlite.hh"
5
6#include "pathlocks.hh"
7#include "store-api.hh"
9#include "sync.hh"
10
11#include <chrono>
12#include <future>
13#include <string>
14#include <unordered_set>
15
16
17namespace nix {
18
19
28const int nixSchemaVersion = 10;
29
30
32{
33 unsigned long filesLinked = 0;
34 uint64_t bytesFreed = 0;
35};
36
37struct LocalStoreConfig : virtual LocalFSStoreConfig
38{
40
41 LocalStoreConfig(
42 std::string_view scheme,
43 std::string_view authority,
44 const Params & params);
45
46 Setting<bool> requireSigs{this,
47 settings.requireSigs,
48 "require-sigs",
49 "Whether store paths copied into this store should have a trusted signature."};
50
51 Setting<bool> readOnly{this,
52 false,
53 "read-only",
54 R"(
55 Allow this store to be opened when its [database](@docroot@/glossary.md#gloss-nix-database) is on a read-only filesystem.
56
57 Normally Nix will attempt to open the store database in read-write mode, even for querying (when write access is not needed), causing it to fail if the database is on a read-only filesystem.
58
59 Enable read-only mode to disable locking and open the SQLite database with the [`immutable` parameter](https://www.sqlite.org/c3ref/open.html) set.
60
61 > **Warning**
62 > Do not use this unless the filesystem is read-only.
63 >
64 > Using it when the filesystem is writable can cause incorrect query results or corruption errors if the database is changed by another process.
65 > While the filesystem the database resides on might appear to be read-only, consider whether another user or system might have write access to it.
66 )"};
67
68 const std::string name() override { return "Local Store"; }
69
70 static std::set<std::string> uriSchemes()
71 { return {"local"}; }
72
73 std::string doc() override;
74};
75
76class LocalStore : public virtual LocalStoreConfig
77 , public virtual IndirectRootStore
78 , public virtual GcStore
79{
80private:
81
85 AutoCloseFD globalLock;
86
87 struct State
88 {
92 SQLite db;
93
94 struct Stmts;
95 std::unique_ptr<Stmts> stmts;
96
101 std::chrono::time_point<std::chrono::steady_clock> lastGCCheck;
102
107 bool gcRunning = false;
108 std::shared_future<void> gcFuture;
109
116 uint64_t availAfterGC = std::numeric_limits<uint64_t>::max();
117
118 std::unique_ptr<PublicKeys> publicKeys;
119 };
120
121 Sync<State> _state;
122
123public:
124
125 const Path dbDir;
126 const Path linksDir;
127 const Path reservedPath;
128 const Path schemaPath;
129 const Path tempRootsDir;
130 const Path fnTempRoots;
131
132private:
133
134 const PublicKeys & getPublicKeys();
135
136public:
137
141 PathSet locksHeld;
142
147 LocalStore(const Params & params);
149 std::string_view scheme,
150 PathView path,
151 const Params & params);
152
153 ~LocalStore();
154
158
159 std::string getUri() override;
160
161 bool isValidPathUncached(const StorePath & path) override;
162
163 StorePathSet queryValidPaths(const StorePathSet & paths,
164 SubstituteFlag maybeSubstitute = NoSubstitute) override;
165
166 StorePathSet queryAllValidPaths() override;
167
168 void queryPathInfoUncached(const StorePath & path,
169 Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept override;
170
171 void queryReferrers(const StorePath & path, StorePathSet & referrers) override;
172
173 StorePathSet queryValidDerivers(const StorePath & path) override;
174
175 std::map<std::string, std::optional<StorePath>> queryStaticPartialDerivationOutputMap(const StorePath & path) override;
176
177 std::optional<StorePath> queryPathFromHashPart(const std::string & hashPart) override;
178
179 StorePathSet querySubstitutablePaths(const StorePathSet & paths) override;
180
181 bool pathInfoIsUntrusted(const ValidPathInfo &) override;
182 bool realisationIsUntrusted(const Realisation & ) override;
183
184 void addToStore(const ValidPathInfo & info, Source & source,
185 RepairFlag repair, CheckSigsFlag checkSigs) override;
186
188 Source & dump,
189 std::string_view name,
190 FileSerialisationMethod dumpMethod,
191 ContentAddressMethod hashMethod,
192 HashAlgorithm hashAlgo,
193 const StorePathSet & references,
194 RepairFlag repair) override;
195
196 void addTempRoot(const StorePath & path) override;
197
198private:
199
200 void createTempRootsFile();
201
205 Sync<AutoCloseFD> _fdTempRoots;
206
210 Sync<AutoCloseFD> _fdGCLock;
211
215 Sync<AutoCloseFD> _fdRootsSocket;
216
217public:
218
225 void addIndirectRoot(const Path & path) override;
226
227private:
228
229 void findTempRoots(Roots & roots, bool censor);
230
231 AutoCloseFD openGCLock();
232
233public:
234
235 Roots findRoots(bool censor) override;
236
237 void collectGarbage(const GCOptions & options, GCResults & results) override;
238
246 virtual void queryGCReferrers(const StorePath & path, StorePathSet & referrers)
247 {
248 return queryReferrers(path, referrers);
249 }
250
256 virtual void deleteStorePath(const Path & path, uint64_t & bytesFreed);
257
262 void optimiseStore(OptimiseStats & stats);
263
264 void optimiseStore() override;
265
270 void optimisePath(const Path & path, RepairFlag repair);
271
272 bool verifyStore(bool checkContents, RepairFlag repair) override;
273
274protected:
275
283 bool errors;
284
289 StorePathSet validPaths;
290 };
291
296
297public:
298
307 void registerValidPath(const ValidPathInfo & info);
308
309 virtual void registerValidPaths(const ValidPathInfos & infos);
310
311 unsigned int getProtocol() override;
312
313 std::optional<TrustedFlag> isTrustedClient() override;
314
315 void vacuumDB();
316
317 void addSignatures(const StorePath & storePath, const StringSet & sigs) override;
318
323 void autoGC(bool sync = true);
324
329 void registerDrvOutput(const Realisation & info) override;
330 void registerDrvOutput(const Realisation & info, CheckSigsFlag checkSigs) override;
331 void cacheDrvOutputMapping(
332 State & state,
333 const uint64_t deriver,
334 const std::string & outputName,
335 const StorePath & output);
336
337 std::optional<const Realisation> queryRealisation_(State & state, const DrvOutput & id);
338 std::optional<std::pair<int64_t, Realisation>> queryRealisationCore_(State & state, const DrvOutput & id);
339 void queryRealisationUncached(const DrvOutput&,
340 Callback<std::shared_ptr<const Realisation>> callback) noexcept override;
341
342 std::optional<std::string> getVersion() override;
343
344protected:
345
346 void verifyPath(const StorePath & path, std::function<bool(const StorePath &)> existsInStoreDir,
347 StorePathSet & done, StorePathSet & validPaths, RepairFlag repair, bool & errors);
348
349private:
350
355 int getSchema();
356
357 void openDB(State & state, bool create);
358
359 void upgradeDBSchema(State & state);
360
361 void makeStoreWritable();
362
363 uint64_t queryValidPathId(State & state, const StorePath & path);
364
365 uint64_t addValidPath(State & state, const ValidPathInfo & info, bool checkOutputs = true);
366
367 void invalidatePath(State & state, const StorePath & path);
368
372 void invalidatePathChecked(const StorePath & path);
373
374 std::shared_ptr<const ValidPathInfo> queryPathInfoInternal(State & state, const StorePath & path);
375
376 void updatePathInfo(State & state, const ValidPathInfo & info);
377
378 PathSet queryValidPathsOld();
379 ValidPathInfo queryPathInfoOld(const Path & path);
380
381 void findRoots(const Path & path, std::filesystem::file_type type, Roots & roots);
382
383 void findRootsNoTemp(Roots & roots, bool censor);
384
385 void findRuntimeRoots(Roots & roots, bool censor);
386
387 std::pair<std::filesystem::path, AutoCloseFD> createTempDirInStore();
388
389 typedef std::unordered_set<ino_t> InodeHash;
390
391 InodeHash loadInodeHash();
392 Strings readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash);
393 void optimisePath_(Activity * act, OptimiseStats & stats, const Path & path, InodeHash & inodeHash, RepairFlag repair);
394
395 // Internal versions that are not wrapped in retry_sqlite.
396 bool isValidPath_(State & state, const StorePath & path);
397 void queryReferrers(State & state, const StorePath & path, StorePathSet & referrers);
398
403 void signPathInfo(ValidPathInfo & info);
404 void signRealisation(Realisation &);
405
406 void addBuildLog(const StorePath & drvPath, std::string_view log) override;
407
408 friend struct LocalDerivationGoal;
409 friend struct PathSubstitutionGoal;
410 friend struct SubstitutionGoal;
411 friend struct DerivationGoal;
412};
413
414}
Definition file-descriptor.hh:152
Definition callback.hh:17
virtual VerificationResult verifyAllValidPaths(RepairFlag repair)
Definition local-store.cc:1439
StorePathSet queryValidPaths(const StorePathSet &paths, SubstituteFlag maybeSubstitute=NoSubstitute) override
Definition local-store.cc:790
std::string getUri() override
Definition local-store.cc:407
StorePathSet queryAllValidPaths() override
Definition local-store.cc:799
Roots findRoots(bool censor) override
Definition gc.cc:299
std::optional< TrustedFlag > isTrustedClient() override
Definition local-store.cc:1529
void queryReferrers(const StorePath &path, StorePathSet &referrers) override
Definition local-store.cc:820
void addSignatures(const StorePath &storePath, const StringSet &sigs) override
Definition local-store.cc:1542
virtual void deleteStorePath(const Path &path, uint64_t &bytesFreed)
Definition local-store.cc:374
void addTempRoot(const StorePath &path) override
Definition gc.cc:81
LocalStore(const Params &params)
Definition local-store.cc:354
bool pathInfoIsUntrusted(const ValidPathInfo &) override
Definition local-store.cc:1006
void addIndirectRoot(const Path &path) override
Definition gc.cc:41
StorePathSet queryValidDerivers(const StorePath &path) override
Definition local-store.cc:829
void addToStore(const ValidPathInfo &info, Source &source, RepairFlag repair, CheckSigsFlag checkSigs) override
Definition local-store.cc:1016
void registerDrvOutput(const Realisation &info) override
Definition local-store.cc:571
void collectGarbage(const GCOptions &options, GCResults &results) override
Definition gc.cc:452
void registerValidPath(const ValidPathInfo &info)
Definition local-store.cc:914
bool verifyStore(bool checkContents, RepairFlag repair) override
Definition local-store.cc:1342
unsigned int getProtocol() override
Definition local-store.cc:1524
StorePath addToStoreFromDump(Source &dump, std::string_view name, FileSerialisationMethod dumpMethod, ContentAddressMethod hashMethod, HashAlgorithm hashAlgo, const StorePathSet &references, RepairFlag repair) override
Definition local-store.cc:1133
virtual void queryGCReferrers(const StorePath &path, StorePathSet &referrers)
Definition local-store.hh:246
PathSet locksHeld
Definition local-store.hh:141
std::map< std::string, std::optional< StorePath > > queryStaticPartialDerivationOutputMap(const StorePath &path) override
Definition local-store.cc:846
StorePathSet querySubstitutablePaths(const StorePathSet &paths) override
Definition local-store.cc:883
void optimiseStore() override
Definition optimise-store.cc:294
void autoGC(bool sync=true)
Definition gc.cc:898
void optimisePath(const Path &path, RepairFlag repair)
Definition optimise-store.cc:305
std::optional< StorePath > queryPathFromHashPart(const std::string &hashPart) override
Definition local-store.cc:862
Definition config.hh:320
Definition path.hh:27
FileSerialisationMethod
Definition file-content-address.hh:20
ErrorPrintBehavior errors
Definition lexer.l:4867
const Activity & act
Definition lexer.l:2371
Symbol create(std::string_view s)
Definition lexer.l:1018
ValueType type
Definition lexer.l:7098
RepairFlag repair
Definition lexer.l:7173
const int nixSchemaVersion
Definition local-store.hh:28
Definition logging.hh:137
Definition content-address.hh:31
Definition realisation.hh:24
Definition gc-store.hh:15
Definition gc-store.hh:60
Definition gc-store.hh:102
Definition indirect-root-store.hh:40
LocalFSStoreConfig(PathView path, const Params &params)
Definition local-fs-store.cc:11
const std::string name() override
Definition local-store.hh:68
LocalFSStoreConfig(PathView path, const Params &params)
Definition local-fs-store.cc:11
std::string doc() override
Definition local-store.cc:69
Definition local-store.hh:279
StorePathSet validPaths
Definition local-store.hh:289
bool errors
Definition local-store.hh:283
Definition local-store.hh:32
Definition realisation.hh:49
Definition sqlite.hh:39
Definition serialise.hh:68
Definition path-info.hh:130
std::string Path
Definition types.hh:22