summaryrefslogtreecommitdiffstats
path: root/src/stream.c
blob: 75f2c048be28d8594b75c50e3eecf717930569f0 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* SPDX-License-Identifier: LGPL-2.1-only */

/* Copyright (C) 2020-2021 Gediminas Jakutis */

#include <sys/types.h>
#include <sys/stat.h>
#include <linux/limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "stream.h"
#include "defs.h"
#include "datagen.h"
#include "cache.h"
#include "util.h"

static int stream_open_out(struct stream * const in);
static int stream_open_lite(struct stream * const in);
static int stream_open_in(struct stream * const in);
static int stream_flush(struct stream * const in);
static int stream_flush_array(struct stream * const in);
static int stream_flush_list(struct stream * const in);

int stream_open(struct stream * const in)
{
	int ret = 0;

	try(!in || in->fd > 0 || (!in->name && in->type != stream_randread), err, EINVAL, "invalid argunent");

	switch (in->type) {
	case (stream_lite):
		ret = stream_open_lite(in);
		break;
	case (stream_out):
		try(!in->name, err, EINVAL, "no filename given");
		ret = stream_open_out(in);
		break;
	case (stream_in):
		try(!in->name, err, EINVAL, "no filename given");
		ret = stream_open_in(in);
		break;
	case (stream_randread):
		; /* NOOP */
		break;
	default:
		try(0, err, EINVAL, "cannot open stream: stream is invalid");
	}

err:
	return ret;
}

int stream_close(struct stream * const in)
{
	int ret = 0;

	if (in->type != stream_out || in->settings->opmode == mode_fetch) {
		goto out;
	}

	if (in->name) {
		char path[PATH_MAX];
		struct stat st;

		if (in->settings->access == cached) {
			try_s((ret = stream_flush(in)), err);
		}

		snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd);

		if (!stat(in->name, &st)) {
			if (st.st_mode & S_IFREG) {
				unlink(in->name);
			} else {
				ret = EINVAL;
				rin_err("the given output file already exists and is not a regular file");
				goto err;
			}
		}

		try(linkat(AT_FDCWD, path, AT_FDCWD, in->name, AT_SYMLINK_FOLLOW), err, errno, "error linking output file to filesystem: %s", strerror(ret));
	} else {
		rin_err("no output filename given");
		ret = EINVAL;
		goto err;
	}

out:
	if (in->settings && in->settings->access == cached) {
		cache_destroy(in);
	}
err:
	in->fd > 2 ? close(in->fd) : 0;
	in->fd = -1;
	return ret;
}

struct entry_l *file_get_array(struct stream * const in, struct entry_l * const store)
{
	struct entry ent;
	struct entry_l *ret = NULL;
	ssize_t bytes_read;

	if (in->index < in->n) {
		do {
			try(0 > (bytes_read = pread(in->fd, &ent, sizeof(ent), in->index * in->settings->stride)), err, NULL, "Reading from stream failed with %i", errno);
		} while (bytes_read != sizeof(ent));

		++in->index;
		store->val = ent.val;
		ret = store;
	}

err:
	return ret;
}

struct entry_l *file_get_list(struct stream * const in, struct entry_l * const store)
{
	struct entry_l ent;
	struct entry_l *ret = NULL;
	ssize_t bytes_read;

	if (in->cnode) {
		do {
			try(0 > (bytes_read = pread(in->fd, &ent, sizeof(ent), in->cnode->file_offset)), err, NULL, "Reading from stream failed with %i", errno);
		} while (bytes_read != sizeof(ent));

		store->val = ent.val;

		if (ent.file_offset) {
			in->cnode->file_offset = ent.file_offset;
		} else { /* reached end of list; unhook */
			in->cnode = NULL;
		}

		ret = store;
	}

err:
	return ret;
}

int file_put_array(struct stream * const in, const struct entry_l * const data)
{
	int ret = 0;
	ssize_t bytes_written;

	if (in->index < in->n) {
		do {
			try(0 > (bytes_written = pwrite(in->fd, data, sizeof(data->val), in->index * in->settings->stride)), err, errno, "Writing to stream failed with %i", ret);
		} while (bytes_written != sizeof(data->val));
		++in->index;
	}

err:
	return ret;
}

int file_put_list(struct stream * const in, const struct entry_l * const node)
{
	int ret = 0;
	ssize_t bytes_written;
	struct entry_l tmp = {0}; /* we use a temp var for next offest to be zero */
	off_t offset = 0;

	if (!in->cnode) { /* if this is the very first one */
		in->cnode = &in->cnode_f;
	} else {
		offset = in->cnode->file_offset;
		offset += sizeof(*node); /* advance by one */
		/* writing the offset of the node being added to the current node's offset field */
		do {
			try(0 > (bytes_written = pwrite(in->fd, &offset, sizeof(offset), offset - sizeof(in->cnode->file_offset))), err, errno, "Writing to stream failed with %i", ret);
		} while (bytes_written != sizeof(in->cnode->file_offset));
	}

	tmp.val = node->val;

	do {
		try(0 > (bytes_written = pwrite(in->fd, &tmp, sizeof(tmp), offset)), err, errno, "Writing to stream failed with %i", ret);
	} while (bytes_written != sizeof(tmp));

	in->cnode->file_offset = offset; /* store the new "current" offset */
err:
	return ret;
}

int file_transfer(struct stream * const src, struct stream * const dest)
{
	int ret = 0;
	int tmp;

	/* just a simple switcheroo of file descriptors */

	tmp = src->fd;
	src->fd = dest->fd;
	dest->fd = tmp;

	return ret;
}

