2012-08-01 22:09:47 +02:00
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
|
#include "util.hh"
|
|
|
|
|
#include "local-store.hh"
|
2012-02-15 01:31:56 +01:00
|
|
|
|
#include "immutable.hh"
|
2012-07-23 18:08:34 +02:00
|
|
|
|
#include "globals.hh"
|
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>
|
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))
|
2012-07-31 01:55:41 +02:00
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
2012-02-15 01:31:56 +01:00
|
|
|
|
if (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode)) makeMutable(path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1)
|
|
|
|
|
throw SysError(format("changing writability of `%1%'") % path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-18 16:13:00 +02:00
|
|
|
|
struct MakeReadOnly
|
|
|
|
|
{
|
|
|
|
|
Path path;
|
|
|
|
|
MakeReadOnly(const Path & path) : path(path) { }
|
|
|
|
|
~MakeReadOnly()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2012-02-15 01:31:56 +01:00
|
|
|
|
/* This will make the path read-only (and restore the
|
|
|
|
|
immutable bit on platforms that support it). */
|
2008-06-18 16:13:00 +02:00
|
|
|
|
if (path != "") canonicalisePathMetaData(path, false);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
ignoreException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-02-15 01:31:56 +01:00
|
|
|
|
struct MakeImmutable
|
|
|
|
|
{
|
|
|
|
|
Path path;
|
|
|
|
|
MakeImmutable(const Path & path) : path(path) { }
|
|
|
|
|
~MakeImmutable() { makeImmutable(path); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-07-23 21:02:52 +02:00
|
|
|
|
void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path)
|
2008-06-09 15:52:45 +02:00
|
|
|
|
{
|
2012-08-01 22:06:49 +02:00
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st))
|
2012-07-31 01:55:41 +02:00
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
|
Strings names = readDirectory(path);
|
2012-07-31 01:55:41 +02:00
|
|
|
|
foreach (Strings::iterator, i, names)
|
|
|
|
|
optimisePath_(stats, path + "/" + *i);
|
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)) {
|
|
|
|
|
printMsg(lvlError, format("skipping suspicious writable file `%1%'") % path);
|
|
|
|
|
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;
|
|
|
|
|
stats.totalFiles++;
|
|
|
|
|
printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash));
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
/* Check if this is a known hash. */
|
2012-07-23 21:02:52 +02:00
|
|
|
|
Path linkPath = linksDir + "/" + printHash32(hash);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
if (!pathExists(linkPath)) {
|
|
|
|
|
/* Nope, create a hard link in the links directory. */
|
|
|
|
|
makeMutable(path);
|
|
|
|
|
MakeImmutable mk1(path);
|
2012-08-06 00:17:55 +02:00
|
|
|
|
if (link(path.c_str(), linkPath.c_str()) == 0) return;
|
|
|
|
|
if (errno != EEXIST)
|
2012-07-23 18:08:34 +02:00
|
|
|
|
throw SysError(format("cannot link `%1%' to `%2%'") % linkPath % path);
|
2012-08-06 00:17:55 +02:00
|
|
|
|
/* Fall through if another process created ‘linkPath’ before
|
|
|
|
|
we did. */
|
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))
|
2012-07-31 01:55:41 +02:00
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % linkPath);
|
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
stats.sameContents++;
|
|
|
|
|
if (st.st_ino == stLink.st_ino) {
|
|
|
|
|
printMsg(lvlDebug, format("`%1%' is already linked to `%2%'") % path % linkPath);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-07-31 01:55:41 +02:00
|
|
|
|
|
2012-07-23 18:08:34 +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). */
|
|
|
|
|
bool mustToggle = !isStorePath(path);
|
|
|
|
|
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) : "");
|
|
|
|
|
|
|
|
|
|
/* If ‘linkPath’ is immutable, we can't create hard links to it,
|
|
|
|
|
so make it mutable first (and make it immutable again when
|
|
|
|
|
we're done). We also have to make ‘path’ mutable, otherwise
|
|
|
|
|
rename() will fail to delete it. */
|
|
|
|
|
makeMutable(path);
|
|
|
|
|
MakeImmutable mk2(path);
|
|
|
|
|
|
2012-08-06 03:41:44 +02:00
|
|
|
|
/* Another process might be doing the same thing (creating a new
|
|
|
|
|
link to ‘linkPath’) and make ‘linkPath’ immutable before we're
|
|
|
|
|
done. In that case, just retry. */
|
|
|
|
|
unsigned int retries = 1024;
|
|
|
|
|
while (--retries > 0) {
|
|
|
|
|
makeMutable(linkPath);
|
|
|
|
|
MakeImmutable mk1(linkPath);
|
|
|
|
|
|
|
|
|
|
Path tempLink = (format("%1%/.tmp-link-%2%-%3%")
|
2012-08-27 17:09:07 +02:00
|
|
|
|
% settings.nixStore % getpid() % rand()).str();
|
2012-08-06 03:41:44 +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. */
|
2012-08-07 22:22:54 +02:00
|
|
|
|
if (st.st_size)
|
|
|
|
|
printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
|
2012-08-06 03:41:44 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (errno == EPERM) continue;
|
|
|
|
|
throw SysError(format("cannot link `%1%' to `%2%'") % tempLink % linkPath);
|
2012-07-23 18:08:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-06 03:41:44 +02:00
|
|
|
|
/* Atomically replace the old file with the new hard link. */
|
|
|
|
|
if (rename(tempLink.c_str(), path.c_str()) == -1) {
|
2012-07-24 00:06:37 +02:00
|
|
|
|
if (unlink(tempLink.c_str()) == -1)
|
|
|
|
|
printMsg(lvlError, format("unable to unlink `%1%'") % tempLink);
|
2012-08-06 03:41:44 +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.) */
|
2012-08-07 22:22:54 +02:00
|
|
|
|
if (st.st_size)
|
|
|
|
|
printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);
|
2012-08-06 03:41:44 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (errno == EPERM) continue;
|
|
|
|
|
throw SysError(format("cannot rename `%1%' to `%2%'") % tempLink % path);
|
2012-07-23 18:08:34 +02:00
|
|
|
|
}
|
2012-08-06 03:41:44 +02:00
|
|
|
|
|
|
|
|
|
break;
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
2012-07-23 18:08:34 +02:00
|
|
|
|
|
2012-08-06 03:41:44 +02:00
|
|
|
|
if (retries == 0) throw Error(format("cannot link `%1%' to `%2%'") % path % linkPath);
|
|
|
|
|
|
2012-07-23 18:08:34 +02:00
|
|
|
|
stats.filesLinked++;
|
|
|
|
|
stats.bytesFreed += st.st_size;
|
|
|
|
|
stats.blocksFreed += 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
|
|
|
|
{
|
2012-07-11 16:49:04 +02:00
|
|
|
|
PathSet paths = queryAllValidPaths();
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
2009-04-21 13:52:16 +02:00
|
|
|
|
foreach (PathSet::iterator, i, paths) {
|
2008-06-09 15:52:45 +02:00
|
|
|
|
addTempRoot(*i);
|
|
|
|
|
if (!isValidPath(*i)) continue; /* path was GC'ed, probably */
|
|
|
|
|
startNest(nest, lvlChatty, format("hashing files in `%1%'") % *i);
|
2012-07-23 21:02:52 +02:00
|
|
|
|
optimisePath_(stats, *i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::optimisePath(const Path & path)
|
|
|
|
|
{
|
2012-07-31 01:55:41 +02:00
|
|
|
|
OptimiseStats stats;
|
|
|
|
|
if (settings.autoOptimiseStore) optimisePath_(stats, path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|