sparse.c revision bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <stdlib.h>
19
20#include <sparse/sparse.h>
21
22#include "sparse_file.h"
23
24#include "output_file.h"
25#include "backed_block.h"
26#include "sparse_defs.h"
27#include "sparse_format.h"
28
29struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len)
30{
31	struct sparse_file *s = calloc(sizeof(struct sparse_file), 1);
32	if (!s) {
33		return NULL;
34	}
35
36	s->backed_block_list = backed_block_list_new(block_size);
37	if (!s->backed_block_list) {
38		free(s);
39		return NULL;
40	}
41
42	s->block_size = block_size;
43	s->len = len;
44
45	return s;
46}
47
48void sparse_file_destroy(struct sparse_file *s)
49{
50	backed_block_list_destroy(s->backed_block_list);
51	free(s);
52}
53
54int sparse_file_add_data(struct sparse_file *s,
55		void *data, unsigned int len, unsigned int block)
56{
57	return backed_block_add_data(s->backed_block_list, data, len, block);
58}
59
60int sparse_file_add_fill(struct sparse_file *s,
61		uint32_t fill_val, unsigned int len, unsigned int block)
62{
63	return backed_block_add_fill(s->backed_block_list, fill_val, len, block);
64}
65
66int sparse_file_add_file(struct sparse_file *s,
67		const char *filename, int64_t file_offset, unsigned int len,
68		unsigned int block)
69{
70	return backed_block_add_file(s->backed_block_list, filename, file_offset,
71			len, block);
72}
73
74int sparse_file_add_fd(struct sparse_file *s,
75		int fd, int64_t file_offset, unsigned int len, unsigned int block)
76{
77	return backed_block_add_fd(s->backed_block_list, fd, file_offset,
78			len, block);
79}
80unsigned int sparse_count_chunks(struct sparse_file *s)
81{
82	struct backed_block *bb;
83	unsigned int last_block = 0;
84	unsigned int chunks = 0;
85
86	for (bb = backed_block_iter_new(s->backed_block_list); bb;
87			bb = backed_block_iter_next(bb)) {
88		if (backed_block_block(bb) > last_block) {
89			/* If there is a gap between chunks, add a skip chunk */
90			chunks++;
91		}
92		chunks++;
93		last_block = backed_block_block(bb) +
94				DIV_ROUND_UP(backed_block_len(bb), s->block_size);
95	}
96	if (last_block < DIV_ROUND_UP(s->len, s->block_size)) {
97		chunks++;
98	}
99
100	return chunks;
101}
102
103static void sparse_file_write_block(struct output_file *out,
104		struct backed_block *bb)
105{
106	switch (backed_block_type(bb)) {
107	case BACKED_BLOCK_DATA:
108		write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
109		break;
110	case BACKED_BLOCK_FILE:
111		write_file_chunk(out, backed_block_len(bb),
112				backed_block_filename(bb), backed_block_file_offset(bb));
113		break;
114	case BACKED_BLOCK_FD:
115		write_fd_chunk(out, backed_block_len(bb),
116				backed_block_fd(bb), backed_block_file_offset(bb));
117		break;
118	case BACKED_BLOCK_FILL:
119		write_fill_chunk(out, backed_block_len(bb),
120				backed_block_fill_val(bb));
121		break;
122	}
123}
124
125static int write_all_blocks(struct sparse_file *s, struct output_file *out)
126{
127	struct backed_block *bb;
128	unsigned int last_block = 0;
129	int64_t pad;
130
131	for (bb = backed_block_iter_new(s->backed_block_list); bb;
132			bb = backed_block_iter_next(bb)) {
133		if (backed_block_block(bb) > last_block) {
134			unsigned int blocks = backed_block_block(bb) - last_block;
135			write_skip_chunk(out, (int64_t)blocks * s->block_size);
136		}
137		sparse_file_write_block(out, bb);
138		last_block = backed_block_block(bb) +
139				DIV_ROUND_UP(backed_block_len(bb), s->block_size);
140	}
141
142	pad = s->len - last_block * s->block_size;
143	assert(pad >= 0);
144	if (pad > 0) {
145		write_skip_chunk(out, pad);
146	}
147
148	return 0;
149}
150
151int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
152		bool crc)
153{
154	int ret;
155	int chunks;
156	struct output_file *out;
157
158	chunks = sparse_count_chunks(s);
159	out = open_output_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
160
161	if (!out)
162		return -ENOMEM;
163
164	ret = write_all_blocks(s, out);
165
166	close_output_file(out);
167
168	return ret;
169}
170
171int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
172		int (*write)(void *priv, const void *data, int len), void *priv)
173{
174	int ret;
175	int chunks;
176	struct output_file *out;
177
178	chunks = sparse_count_chunks(s);
179	out = open_output_callback(write, priv, s->block_size, s->len, false,
180			sparse, chunks, crc);
181
182	if (!out)
183		return -ENOMEM;
184
185	ret = write_all_blocks(s, out);
186
187	close_output_file(out);
188
189	return ret;
190}
191
192static int out_counter_write(void *priv, const void *data, int len)
193{
194	int64_t *count = priv;
195	*count += len;
196	return 0;
197}
198
199static struct backed_block *move_chunks_up_to_len(struct sparse_file *from,
200		struct sparse_file *to, unsigned int len)
201{
202	int64_t count = 0;
203	struct output_file *out_counter;
204	struct backed_block *last_bb = NULL;
205	struct backed_block *bb;
206	struct backed_block *start;
207	int64_t file_len = 0;
208
209	/*
210	 * overhead is sparse file header, initial skip chunk, split chunk, end
211	 * skip chunk, and crc chunk.
212	 */
213	int overhead = sizeof(sparse_header_t) + 4 * sizeof(chunk_header_t) +
214			sizeof(uint32_t);
215	len -= overhead;
216
217	start = backed_block_iter_new(from->backed_block_list);
218	out_counter = open_output_callback(out_counter_write, &count,
219			to->block_size, to->len, false, true, 0, false);
220	if (!out_counter) {
221		return NULL;
222	}
223
224	for (bb = start; bb; bb = backed_block_iter_next(bb)) {
225		count = 0;
226		/* will call out_counter_write to update count */
227		sparse_file_write_block(out_counter, bb);
228		if (file_len + count > len) {
229			/*
230			 * If the remaining available size is more than 1/8th of the
231			 * requested size, split the chunk.  Results in sparse files that
232			 * are at least 7/8ths of the requested size
233			 */
234			if (!last_bb || (len - file_len > (len / 8))) {
235				backed_block_split(from->backed_block_list, bb, len - file_len);
236				last_bb = bb;
237			}
238			goto out;
239		}
240		file_len += count;
241		last_bb = bb;
242	}
243
244out:
245	backed_block_list_move(from->backed_block_list,
246		to->backed_block_list, start, last_bb);
247
248	close_output_file(out_counter);
249
250	return bb;
251}
252
253int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len,
254		struct sparse_file **out_s, int out_s_count)
255{
256	struct backed_block *bb;
257	unsigned int overhead;
258	struct sparse_file *s;
259	struct sparse_file *tmp;
260	int c = 0;
261
262	tmp = sparse_file_new(in_s->block_size, in_s->len);
263	if (!tmp) {
264		return -ENOMEM;
265	}
266
267	do {
268		s = sparse_file_new(in_s->block_size, in_s->len);
269
270		bb = move_chunks_up_to_len(in_s, s, max_len);
271
272		if (c < out_s_count) {
273			out_s[c] = s;
274		} else {
275			backed_block_list_move(s->backed_block_list, tmp->backed_block_list,
276					NULL, NULL);
277			sparse_file_destroy(s);
278		}
279		c++;
280	} while (bb);
281
282	backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list,
283			NULL, NULL);
284
285	sparse_file_destroy(tmp);
286
287	return c;
288}
289
290void sparse_file_verbose(struct sparse_file *s)
291{
292	s->verbose = true;
293}
294