]>
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> |
7a309a8c | 8 | #include <sys/stat.h> |
0d39fa9c | 9 | #include <sys/types.h> |
b4bc041b | 10 | |
a5c32cff | 11 | #include "macro.h" |
33d52ab9 | 12 | #include "time-util.h" |
a5c32cff | 13 | |
fde32028 ZJS |
14 | #define LONG_LINE_MAX (1U*1024U*1024U) |
15 | ||
4c1fc3e4 | 16 | typedef enum { |
ef31828d LP |
17 | WRITE_STRING_FILE_CREATE = 1 << 0, |
18 | WRITE_STRING_FILE_ATOMIC = 1 << 1, | |
19 | WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 2, | |
20 | WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 3, | |
21 | WRITE_STRING_FILE_SYNC = 1 << 4, | |
22 | WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 5, | |
835d18ba | 23 | WRITE_STRING_FILE_NOFOLLOW = 1 << 6, |
50ccd864 | 24 | WRITE_STRING_FILE_MKDIR_0755 = 1 << 7, |
7d7a99ac LP |
25 | |
26 | /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one | |
27 | more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file() | |
28 | and friends. */ | |
29 | ||
4c1fc3e4 DM |
30 | } WriteStringFileFlags; |
31 | ||
15f8f026 | 32 | typedef enum { |
07d8c0eb YW |
33 | READ_FULL_FILE_SECURE = 1 << 0, |
34 | READ_FULL_FILE_UNBASE64 = 1 << 1, | |
89aaf655 | 35 | READ_FULL_FILE_UNHEX = 1 << 2, |
15f8f026 YW |
36 | } ReadFullFileFlags; |
37 | ||
fdeea3f4 | 38 | int fopen_unlocked(const char *path, const char *options, FILE **ret); |
02e23d1a | 39 | int fdopen_unlocked(int fd, const char *options, FILE **ret); |
2fe21124 | 40 | FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); |
673a1e6f | 41 | FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); |
fdeea3f4 | 42 | |
b1837133 LP |
43 | int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts); |
44 | static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { | |
45 | return write_string_stream_ts(f, line, flags, NULL); | |
39c38d77 ZJS |
46 | } |
47 | int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts); | |
48 | static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { | |
49 | return write_string_file_ts(fn, line, flags, NULL); | |
50 | } | |
b4bc041b | 51 | |
3130fca5 LP |
52 | int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); |
53 | ||
15f8f026 YW |
54 | int read_one_line_file(const char *filename, char **line); |
55 | int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size); | |
56 | static inline int read_full_file(const char *filename, char **contents, size_t *size) { | |
57 | return read_full_file_full(filename, 0, contents, size); | |
58 | } | |
21b40f16 | 59 | int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size); |
50caae7b | 60 | int read_full_stream_full(FILE *f, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size); |
15f8f026 | 61 | static inline int read_full_stream(FILE *f, char **contents, size_t *size) { |
50caae7b | 62 | return read_full_stream_full(f, NULL, 0, contents, size); |
15f8f026 | 63 | } |
a5c32cff | 64 | |
eb3da901 | 65 | int verify_file(const char *fn, const char *blob, bool accept_extra_nl); |
15dee3f0 | 66 | |
68fee104 | 67 | int executable_is_script(const char *path, char **interpreter); |
69ab8088 | 68 | |
c4cd1d4d | 69 | int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field); |
0d39fa9c LP |
70 | |
71 | DIR *xopendirat(int dirfd, const char *name, int flags); | |
0a38e6b9 | 72 | int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **ret); |
0d39fa9c LP |
73 | |
74 | int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f); | |
75 | int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f); | |
76 | ||
0d39fa9c | 77 | int fflush_and_check(FILE *f); |
0675e94a | 78 | int fflush_sync_and_check(FILE *f); |
0d39fa9c | 79 | |
33d52ab9 LP |
80 | int write_timestamp_file_atomic(const char *fn, usec_t n); |
81 | int read_timestamp_file(const char *fn, usec_t *ret); | |
d390f8ef LP |
82 | |
83 | int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space); | |
03532f0a | 84 | |
41f11239 LP |
85 | typedef enum ReadLineFlags { |
86 | READ_LINE_ONLY_NUL = 1 << 0, | |
87 | } ReadLineFlags; | |
676bcb0f | 88 | |
41f11239 LP |
89 | int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); |
90 | ||
91 | static inline int read_line(FILE *f, size_t limit, char **ret) { | |
92 | return read_line_full(f, limit, 0, ret); | |
93 | } | |
94 | ||
95 | static inline int read_nul_string(FILE *f, size_t limit, char **ret) { | |
96 | return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); | |
97 | } | |
285a9b27 LP |
98 | |
99 | int safe_fgetc(FILE *f, char *ret); | |
7a309a8c YW |
100 | |
101 | int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line); |