From dfebfc835f7b8156a559314bcd1ecff739c14fd1 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 3 May 2016 14:45:50 +0200 Subject: [PATCH] Add a copyStorePath() utility function --- src/libstore/build.cc | 6 +----- src/libstore/store-api.cc | 13 +++++++++++++ src/libstore/store-api.hh | 5 +++++ src/libutil/ref.hh | 7 +++++++ src/nix/copy.cc | 6 +----- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/libstore/build.cc b/src/libstore/build.cc index 65df2eea5..3b9ecab1c 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -3222,11 +3222,7 @@ void SubstitutionGoal::tryToRun() /* Wake up the worker loop when we're done. */ Finally updateStats([this]() { outPipe.writeSide.close(); }); - StringSink sink; - sub->exportPaths({storePath}, false, sink); - - StringSource source(*sink.s); - worker.store.importPaths(false, source, 0); + copyStorePath(ref(sub), ref(worker.store.shared_from_this()), storePath); promise.set_value(); } catch (...) { diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 463e132e0..b03e4080a 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -357,6 +357,19 @@ const Store::Stats & Store::getStats() } +void copyStorePath(ref srcStore, ref dstStore, + const Path & storePath) +{ + auto info = srcStore->queryPathInfo(storePath); + + StringSink sink; + srcStore->exportPaths({storePath}, false, sink); + + StringSource source(*sink.s); + dstStore->importPaths(false, source, 0); +} + + ValidPathInfo decodeValidPathInfo(std::istream & str, bool hashGiven) { ValidPathInfo info; diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 29685c9d1..099aa1d67 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -502,6 +502,11 @@ Path computeStorePathForText(const string & name, const string & s, const PathSet & references); +/* Copy a path from one store to another. */ +void copyStorePath(ref srcStore, ref dstStore, + const Path & storePath); + + /* Remove the temporary roots file for this process. Any temporary root becomes garbage after this point unless it has been registered as a (permanent) root. */ diff --git a/src/libutil/ref.hh b/src/libutil/ref.hh index 9f5da0915..85afa2811 100644 --- a/src/libutil/ref.hh +++ b/src/libutil/ref.hh @@ -28,6 +28,13 @@ public: throw std::invalid_argument("null pointer cast to ref"); } + explicit ref(T * p) + : p(p) + { + if (!p) + throw std::invalid_argument("null pointer cast to ref"); + } + T* operator ->() const { return &*p; diff --git a/src/nix/copy.cc b/src/nix/copy.cc index be51fee62..de306cbf9 100644 --- a/src/nix/copy.cc +++ b/src/nix/copy.cc @@ -65,11 +65,7 @@ struct CmdCopy : StorePathsCommand if (!dstStore->isValidPath(storePath)) { Activity act(*logger, lvlInfo, format("copying ā€˜%sā€™...") % storePath); - StringSink sink; - srcStore->exportPaths({storePath}, false, sink); - - StringSource source(*sink.s); - dstStore->importPaths(false, source, 0); + copyStorePath(srcStore, dstStore, storePath); logger->incProgress(copiedLabel); } else