chiark / gitweb /
core: simplify cg_[all_]unified()
[elogind.git] / src / basic / io-util.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <sys/types.h>
26 #include <sys/uio.h>
27
28 #include "macro.h"
29 #include "time-util.h"
30
31 int flush_fd(int fd);
32
33 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
34 int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
35 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
36
37 int pipe_eof(int fd);
38
39 int fd_wait_for_event(int fd, int event, usec_t timeout);
40
41 ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length);
42
43 #define IOVEC_SET_STRING(i, s)                  \
44         do {                                    \
45                 struct iovec *_i = &(i);        \
46                 char *_s = (char *)(s);         \
47                 _i->iov_base = _s;              \
48                 _i->iov_len = strlen(_s);       \
49         } while (false)
50
51 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
52         unsigned j;
53         size_t r = 0;
54
55         for (j = 0; j < n; j++)
56                 r += i[j].iov_len;
57
58         return r;
59 }
60
61 static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
62         unsigned j;
63
64         for (j = 0; j < n; j++) {
65                 size_t sub;
66
67                 if (_unlikely_(k <= 0))
68                         break;
69
70                 sub = MIN(i[j].iov_len, k);
71                 i[j].iov_len -= sub;
72                 i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
73                 k -= sub;
74         }
75
76         return k;
77 }
78
79 static inline bool FILE_SIZE_VALID(uint64_t l) {
80         /* ftruncate() and friends take an unsigned file size, but actually cannot deal with file sizes larger than
81          * 2^63 since the kernel internally handles it as signed value. This call allows checking for this early. */
82
83         return (l >> 63) == 0;
84 }
85
86 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
87
88         /* Same as above, but allows one extra value: -1 as indication for infinity. */
89
90         if (l == (uint64_t) -1)
91                 return true;
92
93         return FILE_SIZE_VALID(l);
94
95 }