]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fileio.h
chattr-util: generalize chattr manipulation for files with secrets from journalctl
[thirdparty/systemd.git] / src / basic / fileio.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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>
8241f785 9#include <sys/fcntl.h>
0d39fa9c 10#include <sys/types.h>
b4bc041b 11
a5c32cff 12#include "macro.h"
33d52ab9 13#include "time-util.h"
a5c32cff 14
fde32028
ZJS
15#define LONG_LINE_MAX (1U*1024U*1024U)
16
4c1fc3e4 17typedef enum {
6873d203
YW
18 WRITE_STRING_FILE_CREATE = 1 << 0,
19 WRITE_STRING_FILE_TRUNCATE = 1 << 1,
20 WRITE_STRING_FILE_ATOMIC = 1 << 2,
21 WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 3,
22 WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 4,
23 WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE = 1 << 5,
24 WRITE_STRING_FILE_SYNC = 1 << 6,
25 WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 7,
26 WRITE_STRING_FILE_NOFOLLOW = 1 << 8,
27 WRITE_STRING_FILE_MKDIR_0755 = 1 << 9,
28 WRITE_STRING_FILE_MODE_0600 = 1 << 10,
7d7a99ac
LP
29
30 /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
31 more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
32 and friends. */
33
4c1fc3e4
DM
34} WriteStringFileFlags;
35
15f8f026 36typedef enum {
3177b927
LP
37 READ_FULL_FILE_SECURE = 1 << 0, /* erase any buffers we employ internally, after use */
38 READ_FULL_FILE_UNBASE64 = 1 << 1, /* base64 decode what we read */
39 READ_FULL_FILE_UNHEX = 1 << 2, /* hex decode what we read */
40 READ_FULL_FILE_WARN_WORLD_READABLE = 1 << 3, /* if regular file, log at LOG_WARNING level if access mode above 0700 */
41 READ_FULL_FILE_CONNECT_SOCKET = 1 << 4, /* if socket inode, connect to it and read off it */
15f8f026
YW
42} ReadFullFileFlags;
43
fdeea3f4 44int fopen_unlocked(const char *path, const char *options, FILE **ret);
02e23d1a 45int fdopen_unlocked(int fd, const char *options, FILE **ret);
3ebbb6cb
VC
46int take_fdopen_unlocked(int *fd, const char *options, FILE **ret);
47FILE* take_fdopen(int *fd, const char *options);
f61457b0 48DIR* take_fdopendir(int *dfd);
2fe21124 49FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
673a1e6f 50FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);
fdeea3f4 51
4d5f52e7 52int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, const struct timespec *ts);
b1837133
LP
53static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
54 return write_string_stream_ts(f, line, flags, NULL);
39c38d77 55}
4d5f52e7 56int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, const struct timespec *ts);
39c38d77
ZJS
57static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
58 return write_string_file_ts(fn, line, flags, NULL);
59}
b4bc041b 60
3130fca5
LP
61int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
62
15f8f026 63int read_one_line_file(const char *filename, char **line);
986311c2
LP
64int 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);
65static inline int read_full_file(const char *filename, char **ret_contents, size_t *ret_size) {
66 return read_full_file_full(AT_FDCWD, filename, UINT64_MAX, SIZE_MAX, 0, NULL, ret_contents, ret_size);
15f8f026 67}
ad0e687c
ZJS
68
69int read_virtual_file(const char *filename, size_t max_size, char **ret_contents, size_t *ret_size);
70static inline int read_full_virtual_file(const char *filename, char **ret_contents, size_t *ret_size) {
71 return read_virtual_file(filename, SIZE_MAX, ret_contents, ret_size);
72}
73
986311c2
LP
74int read_full_stream_full(FILE *f, const char *filename, uint64_t offset, size_t size, ReadFullFileFlags flags, char **ret_contents, size_t *ret_size);
75static inline int read_full_stream(FILE *f, char **ret_contents, size_t *ret_size) {
76 return read_full_stream_full(f, NULL, UINT64_MAX, SIZE_MAX, 0, ret_contents, ret_size);
15f8f026 77}
a5c32cff 78
eb3da901 79int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
15dee3f0 80
68fee104 81int executable_is_script(const char *path, char **interpreter);
69ab8088 82
c4cd1d4d 83int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
0d39fa9c
LP
84
85DIR *xopendirat(int dirfd, const char *name, int flags);
0a38e6b9 86int xfopenat(int dir_fd, const char *path, const char *mode, int flags, FILE **ret);
0d39fa9c 87
2708160c
LP
88int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **ret, char **ret_path);
89int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **ret, char **ret_path);
0d39fa9c 90
d338bfff
ZJS
91int chase_symlinks_and_fopen_unlocked(
92 const char *path,
93 const char *root,
94 unsigned chase_flags,
95 const char *open_flags,
96 FILE **ret_file,
97 char **ret_path);
98
0d39fa9c 99int fflush_and_check(FILE *f);
0675e94a 100int fflush_sync_and_check(FILE *f);
0d39fa9c 101
33d52ab9
LP
102int write_timestamp_file_atomic(const char *fn, usec_t n);
103int read_timestamp_file(const char *fn, usec_t *ret);
d390f8ef
LP
104
105int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
03532f0a 106
41f11239 107typedef enum ReadLineFlags {
609ae0f5
LP
108 READ_LINE_ONLY_NUL = 1 << 0,
109 READ_LINE_IS_A_TTY = 1 << 1,
110 READ_LINE_NOT_A_TTY = 1 << 2,
41f11239 111} ReadLineFlags;
676bcb0f 112
41f11239
LP
113int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);
114
115static inline int read_line(FILE *f, size_t limit, char **ret) {
116 return read_line_full(f, limit, 0, ret);
117}
118
119static inline int read_nul_string(FILE *f, size_t limit, char **ret) {
120 return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret);
121}
285a9b27
LP
122
123int safe_fgetc(FILE *f, char *ret);
7a309a8c
YW
124
125int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);
494735f3 126
2da3dc69 127int rename_and_apply_smack_floor_label(const char *temp_path, const char *dest_path);