summaryrefslogtreecommitdiff
path: root/lib/sqfs/meta_writer.c
blob: a5ce3669cc67cdb94b668bc4078b1cc5e338f251 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * meta_writer.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/meta_writer.h"
#include "sqfs/compress.h"
#include "sqfs/error.h"
#include "sqfs/block.h"
#include "sqfs/io.h"
#include "util.h"

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

typedef struct meta_block_t {
	struct meta_block_t *next;

	/* possibly compressed data with 2 byte header */
	sqfs_u8 data[SQFS_META_BLOCK_SIZE + 2];
} meta_block_t;

struct sqfs_meta_writer_t {
	/* A byte offset into the uncompressed data of the current block */
	size_t offset;

	/* The location of the current block in the file */
	size_t block_offset;

	/* The underlying file descriptor to write to */
	sqfs_file_t *file;

	/* A pointer to the compressor to use for compressing the data */
	sqfs_compressor_t *cmp;

	/* The raw data chunk that data is appended to */
	sqfs_u8 data[SQFS_META_BLOCK_SIZE];

	int flags;
	meta_block_t *list;
	meta_block_t *list_end;
};

static int write_block(sqfs_file_t *file, meta_block_t *outblk)
{
	size_t count = le16toh(((sqfs_u16 *)outblk->data)[0]) & 0x7FFF;
	sqfs_u64 off = file->get_size(file);

	return file->write_at(file, off, outblk->data, count + 2);
}

sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file,
					    sqfs_compressor_t *cmp,
					    int flags)
{
	sqfs_meta_writer_t *m;

	if (flags & ~SQFS_META_WRITER_ALL_FLAGS)
		return NULL;

	m = calloc(1, sizeof(*m));
	if (m == NULL)
		return NULL;

	m->cmp = cmp;
	m->file = file;
	m->flags = flags;
	return m;
}

void sqfs_meta_writer_destroy(sqfs_meta_writer_t *m)
{
	meta_block_t *blk;

	while (m->list != NULL) {
		blk = m->list;
		m->list = blk->next;
		free(blk);
	}

	free(m);
}

int sqfs_meta_writer_flush(sqfs_meta_writer_t *m)
{
	meta_block_t *outblk;
	size_t count;
	ssize_t ret;

	if (m->offset == 0)
		return 0;

	outblk = calloc(1, sizeof(*outblk));
	if (outblk == NULL)
		return SQFS_ERROR_ALLOC;

	ret = m->cmp->do_block(m->cmp, m->data, m->offset,
			       outblk->data + 2, sizeof(outblk->data) - 2);
	if (ret < 0) {
		free(outblk);
		return ret;
	}

	if (ret > 0) {
		((sqfs_u16 *)outblk->data)[0] = htole16(ret);
		count = ret + 2;
	} else {
		((sqfs_u16 *)outblk->data)[0] = htole16(m->offset | 0x8000);
		memcpy(outblk->data + 2, m->data, m->offset);
		count = m->offset + 2;
	}

	ret = 0;

	if (m->flags & SQFS_META_WRITER_KEEP_IN_MEMORY) {
		if (m->list == NULL) {
			m->list = outblk;
		} else {
			m->list_end->next = outblk;
		}
		m->list_end = outblk;
	} else {
		ret = write_block(m->file, outblk);
		free(outblk);
	}

	memset(m->data, 0, sizeof(m->data));
	m->offset = 0;
	m->block_offset += count;
	return ret;
}

int sqfs_meta_writer_append(sqfs_meta_writer_t *m, const void *data,
			    size_t size)
{
	size_t diff;
	int ret;

	while (size != 0) {
		diff = sizeof(m->data) - m->offset;

		if (diff == 0) {
			ret = sqfs_meta_writer_flush(m);
			if (ret)
				return ret;
			diff = sizeof(m->data);
		}

		if (diff > size)
			diff = size;

		memcpy(m->data + m->offset, data, diff);
		m->offset += diff;
		size -= diff;
		data = (const char *)data + diff;
	}

	if (m->offset == sizeof(m->data))
		return sqfs_meta_writer_flush(m);

	return 0;
}

void sqfs_meta_writer_get_position(const sqfs_meta_writer_t *m,
				   sqfs_u64 *block_start,
				   sqfs_u32 *offset)
{
	*block_start = m->block_offset;
	*offset = m->offset;
}

void sqfs_meta_writer_reset(sqfs_meta_writer_t *m)
{
	m->block_offset = 0;
	m->offset = 0;
}

int sqfs_meta_write_write_to_file(sqfs_meta_writer_t *m)
{
	meta_block_t *blk;
	int ret;

	while (m->list != NULL) {
		blk = m->list;

		ret = write_block(m->file, blk);
		if (ret)
			return ret;

		m->list = blk->next;
		free(blk);
	}

	m->list_end = NULL;
	return 0;
}