Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #pragma once
3 :
4 : #include <dirent.h>
5 : #include <stdbool.h>
6 : #include <stddef.h>
7 : #include <stdio.h>
8 : #include <sys/stat.h>
9 : #include <sys/types.h>
10 :
11 : #include "macro.h"
12 : #include "time-util.h"
13 :
14 : #define LONG_LINE_MAX (1U*1024U*1024U)
15 :
16 : typedef enum {
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,
23 : WRITE_STRING_FILE_NOFOLLOW = 1 << 6,
24 : WRITE_STRING_FILE_MKDIR_0755 = 1 << 7,
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 :
30 : } WriteStringFileFlags;
31 :
32 : typedef enum {
33 : READ_FULL_FILE_SECURE = 1 << 0,
34 : READ_FULL_FILE_UNBASE64 = 1 << 1,
35 : READ_FULL_FILE_UNHEX = 1 << 2,
36 : } ReadFullFileFlags;
37 :
38 : int fopen_unlocked(const char *path, const char *options, FILE **ret);
39 : int fdopen_unlocked(int fd, const char *options, FILE **ret);
40 : FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
41 : FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);
42 :
43 : int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
44 28 : static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
45 28 : return write_string_stream_ts(f, line, flags, NULL);
46 : }
47 : int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
48 106 : static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
49 106 : return write_string_file_ts(fn, line, flags, NULL);
50 : }
51 :
52 : int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
53 :
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 14987 : static inline int read_full_file(const char *filename, char **contents, size_t *size) {
57 14987 : return read_full_file_full(filename, 0, contents, size);
58 : }
59 : int read_full_stream_full(FILE *f, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size);
60 6 : static inline int read_full_stream(FILE *f, char **contents, size_t *size) {
61 6 : return read_full_stream_full(f, NULL, 0, contents, size);
62 : }
63 :
64 : int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
65 :
66 : int executable_is_script(const char *path, char **interpreter);
67 :
68 : int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
69 :
70 : DIR *xopendirat(int dirfd, const char *name, int flags);
71 :
72 : int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
73 : int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
74 :
75 : int fflush_and_check(FILE *f);
76 : int fflush_sync_and_check(FILE *f);
77 :
78 : int write_timestamp_file_atomic(const char *fn, usec_t n);
79 : int read_timestamp_file(const char *fn, usec_t *ret);
80 :
81 : int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
82 :
83 : typedef enum ReadLineFlags {
84 : READ_LINE_ONLY_NUL = 1 << 0,
85 : } ReadLineFlags;
86 :
87 : int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);
88 :
89 320707 : static inline int read_line(FILE *f, size_t limit, char **ret) {
90 320707 : return read_line_full(f, limit, 0, ret);
91 : }
92 :
93 7 : static inline int read_nul_string(FILE *f, size_t limit, char **ret) {
94 7 : return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret);
95 : }
96 :
97 : int safe_fgetc(FILE *f, char *ret);
98 :
99 : int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);
|