aboutsummaryrefslogtreecommitdiff
path: root/src/libfetchers/path.cc
blob: 77fe87d59e600aedc5952af1adf68be987a984d2 (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
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
#include "fetchers.hh"
#include "store-api.hh"

namespace nix::fetchers {

struct PathInput : Input
{
    Path path;

    /* Allow the user to pass in "fake" tree info attributes. This is
       useful for making a pinned tree work the same as the repository
       from which is exported
       (e.g. path:/nix/store/...-source?lastModified=1585388205&rev=b0c285...). */
    std::optional<Hash> rev;
    std::optional<uint64_t> revCount;
    std::optional<time_t> lastModified;

    std::string type() const override { return "path"; }

    std::optional<Hash> getRev() const override { return rev; }

    bool operator ==(const Input & other) const override
    {
        auto other2 = dynamic_cast<const PathInput *>(&other);
        return
            other2
            && path == other2->path
            && rev == other2->rev
            && revCount == other2->revCount
            && lastModified == other2->lastModified;
    }

    bool isImmutable() const override
    {
        return narHash || rev;
    }

    ParsedURL toURL() const override
    {
        auto query = attrsToQuery(toAttrsInternal());
        query.erase("path");
        return ParsedURL {
            .scheme = "path",
            .path = path,
            .query = query,
        };
    }

    Attrs toAttrsInternal() const override
    {
        Attrs attrs;
        attrs.emplace("path", path);
        if (rev)
            attrs.emplace("rev", rev->gitRev());
        if (revCount)
            attrs.emplace("revCount", *revCount);
        if (lastModified)
            attrs.emplace("lastModified", *lastModified);
        if (!rev && narHash)
            attrs.emplace("narHash", narHash->to_string(SRI));
        return attrs;
    }

    std::optional<Path> getSourcePath() const override
    {
        return path;
    }

    void markChangedFile(std::string_view file, std::optional<std::string> commitMsg) const override
    {
    }

    std::pair<Tree, std::shared_ptr<const Input>> fetchTreeInternal(nix::ref<Store> store) const override
    {
        auto input = std::make_shared<PathInput>(*this);

        // FIXME: check whether access to 'path' is allowed.

        auto storePath = store->maybeParseStorePath(path);

        if (storePath)
            store->addTempRoot(*storePath);

        if (!storePath || storePath->name() != "source" || !store->isValidPath(*storePath))
            // FIXME: try to substitute storePath.
            storePath = store->addToStore("source", path);

        input->narHash = store->queryPathInfo(*storePath)->narHash;

        return
            {
                Tree {
                    .actualPath = store->toRealPath(*storePath),
                    .storePath = std::move(*storePath),
                    .info = TreeInfo {
                        .revCount = revCount,
                        .lastModified = lastModified
                    }
                },
                input
            };
    }

};

struct PathInputScheme : InputScheme
{
    std::unique_ptr<Input> inputFromURL(const ParsedURL & url) override
    {
        if (url.scheme != "path") return nullptr;

        auto input = std::make_unique<PathInput>();
        input->path = url.path;

        if (url.authority && *url.authority != "")
            throw Error("path URL '%s' should not have an authority ('%s')", url.url, *url.authority);

        for (auto & [name, value] : url.query)
            if (name == "rev")
                input->rev = Hash(value, htSHA1);
            else if (name == "revCount") {
                uint64_t revCount;
                if (!string2Int(value, revCount))
                    throw Error("path URL '%s' has invalid parameter '%s'", url.to_string(), name);
                input->revCount = revCount;
            }
            else if (name == "lastModified") {
                time_t lastModified;
                if (!string2Int(value, lastModified))
                    throw Error("path URL '%s' has invalid parameter '%s'", url.to_string(), name);
                input->lastModified = lastModified;
            }
            else if (name == "narHash")
                // FIXME: require SRI hash.
                input->narHash = Hash(value);
            else
                throw Error("path URL '%s' has unsupported parameter '%s'", url.to_string(), name);

        return input;
    }

    std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs) override
    {
        if (maybeGetStrAttr(attrs, "type") != "path") return {};

        auto input = std::make_unique<PathInput>();
        input->path = getStrAttr(attrs, "path");

        for (auto & [name, value] : attrs)
            if (name == "rev")
                input->rev = Hash(getStrAttr(attrs, "rev"), htSHA1);
            else if (name == "revCount")
                input->revCount = getIntAttr(attrs, "revCount");
            else if (name == "lastModified")
                input->lastModified = getIntAttr(attrs, "lastModified");
            else if (name == "narHash")
                // FIXME: require SRI hash.
                input->narHash = Hash(getStrAttr(attrs, "narHash"));
            else if (name == "type" || name == "path")
                ;
            else
                throw Error("unsupported path input attribute '%s'", name);

        return input;
    }
};

static auto r1 = OnStartup([] { registerInputScheme(std::make_unique<PathInputScheme>()); });

}