]>
Commit | Line | Data |
---|---|---|
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/fcntl.h> | |
10 | #include <sys/types.h> | |
11 | ||
12 | #include "macro.h" | |
13 | #include "time-util.h" | |
14 | ||
15 | #define LONG_LINE_MAX (1U*1024U*1024U) | |
16 | ||
17 | typedef enum { | |
18 | WRITE_STRING_FILE_CREATE = 1 << 0, | |
19 | WRITE_STRING_FILE_ATOMIC = 1 << 1, | |
20 | WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 2, | |
21 | WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 3, | |
22 | WRITE_STRING_FILE_SYNC = 1 << 4, | |
23 | WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 5, | |
24 | WRITE_STRING_FILE_NOFOLLOW = 1 << 6, | |
25 | WRITE_STRING_FILE_MKDIR_0755 = 1 << 7, | |
26 | WRITE_STRING_FILE_MODE_0600 = 1 << 8, | |
27 | ||
28 | /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one | |
29 | more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file() | |
30 | and friends. */ | |
31 | ||
32 | } WriteStringFileFlags; | |
33 | ||
34 | typedef enum { | |
35 | READ_FULL_FILE_SECURE = 1 << 0, /* erase any buffers we employ internally, after use */ | |
36 | READ_FULL_FILE_UNBASE64 = 1 << 1, /* base64 decode what we read */ | |
37 | READ_FULL_FILE_UNHEX = 1 << 2, /* hex decode what we read */ | |
38 | READ_FULL_FILE_WARN_WORLD_READABLE = 1 << 3, /* if regular file, log at LOG_WARNING level if access mode above 0700 */ | |
39 | READ_FULL_FILE_CONNECT_SOCKET = 1 << 4, /* if socket inode, connect to it and read off it */ | |
40 | } ReadFullFileFlags; | |
41 | ||
42 | int fopen_unlocked(const char *path, const char *options, FILE **ret); | |
43 | int fdopen_unlocked(int fd, const char *options, FILE **ret); | |
44 | int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); | |
45 | FILE* take_fdopen(int *fd, const char *options); | |
46 | DIR* take_fdopendir(int *dfd); | |
47 | FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc); | |
48 | FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode); | |
49 | ||
50 | int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts); | |
51 | static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { | |
52 | return write_string_stream_ts(f, line, flags, NULL); | |
53 | } | |
54 | int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts); | |
55 | static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { | |
56 | return write_string_file_ts(fn, line, flags, NULL); | |
57 | } | |
58 | ||
59 | int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); | |
60 | ||
61 | int read_one_line_file(const char *filename, char **line); | |
62 | int read_full_file_full(int dir_fd, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size); | |
63 | static inline int read_full_file(const char *filename, char **contents, size_t *size) { | |
64 | return read_full_file_full(AT_FDCWD, filename, 0, contents, size); | |
65 | } | |
66 | int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size); | |
67 | int read_full_stream_full(FILE *f, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size); | |
68 | static inline int read_full_stream(FILE *f, char **contents, size_t *size) { | |
69 | return read_full_stream_full(f, NULL, 0, contents, size); | |
70 | } | |
71 | ||
72 | int verify_file(const char *fn, const char *blob, bool accept_extra_nl); | |
73 | ||
74 | int executable_is_script(const char *path, char **interpreter); | |
75 | ||
76 | int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field); | |
77 | ||
78 | DIR *xopendirat(int dirfd, const char *name, int flags); | |
79 | int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **ret); | |
80 | ||
81 | int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f); | |
82 | int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f); | |
83 | ||
84 | int fflush_and_check(FILE *f); | |
85 | int fflush_sync_and_check(FILE *f); | |
86 | ||
87 | int write_timestamp_file_atomic(const char *fn, usec_t n); | |
88 | int read_timestamp_file(const char *fn, usec_t *ret); | |
89 | ||
90 | int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space); | |
91 | ||
92 | typedef enum ReadLineFlags { | |
93 | READ_LINE_ONLY_NUL = 1 << 0, | |
94 | READ_LINE_IS_A_TTY = 1 << 1, | |
95 | READ_LINE_NOT_A_TTY = 1 << 2, | |
96 | } ReadLineFlags; | |
97 | ||
98 | int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); | |
99 | ||
100 | static inline int read_line(FILE *f, size_t limit, char **ret) { | |
101 | return read_line_full(f, limit, 0, ret); | |
102 | } | |
103 | ||
104 | static inline int read_nul_string(FILE *f, size_t limit, char **ret) { | |
105 | return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); | |
106 | } | |
107 | ||
108 | int safe_fgetc(FILE *f, char *ret); | |
109 | ||
110 | int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line); | |
111 | ||
112 | int sync_rights(int from, int to); | |
113 | ||
114 | int rename_and_apply_smack_floor_label(const char *temp_path, const char *dest_path); |