aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src/unix/ostream.c
blob: 52a566a63c6cf4f8823896511c0f3e9ba43d3c5b (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * ostream.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "../internal.h"
#include "sqfs/io.h"

typedef struct {
	sqfs_ostream_t base;
	char *path;
	int flags;
	int fd;

	off_t sparse_count;
	off_t size;
} file_ostream_t;

static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size)
{
	while (size > 0) {
		ssize_t ret = write(file->fd, data, size);

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

		if (ret < 0) {
			if (errno == EINTR)
				continue;
			perror(file->path);
			return -1;
		}

		file->size += ret;
		size -= ret;
		data += ret;
	}

	return 0;
}

static int realize_sparse(file_ostream_t *file)
{
	unsigned char *buffer;
	size_t diff, bufsz;

	if (file->sparse_count == 0)
		return 0;

	if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) {
		bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count;
		buffer = calloc(1, bufsz);
		if (buffer == NULL)
			goto fail;

		while (file->sparse_count > 0) {
			diff = file->sparse_count > (off_t)bufsz ?
				bufsz : (size_t)file->sparse_count;

			if (write_all(file, buffer, diff)) {
				free(buffer);
				return -1;
			}

			file->sparse_count -= diff;
		}

		free(buffer);
	} else {
		if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1)
			goto fail;

		if (ftruncate(file->fd, file->size) != 0)
			goto fail;

		file->sparse_count = 0;
	}

	return 0;
fail:
	perror(file->path);
	return -1;
}

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

	if (size == 0)
		return 0;

	if (data == NULL) {
		file->sparse_count += size;
		file->size += size;
		return 0;
	}

	if (realize_sparse(file))
		return -1;

	return write_all(file, data, size);
}

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

	if (realize_sparse(file))
		return -1;

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

	return 0;
}

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

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

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

	return file->path;
}

sqfs_ostream_t *ostream_open_handle(const char *path, int fd, int flags)
{
	file_ostream_t *file = calloc(1, sizeof(*file));
	sqfs_ostream_t *strm = (sqfs_ostream_t *)file;

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

	sqfs_object_init(file, file_destroy, NULL);

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

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

	close(fd);

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

sqfs_ostream_t *ostream_open_file(const char *path, int flags)
{
	sqfs_file_handle_t fd;
	sqfs_ostream_t *out;

	if (sqfs_open_native_file(&fd, path, flags)) {
		perror(path);
		return NULL;
	}

	out = ostream_open_handle(path, fd, flags);
	if (out == NULL)
		close(fd);

	return out;
}

sqfs_ostream_t *ostream_open_stdout(void)
{
	return ostream_open_handle("stdout", STDOUT_FILENO,
				   SQFS_FILE_OPEN_NO_SPARSE);
}