| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
#define LONG_LINE_MAX (1U*1024U*1024U) |
| 7 |
|
| 8 |
typedef enum { |
| 9 |
WRITE_STRING_FILE_CREATE = 1 << 0, |
| 10 |
WRITE_STRING_FILE_TRUNCATE = 1 << 1, |
| 11 |
WRITE_STRING_FILE_ATOMIC = 1 << 2, |
| 12 |
WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 3, |
| 13 |
WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 4, |
| 14 |
WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE = 1 << 5, |
| 15 |
WRITE_STRING_FILE_SYNC = 1 << 6, |
| 16 |
WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 7, |
| 17 |
WRITE_STRING_FILE_NOFOLLOW = 1 << 8, |
| 18 |
WRITE_STRING_FILE_MKDIR_0755 = 1 << 9, |
| 19 |
WRITE_STRING_FILE_MODE_0600 = 1 << 10, |
| 20 |
WRITE_STRING_FILE_MODE_0444 = 1 << 11, |
| 21 |
WRITE_STRING_FILE_SUPPRESS_REDUNDANT_VIRTUAL = 1 << 12, |
| 22 |
WRITE_STRING_FILE_LABEL = 1 << 13, |
| 23 |
WRITE_STRING_FILE_OPEN_NONBLOCKING = 1 << 14, |
| 24 |
} WriteStringFileFlags; |
| 25 |
|
| 26 |
typedef enum { |
| 27 |
READ_FULL_FILE_SECURE = 1 << 0, /* erase any buffers we employ internally, after use */ |
| 28 |
READ_FULL_FILE_UNBASE64 = 1 << 1, /* base64 decode what we read */ |
| 29 |
READ_FULL_FILE_UNHEX = 1 << 2, /* hex decode what we read */ |
| 30 |
READ_FULL_FILE_WARN_WORLD_READABLE = 1 << 3, /* if regular file, log at LOG_WARNING level if access mode above 0700 */ |
| 31 |
READ_FULL_FILE_CONNECT_SOCKET = 1 << 4, /* if socket inode, connect to it and read off it */ |
| 32 |
READ_FULL_FILE_FAIL_WHEN_LARGER = 1 << 5, /* fail loading if file is larger than specified size */ |
| 33 |
READ_FULL_FILE_VERIFY_REGULAR = 1 << 6, /* before reading, verify this is a regular file */ |
| 34 |
} ReadFullFileFlags; |
| 35 |
|
| 36 |
int fdopen_unlocked(int fd, const char *options, FILE **ret); |
| 37 |
int take_fdopen_unlocked(int *fd, const char *options, FILE **ret); |
| 38 |
FILE* take_fdopen(int *fd, const char *options); |
| 39 |
DIR* take_fdopendir(int *dfd); |
| 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_full(FILE *f, const char *line, WriteStringFileFlags flags, const struct timespec *ts); |
| 44 |
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { |
| 45 |
return write_string_stream_full(f, line, flags, NULL); |
| 46 |
} |
| 47 |
|
| 48 |
int write_string_file_full(int dir_fd, const char *fn, const char *line, WriteStringFileFlags flags, const struct timespec *ts, const char *label_fn); |
| 49 |
static inline int write_string_file_at(int dir_fd, const char *fn, const char *line, WriteStringFileFlags flags) { |
| 50 |
return write_string_file_full(dir_fd, fn, line, flags, NULL, NULL); |
| 51 |
} |
| 52 |
static inline int write_string_file_fd(int dir_fd, const char *line, WriteStringFileFlags flags) { |
| 53 |
return write_string_file_at(dir_fd, NULL, line, flags); |
| 54 |
} |
| 55 |
static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) { |
| 56 |
return write_string_file_at(AT_FDCWD, fn, line, flags); |
| 57 |
} |
| 58 |
int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4); |
| 59 |
|
| 60 |
int write_base64_file_at(int dir_fd, const char *fn, const struct iovec *data, WriteStringFileFlags flags); |
| 61 |
|
| 62 |
int read_one_line_file_at(int dir_fd, const char *filename, char **ret); |
| 63 |
static inline int read_one_line_file(const char *filename, char **ret) { |
| 64 |
return read_one_line_file_at(AT_FDCWD, filename, ret); |
| 65 |
} |
| 66 |
int read_boolean_file_at(int dir_fd, const char *filename); |
| 67 |
static inline int read_boolean_file(const char *filename) { |
| 68 |
return read_boolean_file_at(AT_FDCWD, filename); |
| 69 |
} |
| 70 |
int read_full_file_full(int dir_fd, const char *filename, uint64_t offset, size_t size, ReadFullFileFlags flags, const char *bind_name, char **ret_contents, size_t *ret_size); |
| 71 |
static inline int read_full_file_at(int dir_fd, const char *filename, char **ret_contents, size_t *ret_size) { |
| 72 |
return read_full_file_full(dir_fd, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size); |
| 73 |
} |
| 74 |
static inline int read_full_file(const char *filename, char **ret_contents, size_t *ret_size) { |
| 75 |
return read_full_file_full(AT_FDCWD, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size); |
| 76 |
} |
| 77 |
|
| 78 |
int read_virtual_file_at(int dir_fd, const char *filename, size_t max_size, char **ret_contents, size_t *ret_size); |
| 79 |
static inline int read_virtual_file_fd(int fd, size_t max_size, char **ret_contents, size_t *ret_size) { |
| 80 |
return read_virtual_file_at(fd, NULL, max_size, ret_contents, ret_size); |
| 81 |
} |
| 82 |
static inline int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size) { |
| 83 |
return read_virtual_file_at(AT_FDCWD, filename, max_size, ret_contents, ret_size); |
| 84 |
} |
| 85 |
static inline int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) { |
| 86 |
return read_virtual_file(filename, SIZE_MAX, ret_contents, ret_size); |
| 87 |
} |
| 88 |
|
| 89 |
int read_full_stream_full(FILE *f, const char *filename, uint64_t offset, size_t size, ReadFullFileFlags flags, char **ret_contents, size_t *ret_size); |
| 90 |
static inline int read_full_stream(FILE *f, char **ret_contents, size_t *ret_size) { |
| 91 |
return read_full_stream_full(f, NULL, UINT64_MAX, SIZE_MAX, 0, ret_contents, ret_size); |
| 92 |
} |
| 93 |
|
| 94 |
int verify_file_at(int dir_fd, const char *fn, const char *blob, bool accept_extra_nl); |
| 95 |
|
| 96 |
int script_get_shebang_interpreter(const char *path, char **ret); |
| 97 |
|
| 98 |
int get_proc_field(const char *path, const char *key, char **ret); |
| 99 |
|
| 100 |
DIR* xopendirat(int dir_fd, const char *path, int flags); |
| 101 |
|
| 102 |
typedef enum XfopenFlags { |
| 103 |
XFOPEN_UNLOCKED = 1 << 0, /* call __fsetlocking(FSETLOCKING_BYCALLER) after opened */ |
| 104 |
XFOPEN_SOCKET = 1 << 1, /* also try to open unix socket */ |
| 105 |
} XfopenFlags; |
| 106 |
|
| 107 |
int xfopenat_full( |
| 108 |
int dir_fd, |
| 109 |
const char *path, |
| 110 |
const char *mode, |
| 111 |
int open_flags, |
| 112 |
XfopenFlags flags, |
| 113 |
const char *bind_name, |
| 114 |
FILE **ret); |
| 115 |
static inline int xfopenat(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) { |
| 116 |
return xfopenat_full(dir_fd, path, mode, open_flags, 0, NULL, ret); |
| 117 |
} |
| 118 |
static inline int fopen_unlocked_at(int dir_fd, const char *path, const char *mode, int open_flags, FILE **ret) { |
| 119 |
return xfopenat_full(dir_fd, path, mode, open_flags, XFOPEN_UNLOCKED, NULL, ret); |
| 120 |
} |
| 121 |
static inline int fopen_unlocked(const char *path, const char *mode, FILE **ret) { |
| 122 |
return fopen_unlocked_at(AT_FDCWD, path, mode, 0, ret); |
| 123 |
} |
| 124 |
|
| 125 |
int fdopen_independent(int fd, const char *mode, FILE **ret); |
| 126 |
|
| 127 |
int search_and_open(const char *path, int mode, const char *root, char **search, int *ret_fd, char **ret_path); |
| 128 |
static inline int search_and_access(const char *path, int mode, const char *root, char**search, char **ret_path) { |
| 129 |
return search_and_open(path, mode, root, search, NULL, ret_path); |
| 130 |
} |
| 131 |
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret_file, char **ret_path); |
| 132 |
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret_file, char **ret_path); |
| 133 |
|
| 134 |
int fflush_and_check(FILE *f); |
| 135 |
int fflush_sync_and_check(FILE *f); |
| 136 |
|
| 137 |
int write_timestamp_file_atomic(const char *fn, usec_t n); |
| 138 |
int read_timestamp_file(const char *fn, usec_t *ret); |
| 139 |
|
| 140 |
int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space); |
| 141 |
int fputs_with_newline(FILE *f, const char *s); |
| 142 |
|
| 143 |
typedef enum ReadLineFlags { |
| 144 |
READ_LINE_ONLY_NUL = 1 << 0, |
| 145 |
READ_LINE_IS_A_TTY = 1 << 1, |
| 146 |
READ_LINE_NOT_A_TTY = 1 << 2, |
| 147 |
} ReadLineFlags; |
| 148 |
|
| 149 |
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); |
| 150 |
static inline int read_line(FILE *f, size_t limit, char **ret) { |
| 151 |
return read_line_full(f, limit, 0, ret); |
| 152 |
} |
| 153 |
static inline int read_nul_string(FILE *f, size_t limit, char **ret) { |
| 154 |
return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); |
| 155 |
} |
| 156 |
|
| 157 |
int read_stripped_line(FILE *f, size_t limit, char **ret); |
| 158 |
|
| 159 |
static inline bool file_offset_beyond_memory_size(off_t x) { |
| 160 |
if (x < 0) /* off_t is signed, filter that out */ |
| 161 |
return false; |
| 162 |
return (uint64_t) x > (uint64_t) SIZE_MAX; |
| 163 |
} |
| 164 |
|
| 165 |
int safe_fgetc(FILE *f, char *ret); |
| 166 |
|
| 167 |
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line); |
| 168 |
|
| 169 |
int fopen_mode_to_flags(const char *mode); |
| 170 |
|
| 171 |
typedef enum WriteDataFileFlags { |
| 172 |
WRITE_DATA_FILE_MKDIR_0755 = 1 << 0, |
| 173 |
WRITE_DATA_FILE_MODE_0400 = 1 << 1, |
| 174 |
} WriteDataFileFlags; |
| 175 |
|
| 176 |
int write_data_file_atomic_at(int dir_fd, const char *path, const struct iovec *iovec, WriteDataFileFlags flags); |