MatN work for versatile appimage creation for all types of os
[goodguy/cinelerra.git] / cinelerra-5.1 / tools / makeappimagetool / pipe_reader.cpp
1 // system headers
2 #include <algorithm>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <functional>
6 #include <cstring>
7 #include <stdexcept>
8
9 // local headers
10 #include "includes/pipe_reader.h"
11
12 pipe_reader::pipe_reader(int pipe_fd) : pipe_fd_(pipe_fd) {
13     // add O_NONBLOCK TO fd's flags to be able to read
14     auto flags = fcntl(pipe_fd_, F_GETFL, 0);
15     flags |= O_NONBLOCK;
16     fcntl(pipe_fd_, F_SETFL, flags);
17 }
18
19 size_t pipe_reader::read(std::vector<std::string::value_type>& buffer) const {
20     ssize_t rv = ::read(pipe_fd_, buffer.data(), buffer.size());
21
22     if (rv == -1) {
23         // no data available
24         if (errno == EAGAIN)
25             return 0;
26
27         // TODO: introduce custom subprocess_error
28         throw std::runtime_error{"unexpected error reading from pipe: " + std::string(strerror(errno))};
29     }
30
31     return rv;
32 }