blob: baa30cdc15c17213770a5918b421b5c8df9f924d [file] [log] [blame]
Colin Cross28fa5bc2012-05-20 13:28:05 -07001/*
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
Colin Crossb55dcee2012-04-24 23:07:49 -070017#include <assert.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070018#include <stdlib.h>
19
20#include <sparse/sparse.h>
21
Mark Salyzyn031a7482014-02-27 16:56:15 -080022#include "defs.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070023#include "sparse_file.h"
24
25#include "output_file.h"
26#include "backed_block.h"
27#include "sparse_defs.h"
Colin Crossbdc6d392012-05-02 15:18:22 -070028#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070029
Colin Cross28fa5bc2012-05-20 13:28:05 -070030struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len)
31{
32 struct sparse_file *s = calloc(sizeof(struct sparse_file), 1);
33 if (!s) {
34 return NULL;
35 }
36
Colin Crossbe8ddcb2012-04-25 21:02:16 -070037 s->backed_block_list = backed_block_list_new(block_size);
Colin Cross411619e2012-04-24 18:51:42 -070038 if (!s->backed_block_list) {
39 free(s);
40 return NULL;
41 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070042
43 s->block_size = block_size;
44 s->len = len;
45
46 return s;
47}
48
49void sparse_file_destroy(struct sparse_file *s)
50{
Colin Cross411619e2012-04-24 18:51:42 -070051 backed_block_list_destroy(s->backed_block_list);
Colin Cross28fa5bc2012-05-20 13:28:05 -070052 free(s);
53}
54
55int sparse_file_add_data(struct sparse_file *s,
56 void *data, unsigned int len, unsigned int block)
57{
Colin Crossb55dcee2012-04-24 23:07:49 -070058 return backed_block_add_data(s->backed_block_list, data, len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070059}
60
61int sparse_file_add_fill(struct sparse_file *s,
62 uint32_t fill_val, unsigned int len, unsigned int block)
63{
Colin Crossb55dcee2012-04-24 23:07:49 -070064 return backed_block_add_fill(s->backed_block_list, fill_val, len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070065}
66
67int sparse_file_add_file(struct sparse_file *s,
68 const char *filename, int64_t file_offset, unsigned int len,
69 unsigned int block)
70{
Colin Crossb55dcee2012-04-24 23:07:49 -070071 return backed_block_add_file(s->backed_block_list, filename, file_offset,
72 len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070073}
74
Colin Cross9e1f17e2012-04-25 18:31:39 -070075int sparse_file_add_fd(struct sparse_file *s,
76 int fd, int64_t file_offset, unsigned int len, unsigned int block)
77{
78 return backed_block_add_fd(s->backed_block_list, fd, file_offset,
79 len, block);
80}
Colin Crossb55dcee2012-04-24 23:07:49 -070081unsigned int sparse_count_chunks(struct sparse_file *s)
Colin Cross28fa5bc2012-05-20 13:28:05 -070082{
Colin Crossb55dcee2012-04-24 23:07:49 -070083 struct backed_block *bb;
84 unsigned int last_block = 0;
85 unsigned int chunks = 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -070086
Colin Crossb55dcee2012-04-24 23:07:49 -070087 for (bb = backed_block_iter_new(s->backed_block_list); bb;
88 bb = backed_block_iter_next(bb)) {
89 if (backed_block_block(bb) > last_block) {
90 /* If there is a gap between chunks, add a skip chunk */
91 chunks++;
92 }
93 chunks++;
94 last_block = backed_block_block(bb) +
95 DIV_ROUND_UP(backed_block_len(bb), s->block_size);
96 }
97 if (last_block < DIV_ROUND_UP(s->len, s->block_size)) {
98 chunks++;
99 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700100
Colin Crossb55dcee2012-04-24 23:07:49 -0700101 return chunks;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700102}
103
Colin Cross1e17b312012-05-21 16:35:45 -0700104static void sparse_file_write_block(struct output_file *out,
105 struct backed_block *bb)
106{
107 switch (backed_block_type(bb)) {
108 case BACKED_BLOCK_DATA:
109 write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
110 break;
111 case BACKED_BLOCK_FILE:
112 write_file_chunk(out, backed_block_len(bb),
113 backed_block_filename(bb), backed_block_file_offset(bb));
114 break;
115 case BACKED_BLOCK_FD:
116 write_fd_chunk(out, backed_block_len(bb),
117 backed_block_fd(bb), backed_block_file_offset(bb));
118 break;
119 case BACKED_BLOCK_FILL:
120 write_fill_chunk(out, backed_block_len(bb),
121 backed_block_fill_val(bb));
122 break;
123 }
124}
125
126static int write_all_blocks(struct sparse_file *s, struct output_file *out)
Colin Cross28fa5bc2012-05-20 13:28:05 -0700127{
Colin Crossb55dcee2012-04-24 23:07:49 -0700128 struct backed_block *bb;
129 unsigned int last_block = 0;
130 int64_t pad;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700131
Colin Crossb55dcee2012-04-24 23:07:49 -0700132 for (bb = backed_block_iter_new(s->backed_block_list); bb;
133 bb = backed_block_iter_next(bb)) {
134 if (backed_block_block(bb) > last_block) {
135 unsigned int blocks = backed_block_block(bb) - last_block;
136 write_skip_chunk(out, (int64_t)blocks * s->block_size);
137 }
Colin Cross1e17b312012-05-21 16:35:45 -0700138 sparse_file_write_block(out, bb);
Colin Crossb55dcee2012-04-24 23:07:49 -0700139 last_block = backed_block_block(bb) +
140 DIV_ROUND_UP(backed_block_len(bb), s->block_size);
141 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700142
Colin Crossf1ec8ac2012-07-23 17:09:42 -0700143 pad = s->len - (int64_t)last_block * s->block_size;
Colin Crossb55dcee2012-04-24 23:07:49 -0700144 assert(pad >= 0);
145 if (pad > 0) {
146 write_skip_chunk(out, pad);
147 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700148
Colin Cross1e17b312012-05-21 16:35:45 -0700149 return 0;
150}
151
152int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
153 bool crc)
154{
155 int ret;
156 int chunks;
157 struct output_file *out;
158
159 chunks = sparse_count_chunks(s);
Colin Crossb43828b2012-06-08 16:55:35 -0700160 out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
Colin Cross1e17b312012-05-21 16:35:45 -0700161
162 if (!out)
163 return -ENOMEM;
164
165 ret = write_all_blocks(s, out);
166
Colin Crossb43828b2012-06-08 16:55:35 -0700167 output_file_close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700168
Colin Cross1e17b312012-05-21 16:35:45 -0700169 return ret;
170}
171
172int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
173 int (*write)(void *priv, const void *data, int len), void *priv)
174{
175 int ret;
176 int chunks;
177 struct output_file *out;
178
179 chunks = sparse_count_chunks(s);
Colin Crossb43828b2012-06-08 16:55:35 -0700180 out = output_file_open_callback(write, priv, s->block_size, s->len, false,
Colin Cross1e17b312012-05-21 16:35:45 -0700181 sparse, chunks, crc);
182
183 if (!out)
184 return -ENOMEM;
185
186 ret = write_all_blocks(s, out);
187
Colin Crossb43828b2012-06-08 16:55:35 -0700188 output_file_close(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700189
190 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700191}
Colin Crossa21930b2012-04-26 14:24:35 -0700192
Mark Salyzyn031a7482014-02-27 16:56:15 -0800193static int out_counter_write(void *priv, const void *data __unused, int len)
Colin Crossbdc6d392012-05-02 15:18:22 -0700194{
195 int64_t *count = priv;
196 *count += len;
197 return 0;
198}
199
Colin Cross317a09e2012-05-24 17:15:43 -0700200int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc)
201{
202 int ret;
203 int chunks = sparse_count_chunks(s);
204 int64_t count = 0;
205 struct output_file *out;
206
Colin Crossb43828b2012-06-08 16:55:35 -0700207 out = output_file_open_callback(out_counter_write, &count,
Colin Cross317a09e2012-05-24 17:15:43 -0700208 s->block_size, s->len, false, sparse, chunks, crc);
209 if (!out) {
210 return -1;
211 }
212
213 ret = write_all_blocks(s, out);
214
Colin Crossb43828b2012-06-08 16:55:35 -0700215 output_file_close(out);
Colin Cross317a09e2012-05-24 17:15:43 -0700216
217 if (ret < 0) {
218 return -1;
219 }
220
221 return count;
222}
223
Colin Crossbdc6d392012-05-02 15:18:22 -0700224static struct backed_block *move_chunks_up_to_len(struct sparse_file *from,
225 struct sparse_file *to, unsigned int len)
226{
227 int64_t count = 0;
228 struct output_file *out_counter;
229 struct backed_block *last_bb = NULL;
230 struct backed_block *bb;
231 struct backed_block *start;
232 int64_t file_len = 0;
233
234 /*
235 * overhead is sparse file header, initial skip chunk, split chunk, end
236 * skip chunk, and crc chunk.
237 */
238 int overhead = sizeof(sparse_header_t) + 4 * sizeof(chunk_header_t) +
239 sizeof(uint32_t);
240 len -= overhead;
241
242 start = backed_block_iter_new(from->backed_block_list);
Colin Crossb43828b2012-06-08 16:55:35 -0700243 out_counter = output_file_open_callback(out_counter_write, &count,
Colin Crossbdc6d392012-05-02 15:18:22 -0700244 to->block_size, to->len, false, true, 0, false);
245 if (!out_counter) {
246 return NULL;
247 }
248
249 for (bb = start; bb; bb = backed_block_iter_next(bb)) {
250 count = 0;
251 /* will call out_counter_write to update count */
252 sparse_file_write_block(out_counter, bb);
253 if (file_len + count > len) {
254 /*
255 * If the remaining available size is more than 1/8th of the
256 * requested size, split the chunk. Results in sparse files that
257 * are at least 7/8ths of the requested size
258 */
259 if (!last_bb || (len - file_len > (len / 8))) {
260 backed_block_split(from->backed_block_list, bb, len - file_len);
261 last_bb = bb;
262 }
263 goto out;
264 }
265 file_len += count;
266 last_bb = bb;
267 }
268
269out:
270 backed_block_list_move(from->backed_block_list,
271 to->backed_block_list, start, last_bb);
272
Colin Crossb43828b2012-06-08 16:55:35 -0700273 output_file_close(out_counter);
Colin Crossbdc6d392012-05-02 15:18:22 -0700274
275 return bb;
276}
277
278int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len,
279 struct sparse_file **out_s, int out_s_count)
280{
281 struct backed_block *bb;
Colin Crossbdc6d392012-05-02 15:18:22 -0700282 struct sparse_file *s;
283 struct sparse_file *tmp;
284 int c = 0;
285
286 tmp = sparse_file_new(in_s->block_size, in_s->len);
287 if (!tmp) {
288 return -ENOMEM;
289 }
290
291 do {
292 s = sparse_file_new(in_s->block_size, in_s->len);
293
294 bb = move_chunks_up_to_len(in_s, s, max_len);
295
296 if (c < out_s_count) {
297 out_s[c] = s;
298 } else {
299 backed_block_list_move(s->backed_block_list, tmp->backed_block_list,
300 NULL, NULL);
301 sparse_file_destroy(s);
302 }
303 c++;
304 } while (bb);
305
306 backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list,
307 NULL, NULL);
308
309 sparse_file_destroy(tmp);
310
311 return c;
312}
313
Colin Crossa21930b2012-04-26 14:24:35 -0700314void sparse_file_verbose(struct sparse_file *s)
315{
316 s->verbose = true;
317}