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