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