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
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
#include "lix/libstore/fs-accessor.hh"
#include "lix/libstore/globals.hh"
#include "lix/libstore/derivations.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libstore/nar-info-disk-cache.hh"
#include "lix/libutil/async-collect.hh"
#include "lix/libutil/async-io.hh"
#include "lix/libutil/async-semaphore.hh"
#include "lix/libutil/async.hh"
#include "lix/libutil/box_ptr.hh"
#include "lix/libutil/c-calls.hh"
#include "lix/libutil/hash.hh"
#include "lix/libutil/json.hh"
#include "lix/libutil/logging.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/serialise.hh"
#include "lix/libutil/sync.hh"
#include "lix/libutil/thread-pool.hh"
#include "lix/libutil/url.hh"
#include "lix/libutil/archive.hh"
#include "lix/libstore/uds-remote-store.hh"
#include "lix/libutil/regex.hh"
#include "lix/libutil/signals.hh"
#include "lix/libutil/strings.hh"
// FIXME this should not be here, see TODO below on
// `addMultipleToStore`.
#include "lix/libstore/worker-protocol.hh"
#include "lix/libutil/users.hh"

#include <algorithm>
#include <functional>
#include <kj/async.h>
#include <memory>
#include <mutex>
#include <regex>

namespace nix {

BuildMode buildModeFromInteger(int raw) {
    switch (raw) {
    case bmNormal: return bmNormal;
    case bmRepair: return bmRepair;
    case bmCheck: return bmCheck;
    default: throw Error("Invalid BuildMode");
    }
}

bool Store::isInStore(PathView path) const
{
    return isInDir(path, config().storeDir);
}


std::pair<StorePath, Path> Store::toStorePath(PathView path) const
{
    if (!isInStore(path))
        throw Error("path '%1%' is not in the Nix store", path);
    auto slash = path.find('/', config().storeDir.size() + 1);
    if (slash == Path::npos)
        return {parseStorePath(path), ""};
    else
        return {parseStorePath(path.substr(0, slash)), (Path) path.substr(slash)};
}


Path Store::followLinksToStore(std::string_view _path) const
{
    Path path = absPath(std::string(_path));
    while (!isInStore(path)) {
        if (!isLink(path)) break;
        auto target = readLink(path);
        path = absPath(target, dirOf(path));
    }
    if (!isInStore(path))
        throw BadStorePath("path '%1%' is not in the Nix store", path);
    return path;
}


StorePath Store::followLinksToStorePath(std::string_view path) const
{
    return toStorePath(followLinksToStore(path)).first;
}


/* Store paths have the following form:

   <realized-path> = <store>/<h>-<name>

   where

   <store> = the location of the Nix store, usually /nix/store

   <name> = a human readable name for the path, typically obtained
     from the name attribute of the derivation, or the name of the
     source file from which the store path is created.  For derivation
     outputs other than the default "out" output, the string "-<id>"
     is suffixed to <name>.

   <h> = base-32 representation of the first 160 bits of a SHA-256
     hash of <s>; the hash part of the store name

   <s> = the string "<type>:sha256:<h2>:<store>:<name>";
     note that it includes the location of the store as well as the
     name to make sure that changes to either of those are reflected
     in the hash (e.g. you won't get /nix/store/<h>-name1 and
     /nix/store/<h>-name2 with equal hash parts).

   <type> = one of:
     "text:<r1>:<r2>:...<rN>"
       for plain text files written to the store using
       addTextToStore(); <r1> ... <rN> are the store paths referenced
       by this path, in the form described by <realized-path>
     "source:<r1>:<r2>:...:<rN>:self"
       for paths copied to the store using addToStore() when recursive
       = true and hashAlgo = "sha256". Just like in the text case, we
       can have the store paths referenced by the path.
       Additionally, we can have an optional :self label to denote self
       reference.
     "output:<id>"
       for either the outputs created by derivations, OR paths copied
       to the store using addToStore() with recursive != true or
       hashAlgo != "sha256" (in that case "source" is used; it's
       silly, but it's done that way for compatibility).  <id> is the
       name of the output (usually, "out").

   <h2> = base-16 representation of a SHA-256 hash of <s2>

   <s2> =
     if <type> = "text:...":
       the string written to the resulting store path
     if <type> = "source:...":
       the serialisation of the path from which this store path is
       copied, as returned by hashPath()
     if <type> = "output:<id>":
       for non-fixed derivation outputs:
         the derivation (see hashDerivationModulo() in
         primops.cc)
       for paths copied by addToStore() or produced by fixed-output
       derivations:
         the string "fixed:out:<rec><algo>:<hash>:", where
           <rec> = "r:" for recursive (path) hashes, or "" for flat
             (file) hashes
           <algo> = "md5", "sha1" or "sha256"
           <hash> = base-16 representation of the path or flat hash of
             the contents of the path (or expected contents of the
             path for fixed-output derivations)

   Note that since an output derivation has always type output, while
   something added by addToStore can have type output or source depending
   on the hash, this means that the same input can be hashed differently
   if added to the store via addToStore or via a derivation, in the sha256
   recursive case.

   It would have been nicer to handle fixed-output derivations under
   "source", e.g. have something like "source:<rec><algo>", but we're
   stuck with this for now...

   The main reason for this way of computing names is to prevent name
   collisions (for security).  For instance, it shouldn't be feasible
   to come up with a derivation whose output path collides with the
   path for a copied source.  The former would have a <s> starting with
   "output:out:", while the latter would have a <s> starting with
   "source:".
*/


StorePath Store::makeStorePath(std::string_view type,
    std::string_view hash, std::string_view name) const
{
    /* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
    auto s = std::string(type) + ":" + std::string(hash)
        + ":" + config().storeDir + ":" + std::string(name);
    auto h = compressHash(hashString(HashType::SHA256, s), 20);
    return StorePath(h, name);
}


StorePath Store::makeStorePath(std::string_view type,
    const Hash & hash, std::string_view name) const
{
    return makeStorePath(type, hash.to_string(HashFormat::Base16), name);
}


StorePath Store::makeOutputPath(std::string_view id,
    const Hash & hash, std::string_view name) const
{
    return makeStorePath("output:" + std::string { id }, hash, outputPathName(name, id));
}


/* Stuff the references (if any) into the type.  This is a bit
   hacky, but we can't put them in, say, <s2> (per the grammar above)
   since that would be ambiguous. */
static std::string makeType(
    const Store & store,
    std::string && type,
    const StoreReferences & references)
{
    for (auto & i : references.others) {
        type += ":";
        type += store.printStorePath(i);
    }
    if (references.self) type += ":self";
    return std::move(type);
}


StorePath Store::makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const
{
    if (info.hash.type == HashType::SHA256 && info.method == FileIngestionMethod::Recursive) {
        return makeStorePath(makeType(*this, "source", info.references), info.hash, name);
    } else {
        if (!info.references.empty()) {
            throw Error("fixed output derivation '%s' is not allowed to refer to other store paths.\nYou may need to use the 'unsafeDiscardReferences' derivation attribute, see the manual for more details.",
                name);
        }
        return makeStorePath(
            "output:out",
            hashString(
                HashType::SHA256,
                "fixed:out:" + makeFileIngestionPrefix(info.method) + info.hash.to_string(HashFormat::Base16)
                    + ":"
            ),
            name
        );
    }
}


StorePath Store::makeTextPath(std::string_view name, const TextInfo & info) const
{
    assert(info.hash.type == HashType::SHA256);
    return makeStorePath(
        makeType(*this, "text", StoreReferences {
            .others = info.references,
            .self = false,
        }),
        info.hash,
        name);
}


StorePath Store::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
{
    // New template
    return std::visit(overloaded {
        [&](const TextInfo & ti) {
            return makeTextPath(name, ti);
        },
        [&](const FixedOutputInfo & foi) {
            return makeFixedOutputPath(name, foi);
        }
    }, ca.raw);
}


StorePath Store::computeStorePathForPathRecursive(std::string_view name,
    const PreparedDump & source) const
{
    FixedOutputInfo caInfo {
        .method = FileIngestionMethod::Recursive,
        .hash = hashPath(HashType::SHA256, source).first,
        .references = {},
    };
    return makeFixedOutputPath(name, caInfo);
}

StorePath Store::computeStorePathForPathFlat(std::string_view name, const Path & srcPath) const
{
    FixedOutputInfo caInfo {
        .method = FileIngestionMethod::Flat,
        .hash = hashFile(HashType::SHA256, srcPath),
        .references = {},
    };
    return makeFixedOutputPath(name, caInfo);
}


StorePath Store::computeStorePathForText(
    std::string_view name,
    std::string_view s,
    const StorePathSet & references) const
{
    return makeTextPath(name, TextInfo {
        .hash = hashString(HashType::SHA256, s),
        .references = references,
    });
}


kj::Promise<Result<StorePath>> Store::addToStoreRecursive(
    std::string_view name,
    const PreparedDump & _source,
    HashType hashAlgo,
    RepairFlag repair)
try {
    auto source = AsyncGeneratorInputStream{_source.dump()};
    co_return TRY_AWAIT(
        addToStoreFromDump(source, name, FileIngestionMethod::Recursive, hashAlgo, repair, {})
    );
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<StorePath>> Store::addToStoreFlat(
    std::string_view name,
    const Path & _srcPath,
    HashType hashAlgo,
    RepairFlag repair)
try {
    Path srcPath(absPath(_srcPath));
    auto source = AsyncGeneratorInputStream{readFileSource(srcPath)};
    co_return TRY_AWAIT(
        addToStoreFromDump(source, name, FileIngestionMethod::Flat, hashAlgo, repair, {})
    );
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<void>> Store::addMultipleToStore(
    PathsSource & pathsToCopy,
    Activity & act,
    RepairFlag repair,
    CheckSigsFlag checkSigs)
try {
    std::atomic<size_t> nrDone{0};
    std::atomic<size_t> nrFailed{0};
    std::atomic<uint64_t> bytesExpected{0};
    std::atomic<uint64_t> nrRunning{0};

    std::map<StorePath, PathsSource::value_type *> infosMap;
    StorePathSet storePathsToAdd;
    for (auto & thingToAdd : pathsToCopy) {
        infosMap.insert_or_assign(thingToAdd.first.path, &thingToAdd);
        storePathsToAdd.insert(thingToAdd.first.path);
    }

#define SHOW_PROGRESS() ACTIVITY_PROGRESS(act, nrDone, pathsToCopy.size(), nrRunning, nrFailed)

    TRY_AWAIT(processGraphAsync<StorePath>(
        storePathsToAdd,

        // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
        [&](const StorePath & path) -> kj::Promise<Result<StorePathSet>> {
            try {
                auto & [info, _] = *infosMap.at(path);

                if (TRY_AWAIT(isValidPath(info.path))) {
                    nrDone++;
                    SHOW_PROGRESS();
                    co_return StorePathSet();
                }

                bytesExpected += info.narSize;
                ACTIVITY_SET_EXPECTED(act, actCopyPath, bytesExpected);

                co_return info.references;
            } catch (...) {
                co_return result::current_exception();
            }
        },

        // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
        [&](const StorePath & path) -> kj::Promise<Result<void>> {
            try {
                checkInterrupt();

                auto & [info_, source_] = *infosMap.at(path);
                auto info = info_;
                info.ultimate = false;

                /* Make sure that the Source object is destroyed when
                we're done. In particular, a coroutine object must
                be destroyed to ensure that the destructors in its
                state are run; this includes
                LegacySSHStore::narFromPath()'s connection lock. */
                auto source = std::move(source_);

                if (!TRY_AWAIT(isValidPath(info.path))) {
                    MaintainCount<decltype(nrRunning)> mc(nrRunning);
                    SHOW_PROGRESS();
                    try {
                        TRY_AWAIT(addToStore(info, *TRY_AWAIT(source()), repair, checkSigs));
                    } catch (Error & e) {
                        nrFailed++;
                        if (!settings.keepGoing)
                            throw e;
                        printMsg(lvlError, "could not copy %s: %s", printStorePath(path), e.what());
                        goto failed;
                    }

                    // can't co_await in catch, so we need this monstrosity
                    if (false) {
                    failed:
                        SHOW_PROGRESS();
                        co_return result::success();
                    }
                }

                nrDone++;
                SHOW_PROGRESS();
                co_return result::success();
            } catch (...) {
                co_return result::current_exception();
            }
        }
    ));
    co_return result::success();

#undef SHOW_PROGRESS
} catch (...) {
    co_return result::current_exception();
}

/*
The aim of this function is to compute in one pass the correct ValidPathInfo for
the files that we are trying to add to the store. To accomplish that in one
pass, given the different kind of inputs that we can take (normal nar archives,
nar archives with non SHA-256 hashes, and flat files), we use a passthru generator
to always pass data to narHashSink (to compute the NAR hash) and have our handlers
for various ingestion types and hash algorithms pass data to hash sinks as needed.
*/
kj::Promise<Result<ValidPathInfo>> Store::addToStoreSlow(std::string_view name, const Path & srcPath,
    FileIngestionMethod method, HashType hashAlgo,
    std::optional<Hash> expectedCAHash)
try {
    HashSink narHashSink { HashType::SHA256 };
    HashSink caHashSink { hashAlgo };

    GeneratorSource nar{[](auto nar, auto & narHashSink) -> WireFormatGenerator {
        while (auto block = nar.next()) {
            narHashSink({block->data(), block->size()});
            co_yield *block;
        }
    }(dumpPath(srcPath), narHashSink)};

    // information always flows from nar to hashSinks. we only check that the
    // nar is correct, and during flat ingestion contains only a single file.
    if (method == FileIngestionMethod::Flat) {
        auto parsed = nar::parse(nar);
        auto entry = parsed.next();
        // if the path was inaccessible we'd get an error from dumpPath
        assert(entry.has_value());
        std::visit(
            overloaded{
                [&](nar::File & f) {
                    while (auto block = f.contents.next()) {
                        caHashSink({block->data(), block->size()});
                    }
                },
                [](nar::Symlink &) { throw Error("cannot import symlink using flat ingestion"); },
                [](nar::Directory &) {
                    throw Error("cannot import directory using flat ingestion");
                },
            },
            *entry
        );
        // drain internal state through the tee as well
        while (parsed.next()) {}
    } else if (hashAlgo != HashType::SHA256) {
        nar.drainInto(caHashSink);
    } else {
        NullSink null;
        nar.drainInto(null);
    }

    /* We extract the result of the computation from the sink by calling
       finish. */
    auto [narHash, narSize] = narHashSink.finish();

    auto hash = method == FileIngestionMethod::Recursive && hashAlgo == HashType::SHA256
        ? narHash
        : caHashSink.finish().first;

    if (expectedCAHash && expectedCAHash != hash)
        throw Error("hash mismatch for '%s'", srcPath);

    ValidPathInfo info {
        *this,
        name,
        FixedOutputInfo {
            .method = method,
            .hash = hash,
            .references = {},
        },
        narHash,
    };
    info.narSize = narSize;

    if (!TRY_AWAIT(isValidPath(info.path))) {
        auto source = AsyncGeneratorInputStream{dumpPath(srcPath)};
        TRY_AWAIT(addToStore(info, source));
    }

    co_return info;
} catch (...) {
    co_return result::current_exception();
}

StringSet StoreConfig::getDefaultSystemFeatures()
{
    return settings.systemFeatures.get();
}

Store::Store(const StoreConfig & config) : state({(size_t) config.pathInfoCacheSize})
{
    assertLibStoreInitialized();
}


std::string Store::getUri()
{
    return "";
}

bool Store::PathInfoCacheValue::isKnownNow()
{
    std::chrono::duration ttl = didExist()
        ? std::chrono::seconds(settings.ttlPositiveNarInfoCache)
        : std::chrono::seconds(settings.ttlNegativeNarInfoCache);

    return std::chrono::steady_clock::now() < time_point + ttl;
}

kj::Promise<Result<std::map<std::string, StorePath>>>
Store::queryStaticDerivationOutputMap(const StorePath & path)
try {
    std::map<std::string, StorePath> outputs;
    auto drv = TRY_AWAIT(readInvalidDerivation(path));
    for (auto & [outputName, output] : drv.outputsAndPaths(*this)) {
        outputs.emplace(outputName, output.second);
    }
    co_return outputs;
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<std::map<std::string, StorePath>>>
Store::queryDerivationOutputMap(const StorePath & path, Store * evalStore_)
try {
    auto & evalStore = evalStore_ ? *evalStore_ : *this;

    co_return TRY_AWAIT(evalStore.queryStaticDerivationOutputMap(path));
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<StorePathSet>> Store::queryDerivationOutputs(const StorePath & path)
try {
    auto outputMap = TRY_AWAIT(this->queryDerivationOutputMap(path));
    StorePathSet outputPaths;
    for (auto & i: outputMap) {
        outputPaths.emplace(std::move(i.second));
    }
    co_return outputPaths;
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<void>> Store::querySubstitutablePathInfos(const StorePathCAMap & paths, SubstitutablePathInfos & infos)
try {
    if (!settings.useSubstitutes) co_return result::success();

    std::unordered_map<StorePath, std::exception_ptr> errors;

    for (auto & sub : TRY_AWAIT(getDefaultSubstituters())) {
        for (auto & path : paths) {
            if (infos.count(path.first))
                // Choose first succeeding substituter.
                continue;

            auto subPath(path.first);

            // Recompute store path so that we can use a different store root.
            if (path.second) {
                subPath = makeFixedOutputPathFromCA(
                    path.first.name(),
                    ContentAddressWithReferences::withoutRefs(*path.second));
                if (sub->config().storeDir == config().storeDir)
                    assert(subPath == path.first);
                if (subPath != path.first)
                    debug("replaced path '%s' with '%s' for substituter '%s'", printStorePath(path.first), sub->printStorePath(subPath), sub->getUri());
            } else if (sub->config().storeDir != config().storeDir) continue;

            debug("checking substituter '%s' for path '%s'", sub->getUri(), sub->printStorePath(subPath));
            try {
                auto info = TRY_AWAIT(sub->queryPathInfo(subPath));

                if (sub->config().storeDir != config().storeDir
                    && !(info->isContentAddressed(*sub) && info->references.empty()))
                {
                    continue;
                }

                auto narInfo = std::dynamic_pointer_cast<const NarInfo>(
                    std::shared_ptr<const ValidPathInfo>(info));
                infos.insert_or_assign(path.first, SubstitutablePathInfo{
                    .deriver = info->deriver,
                    .references = info->references,
                    .downloadSize = narInfo ? narInfo->fileSize : 0,
                    .narSize = info->narSize,
                });
                errors.erase(path.first);
            } catch (InvalidPath &) {
            } catch (SubstituterDisabled &) {
            } catch (Error & e) {
                if (settings.tryFallback) {
                    logError(e.info());
                } else {
                    logErrorInfo(lvlWarn, e.info());
                    errors.emplace(path.first, std::current_exception());
                }
            }
        }
    }

    if (!errors.empty() && !settings.tryFallback) {
        std::rethrow_exception(errors.begin()->second);
    }

    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<bool>> Store::isValidPath(const StorePath & storePath, const Activity * context)
try {
    {
        auto state_(co_await state.lock());
        auto res = state_->pathInfoCache.get(std::string(storePath.to_string()));
        if (res && res->isKnownNow()) {
            stats.narInfoReadAverted++;
            co_return res->didExist();
        }
    }

    if (diskCache) {
        auto res = diskCache->lookupNarInfo(getUri(), std::string(storePath.hashPart()));
        if (res.first != NarInfoDiskCache::oUnknown) {
            stats.narInfoReadAverted++;
            auto state_(co_await state.lock());
            state_->pathInfoCache.upsert(std::string(storePath.to_string()),
                res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue { .value = res.second });
            co_return res.first == NarInfoDiskCache::oValid;
        }
    }

    bool valid = TRY_AWAIT(isValidPathUncached(storePath, context));

    if (diskCache && !valid)
        // FIXME: handle valid = true case.
        diskCache->upsertNarInfo(getUri(), std::string(storePath.hashPart()), 0);

    co_return valid;
} catch (...) {
    co_return result::current_exception();
}

/* Default implementation for stores that only implement
   queryPathInfoUncached(). */
kj::Promise<Result<bool>>
Store::isValidPathUncached(const StorePath & path, const Activity * context)
try {
    TRY_AWAIT(queryPathInfo(path, context));
    co_return true;
} catch (InvalidPath &) {
    co_return false;
} catch (...) {
    co_return result::current_exception();
}


static void ensureGoodStorePath(Store * store, const StorePath & expected, const StorePath & actual)
{
    if (expected.hashPart() != actual.hashPart()) {
        throw Error(
            "the queried store path hash '%s' did not match expected '%s' while querying the store path '%s'",
            expected.hashPart(), actual.hashPart(), store->printStorePath(expected)
        );
    } else if (expected.name() != Store::MissingName && expected.name() != actual.name()) {
        throw Error(
            "the queried store path name '%s' did not match expected '%s' while querying the store path '%s'",
            expected.name(), actual.name(), store->printStorePath(expected)
        );
    }
}

kj::Promise<Result<ref<const ValidPathInfo>>>
Store::queryPathInfo(const StorePath & storePath, const Activity * context)
try {
    auto hashPart = std::string(storePath.hashPart());

    {
        auto res = (co_await state.lock())->pathInfoCache.get(std::string(storePath.to_string()));
        if (res && res->isKnownNow()) {
            stats.narInfoReadAverted++;
            if (!res->didExist())
                throw InvalidPath(
                    "path '%s' does not exist in the store", toRealPath(printStorePath(storePath))
                );
            co_return ref<const ValidPathInfo>::unsafeFromPtr(res->value);
        }
    }

    if (diskCache) {
        auto res = diskCache->lookupNarInfo(getUri(), hashPart);
        if (res.first != NarInfoDiskCache::oUnknown) {
            stats.narInfoReadAverted++;
            {
                auto state_(co_await state.lock());
                state_->pathInfoCache.upsert(std::string(storePath.to_string()),
                    res.first == NarInfoDiskCache::oInvalid ? PathInfoCacheValue{} : PathInfoCacheValue{ .value = res.second });
                if (res.first == NarInfoDiskCache::oInvalid)
                    throw InvalidPath(
                        "path '%s' does not exist in the store",
                        toRealPath(printStorePath(storePath))
                    );
            }
            co_return ref<const ValidPathInfo>::unsafeFromPtr(res.second);
        }
    }

    auto info = TRY_AWAIT(queryPathInfoUncached(storePath, context));
    if (info) {
        // first, before we cache anything, check that the store gave us valid data.
        ensureGoodStorePath(this, storePath, info->path);
    }

    if (diskCache) {
        diskCache->upsertNarInfo(getUri(), hashPart, info);
    }

    {
        auto state_(co_await state.lock());
        state_->pathInfoCache.upsert(std::string(storePath.to_string()), PathInfoCacheValue { .value = info });
    }

    if (!info) {
        stats.narInfoMissing++;
        throw InvalidPath(
            "path '%s' does not exist in the store", toRealPath(printStorePath(storePath))
        );
    }

    co_return ref<const ValidPathInfo>::unsafeFromPtr(info);
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<void>> Store::substitutePaths(const StorePathSet & paths)
try {
    std::vector<DerivedPath> paths2;
    for (auto & path : paths)
        if (!path.isDerivation())
            paths2.emplace_back(DerivedPath::Opaque{path});
    uint64_t downloadSize, narSize;
    StorePathSet willBuild, willSubstitute, unknown;
    TRY_AWAIT(queryMissing(paths2,
        willBuild, willSubstitute, unknown, downloadSize, narSize));

    if (!willSubstitute.empty())
        try {
            std::vector<DerivedPath> subs;
            for (auto & p : willSubstitute) subs.emplace_back(DerivedPath::Opaque{p});
            TRY_AWAIT(buildPaths(subs));
        } catch (Error & e) {
            logWarning(e.info());
        }

    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<StorePathSet>>
Store::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute)
try {
    StorePathSet valid;

    // NOLINTNEXTLINE(cppcoreguidelines-avoid-capturing-lambda-coroutines)
    auto doQuery = [&](const StorePath & path) -> kj::Promise<Result<void>> {
        try {
            TRY_AWAIT(queryPathInfo(path));
            valid.insert(path);
        } catch (InvalidPath &) {
        } catch (...) {
            co_return result::current_exception();
        }
        co_return result::success();
    };

    TRY_AWAIT(asyncSpread(paths, doQuery));

    co_return valid;
} catch (...) {
    co_return result::current_exception();
}


/* Return a string accepted by decodeValidPathInfo() that
   registers the specified paths as valid.  Note: it's the
   responsibility of the caller to provide a closure. */
kj::Promise<Result<std::string>> Store::makeValidityRegistration(const StorePathSet & paths,
    bool showDerivers, bool showHash)
try {
    std::string s = "";

    for (auto & i : paths) {
        s += printStorePath(i) + "\n";

        auto info = TRY_AWAIT(queryPathInfo(i));

        if (showHash) {
            s += info->narHash.to_string(HashFormat::Base16, false) + "\n";
            s += fmt("%1%\n", info->narSize);
        }

        auto deriver = showDerivers && info->deriver ? printStorePath(*info->deriver) : "";
        s += deriver + "\n";

        s += fmt("%1%\n", info->references.size());

        for (auto & j : info->references)
            s += printStorePath(j) + "\n";
    }

    co_return s;
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<StorePathSet>>
Store::exportReferences(const StorePathSet & storePaths, const StorePathSet & inputPaths)
try {
    StorePathSet paths;

    for (auto & storePath : storePaths) {
        if (!inputPaths.count(storePath))
            throw BuildError("cannot export references of path '%s' because it is not in the input closure of the derivation", printStorePath(storePath));

        TRY_AWAIT(computeFSClosure({storePath}, paths));
    }

    /* If there are derivations in the graph, then include their
       outputs as well.  This is useful if you want to do things
       like passing all build-time dependencies of some path to a
       derivation that builds a NixOS DVD image. */
    auto paths2 = paths;

    for (auto & j : paths2) {
        if (j.isDerivation()) {
            Derivation drv = TRY_AWAIT(derivationFromPath(j));
            for (auto & k : drv.outputsAndPaths(*this)) {
                TRY_AWAIT(computeFSClosure(k.second.second, paths));
            }
        }
    }

    co_return paths;
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<JSON>> Store::pathInfoToJSON(
    const StorePathSet & storePaths,
    bool includeImpureInfo,
    bool showClosureSize,
    HashFormat hashFormat,
    AllowInvalidFlag allowInvalid
)
try {
    JSON::array_t jsonList = JSON::array();

    for (auto & storePath : storePaths) {
        auto& jsonPath = jsonList.emplace_back(JSON::object());

        try {
            auto info = TRY_AWAIT(queryPathInfo(storePath));

            jsonPath["path"] = printStorePath(info->path);
            jsonPath["valid"] = true;
            jsonPath["narHash"] = info->narHash.to_string(hashFormat, true);
            jsonPath["narSize"] = info->narSize;

            {
                auto& jsonRefs = (jsonPath["references"] = JSON::array());
                for (auto & ref : info->references)
                    jsonRefs.emplace_back(printStorePath(ref));
            }

            if (info->ca)
                jsonPath["ca"] = renderContentAddress(info->ca);

            std::pair<uint64_t, uint64_t> closureSizes;

            if (showClosureSize) {
                closureSizes = TRY_AWAIT(getClosureSize(info->path));
                jsonPath["closureSize"] = closureSizes.first;
            }

            if (includeImpureInfo) {

                if (info->deriver)
                    jsonPath["deriver"] = printStorePath(*info->deriver);

                if (info->registrationTime)
                    jsonPath["registrationTime"] = info->registrationTime;

                if (info->ultimate)
                    jsonPath["ultimate"] = info->ultimate;

                if (!info->sigs.empty()) {
                    for (auto & sig : info->sigs)
                        jsonPath["signatures"].push_back(sig);
                }

                auto narInfo = std::dynamic_pointer_cast<const NarInfo>(
                    std::shared_ptr<const ValidPathInfo>(info));

                if (narInfo) {
                    if (!narInfo->url.empty())
                        jsonPath["url"] = narInfo->url;
                    if (narInfo->fileHash)
                        jsonPath["downloadHash"] = narInfo->fileHash->to_string(hashFormat, true);
                    if (narInfo->fileSize)
                        jsonPath["downloadSize"] = narInfo->fileSize;
                    if (showClosureSize)
                        jsonPath["closureDownloadSize"] = closureSizes.second;
                }
            }

        } catch (InvalidPath &) {
            jsonPath["path"] = printStorePath(storePath);
            jsonPath["valid"] = false;
        }
    }
    co_return jsonList;
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<std::pair<uint64_t, uint64_t>>>
Store::getClosureSize(const StorePath & storePath)
try {
    uint64_t totalNarSize = 0, totalDownloadSize = 0;
    StorePathSet closure;
    TRY_AWAIT(computeFSClosure(storePath, closure, false, false));
    for (auto & p : closure) {
        auto info = TRY_AWAIT(queryPathInfo(p));
        totalNarSize += info->narSize;
        auto narInfo = std::dynamic_pointer_cast<const NarInfo>(
            std::shared_ptr<const ValidPathInfo>(info));
        if (narInfo)
            totalDownloadSize += narInfo->fileSize;
    }
    co_return {totalNarSize, totalDownloadSize};
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<Store::Stats<>>> Store::getStats()
try {
    {
        auto state_(co_await state.lock());
        stats.pathInfoCacheSize = state_->pathInfoCache.size();
    }
    co_return {
        stats.narInfoRead,
        stats.narInfoReadAverted,
        stats.narInfoMissing,
        stats.narInfoWrite,
        stats.pathInfoCacheSize,
        stats.narRead,
        stats.narReadBytes,
        stats.narReadCompressedBytes,
        stats.narWrite,
        stats.narWriteAverted,
        stats.narWriteBytes,
        stats.narWriteCompressedBytes,
        stats.narWriteCompressionTimeMs,
    };
} catch (...) {
    co_return result::current_exception();
}


static std::string makeCopyPathMessage(
    std::string_view srcUri,
    std::string_view dstUri,
    std::string_view storePath)
{
    return srcUri == "local" || srcUri == "daemon"
        ? fmt("copying path '%s' to '%s'", storePath, dstUri)
        : dstUri == "local" || dstUri == "daemon"
        ? fmt("copying path '%s' from '%s'", storePath, srcUri)
        : fmt("copying path '%s' from '%s' to '%s'", storePath, srcUri, dstUri);
}


namespace {
struct CopyPathStream : AsyncInputStream
{
    Activity & act;
    uint64_t copied = 0, expected;
    box_ptr<AsyncInputStream> inner;

    CopyPathStream(Activity & act, uint64_t expected, box_ptr<AsyncInputStream> inner)
        : act(act)
        , expected(expected)
        , inner(std::move(inner))
    {
    }

    kj::Promise<Result<std::optional<size_t>>> read(void * data, size_t len) override
    try {
        auto result = TRY_AWAIT(inner->read(data, len));

        // do not log progress on every call. nar copies cause a lot of small
        // reads, letting each read report the current copy progress causes a
        // huge amount of overhead (20x or more) in log traffic. reporting at
        // 64 kiB intervals is probably enough, being about 1000 dir entries.
        constexpr size_t CHUNK = 65536;
        const auto doLog = !result || copied / CHUNK < (copied + *result) / CHUNK || *result < len;
        if (result) {
            copied += *result;
        }
        if (doLog) {
            ACTIVITY_PROGRESS(act, copied, expected);
        }
        co_return result;
    } catch (...) {
        co_return result::current_exception();
    }
};
}

kj::Promise<Result<void>> copyStorePath(
    Store & srcStore,
    Store & dstStore,
    const StorePath & storePath,
    RepairFlag repair,
    CheckSigsFlag checkSigs,
    const Activity * context
)
try {
    /* Bail out early (before starting a download from srcStore) if
       dstStore already has this path. */
    if (!repair && TRY_AWAIT(dstStore.isValidPath(storePath, context))) {
        co_return result::success();
    }

    auto srcUri = srcStore.getUri();
    auto dstUri = dstStore.getUri();
    auto storePathS = srcStore.printStorePath(storePath);
    auto act = logger->startActivity(
        lvlInfo,
        actCopyPath,
        makeCopyPathMessage(srcUri, dstUri, storePathS),
        {storePathS, srcUri, dstUri},
        context
    );

    auto info = TRY_AWAIT(srcStore.queryPathInfo(storePath, &act));

    // recompute store path on the chance dstStore does it differently
    if (info->ca && info->references.empty()) {
        auto info2 = make_ref<ValidPathInfo>(*info);
        info2->path = dstStore.makeFixedOutputPathFromCA(
            info->path.name(),
            info->contentAddressWithReferences().value());
        if (dstStore.config().storeDir == srcStore.config().storeDir)
            assert(info->path == info2->path);
        info = info2;
    }

    if (info->ultimate) {
        auto info2 = make_ref<ValidPathInfo>(*info);
        info2->ultimate = false;
        info = info2;
    }

    CopyPathStream source{act, info->narSize, TRY_AWAIT(srcStore.narFromPath(storePath, &act))};
    TRY_AWAIT(dstStore.addToStore(*info, source, repair, checkSigs, &act));
    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}


kj::Promise<Result<std::map<StorePath, StorePath>>> copyPaths(
    Store & srcStore,
    Store & dstStore,
    const RealisedPath::Set & paths,
    RepairFlag repair,
    CheckSigsFlag checkSigs,
    SubstituteFlag substitute)
try {
    StorePathSet storePaths;
    for (auto & path : paths) {
        storePaths.insert(path.path());
        if (auto _ = std::get_if<Realisation>(&path.raw)) {
            throw UnimplementedError("ca derivations are not supported");
        }
    }
    co_return TRY_AWAIT(copyPaths(srcStore, dstStore, storePaths, repair, checkSigs, substitute));
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<std::map<StorePath, StorePath>>> copyPaths(
    Store & srcStore,
    Store & dstStore,
    const StorePathSet & storePaths,
    RepairFlag repair,
    CheckSigsFlag checkSigs,
    SubstituteFlag substitute)
try {
    auto valid = TRY_AWAIT(dstStore.queryValidPaths(storePaths, substitute));

    StorePathSet missing;
    for (auto & path : storePaths)
        if (!valid.count(path)) missing.insert(path);

    auto act =
        logger->startActivity(lvlInfo, actCopyPaths, fmt("copying %d paths", missing.size()));

    // In the general case, `addMultipleToStore` requires a sorted list of
    // store paths to add, so sort them right now
    auto sortedMissing = TRY_AWAIT(srcStore.topoSortPaths(missing));
    std::reverse(sortedMissing.begin(), sortedMissing.end());

    std::map<StorePath, StorePath> pathsMap;
    for (auto & path : storePaths)
        pathsMap.insert_or_assign(path, path);

    Store::PathsSource pathsToCopy;
    std::shared_ptr<AsyncSemaphore> pathCopyRatelimiter = std::make_shared<AsyncSemaphore>(
        // Our maximum amount of copies at the same time is
        // max(25 % of max open files, number of CPUs cores)
        // On a normal shell, getOpenFilesLimit().rlim_cur == 1024.
        // Therefore, the capacity would be around 256 as long as it's not a
        // >256 cores system.
        std::max(
            static_cast<uint32_t>(std::ceil(0.25 * getOpenFilesLimit().rlim_cur)),
            std::thread::hardware_concurrency()
        )
    );

    auto computeStorePathForDst = [&](const ValidPathInfo & currentPathInfo) -> StorePath {
        auto storePathForSrc = currentPathInfo.path;
        auto storePathForDst = storePathForSrc;
        if (currentPathInfo.ca && currentPathInfo.references.empty()) {
            storePathForDst = dstStore.makeFixedOutputPathFromCA(
                currentPathInfo.path.name(),
                currentPathInfo.contentAddressWithReferences().value());
            if (dstStore.config().storeDir == srcStore.config().storeDir)
                assert(storePathForDst == storePathForSrc);
            if (storePathForDst != storePathForSrc)
                debug("replaced path '%s' to '%s' for substituter '%s'",
                        srcStore.printStorePath(storePathForSrc),
                        dstStore.printStorePath(storePathForDst),
                        dstStore.getUri());
        }
        return storePathForDst;
    };

    for (auto & missingPath : sortedMissing) {
        auto info = TRY_AWAIT(srcStore.queryPathInfo(missingPath));

        auto storePathForDst = computeStorePathForDst(*info);
        pathsMap.insert_or_assign(missingPath, storePathForDst);

        ValidPathInfo infoForDst = *info;
        infoForDst.path = storePathForDst;

        struct SinglePathStream : CopyPathStream
        {
            std::shared_ptr<Activity> act;
            AsyncSemaphore::Token rateLimitToken;

            SinglePathStream(
                const std::shared_ptr<Activity> & act,
                AsyncSemaphore::Token && rateLimitToken,
                size_t expected,
                box_ptr<AsyncInputStream> inner
            )
                : CopyPathStream(*act, expected, std::move(inner))
                , act(act)
                , rateLimitToken(std::move(rateLimitToken))
            {
            }
        };

        auto source = [](auto pathCopyRatelimiter,
                         auto & srcStore,
                         auto & dstStore,
                         auto missingPath,
                         auto info) -> kj::Promise<Result<box_ptr<AsyncInputStream>>> {
            try {
                // We can reasonably assume that the copy will happen whenever we
                // read the path, so log something about that at that point
                auto srcUri = srcStore.getUri();
                auto dstUri = dstStore.getUri();
                auto storePathS = srcStore.printStorePath(missingPath);
                auto act = std::make_shared<Activity>(logger->startActivity(
                    lvlInfo,
                    actCopyPath,
                    makeCopyPathMessage(srcUri, dstUri, storePathS),
                    Logger::Fields{storePathS, srcUri, dstUri}
                ));

                // We prevent the copy process to overwhelm the I/O path.
                // Certain store implementations may open files which will count
                // towards the open files limit.
                auto token = co_await pathCopyRatelimiter->acquire();
                co_return make_box_ptr<SinglePathStream>(
                    act,
                    std::move(token),
                    info->narSize,
                    TRY_AWAIT(srcStore.narFromPath(missingPath, act.get()))
                );
            } catch (...) {
                co_return result::current_exception();
            }
        };
        pathsToCopy.push_back(std::pair{
            infoForDst,
            std::bind(
                source,
                pathCopyRatelimiter,
                std::ref(srcStore),
                std::ref(dstStore),
                missingPath,
                info
            )
        });
    }

    TRY_AWAIT(dstStore.addMultipleToStore(pathsToCopy, act, repair, checkSigs));

    co_return pathsMap;
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<void>> copyClosure(
    Store & srcStore,
    Store & dstStore,
    const RealisedPath::Set & paths,
    RepairFlag repair,
    CheckSigsFlag checkSigs,
    SubstituteFlag substitute)
try {
    if (&srcStore == &dstStore) co_return result::success();

    RealisedPath::Set closure;
    TRY_AWAIT(RealisedPath::closure(srcStore, paths, closure));

    TRY_AWAIT(copyPaths(srcStore, dstStore, closure, repair, checkSigs, substitute));
    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<void>> copyClosure(
    Store & srcStore,
    Store & dstStore,
    const StorePathSet & storePaths,
    RepairFlag repair,
    CheckSigsFlag checkSigs,
    SubstituteFlag substitute)
try {
    if (&srcStore == &dstStore) co_return result::success();

    StorePathSet closure;
    TRY_AWAIT(srcStore.computeFSClosure(storePaths, closure));
    TRY_AWAIT(copyPaths(srcStore, dstStore, closure, repair, checkSigs, substitute));
    co_return result::success();
} catch (...) {
    co_return result::current_exception();
}

std::optional<ValidPathInfo> decodeValidPathInfo(const Store & store, std::istream & str, std::optional<HashResult> hashGiven)
{
    std::string path;
    getline(str, path);
    if (str.eof()) { return {}; }
    if (!hashGiven) {
        std::string s;
        getline(str, s);
        auto narHash = Hash::parseAny(s, HashType::SHA256);
        getline(str, s);
        auto narSize = string2Int<uint64_t>(s);
        if (!narSize) throw Error("number expected");
        hashGiven = { narHash, *narSize };
    }
    ValidPathInfo info(store.parseStorePath(path), hashGiven->first);
    info.narSize = hashGiven->second;
    std::string deriver;
    getline(str, deriver);
    if (deriver != "") info.deriver = store.parseStorePath(deriver);
    std::string s;
    getline(str, s);
    auto n = string2Int<int>(s);
    if (!n) throw Error("number expected");
    while ((*n)--) {
        getline(str, s);
        info.references.insert(store.parseStorePath(s));
    }
    if (!str || str.eof()) throw Error("missing input");
    return std::optional<ValidPathInfo>(std::move(info));
}


std::string Store::showPaths(const StorePathSet & paths)
{
    std::string s;
    for (auto & i : paths) {
        if (s.size() != 0) s += ", ";
        s += "'" + printStorePath(i) + "'";
    }
    return s;
}


std::string showPaths(const PathSet & paths)
{
    return concatStringsSep(", ", quoteStrings(paths));
}


kj::Promise<Result<Derivation>> Store::derivationFromPath(const StorePath & drvPath)
try {
    TRY_AWAIT(ensurePath(drvPath));
    co_return TRY_AWAIT(readDerivation(drvPath));
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<Derivation>>
readDerivationCommon(Store& store, const StorePath& drvPath, bool requireValidPath)
try {
    auto accessor = store.getFSAccessor();
    try {
        co_return parseDerivation(
            store,
            TRY_AWAIT(accessor->readFile(store.printStorePath(drvPath), requireValidPath)),
            Derivation::nameFromPath(drvPath)
        );
    } catch (FormatError & e) {
        auto drvPathS = store.printStorePath(drvPath);
        throw Error(
            fmt("error parsing derivation '%1%': %2%\n"
                "This can occur when the derivation is corrupted.\n"
                "You can check this with `nix-store --verify-path %1%` and possibly repair with "
                "`nix-store --repair-path %1%`.\n"
                "In case the repair cannot be done, `nix-store --delete %1%` may be able "
                "to remove the broken path.\n"
                "We would appreciate a bug report at "
                "https://git.lix.systems/lix-project/lix/issues if you think this is a bug.",
                drvPathS,
                e.msg())
        );
    }
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<std::optional<StorePath>>> Store::getBuildDerivationPath(const StorePath & path)
try {

    if (!path.isDerivation()) {
        try {
            auto info = TRY_AWAIT(queryPathInfo(path));
            co_return info->deriver;
        } catch (InvalidPath &) {
            co_return std::nullopt;
        }
    }

    co_return path;
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<Derivation>> Store::readDerivation(const StorePath & drvPath)
{ return readDerivationCommon(*this, drvPath, true); }

kj::Promise<Result<Derivation>> Store::readInvalidDerivation(const StorePath & drvPath)
{ return readDerivationCommon(*this, drvPath, false); }

}


#include "lix/libstore/local-store.hh"
#include "lix/libstore/uds-remote-store.hh"


namespace nix {

/* Split URI into protocol+hierarchy part and its parameter set. */
std::pair<std::string, StoreConfig::Params> splitUriAndParams(const std::string & uri_)
{
    auto uri(uri_);
    StoreConfig::Params params;
    auto q = uri.find('?');
    if (q != std::string::npos) {
        params = decodeQuery(uri.substr(q + 1));
        uri = uri_.substr(0, q);
    }
    return {uri, params};
}

static bool isNonUriPath(const std::string & spec)
{
    return
        // is not a URL
        spec.find("://") == std::string::npos
        // Has at least one path separator, and so isn't a single word that
        // might be special like "auto"
        && spec.find("/") != std::string::npos;
}

static std::optional<ref<Store>>
openFromNonUri(const std::string & uri, const StoreConfig::Params & params, AllowDaemon allowDaemon)
{
    if (uri == "" || uri == "auto") {
        // In the WASM evaluator build, LocalStore and UDSRemoteStore are not
        // available. All store I/O goes through WasmStore which is constructed
        // directly by wasm-exports.cc. openStore() is never called.
#ifndef __EMSCRIPTEN__
        auto stateDir = getOr(params, "state", settings.nixStateDir);
        if (allowDaemon == AllowDaemon::Allow
            && std::ranges::any_of(
                settings.nixDaemonSockets(), [](auto & socket) { return pathExists(socket.path); }
            ))
        {
            return make_ref<UDSRemoteStore>(params);
        } else if (sys::access(stateDir, R_OK | W_OK) == 0) {
            return LocalStore::makeLocalStore(params);
        }
#if __linux__
        else if (!pathExists(stateDir) && params.empty() && getuid() != 0
                 && !getEnv("NIX_STORE_DIR").has_value() && !getEnv("NIX_STATE_DIR").has_value())
        {
            /* If /nix doesn't exist, there is no daemon socket, and
               we're not root, then automatically set up a chroot
               store in ~/.local/share/nix/root. */
            auto chrootStore = getDataDir() + "/nix/root";
            if (!pathExists(chrootStore)) {
                try {
                    createDirs(chrootStore);
                } catch (Error & e) {
                    return LocalStore::makeLocalStore(params);
                }
                printTaggedWarning(
                    "'%s' does not exist, so Lix will use '%s' as a chroot store",
                    stateDir,
                    chrootStore
                );
            } else
                debug("'%s' does not exist, so Lix will use '%s' as a chroot store", stateDir, chrootStore);
            StoreConfig::Params chrootStoreParams;
            chrootStoreParams["root"] = chrootStore;
            // FIXME? this ignores *all* store parameters passed to this function?
            return LocalStore::makeLocalStore(chrootStoreParams);
        }
#endif
        else
            return LocalStore::makeLocalStore(params);
    } else if (uri == "daemon") {
        if (allowDaemon == AllowDaemon::Disallow) {
            throw Error("tried to open a daemon store in a context that doesn't support this");
        }
        return make_ref<UDSRemoteStore>(params);
    } else if (uri == "local") {
        return LocalStore::makeLocalStore(params);
    } else if (isNonUriPath(uri)) {
        StoreConfig::Params params2 = params;
        params2["root"] = absPath(uri);
        return LocalStore::makeLocalStore(params2);
    } else {
        return std::nullopt;
    }
#else // __EMSCRIPTEN__
        return std::nullopt;
    } else {
        return std::nullopt;
    }
#endif
}

// The `parseURL` function supports both IPv6 URIs as defined in
// RFC2732, but also pure addresses. The latter one is needed here to
// connect to a remote store via SSH (it's possible to do e.g. `ssh root@::1`).
//
// This function now ensures that a usable connection string is available:
// * If the store to be opened is not an SSH store, nothing will be done.
// * If the URL looks like `root@[::1]` (which is allowed by the URL parser and probably
//   needed to pass further flags), it
//   will be transformed into `root@::1` for SSH (same for `[::1]` -> `::1`).
// * If the URL looks like `root@::1` it will be left as-is.
// * In any other case, the string will be left as-is.
static std::string extractConnStr(const std::string &proto, const std::string &connStr)
{
    if (proto.rfind("ssh") != std::string::npos) {
        std::smatch result;
        std::regex v6AddrRegex = regex::parse("^((.*)@)?\\[(.*)\\]$");

        if (std::regex_match(connStr, result, v6AddrRegex)) {
            if (result[1].matched) {
                return result.str(1) + result.str(3);
            }
            return result.str(3);
        }
    }

    return connStr;
}

kj::Promise<Result<ref<Store>>>
openStore(const std::string & uri_, const StoreConfig::Params & extraParams, AllowDaemon allowDaemon)
try {
    auto params = extraParams;
    try {
        auto parsedUri = parseURL(uri_);
        params.insert(parsedUri.query.begin(), parsedUri.query.end());

        auto baseURI = extractConnStr(
            parsedUri.scheme,
            parsedUri.authority.value_or("") + parsedUri.path
        );

        for (auto implem : *StoreImplementations::registered) {
            if (implem.uriSchemes.count(parsedUri.scheme)) {
                auto store = implem.create(parsedUri.scheme, baseURI, params);
                if (store) {
                    experimentalFeatureSettings.require((*store)->config().experimentalFeature());
                    TRY_AWAIT((*store)->init());
                    (*store)->config().warnUnknownSettings();
                    co_return *store;
                }
            }
        }
    }
    catch (BadURL &) {
        auto [uri, uriParams] = splitUriAndParams(uri_);
        params.insert(uriParams.begin(), uriParams.end());

        if (auto store = openFromNonUri(uri, params, allowDaemon)) {
            (*store)->config().warnUnknownSettings();
            co_return *store;
        }
    }

    throw Error("don't know how to open Nix store '%s'", uri_);
} catch (...) {
    co_return result::current_exception();
}

kj::Promise<Result<std::list<ref<Store>>>> getDefaultSubstituters()
try {
    static Sync<std::optional<std::list<ref<Store>>>, AsyncMutex> stores;

    auto lk = co_await stores.lock();

    if (!lk->has_value()) {
        StringSet done;

        lk->emplace();
        for (auto uri : settings.substituters.get()) {
            if (!done.insert(uri).second) continue;
            try {
                (*lk)->push_back(TRY_AWAIT(openStore(uri)));
            } catch (Error & e) {
                logWarning(
                    {.msg = HintFmt("Failed to setup the substituter at URI '%s': %s", uri, e.msg())
                    }
                );
            }
        }

        (*lk)->sort([](ref<Store> & a, ref<Store> & b) {
            return a->config().priority < b->config().priority;
        });
    }

    co_return **lk;
} catch (...) {
    co_return result::current_exception();
}

std::vector<StoreFactory> * StoreImplementations::registered = 0;

}