1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
13 #include <sys/types.h>
17 #include "time-util.h"
21 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
22 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
23 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
27 int fd_wait_for_event(int fd, int event, usec_t timeout);
29 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
31 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
35 for (j = 0; j < n; j++)
41 static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
44 for (j = 0; j < n; j++) {
47 if (_unlikely_(k <= 0))
50 sub = MIN(i[j].iov_len, k);
52 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
59 static inline bool FILE_SIZE_VALID(uint64_t l) {
60 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
61 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
63 return (l >> 63) == 0;
66 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
68 /* Same as above, but allows one extra value: -1 as indication for infinity. */
70 if (l == (uint64_t) -1)
73 return FILE_SIZE_VALID(l);
77 #define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
78 #define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
79 #define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
80 #define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)