mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
binary-cache-store: UpsertFile accept Source & instead of std::istream
This commit is contained in:
parent
e3c41407f9
commit
e947c895ec
6 changed files with 16 additions and 38 deletions
|
|
@ -79,7 +79,8 @@ std::optional<std::string> BinaryCacheStore::getNixCacheInfo()
|
|||
void BinaryCacheStore::upsertFile(
|
||||
const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint)
|
||||
{
|
||||
upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType, sizeHint);
|
||||
StringSource source{data};
|
||||
upsertFile(path, source, mimeType, sizeHint);
|
||||
}
|
||||
|
||||
void BinaryCacheStore::getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept
|
||||
|
|
@ -271,12 +272,10 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
|
|||
|
||||
/* Atomically write the NAR file. */
|
||||
if (repair || !fileExists(narInfo->url)) {
|
||||
AutoCloseFD fd = toDescriptor(open(fnTemp.c_str(), O_RDONLY));
|
||||
FdSource source{fd.get()};
|
||||
stats.narWrite++;
|
||||
upsertFile(
|
||||
narInfo->url,
|
||||
std::make_shared<std::fstream>(fnTemp, std::ios_base::in | std::ios_base::binary),
|
||||
"application/x-nix-nar",
|
||||
narInfo->fileSize);
|
||||
upsertFile(narInfo->url, source, "application/x-nix-nar", narInfo->fileSize);
|
||||
} else
|
||||
stats.narWriteAverted++;
|
||||
|
||||
|
|
|
|||
|
|
@ -135,15 +135,11 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
|
|||
}
|
||||
|
||||
void HttpBinaryCacheStore::upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint)
|
||||
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
|
||||
{
|
||||
auto req = makeRequest(path);
|
||||
req.method = HttpMethod::PUT;
|
||||
auto data = StreamToSourceAdapter(istream).drain();
|
||||
|
||||
auto data = source.drain();
|
||||
auto compressionMethod = getCompressionMethod(path);
|
||||
|
||||
if (compressionMethod) {
|
||||
|
|
|
|||
|
|
@ -100,11 +100,8 @@ public:
|
|||
|
||||
virtual bool fileExists(const std::string & path) = 0;
|
||||
|
||||
virtual void upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint) = 0;
|
||||
virtual void
|
||||
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) = 0;
|
||||
|
||||
void upsertFile(
|
||||
const std::string & path,
|
||||
|
|
|
|||
|
|
@ -80,11 +80,8 @@ protected:
|
|||
|
||||
bool fileExists(const std::string & path) override;
|
||||
|
||||
void upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint) override;
|
||||
void
|
||||
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
|
||||
|
||||
FileTransferRequest makeRequest(std::string_view path);
|
||||
|
||||
|
|
|
|||
|
|
@ -53,17 +53,12 @@ protected:
|
|||
|
||||
bool fileExists(const std::string & path) override;
|
||||
|
||||
void upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint) override
|
||||
void upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override
|
||||
{
|
||||
auto path2 = config->binaryCacheDir + "/" + path;
|
||||
static std::atomic<int> counter{0};
|
||||
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
|
||||
AutoDelete del(tmp, false);
|
||||
StreamToSourceAdapter source(istream);
|
||||
writeFile(tmp, source);
|
||||
std::filesystem::rename(tmp, path2);
|
||||
del.cancel();
|
||||
|
|
|
|||
|
|
@ -20,11 +20,8 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
void upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint) override;
|
||||
void
|
||||
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
|
||||
|
||||
private:
|
||||
ref<S3BinaryCacheStoreConfig> s3Config;
|
||||
|
|
@ -70,12 +67,9 @@ private:
|
|||
};
|
||||
|
||||
void S3BinaryCacheStore::upsertFile(
|
||||
const std::string & path,
|
||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
||||
const std::string & mimeType,
|
||||
uint64_t sizeHint)
|
||||
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
|
||||
{
|
||||
HttpBinaryCacheStore::upsertFile(path, istream, mimeType, sizeHint);
|
||||
HttpBinaryCacheStore::upsertFile(path, source, mimeType, sizeHint);
|
||||
}
|
||||
|
||||
std::string S3BinaryCacheStore::createMultipartUpload(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue