aboutsummaryrefslogtreecommitdiff
path: root/lib/common/data_writer_ostream.c
blob: 94a04f2c5302010fef18fa223539c5d8527c78ac (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * data_writer_ostream.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "common.h"

#include <stdlib.h>

typedef struct{
	ostream_t base;

	sqfs_block_processor_t *proc;
	const char *filename;
} data_writer_ostream_t;

static int stream_append(ostream_t *base, const void *data, size_t size)
{
	data_writer_ostream_t *strm = (data_writer_ostream_t *)base;
	int ret;

	ret = sqfs_block_processor_append(strm->proc, data, size);

	if (ret != 0) {
		sqfs_perror(strm->filename, NULL, ret);
		return -1;
	}

	return 0;
}

static int stream_flush(ostream_t *base)
{
	data_writer_ostream_t *strm = (data_writer_ostream_t *)base;
	int ret;

	ret = sqfs_block_processor_end_file(strm->proc);

	if (ret != 0) {
		sqfs_perror(strm->filename, NULL, ret);
		return -1;
	}

	return 0;
}

static const char *stream_get_filename(ostream_t *base)
{
	data_writer_ostream_t *strm = (data_writer_ostream_t *)base;

	return strm->filename;
}

static void stream_destroy(sqfs_object_t *base)
{
	free(base);
}

ostream_t *data_writer_ostream_create(const char *filename,
				      sqfs_block_processor_t *proc,
				      sqfs_inode_generic_t **inode,
				      int flags)
{
	data_writer_ostream_t *strm = calloc(1, sizeof(*strm));
	sqfs_object_t *obj = (sqfs_object_t *)strm;
	ostream_t *base = (ostream_t *)strm;
	int ret;

	if (strm == NULL) {
		perror(filename);
		return NULL;
	}

	ret = sqfs_block_processor_begin_file(proc, inode, NULL, flags);

	if (ret != 0) {
		sqfs_perror(filename, NULL, ret);
		free(strm);
		return NULL;
	}

	strm->proc = proc;
	strm->filename = filename;
	base->append = stream_append;
	base->flush = stream_flush;
	base->get_filename = stream_get_filename;
	obj->destroy = stream_destroy;
	return base;
}