chiark / gitweb /
Prep v228: Add remaining updates from upstream (3/3)
[elogind.git] / src / basic / fileio.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <dirent.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29
30 #include "macro.h"
31 #include "time-util.h"
32
33 typedef enum {
34         WRITE_STRING_FILE_CREATE = 1,
35         WRITE_STRING_FILE_ATOMIC = 2,
36         WRITE_STRING_FILE_AVOID_NEWLINE = 4,
37         WRITE_STRING_FILE_VERIFY_ON_FAILURE = 8,
38 } WriteStringFileFlags;
39
40 int write_string_stream(FILE *f, const char *line, bool enforce_newline);
41 int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags);
42
43 int read_one_line_file(const char *fn, char **line);
44 int read_full_file(const char *fn, char **contents, size_t *size);
45 int read_full_stream(FILE *f, char **contents, size_t *size);
46
47 int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
48
49 int parse_env_file(const char *fname, const char *separator, ...) _sentinel_;
50 int load_env_file(FILE *f, const char *fname, const char *separator, char ***l);
51 int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l);
52
53 int write_env_file(const char *fname, char **l);
54
55 // UNNEEDED int executable_is_script(const char *path, char **interpreter);
56
57 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
58
59 DIR *xopendirat(int dirfd, const char *name, int flags);
60
61 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
62 // UNNEEDED int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
63
64 #define FOREACH_LINE(line, f, on_error)                         \
65         for (;;)                                                \
66                 if (!fgets(line, sizeof(line), f)) {            \
67                         if (ferror(f)) {                        \
68                                 on_error;                       \
69                         }                                       \
70                         break;                                  \
71                 } else
72
73 int fflush_and_check(FILE *f);
74
75 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
76 int mkostemp_safe(char *pattern, int flags);
77 // UNNEEDED int open_tmpfile(const char *path, int flags);
78
79 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
80 int tempfn_random(const char *p, const char *extra, char **ret);
81 // UNNEEDED int tempfn_random_child(const char *p, const char *extra, char **ret);
82
83 // UNNEEDED int write_timestamp_file_atomic(const char *fn, usec_t n);
84 // UNNEEDED int read_timestamp_file(const char *fn, usec_t *ret);