aboutsummaryrefslogtreecommitdiff
path: root/include/sqfs/block_processor.h
blob: 1ec2f7c851eeefd9a4a8057ec7beba1a85125ad2 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * block_processor.h
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#ifndef SFQS_BLOCK_PROCESSOR_H
#define SFQS_BLOCK_PROCESSOR_H

#include "sqfs/predef.h"

enum {
	/* only calculate checksum, do NOT compress the data */
	SQFS_BLK_DONT_COMPRESS = 0x0001,

	/* set by compressor worker if the block was actually compressed */
	SQFS_BLK_IS_COMPRESSED = 0x0002,

	/* do not calculate block checksum */
	SQFS_BLK_DONT_CHECKSUM = 0x0004,

	/* set by compressor worker if compression failed */
	SQFS_BLK_COMPRESS_ERROR = 0x0008,

	/* first user setable block flag */
	SQFS_BLK_USER = 0x0080
};

struct sqfs_block_t {
	/* used internally, ignored and overwritten when enqueueing blocks */
	sqfs_block_t *next;
	uint32_t sequence_number;

	/* Size of the data area */
	uint32_t size;

	/* checksum of the input data */
	uint32_t checksum;

	/* user settable file block index */
	uint32_t index;

	/* user pointer associated with the block */
	void *user;

	/* user settable flag field */
	uint32_t flags;

	/* raw data to be processed */
	uint8_t data[];
};

/*
  Gets called for each processed block. May be called from a different thread
  than the one that calls enqueue, but only from one thread at a time.
  Guaranteed to be called on blocks in the order that they are submitted
  to enqueue.

  A non-zero return value is interpreted as fatal error.
 */
typedef int (*sqfs_block_cb)(void *user, sqfs_block_t *blk);

#ifdef __cplusplus
extern "C" {
#endif

SQFS_API
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_API void sqfs_block_processor_destroy(sqfs_block_processor_t *proc);

/*
  Add a block to be processed. Returns non-zero on error and prints a message
  to stderr.

  The function takes over ownership of the submitted block. It is freed with
  a after processing and calling the block callback.

  Even on failure, the workers may still be running and
  sqfs_block_processor_finish must be called before cleaning up.
*/
SQFS_API int sqfs_block_processor_enqueue(sqfs_block_processor_t *proc,
					  sqfs_block_t *block);

/*
  Wait for the compressor workers to finish. Returns zero on success, non-zero
  if an internal error occoured or one of the block callbacks returned a
  non-zero value.
 */
SQFS_API int sqfs_block_processor_finish(sqfs_block_processor_t *proc);

/*
  Convenience function to process a data block. Returns 0 on success,
  prints to stderr on failure.
 */
SQFS_API int sqfs_block_process(sqfs_block_t *block, sqfs_compressor_t *cmp,
				uint8_t *scratch, size_t scratch_size);

#ifdef __cplusplus
}
#endif

#endif /* SFQS_BLOCK_PROCESSOR_H */