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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "lix/libfetchers/fetchers.hh"
#include "lix/libfetchers/cache.hh"
#include "lix/libstore/content-address.hh"
#include "lix/libstore/filetransfer.hh"
#include "lix/libstore/globals.hh"
#include "lix/libfetchers/builtin-fetchers.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libutil/archive.hh"
#include "lix/libutil/async-io.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/tarfile.hh"
#include "lix/libstore/temporary-dir.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/split.hh"

namespace nix::fetchers {

/* Note [Recursive hashing of file inputs]:
 * We recursively hash `file` inputs to be consistent with the way that we look
 * up the paths in binary caches (which is assumed all over the place in
 * flakes, like in `nix flake archive`).
 *
 * It would also be easier to always hash downloadFile outputs as recursive,
 * but there are a *whole bunch* of random usages of downloadFile for stuff
 * from channel tarballs to GitHub API that I haven't fully figured out what
 * they are doing or what the implications are for changing them. It seems that
 * for things that are not directly *themselves* flake inputs, the intent is
 * that they are flat-hashed, but flake inputs are always assumed
 * recursive-hashed.
 *
 * So we make the flake usage of it do the thing that's right for flakes, and
 * everything else we leave as before.
 *
 * Quoth Dr. Eelco Dolstra: https://github.com/NixOS/nix/pull/6548#discussion_r877921756
 * > A problem with the use of downloadFile() is that it uses
 * > FileIngestionMethod::Flat instead of FileIngestionMethod::Recursive.
 * > Currently it's assumed that all flake inputs use recursive+sha256 with a
 * > name of "source". This allows inputs to be substituted using the narHash
 * > attribute in the lock file (see Input::computeStorePath()). However, the
 * > lazy trees branch will probably remove the ability to substitute inputs
 * > anyway...
 *
 * In other words, the feature was supposed to have been implemented consistent
 * with other flake input types as recursive-hashed, but it was forgotten
 * before merging it into CppNix, and was later fixed by some version between
 * 2.19 and 2.24, which we are now consistent with.
 *
 * See: https://github.com/edolstra/flake-compat/pull/44
 */

kj::Promise<Result<DownloadFileResult>> downloadFile(
    ref<Store> store,
    const std::string & url,
    const std::string & name,
    bool locked,
    Headers headers,
    FileIngestionMethod ingestionMethod)
try {
    // FIXME: check store

    Attrs inAttrs({
        {"type", "file"},
        {"url", url},
        {"name", name},
        {"ingestionMethod", uint64_t(ingestionMethod)},
    });

    auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs));

    auto useCached = [&]() -> DownloadFileResult
    {
        return {
            .storePath = std::move(cached->storePath),
            .etag = getStrAttr(cached->infoAttrs, "etag"),
            .effectiveUrl = getStrAttr(cached->infoAttrs, "url"),
            .immutableUrl = maybeGetStrAttr(cached->infoAttrs, "immutableUrl"),
        };
    };

    if (cached && !cached->expired)
        co_return useCached();

    if (cached)
        headers.emplace_back("If-None-Match", getStrAttr(cached->infoAttrs, "etag"));
    FileTransferResult res;
    std::string data;
    try {
        auto [meta, content] = TRY_AWAIT(getFileTransfer()->download(url, {headers}));
        res = std::move(meta);
        data = TRY_AWAIT(content->drain());
    } catch (FileTransferError & e) {
        if (cached) {
            printTaggedWarning("%s; using cached version", Uncolored(e.msg()));
            co_return useCached();
        } else
            throw;
    }

    // FIXME: write to temporary file. or stream it. or like. anything but load
    // the entire thing in memory.
    Attrs infoAttrs({
        {"etag", res.etag},
        {"url", res.effectiveUri},
    });

    if (res.immutableUrl)
        infoAttrs.emplace("immutableUrl", *res.immutableUrl);

    std::optional<StorePath> storePath;

    if (res.cached) {
        assert(cached);
        storePath = std::move(cached->storePath);
    } else {
        StringSink sink;
        sink << dumpString(data);
        auto flatHash = [&]() { return hashString(HashType::SHA256, data); };
        // See Note [Recursive hashing of file inputs].
        auto narHash = hashString(HashType::SHA256, sink.s);
        ValidPathInfo info {
            *store,
            name,
            FixedOutputInfo {
                .method = ingestionMethod,
                .hash = ingestionMethod == FileIngestionMethod::Flat ? flatHash() : narHash,
                .references = {},
            },
            narHash,
        };
        info.narSize = sink.s.size();
        auto source = AsyncStringInputStream { sink.s };
        TRY_AWAIT(store->addToStore(info, source, NoRepair, NoCheckSigs));
        storePath = std::move(info.path);
    }

    getCache()->add(
        store,
        inAttrs,
        infoAttrs,
        *storePath,
        locked);

    if (url != res.effectiveUri)
        getCache()->add(
            store,
            {
                {"type", "file"},
                {"url", res.effectiveUri},
                {"name", name},
            },
            infoAttrs,
            *storePath,
            locked);

    co_return DownloadFileResult{
        .storePath = std::move(*storePath),
        .etag = res.etag,
        .effectiveUrl = res.effectiveUri,
        .immutableUrl = res.immutableUrl,
    };
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<DownloadTarballResult>> downloadTarball(
    ref<Store> store,
    const std::string & url,
    const std::string & name,
    bool locked,
    const Headers & headers)
