chiark / gitweb /
fileio: document why fileio-label.c and fileio.c are two different modules
[elogind.git] / src / basic / fileio.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dirent.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #include "macro.h"
29 #include "time-util.h"
30
31 typedef enum {
32         WRITE_STRING_FILE_CREATE            = 1<<0,
33         WRITE_STRING_FILE_ATOMIC            = 1<<1,
34         WRITE_STRING_FILE_AVOID_NEWLINE     = 1<<2,
35         WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1<<3,
36         WRITE_STRING_FILE_SYNC              = 1<<4,
37
38         /* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
39            more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
40            and friends. */
41
42 } WriteStringFileFlags;
43
44 int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
45 static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
46         return write_string_stream_ts(f, line, flags, NULL);
47 }
48 int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
49 static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
50         return write_string_file_ts(fn, line, flags, NULL);
51 }
52
53 int read_one_line_file(const char *fn, char **line);
54 int read_full_file(const char *fn, char **contents, size_t *size);
55 int read_full_stream(FILE *f, char **contents, size_t *size);
56
57 int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
58
59 int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
60 #if 0 /// UNNEEDED by elogind
61 int load_env_file(FILE *f, const char *fname, const char *separator, char ***l);
62 #endif // 0
63 int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l);
64
65 #if 0 /// UNNEEDED by elogind
66 int merge_env_file(char ***env, FILE *f, const char *fname);
67
68 int write_env_file(const char *fname, char **l);
69
70 int executable_is_script(const char *path, char **interpreter);
71 #endif // 0
72
73 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
74
75 DIR *xopendirat(int dirfd, const char *name, int flags);
76
77 #if 0 /// UNNEEDED by elogind
78 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
79 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
80 #endif // 0
81
82 #define FOREACH_LINE(line, f, on_error)                         \
83         for (;;)                                                \
84                 if (!fgets(line, sizeof(line), f)) {            \
85                         if (ferror(f)) {                        \
86                                 on_error;                       \
87                         }                                       \
88                         break;                                  \
89                 } else
90
91 int fflush_and_check(FILE *f);
92 int fflush_sync_and_check(FILE *f);
93
94 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
95 int mkostemp_safe(char *pattern);
96
97 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
98 int tempfn_random(const char *p, const char *extra, char **ret);
99 #if 0 /// UNNEEDED by elogind
100 int tempfn_random_child(const char *p, const char *extra, char **ret);
101
102 int write_timestamp_file_atomic(const char *fn, usec_t n);
103 int read_timestamp_file(const char *fn, usec_t *ret);
104
105 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
106 #endif // 0
107
108 int open_tmpfile_unlinkable(const char *directory, int flags);
109 #if 0 /// UNNEEDED by elogind
110 int open_tmpfile_linkable(const char *target, int flags, char **ret_path);
111 #endif // 0
112 int open_serialization_fd(const char *ident);
113
114 #if 0 /// UNNEEDED by elogind
115 int link_tmpfile(int fd, const char *path, const char *target);
116
117 int read_nul_string(FILE *f, char **ret);
118
119 int mkdtemp_malloc(const char *template, char **ret);
120 #endif // 0
121
122 int read_line(FILE *f, size_t limit, char **ret);