2008-06-09 15:52:45 +02:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
#include "local-store.hh"
|
2012-07-23 18:08:34 +02:00
|
|
|
|
#include "globals.hh"
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2014-12-14 01:51:14 +01:00
|
|
|
|
#include <cstdlib>
|
2016-11-14 13:37:16 +01:00
|
|
|
|
#include <cstring>
|
2008-06-09 15:52:45 +02:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2008-06-18 16:13:00 +02:00
|
|
|
|
#include <errno.h>
|
2009-09-24 09:39:55 +02:00
|
|
|
|
#include <stdio.h>
|
2017-07-06 22:42:12 +02:00
|
|
|
|
#include <regex>
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void makeWritable(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st))
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError(format("getting attributes of path '%1%'") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError(format("changing writability of '%1%'") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 16:13:00 +02:00
|
|
|
|
struct MakeReadOnly
|
|
|
|
|
{
|
|
|
|
|
Path path;
|
|
|
|
|
MakeReadOnly(const Path & path) : path(path) { }
|
|
|
|
|
~MakeReadOnly()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2012-09-19 22:17:54 +02:00
|
|
|
|
/* This will make the path read-only. */
|
2013-03-08 01:24:59 +01:00
|
|
|
|
if (path != "") canonicaliseTimestampAndPermissions(path);
|
2008-06-18 16:13:00 +02:00
|
|
|
|
} catch (...) {
|
|
|
|
|
ignoreException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-15 11:33:46 +02:00
|
|
|
|
|
2014-05-14 22:52:10 +02:00
|
|
|
|
LocalStore::InodeHash LocalStore::loadInodeHash()
|
2014-05-13 23:10:06 +02:00
|
|
|
|
{
|
2016-09-21 16:11:01 +02:00
|
|
|
|
debug("loading hash inodes in memory");
|
2014-05-15 09:02:22 +02:00
|
|
|
|
InodeHash inodeHash;
|
2014-05-14 22:52:10 +02:00
|
|
|
|
|
2017-01-16 22:39:27 +01:00
|
|
|
|
AutoCloseDir dir(opendir(linksDir.c_str()));
|
2017-07-30 13:27:57 +02:00
|
|
|
|
if (!dir) throw SysError(format("opening directory '%1%'") % linksDir);
|
2014-05-14 22:52:10 +02:00
|
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
2017-01-16 22:39:27 +01:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) { /* sic */
|
2014-05-14 22:52:10 +02:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
// We don't care if we hit non-hash files, anything goes
|
2014-05-15 09:02:22 +02:00
|
|
|
|
inodeHash.insert(dirent->d_ino);
|
2014-05-13 23:10:06 +02:00
|
|
|
|
}
|
2017-07-30 13:27:57 +02:00
|
|
|
|
if (errno) throw SysError(format("reading directory '%1%'") % linksDir);
|
2014-05-14 22:52:10 +02:00
|
|
|
|
|
2014-05-15 11:37:44 +02:00
|
|
|
|
printMsg(lvlTalkative, format("loaded %1% hash inodes") % inodeHash.size());
|
2014-05-14 22:52:10 +02:00
|
|
|
|
|
2014-05-15 09:02:22 +02:00
|
|
|
|
return inodeHash;
|
2014-05-13 23:10:06 +02:00
|
|
|
|
}
|
2008-06-18 16:13:00 +02:00
|
|
|
|
|
2014-05-15 11:33:46 +02:00
|
|
|
|
|
|
|
|
|
Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash)
|
2014-05-15 09:02:22 +02:00
|
|
|
|
{
|
|
|
|
|
Strings names;
|
|
|
|
|
|
2017-01-16 22:39:27 +01:00
|
|
|
|
AutoCloseDir dir(opendir(path.c_str()));
|
2017-07-30 13:27:57 +02:00
|
|
|
|
if (!dir) throw SysError(format("opening directory '%1%'") % path);
|
2014-05-15 09:02:22 +02:00
|
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
2017-01-16 22:39:27 +01:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) { /* sic */
|
2014-05-15 09:02:22 +02:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
|
|
if (inodeHash.count(dirent->d_ino)) {
|
2017-07-30 13:27:57 +02:00
|
|
|
|
debug(format("'%1%' is already linked") % dirent->d_name);
|
2014-05-15 09:02:22 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
|
names.push_back(name);
|
|
|
|
|
}
|
2017-07-30 13:27:57 +02:00
|
|
|
|
if (errno) throw SysError(format("reading directory '%1%'") % path);
|
2014-05-15 09:02:22 +02:00
|
|
|
|
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 11:33:46 +02:00
|
|
|
|
|
2017-08-16 17:32:18 +02:00
|
|
|
|
void LocalStore::optimisePath_(Activity * act, OptimiseStats & stats,
|
|
|
|
|
const Path & path, InodeHash & inodeHash)
|
2008-06-09 15:52:45 +02:00
|
|
|
|
{
|
2012-08-01 22:06:49 +02:00
|
|
|
|
checkInterrupt();
|
2014-05-15 11:19:16 +02:00
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st))
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError(format("getting attributes of path '%1%'") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2017-07-06 22:42:12 +02:00
|
|
|
|
#if __APPLE__
|
2017-07-30 12:28:50 +02:00
|
|
|
|
/* HFS/macOS has some undocumented security feature disabling hardlinking for
|
2017-07-06 22:42:12 +02:00
|
|
|
|
special files within .app dirs. *.app/Contents/PkgInfo and
|
|
|
|
|
*.app/Contents/Resources/\*.lproj seem to be the only paths affected. See
|
|
|
|
|
https://github.com/NixOS/nix/issues/1443 for more discussion. */
|
|
|
|
|
|
|
|
|
|
if (std::regex_search(path, std::regex("\\.app/Contents/PkgInfo$")) ||
|
2017-08-10 13:51:07 +02:00
|
|
|
|
std::regex_search(path, std::regex("\\.app/Contents/Resources/.+\\.lproj$")))
|
|
|
|
|
{
|
2017-07-31 10:31:51 +02:00
|
|
|
|
debug(format("'%1%' is not allowed to be linked in macOS") % path);
|
2017-07-06 22:42:12 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2014-05-15 11:33:46 +02:00
|
|
|
|
Strings names = readDirectoryIgnoringInodes(path, inodeHash);
|
2015-07-17 19:24:28 +02:00
|
|
|
|
for (auto & i : names)
|
2017-08-16 17:32:18 +02:00
|
|
|
|
optimisePath_(act, stats, path + "/" + i, inodeHash);
|
2012-07-23 18:08:34 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-07-24 00:42:18 +02:00
|
|
|
|
/* We can hard link regular files and maybe symlinks. */
|
|
|
|
|
if (!S_ISREG(st.st_mode)
|
|
|
|
|
#if CAN_LINK_SYMLINK
|
|
|
|
|
&& !S_ISLNK(st.st_mode)
|
|
|
|
|
#endif
|
|
|
|
|
) return;
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
|
/* Sometimes SNAFUs can cause files in the Nix store to be
|
|
|
|
|
modified, in particular when running programs as root under
|
|
|
|
|
NixOS (example: $fontconfig/var/cache being modified). Skip
|
2012-07-23 18:08:34 +02:00
|
|
|
|
those files. FIXME: check the modification time. */
|
2008-06-09 15:52:45 +02:00
|
|
|
|
if (S_ISREG(st.st_mode) && (st.st_mode & S_IWUSR)) {
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printError(format("skipping suspicious writable file '%1%'") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 20:48:09 +01:00
|
|
|
|
/* This can still happen on top-level files. */
|
2014-05-15 09:02:22 +02:00
|
|
|
|
if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) {
|
2017-07-30 13:27:57 +02:00
|
|
|
|
debug(format("'%1%' is already linked, with %2% other file(s)") % path % (st.st_nlink - 2));
|
2014-05-10 15:53:01 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
/* Hash the file. Note that hashPath() returns the hash over the
|
|
|
|
|
NAR serialisation, which includes the execute bit on the file.
|
|
|
|
|
Thus, executable and non-executable files with the same
|
|
|
|
|
contents *won't* be linked (which is good because otherwise the
|
|
|
|
|
permissions would be screwed up).
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
Also note that if `path' is a symlink, then we're hashing the
|
|
|
|
|
contents of the symlink (i.e. the result of readlink()), not
|
|
|
|
|
the contents of the target (which may not even exist). */
|
|
|
|
|
Hash hash = hashPath(htSHA256, path).first;
|
2017-07-30 13:27:57 +02:00
|
|
|
|
debug(format("'%1%' has hash '%2%'") % path % hash.to_string());
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
/* Check if this is a known hash. */
|
2017-07-04 14:47:59 +02:00
|
|
|
|
Path linkPath = linksDir + "/" + hash.to_string(Base32, false);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2015-11-09 20:48:09 +01:00
|
|
|
|
retry:
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (!pathExists(linkPath)) {
|
|
|
|
|
/* Nope, create a hard link in the links directory. */
|
2014-05-13 23:10:06 +02:00
|
|
|
|
if (link(path.c_str(), linkPath.c_str()) == 0) {
|
2014-05-15 09:02:22 +02:00
|
|
|
|
inodeHash.insert(st.st_ino);
|
2014-05-13 23:10:06 +02:00
|
|
|
|
return;
|
2014-05-15 11:19:16 +02:00
|
|
|
|
}
|
2016-11-14 13:33:36 +01:00
|
|
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
|
case EEXIST:
|
2016-11-26 00:37:43 +01:00
|
|
|
|
/* Fall through if another process created ‘linkPath’ before
|
2016-11-14 13:33:36 +01:00
|
|
|
|
we did. */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ENOSPC:
|
|
|
|
|
/* On ext4, that probably means the directory index is
|
|
|
|
|
full. When that happens, it's fine to ignore it: we
|
|
|
|
|
just effectively disable deduplication of this
|
|
|
|
|
file. */
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printInfo("cannot link '%s' to '%s': %s", linkPath, path, strerror(errno));
|
2016-11-14 13:33:36 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
default:
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError("cannot link '%1%' to '%2%'", linkPath, path);
|
2016-11-14 13:33:36 +01:00
|
|
|
|
}
|
2012-07-23 18:08:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Yes! We've seen a file with the same contents. Replace the
|
|
|
|
|
current file with a hard link to that file. */
|
|
|
|
|
struct stat stLink;
|
|
|
|
|
if (lstat(linkPath.c_str(), &stLink))
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError(format("getting attributes of path '%1%'") % linkPath);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (st.st_ino == stLink.st_ino) {
|
2017-07-30 13:27:57 +02:00
|
|
|
|
debug(format("'%1%' is already linked to '%2%'") % path % linkPath);
|
2012-07-23 18:08:34 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2015-11-09 20:48:09 +01:00
|
|
|
|
if (st.st_size != stLink.st_size) {
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printError(format("removing corrupted link '%1%'") % linkPath);
|
2015-11-09 20:48:09 +01:00
|
|
|
|
unlink(linkPath.c_str());
|
|
|
|
|
goto retry;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printMsg(lvlTalkative, format("linking '%1%' to '%2%'") % path % linkPath);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
/* Make the containing directory writable, but only if it's not
|
|
|
|
|
the store itself (we don't want or need to mess with its
|
|
|
|
|
permissions). */
|
2016-06-02 15:08:18 +02:00
|
|
|
|
bool mustToggle = dirOf(path) != realStoreDir;
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (mustToggle) makeWritable(dirOf(path));
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
/* When we're done, make the directory read-only again and reset
|
|
|
|
|
its timestamp back to 0. */
|
|
|
|
|
MakeReadOnly makeReadOnly(mustToggle ? dirOf(path) : "");
|
|
|
|
|
|
2012-09-19 22:17:54 +02:00
|
|
|
|
Path tempLink = (format("%1%/.tmp-link-%2%-%3%")
|
2018-03-07 00:34:44 +01:00
|
|
|
|
% realStoreDir % getpid() % random()).str();
|
2012-09-19 22:17:54 +02:00
|
|
|
|
|
|
|
|
|
if (link(linkPath.c_str(), tempLink.c_str()) == -1) {
|
|
|
|
|
if (errno == EMLINK) {
|
|
|
|
|
/* Too many links to the same file (>= 32000 on most file
|
|
|
|
|
systems). This is likely to happen with empty files.
|
|
|
|
|
Just shrug and ignore. */
|
|
|
|
|
if (st.st_size)
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printInfo(format("'%1%' has maximum number of links") % linkPath);
|
2012-09-19 22:17:54 +02:00
|
|
|
|
return;
|
2012-07-23 18:08:34 +02:00
|
|
|
|
}
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError("cannot link '%1%' to '%2%'", tempLink, linkPath);
|
2012-09-19 22:17:54 +02:00
|
|
|
|
}
|
2012-07-23 18:08:34 +02:00
|
|
|
|
|
2012-09-19 22:17:54 +02:00
|
|
|
|
/* Atomically replace the old file with the new hard link. */
|
|
|
|
|
if (rename(tempLink.c_str(), path.c_str()) == -1) {
|
|
|
|
|
if (unlink(tempLink.c_str()) == -1)
|
2017-07-30 13:27:57 +02:00
|
|
|
|
printError(format("unable to unlink '%1%'") % tempLink);
|
2012-09-19 22:17:54 +02:00
|
|
|
|
if (errno == EMLINK) {
|
|
|
|
|
/* Some filesystems generate too many links on the rename,
|
|
|
|
|
rather than on the original link. (Probably it
|
|
|
|
|
temporarily increases the st_nlink field before
|
|
|
|
|
decreasing it again.) */
|
2017-07-30 13:27:57 +02:00
|
|
|
|
debug("'%s' has reached maximum number of links", linkPath);
|
2012-09-19 22:17:54 +02:00
|
|
|
|
return;
|
2012-07-23 18:08:34 +02:00
|
|
|
|
}
|
2017-07-30 13:27:57 +02:00
|
|
|
|
throw SysError(format("cannot rename '%1%' to '%2%'") % tempLink % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
2012-07-23 18:08:34 +02:00
|
|
|
|
|
|
|
|
|
stats.filesLinked++;
|
|
|
|
|
stats.bytesFreed += st.st_size;
|
|
|
|
|
stats.blocksFreed += st.st_blocks;
|
2017-08-16 17:32:18 +02:00
|
|
|
|
|
|
|
|
|
if (act)
|
|
|
|
|
act->result(resFileLinked, st.st_size, st.st_blocks);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
void LocalStore::optimiseStore(OptimiseStats & stats)
|
2008-06-09 15:52:45 +02:00
|
|
|
|
{
|
2017-08-16 17:00:24 +02:00
|
|
|
|
Activity act(*logger, actOptimiseStore);
|
|
|
|
|
|
2012-07-11 16:49:04 +02:00
|
|
|
|
PathSet paths = queryAllValidPaths();
|
2014-05-14 22:52:10 +02:00
|
|
|
|
InodeHash inodeHash = loadInodeHash();
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2017-08-16 17:00:24 +02:00
|
|
|
|
act.progress(0, paths.size());
|
|
|
|
|
|
|
|
|
|
uint64_t done = 0;
|
|
|
|
|
|
2015-07-17 19:24:28 +02:00
|
|
|
|
for (auto & i : paths) {
|
|
|
|
|
addTempRoot(i);
|
|
|
|
|
if (!isValidPath(i)) continue; /* path was GC'ed, probably */
|
2017-08-16 17:00:24 +02:00
|
|
|
|
{
|
2017-08-28 19:13:24 +02:00
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("optimising path '%s'", i));
|
2017-08-16 17:32:18 +02:00
|
|
|
|
optimisePath_(&act, stats, realStoreDir + "/" + baseNameOf(i), inodeHash);
|
2017-08-16 17:00:24 +02:00
|
|
|
|
}
|
|
|
|
|
done++;
|
|
|
|
|
act.progress(done, paths.size());
|
2012-07-23 21:02:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 22:21:42 +02:00
|
|
|
|
static string showBytes(unsigned long long bytes)
|
|
|
|
|
{
|
|
|
|
|
return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LocalStore::optimiseStore()
|
|
|
|
|
{
|
|
|
|
|
OptimiseStats stats;
|
|
|
|
|
|
|
|
|
|
optimiseStore(stats);
|
|
|
|
|
|
2017-08-16 17:32:18 +02:00
|
|
|
|
printInfo(
|
2014-09-01 22:21:42 +02:00
|
|
|
|
format("%1% freed by hard-linking %2% files")
|
|
|
|
|
% showBytes(stats.bytesFreed)
|
|
|
|
|
% stats.filesLinked);
|
|
|
|
|
}
|
2012-07-23 21:02:52 +02:00
|
|
|
|
|
|
|
|
|
void LocalStore::optimisePath(const Path & path)
|
|
|
|
|
{
|
2012-07-31 01:55:41 +02:00
|
|
|
|
OptimiseStats stats;
|
2014-05-14 22:52:10 +02:00
|
|
|
|
InodeHash inodeHash;
|
2014-05-13 23:10:06 +02:00
|
|
|
|
|
2017-08-16 17:32:18 +02:00
|
|
|
|
if (settings.autoOptimiseStore) optimisePath_(nullptr, stats, path, inodeHash);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|