blob: 311678a34f79a738a8fe611ddf375ebcdabd9959 [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
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100104static int sparse_file_write_block(struct output_file *out,
Colin Cross1e17b312012-05-21 16:35:45 -0700105 struct backed_block *bb)
106{
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100107 int ret = -EINVAL;
108
Colin Cross1e17b312012-05-21 16:35:45 -0700109 switch (backed_block_type(bb)) {
110 case BACKED_BLOCK_DATA:
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100111 ret = write_data_chunk(out, backed_block_len(bb), backed_block_data(bb));
Colin Cross1e17b312012-05-21 16:35:45 -0700112 break;
113 case BACKED_BLOCK_FILE:
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100114 ret = write_file_chunk(out, backed_block_len(bb),
115 backed_block_filename(bb),
116 backed_block_file_offset(bb));
Colin Cross1e17b312012-05-21 16:35:45 -0700117 break;
118 case BACKED_BLOCK_FD:
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100119 ret = write_fd_chunk(out, backed_block_len(bb),
120 backed_block_fd(bb),
121 backed_block_file_offset(bb));
Colin Cross1e17b312012-05-21 16:35:45 -0700122 break;
123 case BACKED_BLOCK_FILL:
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100124 ret = write_fill_chunk(out, backed_block_len(bb),
125 backed_block_fill_val(bb));
Colin Cross1e17b312012-05-21 16:35:45 -0700126 break;
127 }
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100128
129 return ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700130}
131
132static int write_all_blocks(struct sparse_file *s, struct output_file *out)
Colin Cross28fa5bc2012-05-20 13:28:05 -0700133{
Colin Crossb55dcee2012-04-24 23:07:49 -0700134 struct backed_block *bb;
135 unsigned int last_block = 0;
136 int64_t pad;
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100137 int ret = 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700138
Colin Crossb55dcee2012-04-24 23:07:49 -0700139 for (bb = backed_block_iter_new(s->backed_block_list); bb;
140 bb = backed_block_iter_next(bb)) {
141 if (backed_block_block(bb) > last_block) {
142 unsigned int blocks = backed_block_block(bb) - last_block;
143 write_skip_chunk(out, (int64_t)blocks * s->block_size);
144 }
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100145 ret = sparse_file_write_block(out, bb);
146 if (ret)
147 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700148 last_block = backed_block_block(bb) +
149 DIV_ROUND_UP(backed_block_len(bb), s->block_size);
150 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700151
Colin Crossf1ec8ac2012-07-23 17:09:42 -0700152 pad = s->len - (int64_t)last_block * s->block_size;
Colin Crossb55dcee2012-04-24 23:07:49 -0700153 assert(pad >= 0);
154 if (pad > 0) {
155 write_skip_chunk(out, pad);
156 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700157
Colin Cross1e17b312012-05-21 16:35:45 -0700158 return 0;
159}
160
161int sparse_file_write(struct sparse_file *s, int fd, bool gz, bool sparse,
162 bool crc)
163{
164 int ret;
165 int chunks;
166 struct output_file *out;
167
168 chunks = sparse_count_chunks(s);
Colin Crossb43828b2012-06-08 16:55:35 -0700169 out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc);
Colin Cross1e17b312012-05-21 16:35:45 -0700170
171 if (!out)
172 return -ENOMEM;
173
174 ret = write_all_blocks(s, out);
175
Colin Crossb43828b2012-06-08 16:55:35 -0700176 output_file_close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700177
Colin Cross1e17b312012-05-21 16:35:45 -0700178 return ret;
179}
180
181int sparse_file_callback(struct sparse_file *s, bool sparse, bool crc,
182 int (*write)(void *priv, const void *data, int len), void *priv)
183{
184 int ret;
185 int chunks;
186 struct output_file *out;
187
188 chunks = sparse_count_chunks(s);
Colin Crossb43828b2012-06-08 16:55:35 -0700189 out = output_file_open_callback(write, priv, s->block_size, s->len, false,
Colin Cross1e17b312012-05-21 16:35:45 -0700190 sparse, chunks, crc);
191
192 if (!out)
193 return -ENOMEM;
194
195 ret = write_all_blocks(s, out);
196
Colin Crossb43828b2012-06-08 16:55:35 -0700197 output_file_close(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700198
199 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700200}
Colin Crossa21930b2012-04-26 14:24:35 -0700201
Mark Salyzyn031a7482014-02-27 16:56:15 -0800202static int out_counter_write(void *priv, const void *data __unused, int len)
Colin Crossbdc6d392012-05-02 15:18:22 -0700203{
204 int64_t *count = priv;
205 *count += len;
206 return 0;
207}
208
Colin Cross317a09e2012-05-24 17:15:43 -0700209int64_t sparse_file_len(struct sparse_file *s, bool sparse, bool crc)
210{
211 int ret;
212 int chunks = sparse_count_chunks(s);
213 int64_t count = 0;
214 struct output_file *out;
215
Colin Crossb43828b2012-06-08 16:55:35 -0700216 out = output_file_open_callback(out_counter_write, &count,
Colin Cross317a09e2012-05-24 17:15:43 -0700217 s->block_size, s->len, false, sparse, chunks, crc);
218 if (!out) {
219 return -1;
220 }
221
222 ret = write_all_blocks(s, out);
223
Colin Crossb43828b2012-06-08 16:55:35 -0700224 output_file_close(out);
Colin Cross317a09e2012-05-24 17:15:43 -0700225
226 if (ret < 0) {
227 return -1;
228 }
229
230 return count;
231}
232
Colin Crossbdc6d392012-05-02 15:18:22 -0700233static struct backed_block *move_chunks_up_to_len(struct sparse_file *from,
234 struct sparse_file *to, unsigned int len)
235{
236 int64_t count = 0;
237 struct output_file *out_counter;
238 struct backed_block *last_bb = NULL;
239 struct backed_block *bb;
240 struct backed_block *start;
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200241 unsigned int last_block = 0;
Colin Crossbdc6d392012-05-02 15:18:22 -0700242 int64_t file_len = 0;
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100243 int ret;
Colin Crossbdc6d392012-05-02 15:18:22 -0700244
245 /*
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200246 * overhead is sparse file header, the potential end skip
247 * chunk and crc chunk.
Colin Crossbdc6d392012-05-02 15:18:22 -0700248 */
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200249 int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) +
Colin Crossbdc6d392012-05-02 15:18:22 -0700250 sizeof(uint32_t);
251 len -= overhead;
252
253 start = backed_block_iter_new(from->backed_block_list);
Colin Crossb43828b2012-06-08 16:55:35 -0700254 out_counter = output_file_open_callback(out_counter_write, &count,
Colin Crossbdc6d392012-05-02 15:18:22 -0700255 to->block_size, to->len, false, true, 0, false);
256 if (!out_counter) {
257 return NULL;
258 }
259
260 for (bb = start; bb; bb = backed_block_iter_next(bb)) {
261 count = 0;
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200262 if (backed_block_block(bb) > last_block)
263 count += sizeof(chunk_header_t);
264 last_block = backed_block_block(bb) +
265 DIV_ROUND_UP(backed_block_len(bb), to->block_size);
266
Colin Crossbdc6d392012-05-02 15:18:22 -0700267 /* will call out_counter_write to update count */
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100268 ret = sparse_file_write_block(out_counter, bb);
269 if (ret) {
270 bb = NULL;
271 goto out;
272 }
Colin Crossbdc6d392012-05-02 15:18:22 -0700273 if (file_len + count > len) {
274 /*
275 * If the remaining available size is more than 1/8th of the
276 * requested size, split the chunk. Results in sparse files that
277 * are at least 7/8ths of the requested size
278 */
Jeremy Compostellacfd3a032015-04-03 14:31:19 +0200279 file_len += sizeof(chunk_header_t);
Colin Crossbdc6d392012-05-02 15:18:22 -0700280 if (!last_bb || (len - file_len > (len / 8))) {
281 backed_block_split(from->backed_block_list, bb, len - file_len);
282 last_bb = bb;
283 }
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100284 goto move;
Colin Crossbdc6d392012-05-02 15:18:22 -0700285 }
286 file_len += count;
287 last_bb = bb;
288 }
289
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100290move:
Colin Crossbdc6d392012-05-02 15:18:22 -0700291 backed_block_list_move(from->backed_block_list,
292 to->backed_block_list, start, last_bb);
293
Jeremy Compostella9f0d6bd2015-02-22 10:47:16 +0100294out:
Colin Crossb43828b2012-06-08 16:55:35 -0700295 output_file_close(out_counter);
Colin Crossbdc6d392012-05-02 15:18:22 -0700296
297 return bb;
298}
299
300int sparse_file_resparse(struct sparse_file *in_s, unsigned int max_len,
301 struct sparse_file **out_s, int out_s_count)
302{
303 struct backed_block *bb;
Colin Crossbdc6d392012-05-02 15:18:22 -0700304 struct sparse_file *s;
305 struct sparse_file *tmp;
306 int c = 0;
307
308 tmp = sparse_file_new(in_s->block_size, in_s->len);
309 if (!tmp) {
310 return -ENOMEM;
311 }
312
313 do {
314 s = sparse_file_new(in_s->block_size, in_s->len);
315
316 bb = move_chunks_up_to_len(in_s, s, max_len);
317
318 if (c < out_s_count) {
319 out_s[c] = s;
320 } else {
321 backed_block_list_move(s->backed_block_list, tmp->backed_block_list,
322 NULL, NULL);
323 sparse_file_destroy(s);
324 }
325 c++;
326 } while (bb);
327
328 backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list,
329 NULL, NULL);
330
331 sparse_file_destroy(tmp);
332
333 return c;
334}
335
Colin Crossa21930b2012-04-26 14:24:35 -0700336void sparse_file_verbose(struct sparse_file *s)
337{
338 s->verbose = true;
339}