]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a5c32cff HH |
2 | #pragma once |
3 | ||
0d39fa9c LP |
4 | #include <dirent.h> |
5 | #include <stdbool.h> | |
a5c32cff | 6 | #include <stddef.h> |
b4bc041b | 7 | #include <stdio.h> |
0d39fa9c | 8 | #include <sys/types.h> |
b4bc041b | 9 | |
a5c32cff | 10 | #include "macro.h" |
33d52ab9 | 11 | #include "time-util.h" |
a5c32cff | 12 | |
fde32028 ZJS |
13 | #define LONG_LINE_MAX (1U*1024U*1024U) |
14 | ||
4c1fc3e4 | 15 | typedef enum { |
ef31828d LP |
16 | WRITE_STRING_FILE_CREATE = 1 << 0, |
17 | WRITE_STRING_FILE_ATOMIC = 1 << 1, | |
18 | WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 2, | |
19 | WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 3, | |
20 | WRITE_STRING_FILE_SYNC = 1 << 4, | |
21 | WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 5, | |
835d18ba | 22 | WRITE_STRING_FILE_NOFOLLOW = 1 << 6, |
7d7a99ac LP |
23 | |
24 | /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one | |
25 | more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file() | |
26 | and friends. */ | |
27 | ||
4c1fc3e4 DM |
28 | } WriteStringFileFlags; |
29 | ||
15f8f026 YW |
30 | typedef enum { |
31 | READ_FULL_FILE_SECURE = 1 << 0, | |
32 | } ReadFullFileFlags; | |
33 | ||
b1837133 LP |
34 | int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts); |
35 | static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { | |
36 | return write_string_stream_ts(f, line, flags, NULL); | |
39c38d77 ZJS |
37 | } |
38 | int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts); | |
39 | static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { | |
40 | return write_string_file_ts(fn, line, flags, NULL); | |
41 | } | |
b4bc041b | 42 | |
3130fca5 LP |
43 | int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); |
44 | ||
15f8f026 YW |
45 | int read_one_line_file(const char *filename, char **line); |
46 | int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size); | |
47 | static inline int read_full_file(const char *filename, char **contents, size_t *size) { | |
48 | return read_full_file_full(filename, 0, contents, size); | |
49 | } | |
50 | int read_full_stream_full(FILE *f, ReadFullFileFlags flags, char **contents, size_t *size); | |
51 | static inline int read_full_stream(FILE *f, char **contents, size_t *size) { | |
52 | return read_full_stream_full(f, 0, contents, size); | |
53 | } | |
a5c32cff | 54 | |
eb3da901 | 55 | int verify_file(const char *fn, const char *blob, bool accept_extra_nl); |
15dee3f0 | 56 | |
68fee104 | 57 | int executable_is_script(const char *path, char **interpreter); |
69ab8088 | 58 | |
c4cd1d4d | 59 | int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field); |
0d39fa9c LP |
60 | |
61 | DIR *xopendirat(int dirfd, const char *name, int flags); | |
62 | ||
63 | int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f); | |
64 | int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f); | |
65 | ||
0d39fa9c | 66 | int fflush_and_check(FILE *f); |
0675e94a | 67 | int fflush_sync_and_check(FILE *f); |
0d39fa9c | 68 | |
33d52ab9 LP |
69 | int write_timestamp_file_atomic(const char *fn, usec_t n); |
70 | int read_timestamp_file(const char *fn, usec_t *ret); | |
d390f8ef LP |
71 | |
72 | int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space); | |
03532f0a | 73 | |
41f11239 LP |
74 | typedef enum ReadLineFlags { |
75 | READ_LINE_ONLY_NUL = 1 << 0, | |
76 | } ReadLineFlags; | |
676bcb0f | 77 | |
41f11239 LP |
78 | int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); |
79 | ||
80 | static inline int read_line(FILE *f, size_t limit, char **ret) { | |
81 | return read_line_full(f, limit, 0, ret); | |
82 | } | |
83 | ||
84 | static inline int read_nul_string(FILE *f, size_t limit, char **ret) { | |
85 | return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); | |
86 | } | |
285a9b27 LP |
87 | |
88 | int safe_fgetc(FILE *f, char *ret); |