Skip to content

Commit c602ebf

Browse files
committed
Refactor wopAddToStore to make wopAddTextToStore obsolete
1 parent e34fe47 commit c602ebf

File tree

3 files changed

+53
-42
lines changed

3 files changed

+53
-42
lines changed

src/libstore/remote-store.cc

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -526,20 +526,16 @@ std::optional<StorePath> RemoteStore::queryPathFromHashPart(const std::string &
526526
}
527527

528528

529-
StorePath RemoteStore::addToStoreFromDump(Source & dump, const string & name,
530-
FileIngestionMethod method, HashType hashType, RepairFlag repair)
529+
StorePath RemoteStore::addCAToStore(Source & dump, const string & name, ContentAddressMethod caMethod, StorePathSet references)
531530
{
532-
if (repair) throw Error("repairing is not supported when adding to store through the Nix daemon");
533-
StorePathSet references;
534-
535531
auto conn(getConnection());
536532

537533
if (GET_PROTOCOL_MINOR(conn->daemonVersion) >= 25) {
538534

539535
conn->to
540536
<< wopAddToStore
541537
<< name
542-
<< renderContentAddressMethod(FixedOutputHashMethod{ .fileIngestionMethod = method, .hashType = hashType });
538+
<< renderContentAddressMethod(caMethod);
543539
writeStorePaths(*this, conn->to, references);
544540

545541
conn.withFramedSink([&](Sink & sink) {
@@ -552,42 +548,60 @@ StorePath RemoteStore::addToStoreFromDump(Source & dump, const string & name,
552548
else {
553549
if (repair) throw Error("repairing is not supported when building through the Nix daemon protocol < 1.25");
554550

555-
conn->to
556-
<< wopAddToStore
557-
<< name
558-
<< ((hashType == htSHA256 && method == FileIngestionMethod::Recursive) ? 0 : 1) /* backwards compatibility hack */
559-
<< (method == FileIngestionMethod::Recursive ? 1 : 0)
560-
<< printHashType(hashType);
551+
std::visit(overloaded {
552+
[&](TextHashMethod thm) -> void {
553+
std::string s = dump.drain();
554+
conn->to << wopAddTextToStore << name << s;
555+
writeStorePaths(*this, conn->to, references);
556+
conn.processStderr();
557+
},
558+
[&](FixedOutputHashMethod fohm) -> void {
559+
conn->to
560+
<< wopAddToStore
561+
<< name
562+
<< ((fohm.hashType == htSHA256 && fohm.fileIngestionMethod == FileIngestionMethod::Recursive) ? 0 : 1) /* backwards compatibility hack */
563+
<< (fohm.fileIngestionMethod == FileIngestionMethod::Recursive ? 1 : 0)
564+
<< printHashType(fohm.hashType);
561565

562-
try {
563-
conn->to.written = 0;
564-
conn->to.warn = true;
565-
connections->incCapacity();
566-
{
567-
Finally cleanup([&]() { connections->decCapacity(); });
568-
if (method == FileIngestionMethod::Recursive) {
569-
dump.drainInto(conn->to);
570-
} else {
571-
std::string contents = dump.drain();
572-
dumpString(contents, conn->to);
573-
}
574-
}
575-
conn->to.warn = false;
576-
conn.processStderr();
577-
} catch (SysError & e) {
578-
/* Daemon closed while we were sending the path. Probably OOM
579-
or I/O error. */
580-
if (e.errNo == EPIPE)
581566
try {
567+
conn->to.written = 0;
568+
conn->to.warn = true;
569+
connections->incCapacity();
570+
{
571+
Finally cleanup([&]() { connections->decCapacity(); });
572+
if (fohm.fileIngestionMethod == FileIngestionMethod::Recursive) {
573+
dump.drainInto(conn->to);
574+
} else {
575+
std::string contents = dump.drain();
576+
dumpString(contents, conn->to);
577+
}
578+
}
579+
conn->to.warn = false;
582580
conn.processStderr();
583-
} catch (EndOfFile & e) { }
584-
throw;
585-
}
581+
} catch (SysError & e) {
582+
/* Daemon closed while we were sending the path. Probably OOM
583+
or I/O error. */
584+
if (e.errNo == EPIPE)
585+
try {
586+
conn.processStderr();
587+
} catch (EndOfFile & e) { }
588+
throw;
589+
}
586590

591+
}
592+
}, caMethod);
587593
return parseStorePath(readString(conn->from));
588594
}
589595
}
590596

597+
StorePath RemoteStore::addToStoreFromDump(Source & dump, const string & name,
598+
FileIngestionMethod method, HashType hashType, RepairFlag repair)
599+
{
600+
if (repair) throw Error("repairing is not supported when adding to store through the Nix daemon");
601+
StorePathSet references;
602+
return addCAToStore(dump, name, FixedOutputHashMethod{ .fileIngestionMethod = method, .hashType = hashType }, references);
603+
}
604+
591605

592606
void RemoteStore::addToStore(const ValidPathInfo & info, Source & source,
593607
RepairFlag repair, CheckSigsFlag checkSigs)
@@ -646,13 +660,8 @@ StorePath RemoteStore::addTextToStore(const string & name, const string & s,
646660
const StorePathSet & references, RepairFlag repair)
647661
{
648662
if (repair) throw Error("repairing is not supported when building through the Nix daemon");
649-
650-
auto conn(getConnection());
651-
conn->to << wopAddTextToStore << name << s;
652-
writeStorePaths(*this, conn->to, references);
653-
654-
conn.processStderr();
655-
return parseStorePath(readString(conn->from));
663+
StringSource source(s);
664+
return addCAToStore(source, name, TextHashMethod{}, references);
656665
}
657666

658667

src/libstore/remote-store.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public:
6363
void querySubstitutablePathInfos(const StorePathCAMap & paths,
6464
SubstitutablePathInfos & infos) override;
6565

66+
StorePath addCAToStore(Source & dump, const string & name, ContentAddressMethod caMethod, StorePathSet references);
67+
6668
StorePath addToStoreFromDump(Source & dump, const string & name,
6769
FileIngestionMethod method = FileIngestionMethod::Recursive, HashType hashAlgo = htSHA256, RepairFlag repair = NoRepair) override;
6870

src/libstore/worker-protocol.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef enum {
1818
wopQueryReferences = 5, // obsolete
1919
wopQueryReferrers = 6,
2020
wopAddToStore = 7,
21-
wopAddTextToStore = 8,
21+
wopAddTextToStore = 8, // obsolete since 1.25, Nix 3.0. Use wopAddToStore
2222
wopBuildPaths = 9,
2323
wopEnsurePath = 10,
2424
wopAddTempRoot = 11,

0 commit comments

Comments
 (0)