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