about summary refs log tree commit diff
path: root/src/libstore/storeexpr.cc
blob: 29f271de884bb496f07ad45e5f4a547d250dc6ef (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "storeexpr.hh"
#include "globals.hh"
#include "store.hh"


Hash hashTerm(ATerm t)
{
    return hashString(atPrint(t));
}


Path writeTerm(ATerm t, const string & suffix)
{
    /* The id of a term is its hash. */
    Hash h = hashTerm(t);

    Path path = canonPath(nixStore + "/" + 
        (string) h + suffix + ".store");

    if (!readOnlyMode && !isValidPath(path)) {
        char * s = ATwriteToString(t);
        if (!s) throw Error(format("cannot write aterm to `%1%'") % path);
        addTextToStore(path, string(s));
    }
    
    return path;
}


static void parsePaths(ATermList paths, PathSet & out)
{
    ATMatcher m;
    for (ATermIterator i(paths); i; ++i) {
        string s;
        if (!(atMatch(m, *i) >> s))
            throw badTerm("not a path", *i);
        out.insert(s);
    }
}


static void checkClosure(const Closure & closure)
{
    if (closure.elems.size() == 0)
        throw Error("empty closure");

    PathSet decl;
    for (ClosureElems::const_iterator i = closure.elems.begin();
         i != closure.elems.end(); i++)
        decl.insert(i->first);
    
    for (PathSet::const_iterator i = closure.roots.begin();
         i != closure.roots.end(); i++)
        if (decl.find(*i) == decl.end())
            throw Error(format("undefined root path `%1%'") % *i);
    
    for (ClosureElems::const_iterator i = closure.elems.begin();
         i != closure.elems.end(); i++)
        for (PathSet::const_iterator j = i->second.refs.begin();
             j != i->second.refs.end(); j++)
            if (decl.find(*j) == decl.end())
                throw Error(
		    format("undefined path `%1%' referenced by `%2%'")
		    % *j % i->first);
}


/* Parse a closure. */
static bool parseClosure(ATerm t, Closure & closure)
{
    ATermList roots, elems;
    ATMatcher m;

    if (!(atMatch(m, t) >> "Closure" >> roots >> elems))
        return false;

    parsePaths(roots, closure.roots);

    for (ATermIterator i(elems); i; ++i) {
        string path;
        ATermList refs;
        if (!(atMatch(m, *i) >> "" >> path >> refs))
            throw badTerm("not a closure element", *i);
        ClosureElem elem;
        parsePaths(refs, elem.refs);
        closure.elems[path] = elem;
    }

    checkClosure(closure);
    return true;
}


static bool parseDerivation(ATerm t, Derivation & derivation)
{
    ATMatcher m;
    ATermList outs, ins, args, bnds;
    string builder, platform;

    if (!(atMatch(m, t) >> "Derive" >> outs >> ins >> platform
            >> builder >> args >> bnds))
        return false;

    parsePaths(outs, derivation.outputs);
    parsePaths(ins, derivation.inputs);

    derivation.builder = builder;
    derivation.platform = platform;
    
    for (ATermIterator i(args); i; ++i) {
        string s;
        if (!(atMatch(m, *i) >> s))
            throw badTerm("string expected", *i);
        derivation.args.push_back(s);
    }

    for (ATermIterator i(bnds); i; ++i) {
        string s1, s2;
        if (!(atMatch(m, *i) >> "" >> s1 >> s2))
            throw badTerm("tuple of strings expected", *i);
        derivation.env[s1] = s2;
    }

    return true;
}


StoreExpr parseStoreExpr(ATerm t)
{
    StoreExpr ne;
    if (parseClosure(t, ne.closure))
        ne.type = StoreExpr::neClosure;
    else if (parseDerivation(t, ne.derivation))
        ne.type = StoreExpr::neDerivation;
    else throw badTerm("not a store expression", t);
    return ne;
}


static ATermList unparsePaths(const PathSet & paths)
{
    ATermList l = ATempty;
    for (PathSet::const_iterator i = paths.begin();
         i != paths.end(); i++)
        l = ATinsert(l, ATmake("<str>", i->c_str()));
    return ATreverse(l);
}


static ATerm unparseClosure(const Closure & closure)
{
    ATermList roots = unparsePaths(closure.roots);
    
    ATermList elems = ATempty;
    for (ClosureElems::const_iterator i = closure.elems.begin();
         i != closure.elems.end(); i++)
        elems = ATinsert(elems,
            ATmake("(<str>, <term>)",
                i->first.c_str(),
                unparsePaths(i->second.refs)));

    return ATmake("Closure(<term>, <term>)", roots, elems);
}


static ATerm unparseDerivation(const Derivation & derivation)
{
    ATermList args = ATempty;
    for (Strings::const_iterator i = derivation.args.begin();
         i != derivation.args.end(); i++)
        args = ATinsert(args, ATmake("<str>", i->c_str()));

    ATermList env = ATempty;
    for (StringPairs::const_iterator i = derivation.env.begin();
         i != derivation.env.end(); i++)
        env = ATinsert(env,
            ATmake("(<str>, <str>)", 
                i->first.c_str(), i->second.c_str()));

    return ATmake("Derive(<term>, <term>, <str>, <str>, <term>, <term>)",
        unparsePaths(derivation.outputs),
        unparsePaths(derivation.inputs),
        derivation.platform.c_str(),
        derivation.builder.c_str(),
        ATreverse(args),
        ATreverse(env));
}


ATerm unparseStoreExpr(const StoreExpr & ne)
{
    if (ne.type == StoreExpr::neClosure)
        return unparseClosure(ne.closure);
    else if (ne.type == StoreExpr::neDerivation)
        return unparseDerivation(ne.derivation);
    else abort();
}