chiark / gitweb /
0a23c9fae944e062d79c405989b659093172fb71
[elogind.git] / src / basic / fileio.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8 ***/
9
10 #include <dirent.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <sys/types.h>
15
16 #include "macro.h"
17 #include "time-util.h"
18
19 typedef enum {
20         WRITE_STRING_FILE_CREATE            = 1 << 0,
21         WRITE_STRING_FILE_ATOMIC            = 1 << 1,
22         WRITE_STRING_FILE_AVOID_NEWLINE     = 1 << 2,
23         WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 3,
24         WRITE_STRING_FILE_SYNC              = 1 << 4,
25         WRITE_STRING_FILE_DISABLE_BUFFER    = 1 << 5,
26
27         /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
28            more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
29            and friends. */
30
31 } WriteStringFileFlags;
32
33 int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
34 static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
35         return write_string_stream_ts(f, line, flags, NULL);
36 }
37 int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
38 static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
39         return write_string_file_ts(fn, line, flags, NULL);
40 }
41
42 int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
43
44 int read_one_line_file(const char *fn, char **line);
45 int read_full_file(const char *fn, char **contents, size_t *size);
46 int read_full_stream(FILE *f, char **contents, size_t *size);
47
48 int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
49
50 int parse_env_filev(FILE *f, const char *fname, const char *separator, va_list ap);
51 int parse_env_file(FILE *f, const char *fname, const char *separator, ...) _sentinel_;
52 #if 0 /// UNNEEDED by elogind
53 int load_env_file(FILE *f, const char *fname, const char *separator, char ***l);
54 #endif // 0
55 int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l);
56
57 #if 0 /// UNNEEDED by elogind
58 int merge_env_file(char ***env, FILE *f, const char *fname);
59
60 int write_env_file(const char *fname, char **l);
61
62 int executable_is_script(const char *path, char **interpreter);
63 #endif // 0
64
65 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
66
67 DIR *xopendirat(int dirfd, const char *name, int flags);
68
69 #if 0 /// UNNEEDED by elogind
70 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
71 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
72 #endif // 0
73
74 #define FOREACH_LINE(line, f, on_error)                         \
75         for (;;)                                                \
76                 if (!fgets(line, sizeof(line), f)) {            \
77                         if (ferror(f)) {                        \
78                                 on_error;                       \
79                         }                                       \
80                         break;                                  \
81                 } else
82
83 int fflush_and_check(FILE *f);
84 int fflush_sync_and_check(FILE *f);
85
86 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
87 int mkostemp_safe(char *pattern);
88
89 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
90 int tempfn_random(const char *p, const char *extra, char **ret);
91 #if 0 /// UNNEEDED by elogind
92 int tempfn_random_child(const char *p, const char *extra, char **ret);
93
94 int write_timestamp_file_atomic(const char *fn, usec_t n);
95 int read_timestamp_file(const char *fn, usec_t *ret);
96
97 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
98 #endif // 0
99
100 int open_tmpfile_unlinkable(const char *directory, int flags);
101 #if 0 /// UNNEEDED by elogind
102 int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
103 #endif // 0
104 int open_serialization_fd(const char *ident);
105
106 #if 0 /// UNNEEDED by elogind
107 int link_tmpfile(int fd, const char *path, const char *target);
108
109 int read_nul_string(FILE *f, char **ret);
110
111 int mkdtemp_malloc(const char *template, char **ret);
112 #endif // 0
113
114 int read_line(FILE *f, size_t limit, char **ret);