;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see . (define-module (test-records) #:use-module (srfi srfi-1) #:use-module (srfi srfi-64) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:use-module (guix records)) (define (test-module) ;; A module in which to evaluate things that are known to fail. (let ((module (make-fresh-user-module))) (module-use! module (resolve-interface '(guix records))) module)) (test-begin "records") (test-assert "define-record-type*" (begin (define-record-type* foo make-foo foo? (bar foo-bar) (baz foo-baz (default (+ 40 2)))) (and (match (foo (bar 1) (baz 2)) (($ 1 2) #t)) (match (foo (baz 2) (bar 1)) (($ 1 2) #t)) (match (foo (bar 1)) (($ 1 42) #t))))) (test-assert "define-record-type* with let* behavior" ;; Make sure field initializers can refer to each other as if they were in ;; a 'let*'. (begin (define-record-type* bar make-bar foo? (x bar-x) (y bar-y (default (+ 40 2))) (z bar-z)) (and (match (bar (x 1) (y (+ x 1)) (z (* y 2))) (($ 1 2 4) #t)) (match (bar (#include "config.h" #include "util.hh" #include "local-store.hh" #include "globals.hh" #include <cstdlib> #include <cstring> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> #include <stdio.h> namespace nix { /* Any file smaller than this is not considered for deduplication. Keep in sync with (guix store deduplication). */ const size_t deduplicationMinSize = 8192; static void makeWritable(const Path & path) { struct stat st; if (lstat(path.c_str(), &st)) throw SysError(format("getting attributes of path `%1%'") % path); if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) throw SysError(format("changing writability of `%1%'") % path); } struct MakeReadOnly { Path path; MakeReadOnly(const Path & path) : path(path) { } ~MakeReadOnly() { try { /* This will make the path read-only. */ if (path != "") canonicaliseTimestampAndPermissions(path); } catch (...) { ignoreException(); } } }; LocalStore::InodeHash LocalStore::loadInodeHash() { printMsg(lvlDebug, "loading hash inodes in memory"); InodeHash inodeHash; AutoCloseDir dir = opendir(linksDir.c_str()); if (!dir) throw SysError(format("opening directory `%1%'") % linksDir); struct dirent * dirent; while (errno = 0, dirent = readdir(dir)) { /* sic */ checkInterrupt(); // We don't care if we hit non-hash files, anything goes inodeHash.insert(dirent->d_ino); } if (errno) throw SysError(format("reading directory `%1%'") % linksDir); printMsg(lvlTalkative, format("loaded %1% hash inodes") % inodeHash.size()); return inodeHash; } Strings LocalStore::readDirectoryIgnoringInodes(const Path & path, const InodeHash & inodeHash) { Strings names; AutoCloseDir dir = opendir(path.c_str()); if (!dir) throw SysError(format("opening directory `%1%'") % path); struct dirent * dirent; while (errno = 0, dirent = readdir(dir)) { /* sic */ checkInterrupt(); if (inodeHash.count(dirent->d_ino)) { printMsg(lvlDebug, format("`%1%' is already linked") % dirent->d_name); continue; } string name = dirent->d_name; if (name == "." || name == "..") continue; names.push_back(name); } if (errno) throw SysError(format("reading directory `%1%'") % path); return names; } void LocalStore::optimisePath_(OptimiseStats & stats, const Path & path, InodeHash & inodeHash) { checkInterrupt(); struct stat st; if (lstat(path.c_str(), &st)) throw SysError(format("getting attributes of path `%1%'") % path); if (S_ISDIR(st.st_mode)) { Strings names = readDirectoryIgnoringInodes(path, inodeHash); foreach (Strings::iterator, i, names) optimisePath_(stats, path + "/" + *i, inodeHash); return; } /* We can hard link regular files (and maybe symlinks), but do that only for files larger than some threshold. This avoids adding too many entries to '.links', which would slow down 'removeUnusedLinks' while saving little space. */ if (!S_ISREG(st.st_mode) || ((size_t) st.st_size) < deduplicationMinSize) return; /* Sometimes SNAFUs can cause files in the store to be modified, in particular when running programs as root under Guix System (example: $fontconfig/var/cache being modified). Skip those files. FIXME: check the modification time. */ if (S_ISREG(st.st_mode) && (st.st_mode & S_IWUSR)) { printMsg(lvlError, format("skipping suspicious writable file `%1%'") % path); return; } /* This can still happen on top-level files. */ if (st.st_nlink > 1 && inodeHash.count(st.st_ino)) { printMsg(lvlDebug, format("`%1%' is already linked, with %2% other file(s).") % path % (st.st_nlink - 2)); return; } /* 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). 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; printMsg(lvlDebug, format("`%1%' has hash `%2%'") % path % printHash(hash)); /* Check if this is a known hash. */ Path linkPath = linksDir + "/" + printHash32(hash); retry: if (!pathExists(linkPath)) { /* Nope, create a hard link in the links directory. */ if (link(path.c_str(), linkPath.c_str()) == 0) { inodeHash.insert(st.st_ino); return; } switch (errno) { case EEXIST: /* Fall through if another process created ‘linkPath’ before 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. */ printMsg(lvlInfo, format("cannot link `%1%' to `%2%': %3%") % linkPath % path % strerror(ENOSPC)); return; default: throw SysError(format("cannot link `%1%' to `%2%'") % linkPath % path); } } /* 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)) throw SysError(format("getting attributes of path `%1%'") % linkPath); if (st.st_ino == stLink.st_ino) { printMsg(lvlDebug, format("`%1%' is already linked to `%2%'") % path % linkPath); return; } if (st.st_size != stLink.st_size) { printMsg(lvlError, format("removing corrupted link ‘%1%’") % linkPath); unlink(linkPath.c_str()); goto retry; } printMsg(lvlTalkative, format("linking ‘%1%’ to ‘%2%’") % path % linkPath); /* 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)); /* When we're done, make the directory read-only again and reset its timestamp back to 0. */ MakeReadOnly makeReadOnly(mustToggle ? dirOf(path) : ""); Path tempLink = (format("%1%/.tmp-link-%2%-%3%") % settings.nixStore % getpid() % rand()).str(); 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) printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath); return; } throw SysError(format("cannot link `%1%' to `%2%'") % tempLink % linkPath); } /* Atomically replace the old file with the new hard link. */ if (rename(tempLink.c_str(), path.c_str()) == -1) { int renameErrno = errno; if (unlink(tempLink.c_str()) == -1) printMsg(lvlError, format("unable to unlink `%1%'") % tempLink); if (renameErrno == 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.) */ if (st.st_size) printMsg(lvlInfo, format("`%1%' has maximum number of links") % linkPath);