2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-02-14 12:26:13 +01:00
|
|
|
#pragma once
|
|
|
|
|
2015-10-26 18:05:03 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdbool.h>
|
2013-02-14 12:26:13 +01:00
|
|
|
#include <stddef.h>
|
2013-05-04 17:38:23 +02:00
|
|
|
#include <stdio.h>
|
2019-04-07 20:48:30 +02:00
|
|
|
#include <sys/stat.h>
|
2015-10-26 18:05:03 +01:00
|
|
|
#include <sys/types.h>
|
2013-05-04 17:38:23 +02:00
|
|
|
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "macro.h"
|
2015-11-12 11:17:01 +01:00
|
|
|
#include "time-util.h"
|
2013-02-14 12:26:13 +01:00
|
|
|
|
2018-11-11 18:39:39 +01:00
|
|
|
#define LONG_LINE_MAX (1U*1024U*1024U)
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
typedef enum {
|
2018-06-07 16:03:43 +02:00
|
|
|
WRITE_STRING_FILE_CREATE = 1 << 0,
|
|
|
|
WRITE_STRING_FILE_ATOMIC = 1 << 1,
|
|
|
|
WRITE_STRING_FILE_AVOID_NEWLINE = 1 << 2,
|
|
|
|
WRITE_STRING_FILE_VERIFY_ON_FAILURE = 1 << 3,
|
|
|
|
WRITE_STRING_FILE_SYNC = 1 << 4,
|
|
|
|
WRITE_STRING_FILE_DISABLE_BUFFER = 1 << 5,
|
2018-11-06 13:23:03 +01:00
|
|
|
WRITE_STRING_FILE_NOFOLLOW = 1 << 6,
|
2017-11-27 16:06:39 +01:00
|
|
|
|
|
|
|
/* And before you wonder, why write_string_file_atomic_label_ts() is a separate function instead of just one
|
|
|
|
more flag here: it's about linking: we don't want to pull -lselinux into all users of write_string_file()
|
|
|
|
and friends. */
|
|
|
|
|
2015-07-07 01:19:25 +02:00
|
|
|
} WriteStringFileFlags;
|
|
|
|
|
2019-04-07 19:22:40 +02:00
|
|
|
typedef enum {
|
2019-04-08 16:40:22 +02:00
|
|
|
READ_FULL_FILE_SECURE = 1 << 0,
|
|
|
|
READ_FULL_FILE_UNBASE64 = 1 << 1,
|
2019-04-07 19:22:40 +02:00
|
|
|
} ReadFullFileFlags;
|
|
|
|
|
2019-04-04 10:17:16 +02:00
|
|
|
int fopen_unlocked(const char *path, const char *options, FILE **ret);
|
2019-04-04 11:27:08 +02:00
|
|
|
int fdopen_unlocked(int fd, const char *options, FILE **ret);
|
2019-04-04 11:46:44 +02:00
|
|
|
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
|
2019-04-04 10:17:16 +02:00
|
|
|
|
2017-09-22 20:55:34 +02:00
|
|
|
int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
|
|
|
|
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
|
|
|
|
return write_string_stream_ts(f, line, flags, NULL);
|
2017-05-11 16:23:36 +02:00
|
|
|
}
|
|
|
|
int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts);
|
|
|
|
static inline int write_string_file(const char *fn, const char *line, WriteStringFileFlags flags) {
|
|
|
|
return write_string_file_ts(fn, line, flags, NULL);
|
|
|
|
}
|
2013-05-04 17:38:23 +02:00
|
|
|
|
2018-06-05 16:02:32 +02:00
|
|
|
int write_string_filef(const char *fn, WriteStringFileFlags flags, const char *format, ...) _printf_(3, 4);
|
|
|
|
|
2019-04-07 19:22:40 +02:00
|
|
|
int read_one_line_file(const char *filename, char **line);
|
|
|
|
int read_full_file_full(const char *filename, ReadFullFileFlags flags, char **contents, size_t *size);
|
|
|
|
static inline int read_full_file(const char *filename, char **contents, size_t *size) {
|
|
|
|
return read_full_file_full(filename, 0, contents, size);
|
|
|
|
}
|
2019-04-08 07:15:10 +02:00
|
|
|
int read_full_stream_full(FILE *f, const char *filename, ReadFullFileFlags flags, char **contents, size_t *size);
|
2019-04-07 19:22:40 +02:00
|
|
|
static inline int read_full_stream(FILE *f, char **contents, size_t *size) {
|
2019-04-08 07:15:10 +02:00
|
|
|
return read_full_stream_full(f, NULL, 0, contents, size);
|
2019-04-07 19:22:40 +02:00
|
|
|
}
|
2013-02-14 12:26:13 +01:00
|
|
|
|
2015-11-13 00:54:56 +01:00
|
|
|
int verify_file(const char *fn, const char *blob, bool accept_extra_nl);
|
2015-07-06 13:38:47 +02:00
|
|
|
|
2013-07-19 10:02:50 +02:00
|
|
|
int executable_is_script(const char *path, char **interpreter);
|
2013-09-14 01:41:52 +02:00
|
|
|
|
2015-09-30 14:57:55 +02:00
|
|
|
int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field);
|
2015-10-26 18:05:03 +01:00
|
|
|
|
|
|
|
DIR *xopendirat(int dirfd, const char *name, int flags);
|
|
|
|
|
|
|
|
int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
|
|
|
|
int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
|
|
|
|
|
|
|
|
int fflush_and_check(FILE *f);
|
2017-08-17 18:09:44 +02:00
|
|
|
int fflush_sync_and_check(FILE *f);
|
2015-10-26 18:05:03 +01:00
|
|
|
|
2015-11-12 11:17:01 +01:00
|
|
|
int write_timestamp_file_atomic(const char *fn, usec_t n);
|
|
|
|
int read_timestamp_file(const char *fn, usec_t *ret);
|
2016-01-25 22:42:36 +01:00
|
|
|
|
|
|
|
int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
|
2016-04-20 19:27:32 +02:00
|
|
|
|
2018-12-17 11:50:33 +01:00
|
|
|
typedef enum ReadLineFlags {
|
|
|
|
READ_LINE_ONLY_NUL = 1 << 0,
|
|
|
|
} ReadLineFlags;
|
2016-12-01 23:19:31 +01:00
|
|
|
|
2018-12-17 11:50:33 +01:00
|
|
|
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);
|
|
|
|
|
|
|
|
static inline int read_line(FILE *f, size_t limit, char **ret) {
|
|
|
|
return read_line_full(f, limit, 0, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int read_nul_string(FILE *f, size_t limit, char **ret) {
|
|
|
|
return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret);
|
|
|
|
}
|
2018-12-17 11:21:12 +01:00
|
|
|
|
|
|
|
int safe_fgetc(FILE *f, char *ret);
|
2019-04-07 20:48:30 +02:00
|
|
|
|
|
|
|
int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);
|