Debian Codegraph @

src/basic/io-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5
6 /* On most architectures POLL* and EPOLL* share numeric values, but sparc allocates POLLRDHUP at
7 * 0x800 while EPOLLRDHUP is 0x2000 everywhere. Always go through these helpers when crossing
8 * between poll() and epoll() event masks. Bits outside the mapped set (EPOLLET, EPOLLONESHOT,
9 * POLLNVAL, …) are dropped. */
10 uint32_t poll_events_to_epoll(uint32_t events);
11 uint32_t epoll_events_to_poll(uint32_t events);
12
13 int flush_fd(int fd);
14
15 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
16 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
17
18 int loop_write_full(int fd, const void *buf, size_t nbytes, usec_t timeout) _nonnull_if_nonzero_(2, 3);
19 static inline int loop_write(int fd, const void *buf, size_t nbytes) {
20 return loop_write_full(fd, buf, nbytes, 0);
21 }
22
23 int pipe_eof(int fd);
24
25 int ppoll_usec_full(struct pollfd *fds, size_t n_fds, usec_t timeout, const sigset_t *ss) _nonnull_if_nonzero_(1, 2);
26 _nonnull_if_nonzero_(1, 2) static inline int ppoll_usec(struct pollfd *fds, size_t n_fds, usec_t timeout) {
27 return ppoll_usec_full(fds, n_fds, timeout, NULL);
28 }
29
30 int fd_wait_for_event(int fd, int event, usec_t timeout);
31
32 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
33
34 static inline bool FILE_SIZE_VALID(uint64_t l) {
35 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
36 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
37
38 return (l >> 63) == 0;
39 }
40
41 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
42
43 /* Same as above, but allows one extra value: -1 as indication for infinity. */
44
45 if (l == UINT64_MAX)
46 return true;
47
48 return FILE_SIZE_VALID(l);
49 }