aboutsummaryrefslogtreecommitdiff
path: root/mkfs/block.c
blob: 76b261dfd809b858ae503e97d58625c855cd1bff (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "mkfs.h"
#include "util.h"

static int write_block(file_info_t *fi, sqfs_info_t *info)
{
	size_t idx, bs;
	ssize_t ret;
	void *ptr;

	idx = info->file_block_count++;
	bs = info->super.block_size;

	ret = info->cmp->do_block(info->cmp, info->block, bs,
				  info->scratch, bs);
	if (ret < 0)
		return -1;

	if (ret > 0) {
		ptr = info->scratch;
		bs = ret;
		fi->blocksizes[idx] = bs;
	} else {
		ptr = info->block;
		fi->blocksizes[idx] = bs | (1 << 24);
	}

	ret = write_retry(info->outfd, ptr, bs);
	if (ret < 0) {
		perror("writing to output file");
		return -1;
	}

	if ((size_t)ret < bs) {
		fputs("write to output file truncated\n", stderr);
		return -1;
	}

	info->super.bytes_used += bs;
	return 0;
}

static int flush_fragments(sqfs_info_t *info)
{
	size_t newsz, size;
	file_info_t *fi;
	uint64_t offset;
	void *new, *ptr;
	ssize_t ret;

	if (info->num_fragments == info->max_fragments) {
		newsz = info->max_fragments ? info->max_fragments * 2 : 16;
		new = realloc(info->fragments,
			      sizeof(info->fragments[0]) * newsz);

		if (new == NULL) {
			perror("appending to fragment table");
			return -1;
		}

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

	offset = info->super.bytes_used;
	size = info->frag_offset;

	for (fi = info->frag_list; fi != NULL; fi = fi->frag_next)
		fi->fragment = info->num_fragments;

	ret = info->cmp->do_block(info->cmp, info->fragment, size,
				  info->scratch, info->super.block_size);
	if (ret < 0)
		return -1;

	info->fragments[info->num_fragments].start_offset = htole64(offset);
	info->fragments[info->num_fragments].pad0 = 0;

	if (ret > 0) {
		ptr = info->scratch;
		size = ret;
		info->fragments[info->num_fragments].size = htole32(size);
	} else {
		ptr = info->fragment;
		info->fragments[info->num_fragments].size =
			htole32(size | (1 << 24));
	}

	info->num_fragments += 1;

	ret = write_retry(info->outfd, ptr, size);
	if (ret < 0) {
		perror("writing to output file");
		return -1;
	}

	if ((size_t)ret < size) {
		fputs("write to output file truncated\n", stderr);
		return -1;
	}

	memset(info->fragment, 0, info->super.block_size);

	info->super.bytes_used += size;
	info->frag_offset = 0;
	info->frag_list = NULL;

	info->super.flags &= ~SQFS_FLAG_NO_FRAGMENTS;
	info->super.flags |= SQFS_FLAG_ALWAYS_FRAGMENTS;
	return 0;
}

static int add_fragment(file_info_t *fi, sqfs_info_t *info, size_t size)
{
	if (info->frag_offset + size > info->super.block_size) {
		if (flush_fragments(info))
			return -1;
	}

	fi->fragment_offset = info->frag_offset;
	fi->frag_next = info->frag_list;
	info->frag_list = fi;

	memcpy((char *)info->fragment + info->frag_offset, info->block, size);
	info->frag_offset += size;
	return 0;
}

static int process_file(sqfs_info_t *info, file_info_t *fi)
{
	uint64_t count = fi->size;
	int infd, ret;
	size_t diff;

	infd = open(fi->input_file, O_RDONLY);
	if (infd < 0) {
		perror(fi->input_file);
		return -1;
	}

	fi->startblock = info->super.bytes_used;
	info->file_block_count = 0;

	while (count != 0) {
		diff = count > (uint64_t)info->super.block_size ?
			info->super.block_size : count;

		ret = read_retry(infd, info->block, diff);
		if (ret < 0)
			goto fail_read;
		if ((size_t)ret < diff)
			goto fail_trunc;

		if (diff < info->super.block_size) {
			if (add_fragment(fi, info, diff))
				goto fail;
		} else {
			if (write_block(fi, info))
				goto fail;
		}

		count -= diff;
	}

	close(infd);
	return 0;
fail:
	close(infd);
	return -1;
fail_read:
	fprintf(stderr, "read from %s: %s\n", fi->input_file, strerror(errno));
	goto fail;
fail_trunc:
	fprintf(stderr, "%s: truncated read\n", fi->input_file);
	goto fail;
}

static int find_and_process_files(sqfs_info_t *info, tree_node_t *n)
{
	if (S_ISDIR(n->mode)) {
		for (n = n->data.dir->children; n != NULL; n = n->next) {
			if (find_and_process_files(info, n))
				return -1;
		}
		return 0;
	}

	if (S_ISREG(n->mode))
		return process_file(info, n->data.file);

	return 0;
}

int write_data_to_image(sqfs_info_t *info)
{
	int ret;

	info->block = malloc(info->super.block_size);

	if (info->block == NULL) {
		perror("allocating data block buffer");
		return -1;
	}

	info->fragment = malloc(info->super.block_size);

	if (info->fragment == NULL) {
		perror("allocating fragment buffer");
		free(info->block);
		return -1;
	}

	info->scratch = malloc(info->super.block_size);
	if (info->scratch == NULL) {
		perror("allocating scratch buffer");
		free(info->block);
		free(info->fragment);
		return -1;
	}

	ret = find_and_process_files(info, info->fs.root);

	free(info->block);
	free(info->fragment);
	free(info->scratch);

	info->block = NULL;
	info->fragment = NULL;
	info->scratch = NULL;
	return ret;
}