read(): Use char * instead of unsigned char *

This gets rid of some pointless casts.
This commit is contained in:
Eelco Dolstra 2020-12-02 14:10:56 +01:00
parent faa31f4084
commit 1b79b5b983
11 changed files with 56 additions and 56 deletions

View file

@ -165,7 +165,7 @@ struct TunnelSource : BufferedSource
Source & from;
BufferedSink & to;
TunnelSource(Source & from, BufferedSink & to) : from(from), to(to) { }
size_t readUnbuffered(unsigned char * data, size_t len) override
size_t readUnbuffered(char * data, size_t len) override
{
to << STDERR_READ << len;
to.flush();

View file

@ -1143,7 +1143,7 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, const string & name,
dump.resize(oldSize + want);
auto got = 0;
try {
got = source.read((uint8_t *) dump.data() + oldSize, want);
got = source.read(dump.data() + oldSize, want);
} catch (EndOfFile &) {
inMemory = true;
break;

View file

@ -96,7 +96,7 @@ struct NarAccessor : public FSAccessor
NarMember{FSAccessor::Type::tSymlink, false, 0, 0, target});
}
size_t read(unsigned char * data, size_t len) override
size_t read(char * data, size_t len) override
{
auto n = source.read(data, len);
pos += n;

View file

@ -75,7 +75,7 @@ std::pair<ref<FSAccessor>, Path> RemoteFSAccessor::fetch(const Path & path_)
throw SysError("seeking in '%s'", cacheFile);
std::string buf(length, 0);
readFull(fd.get(), (unsigned char *) buf.data(), length);
readFull(fd.get(), buf.data(), length);
return buf;
});

View file

