]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a5c32cff HH |
2 | #pragma once |
3 | ||
0c15577a | 4 | #include "forward.h" |
a5c32cff | 5 | |
fde32028 ZJS |
6 | #define LONG_LINE_MAX (1U*1024U*1024U) |
7 | ||
4c1fc3e4 | 8 | typedef enum { |
e565cfd2 ADT |
9 | WRITE_STRING_FILE_CREATE = 1 << 0, |
10 | WRITE_STRING_FILE_TRUNCATE = 1 << 1, | |
11 | WRITE_STRING_FILE_ATOMIC = 1 << 2, | |
12 | WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 3, | |
13 | WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 4, | |
14 | WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE = 1 << 5, | |
15 | WRITE_STRING_FILE_SYNC = 1 << 6, | |
16 | WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 7, | |
17 | WRITE_STRING_FILE_NOFOLLOW = 1 << 8, | |
18 | WRITE_STRING_FILE_MKDIR_0755 = 1 << 9, | |
19 | WRITE_STRING_FILE_MODE_0600 = 1 << 10, | |
2148c669 LP |
20 | WRITE_STRING_FILE_MODE_0444 = 1 << 11, |
21 | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL = 1 << 12, | |
8eeb8709 | 22 | WRITE_STRING_FILE_LABEL = 1 << 13, |
4fe348cf | 23 | WRITE_STRING_FILE_OPEN_NONBLOCKING = 1 << 14, |
4c1fc3e4 DM |
24 | } WriteStringFileFlags; |
25 | ||
15f8f026 | 26 | typedef enum { |
3177b927 LP |
27 | READ_FULL_FILE_SECURE = 1 << 0, /* erase any buffers we employ internally, after use */ |
28 | READ_FULL_FILE_UNBASE64 = 1 << 1, /* base64 decode what we read */ | |
29 | READ_FULL_FILE_UNHEX = 1 << 2, /* hex decode what we read */ | |
30 | READ_FULL_FILE_WARN_WORLD_READABLE = 1 << 3, /* if regular file, log at LOG_WARNING level if access mode above 0700 */ | |
31 | READ_FULL_FILE_CONNECT_SOCKET = 1 << 4, /* if socket inode, connect to it and read off it */ | |
7b0da71d | 32 | READ_FULL_FILE_FAIL_WHEN_LARGER = 1 << 5, /* fail loading if file is larger than specified size */ |
15f8f026 YW |
33 | } ReadFullFileFlags; |
34 | ||
02e23d1a | 35 | int fdopen_unlocked(int fd, const char *options, FILE **ret); |
3ebbb6cb VC |
36 | int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); |
37 | FILE* take_fdopen(int *fd, const char *options); | |
f61457b0 | 38 | DIR* take_fdopendir(int *dfd); |
2fe21124 | 39 | FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); |
673a1e6f | 40 | FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); |
fdeea3f4 | 41 | |
8a0adc97 | 42 | int write_string_stream_full(FILE *f, const char *line, WriteStringFileFlags flags, const struct timespec *ts); |
b1837133 | 43 | static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { |
7a719510 | 44 | return write_string_stream_full(f, line, flags, NULL); |
39c38d77 | 45 | } |
bbec1c87 | 46 | |
47 | int write_string_file_full(int dir_fd, const char *fn, const char *line, WriteStringFileFlags flags, const struct timespec *ts, const char *label_fn); | |
0ab5e2a4 | 48 | static inline int write_string_file_at(int dir_fd, const char *fn, const char *line, WriteStringFileFlags flags) { |
7a719510 MY |
49 | return write_string_file_full(dir_fd, fn, line, flags, NULL, NULL); |
50 | } | |
f3c5c2b0 YW |
51 | static inline int write_string_file_fd(int dir_fd, const char *line, WriteStringFileFlags flags) { |
52 | return write_string_file_at(dir_fd, NULL, line, flags); | |
53 | } | |
7a719510 MY |
54 | static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { |
55 | return write_string_file_at(AT_FDCWD, fn, line, flags); | |
39c38d77 | 56 | } |
8a0adc97 | 57 | int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); |
b4bc041b | 58 | |
dc163cd4 LP |
59 | int write_base64_file_at(int dir_fd, const char *fn, const struct iovec *data, WriteStringFileFlags flags); |
60 | ||
46b457e5 YW |
61 | int read_one_line_file_at(int dir_fd, const char *filename, char **ret); |
62 | static inline int read_one_line_file(const char *filename, char **ret) { | |
63 | return read_one_line_file_at(AT_FDCWD, filename, ret); | |
64 | } | |
986311c2 | 65 | int read_full_file_full(int dir_fd, const char *filename, uint64_t offset, size_t size, ReadFullFileFlags flags, const char *bind_name, char **ret_contents, size_t *ret_size); |
0ab5e2a4 DDM |
66 | static inline int read_full_file_at(int dir_fd, const char *filename, char **ret_contents, size_t *ret_size) { |
67 | return read_full_file_full(dir_fd, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size); | |
68 | } | |
986311c2 LP |
69 | static inline int read_full_file(const char *filename, char **ret_contents, size_t *ret_size) { |
70 | return read_full_file_full(AT_FDCWD, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size); | |
15f8f026 | 71 | } |
ad0e687c | 72 | |
6bfd44ee | 73 | int read_virtual_file_at(int dir_fd, const char *filename, size_t max_size, char **ret_contents, size_t *ret_size); |
06503dd0 YW |
74 | static inline int read_virtual_file_fd(int fd, size_t max_size, char **ret_contents, size_t *ret_size) { |
75 | return read_virtual_file_at(fd, NULL, max_size, ret_contents, ret_size); | |
76 | } | |
6bfd44ee LP |
77 | static inline int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) { |
78 | return read_virtual_file_at(AT_FDCWD, filename, max_size, ret_contents, ret_size); | |
79 | } | |
ad0e687c ZJS |
80 | static inline int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) { |
81 | return read_virtual_file(filename, SIZE_MAX, ret_contents, ret_size); | |
82 | } | |
83 | ||
986311c2 LP |
84 | int read_full_stream_full(FILE *f, const char *filename, uint64_t offset, size_t size, ReadFullFileFlags flags, char **ret_contents, size_t *ret_size); |
85 | static inline int read_full_stream(FILE *f, char **ret_contents, size_t *ret_size) { | |
86 | return read_full_stream_full(f, NULL, UINT64_MAX, SIZE_MAX, 0, ret_contents, ret_size); | |
15f8f026 | 87 | } |
a5c32cff | 88 | |
0ab5e2a4 | 89 | int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl); |
15dee3f0 | 90 | |
ad9a66fe | 91 | int script_get_shebang_interpreter(const char *path, char **ret); |
69ab8088 | 92 | |
15036f85 | 93 | int get_proc_field(const char *path, const char *key, char **ret); |
0d39fa9c | 94 | |
c0cf1c58 | 95 | DIR* xopendirat(int dir_fd, const char *name, int flags); |
61d9982c YW |
96 | |
97 | typedef enum XfopenFlags { | |
98 | XFOPEN_UNLOCKED = 1 << 0, /* call __fsetlocking(FSETLOCKING_BYCALLER) after opened */ | |
99 | XFOPEN_SOCKET = 1 << 1, /* also try to open unix socket */ | |
100 | } XfopenFlags; | |
101 | ||
102 | int xfopenat_full( | |
103 | int dir_fd, | |
104 | const char *path, | |
105 | const char *mode, | |
106 | int open_flags, | |
107 | XfopenFlags flags, | |
108 | const char *bind_name, | |
109 | FILE **ret); | |
110 | static inline int xfopenat(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) { | |
111 | return xfopenat_full(dir_fd, path, mode, open_flags, 0, NULL, ret); | |
112 | } | |
113 | static inline int fopen_unlocked_at(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) { | |
114 | return xfopenat_full(dir_fd, path, mode, open_flags, XFOPEN_UNLOCKED, NULL, ret); | |
115 | } | |
116 | static inline int fopen_unlocked(const char *path, const char *mode, FILE **ret) { | |
117 | return fopen_unlocked_at(AT_FDCWD, path, mode, 0, ret); | |
118 | } | |
0d39fa9c | 119 | |
b839101a LP |
120 | int fdopen_independent(int fd, const char *mode, FILE **ret); |
121 | ||
2c07d314 LP |
122 | int search_and_open(const char *path, int mode, const char *root, char **search, int *ret_fd, char **ret_path); |
123 | static inline int search_and_access(const char *path, int mode, const char *root, char**search, char **ret_path) { | |
124 | return search_and_open(path, mode, root, search, NULL, ret_path); | |
125 | } | |
126 | int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret_file, char **ret_path); | |
127 | int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret_file, char **ret_path); | |
0d39fa9c | 128 | |
0d39fa9c | 129 | int fflush_and_check(FILE *f); |
0675e94a | 130 | int fflush_sync_and_check(FILE *f); |
0d39fa9c | 131 | |
33d52ab9 LP |
132 | int write_timestamp_file_atomic(const char *fn, usec_t n); |
133 | int read_timestamp_file(const char *fn, usec_t *ret); | |
d390f8ef | 134 | |
215286a4 | 135 | int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space); |
0160a1db | 136 | int fputs_with_newline(FILE *f, const char *s); |
03532f0a | 137 | |
41f11239 | 138 | typedef enum ReadLineFlags { |
609ae0f5 LP |
139 | READ_LINE_ONLY_NUL = 1 << 0, |
140 | READ_LINE_IS_A_TTY = 1 << 1, | |
141 | READ_LINE_NOT_A_TTY = 1 << 2, | |
41f11239 | 142 | } ReadLineFlags; |
676bcb0f | 143 | |
41f11239 | 144 | int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); |
41f11239 LP |
145 | static inline int read_line(FILE *f, size_t limit, char **ret) { |
146 | return read_line_full(f, limit, 0, ret); | |
147 | } | |
41f11239 LP |
148 | static inline int read_nul_string(FILE *f, size_t limit, char **ret) { |
149 | return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); | |
150 | } | |
285a9b27 | 151 | |
c56cb33f LP |
152 | int read_stripped_line(FILE *f, size_t limit, char **ret); |
153 | ||
c0cf1c58 MY |
154 | static inline bool file_offset_beyond_memory_size(off_t x) { |
155 | if (x < 0) /* off_t is signed, filter that out */ | |
156 | return false; | |
157 | return (uint64_t) x > (uint64_t) SIZE_MAX; | |
158 | } | |
159 | ||
285a9b27 | 160 | int safe_fgetc(FILE *f, char *ret); |
7a309a8c YW |
161 | |
162 | int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line); | |
01bebba3 LP |
163 | |
164 | int fopen_mode_to_flags(const char *mode); |