replace "(cd path && git cmd)" with "git -C path cmd"
[got-portable.git] / lib / pollfd.c
blob395fd95072c2e4346a204fa2ac44eb11c2dc9cd4
1 /*
2 * Copyright (c) 2018, 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <errno.h>
20 #include <stdio.h>
21 #include <signal.h>
22 #include <poll.h>
23 #include <time.h>
24 #include <unistd.h>
26 #include "got_error.h"
28 #include "got_lib_poll.h"
30 const struct got_error *
31 got_poll_fd(int fd, int events, int timeout)
33 struct pollfd pfd[1];
34 struct timespec ts;
35 sigset_t sigset;
36 int n;
38 pfd[0].fd = fd;
39 pfd[0].events = events;
41 ts.tv_sec = timeout;
42 ts.tv_nsec = 0;
44 if (sigemptyset(&sigset) == -1)
45 return got_error_from_errno("sigemptyset");
46 if (sigaddset(&sigset, SIGWINCH) == -1)
47 return got_error_from_errno("sigaddset");
49 n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
50 if (n == -1)
51 return got_error_from_errno("ppoll");
52 if (n == 0) {
53 if (pfd[0].revents & POLLHUP)
54 return got_error(GOT_ERR_EOF);
55 return got_error(GOT_ERR_TIMEOUT);
57 if (pfd[0].revents & (POLLERR | POLLNVAL))
58 return got_error_from_errno("poll error");
59 if (pfd[0].revents & events)
60 return NULL;
61 if (pfd[0].revents & POLLHUP)
62 return got_error(GOT_ERR_EOF);
64 return got_error(GOT_ERR_INTERRUPT);
67 const struct got_error *
68 got_poll_read_full(int fd, size_t *len, void *buf, size_t bufsize,
69 size_t minbytes)
71 const struct got_error *err = NULL;
72 size_t have = 0;
73 ssize_t r;
75 if (minbytes > bufsize)
76 return got_error(GOT_ERR_NO_SPACE);
78 while (have < minbytes) {
79 err = got_poll_fd(fd, POLLIN, INFTIM);
80 if (err)
81 return err;
82 r = read(fd, buf + have, bufsize - have);
83 if (r == -1)
84 return got_error_from_errno("read");
85 if (r == 0)
86 return got_error(GOT_ERR_EOF);
87 have += r;
90 *len = have;
91 return NULL;
94 const struct got_error *
95 got_poll_write_full(int fd, const void *buf, off_t len)
97 const struct got_error *err = NULL;
98 off_t wlen = 0;
99 ssize_t w = 0;
101 while (wlen != len) {
102 if (wlen > 0) {
103 err = got_poll_fd(fd, POLLOUT, INFTIM);
104 if (err)
105 return err;
107 w = write(fd, buf + wlen, len - wlen);
108 if (w == -1) {
109 if (errno != EAGAIN)
110 return got_error_from_errno("write");
111 } else
112 wlen += w;
115 return NULL;