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

#include "sqfs/block_processor.h"
#include "util.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct sqfs_block_processor_t {
	size_t max_block_size;
	sqfs_compressor_t *cmp;
	sqfs_block_cb cb;
	void *user;
	int status;

	uint8_t scratch[];
};

sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size,
						    sqfs_compressor_t *cmp,
						    unsigned int num_workers,
						    void *user,
						    sqfs_block_cb callback)
{
	sqfs_block_processor_t *proc;
	(void)num_workers;

	proc = alloc_flex(sizeof(*proc), 1, max_block_size);

	if (proc == NULL) {
		perror("Creating block processor");
		return NULL;
	}

	proc->max_block_size = max_block_size;
	proc->cmp = cmp;
	proc->cb = callback;
	proc->user = user;
	return proc;
}

void sqfs_block_processor_destroy(sqfs_block_processor_t *proc)
{
	free(proc);
}

int sqfs_block_processor_enqueue(sqfs_block_processor_t *proc,
				 sqfs_block_t *block)
{
	if (sqfs_block_process(block, proc->cmp,
			       proc->scratch, proc->max_block_size))
		goto fail;

	if (proc->cb(proc->user, block))
		goto fail;

	free(block);
	return 0;
fail:
	free(block);
	proc->status = -1;
	return -1;
}

int sqfs_block_processor_finish(sqfs_block_processor_t *proc)
{
	return proc->status;
}