]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c004493c LP |
2 | #pragma once |
3 | ||
c004493c | 4 | #include <stdbool.h> |
11c3a366 TA |
5 | #include <stddef.h> |
6 | #include <stdint.h> | |
afc5dbf3 LP |
7 | #include <sys/types.h> |
8 | #include <sys/uio.h> | |
c004493c | 9 | |
11c3a366 | 10 | #include "macro.h" |
c004493c LP |
11 | #include "time-util.h" |
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 | int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll); | |
18 | ||
19 | int pipe_eof(int fd); | |
20 | ||
21 | int fd_wait_for_event(int fd, int event, usec_t timeout); | |
22 | ||
23 | ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length); | |
afc5dbf3 | 24 | |
da6053d0 LP |
25 | static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) { |
26 | size_t j, r = 0; | |
afc5dbf3 LP |
27 | |
28 | for (j = 0; j < n; j++) | |
29 | r += i[j].iov_len; | |
30 | ||
31 | return r; | |
32 | } | |
33 | ||
da6053d0 LP |
34 | static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) { |
35 | size_t j; | |
afc5dbf3 LP |
36 | |
37 | for (j = 0; j < n; j++) { | |
38 | size_t sub; | |
39 | ||
40 | if (_unlikely_(k <= 0)) | |
41 | break; | |
42 | ||
43 | sub = MIN(i[j].iov_len, k); | |
44 | i[j].iov_len -= sub; | |
45 | i[j].iov_base = (uint8_t*) i[j].iov_base + sub; | |
46 | k -= sub; | |
47 | } | |
48 | ||
49 | return k; | |
50 | } | |
a90fb858 LP |
51 | |
52 | static inline bool FILE_SIZE_VALID(uint64_t l) { | |
53 | /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than | |
54 | * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */ | |
55 | ||
56 | return (l >> 63) == 0; | |
57 | } | |
58 | ||
59 | static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) { | |
60 | ||
61 | /* Same as above, but allows one extra value: -1 as indication for infinity. */ | |
62 | ||
63 | if (l == (uint64_t) -1) | |
64 | return true; | |
65 | ||
66 | return FILE_SIZE_VALID(l); | |
67 | ||
68 | } | |
e6a7ec4b LP |
69 | |
70 | #define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) } | |
71 | #define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len) | |
72 | #define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string)) | |
73 | #define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string) | |
084eeb86 ZJS |
74 | |
75 | char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value); |