blob: 0fb31d94011725ec1ec57cbb066845517df784df (
plain)
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
32
33
34
35
36
37
38
39
40
41
|
#include "common.hh"
#include "io.hh"
#include <errno.h>
#include <unistd.h>
namespace io {
bool read_all(int fd, std::string* out) {
char buf[32768];
while (true) {
auto ret = read(fd, buf, sizeof(buf));
if (ret == 0)
return true;
if (ret < 0) {
if (errno == EINTR)
continue;
return false;
}
out->append(buf, ret);
}
}
bool write_all(int fd, std::string const& in) {
size_t offset = 0;
while (offset < in.size()) {
auto ret = write(fd, in.data() + offset, in.size() - offset);
if (ret == 0)
return false;
if (ret < 0) {
if (errno == EINTR)
continue;
return false;
}
offset += ret;
}
return true;
}
} // namespace io
|