]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/io-util.h
tree-wide: use UINT64_MAX or friends
[thirdparty/systemd.git] / src / basic / io-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c004493c
LP
2#pragma once
3
c4febde9 4#include <poll.h>
c004493c 5#include <stdbool.h>
11c3a366
TA
6#include <stddef.h>
7#include <stdint.h>
afc5dbf3
LP
8#include <sys/types.h>
9#include <sys/uio.h>
c004493c 10
11c3a366 11#include "macro.h"
c004493c
LP
12#include "time-util.h"
13
14int flush_fd(int fd);
15
16ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
17int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
18int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
19
20int pipe_eof(int fd);
21
c4febde9 22int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout);
c004493c
LP
23int fd_wait_for_event(int fd, int event, usec_t timeout);
24
25ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
afc5dbf3 26
da6053d0
LP
27static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, size_t n) {
28 size_t j, r = 0;
afc5dbf3
LP
29
30 for (j = 0; j < n; j++)
31 r += i[j].iov_len;
32
33 return r;
34}
35
da6053d0
LP
36static inline size_t IOVEC_INCREMENT(struct iovec *i, size_t n, size_t k) {
37 size_t j;
afc5dbf3
LP
38
39 for (j = 0; j < n; j++) {
40 size_t sub;
41
42 if (_unlikely_(k <= 0))
43 break;
44
45 sub = MIN(i[j].iov_len, k);
46 i[j].iov_len -= sub;
47 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
48 k -= sub;
49 }
50
51 return k;
52}
a90fb858
LP
53
54static inline bool FILE_SIZE_VALID(uint64_t l) {
55 /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
56 * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
57
58 return (l >> 63) == 0;
59}
60
61static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
62
63 /* Same as above, but allows one extra value: -1 as indication for infinity. */
64
f5fbe71d 65 if (l == UINT64_MAX)
a90fb858
LP
66 return true;
67
68 return FILE_SIZE_VALID(l);
69
70}
e6a7ec4b
LP
71
72#define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
73#define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
74#define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
75#define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)
084eeb86
ZJS
76
77char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
30a0554e 78char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value);
11e6d971
FB
79
80struct iovec_wrapper {
81 struct iovec *iovec;
82 size_t count;
83 size_t size_bytes;
84};
85
86struct iovec_wrapper *iovw_new(void);
87struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
88struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
89void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
90int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
ae41fdb6
FB
91int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
92int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
11e6d971
FB
93void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new);
94size_t iovw_size(struct iovec_wrapper *iovw);