/* generic, naïve and slow copy routine when an optimized one cannot be used */
int stream_copy_range(struct stream * const restrict src, struct stream * const restrict dest)
{
	int ret = 0;
	struct entry_l *tmp;
	struct entry_l tmp_store;
	size_t i = 0;

	try(src->n < dest->settings->to, err, EINVAL, "invalid copy size");

	while (i++ < (dest->n) && (tmp = src->get(src, &tmp_store))) {
		dest->put(dest, tmp);
	}

	stream_rewind(dest);

err:
	return ret;
}

int stream_shallow_copy(struct stream const * const restrict src, struct stream * const dest)
{
	int ret = 0;

	dest->n = dest->n ? dest->n : src->n;
	dest->settings = src->settings;
	dest->index = 0;

	dest->get = src->get;
	dest->put = src->put;
	dest->copy = src->copy;

	if (src->settings->access == cached) {
		dest->fd = -1;
		dest->type = stream_lite;
		try_s((ret = cache_create(dest)), err);
	} else {
		dest->name = src->name;
		dest->type = stream_lite;
		try_s((ret = stream_open_lite(dest)), err);
	}

err:
	return ret;
}

static int stream_open_out(struct stream * const in)
{
	struct stat st;
	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP;
	char *dname = NULL;
	char *tmp[2];
	int ret = 0;

	tmp[0] = strdup(in->name);
	tmp[1] = dirname(tmp[0]);
	dname = strdup(tmp[1]);
	free(tmp[0]);

	try(stat(dname, &st), err, errno, "stat failed: %s", strerror(ret));
	try(!(st.st_mode & S_IFDIR), err, EINVAL, "invalid output path");

	if (!stat(in->name, &st)) {
		try(!(st.st_mode & S_IFREG), err, EINVAL, "the given output file already exists and is not a regular file");
		mode = st.st_mode;
	}

	in->fd = open(dname, O_TMPFILE | O_RDWR, mode);
	try(in->fd < 0, err, errno, "failed creating temporary output file: %s", strerror(ret));
	try(ftruncate(in->fd, in->settings->stride * in->n), err, errno, "failed setting output file size: %s", strerror(ret));

err:
	free(dname);
	return ret;
}

static int stream_open_lite(struct stream * const in)
{
	struct stat st;
	char *dname = NULL;
	char *tmp[2];
	int ret = 0;

	tmp[0] = strdup(in->name);
	tmp[1] = dirname(tmp[0]);
	dname = strdup(tmp[1]);
	free(tmp[0]);

	try(stat(dname, &st), err, errno, "stat failed: %s", strerror(ret));
	try(!(st.st_mode & S_IFDIR), err, EINVAL, "invalid output path");

	in->fd = open(dname, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
	try(in->fd < 0, err, errno, "failed creating temporary output file: %s", strerror(ret));
	try(ftruncate(in->fd, in->settings->stride * in->n), err, errno, "failed setting output file size: %s", strerror(ret));

err:
	free(dname);
	return ret;
}

static int stream_open_in(struct stream * const in)
{
	struct stat st;
	int ret = 0;

	try(stat(in->name, &st), err, errno, "stat failed: %s", strerror(ret));
	try(!(st.st_mode & S_IFREG) || !st.st_size || (st.st_size % in->settings->stride), err, EINVAL, "invalid input file");
	in->n = st.st_size / in->settings->stride;
	in->fd = open(in->name, O_RDONLY | O_NOATIME);
	try(in->fd < 0, err, errno, "failed opening input file: %s", strerror(ret));

	if (in->settings->access == direct) {
		in->cnode = &in->cnode_f;
	}

err:
	return ret;
}

static int stream_flush(struct stream * const in)
{
	int ret = 0;

	try(in->settings->access != cached, err, EINVAL, "cannot flush an uncached stream");
	try(in->type != stream_out, err, EINVAL, "cannot flush a non-output cache");

	if (in->settings->format == array) {
		ret = stream_flush_array(in);
	} else {
		ret = stream_flush_list(in);
	}
err:
	return ret;
}

static int stream_flush_array(struct stream * const in)
{
	ssize_t ret = 0;
	size_t remaining = in->n * in->settings->stride;
	ssize_t written = 0;

	try(in->fd < 3, err, EINVAL, "can't dump without an open file");

	do {
		ret = write(in->fd, in->cache + written, remaining);
		if (ret < 0) {
			try(errno != EAGAIN, err, errno, "Writing to stream failed with %zi", ret);
		} else {
			written += ret;
			remaining -= ret;
			ret = 0;
		}
	} while (remaining);

err:
	return ret;
}

static int stream_flush_list(struct stream * const in)
{
	ssize_t ret = 0;
	size_t remaining = in->n * in->settings->stride;
	ssize_t written = 0;
	size_t i;

	/* adjust pointers to have a base starting at 0, to cater to disk storage format.
	 * File offsets are otherwise 1:1 to memory addresses, which is neat.
	 * Just don't process the last offset, as that is always NULL/zero.
	 * */

	for (i = 0; i < (in->n - 1); ++i) {
		in->cache_l[i].offset = (char *) in->cache_l[i].next - in->cache;
	}

	/* dump the bloody list */
	do {
		ret = write(in->fd, in->cache + written, remaining);
		if (ret < 0) {
			try(errno != EAGAIN, err, errno, "Writing to stream failed with %zi", ret);
		} else {
			written += ret;
			remaining -= ret;
			ret = 0;
		}
	} while (remaining);

err:
	return ret;
}