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 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 25 | #include "backed_block.h" |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 26 | #include "output_file.h" |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 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 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 30 | struct 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 Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 33 | return nullptr; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 34 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 35 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 36 | s->backed_block_list = backed_block_list_new(block_size); |
| 37 | if (!s->backed_block_list) { |
| 38 | free(s); |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 39 | return nullptr; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 40 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 41 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 42 | s->block_size = block_size; |
| 43 | s->len = len; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 44 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 45 | return s; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 48 | void sparse_file_destroy(struct sparse_file* s) { |
| 49 | backed_block_list_destroy(s->backed_block_list); |
| 50 | free(s); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Hyeongseok Kim | e8d02c5 | 2020-08-10 12:11:57 +0900 | [diff] [blame] | 53 | int sparse_file_add_data(struct sparse_file* s, void* data, uint64_t len, unsigned int block) { |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 54 | return backed_block_add_data(s->backed_block_list, data, len, block); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Hyeongseok Kim | e8d02c5 | 2020-08-10 12:11:57 +0900 | [diff] [blame] | 57 | int sparse_file_add_fill(struct sparse_file* s, uint32_t fill_val, uint64_t len, |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 58 | unsigned int block) { |
| 59 | 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] | 60 | } |
| 61 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 62 | int sparse_file_add_file(struct sparse_file* s, const char* filename, int64_t file_offset, |
Hyeongseok Kim | e8d02c5 | 2020-08-10 12:11:57 +0900 | [diff] [blame] | 63 | uint64_t len, unsigned int block) { |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 64 | return backed_block_add_file(s->backed_block_list, filename, file_offset, len, block); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Hyeongseok Kim | e8d02c5 | 2020-08-10 12:11:57 +0900 | [diff] [blame] | 67 | int sparse_file_add_fd(struct sparse_file* s, int fd, int64_t file_offset, uint64_t len, |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 68 | unsigned int block) { |
| 69 | return backed_block_add_fd(s->backed_block_list, fd, file_offset, len, block); |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 70 | } |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 71 | unsigned 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 75 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 76 | 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 87 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 88 | return chunks; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 91 | static int sparse_file_write_block(struct output_file* out, struct backed_block* bb) { |
| 92 | int ret = -EINVAL; |
Jeremy Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 93 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 94 | 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 Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 110 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 111 | return ret; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 114 | static 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 119 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 120 | 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 129 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 130 | 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 Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 135 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 136 | return 0; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Woody Lin | a386482 | 2020-05-13 20:50:54 +0800 | [diff] [blame] | 139 | /* |
| 140 | * This is a workaround for 32-bit Windows: Limit the block size to 64 MB before |
| 141 | * fastboot executable binary for windows 64-bit is released (b/156057250). |
| 142 | */ |
| 143 | #define MAX_BACKED_BLOCK_SIZE ((unsigned int) (64UL << 20)) |
| 144 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 145 | int sparse_file_write(struct sparse_file* s, int fd, bool gz, bool sparse, bool crc) { |
Woody Lin | a386482 | 2020-05-13 20:50:54 +0800 | [diff] [blame] | 146 | struct backed_block* bb; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 147 | int ret; |
| 148 | int chunks; |
| 149 | struct output_file* out; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 150 | |
Woody Lin | a386482 | 2020-05-13 20:50:54 +0800 | [diff] [blame] | 151 | for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) { |
| 152 | ret = backed_block_split(s->backed_block_list, bb, MAX_BACKED_BLOCK_SIZE); |
| 153 | if (ret) return ret; |
| 154 | } |
| 155 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 156 | chunks = sparse_count_chunks(s); |
| 157 | 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] | 158 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 159 | if (!out) return -ENOMEM; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 160 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 161 | ret = write_all_blocks(s, out); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 162 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 163 | output_file_close(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 164 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 165 | return ret; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 168 | int sparse_file_callback(struct sparse_file* s, bool sparse, bool crc, |
| 169 | int (*write)(void* priv, const void* data, size_t len), void* priv) { |
| 170 | int ret; |
| 171 | int chunks; |
| 172 | struct output_file* out; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 173 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 174 | chunks = sparse_count_chunks(s); |
Tobias Thierer | fca4a9c | 2018-07-26 03:13:45 +0000 | [diff] [blame] | 175 | out = output_file_open_callback(write, priv, s->block_size, s->len, false, sparse, chunks, crc); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 176 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 177 | if (!out) return -ENOMEM; |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 178 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 179 | ret = write_all_blocks(s, out); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 180 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 181 | output_file_close(out); |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 182 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 183 | return ret; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 184 | } |
Colin Cross | a21930b | 2012-04-26 14:24:35 -0700 | [diff] [blame] | 185 | |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 186 | struct chunk_data { |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 187 | void* priv; |
| 188 | unsigned int block; |
| 189 | unsigned int nr_blocks; |
| 190 | int (*write)(void* priv, const void* data, size_t len, unsigned int block, unsigned int nr_blocks); |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 193 | static int foreach_chunk_write(void* priv, const void* data, size_t len) { |
| 194 | struct chunk_data* chk = reinterpret_cast<chunk_data*>(priv); |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 195 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 196 | return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks); |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 199 | int sparse_file_foreach_chunk(struct sparse_file* s, bool sparse, bool crc, |
| 200 | int (*write)(void* priv, const void* data, size_t len, |
| 201 | unsigned int block, unsigned int nr_blocks), |
| 202 | void* priv) { |
Stephen Hines | 45b11d9 | 2019-05-02 12:54:09 -0700 | [diff] [blame] | 203 | int ret = 0; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 204 | int chunks; |
| 205 | struct chunk_data chk; |
| 206 | struct output_file* out; |
| 207 | struct backed_block* bb; |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 208 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 209 | chk.priv = priv; |
| 210 | chk.write = write; |
| 211 | chk.block = chk.nr_blocks = 0; |
| 212 | chunks = sparse_count_chunks(s); |
Tobias Thierer | fca4a9c | 2018-07-26 03:13:45 +0000 | [diff] [blame] | 213 | out = output_file_open_callback(foreach_chunk_write, &chk, s->block_size, s->len, false, sparse, |
| 214 | chunks, crc); |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 215 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 216 | if (!out) return -ENOMEM; |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 217 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 218 | for (bb = backed_block_iter_new(s->backed_block_list); bb; bb = backed_block_iter_next(bb)) { |
| 219 | chk.block = backed_block_block(bb); |
| 220 | chk.nr_blocks = (backed_block_len(bb) - 1) / s->block_size + 1; |
| 221 | ret = sparse_file_write_block(out, bb); |
| 222 | if (ret) return ret; |
| 223 | } |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 224 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 225 | output_file_close(out); |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 226 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 227 | return ret; |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 230 | static int out_counter_write(void* priv, const void* data __unused, size_t len) { |
| 231 | int64_t* count = reinterpret_cast<int64_t*>(priv); |
| 232 | *count += len; |
| 233 | return 0; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 236 | int64_t sparse_file_len(struct sparse_file* s, bool sparse, bool crc) { |
| 237 | int ret; |
| 238 | int chunks = sparse_count_chunks(s); |
| 239 | int64_t count = 0; |
| 240 | struct output_file* out; |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 241 | |
Tobias Thierer | fca4a9c | 2018-07-26 03:13:45 +0000 | [diff] [blame] | 242 | out = output_file_open_callback(out_counter_write, &count, s->block_size, s->len, false, sparse, |
| 243 | chunks, crc); |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 244 | if (!out) { |
| 245 | return -1; |
| 246 | } |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 247 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 248 | ret = write_all_blocks(s, out); |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 249 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 250 | output_file_close(out); |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 251 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 252 | if (ret < 0) { |
| 253 | return -1; |
| 254 | } |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 255 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 256 | return count; |
Colin Cross | 317a09e | 2012-05-24 17:15:43 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 259 | unsigned int sparse_file_block_size(struct sparse_file* s) { |
| 260 | return s->block_size; |
Adrien Schildknecht | a26a6bd | 2016-11-30 11:49:47 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 263 | static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, struct sparse_file* to, |
| 264 | unsigned int len) { |
| 265 | int64_t count = 0; |
| 266 | struct output_file* out_counter; |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 267 | struct backed_block* last_bb = nullptr; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 268 | struct backed_block* bb; |
| 269 | struct backed_block* start; |
| 270 | unsigned int last_block = 0; |
| 271 | int64_t file_len = 0; |
| 272 | int ret; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 273 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 274 | /* |
| 275 | * overhead is sparse file header, the potential end skip |
| 276 | * chunk and crc chunk. |
| 277 | */ |
| 278 | int overhead = sizeof(sparse_header_t) + 2 * sizeof(chunk_header_t) + sizeof(uint32_t); |
| 279 | len -= overhead; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 280 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 281 | start = backed_block_iter_new(from->backed_block_list); |
Tobias Thierer | fca4a9c | 2018-07-26 03:13:45 +0000 | [diff] [blame] | 282 | out_counter = output_file_open_callback(out_counter_write, &count, to->block_size, to->len, false, |
| 283 | true, 0, false); |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 284 | if (!out_counter) { |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 285 | return nullptr; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 286 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 287 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 288 | for (bb = start; bb; bb = backed_block_iter_next(bb)) { |
| 289 | count = 0; |
| 290 | if (backed_block_block(bb) > last_block) count += sizeof(chunk_header_t); |
| 291 | last_block = backed_block_block(bb) + DIV_ROUND_UP(backed_block_len(bb), to->block_size); |
Jeremy Compostella | cfd3a03 | 2015-04-03 14:31:19 +0200 | [diff] [blame] | 292 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 293 | /* will call out_counter_write to update count */ |
| 294 | ret = sparse_file_write_block(out_counter, bb); |
| 295 | if (ret) { |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 296 | bb = nullptr; |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 297 | goto out; |
| 298 | } |
| 299 | if (file_len + count > len) { |
| 300 | /* |
| 301 | * If the remaining available size is more than 1/8th of the |
| 302 | * requested size, split the chunk. Results in sparse files that |
| 303 | * are at least 7/8ths of the requested size |
| 304 | */ |
| 305 | file_len += sizeof(chunk_header_t); |
| 306 | if (!last_bb || (len - file_len > (len / 8))) { |
| 307 | backed_block_split(from->backed_block_list, bb, len - file_len); |
| 308 | last_bb = bb; |
| 309 | } |
| 310 | goto move; |
| 311 | } |
| 312 | file_len += count; |
| 313 | last_bb = bb; |
| 314 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 315 | |
Jeremy Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 316 | move: |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 317 | backed_block_list_move(from->backed_block_list, to->backed_block_list, start, last_bb); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 318 | |
Jeremy Compostella | 9f0d6bd | 2015-02-22 10:47:16 +0100 | [diff] [blame] | 319 | out: |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 320 | output_file_close(out_counter); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 321 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 322 | return bb; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 325 | int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct sparse_file** out_s, |
| 326 | int out_s_count) { |
| 327 | struct backed_block* bb; |
| 328 | struct sparse_file* s; |
| 329 | struct sparse_file* tmp; |
| 330 | int c = 0; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 331 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 332 | tmp = sparse_file_new(in_s->block_size, in_s->len); |
| 333 | if (!tmp) { |
| 334 | return -ENOMEM; |
| 335 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 336 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 337 | do { |
| 338 | s = sparse_file_new(in_s->block_size, in_s->len); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 339 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 340 | bb = move_chunks_up_to_len(in_s, s, max_len); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 341 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 342 | if (c < out_s_count) { |
| 343 | out_s[c] = s; |
| 344 | } else { |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 345 | backed_block_list_move(s->backed_block_list, tmp->backed_block_list, nullptr, nullptr); |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 346 | sparse_file_destroy(s); |
| 347 | } |
| 348 | c++; |
| 349 | } while (bb); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 350 | |
Yi Kong | 17ba95e | 2018-07-23 16:31:11 -0700 | [diff] [blame] | 351 | backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, nullptr, nullptr); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 352 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 353 | sparse_file_destroy(tmp); |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 354 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 355 | return c; |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 358 | void sparse_file_verbose(struct sparse_file* s) { |
| 359 | s->verbose = true; |
Colin Cross | a21930b | 2012-04-26 14:24:35 -0700 | [diff] [blame] | 360 | } |