Skip to content

Commit 2d76bd8

Browse files
committed
binary-cache-store: upsertFile accept Source & instead of std::istream
1 parent dd0d006 commit 2d76bd8

File tree

6 files changed

+16
-39
lines changed

6 files changed

+16
-39
lines changed

src/libstore/binary-cache-store.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ std::optional<std::string> BinaryCacheStore::getNixCacheInfo()
7979
void BinaryCacheStore::upsertFile(
8080
const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint)
8181
{
82-
upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType, sizeHint);
82+
StringSource source{data};
83+
upsertFile(path, source, mimeType, sizeHint);
8384
}
8485

8586
void BinaryCacheStore::getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept
@@ -271,12 +272,10 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
271272

272273
/* Atomically write the NAR file. */
273274
if (repair || !fileExists(narInfo->url)) {
275+
AutoCloseFD fd = toDescriptor(open(fnTemp.c_str(), O_RDONLY));
276+
FdSource source{fd.get()};
274277
stats.narWrite++;
275-
upsertFile(
276-
narInfo->url,
277-
std::make_shared<std::fstream>(fnTemp, std::ios_base::in | std::ios_base::binary),
278-
"application/x-nix-nar",
279-
narInfo->fileSize);
278+
upsertFile(narInfo->url, source, "application/x-nix-nar", narInfo->fileSize);
280279
} else
281280
stats.narWriteAverted++;
282281

src/libstore/http-binary-cache-store.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,10 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
135135
}
136136

137137
void HttpBinaryCacheStore::upsertFile(
138-
const std::string & path,
139-
std::shared_ptr<std::basic_iostream<char>> istream,
140-
const std::string & mimeType,
141-
uint64_t sizeHint)
138+
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
142139
{
143140
auto req = makeRequest(path);
144-
145-
auto data = StreamToSourceAdapter(istream).drain();
146-
141+
auto data = source.drain();
147142
auto compressionMethod = getCompressionMethod(path);
148143

149144
if (compressionMethod) {

src/libstore/include/nix/store/binary-cache-store.hh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,8 @@ public:
100100

101101
virtual bool fileExists(const std::string & path) = 0;
102102

103-
virtual void upsertFile(
104-
const std::string & path,
105-
std::shared_ptr<std::basic_iostream<char>> istream,
106-
const std::string & mimeType,
107-
uint64_t sizeHint) = 0;
103+
virtual void
104+
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) = 0;
108105

109106
void upsertFile(
110107
const std::string & path,

src/libstore/include/nix/store/http-binary-cache-store.hh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ protected:
8080

8181
bool fileExists(const std::string & path) override;
8282

83-
void upsertFile(
84-
const std::string & path,
85-
std::shared_ptr<std::basic_iostream<char>> istream,
86-
const std::string & mimeType,
87-
uint64_t sizeHint) override;
83+
void
84+
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
8885

8986
FileTransferRequest makeRequest(std::string_view path);
9087

src/libstore/local-binary-cache-store.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,12 @@ struct LocalBinaryCacheStore : virtual BinaryCacheStore
5353

5454
bool fileExists(const std::string & path) override;
5555

56-
void upsertFile(
57-
const std::string & path,
58-
std::shared_ptr<std::basic_iostream<char>> istream,
59-
const std::string & mimeType,
60-
uint64_t sizeHint) override
56+
void upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override
6157
{
6258
auto path2 = config->binaryCacheDir + "/" + path;
6359
static std::atomic<int> counter{0};
6460
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
6561
AutoDelete del(tmp, false);
66-
StreamToSourceAdapter source(istream);
6762
writeFile(tmp, source);
6863
std::filesystem::rename(tmp, path2);
6964
del.cancel();

src/libstore/s3-binary-cache-store.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ class S3BinaryCacheStore : public virtual HttpBinaryCacheStore
1919
{
2020
}
2121

22-
void upsertFile(
23-
const std::string & path,
24-
std::shared_ptr<std::basic_iostream<char>> istream,
25-
const std::string & mimeType,
26-
uint64_t sizeHint) override;
22+
void
23+
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
2724

2825
private:
2926
ref<S3BinaryCacheStoreConfig> s3Config;
@@ -56,12 +53,9 @@ class S3BinaryCacheStore : public virtual HttpBinaryCacheStore
5653
};
5754

5855
void S3BinaryCacheStore::upsertFile(
59-
const std::string & path,
60-
std::shared_ptr<std::basic_iostream<char>> istream,
61-
const std::string & mimeType,
62-
uint64_t sizeHint)
56+
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
6357
{
64-
HttpBinaryCacheStore::upsertFile(path, istream, mimeType, sizeHint);
58+
HttpBinaryCacheStore::upsertFile(path, source, mimeType, sizeHint);
6559
}
6660

6761
std::string S3BinaryCacheStore::createMultipartUpload(

0 commit comments

Comments
 (0)