mirror of
https://github.com/NixOS/nix
synced 2025-06-30 07:33:16 +02:00
Merge remote-tracking branch 'origin/master' into flakes
This commit is contained in:
commit
9a18f544ac
16 changed files with 307 additions and 65 deletions
|
@ -256,23 +256,9 @@ Hash hashString(HashType ht, const string & s)
|
|||
|
||||
Hash hashFile(HashType ht, const Path & path)
|
||||
{
|
||||
Ctx ctx;
|
||||
Hash hash(ht);
|
||||
start(ht, ctx);
|
||||
|
||||
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (!fd) throw SysError(format("opening file '%1%'") % path);
|
||||
|
||||
std::vector<unsigned char> buf(8192);
|
||||
ssize_t n;
|
||||
while ((n = read(fd.get(), buf.data(), buf.size()))) {
|
||||
checkInterrupt();
|
||||
if (n == -1) throw SysError(format("reading file '%1%'") % path);
|
||||
update(ht, ctx, buf.data(), n);
|
||||
}
|
||||
|
||||
finish(ht, ctx, hash.hash);
|
||||
return hash;
|
||||
HashSink sink(ht);
|
||||
readFile(path, sink);
|
||||
return sink.finish().first;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -123,7 +123,12 @@ string printHashType(HashType ht);
|
|||
|
||||
union Ctx;
|
||||
|
||||
class HashSink : public BufferedSink
|
||||
struct AbstractHashSink : virtual Sink
|
||||
{
|
||||
virtual HashResult finish() = 0;
|
||||
};
|
||||
|
||||
class HashSink : public BufferedSink, public AbstractHashSink
|
||||
{
|
||||
private:
|
||||
HashType ht;
|
||||
|
@ -134,8 +139,8 @@ public:
|
|||
HashSink(HashType ht);
|
||||
HashSink(const HashSink & h);
|
||||
~HashSink();
|
||||
void write(const unsigned char * data, size_t len);
|
||||
HashResult finish();
|
||||
void write(const unsigned char * data, size_t len) override;
|
||||
HashResult finish() override;
|
||||
HashResult currentHash();
|
||||
};
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ struct Sink
|
|||
|
||||
|
||||
/* A buffered abstract sink. */
|
||||
struct BufferedSink : Sink
|
||||
struct BufferedSink : virtual Sink
|
||||
{
|
||||
size_t bufSize, bufPos;
|
||||
std::unique_ptr<unsigned char[]> buffer;
|
||||
|
|
|
@ -1265,6 +1265,19 @@ string replaceStrings(const std::string & s,
|
|||
}
|
||||
|
||||
|
||||
std::string rewriteStrings(const std::string & _s, const StringMap & rewrites)
|
||||
{
|
||||
auto s = _s;
|
||||
for (auto & i : rewrites) {
|
||||
if (i.first == i.second) continue;
|
||||
size_t j = 0;
|
||||
while ((j = s.find(i.first, j)) != string::npos)
|
||||
s.replace(j, i.first.size(), i.second);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
string statusToString(int status)
|
||||
{
|
||||
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
||||
|
|
|
@ -386,6 +386,20 @@ string replaceStrings(const std::string & s,
|
|||
const std::string & from, const std::string & to);
|
||||
|
||||
|
||||
std::string rewriteStrings(const std::string & s, const StringMap & rewrites);
|
||||
|
||||
|
||||
/* If a set contains 'from', remove it and insert 'to'. */
|
||||
template<typename T>
|
||||
void replaceInSet(std::set<T> & set, const T & from, const T & to)
|
||||
{
|
||||
auto i = set.find(from);
|
||||
if (i == set.end()) return;
|
||||
set.erase(i);
|
||||
set.insert(to);
|
||||
}
|
||||
|
||||
|
||||
/* Convert the exit status of a child as returned by wait() into an
|
||||
error string. */
|
||||
string statusToString(int status);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue