hoist out handle_nonblock function for xread and xwrite
At least for me, this improves the readability of xread and xwrite; hopefully allowing missing "continue" statements to be spotted more easily. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Wong committed
Jul 10, 2016 at 08:20 UTC
d751dd11ae16fd4e2410d3a32c8e2d951fafc923
1 file changed
+20
-28
wrapper.c
+20
-28
@@ -224,6 +224,24 @@ int xopen(const char *path, int oflag, ...)
224
}
225
}
226
227
+static int handle_nonblock(int fd, short poll_events, int err)
228
+{
229
+ struct pollfd pfd;
230
+
231
+ if (err != EAGAIN && err != EWOULDBLOCK)
232
+ return 0;
233
+
234
+ pfd.fd = fd;
235
+ pfd.events = poll_events;
236
+
237
+ /*
238
+ * no need to check for errors, here;
239
+ * a subsequent read/write will detect unrecoverable errors
240
+ */
241
+ poll(&pfd, 1, -1);
242
+ return 1;
243
+}
244
+
245
/*
246
* xread() is the same a read(), but it automatically restarts read()
247
* operations with a recoverable error (EAGAIN and EINTR). xread()
@@ -239,21 +257,8 @@ ssize_t xread(int fd, void *buf, size_t len)
257
if (nr < 0) {
258
if (errno == EINTR)
259
continue;
242
- if (errno == EAGAIN || errno == EWOULDBLOCK) {
243
- struct pollfd pfd;
244
- pfd.events = POLLIN;
245
- pfd.fd = fd;
246
- /*
247
- * it is OK if this poll() failed; we
248
- * want to leave this infinite loop
249
- * only when read() returns with
250
- * success, or an expected failure,
251
- * which would be checked by the next
252
- * call to read(2).
253
- */
254
- poll(&pfd, 1, -1);
260
+ if (handle_nonblock(fd, POLLIN, errno))
261
continue;
256
- }
262
}
263
return nr;
264
}
@@ -274,21 +279,8 @@ ssize_t xwrite(int fd, const void *buf, size_t len)
279
if (nr < 0) {
280
if (errno == EINTR)
281
continue;
277
- if (errno == EAGAIN || errno == EWOULDBLOCK) {
278
- struct pollfd pfd;
279
- pfd.events = POLLOUT;
280
- pfd.fd = fd;
281
- /*
282
- * it is OK if this poll() failed; we
283
- * want to leave this infinite loop
284
- * only when write() returns with
285
- * success, or an expected failure,
286
- * which would be checked by the next
287
- * call to write(2).
288
- */
289
- poll(&pfd, 1, -1);
282
+ if (handle_nonblock(fd, POLLOUT, errno))
283
continue;
291
- }
284
}
285
286
return nr;