try {
    Attrs inAttrs({
        {"type", "tarball"},
        {"url", url},
        {"name", name},
    });

    auto cached = TRY_AWAIT(getCache()->lookupExpired(store, inAttrs));

    if (cached && !cached->expired)
        co_return DownloadTarballResult{
            .tree = Tree { .actualPath = store->toRealPath(cached->storePath), .storePath = std::move(cached->storePath) },
            .lastModified = (time_t) getIntAttr(cached->infoAttrs, "lastModified"),
            .immutableUrl = maybeGetStrAttr(cached->infoAttrs, "immutableUrl"),
        };

    auto res = TRY_AWAIT(downloadFile(store, url, name, locked, headers));

    std::optional<StorePath> unpackedStorePath;
    time_t lastModified;

    if (cached && res.etag != "" && getStrAttr(cached->infoAttrs, "etag") == res.etag) {
        unpackedStorePath = std::move(cached->storePath);
        lastModified = getIntAttr(cached->infoAttrs, "lastModified");
    } else {
        Path tmpDir = createTempDir();
        AutoDelete autoDelete(tmpDir, true);
        unpackTarfile(store->toRealPath(res.storePath), tmpDir);
        auto members = readDirectory(tmpDir);
        if (members.size() != 1)
            throw nix::Error("tarball '%s' contains an unexpected number of top-level files", url);
        auto topDir = tmpDir + "/" + members.begin()->name;
        lastModified = lstat(topDir).st_mtime;
        unpackedStorePath = TRY_AWAIT(
            store->addToStoreRecursive(name, *prepareDump(topDir), HashType::SHA256, NoRepair)
        );
    }

    Attrs infoAttrs({
        {"lastModified", uint64_t(lastModified)},
        {"etag", res.etag},
    });

    if (res.immutableUrl)
        infoAttrs.emplace("immutableUrl", *res.immutableUrl);

    getCache()->add(
        store,
        inAttrs,
        infoAttrs,
        *unpackedStorePath,
        locked);

    co_return DownloadTarballResult{
        .tree = Tree { .actualPath = store->toRealPath(*unpackedStorePath), .storePath = std::move(*unpackedStorePath) },
        .lastModified = lastModified,
        .immutableUrl = res.immutableUrl,
    };
} catch (...) {
    co_return result::current_exception();
}

// FIXME: some of these only apply to TarballInputScheme.
static const std::set<std::string> allowedCurlAttrs = {
    "lastModified",
    "name",
    "rev",
    "revCount",
    "unpack",
    "url",
};

// An input scheme corresponding to a curl-downloadable resource.
struct CurlInputScheme : InputScheme
{
    const std::set<std::string> transportUrlSchemes = {"file", "http", "https"};

    const std::set<std::string> & allowedAttrs() const override {
        return allowedCurlAttrs;
    }

    bool hasTarballExtension(std::string_view path) const
    {
        return path.ends_with(".zip") || path.ends_with(".tar")
            || path.ends_with(".tgz") || path.ends_with(".tar.gz")
            || path.ends_with(".tar.xz") || path.ends_with(".tar.bz2")
            || path.ends_with(".tar.zst");
    }

