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;
}


}
nt: Update to 1.0.0....* gnu/packages/elixir-xyz.scm (elixir-bunt): Update to 1.0.0. Change-Id: I5b51b2063f5eb07adeadb3ac33ad78a834b4b962 Signed-off-by: Andrew Tropin <andrew@trop.in> Igor Goryachev 2024-07-02gnu: elixir-file-system: Update to 1.0.0....* gnu/packages/elixir-xyz.scm (elixir-file-system): Update to 1.0.0. Change-Id: I532d13e76c5a0a2cc683b97799eb52c6df5bc161 Signed-off-by: Andrew Tropin <andrew@trop.in> Igor Goryachev 2024-07-02gnu: elixir-jason: Fix lint warnings....* gnu/packages/elixir-xyz.scm (elixir-jason): Fix lint warnings. Change-Id: Iafefc4758d3b505cc725f32a0aa1061f1814188f Signed-off-by: Andrew Tropin <andrew@trop.in> Igor Goryachev 2024-07-02gnu: elixir-makeup: Update to 1.1.2, make reproducible....* gnu/packages/elixir-xyz.scm (elixir-makeup): Update to 1.1.2, make reproducible. Change-Id: I43fca3e4d1f6d2a5e678940416710c126b51a59b Signed-off-by: Andrew Tropin <andrew@trop.in> Igor Goryachev 2024-07-02gnu: elixir-nimble-parsec: Update to 1.4.0....* gnu/packages/elixir-xyz.scm (elixir-nimble-parsec): Update to 1.4.0. Change-Id: I4622df968c176a6ad318b70d18dba31909d9276c Signed-off-by: Andrew Tropin <andrew@trop.in> Igor Goryachev 2023-12-17gnu: Add elixir-machete....* gnu/packages/elixir-xyz.scm (elixir-machete): New variable. Change-Id: I76e0fd78aeeaeaa94a297032b3b309846510c975 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-dialyxir....* gnu/packages/elixir-xyz.scm (elixir-dialyxir): New variable. Change-Id: I387062e0aaaded448022aa7534b95859b5979a06 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-erlex....* gnu/packages/elixir-xyz.scm (elixir-erlex): New variable. Change-Id: Ia518ad5fd32d3f5942b20113e61e4ef7e7895ff3 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-credo....* gnu/packages/elixir-xyz.scm (elixir-credo): New variable. Change-Id: I6df0d926e0fb96caa65d081892026b3495431c83 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-excoveralls....* gnu/packages/elixir-xyz.scm (elixir-excoveralls): New variable. Change-Id: Ib271d8a7393e21178806df28ab892afe99757297 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-castore....* gnu/packages/elixir-xyz.scm (elixir-castore): New variable. Change-Id: Iea1fea8241d19ff35a183af9c3fbf2ed5198a259 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-inch-ex....* gnu/packages/elixir-xyz.scm (elixir-inch-ex): New variable. Change-Id: I6e98424528a0264a2587d28fcf065fa74eff0f1a Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-bunt....* gnu/packages/elixir-xyz.scm (elixir-bunt): New variable. Change-Id: Ib84797c31d6ab40d59b87b040993dae9391ead6b Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-file-system....* gnu/packages/elixir-xyz.scm (elixir-file-system): New variable. Change-Id: I54e9c9069ddf150bb0c882775b0b2c9d5e59abd6 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-jason....* gnu/packages/elixir-xyz.scm (elixir-jason): New variable. Change-Id: Iadd816e2b5db6d146728aacb4d6b2c66aba4304c Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-makeup....* gnu/packages/elixir-xyz.scm (elixir-makeup): New variable. Change-Id: I21fe9ffddb4c95a2c67b74340b1105c2a54d3c82 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring 2023-12-17gnu: Add elixir-nimble-parsec....* gnu/packages/elixir-xyz.scm (elixir-nimble-parsec): New variable. Change-Id: I79bab8095aee3212bc295354ead55df49ef92d17 Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com> Pierre-Henry Fröhring