]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/io-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "time-util.h"
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
);
21 int fd_wait_for_event(int fd
, int event
, usec_t timeout
);
23 ssize_t
sparse_write(int fd
, const void *p
, size_t sz
, size_t run_length
);
25 static inline size_t IOVEC_TOTAL_SIZE(const struct iovec
*i
, size_t n
) {
28 for (j
= 0; j
< n
; j
++)
34 static inline size_t IOVEC_INCREMENT(struct iovec
*i
, size_t n
, size_t k
) {
37 for (j
= 0; j
< n
; j
++) {
40 if (_unlikely_(k
<= 0))
43 sub
= MIN(i
[j
].iov_len
, k
);
45 i
[j
].iov_base
= (uint8_t*) i
[j
].iov_base
+ sub
;
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. */
56 return (l
>> 63) == 0;
59 static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l
) {
61 /* Same as above, but allows one extra value: -1 as indication for infinity. */
63 if (l
== (uint64_t) -1)
66 return FILE_SIZE_VALID(l
);
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)
75 char* set_iovec_string_field(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, const char *value
);
76 char* set_iovec_string_field_free(struct iovec
*iovec
, size_t *n_iovec
, const char *field
, char *value
);
78 struct iovec_wrapper
{
84 struct iovec_wrapper
*iovw_new(void);
85 struct iovec_wrapper
*iovw_free(struct iovec_wrapper
*iovw
);
86 struct iovec_wrapper
*iovw_free_free(struct iovec_wrapper
*iovw
);
87 void iovw_free_contents(struct iovec_wrapper
*iovw
, bool free_vectors
);
88 int iovw_put(struct iovec_wrapper
*iovw
, void *data
, size_t len
);
89 int iovw_put_string_field(struct iovec_wrapper
*iovw
, const char *field
, const char *value
);
90 int iovw_put_string_field_free(struct iovec_wrapper
*iovw
, const char *field
, char *value
);
91 void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new);
92 size_t iovw_size(struct iovec_wrapper
*iovw
);