aboutsummaryrefslogtreecommitdiff
#include "references.hh"
#include "hash.hh"
#include "util.hh"
#include "archive.hh"

#include <map>
#include <cstdlib>


namespace nix {


static unsigned int refLength = 32; /* characters */


static void search(const unsigned char * s, unsigned int len, 
    StringSet & hashes, StringSet & seen)
{
    static bool initialised = false;
    static bool isBase32[256];
    if (!initialised) {
        for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false;
        for (unsigned int i = 0; i < base32Chars.size(); ++i)
            isBase32[(unsigned char) base32Chars[i]] = true;
        initialised = true;
    }
    
    for (unsigned int i = 0; i + refLength <= len; ) {
        int j;
        bool match = true;
        for (j = refLength - 1; j >= 0; --j)
            if (!isBase32[(unsigned char) s[i + j]]) {
                i += j + 1;
                match = false;
                break;
            }
        if (!match) continue;
        string ref((const char *) s + i, refLength);
        if (hashes.find(ref) != hashes.end()) {
            debug(format("found reference to `%1%' at offset `%2%'")
                  % ref % i);
            seen.insert(ref);
            hashes.erase(ref);
        }
        ++i;
    }
}


struct RefScanSink : Sink
{
    HashSink hashSink;
    StringSet hashes;
    StringSet seen;

    string tail;

    RefScanSink() : hashSink(htSHA256) { }
    
