1 : #ifndef __LOCAL_STORE_H
2 : #define __LOCAL_STORE_H
3 :
4 : #include <string>
5 :
6 : #include <ext/stdio_filebuf.h>
7 :
8 : #include "store-api.hh"
9 : #include "util.hh"
10 :
11 :
12 : namespace nix {
13 :
14 :
15 : /* Nix store and database schema version. Version 1 (or 0) was Nix <=
16 : 0.7. Version 2 was Nix 0.8 and 0.9. Version 3 is Nix 0.10.
17 : Version 4 is Nix 0.11. Version 5 is Nix 0.12*/
18 : const int nixSchemaVersion = 5;
19 :
20 :
21 : extern string drvsLogDir;
22 :
23 :
24 : struct OptimiseStats
25 : {
26 : unsigned long totalFiles;
27 : unsigned long sameContents;
28 : unsigned long filesLinked;
29 : unsigned long long bytesFreed;
30 : unsigned long long blocksFreed;
31 0 : OptimiseStats()
32 : {
33 0 : totalFiles = sameContents = filesLinked = 0;
34 0 : bytesFreed = blocksFreed = 0;
35 0 : }
36 : };
37 :
38 :
39 : typedef __gnu_cxx::stdio_filebuf<char> stdio_filebuf;
40 :
41 :
42 : struct RunningSubstituter
43 822 : {
44 : Pid pid;
45 : boost::shared_ptr<stdio_filebuf> toBuf, fromBuf;
46 : boost::shared_ptr<std::ostream> to;
47 : boost::shared_ptr<std::istream> from;
48 : };
49 :
50 :
51 : class LocalStore : public StoreAPI
52 : {
53 : private:
54 : bool substitutablePathsLoaded;
55 : PathSet substitutablePaths;
56 :
57 : typedef std::map<Path, RunningSubstituter> RunningSubstituters;
58 : RunningSubstituters runningSubstituters;
59 :
60 : public:
61 :
62 : /* Initialise the local store, upgrading the schema if
63 : necessary. */
64 : LocalStore();
65 :
66 : ~LocalStore();
67 :
68 : /* Implementations of abstract store API methods. */
69 :
70 : bool isValidPath(const Path & path);
71 :
72 : PathSet queryValidPaths();
73 :
74 : Hash queryPathHash(const Path & path);
75 :
76 : void queryReferences(const Path & path, PathSet & references);
77 :
78 : void queryReferrers(const Path & path, PathSet & referrers);
79 :
80 : Path queryDeriver(const Path & path);
81 :
82 : PathSet querySubstitutablePaths();
83 :
84 : bool hasSubstitutes(const Path & path);
85 :
86 : bool querySubstitutablePathInfo(const Path & path,
87 : SubstitutablePathInfo & info);
88 :
89 : bool querySubstitutablePathInfo(const Path & substituter,
90 : const Path & path, SubstitutablePathInfo & info);
91 :
92 : Path addToStore(const Path & srcPath, bool fixed = false,
93 : bool recursive = false, string hashAlgo = "",
94 : PathFilter & filter = defaultPathFilter);
95 :
96 : Path addTextToStore(const string & suffix, const string & s,
97 : const PathSet & references);
98 :
99 : void exportPath(const Path & path, bool sign,
100 : Sink & sink);
101 :
102 : Path importPath(bool requireSignature, Source & source);
103 :
104 : void buildDerivations(const PathSet & drvPaths);
105 :
106 : void ensurePath(const Path & path);
107 :
108 : void addTempRoot(const Path & path);
109 :
110 : void addIndirectRoot(const Path & path);
111 :
112 : void syncWithGC();
113 :
114 : Roots findRoots();
115 :
116 : void collectGarbage(const GCOptions & options, GCResults & results);
117 :
118 : /* Delete a path from the Nix store. */
119 : void deleteFromStore(const Path & path, unsigned long long & bytesFreed,
120 : unsigned long long & blocksFreed);
121 :
122 : /* Optimise the disk space usage of the Nix store by hard-linking
123 : files with the same contents. */
124 : void optimiseStore(bool dryRun, OptimiseStats & stats);
125 :
126 : /* Check the integrity of the Nix store. */
127 : void verifyStore(bool checkContents);
128 :
129 : /* Register the validity of a path, i.e., that `path' exists, that
130 : the paths referenced by it exists, and in the case of an output
131 : path of a derivation, that it has been produced by a succesful
132 : execution of the derivation (or something equivalent). Also
133 : register the hash of the file system contents of the path. The
134 : hash must be a SHA-256 hash. */
135 : void registerValidPath(const Path & path,
136 : const Hash & hash, const PathSet & references, const Path & deriver);
137 :
138 : void registerValidPaths(const ValidPathInfos & infos);
139 :
140 : private:
141 :
142 : Path schemaPath;
143 :
144 : /* Lock file used for upgrading. */
145 : AutoCloseFD globalLock;
146 :
147 : /* !!! The cache can grow very big. Maybe it should be pruned
148 : every once in a while. */
149 : std::map<Path, ValidPathInfo> pathInfoCache;
150 :
151 : /* Store paths for which the referrers file must be purged. */
152 : PathSet delayedUpdates;
153 :
154 : int getSchema();
155 :
156 : void registerValidPath(const ValidPathInfo & info, bool ignoreValidity = false);
157 :
158 : ValidPathInfo queryPathInfo(const Path & path);
159 :
160 : void rewriteReferrers(const Path & path, bool purge, PathSet referrers);
161 :
162 : void flushDelayedUpdates();
163 :
164 : bool queryReferrersInternal(const Path & path, PathSet & referrers);
165 :
166 : void invalidatePath(const Path & path);
167 :
168 : void upgradeStore12();
169 :
170 : void gcPath(const GCOptions & options, GCResults & results,
171 : const Path & path);
172 :
173 : void gcPathRecursive(const GCOptions & options,
174 : GCResults & results, PathSet & done, const Path & path);
175 :
176 : void startSubstituter(const Path & substituter,
177 : RunningSubstituter & runningSubstituter);
178 : };
179 :
180 :
181 : /* Copy a path recursively. */
182 : void copyPath(const Path & src, const Path & dst);
183 :
184 : /* "Fix", or canonicalise, the meta-data of the files in a store path
185 : after it has been built. In particular:
186 : - the last modification date on each file is set to 0 (i.e.,
187 : 00:00:00 1/1/1970 UTC)
188 : - the permissions are set of 444 or 555 (i.e., read-only with or
189 : without execute permission; setuid bits etc. are cleared)
190 : - the owner and group are set to the Nix user and group, if we're
191 : in a setuid Nix installation. */
192 : void canonicalisePathMetaData(const Path & path);
193 :
194 : void canonicalisePathMetaData(const Path & path, bool recurse);
195 :
196 0 : MakeError(PathInUse, Error);
197 :
198 : /* Whether we are in build users mode. */
199 : bool haveBuildUsers();
200 :
201 : /* Whether we are root. */
202 : bool amPrivileged();
203 :
204 : /* Recursively change the ownership of `path' to the current uid. */
205 : void getOwnership(const Path & path);
206 :
207 : /* Like deletePath(), but changes the ownership of `path' using the
208 : setuid wrapper if necessary (and possible). */
209 : void deletePathWrapped(const Path & path,
210 : unsigned long long & bytesFreed, unsigned long long & blocksFreed);
211 :
212 : void deletePathWrapped(const Path & path);
213 :
214 : }
215 :
216 :
217 : #endif /* !__LOCAL_STORE_H */
|