Use read_fd to get pipe command from stdin instead of ad-hoc code

read_fd will also now throw on read error instead of just returning
the data read so far as if nothing failed.
This commit is contained in:
Maxime Coste 2017-06-12 05:21:34 +01:00
parent 398b2b115c
commit 250886a9e1
2 changed files with 7 additions and 16 deletions

View File

@ -41,6 +41,9 @@ public:
file_access_error(StringView filename, file_access_error(StringView filename,
StringView error_desc) StringView error_desc)
: runtime_error(format("{}: {}", filename, error_desc)) {} : runtime_error(format("{}: {}", filename, error_desc)) {}
file_access_error(int fd, StringView error_desc)
: runtime_error(format("fd {}: {}", fd, error_desc)) {}
}; };
String parse_filename(StringView filename) String parse_filename(StringView filename)
@ -168,11 +171,10 @@ String read_fd(int fd, bool text)
String content; String content;
constexpr size_t bufsize = 256; constexpr size_t bufsize = 256;
char buf[bufsize]; char buf[bufsize];
while (true) while (ssize_t size = read(fd, buf, bufsize))
{ {
ssize_t size = read(fd, buf, bufsize); if (size == -1)
if (size == -1 or size == 0) throw file_access_error{fd, strerror(errno)};
break;
if (text) if (text)
{ {

View File

@ -744,20 +744,9 @@ int run_filter(StringView keystr, StringView commands, ConstArrayView<StringView
int run_pipe(StringView session) int run_pipe(StringView session)
{ {
char buf[512];
String command;
while (ssize_t count = read(0, buf, 512))
{
if (count < 0)
{
write_stderr("error while reading stdin\n");
return -1;
}
command += StringView{buf, buf + count};
}
try try
{ {
send_command(session, command); send_command(session, read_fd(0));
} }
catch (disconnected& e) catch (disconnected& e)
{ {