aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/references.cc
blob: 843aed97fc3b8b2e96a1c79a4899a44615e3db3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cerrno>
#include <map>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>

#include "references.hh"
#include "hash.hh"


static void search(const string & s,
    Strings & ids, Strings & seen)
{
    for (Strings::iterator i = ids.begin();
         i != ids.end(); )
    {
        checkInterrupt();
        if (s.find(*i) == string::npos)
            i++;
        else {
            debug(format("found reference to `%1%'") % *i);
            seen.push_back(*i);
            i = ids.erase(i);
        }
    }
}


void checkPath(const string & path,
    Strings & ids, Strings & seen)
{
    checkInterrupt();
    
    debug(format("checking `%1%'") % path);

    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 = readDirectory(path);
	for (Strings::iterator i = names.begin(); i != names.end(); i++) {
            search(*i, ids, seen);
            checkPath(path + "/" + *i, ids, seen);
        }
    }

    else if (S_ISREG(st.st_mode)) {
        
        AutoCloseFD fd = open(path.c_str(), O_RDONLY);
        if (fd == -1) throw SysError(format("opening file `%1%'") % path);

        unsigned char * buf = new unsigned char[st.st_size];

        readFull(fd, buf, st.st_size);

        search(string((char *) buf, st.st_size), ids, seen);
        
        delete buf; /* !!! autodelete */
    }
    
    else if (S_ISLNK(st.st_mode))
        search(readLink(path), ids, seen);
    
    else throw Error(format("unknown file type: %1%") % path);
}


Strings filterReferences(const string & path, const Strings & paths)
{
    map<string, string> backMap;
    Strings ids;
    Strings seen;

    /* 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'). */
    for (Strings::const_iterator i = paths.begin();
         i != paths.end(); i++)
    {
        string baseName = baseNameOf(*i);
        unsigned int pos = baseName.find('-');
        if (pos == string::npos)
            throw Error(format("bad reference `%1%'") % *i);
        string s = string(baseName, 0, pos);
        // parseHash(htSHA256, s);
        ids.push_back(s);
        backMap[s] = *i;
    }

    checkPath(path, ids, seen);

    Strings found;
    for (Strings::iterator i = seen.begin(); i != seen.end(); i++)
    {
        map<string, string>::iterator j;
        if ((j = backMap.find(*i)) == backMap.end()) abort();
        found.push_back(j->second);
    }

    return found;
}