]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fileio.h
fileio: add READ_FULL_FILE_UNBASE64 flag for read_full_file_full()
[thirdparty/systemd.git] / src / basic / fileio.h
CommitLineData
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 16typedef 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,
7d7a99ac
LP
24
25 /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
26 more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
27 and friends. */
28
4c1fc3e4
DM
29} WriteStringFileFlags;
30
15f8f026 31typedef enum {
07d8c0eb
YW
32 READ_FULL_FILE_SECURE = 1 << 0,
33 READ_FULL_FILE_UNBASE64 = 1 << 1,
15f8f026
YW
34} ReadFullFileFlags;
35
b1837133
LP
36int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
37static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
38 return write_string_stream_ts(f, line, flags, NULL);
39c38d77
ZJS
39}
40int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
41static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
42 return write_string_file_ts(fn, line, flags, NULL);
43}
b4bc041b 44
3130fca5
LP
45int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
46
15f8f026
YW
47int read_one_line_file(const char *filename, char **line);
48int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size);
49static inline int read_full_file(const char *filename, char **contents, size_t *size) {
50 return read_full_file_full(filename, 0, contents, size);
51}
50caae7b 52int read_full_stream_full(FILE *f, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size);
15f8f026 53static inline int read_full_stream(FILE *f, char **contents, size_t *size) {
50caae7b 54 return read_full_stream_full(f, NULL, 0, contents, size);
15f8f026 55}
a5c32cff 56
eb3da901 57int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
15dee3f0 58
68fee104 59int executable_is_script(const char *path, char **interpreter);
69ab8088 60
c4cd1d4d 61int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
0d39fa9c
LP
62
63DIR *xopendirat(int dirfd, const char *name, int flags);
64
65int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
66int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
67
0d39fa9c 68int fflush_and_check(FILE *f);
0675e94a 69int fflush_sync_and_check(FILE *f);
0d39fa9c 70
33d52ab9
LP
71int write_timestamp_file_atomic(const char *fn, usec_t n);
72int read_timestamp_file(const char *fn, usec_t *ret);
d390f8ef
LP
73
74int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
03532f0a 75
41f11239
LP
76typedef enum ReadLineFlags {
77 READ_LINE_ONLY_NUL = 1 << 0,
78} ReadLineFlags;
676bcb0f 79
41f11239
LP
80int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);
81
82static inline int read_line(FILE *f, size_t limit, char **ret) {
83 return read_line_full(f, limit, 0, ret);
84}
85
86static inline int read_nul_string(FILE *f, size_t limit, char **ret) {
87 return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret);
88}
285a9b27
LP
89
90int safe_fgetc(FILE *f, char *ret);
7a309a8c
YW
91
92int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);