-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathpipe_reader.cpp
More file actions
31 lines (25 loc) · 838 Bytes
/
pipe_reader.cpp
File metadata and controls
31 lines (25 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// system headers
#include <algorithm>
#include <fcntl.h>
#include <unistd.h>
#include <functional>
#include <cstring>
// local headers
#include "linuxdeploy/subprocess/pipe_reader.h"
pipe_reader::pipe_reader(int pipe_fd) : pipe_fd_(pipe_fd) {
// add O_NONBLOCK TO fd's flags to be able to read
auto flags = fcntl(pipe_fd_, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(pipe_fd_, F_SETFL, flags);
}
size_t pipe_reader::read(std::vector<std::string::value_type>& buffer) const {
ssize_t rv = ::read(pipe_fd_, buffer.data(), buffer.size());
if (rv == -1) {
// no data available
if (errno == EAGAIN)
return 0;
// TODO: introduce custom subprocess_error
throw std::runtime_error{"unexpected error reading from pipe: " + std::string(strerror(errno))};
}
return rv;
}