    void operator () (const unsigned char * data, size_t len);
};


void RefScanSink::operator () (const unsigned char * data, size_t len)
{
    hashSink(data, len);

    /* It's possible that a reference spans the previous and current
       fragment, so search in the concatenation of the tail of the
       previous fragment and the start of the current fragment. */
    string s = tail + string((const char *) data, len > refLength ? refLength : len);
    search((const unsigned char *) s.data(), s.size(), hashes, seen);

    search(data, len, hashes, seen);

    unsigned int tailLen = len <= refLength ? len : refLength;
    tail =
        string(tail, tail.size() < refLength - tailLen ? 0 : tail.size() - (refLength - tailLen)) +
        string((const char *) data + len - tailLen, tailLen);
}


PathSet scanForReferences(const string & path,
    const PathSet & refs, HashResult & hash)
{
    RefScanSink sink;
    std::map<string, Path> backMap;

    /* For efficiency (and a higher hit rate), just search for the
       hash part of the file name.  (This assumes that all references
       have the form `HASH-bla'). */
    foreach (PathSet::const_iterator, i, refs) {
        string baseName = baseNameOf(*i);
        string::size_type pos = baseName.find('-');
        if (pos == string::npos)
            throw Error(format("bad reference `%1%'") % *i);
        string s = string(baseName, 0, pos);
        assert(s.size() == refLength);
        assert(backMap.find(s) == backMap.end());
        // parseHash(htSHA256, s);
        sink.hashes.insert(s);
        backMap[s] = *i;
    }

    /* Look for the hashes in the NAR dump of the path. */
    dumpPath(path, sink);

    /* Map the hashes found back to their store paths. */
    PathSet found;
    foreach (StringSet::iterator, i, sink.seen) {
        std::map<string, Path>::iterator j;
        if ((j = backMap.find(*i)) == backMap.end()) abort();
        found.insert(j->second);
    }

    hash = sink.hashSink.finish();
        
    return found;
}


}
future versions, autoloading a module gives access only to the listed bindings, as per #:select (see <https://bugs.gnu.org/38895>). This commit adjusts autoloads to the new semantics, allowing Guix to be built with Guile 2.9.7/2.9.8. * guix/build/download.scm <top level>: Remove call to 'module-autoload!'. (load-gnutls): New procedure. (tls-wrap): Call it. * guix/git.scm <top level>: Remove call to 'module-autoload!'. (load-git-submodules): New procedure. (update-submodules): Call it instead of 'resolve-interface'. * gnu/bootloader/grub.scm: Replace #:autoload with #:use-module. * gnu/packages.scm: Likewise. * gnu/packages/ssh.scm: Likewise. * gnu/packages/tex.scm: Likewise. * gnu/services/cuirass.scm: Likewise. * gnu/services/mcron.scm: Likewise. * guix/lint.scm: Augment list of bindings in #:autoload. * guix/scripts/build.scm: Likewise. * guix/scripts/gc.scm: Likewise. * guix/scripts/pack.scm: Likewise. * guix/scripts/publish.scm: Likewise. * guix/scripts/pull.scm: Likewise. * guix/utils.scm: Remove unnecessary #:autoload clauses; replace one of them with #:use-module. Ludovic Courtès 2019-10-23services: cuirass: Create /var/log/cuirass during activation....* gnu/services/cuirass.scm (cuirass-activation): Create "/var/log/cuirass". Ludovic Courtès 2019-09-23services: cuirass: Remove unneeded conditional....* gnu/services/cuirass.scm (cuirass-shepherd-service): Remove unneeded 'and'. Ludovic Courtès 2019-08-29services: cuirass: Log web interface to separate file....* gnu/services/cuirass.scm (<cuirass-configuration>): Add web-log-file field. (cuirass-shepherd-service): Read it and use it. * doc/guix.texi (Continuous Integration): Document it. Ricardo Wurmus 2019-08-26services: cuirass: Split into two separate services....* gnu/services/cuirass.scm (cuirass-shepherd-service): Return two shepherd services, one for the web interface, another for building packages. Ricardo Wurmus 2019-03-04services: Use 'file-append' for user account shells....* gnu/services/cuirass.scm (cuirass-account): Use 'file-append' instead of #~(string-append #$shadow "/sbin/nologin"). * gnu/services/monitoring.scm (zabbix-server-account): Likewise. (zabbix-agent-account): Likewise. * gnu/services/rsync.scm (rsync-account): Likewise. * gnu/services/ssh.scm (%openssh-accounts): Likewise. Ludovic Courtès 2018-11-07services: cuirass: Add 'ttl' configuration field....* gnu/services/cuirass.scm (<cuirass-configuration>)[ttl]: New field. (cuirass-shepherd-service): Honor it. Ludovic Courtès 2018-08-16services: cuirass: Put data in /var/lib to avoid removal at boot....Fixes <https://bugs.gnu.org/32330>. * gnu/services/cuirass.scm (<cuirass-configuration>)[database]: Change default from /var/run/cuirass/cuirass.db to /var/lib/cuirass/cuirass.db. (cuirass-account): Change home directory from /var/run/<user> to /var/lib/<user>. Clément Lassieur 2018-07-16services: cuirass: Remove the LOAD-PATH option....* gnu/services/cuirass.scm (<cuirass-configuration>, cuirass-shepherd-service): Remove the LOAD-PATH option. Clément Lassieur 2018-06-22services: cuirass: Add description....* gnu/services/cuirass.scm (cuirass-service-type): Add description. Ricardo Wurmus 2018-06-20services: Fix GPLv3 header in cuirass and pm modules....* gnu/services/cuirass.scm, gnu/services/pm.scm: Adjust to canonical GPLv3 header used throughout Guix. Signed-off-by: Leo Famulari <leo@famulari.name> Nils Gillmann 2017-09-26services: cuirass: Add host option....* gnu/services/cuirass.scm (<cuirass-configuration>): Add host option. (cuirass-shepherd-service): Pass host option. * doc/guix.texi (Continuous Integration): Document it. Jan Nieuwenhuizen 2017-09-13gnu: cuirrass: Default port to 8081....* gnu/services/cuirass.scm (<cuirass-configuration>): Default port to 8081. Fixes conflict with guix-publish default. * doc/guix.texi (Continuous Integration): Update. Jan Nieuwenhuizen 2017-07-30services: cuirass: Set 'GIT_SSL_CAINFO' and 'GIT_EXEC_PATH'....Reported by Ricardo Wurmus. * gnu/services/cuirass.scm (cuirass-shepherd-service): Pass #:environment-variables. Ludovic Courtès 2017-07-21services: cuirass: Add fallback parameter....* gnu/services/cuirass.scm (<cuirass-configuration>)[fallback?]: New field. (cuirass-shepherd-service): Take it into account. * doc/guix.texi (Continuous Integration): Document it. Mathieu Othacehe