]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | #pragma once | |
3 | ||
4 | #include "forward.h" | |
5 | ||
6 | #define LONG_LINE_MAX (1U*1024U*1024U) | |
7 | ||
8 | typedef enum { | |
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, | |
20 | WRITE_STRING_FILE_MODE_0444 = 1 << 11, | |
21 | WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL = 1 << 12, | |
22 | WRITE_STRING_FILE_LABEL = 1 << 13, | |
23 | WRITE_STRING_FILE_OPEN_NONBLOCKING = 1 << 14, | |
24 | } WriteStringFileFlags; | |
25 | ||
26 | typedef enum { | |
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 */ | |
32 | READ_FULL_FILE_FAIL_WHEN_LARGER = 1 << 5, /* fail loading if file is larger than specified size */ | |
33 | } ReadFullFileFlags; | |
34 | ||
35 | int fdopen_unlocked(int fd, const char *options, FILE **ret); | |
36 | int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); | |
37 | FILE* take_fdopen(int *fd, const char *options); | |
38 | DIR* take_fdopendir(int *dfd); | |
39 | FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); | |
40 | FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); | |
41 | ||
42 | int write_string_stream_full(FILE *f, const char *line, WriteStringFileFlags flags, const struct timespec *ts); | |
43 | static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { | |
44 | return write_string_stream_full(f, line, flags, NULL); | |
45 | } | |
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); | |
48 | static inline int write_string_file_at(int dir_fd, const char *fn, const char *line, WriteStringFileFlags flags) { | |
49 | return write_string_file_full(dir_fd, fn, line, flags, NULL, NULL); | |
50 | } | |
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 | } | |
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); | |
56 | } | |
57 | int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); | |
58 | ||
59 | int write_base64_file_at(int dir_fd, const char *fn, const struct iovec *data, WriteStringFileFlags flags); | |
60 | ||
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 | } | |
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); | |
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 | } | |
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); | |
71 | } | |
72 | ||
73 | int read_virtual_file_at(int dir_fd, const char *filename, size_t max_size, char **ret_contents, size_t *ret_size); | |
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 | } | |
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 | } | |
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 | ||
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); | |
87 | } | |
88 | ||
89 | int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl); | |
90 | ||
91 | int script_get_shebang_interpreter(const char *path, char **ret); | |
92 | ||
93 | int get_proc_field(const char *path, const char *key, char **ret); | |
94 | ||
95 | DIR* xopendirat(int dir_fd, const char *name, int flags); | |
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 | } | |
119 | ||
120 | int fdopen_independent(int fd, const char *mode, FILE **ret); | |
121 | ||
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); | |
128 | ||
129 | int fflush_and_check(FILE *f); | |
130 | int fflush_sync_and_check(FILE *f); | |
131 | ||
132 | int write_timestamp_file_atomic(const char *fn, usec_t n); | |
133 | int read_timestamp_file(const char *fn, usec_t *ret); | |
134 | ||
135 | int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space); | |
136 | int fputs_with_newline(FILE *f, const char *s); | |
137 | ||
138 | typedef enum ReadLineFlags { | |
139 | READ_LINE_ONLY_NUL = 1 << 0, | |
140 | READ_LINE_IS_A_TTY = 1 << 1, | |
141 | READ_LINE_NOT_A_TTY = 1 << 2, | |
142 | } ReadLineFlags; | |
143 | ||
144 | int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); | |
145 | static inline int read_line(FILE *f, size_t limit, char **ret) { | |
146 | return read_line_full(f, limit, 0, ret); | |
147 | } | |
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 | } | |
151 | ||
152 | int read_stripped_line(FILE *f, size_t limit, char **ret); | |
153 | ||
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 | ||
160 | int safe_fgetc(FILE *f, char *ret); | |
161 | ||
162 | int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line); | |
163 | ||
164 | int fopen_mode_to_flags(const char *mode); |