]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/iovec-util.c
man/systemd.mount: tmpfs automatically gains After=swap.target dep
[thirdparty/systemd.git] / src / basic / iovec-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "iovec-util.h"
4 #include "string-util.h"
5
6 size_t iovec_total_size(const struct iovec *iovec, size_t n) {
7 size_t sum = 0;
8
9 assert(iovec || n == 0);
10
11 FOREACH_ARRAY(j, iovec, n)
12 sum += j->iov_len;
13
14 return sum;
15 }
16
17 bool iovec_increment(struct iovec *iovec, size_t n, size_t k) {
18 assert(iovec || n == 0);
19
20 /* Returns true if there is nothing else to send (bytes written cover all of the iovec),
21 * false if there's still work to do. */
22
23 FOREACH_ARRAY(j, iovec, n) {
24 size_t sub;
25
26 if (j->iov_len == 0)
27 continue;
28 if (k == 0)
29 return false;
30
31 sub = MIN(j->iov_len, k);
32 j->iov_len -= sub;
33 j->iov_base = (uint8_t*) j->iov_base + sub;
34 k -= sub;
35 }
36
37 assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
38 * or the kernel reported writing more bytes than sent. */
39 return true;
40 }
41
42 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
43 char *x;
44
45 assert(iovec);
46 assert(n_iovec);
47
48 x = strjoin(field, value);
49 if (x)
50 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
51 return x;
52 }
53
54 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
55 char *x;
56
57 assert(iovec);
58 assert(n_iovec);
59
60 x = set_iovec_string_field(iovec, n_iovec, field, value);
61 free(value);
62 return x;
63 }
64
65 void iovec_array_free(struct iovec *iovec, size_t n) {
66 FOREACH_ARRAY(i, iovec, n)
67 free(i->iov_base);
68
69 free(iovec);
70 }