2008-06-09 16:52:45 +03:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
#include "local-store.hh"
|
2012-07-23 19:08:34 +03:00
|
|
|
|
#include "globals.hh"
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2014-12-14 02:51:14 +02:00
|
|
|
|
#include <cstdlib>
|
2016-11-14 14:37:16 +02:00
|
|
|
|
#include <cstring>
|
2008-06-09 16:52:45 +03:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2008-06-18 17:13:00 +03:00
|
|
|
|
#include <errno.h>
|
2009-09-24 10:39:55 +03:00
|
|
|
|
#include <stdio.h>
|
2017-07-06 23:42:12 +03:00
|
|
|
|
#include <regex>
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void makeWritable(const Path & path)
|
|
|
|
|
{
|
2020-09-23 20:17:28 +03:00
|
|
|
|
auto st = lstat(path);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
|
2020-04-22 02:07:07 +03:00
|
|
|
|
throw SysError("changing writability of '%1%'", path);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 17:13:00 +03:00
|
|
|
|
struct MakeReadOnly
|
|
|
|
|
{
|
|
|
|
|
Path path;
|
|
|
|
|
MakeReadOnly(const Path & path) : path(path) { }
|
|
|
|
|
~MakeReadOnly()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2012-09-19 23:17:54 +03:00
|
|
|
|
/* This will make the path read-only. */
|
2013-03-08 02:24:59 +02:00
|
|
|
|
if (path != "") canonicaliseTimestampAndPermissions(path);
|
2008-06-18 17:13:00 +03:00
|
|
|
|
} catch (...) {
|
|
|
|
|
ignoreException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-15 12:33:46 +03:00
|
|
|
|
|
2014-05-14 23:52:10 +03:00
|
|
|
|
LocalStore::InodeHash LocalStore::loadInodeHash()
|
2014-05-14 00:10:06 +03:00
|
|
|
|
{
|
2016-09-21 17:11:01 +03:00
|
|
|
|
debug("loading hash inodes in memory");
|
2014-05-15 10:02:22 +03:00
|
|
|
|
InodeHash inodeHash;
|
2014-05-14 23:52:10 +03:00
|
|
|
|
|
2017-01-16 23:39:27 +02:00
|
|
|
|
AutoCloseDir dir(opendir(linksDir.c_str()));
|
2020-04-22 02:07:07 +03:00
|
|
|
|
if (!dir) throw SysError("opening directory '%1%'", linksDir);
|
2014-05-14 23:52:10 +03:00
|
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
2017-01-16 23:39:27 +02:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) { /* sic */
|
2014-05-14 23:52:10 +03:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
// We don't care if we hit non-hash files, anything goes
|
2014-05-15 10:02:22 +03:00
|
|
|
|
inodeHash.insert(dirent->d_ino);
|
2014-05-14 00:10:06 +03:00
|
|
|
|
}
|
2020-04-22 02:07:07 +03:00
|
|
|
|
if (errno) throw SysError("reading directory '%1%'", linksDir);
|
2014-05-14 23:52:10 +03:00
|
|
|
|
|
2014-05-15 12:37:44 +03:00
|
|
|
|
printMsg(lvlTalkative, format("loaded %1% hash inodes") % inodeHash.size());
|
2014-05-14 23:52:10 +03:00
|
|
|
|
|
2014-05-15 10:02:22 +03:00
|
|
|
|
return inodeHash;
|
2014-05-14 00:10:06 +03:00
|
|
|
|
}
|
2008-06-18 17:13:00 +03:00
|
|
|
|
|
2014-05-15 12:33:46 +03:00
|
|
|
|
|
|
|
|
|
Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash)
|
2014-05-15 10:02:22 +03:00
|
|
|
|
{
|
|
|
|
|
Strings names;
|
|
|
|
|
|
2017-01-16 23:39:27 +02:00
|
|
|
|
AutoCloseDir dir(opendir(path.c_str()));
|
2020-04-22 02:07:07 +03:00
|
|
|
|
if (!dir) throw SysError("opening directory '%1%'", path);
|
2014-05-15 10:02:22 +03:00
|
|
|
|
|
|
|
|
|
struct dirent * dirent;
|
2017-01-16 23:39:27 +02:00
|
|
|
|
while (errno = 0, dirent = readdir(dir.get())) { /* sic */
|
2014-05-15 10:02:22 +03:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
|
|
if (inodeHash.count(dirent->d_ino)) {
|
2017-07-30 14:27:57 +03:00
|
|
|
|
debug(format("'%1%' is already linked") % dirent->d_name);
|
2014-05-15 10:02:22 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string name = dirent->d_name;
|
|
|
|
|
if (name == "." || name == "..") continue;
|
|
|
|
|
names.push_back(name);
|
|
|
|
|
}
|
2020-04-22 02:07:07 +03:00
|
|
|
|
if (errno) throw SysError("reading directory '%1%'", path);
|
2014-05-15 10:02:22 +03:00
|
|
|
|
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 12:33:46 +03:00
|
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
|
void LocalStore::optimisePath_(Activity * act, OptimiseStats & stats,
|
|
|
|
|
const Path & path, InodeHash & inodeHash)
|
2008-06-09 16:52:45 +03:00
|
|
|
|
{
|
2012-08-01 23:06:49 +03:00
|
|
|
|
checkInterrupt();
|
2014-05-15 12:19:16 +03:00
|
|
|
|
|
2020-09-23 20:17:28 +03:00
|
|
|
|
auto st = lstat(path);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2017-07-06 23:42:12 +03:00
|
|
|
|
#if __APPLE__
|
2017-07-30 13:28:50 +03:00
|
|
|
|
/* HFS/macOS has some undocumented security feature disabling hardlinking for
|
2017-07-06 23:42:12 +03: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. */
|
|
|
|
|
|
2018-06-14 19:34:55 +03:00
|
|
|
|
if (std::regex_search(path, std::regex("\\.app/Contents/.+$")))
|
2017-08-10 14:51:07 +03:00
|
|
|
|
{
|
2017-07-31 11:31:51 +03:00
|
|
|
|
debug(format("'%1%' is not allowed to be linked in macOS") % path);
|
2017-07-06 23:42:12 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-07-23 19:08:34 +03:00
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2014-05-15 12:33:46 +03:00
|
|
|
|
Strings names = readDirectoryIgnoringInodes(path, inodeHash);
|
2015-07-17 20:24:28 +03:00
|
|
|
|
for (auto & i : names)
|
2017-08-16 18:32:18 +03:00
|
|
|
|
optimisePath_(act, stats, path + "/" + i, inodeHash);
|
2012-07-23 19:08:34 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
|
2012-07-24 01:42:18 +03: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 02:55:41 +03:00
|
|
|
|
|
2008-06-09 16:52:45 +03: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 19:08:34 +03:00
|
|
|
|
those files. FIXME: check the modification time. */
|
2008-06-09 16:52:45 +03:00
|
|
|
|
if (S_ISREG(st.st_mode) && (st.st_mode & S_IWUSR)) {
|
2021-01-21 01:27:36 +02:00
|
|
|
|
warn("skipping suspicious writable file '%1%'", path);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-09 21:48:09 +02:00
|
|
|
|
/* This can still happen on top-level files. */
|
2014-05-15 10:02:22 +03:00
|
|
|
|
if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) {
|
2021-01-21 01:27:36 +02:00
|
|
|
|
debug("'%s' is already linked, with %d other file(s)", path, st.st_nlink - 2);
|
2014-05-10 16:53:01 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 19:08:34 +03: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 16:52:45 +03:00
|
|
|
|
|
2012-07-23 19:08:34 +03: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;
|
2020-06-03 13:38:23 +03:00
|
|
|
|
debug(format("'%1%' has hash '%2%'") % path % hash.to_string(Base32, true));
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2012-07-23 19:08:34 +03:00
|
|
|
|
/* Check if this is a known hash. */
|
2017-07-04 15:47:59 +03:00
|
|
|
|
Path linkPath = linksDir + "/" + hash.to_string(Base32, false);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2015-11-09 21:48:09 +02:00
|
|
|
|
retry:
|
2012-07-23 19:08:34 +03:00
|
|
|
|
if (!pathExists(linkPath)) {
|
|
|
|
|
/* Nope, create a hard link in the links directory. */
|
2014-05-14 00:10:06 +03:00
|
|
|
|
if (link(path.c_str(), linkPath.c_str()) == 0) {
|
2014-05-15 10:02:22 +03:00
|
|
|
|
inodeHash.insert(st.st_ino);
|
2014-05-14 00:10:06 +03:00
|
|
|
|
return;
|
2014-05-15 12:19:16 +03:00
|
|
|
|
}
|
2016-11-14 14:33:36 +02:00
|
|
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
|
case EEXIST:
|
2016-11-26 01:37:43 +02:00
|
|
|
|
/* Fall through if another process created ‘linkPath’ before
|
2016-11-14 14:33:36 +02: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 14:27:57 +03:00
|
|
|
|
printInfo("cannot link '%s' to '%s': %s", linkPath, path, strerror(errno));
|
2016-11-14 14:33:36 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
default:
|
2017-07-30 14:27:57 +03:00
|
|
|
|
throw SysError("cannot link '%1%' to '%2%'", linkPath, path);
|
2016-11-14 14:33:36 +02:00
|
|
|
|
}
|
2012-07-23 19:08:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Yes! We've seen a file with the same contents. Replace the
|
|
|
|
|
current file with a hard link to that file. */
|
2020-09-23 20:17:28 +03:00
|
|
|
|
auto stLink = lstat(linkPath);
|
2012-07-31 02:55:41 +03:00
|
|
|
|
|
2012-07-23 19:08:34 +03:00
|
|
|
|
if (st.st_ino == stLink.st_ino) {
|
2017-07-30 14:27:57 +03:00
|
|
|
|
debug(format("'%1%' is already linked to '%2%'") % path % linkPath);
|
2012-07-23 19:08:34 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2012-07-31 02:55:41 +03:00
|
|
|
|
|
2015-11-09 21:48:09 +02:00
|
|
|
|
if (st.st_size != stLink.st_size) {
|
2021-01-21 01:27:36 +02:00
|
|
|
|
warn("removing corrupted link '%s'", linkPath);
|
2015-11-09 21:48:09 +02:00
|
|
|
|
unlink(linkPath.c_str());
|
|
|
|
|
goto retry;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 14:27:57 +03:00
|
|
|
|
printMsg(lvlTalkative, format("linking '%1%' to '%2%'") % path % linkPath);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2012-07-23 19:08:34 +03: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 16:08:18 +03:00
|
|
|
|
bool mustToggle = dirOf(path) != realStoreDir;
|
2012-07-23 19:08:34 +03:00
|
|
|
|
if (mustToggle) makeWritable(dirOf(path));
|
2012-07-31 02:55:41 +03:00
|
|
|
|
|
2012-07-23 19:08:34 +03: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 23:17:54 +03:00
|
|
|
|
Path tempLink = (format("%1%/.tmp-link-%2%-%3%")
|
2018-03-07 01:34:44 +02:00
|
|
|
|
% realStoreDir % getpid() % random()).str();
|
2012-09-19 23:17:54 +03: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 14:27:57 +03:00
|
|
|
|
printInfo(format("'%1%' has maximum number of links") % linkPath);
|
2012-09-19 23:17:54 +03:00
|
|
|
|
return;
|
2012-07-23 19:08:34 +03:00
|
|
|
|
}
|
2017-07-30 14:27:57 +03:00
|
|
|
|
throw SysError("cannot link '%1%' to '%2%'", tempLink, linkPath);
|
2012-09-19 23:17:54 +03:00
|
|
|
|
}
|
2012-07-23 19:08:34 +03:00
|
|
|
|
|
2012-09-19 23:17:54 +03: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)
|
2021-01-21 01:27:36 +02:00
|
|
|
|
printError("unable to unlink '%1%'", tempLink);
|
2012-09-19 23:17:54 +03: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 14:27:57 +03:00
|
|
|
|
debug("'%s' has reached maximum number of links", linkPath);
|
2012-09-19 23:17:54 +03:00
|
|
|
|
return;
|
2012-07-23 19:08:34 +03:00
|
|
|
|
}
|
2020-04-22 02:07:07 +03:00
|
|
|
|
throw SysError("cannot rename '%1%' to '%2%'", tempLink, path);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
}
|
2012-07-23 19:08:34 +03:00
|
|
|
|
|
|
|
|
|
stats.filesLinked++;
|
|
|
|
|
stats.bytesFreed += st.st_size;
|
|
|
|
|
stats.blocksFreed += st.st_blocks;
|
2017-08-16 18:32:18 +03:00
|
|
|
|
|
|
|
|
|
if (act)
|
|
|
|
|
act->result(resFileLinked, st.st_size, st.st_blocks);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-23 19:08:34 +03:00
|
|
|
|
void LocalStore::optimiseStore(OptimiseStats & stats)
|
2008-06-09 16:52:45 +03:00
|
|
|
|
{
|
2017-08-16 18:00:24 +03:00
|
|
|
|
Activity act(*logger, actOptimiseStore);
|
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
|
auto paths = queryAllValidPaths();
|
2014-05-14 23:52:10 +03:00
|
|
|
|
InodeHash inodeHash = loadInodeHash();
|
2008-06-09 16:52:45 +03:00
|
|
|
|
|
2017-08-16 18:00:24 +03:00
|
|
|
|
act.progress(0, paths.size());
|
|
|
|
|
|
|
|
|
|
uint64_t done = 0;
|
|
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
|
for (auto & i : paths) {
|
|
|
|
|
addTempRoot(i);
|
|
|
|
|
if (!isValidPath(i)) continue; /* path was GC'ed, probably */
|
2017-08-16 18:00:24 +03:00
|
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("optimising path '%s'", printStorePath(i)));
|
|
|
|
|
optimisePath_(&act, stats, realStoreDir + "/" + std::string(i.to_string()), inodeHash);
|
2017-08-16 18:00:24 +03:00
|
|
|
|
}
|
|
|
|
|
done++;
|
|
|
|
|
act.progress(done, paths.size());
|
2012-07-23 22:02:52 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 23:21:42 +03:00
|
|
|
|
void LocalStore::optimiseStore()
|
|
|
|
|
{
|
|
|
|
|
OptimiseStats stats;
|
|
|
|
|
|
|
|
|
|
optimiseStore(stats);
|
|
|
|
|
|
2020-10-06 11:40:49 +03:00
|
|
|
|
printInfo("%s freed by hard-linking %d files",
|
|
|
|
|
showBytes(stats.bytesFreed),
|
|
|
|
|
stats.filesLinked);
|
2014-09-01 23:21:42 +03:00
|
|
|
|
}
|
2012-07-23 22:02:52 +03:00
|
|
|
|
|
|
|
|
|
void LocalStore::optimisePath(const Path & path)
|
|
|
|
|
{
|
2012-07-31 02:55:41 +03:00
|
|
|
|
OptimiseStats stats;
|
2014-05-14 23:52:10 +03:00
|
|
|
|
InodeHash inodeHash;
|
2014-05-14 00:10:06 +03:00
|
|
|
|
|
2017-08-16 18:32:18 +03:00
|
|
|
|
if (settings.autoOptimiseStore) optimisePath_(nullptr, stats, path, inodeHash);
|
2008-06-09 16:52:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|