aboutsummaryrefslogtreecommitdiff
path: root/lib/fstream/unix/ostream.c
blob: dac675f42fd1ee0077867f8e71537a6f06d585b8 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * ostream.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "../internal.h"

typedef struct {
	ostream_t base;
	char *path;
	int fd;
} file_ostream_t;

static int file_append(ostream_t *strm, const void *data, size_t size)
{
	file_ostream_t *file = (file_ostream_t *)strm;
	ssize_t ret;

	while (size > 0) {
		ret = write(file->fd, data, size);

		if (ret == 0) {
			fprintf(stderr, "%s: truncated data write.\n",
				file->path);
			return -1;
		}

		if (ret < 0) {
			if (errno == EINTR)
				continue;

			perror(file->path);
			return -1;
		}

		size -= ret;
		data = (const char *)data + ret;
	}

	return 0;
}

static int file_append_sparse(ostream_t *strm, size_t size)
{
	file_ostream_t *file = (file_ostream_t *)strm;

	if (lseek(file->fd, size, SEEK_CUR) == (off_t)-1) {
		perror(file->path);
		return -1;
	}

	return 0;
}

static int file_flush(ostream_t *strm)
{
	file_ostream_t *file = (file_ostream_t *)strm;

	if (fsync(file->fd) != 0) {
		perror(file->path);
		return -1;
	}

	return 0;
}

static void file_destroy(sqfs_object_t *obj)
{
	file_ostream_t *file = (file_ostream_t *)obj;

	if (file->fd != STDOUT_FILENO)
		close(file->fd);

	free(file->path);
	free(file);
}

static const char *file_get_filename(ostream_t *strm)
{
	file_ostream_t *file = (file_ostream_t *)strm;

	return file->path;
}

ostream_t *ostream_open_file(const char *path, int flags)
{
	file_ostream_t *file = calloc(1, sizeof(*file));
	sqfs_object_t *obj = (sqfs_object_t *)file;
	ostream_t *strm = (ostream_t *)file;

	if (file == NULL) {
		perror(path);
		return NULL;
	}

	file->path = strdup(path);
	if (file->path == NULL) {
		perror(path);
		goto fail_free;
	}

	if (flags & OSTREAM_OPEN_OVERWRITE) {
		file->fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	} else {
		file->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
	}

	if (file->fd < 0) {
		perror(path);
		goto fail_path;
	}

	if (flags & OSTREAM_OPEN_SPARSE)
		strm->append_sparse = file_append_sparse;

	strm->append = file_append;
	strm->flush = file_flush;
	strm->get_filename = file_get_filename;
	obj->destroy = file_destroy;
	return strm;
fail_path:
	free(file->path);
fail_free:
	free(file);
	return NULL;
}

ostream_t *ostream_open_stdout(void)
{
	file_ostream_t *file = calloc(1, sizeof(*file));
	sqfs_object_t *obj = (sqfs_object_t *)file;
	ostream_t *strm = (ostream_t *)file;

	if (file == NULL)
		goto fail;

	file->path = strdup("stdout");
	if (file->path == NULL)
		goto fail;

	file->fd = STDOUT_FILENO;
	strm->append = file_append;
	strm->flush = file_flush;
	strm->get_filename = file_get_filename;
	obj->destroy = file_destroy;
	return strm;
fail:
	perror("creating file wrapper for stdout");
	free(file);
	return NULL;
}