aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/block_processor/serial.c
blob: a9190b177baa8fc7ab59fc2decf73e9a9fe8a352 (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * serial.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "internal.h"

typedef struct {
	sqfs_block_processor_t base;
	int status;
	sqfs_u8 scratch[];
} serial_block_processor_t;

static void block_processor_destroy(sqfs_object_t *obj)
{
	sqfs_block_processor_t *proc = (sqfs_block_processor_t *)obj;
	sqfs_block_t *blk;

	while (proc->free_list != NULL) {
		blk = proc->free_list;
		proc->free_list = blk->next;
		free(blk);
	}

	free(proc->blk_current);
	free(proc->frag_block);
	free(proc);
}

sqfs_block_processor_t *sqfs_block_processor_create(size_t max_block_size,
						    sqfs_compressor_t *cmp,
						    unsigned int num_workers,
						    size_t max_backlog,
						    sqfs_block_writer_t *wr,
						    sqfs_frag_table_t *tbl)
{
	serial_block_processor_t *proc;
	(void)num_workers; (void)max_backlog;

	proc = alloc_flex(sizeof(*proc), 1, max_block_size);
	if (proc == NULL)
		return NULL;

	proc->base.max_block_size = max_block_size;
	proc->base.cmp = cmp;
	proc->base.frag_tbl = tbl;
	proc->base.wr = wr;
	proc->base.stats.size = sizeof(proc->base.stats);
	((sqfs_object_t *)proc)->destroy = block_processor_destroy;
	return (sqfs_block_processor_t *)proc;
}

int append_to_work_queue(sqfs_block_processor_t *proc, sqfs_block_t *block)
{
	serial_block_processor_t *sproc = (serial_block_processor_t *)proc;
	sqfs_block_t *fragblk = NULL;

	if (sproc->status != 0) {
		free(block);
		return sproc->status;
	}

	sproc->status = block_processor_do_block(block, proc->cmp,
						 sproc->scratch,
						 proc->max_block_size);
	if (sproc->status != 0)
		return sproc->status;

	if (block->flags & SQFS_BLK_IS_FRAGMENT) {
		sproc->status = process_completed_fragment(proc, block,
							   &fragblk);
		if (fragblk == NULL)
			return sproc->status;

		sproc->status = block_processor_do_block(fragblk, proc->cmp,
							 sproc->scratch,
							 proc->max_block_size);
		if (sproc->status != 0)
			return sproc->status;
	}

	sproc->status = process_completed_block(proc, block);
	return sproc->status;
}

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

int sqfs_block_processor_finish(sqfs_block_processor_t *proc)
{
	serial_block_processor_t *sproc = (serial_block_processor_t *)proc;

	if (proc->frag_block == NULL || sproc->status != 0)
		goto fail;

	sproc->status = block_processor_do_block(proc->frag_block, proc->cmp,
						 sproc->scratch,
						 proc->max_block_size);
	if (sproc->status != 0)
		goto fail;

	sproc->status = process_completed_block(proc, proc->frag_block);
	return sproc->status;
fail:
	free(proc->frag_block);
	proc->frag_block = NULL;
	return sproc->status;
}