Merge pull request #3546 from guibou/nix_readfile_on_0_sized_files
builtins.readFile: do not truncate content
This commit is contained in:
commit
74a1bfdcab
|
@ -419,7 +419,7 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto mapFile = fmt("/proc/%s/maps", ent->d_name);
|
auto mapFile = fmt("/proc/%s/maps", ent->d_name);
|
||||||
auto mapLines = tokenizeString<std::vector<string>>(readFile(mapFile, true), "\n");
|
auto mapLines = tokenizeString<std::vector<string>>(readFile(mapFile), "\n");
|
||||||
for (const auto & line : mapLines) {
|
for (const auto & line : mapLines) {
|
||||||
auto match = std::smatch{};
|
auto match = std::smatch{};
|
||||||
if (std::regex_match(line, match, mapRegex))
|
if (std::regex_match(line, match, mapRegex))
|
||||||
|
@ -427,7 +427,7 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto envFile = fmt("/proc/%s/environ", ent->d_name);
|
auto envFile = fmt("/proc/%s/environ", ent->d_name);
|
||||||
auto envString = readFile(envFile, true);
|
auto envString = readFile(envFile);
|
||||||
auto env_end = std::sregex_iterator{};
|
auto env_end = std::sregex_iterator{};
|
||||||
for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
|
for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
|
||||||
unchecked[i->str()].emplace(envFile);
|
unchecked[i->str()].emplace(envFile);
|
||||||
|
|
|
@ -148,6 +148,9 @@ struct StringSink : Sink
|
||||||
{
|
{
|
||||||
ref<std::string> s;
|
ref<std::string> s;
|
||||||
StringSink() : s(make_ref<std::string>()) { };
|
StringSink() : s(make_ref<std::string>()) { };
|
||||||
|
explicit StringSink(const size_t reservedSize) : s(make_ref<std::string>()) {
|
||||||
|
s->reserve(reservedSize);
|
||||||
|
};
|
||||||
StringSink(ref<std::string> s) : s(s) { };
|
StringSink(ref<std::string> s) : s(s) { };
|
||||||
void operator () (const unsigned char * data, size_t len) override;
|
void operator () (const unsigned char * data, size_t len) override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -316,19 +316,16 @@ string readFile(int fd)
|
||||||
if (fstat(fd, &st) == -1)
|
if (fstat(fd, &st) == -1)
|
||||||
throw SysError("statting file");
|
throw SysError("statting file");
|
||||||
|
|
||||||
std::vector<unsigned char> buf(st.st_size);
|
return drainFD(fd, true, st.st_size);
|
||||||
readFull(fd, buf.data(), st.st_size);
|
|
||||||
|
|
||||||
return string((char *) buf.data(), st.st_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string readFile(const Path & path, bool drain)
|
string readFile(const Path & path)
|
||||||
{
|
{
|
||||||
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError(format("opening file '%1%'") % path);
|
throw SysError(format("opening file '%1%'") % path);
|
||||||
return drain ? drainFD(fd.get()) : readFile(fd.get());
|
return readFile(fd.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -665,9 +662,9 @@ void writeFull(int fd, const string & s, bool allowInterrupts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string drainFD(int fd, bool block)
|
string drainFD(int fd, bool block, const size_t reserveSize)
|
||||||
{
|
{
|
||||||
StringSink sink;
|
StringSink sink(reserveSize);
|
||||||
drainFD(fd, sink, block);
|
drainFD(fd, sink, block);
|
||||||
return std::move(*sink.s);
|
return std::move(*sink.s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,7 @@ unsigned char getFileType(const Path & path);
|
||||||
|
|
||||||
/* Read the contents of a file into a string. */
|
/* Read the contents of a file into a string. */
|
||||||
string readFile(int fd);
|
string readFile(int fd);
|
||||||
string readFile(const Path & path, bool drain = false);
|
string readFile(const Path & path);
|
||||||
void readFile(const Path & path, Sink & sink);
|
void readFile(const Path & path, Sink & sink);
|
||||||
|
|
||||||
/* Write a string to a file. */
|
/* Write a string to a file. */
|
||||||
|
@ -162,7 +162,7 @@ MakeError(EndOfFile, Error);
|
||||||
|
|
||||||
|
|
||||||
/* Read a file descriptor until EOF occurs. */
|
/* Read a file descriptor until EOF occurs. */
|
||||||
string drainFD(int fd, bool block = true);
|
string drainFD(int fd, bool block = true, const size_t reserveSize=0);
|
||||||
|
|
||||||
void drainFD(int fd, Sink & sink, bool block = true);
|
void drainFD(int fd, Sink & sink, bool block = true);
|
||||||
|
|
||||||
|
|
|
@ -140,7 +140,7 @@ struct CmdLsNar : Command, MixLs
|
||||||
|
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
list(makeNarAccessor(make_ref<std::string>(readFile(narPath, true))));
|
list(makeNarAccessor(make_ref<std::string>(readFile(narPath))));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue