Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 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 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 17 | #include <assert.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <sparse/sparse.h> |
| 21 | |
Mark Salyzyn | 031a748 | 2014-02-27 16:56:15 -0800 | [diff] [blame] | 22 | #include "defs.h" |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 23 | #include "sparse_file.h" |
| 24 | |
| 25 | #include "output_file.h" |
| 26 | #include "backed_block.h" |
| 27 | #include "sparse_defs.h" |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 28 | #include "sparse_format.h" |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 29 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 30 | struct 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 Cross | be8ddcb | 2012-04-25 21:02:16 -0700 | [diff] [blame] | 37 | s->backed_block_list = backed_block_list_new(block_size); |
Colin Cross | 411619e | 2012-04-24 18:51:42 -0700 | [diff] [blame] | 38 | if (!s->backed_block_list) { |
| 39 | free(s); |
| 40 | return NULL; |
| 41 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 42 | |
| 43 | s->block_size = block_size; |
| 44 | s->len = len; |
| 45 | |
| 46 | return s; |
| 47 | } |
| 48 | |
| 49 | void sparse_file_destroy(struct sparse_file *s) |
| 50 | { |
Colin Cross | 411619e | 2012-04-24 18:51:42 -0700 | [diff] [blame] | 51 | backed_block_list_destroy(s->backed_block_list); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 52 | free(s); |
| 53 | } |
| 54 | |
| 55 | int sparse_file_add_data(struct sparse_file *s, |
| 56 | void *data, unsigned int len, unsigned int block) |
| 57 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 58 | return backed_block_add_data(s->backed_block_list, data, len, block); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int sparse_file_add_fill(struct sparse_file *s, |
| 62 | uint32_t fill_val, unsigned int len, unsigned int block) |
| 63 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 64 | return backed_block_add_fill(s->backed_block_list, fill_val, len, block); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int 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 Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 71 | return backed_block_add_file(s->backed_block_list, filename, file_offset, |
| 72 | len, block); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 75 | int 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 Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 81 | unsigned int sparse_count_chunks(struct sparse_file *s) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 82 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 83 | struct backed_block *bb; |
| 84 | unsigned int last_block = 0; |
| 85 | unsigned int chunks = 0; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 86 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 87 | 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 100 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 101 | return chunks; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 104 | static 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 | |
| 126 | static int write_all_blocks(struct sparse_file *s, struct output_file *out) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 127 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 128 | struct backed_block *bb; |
| 129 | unsigned int last_block = 0; |
| 130 | int64_t pad; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 131 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 132 | 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 Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 138 | sparse_file_write_block(out, bb); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 139 | last_block = backed_block_block(bb) + |
| 140 | DIV_ROUND_UP(backed_block_len(bb), s->block_size); |
| 141 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 142 | |
Colin Cross | f1ec8ac | 2012-07-23 17:09:42 -0700 | [diff] [blame] | 143 | pad = s->len - (int64_t)last_block * s->block_size; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 144 | assert(pad >= 0); |
| 145 | if (pad > 0) { |
| 146 | write_skip_chunk(out, pad); |
| 147 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 148 | |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int 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 Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 160 | out = output_file_open_fd(fd, s->block_size, s->len, gz, sparse, chunks, crc); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 161 | |
| 162 | if (!out) |
| 163 | return -ENOMEM; |
| 164 | |
| 165 | ret = write_all_blocks(s, out); |
| 166 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 167 | output_file_close(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 168 | |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | int 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 Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 180 | out = output_file_open_callback(write, priv, s->block_size, s->len, false, |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 181 | sparse, chunks, crc); |
| 182 | |
| 183 | if (!out) |
| 184 | return -ENOMEM; |
| 185 | |
| 186 | ret = write_all_blocks(s, out); |
| 187 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 188 | output_file_close(out); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 189 | |
| 190 | return ret; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 191 | } |
Colin Cross | a21930b | 2012-04-26 14:24:35 -0700 | [diff] [blame] | 192 | |
Mark Salyzyn | 031a748 | 2014-02-27 16:56:15 -0800 | [diff] [blame] | 193 | static int out_counter_write(void *priv, const void *data __unused, int len) |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 194 | { |
| 195 | int64_t *count = priv; |
| 196 | *count += len; |
| 197 | return 0; |
| 198 | } |
| 199 | |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 200 | int64_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 Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 207 | out = output_file_open_callback(out_counter_write, &count, |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 208 | 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 Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 215 | output_file_close(out); |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 216 | |
| 217 | if (ret < 0) { |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | return count; |
| 222 | } |
| 223 | |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 224 | static 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 Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 243 | out_counter = output_file_open_callback(out_counter_write, &count, |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 244 | 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 | |
| 269 | out: |
| 270 | backed_block_list_move(from->backed_block_list, |
| 271 | to->backed_block_list, start, last_bb); |
| 272 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 273 | output_file_close(out_counter); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 274 | |
| 275 | return bb; |
| 276 | } |
| 277 | |
| 278 | int 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 Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 282 | 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 Cross | a21930b | 2012-04-26 14:24:35 -0700 | [diff] [blame] | 314 | void sparse_file_verbose(struct sparse_file *s) |
| 315 | { |
| 316 | s->verbose = true; |
| 317 | } |