@ -856,7 +856,7 @@ std::exception_ptr RemoteStore::Connection::processStderr(Sink * sink, Source *
else if (msg == STDERR_READ) {
if (!source) throw Error("no source");
size_t len = readNum<size_t>(from);
auto buf = std::make_unique<unsigned char[]>(len);
auto buf = std::make_unique<char[]>(len);
writeString({(const char *) buf.get(), source->read(buf.get(), len)}, to);
to.flush();
}

View file

@ -50,14 +50,14 @@ static void dumpContents(const Path & path, size_t size,
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd) throw SysError("opening file '%1%'", path);
std::vector<unsigned char> buf(65536);
std::vector<char> buf(65536);
size_t left = size;
while (left > 0) {
auto n = std::min(left, buf.size());
readFull(fd.get(), buf.data(), n);
left -= n;
sink({(char *) buf.data(), n});
sink({buf.data(), n});
}
writePadding(size, sink);
@ -155,14 +155,14 @@ static void parseContents(ParseSink & sink, Source & source, const Path & path)
sink.preallocateContents(size);
uint64_t left = size;
std::vector<unsigned char> buf(65536);
std::vector<char> buf(65536);
while (left) {
checkInterrupt();
auto n = buf.size();
if ((uint64_t)n > left) n = left;
source(buf.data(), n);
sink.receiveContents({(char *) buf.data(), n});
sink.receiveContents({buf.data(), n});
left -= n;
}

View file

@ -84,7 +84,7 @@ bool FdSink::good()
}
void Source::operator () (unsigned char * data, size_t len)
void Source::operator () (char * data, size_t len)
{
while (len) {
size_t n = read(data, len);
@ -96,12 +96,12 @@ void Source::operator () (unsigned char * data, size_t len)
void Source::drainInto(Sink & sink)
{
std::string s;
std::vector<unsigned char> buf(8192);
std::vector<char> buf(8192);
while (true) {
size_t n;
try {
n = read(buf.data(), buf.size());
sink({(char *) buf.data(), n});
sink({buf.data(), n});
} catch (EndOfFile &) {
break;
}
@ -117,9 +117,9 @@ std::string Source::drain()
}
size_t BufferedSource::read(unsigned char * data, size_t len)
size_t BufferedSource::read(char * data, size_t len)
{
if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
if (!buffer) buffer = decltype(buffer)(new char[bufSize]);
if (!bufPosIn) bufPosIn = readUnbuffered(buffer.get(), bufSize);
@ -138,12 +138,12 @@ bool BufferedSource::hasData()
}
size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
size_t FdSource::readUnbuffered(char * data, size_t len)
{
ssize_t n;
do {
checkInterrupt();
n = ::read(fd, (char *) data, len);
n = ::read(fd, data, len);
} while (n == -1 && errno == EINTR);
if (n == -1) { _good = false; throw SysError("reading from file"); }
if (n == 0) { _good = false; throw EndOfFile("unexpected end-of-file"); }
@ -158,10 +158,10 @@ bool FdSource::good()
}
size_t StringSource::read(unsigned char * data, size_t len)
size_t StringSource::read(char * data, size_t len)
{
if (pos == s.size()) throw EndOfFile("end of string reached");
size_t n = s.copy((char *) data, len, pos);
size_t n = s.copy(data, len, pos);
pos += n;
return n;
}
@ -225,7 +225,7 @@ std::unique_ptr<Source> sinkToSource(
std::string cur;
size_t pos = 0;
size_t read(unsigned char * data, size_t len) override
size_t read(char * data, size_t len) override
{
if (!coro)
coro = coro_t::pull_type(VirtualStackAllocator{}, [&](coro_t::push_type & yield) {
@ -244,7 +244,7 @@ std::unique_ptr<Source> sinkToSource(
}
auto n = std::min(cur.size() - pos, len);
memcpy(data, (unsigned char *) cur.data() + pos, n);
memcpy(data, cur.data() + pos, n);
pos += n;
return n;
@ -258,9 +258,9 @@ std::unique_ptr<Source> sinkToSource(
void writePadding(size_t len, Sink & sink)
{
if (len % 8) {
unsigned char zero[8];
char zero[8];
memset(zero, 0, sizeof(zero));
sink({(char *) zero, 8 - (len % 8)});
sink({zero, 8 - (len % 8)});
}
}
@ -321,7 +321,7 @@ Sink & operator << (Sink & sink, const Error & ex)
void readPadding(size_t len, Source & source)
{
if (len % 8) {
unsigned char zero[8];
char zero[8];
size_t n = 8 - (len % 8);
source(zero, n);
for (unsigned int i = 0; i < n; i++)
@ -330,7 +330,7 @@ void readPadding(size_t len, Source & source)
}
size_t readString(unsigned char * buf, size_t max, Source & source)
size_t readString(char * buf, size_t max, Source & source)
{
auto len = readNum<size_t>(source);
if (len > max) throw SerialisationError("string is too long");
@ -345,7 +345,7 @@ string readString(Source & source, size_t max)
auto len = readNum<size_t>(source);
if (len > max) throw SerialisationError("string is too long");
std::string res(len, 0);
source((unsigned char*) res.data(), len);
source(res.data(), len);
readPadding(len, source);
return res;
}
@ -404,7 +404,7 @@ void StringSink::operator () (std::string_view data)
s->append(data);
}
size_t ChainSource::read(unsigned char * data, size_t len)
size_t ChainSource::read(char * data, size_t len)
{
if (useSecond) {
return source2.read(data, len);

View file

@ -51,12 +51,12 @@ struct Source
/* Store exactly len bytes in the buffer pointed to by data.
It blocks until all the requested data is available, or throws
an error if it is not going to be available. */
void operator () (unsigned char * data, size_t len);
void operator () (char * data, size_t len);
/* Store up to len in the buffer pointed to by data, and
return the number of bytes stored. It blocks until at least
one byte is available. */
virtual size_t read(unsigned char * data, size_t len) = 0;
virtual size_t read(char * data, size_t len) = 0;
virtual bool good() { return true; }
@ -71,18 +71,18 @@ struct Source
struct BufferedSource : Source
{
size_t bufSize, bufPosIn, bufPosOut;
std::unique_ptr<unsigned char[]> buffer;
std::unique_ptr<char[]> buffer;
BufferedSource(size_t bufSize = 32 * 1024)
: bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(nullptr) { }
size_t read(unsigned char * data, size_t len) override;
size_t read(char * data, size_t len) override;
bool hasData();
protected:
/* Underlying read call, to be overridden. */
virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
virtual size_t readUnbuffered(char * data, size_t len) = 0;
};
@ -138,7 +138,7 @@ struct FdSource : BufferedSource
bool good() override;
protected:
size_t readUnbuffered(unsigned char * data, size_t len) override;
size_t readUnbuffered(char * data, size_t len) override;
private:
bool _good = true;
};
@ -163,7 +163,7 @@ struct StringSource : Source
const string & s;
size_t pos;
StringSource(const string & _s) : s(_s), pos(0) { }
size_t read(unsigned char * data, size_t len) override;
size_t read(char * data, size_t len) override;
};
@ -187,10 +187,10 @@ struct TeeSource : Source
Sink & sink;
TeeSource(Source & orig, Sink & sink)
: orig(orig), sink(sink) { }
size_t read(unsigned char * data, size_t len)
size_t read(char * data, size_t len)
{
size_t n = orig.read(data, len);
sink({(char *) data, n});
sink({data, n});
return n;
}
};
@ -202,7 +202,7 @@ struct SizedSource : Source
size_t remain;
SizedSource(Source & orig, size_t size)
: orig(orig), remain(size) { }
size_t read(unsigned char * data, size_t len)
size_t read(char * data, size_t len)
{
if (this->remain <= 0) {
throw EndOfFile("sized: unexpected end-of-file");
@ -216,7 +216,7 @@ struct SizedSource : Source
/* Consume the original source until no remain data is left to consume. */
size_t drainAll()
{
std::vector<unsigned char> buf(8192);
std::vector<char> buf(8192);
size_t sum = 0;
while (this->remain > 0) {
size_t n = read(buf.data(), buf.size());
@ -256,13 +256,13 @@ struct LambdaSink : Sink
/* Convert a function into a source. */
struct LambdaSource : Source
{
typedef std::function<size_t(unsigned char *, size_t)> lambda_t;
typedef std::function<size_t(char *, size_t)> lambda_t;
lambda_t lambda;
LambdaSource(const lambda_t & lambda) : lambda(lambda) { }
size_t read(unsigned char * data, size_t len) override
size_t read(char * data, size_t len) override
{
return lambda(data, len);
}
@ -278,7 +278,7 @@ struct ChainSource : Source
: source1(s1), source2(s2)
{ }
size_t read(unsigned char * data, size_t len) override;
size_t read(char * data, size_t len) override;
};
@ -322,7 +322,7 @@ template<typename T>
T readNum(Source & source)
{
unsigned char buf[8];
source(buf, sizeof(buf));
source((char *) buf, sizeof(buf));
uint64_t n =
((uint64_t) buf[0]) |
@ -354,7 +354,7 @@ inline uint64_t readLongLong(Source & source)
void readPadding(size_t len, Source & source);
size_t readString(unsigned char * buf, size_t max, Source & source);
size_t readString(char * buf, size_t max, Source & source);
string readString(Source & source, size_t max = std::numeric_limits<size_t>::max());
template<class T> T readStrings(Source & source);
@ -386,9 +386,9 @@ struct StreamToSourceAdapter : Source
: istream(istream)
{ }
size_t read(unsigned char * data, size_t len) override
size_t read(char * data, size_t len) override
{
if (!istream->read((char *) data, len)) {
if (!istream->read(data, len)) {
if (istream->eof()) {
if (istream->gcount() == 0)
throw EndOfFile("end of file");
@ -411,7 +411,7 @@ struct FramedSource : Source
{
Source & from;
bool eof = false;
std::vector<unsigned char> pending;
std::vector<char> pending;
size_t pos = 0;
FramedSource(Source & from) : from(from)
@ -423,13 +423,13 @@ struct FramedSource : Source
while (true) {
auto n = readInt(from);
if (!n) break;
std::vector<unsigned char> data(n);
std::vector<char> data(n);
from(data.data(), n);
}
}
}
size_t read(unsigned char * data, size_t len) override
size_t read(char * data, size_t len) override
{
if (eof) throw EndOfFile("reached end of FramedSource");
@ -439,7 +439,7 @@ struct FramedSource : Source
eof = true;
return 0;
}
pending = std::vector<unsigned char>(len);
pending = std::vector<char>(len);
pos = 0;
from(pending.data(), len);
}

View file

@ -66,7 +66,7 @@ private:
*buffer = self->buffer.data();
try {
return self->source->read(self->buffer.data(), 4096);
return self->source->read((char *) self->buffer.data(), 4096);
} catch (EndOfFile &) {
return 0;
} catch (std::exception & err) {

View file

@ -340,13 +340,13 @@ void writeFile(const Path & path, Source & source, mode_t mode)
if (!fd)
throw SysError("opening file '%1%'", path);
std::vector<unsigned char> buf(64 * 1024);
std::vector<char> buf(64 * 1024);
try {
while (true) {
try {
auto n = source.read(buf.data(), buf.size());
writeFull(fd.get(), {(char *) buf.data(), n});
writeFull(fd.get(), {buf.data(), n});
} catch (EndOfFile &) { break; }
}
} catch (Error & e) {
@ -632,11 +632,11 @@ void replaceSymlink(const Path & target, const Path & link,
}
void readFull(int fd, unsigned char * buf, size_t count)
void readFull(int fd, char * buf, size_t count)
{
while (count) {
checkInterrupt();
ssize_t res = read(fd, (char *) buf, count);
ssize_t res = read(fd, buf, count);
if (res == -1) {
if (errno == EINTR) continue;
throw SysError("reading from file");
@ -1137,7 +1137,7 @@ void runProgram2(const RunOptions & options)
in.readSide = -1;
writerThread = std::thread([&]() {
try {
std::vector<unsigned char> buf(8 * 1024);
std::vector<char> buf(8 * 1024);
while (true) {
size_t n;
try {
@ -1145,7 +1145,7 @@ void runProgram2(const RunOptions & options)
} catch (EndOfFile &) {
break;
}
writeFull(in.writeSide.get(), {(char *) buf.data(), n});
writeFull(in.writeSide.get(), {buf.data(), n});
}
promise.set_value();
} catch (...) {

View file

@ -155,7 +155,7 @@ void replaceSymlink(const Path & target, const Path & link,
/* Wrappers arount read()/write() that read/write exactly the
requested number of bytes. */
void readFull(int fd, unsigned char * buf, size_t count);
void readFull(int fd, char * buf, size_t count);
void writeFull(int fd, std::string_view s, bool allowInterrupts = true);
MakeError(EndOfFile, Error);