summaryrefslogtreecommitdiffstats
path: root/liblog_use_file.c
blob: 3083f1a405947619c79d62d48c6a9bc52d5b17bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST

struct liblog_output *
liblog_use_file(struct liblog_context *ctx, int dirfd, const char *path, const char *prefixfmt, int use_backtrace)
{
	struct liblog_output *ret;
	int fd;
	if (!path || !*path)
		return liblog_use_fd(ctx, dirfd, prefixfmt, use_backtrace);
	if (dirfd == -1 && *path != '/') {
		errno = EINVAL;
		return NULL;
	}
	fd = openat(dirfd, path, O_WRONLY | O_CREAT | O_APPEND, 0644);
	if (fd < 0)
		return NULL;
	ret = liblog_use_fd(ctx, fd, prefixfmt, use_backtrace);
	if (!ret) {
		close(fd);
		return NULL;
	}
	ret->sink.file.owns_fd = 1;
	return ret;
}

#else

int main(void) {return 0;} /* TODO test */

#endif