gnu: notmuch: Update to 0.21.
[jackhill/guix/guix.git] / nix / libstore / optimise-store.cc
CommitLineData
36457566
LC
1#include "config.h"
2
3#include "util.hh"
4#include "local-store.hh"
5#include "globals.hh"
6
54c260e6 7#include <cstdlib>
36457566
LC
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11#include <errno.h>
12#include <stdio.h>
13
14
15namespace nix {
16
17
18static void makeWritable(const Path & path)
19{
20 struct stat st;
21 if (lstat(path.c_str(), &st))
22 throw SysError(format("getting attributes of path `%1%'") % path);
23 if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
24 throw SysError(format("changing writability of `%1%'") % path);
25}
26
27
28struct MakeReadOnly
29{
30 Path path;
31 MakeReadOnly(const Path & path) : path(path) { }
32 ~MakeReadOnly()
33 {
34 try {
35 /* This will make the path read-only. */
36 if (path != "") canonicaliseTimestampAndPermissions(path);
37 } catch (...) {
38 ignoreException();
39 }
40 }
41};
42
43
15ddeff5
LC
44LocalStore::InodeHash LocalStore::loadInodeHash()
45{
46 printMsg(lvlDebug, "loading hash inodes in memory");
47 InodeHash inodeHash;
48
49 AutoCloseDir dir = opendir(linksDir.c_str());
50 if (!dir) throw SysError(format("opening directory `%1%'") % linksDir);
51
52 struct dirent * dirent;
53 while (errno = 0, dirent = readdir(dir)) { /* sic */
54 checkInterrupt();
55 // We don't care if we hit non-hash files, anything goes
56 inodeHash.insert(dirent->d_ino);
57 }
58 if (errno) throw SysError(format("reading directory `%1%'") % linksDir);
59
60 printMsg(lvlTalkative, format("loaded %1% hash inodes") % inodeHash.size());
61
62 return inodeHash;
63}
64
65
66Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash)
67{
68 Strings names;
69
70 AutoCloseDir dir = opendir(path.c_str());
71 if (!dir) throw SysError(format("opening directory `%1%'") % path);
72
73 struct dirent * dirent;
74 while (errno = 0, dirent = readdir(dir)) { /* sic */
75 checkInterrupt();
76
77 if (inodeHash.count(dirent->d_ino)) {
78 printMsg(lvlDebug, format("`%1%' is already linked") % dirent->d_name);
79 continue;
80 }
81
82 string name = dirent->d_name;
83 if (name == "." || name == "..") continue;
84 names.push_back(name);
85 }
86 if (errno) throw SysError(format("reading directory `%1%'") % path);
87
88 return names;
89}
90
91
92void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path, InodeHash & inodeHash)
36457566
LC
93{
94 checkInterrupt();
15ddeff5 95
36457566
LC
96 struct stat st;
97 if (lstat(path.c_str(), &st))
98 throw SysError(format("getting attributes of path `%1%'") % path);
99
100 if (S_ISDIR(st.st_mode)) {
15ddeff5 101 Strings names = readDirectoryIgnoringInodes(path, inodeHash);
36457566 102 foreach (Strings::iterator, i, names)
15ddeff5 103 optimisePath_(stats, path + "/" + *i, inodeHash);
36457566
LC
104 return;
105 }
106
107 /* We can hard link regular files and maybe symlinks. */
108 if (!S_ISREG(st.st_mode)
109#if CAN_LINK_SYMLINK
110 && !S_ISLNK(st.st_mode)
111#endif
112 ) return;
113
114 /* Sometimes SNAFUs can cause files in the Nix store to be
115 modified, in particular when running programs as root under
116 NixOS (example: $fontconfig/var/cache being modified). Skip
117 those files. FIXME: check the modification time. */
118 if (S_ISREG(st.st_mode) && (st.st_mode & S_IWUSR)) {
119 printMsg(lvlError, format("skipping suspicious writable file `%1%'") % path);
120 return;
121 }
122
e134baae 123 /* This can still happen on top-level files. */
15ddeff5
LC
124 if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) {
125 printMsg(lvlDebug, format("`%1%' is already linked, with %2% other file(s).") % path % (st.st_nlink - 2));
126 return;
127 }
128
36457566
LC
129 /* Hash the file. Note that hashPath() returns the hash over the
130 NAR serialisation, which includes the execute bit on the file.
131 Thus, executable and non-executable files with the same
132 contents *won't* be linked (which is good because otherwise the
133 permissions would be screwed up).
134
135 Also note that if `path' is a symlink, then we're hashing the
136 contents of the symlink (i.e. the result of readlink()), not
137 the contents of the target (which may not even exist). */
138 Hash hash = hashPath(htSHA256, path).first;
36457566
LC
139 printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash));
140
141 /* Check if this is a known hash. */
142 Path linkPath = linksDir + "/" + printHash32(hash);
143
e134baae 144 retry:
36457566
LC
145 if (!pathExists(linkPath)) {
146 /* Nope, create a hard link in the links directory. */
15ddeff5
LC
147 if (link(path.c_str(), linkPath.c_str()) == 0) {
148 inodeHash.insert(st.st_ino);
149 return;
150 }
36457566
LC
151 if (errno != EEXIST)
152 throw SysError(format("cannot link `%1%' to `%2%'") % linkPath % path);
153 /* Fall through if another process created ‘linkPath’ before
154 we did. */
155 }
156
157 /* Yes! We've seen a file with the same contents. Replace the
158 current file with a hard link to that file. */
159 struct stat stLink;
160 if (lstat(linkPath.c_str(), &stLink))
161 throw SysError(format("getting attributes of path `%1%'") % linkPath);
162
36457566
LC
163 if (st.st_ino == stLink.st_ino) {
164 printMsg(lvlDebug, format("`%1%' is already linked to `%2%'") % path % linkPath);
165 return;
166 }
167
e134baae
ED
168 if (st.st_size != stLink.st_size) {
169 printMsg(lvlError, format("removing corrupted link ‘%1%’") % linkPath);
170 unlink(linkPath.c_str());
171 goto retry;
172 }
173
174 printMsg(lvlTalkative, format("linking ‘%1%’ to ‘%2%’") % path % linkPath);
36457566
LC
175
176 /* Make the containing directory writable, but only if it's not
177 the store itself (we don't want or need to mess with its
178 permissions). */
179 bool mustToggle = !isStorePath(path);
180 if (mustToggle) makeWritable(dirOf(path));
181
182 /* When we're done, make the directory read-only again and reset
183 its timestamp back to 0. */
184 MakeReadOnly makeReadOnly(mustToggle ? dirOf(path) : "");
185
186 Path tempLink = (format("%1%/.tmp-link-%2%-%3%")
187 % settings.nixStore % getpid() % rand()).str();
188
189 if (link(linkPath.c_str(), tempLink.c_str()) == -1) {
190 if (errno == EMLINK) {
191 /* Too many links to the same file (>= 32000 on most file
192 systems). This is likely to happen with empty files.
193 Just shrug and ignore. */
194 if (st.st_size)
195 printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
196 return;
197 }
198 throw SysError(format("cannot link `%1%' to `%2%'") % tempLink % linkPath);
199 }
200
201 /* Atomically replace the old file with the new hard link. */
202 if (rename(tempLink.c_str(), path.c_str()) == -1) {
203 if (unlink(tempLink.c_str()) == -1)
204 printMsg(lvlError, format("unable to unlink `%1%'") % tempLink);
205 if (errno == EMLINK) {
206 /* Some filesystems generate too many links on the rename,
207 rather than on the original link. (Probably it
208 temporarily increases the st_nlink field before
209 decreasing it again.) */
210 if (st.st_size)
211 printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
212 return;
213 }
214 throw SysError(format("cannot rename `%1%' to `%2%'") % tempLink % path);
215 }
216
217 stats.filesLinked++;
218 stats.bytesFreed += st.st_size;
219 stats.blocksFreed += st.st_blocks;
220}
221
222
223void LocalStore::optimiseStore(OptimiseStats & stats)
224{
225 PathSet paths = queryAllValidPaths();
15ddeff5 226 InodeHash inodeHash = loadInodeHash();
36457566
LC
227
228 foreach (PathSet::iterator, i, paths) {
229 addTempRoot(*i);
230 if (!isValidPath(*i)) continue; /* path was GC'ed, probably */
231 startNest(nest, lvlChatty, format("hashing files in `%1%'") % *i);
15ddeff5 232 optimisePath_(stats, *i, inodeHash);
36457566
LC
233 }
234}
235
2bb04905
LC
236static string showBytes(unsigned long long bytes)
237{
238 return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
239}
240
241void LocalStore::optimiseStore()
242{
243 OptimiseStats stats;
244
245 optimiseStore(stats);
246
247 printMsg(lvlError,
248 format("%1% freed by hard-linking %2% files")
249 % showBytes(stats.bytesFreed)
250 % stats.filesLinked);
251}
36457566
LC
252
253void LocalStore::optimisePath(const Path & path)
254{
255 OptimiseStats stats;
15ddeff5
LC
256 InodeHash inodeHash;
257
258 if (settings.autoOptimiseStore) optimisePath_(stats, path, inodeHash);
36457566
LC
259}
260
261
262}