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
#include "lix/libcmd/built-path.hh"
#include "lix/libstore/derivations.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/json.hh"
#include "lix/libutil/result.hh"

#include <kj/async.h>

namespace nix {

#define CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, COMPARATOR) \
    bool MY_TYPE ::operator COMPARATOR (const MY_TYPE & other) const \
    { \
        const MY_TYPE* me = this; \
        auto fields1 = std::tie(me->drvPath, me->FIELD); \
        me = &other; \
        auto fields2 = std::tie(me->drvPath, me->FIELD); \
        return fields1 COMPARATOR fields2; \
    }
#define CMP(CHILD_TYPE, MY_TYPE, FIELD) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, ==) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, !=) \
    CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, <)

#define FIELD_TYPE std::map<std::string, StorePath>
CMP(SingleBuiltPath, BuiltPathBuilt, outputs)
#undef FIELD_TYPE

#undef CMP
#undef CMP_ONE

StorePathSet BuiltPath::outPaths() const
{
    return std::visit(
        overloaded{
            [](const BuiltPath::Opaque & p) { return StorePathSet{p.path}; },
            [](const BuiltPath::Built & b) {
                StorePathSet res;
                for (auto & [_, path] : b.outputs)
                    res.insert(path);
                return res;
            },
        }, raw()
    );
}

kj::Promise<Result<JSON>> BuiltPath::Built::toJSON(const Store & store) const
try {
    JSON res;
    res["drvPath"] = TRY_AWAIT(drvPath.toJSON(store));
    for (const auto & [outputName, outputPath] : outputs) {
        res["outputs"][outputName] = store.printStorePath(outputPath);
    }
    co_return res;
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<RealisedPath::Set>> BuiltPath::toRealisedPaths(Store & store) const
try {
    RealisedPath::Set res;
    auto handlers = overloaded{
        // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
        [&](const BuiltPath::Opaque & p) -> kj::Promise<Result<void>> {
            try {
                res.insert(p.path);
                return {result::success()};
            } catch (...) {
                return {result::current_exception()};
            }
        },
        // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
        [&](const BuiltPath::Built & p) -> kj::Promise<Result<void>> {
            try {
                auto drvHashes = TRY_AWAIT(
                    staticOutputHashes(store, TRY_AWAIT(store.readDerivation(p.drvPath.path)))
                );
                for (auto& [outputName, outputPath] : p.outputs) {
                    res.insert(outputPath);
                }
                co_return result::success();
            } catch (...) {
                co_return result::current_exception();
            }
        },
    };
    TRY_AWAIT(std::visit(handlers, raw()));
    co_return res;
} catch (...) {
    co_return result::current_exception();
}

}