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

int sqfs_block_processor_write_fragment_table(sqfs_block_processor_t *proc,
					      sqfs_super_t *super)
{
	uint64_t start;
	size_t size;
	int ret;

	if (proc->num_fragments == 0) {
		super->fragment_entry_count = 0;
		super->fragment_table_start = 0xFFFFFFFFFFFFFFFFUL;
		super->flags &= ~SQFS_FLAG_ALWAYS_FRAGMENTS;
		super->flags |= SQFS_FLAG_NO_FRAGMENTS;
		return 0;
	}

	size = sizeof(proc->fragments[0]) * proc->num_fragments;
	ret = sqfs_write_table(proc->file, proc->cmp,
			       proc->fragments, size, &start);
	if (ret)
		return ret;

	super->flags &= ~SQFS_FLAG_NO_FRAGMENTS;
	super->flags |= SQFS_FLAG_ALWAYS_FRAGMENTS;
	super->fragment_entry_count = proc->num_fragments;
	super->fragment_table_start = start;
	return 0;
}

static int grow_fragment_table(sqfs_block_processor_t *proc)
{
	size_t newsz;
	void *new;

	if (proc->num_fragments >= proc->max_fragments) {
		newsz = proc->max_fragments ? proc->max_fragments * 2 : 16;

		new = realloc(proc->fragments,
			      sizeof(proc->fragments[0]) * newsz);

		if (new == NULL)
			return SQFS_ERROR_ALLOC;

		proc->max_fragments = newsz;
		proc->fragments = new;
	}

	return 0;
}

static int store_fragment(sqfs_block_processor_t *proc, sqfs_block_t *frag,
			  uint64_t signature)
{
	size_t new_sz;
	void *new;

	if (proc->frag_list_num == proc->frag_list_max) {
		new_sz = proc->frag_list_max * 2;
		new = realloc(proc->frag_list,
			      sizeof(proc->frag_list[0]) * new_sz);

		if (new == NULL)
			return SQFS_ERROR_ALLOC;

		proc->frag_list = new;
		proc->frag_list_max = new_sz;
	}

	proc->frag_list[proc->frag_list_num].index = proc->frag_block->index;
	proc->frag_list[proc->frag_list_num].offset = proc->frag_block->size;
	proc->frag_list[proc->frag_list_num].signature = signature;
	proc->frag_list_num += 1;

	sqfs_inode_set_frag_location(frag->inode, proc->frag_block->index,
				     proc->frag_block->size);

	memcpy(proc->frag_block->data + proc->frag_block->size,
	       frag->data, frag->size);

	proc->frag_block->flags |= (frag->flags & SQFS_BLK_DONT_COMPRESS);
	proc->frag_block->size += frag->size;
	return 0;
}

int handle_fragment(sqfs_block_processor_t *proc, sqfs_block_t *frag,
		    sqfs_block_t **blk_out)
{
	uint64_t signature;
	size_t i, size;
	int err;

	signature = MK_BLK_SIG(frag->checksum, frag->size);

	for (i = 0; i < proc->frag_list_num; ++i) {
		if (proc->frag_list[i].signature == signature) {
			sqfs_inode_set_frag_location(frag->inode,
						     proc->frag_list[i].index,
						     proc->frag_list[i].offset);
			return 0;
		}
	}

	if (proc->frag_block != NULL) {
		size = proc->frag_block->size + frag->size;

		if (size > proc->max_block_size) {
			*blk_out = proc->frag_block;
			proc->frag_block = NULL;
		}
	}

	if (proc->frag_block == NULL) {
		size = sizeof(sqfs_block_t) + proc->max_block_size;

		err = grow_fragment_table(proc);
		if (err)
			goto fail;

		proc->frag_block = calloc(1, size);
		if (proc->frag_block == NULL) {
			err = SQFS_ERROR_ALLOC;
			goto fail;
		}

		proc->frag_block->index = proc->num_fragments++;
		proc->frag_block->flags = SQFS_BLK_FRAGMENT_BLOCK;
	}

	err = store_fragment(proc, frag, signature);
	if (err)
		goto fail;

	return 0;
fail:
	free(*blk_out);
	*blk_out = NULL;
	return err;
}