blob: f5ca9071d9c9b80a30daf54ea07c99bff85afc57 [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
Colin Cross28fa5bc2012-05-20 13:28:05 -070025#include "backed_block.h"
Jerry Zhang7b444f02018-06-12 16:42:09 -070026#include "output_file.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070027#include "sparse_defs.h"
Colin Crossbdc6d392012-05-02 15:18:22 -070028#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070029
Jerry Zhang7b444f02018-06-12 16:42:09 -070030struct sparse_file* sparse_file_new(unsigned int block_size, int64_t len) {
31 struct sparse_file* s = reinterpret_cast<sparse_file*>(calloc(sizeof(struct sparse_file), 1));
32 if (!s) {
Yi Kong17ba95e2018-07-23 16:31:11 -070033 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -070034 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070035
Jerry Zhang7b444f02018-06-12 16:42:09 -070036 s->backed_block_list = backed_block_list_new(block_size);
37 if (!s->backed_block_list) {
38 free(s);
Yi Kong17ba95e2018-07-23 16:31:11 -070039 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -070040 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070041
Jerry Zhang7b444f02018-06-12 16:42:09 -070042 s->block_size = block_size;
43 s->len = len;
Colin Cross28fa5bc2012-05-20 13:28:05 -070044
Jerry Zhang7b444f02018-06-12 16:42:09 -070045 return s;
Colin Cross28fa5bc2012-05-20 13:28:05 -070046}
47
Jerry Zhang7b444f02018-06-12 16:42:09 -070048void sparse_file_destroy(struct sparse_file* s) {
49 backed_block_list_destroy(s->backed_block_list);
50 free(s);
Colin Cross28fa5bc2012-05-20 13:28:05 -070051}
52
Jerry Zhang7b444f02018-06-12 16:42:09 -070053int sparse_file_add_data(struct sparse_file* s, void* data, unsigned int len, unsigned int block) {
54 return backed_block_add_data(s->backed_block_list, data, len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070055}
56
Jerry Zhang7b444f02018-06-12 16:42:09 -070057int sparse_file_add_fill(struct sparse_file* s, uint32_t fill_val, unsigned int len,
58 unsigned int block) {
59 return backed_block_add_fill(s->backed_block_list, fill_val, len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070060}
61
Jerry Zhang7b444f02018-06-12 16:42:09 -070062int sparse_file_add_file(struct sparse_file* s, const char* filename, int64_t file_offset,
63 unsigned int len, unsigned int block) {
64 return backed_block_add_file(s->backed_block_list, filename, file_offset, len, block);
Colin Cross28fa5bc2012-05-20 13:28:05 -070065}
66
Jerry Zhang7b444f02018-06-12 16:42:09 -070067int sparse_file_add_fd(struct sparse_file* s, int fd, int64_t file_offset, unsigned int len,
68 unsigned int block) {
69 return backed_block_add_fd(s->backed_block_list, fd, file_offset, len, block);
Colin Cross9e1f17e2012-04-25 18:31:39 -070070}
Jerry Zhang7b444f02018-06-12 16:42:09 -070071unsigned int sparse_count_chunks(struct sparse_file* s) {
72 struct backed_block* bb;
73 unsigned int last_block = 0;
74 unsigned int chunks = 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -070075
Jerry Zhang7b444f02018-06-12 16:42:09 -070076 for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
77 if (backed_block_block(bb) > last_block) {
78 /* If there is a gap between chunks, add a skip chunk */
79 chunks++;
80 }
81 chunks++;
82 last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size);
83 }
84 if (last_block < DIV_ROUND_UP(s->len, s->block_size)) {
85 chunks++;
86 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070087
Jerry Zhang7b444f02018-06-12 16:42:09 -070088 return chunks;
Colin Cross28fa5bc2012-05-20 13:28:05 -070089}
90
Jerry Zhang7b444f02018-06-12 16:42:09 -070091static int sparse_file_write_block(struct output_file* out, struct backed_block* bb) {
92 int ret = -EINVAL;
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +010093
Jerry Zhang7b444f02018-06-12 16:42:09 -070094 switch (backed_block_type(bb)) {
95 case BACKED_BLOCK_DATA:
96 ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
97 break;
98 case BACKED_BLOCK_FILE:
99 ret = write_file_chunk(out, backed_block_len(bb), backed_block_filename(bb),
100 backed_block_file_offset(bb));
101 break;
102 case BACKED_BLOCK_FD:
103 ret = write_fd_chunk(out, backed_block_len(bb), backed_block_fd(bb),
104 backed_block_file_offset(bb));
105 break;
106 case BACKED_BLOCK_FILL:
107 ret = write_fill_chunk(out, backed_block_len(bb), backed_block_fill_val(bb));
108 break;
109 }
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100110
Jerry Zhang7b444f02018-06-12 16:42:09 -0700111 return ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700112}
113
Jerry Zhang7b444f02018-06-12 16:42:09 -0700114static int write_all_blocks(struct sparse_file* s, struct output_file* out) {
115 struct backed_block* bb;
116 unsigned int last_block = 0;
117 int64_t pad;
118 int ret = 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700119
Jerry Zhang7b444f02018-06-12 16:42:09 -0700120 for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
121 if (backed_block_block(bb) > last_block) {
122 unsigned int blocks = backed_block_block(bb) - last_block;
123 write_skip_chunk(out, (int64_t)blocks * s->block_size);
124 }
125 ret = sparse_file_write_block(out, bb);
126 if (ret) return ret;
127 last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), s->block_size);
128 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700129
Jerry Zhang7b444f02018-06-12 16:42:09 -0700130 pad = s->len - (int64_t)last_block * s->block_size;
131 assert(pad >= 0);
132 if (pad > 0) {
133 write_skip_chunk(out, pad);
134 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700135
Jerry Zhang7b444f02018-06-12 16:42:09 -0700136 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700137}
138
Jerry Zhang7b444f02018-06-12 16:42:09 -0700139int sparse_file_write(struct sparse_file* s, int fd, bool gz, bool sparse, bool crc) {
140 int ret;
141 int chunks;
142 struct output_file* out;
Colin Cross1e17b312012-05-21 16:35:45 -0700143
Jerry Zhang7b444f02018-06-12 16:42:09 -0700144 chunks = sparse_count_chunks(s);
145 out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
Colin Cross1e17b312012-05-21 16:35:45 -0700146
Jerry Zhang7b444f02018-06-12 16:42:09 -0700147 if (!out) return -ENOMEM;
Colin Cross1e17b312012-05-21 16:35:45 -0700148
Jerry Zhang7b444f02018-06-12 16:42:09 -0700149 ret = write_all_blocks(s, out);
Colin Cross1e17b312012-05-21 16:35:45 -0700150
Jerry Zhang7b444f02018-06-12 16:42:09 -0700151 output_file_close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700152
Jerry Zhang7b444f02018-06-12 16:42:09 -0700153 return ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700154}
155
Jerry Zhang7b444f02018-06-12 16:42:09 -0700156int sparse_file_callback(struct sparse_file* s, bool sparse, bool crc,
157 int (*write)(void* priv, const void* data, size_t len), void* priv) {
158 int ret;
159 int chunks;
160 struct output_file* out;
Colin Cross1e17b312012-05-21 16:35:45 -0700161
Jerry Zhang7b444f02018-06-12 16:42:09 -0700162 chunks = sparse_count_chunks(s);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700163 out = output_file_open_callback(write, nullptr, nullptr, nullptr, priv, s->block_size, s->len,
164 sparse, chunks, crc);
165
166 if (!out) return -ENOMEM;
167
168 ret = write_all_blocks(s, out);
169
170 output_file_close(out);
171
172 return ret;
173}
174
175int sparse_file_callback_typed(struct sparse_file* s, bool sparse,
176 int (*data_write)(void* priv, const void* data, size_t len),
177 int (*fd_write)(void* priv, int fd, size_t len),
178 int (*fill_write)(void* priv, uint32_t fill_val, size_t len),
179 int (*skip_write)(void* priv, int64_t len), void* priv) {
180 int ret;
181 int chunks;
182 struct output_file* out;
183
184 chunks = sparse_count_chunks(s);
185 out = output_file_open_callback(data_write, fd_write, fill_write, skip_write, priv, s->block_size,
186 s->len, sparse, chunks, false);
Colin Cross1e17b312012-05-21 16:35:45 -0700187
Jerry Zhang7b444f02018-06-12 16:42:09 -0700188 if (!out) return -ENOMEM;
Colin Cross1e17b312012-05-21 16:35:45 -0700189
Jerry Zhang7b444f02018-06-12 16:42:09 -0700190 ret = write_all_blocks(s, out);
Colin Cross1e17b312012-05-21 16:35:45 -0700191
Jerry Zhang7b444f02018-06-12 16:42:09 -0700192 output_file_close(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700193
Jerry Zhang7b444f02018-06-12 16:42:09 -0700194 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700195}
Colin Crossa21930b2012-04-26 14:24:35 -0700196
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800197struct chunk_data {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700198 void* priv;
199 unsigned int block;
200 unsigned int nr_blocks;
201 int (*write)(void* priv, const void* data, size_t len, unsigned int block, unsigned int nr_blocks);
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800202};
203
Jerry Zhang7b444f02018-06-12 16:42:09 -0700204static int foreach_chunk_write(void* priv, const void* data, size_t len) {
205 struct chunk_data* chk = reinterpret_cast<chunk_data*>(priv);
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800206
Jerry Zhang7b444f02018-06-12 16:42:09 -0700207 return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks);
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800208}
209
Jerry Zhang7b444f02018-06-12 16:42:09 -0700210int sparse_file_foreach_chunk(struct sparse_file* s, bool sparse, bool crc,
211 int (*write)(void* priv, const void* data, size_t len,
212 unsigned int block, unsigned int nr_blocks),
213 void* priv) {
214 int ret;
215 int chunks;
216 struct chunk_data chk;
217 struct output_file* out;
218 struct backed_block* bb;
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800219
Jerry Zhang7b444f02018-06-12 16:42:09 -0700220 chk.priv = priv;
221 chk.write = write;
222 chk.block = chk.nr_blocks = 0;
223 chunks = sparse_count_chunks(s);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700224 out = output_file_open_callback(foreach_chunk_write, nullptr, nullptr, nullptr, &chk,
225 s->block_size, s->len, sparse, chunks, crc);
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800226
Jerry Zhang7b444f02018-06-12 16:42:09 -0700227 if (!out) return -ENOMEM;
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800228
Jerry Zhang7b444f02018-06-12 16:42:09 -0700229 for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) {
230 chk.block = backed_block_block(bb);
231 chk.nr_blocks = (backed_block_len(bb) - 1) / s->block_size + 1;
232 ret = sparse_file_write_block(out, bb);
233 if (ret) return ret;
234 }
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800235
Jerry Zhang7b444f02018-06-12 16:42:09 -0700236 output_file_close(out);
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800237
Jerry Zhang7b444f02018-06-12 16:42:09 -0700238 return ret;
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800239}
240
Jerry Zhang7b444f02018-06-12 16:42:09 -0700241static int out_counter_write(void* priv, const void* data __unused, size_t len) {
242 int64_t* count = reinterpret_cast<int64_t*>(priv);
243 *count += len;
244 return 0;
Colin Crossbdc6d392012-05-02 15:18:22 -0700245}
246
Jerry Zhang7b444f02018-06-12 16:42:09 -0700247int64_t sparse_file_len(struct sparse_file* s, bool sparse, bool crc) {
248 int ret;
249 int chunks = sparse_count_chunks(s);
250 int64_t count = 0;
251 struct output_file* out;
Colin Cross317a09e2012-05-24 17:15:43 -0700252
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700253 out = output_file_open_callback(out_counter_write, nullptr, nullptr, nullptr, &count,
254 s->block_size, s->len, sparse, chunks, crc);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700255 if (!out) {
256 return -1;
257 }
Colin Cross317a09e2012-05-24 17:15:43 -0700258
Jerry Zhang7b444f02018-06-12 16:42:09 -0700259 ret = write_all_blocks(s, out);
Colin Cross317a09e2012-05-24 17:15:43 -0700260
Jerry Zhang7b444f02018-06-12 16:42:09 -0700261 output_file_close(out);
Colin Cross317a09e2012-05-24 17:15:43 -0700262
Jerry Zhang7b444f02018-06-12 16:42:09 -0700263 if (ret < 0) {
264 return -1;
265 }
Colin Cross317a09e2012-05-24 17:15:43 -0700266
Jerry Zhang7b444f02018-06-12 16:42:09 -0700267 return count;
Colin Cross317a09e2012-05-24 17:15:43 -0700268}
269
Jerry Zhang7b444f02018-06-12 16:42:09 -0700270unsigned int sparse_file_block_size(struct sparse_file* s) {
271 return s->block_size;
Adrien Schildknechta26a6bd2016-11-30 11:49:47 -0800272}
273
Jerry Zhang7b444f02018-06-12 16:42:09 -0700274static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to,
275 unsigned int len) {
276 int64_t count = 0;
277 struct output_file* out_counter;
Yi Kong17ba95e2018-07-23 16:31:11 -0700278 struct backed_block* last_bb = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700279 struct backed_block* bb;
280 struct backed_block* start;
281 unsigned int last_block = 0;
282 int64_t file_len = 0;
283 int ret;
Colin Crossbdc6d392012-05-02 15:18:22 -0700284
Jerry Zhang7b444f02018-06-12 16:42:09 -0700285 /*
286 * overhead is sparse file header, the potential end skip
287 * chunk and crc chunk.
288 */
289 int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) + sizeof(uint32_t);
290 len -= overhead;
Colin Crossbdc6d392012-05-02 15:18:22 -0700291
Jerry Zhang7b444f02018-06-12 16:42:09 -0700292 start = backed_block_iter_new(from->backed_block_list);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700293 out_counter = output_file_open_callback(out_counter_write, nullptr, nullptr, nullptr, &count,
294 to->block_size, to->len, true, 0, false);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700295 if (!out_counter) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700296 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700297 }
Colin Crossbdc6d392012-05-02 15:18:22 -0700298
Jerry Zhang7b444f02018-06-12 16:42:09 -0700299 for (bb = start; bb; bb = backed_block_iter_next(bb)) {
300 count = 0;
301 if (backed_block_block(bb) > last_block) count += sizeof(chunk_header_t);
302 last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), to->block_size);
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200303
Jerry Zhang7b444f02018-06-12 16:42:09 -0700304 /* will call out_counter_write to update count */
305 ret = sparse_file_write_block(out_counter, bb);
306 if (ret) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700307 bb = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700308 goto out;
309 }
310 if (file_len + count > len) {
311 /*
312 * If the remaining available size is more than 1/8th of the
313 * requested size, split the chunk. Results in sparse files that
314 * are at least 7/8ths of the requested size
315 */
316 file_len += sizeof(chunk_header_t);
317 if (!last_bb || (len - file_len > (len / 8))) {
318 backed_block_split(from->backed_block_list, bb, len - file_len);
319 last_bb = bb;
320 }
321 goto move;
322 }
323 file_len += count;
324 last_bb = bb;
325 }
Colin Crossbdc6d392012-05-02 15:18:22 -0700326
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100327move:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700328 backed_block_list_move(from->backed_block_list, to->backed_block_list, start, last_bb);
Colin Crossbdc6d392012-05-02 15:18:22 -0700329
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100330out:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700331 output_file_close(out_counter);
Colin Crossbdc6d392012-05-02 15:18:22 -0700332
Jerry Zhang7b444f02018-06-12 16:42:09 -0700333 return bb;
Colin Crossbdc6d392012-05-02 15:18:22 -0700334}
335
Jerry Zhang7b444f02018-06-12 16:42:09 -0700336int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct sparse_file** out_s,
337 int out_s_count) {
338 struct backed_block* bb;
339 struct sparse_file* s;
340 struct sparse_file* tmp;
341 int c = 0;
Colin Crossbdc6d392012-05-02 15:18:22 -0700342
Jerry Zhang7b444f02018-06-12 16:42:09 -0700343 tmp = sparse_file_new(in_s->block_size, in_s->len);
344 if (!tmp) {
345 return -ENOMEM;
346 }
Colin Crossbdc6d392012-05-02 15:18:22 -0700347
Jerry Zhang7b444f02018-06-12 16:42:09 -0700348 do {
349 s = sparse_file_new(in_s->block_size, in_s->len);
Colin Crossbdc6d392012-05-02 15:18:22 -0700350
Jerry Zhang7b444f02018-06-12 16:42:09 -0700351 bb = move_chunks_up_to_len(in_s, s, max_len);
Colin Crossbdc6d392012-05-02 15:18:22 -0700352
Jerry Zhang7b444f02018-06-12 16:42:09 -0700353 if (c < out_s_count) {
354 out_s[c] = s;
355 } else {
Yi Kong17ba95e2018-07-23 16:31:11 -0700356 backed_block_list_move(s->backed_block_list, tmp->backed_block_list, nullptr, nullptr);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700357 sparse_file_destroy(s);
358 }
359 c++;
360 } while (bb);
Colin Crossbdc6d392012-05-02 15:18:22 -0700361
Yi Kong17ba95e2018-07-23 16:31:11 -0700362 backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, nullptr, nullptr);
Colin Crossbdc6d392012-05-02 15:18:22 -0700363
Jerry Zhang7b444f02018-06-12 16:42:09 -0700364 sparse_file_destroy(tmp);
Colin Crossbdc6d392012-05-02 15:18:22 -0700365
Jerry Zhang7b444f02018-06-12 16:42:09 -0700366 return c;
Colin Crossbdc6d392012-05-02 15:18:22 -0700367}
368
Jerry Zhang7b444f02018-06-12 16:42:09 -0700369void sparse_file_verbose(struct sparse_file* s) {
370 s->verbose = true;
Colin Crossa21930b2012-04-26 14:24:35 -0700371}