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