    virtual bool isValidURL(const ParsedURL & url, bool requireTree) const = 0;

    std::optional<Input> inputFromURL(const ParsedURL & _url, bool requireTree) const override
    {
        if (!isValidURL(_url, requireTree))
            return std::nullopt;

        auto url = _url;

        Attrs attrs;
        attrs.emplace("type", schemeType());

        url.scheme = parseUrlScheme(url.scheme).transport;

        emplaceURLQueryIntoAttrs(url, attrs, {"revCount", "lastModified"}, {});

        attrs.emplace("url", url.to_string());
        return inputFromAttrs(attrs);
    }

    Attrs preprocessAttrs(const Attrs & attrs) const override
    {
        for (auto & [name, value] : attrs)
            if (name != "type" && name != "narHash" && !allowedAttrs().contains(name))
                throw UnsupportedAttributeError("unsupported tarball input attribute '%s'. If you wanted to fetch a tarball with a query parameter, please use '{ type = \"tarball\"; url = \"...\"; }'", name);

        return attrs;
    }


    ParsedURL toURL(const Input & input) const override
    {
        auto url = parseURL(getStrAttr(input.attrs, "url"));
        // NAR hashes are preferred over file hashes since tar/zip
        // files don't have a canonical representation.
        if (auto narHash = input.getNarHash())
            url.query.insert_or_assign("narHash", narHash->to_string());
        return url;
    }

    bool isLockedByRev() const override { return false; }

    bool hasAllInfo(const Input & input) const override
    {
        return true;
    }

};

struct FileInputScheme : CurlInputScheme
{
    std::string schemeType() const override { return "file"; }

    bool isValidURL(const ParsedURL & url, bool requireTree) const override
    {
        auto parsedUrlScheme = parseUrlScheme(url.scheme);
        return transportUrlSchemes.count(std::string(parsedUrlScheme.transport))
            && (parsedUrlScheme.application
                ? parsedUrlScheme.application.value() == schemeType()
                : (!requireTree && !hasTarballExtension(url.path)));
    }

    kj::Promise<Result<std::pair<StorePath, Input>>>
    fetch(ref<Store> store, const Input & input) override
    try {
        auto file =
            // See Note [Recursive hashing of file inputs].
            TRY_AWAIT(downloadFile(store, getStrAttr(input.attrs, "url"), input.getName(), false, {}, FileIngestionMethod::Recursive));
        co_return {std::move(file.storePath), input};
    } catch (...) {
        co_return result::current_exception();
    }
};

struct TarballInputScheme : CurlInputScheme
{
    std::string schemeType() const override { return "tarball"; }

    bool isValidURL(const ParsedURL & url, bool requireTree) const override
    {
        auto parsedUrlScheme = parseUrlScheme(url.scheme);

        return transportUrlSchemes.count(std::string(parsedUrlScheme.transport))
            && (parsedUrlScheme.application
                ? parsedUrlScheme.application.value() == schemeType()
                : (requireTree || hasTarballExtension(url.path)));
    }

    kj::Promise<Result<std::pair<StorePath, Input>>>
    fetch(ref<Store> store, const Input & _input) override
    try {
        Input input(_input);
        auto url = getStrAttr(input.attrs, "url");
        auto result = TRY_AWAIT(downloadTarball(store, url, input.getName(), false));

        if (result.immutableUrl) {
            auto immutableInput = Input::fromURL(*result.immutableUrl);
            // FIXME: would be nice to support arbitrary flakerefs
            // here, e.g. git flakes.
            if (immutableInput.getType() != "tarball")
                throw Error("tarball 'Link' headers that redirect to non-tarball URLs are not supported");
            input = immutableInput;
        }

        if (result.lastModified && !input.attrs.contains("lastModified"))
            input.attrs.insert_or_assign("lastModified", uint64_t(result.lastModified));

        co_return {result.tree.storePath, std::move(input)};
    } catch (...) {
        co_return result::current_exception();
    }
};

std::unique_ptr<InputScheme> makeFileInputScheme()
{
    return std::make_unique<FileInputScheme>();
}

std::unique_ptr<InputScheme> makeTarballInputScheme()
{
    return std::make_unique<TarballInputScheme>();
}

}