summaryrefslogtreecommitdiff
path: root/src/io.cc
blob: bc70eeebc771395877703b4207ea4bf4d846da92 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "common.hh"

#include <errno.h>
#include <unistd.h>

#include "io.hh"

namespace {

bool do_read(int fd, void* dst, size_t max, size_t* got) {
  auto d = reinterpret_cast<char*>(dst);
  size_t pos = 0;
  while (true) {
    auto ret = ::read(fd, d + pos, max - pos);
    if (ret < 0) {
      if (errno == EINTR) continue;
      if (got && (errno == EAGAIN || errno == EWOULDBLOCK)) {
        *got = pos;
        return true;
      }
      return false;
    }
    if (ret == 0) {
      if (got) {
        *got = pos;
        return true;
      }
      return false;
    }
    pos += ret;
    if (got) {
      *got = pos;
      return true;
    }
    if (pos == max) {
      return true;
    }
  }
}

bool do_write(int fd, void const* src, size_t size, size_t* wrote) {
  auto s = reinterpret_cast<char const*>(src);
  size_t pos = 0;
  while (true) {
    auto ret = ::write(fd, s + pos, size - pos);
    if (ret < 0) {
      if (errno == EINTR) continue;
      if (wrote && (errno == EAGAIN || errno == EWOULDBLOCK)) {
        *wrote = pos;
        return true;
      }
      return false;
    }
    if (ret == 0) {
      if (wrote) {
        *wrote = pos;
        return true;
      }
      return false;
    }
    pos += ret;
    if (pos == size) {
      if (wrote) *wrote = pos;
      return true;
    }
  }
}

}  // namespace

namespace io {

pipe::pipe() {
  fd_[0] = fd_[1] = -1;
}

pipe::pipe(pipe&& pipe) {
  fd_[0] = pipe.fd_[0];
  fd_[1] = pipe.fd_[1];
  pipe.fd_[0] = -1;
  pipe.fd_[1] = -1;
}

pipe::~pipe() {
  reset();
}

bool pipe::open() {
  reset();
  if (::pipe(fd_) == 0) return true;
  fd_[0] = fd_[1] = -1;
  return false;
}

void pipe::reset() {
  if (fd_[0] == -1) return;
  ::close(fd_[0]);
  ::close(fd_[1]);
  fd_[0] = fd_[1] = -1;
}

void pipe::swap(pipe& pipe) {
  auto x = pipe.fd_[0];
  pipe.fd_[0] = fd_[0];
  fd_[0] = x;
  x = pipe.fd_[1];
  pipe.fd_[1] = fd_[1];
  fd_[1] = x;
}

unique_fd::unique_fd()
  : fd_(-1) {
}

unique_fd::unique_fd(int fd)
  : fd_(fd) {
}

unique_fd::unique_fd(unique_fd&& fd)
  : fd_(fd.release()) {
}

unique_fd::~unique_fd() {
  reset();
}

void unique_fd::reset() {
  if (fd_ == -1) return;
  ::close(fd_);
  fd_ = -1;
}

void unique_fd::reset(int fd) {
  if (fd_ == fd) return;
  reset();
  fd_ = fd;
}

void unique_fd::swap(unique_fd& fd) {
  auto x = fd.fd_;
  fd.fd_ = fd_;
  fd_ = x;
}

int unique_fd::release() {
  auto ret = fd_;
  fd_ = -1;
  return ret;
}

bool read(pipe const& pipe, void* dst, size_t max, size_t* got) {
  return do_read(pipe.read(), dst, max, got);
}

bool write(pipe const& pipe, void const* src, size_t size, size_t* wrote) {
  return do_write(pipe.write(), src, size, wrote);
}

bool read(unique_fd const& fd, void* dst, size_t max, size_t* got) {
  return do_read(fd.get(), dst, max, got);
}

bool write(unique_fd const& fd, void const* src, size_t size, size_t* wrote) {
  return do_write(fd.get(), src, size, wrote);
}

}  // namespace io