Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | #define _FILE_OFFSET_BITS 64 |
| 18 | #define _LARGEFILE64_SOURCE 1 |
| 19 | |
| 20 | #include <fcntl.h> |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 22 | #include <limits.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 23 | #include <stdbool.h> |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 24 | #include <stddef.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <zlib.h> |
| 31 | |
| 32 | #include "output_file.h" |
| 33 | #include "sparse_format.h" |
| 34 | #include "sparse_crc32.h" |
| 35 | |
| 36 | #ifndef USE_MINGW |
| 37 | #include <sys/mman.h> |
| 38 | #define O_BINARY 0 |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 39 | #else |
| 40 | #define ftruncate64 ftruncate |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #if defined(__APPLE__) && defined(__MACH__) |
| 44 | #define lseek64 lseek |
| 45 | #define ftruncate64 ftruncate |
| 46 | #define mmap64 mmap |
| 47 | #define off64_t off_t |
| 48 | #endif |
| 49 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 50 | #define min(a, b) \ |
| 51 | ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) |
| 52 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 53 | #define SPARSE_HEADER_MAJOR_VER 1 |
| 54 | #define SPARSE_HEADER_MINOR_VER 0 |
| 55 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) |
| 56 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) |
| 57 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 58 | #define container_of(inner, outer_t, elem) \ |
| 59 | ((outer_t *)((char *)inner - offsetof(outer_t, elem))) |
| 60 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 61 | struct output_file_ops { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 62 | int (*open)(struct output_file *, int fd); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 63 | int (*skip)(struct output_file *, int64_t); |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 64 | int (*pad)(struct output_file *, int64_t); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 65 | int (*write)(struct output_file *, void *, int); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 66 | void (*close)(struct output_file *); |
| 67 | }; |
| 68 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 69 | struct sparse_file_ops { |
| 70 | int (*write_data_chunk)(struct output_file *out, unsigned int len, |
| 71 | void *data); |
| 72 | int (*write_fill_chunk)(struct output_file *out, unsigned int len, |
| 73 | uint32_t fill_val); |
| 74 | int (*write_skip_chunk)(struct output_file *out, int64_t len); |
| 75 | int (*write_end_chunk)(struct output_file *out); |
| 76 | }; |
| 77 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 78 | struct output_file { |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 79 | int64_t cur_out_ptr; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 80 | unsigned int chunk_cnt; |
| 81 | uint32_t crc32; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 82 | struct output_file_ops *ops; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 83 | struct sparse_file_ops *sparse_ops; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 84 | int use_crc; |
| 85 | unsigned int block_size; |
| 86 | int64_t len; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 87 | char *zero_buf; |
| 88 | uint32_t *fill_buf; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 89 | char *buf; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 92 | struct output_file_gz { |
| 93 | struct output_file out; |
| 94 | gzFile gz_fd; |
| 95 | }; |
| 96 | |
| 97 | #define to_output_file_gz(_o) \ |
| 98 | container_of((_o), struct output_file_gz, out) |
| 99 | |
| 100 | struct output_file_normal { |
| 101 | struct output_file out; |
| 102 | int fd; |
| 103 | }; |
| 104 | |
| 105 | #define to_output_file_normal(_o) \ |
| 106 | container_of((_o), struct output_file_normal, out) |
| 107 | |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 108 | struct output_file_callback { |
| 109 | struct output_file out; |
| 110 | void *priv; |
| 111 | int (*write)(void *priv, const void *buf, int len); |
| 112 | }; |
| 113 | |
| 114 | #define to_output_file_callback(_o) \ |
| 115 | container_of((_o), struct output_file_callback, out) |
| 116 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 117 | static int file_open(struct output_file *out, int fd) |
| 118 | { |
| 119 | struct output_file_normal *outn = to_output_file_normal(out); |
| 120 | |
| 121 | outn->fd = fd; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 125 | static int file_skip(struct output_file *out, int64_t cnt) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 126 | { |
| 127 | off64_t ret; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 128 | struct output_file_normal *outn = to_output_file_normal(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 129 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 130 | ret = lseek64(outn->fd, cnt, SEEK_CUR); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 131 | if (ret < 0) { |
| 132 | error_errno("lseek64"); |
| 133 | return -1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 138 | static int file_pad(struct output_file *out, int64_t len) |
| 139 | { |
| 140 | int ret; |
| 141 | struct output_file_normal *outn = to_output_file_normal(out); |
| 142 | |
| 143 | ret = ftruncate64(outn->fd, len); |
| 144 | if (ret < 0) { |
| 145 | return -errno; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 151 | static int file_write(struct output_file *out, void *data, int len) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 152 | { |
| 153 | int ret; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 154 | struct output_file_normal *outn = to_output_file_normal(out); |
| 155 | |
| 156 | ret = write(outn->fd, data, len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 157 | if (ret < 0) { |
| 158 | error_errno("write"); |
| 159 | return -1; |
| 160 | } else if (ret < len) { |
| 161 | error("incomplete write"); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static void file_close(struct output_file *out) |
| 169 | { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 170 | struct output_file_normal *outn = to_output_file_normal(out); |
| 171 | |
| 172 | free(outn); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 175 | static struct output_file_ops file_ops = { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 176 | .open = file_open, |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 177 | .skip = file_skip, |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 178 | .pad = file_pad, |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 179 | .write = file_write, |
| 180 | .close = file_close, |
| 181 | }; |
| 182 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 183 | static int gz_file_open(struct output_file *out, int fd) |
| 184 | { |
| 185 | struct output_file_gz *outgz = to_output_file_gz(out); |
| 186 | |
| 187 | outgz->gz_fd = gzdopen(fd, "wb9"); |
| 188 | if (!outgz->gz_fd) { |
| 189 | error_errno("gzopen"); |
| 190 | return -errno; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 197 | static int gz_file_skip(struct output_file *out, int64_t cnt) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 198 | { |
| 199 | off64_t ret; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 200 | struct output_file_gz *outgz = to_output_file_gz(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 201 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 202 | ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 203 | if (ret < 0) { |
| 204 | error_errno("gzseek"); |
| 205 | return -1; |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 210 | static int gz_file_pad(struct output_file *out, int64_t len) |
| 211 | { |
| 212 | off64_t ret; |
| 213 | struct output_file_gz *outgz = to_output_file_gz(out); |
| 214 | |
| 215 | ret = gztell(outgz->gz_fd); |
| 216 | if (ret < 0) { |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | if (ret >= len) { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET); |
| 225 | if (ret < 0) { |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | gzwrite(outgz->gz_fd, "", 1); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 234 | static int gz_file_write(struct output_file *out, void *data, int len) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 235 | { |
| 236 | int ret; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 237 | struct output_file_gz *outgz = to_output_file_gz(out); |
| 238 | |
| 239 | ret = gzwrite(outgz->gz_fd, data, len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 240 | if (ret < 0) { |
| 241 | error_errno("gzwrite"); |
| 242 | return -1; |
| 243 | } else if (ret < len) { |
| 244 | error("incomplete gzwrite"); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void gz_file_close(struct output_file *out) |
| 252 | { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 253 | struct output_file_gz *outgz = to_output_file_gz(out); |
| 254 | |
| 255 | gzclose(outgz->gz_fd); |
| 256 | free(outgz); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static struct output_file_ops gz_file_ops = { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 260 | .open = gz_file_open, |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 261 | .skip = gz_file_skip, |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 262 | .pad = gz_file_pad, |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 263 | .write = gz_file_write, |
| 264 | .close = gz_file_close, |
| 265 | }; |
| 266 | |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 267 | static int callback_file_open(struct output_file *out, int fd) |
| 268 | { |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int callback_file_skip(struct output_file *out, int64_t off) |
| 273 | { |
| 274 | struct output_file_callback *outc = to_output_file_callback(out); |
| 275 | int to_write; |
| 276 | int ret; |
| 277 | |
| 278 | while (off > 0) { |
| 279 | to_write = min(off, (int64_t)INT_MAX); |
| 280 | ret = outc->write(outc->priv, NULL, to_write); |
| 281 | if (ret < 0) { |
| 282 | return ret; |
| 283 | } |
| 284 | off -= to_write; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int callback_file_pad(struct output_file *out, int64_t len) |
| 291 | { |
| 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | static int callback_file_write(struct output_file *out, void *data, int len) |
| 296 | { |
| 297 | int ret; |
| 298 | struct output_file_callback *outc = to_output_file_callback(out); |
| 299 | |
| 300 | return outc->write(outc->priv, data, len); |
| 301 | } |
| 302 | |
| 303 | static void callback_file_close(struct output_file *out) |
| 304 | { |
| 305 | struct output_file_callback *outc = to_output_file_callback(out); |
| 306 | |
| 307 | free(outc); |
| 308 | } |
| 309 | |
| 310 | static struct output_file_ops callback_file_ops = { |
| 311 | .open = callback_file_open, |
| 312 | .skip = callback_file_skip, |
| 313 | .pad = callback_file_pad, |
| 314 | .write = callback_file_write, |
| 315 | .close = callback_file_close, |
| 316 | }; |
| 317 | |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 318 | int read_all(int fd, void *buf, size_t len) |
| 319 | { |
| 320 | size_t total = 0; |
| 321 | int ret; |
| 322 | char *ptr = buf; |
| 323 | |
| 324 | while (total < len) { |
| 325 | ret = read(fd, ptr, len - total); |
| 326 | |
| 327 | if (ret < 0) |
| 328 | return -errno; |
| 329 | |
| 330 | if (ret == 0) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | ptr += ret; |
| 334 | total += ret; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 340 | static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 341 | { |
| 342 | chunk_header_t chunk_header; |
| 343 | int ret, chunk; |
| 344 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 345 | if (skip_len % out->block_size) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 346 | error("don't care size %"PRIi64" is not a multiple of the block size %u", |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 347 | skip_len, out->block_size); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | /* We are skipping data, so emit a don't care chunk. */ |
| 352 | chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE; |
| 353 | chunk_header.reserved1 = 0; |
| 354 | chunk_header.chunk_sz = skip_len / out->block_size; |
| 355 | chunk_header.total_sz = CHUNK_HEADER_LEN; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 356 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 357 | if (ret < 0) |
| 358 | return -1; |
| 359 | |
| 360 | out->cur_out_ptr += skip_len; |
| 361 | out->chunk_cnt++; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 366 | static int write_sparse_fill_chunk(struct output_file *out, unsigned int len, |
| 367 | uint32_t fill_val) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 368 | { |
| 369 | chunk_header_t chunk_header; |
| 370 | int rnd_up_len, zero_len, count; |
| 371 | int ret; |
| 372 | unsigned int i; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 373 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 374 | /* Round up the fill length to a multiple of the block size */ |
| 375 | rnd_up_len = ALIGN(len, out->block_size); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 376 | |
| 377 | /* Finally we can safely emit a chunk of data */ |
| 378 | chunk_header.chunk_type = CHUNK_TYPE_FILL; |
| 379 | chunk_header.reserved1 = 0; |
| 380 | chunk_header.chunk_sz = rnd_up_len / out->block_size; |
| 381 | chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 382 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 383 | |
| 384 | if (ret < 0) |
| 385 | return -1; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 386 | ret = out->ops->write(out, &fill_val, sizeof(fill_val)); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 387 | if (ret < 0) |
| 388 | return -1; |
| 389 | |
| 390 | if (out->use_crc) { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 391 | count = out->block_size / sizeof(uint32_t); |
| 392 | while (count--) |
| 393 | out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t)); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | out->cur_out_ptr += rnd_up_len; |
| 397 | out->chunk_cnt++; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 402 | static int write_sparse_data_chunk(struct output_file *out, unsigned int len, |
| 403 | void *data) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 404 | { |
| 405 | chunk_header_t chunk_header; |
| 406 | int rnd_up_len, zero_len; |
| 407 | int ret; |
| 408 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 409 | /* Round up the data length to a multiple of the block size */ |
| 410 | rnd_up_len = ALIGN(len, out->block_size); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 411 | zero_len = rnd_up_len - len; |
| 412 | |
| 413 | /* Finally we can safely emit a chunk of data */ |
| 414 | chunk_header.chunk_type = CHUNK_TYPE_RAW; |
| 415 | chunk_header.reserved1 = 0; |
| 416 | chunk_header.chunk_sz = rnd_up_len / out->block_size; |
| 417 | chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 418 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 419 | |
| 420 | if (ret < 0) |
| 421 | return -1; |
| 422 | ret = out->ops->write(out, data, len); |
| 423 | if (ret < 0) |
| 424 | return -1; |
| 425 | if (zero_len) { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 426 | ret = out->ops->write(out, out->zero_buf, zero_len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 427 | if (ret < 0) |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | if (out->use_crc) { |
| 432 | out->crc32 = sparse_crc32(out->crc32, data, len); |
| 433 | if (zero_len) |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 434 | out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | out->cur_out_ptr += rnd_up_len; |
| 438 | out->chunk_cnt++; |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 443 | int write_sparse_end_chunk(struct output_file *out) |
| 444 | { |
| 445 | chunk_header_t chunk_header; |
| 446 | int ret; |
| 447 | |
| 448 | if (out->use_crc) { |
| 449 | chunk_header.chunk_type = CHUNK_TYPE_CRC32; |
| 450 | chunk_header.reserved1 = 0; |
| 451 | chunk_header.chunk_sz = 0; |
| 452 | chunk_header.total_sz = CHUNK_HEADER_LEN + 4; |
| 453 | |
| 454 | ret = out->ops->write(out, &chunk_header, sizeof(chunk_header)); |
| 455 | if (ret < 0) { |
| 456 | return ret; |
| 457 | } |
| 458 | out->ops->write(out, &out->crc32, 4); |
| 459 | if (ret < 0) { |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | out->chunk_cnt++; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static struct sparse_file_ops sparse_file_ops = { |
| 470 | .write_data_chunk = write_sparse_data_chunk, |
| 471 | .write_fill_chunk = write_sparse_fill_chunk, |
| 472 | .write_skip_chunk = write_sparse_skip_chunk, |
| 473 | .write_end_chunk = write_sparse_end_chunk, |
| 474 | }; |
| 475 | |
| 476 | static int write_normal_data_chunk(struct output_file *out, unsigned int len, |
| 477 | void *data) |
| 478 | { |
| 479 | int ret; |
| 480 | unsigned int rnd_up_len = ALIGN(len, out->block_size); |
| 481 | |
| 482 | ret = out->ops->write(out, data, len); |
| 483 | if (ret < 0) { |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | if (rnd_up_len > len) { |
| 488 | ret = out->ops->skip(out, rnd_up_len - len); |
| 489 | } |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static int write_normal_fill_chunk(struct output_file *out, unsigned int len, |
| 495 | uint32_t fill_val) |
| 496 | { |
| 497 | int ret; |
| 498 | unsigned int i; |
| 499 | unsigned int write_len; |
| 500 | |
| 501 | /* Initialize fill_buf with the fill_val */ |
| 502 | for (i = 0; i < out->block_size / sizeof(uint32_t); i++) { |
| 503 | out->fill_buf[i] = fill_val; |
| 504 | } |
| 505 | |
| 506 | while (len) { |
| 507 | write_len = min(len, out->block_size); |
| 508 | ret = out->ops->write(out, out->fill_buf, write_len); |
| 509 | if (ret < 0) { |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | len -= write_len; |
| 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int write_normal_skip_chunk(struct output_file *out, int64_t len) |
| 520 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 521 | return out->ops->skip(out, len); |
| 522 | } |
| 523 | |
| 524 | int write_normal_end_chunk(struct output_file *out) |
| 525 | { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 526 | return out->ops->pad(out, out->len); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static struct sparse_file_ops normal_file_ops = { |
| 530 | .write_data_chunk = write_normal_data_chunk, |
| 531 | .write_fill_chunk = write_normal_fill_chunk, |
| 532 | .write_skip_chunk = write_normal_skip_chunk, |
| 533 | .write_end_chunk = write_normal_end_chunk, |
| 534 | }; |
| 535 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 536 | void output_file_close(struct output_file *out) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 537 | { |
| 538 | int ret; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 539 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 540 | out->sparse_ops->write_end_chunk(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 541 | out->ops->close(out); |
| 542 | } |
| 543 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 544 | static int output_file_init(struct output_file *out, int block_size, |
| 545 | int64_t len, bool sparse, int chunks, bool crc) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 546 | { |
| 547 | int ret; |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 548 | |
| 549 | out->len = len; |
| 550 | out->block_size = block_size; |
| 551 | out->cur_out_ptr = 0ll; |
| 552 | out->chunk_cnt = 0; |
| 553 | out->crc32 = 0; |
| 554 | out->use_crc = crc; |
| 555 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 556 | out->zero_buf = calloc(block_size, 1); |
| 557 | if (!out->zero_buf) { |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 558 | error_errno("malloc zero_buf"); |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 559 | return -ENOMEM; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 560 | } |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 561 | |
| 562 | out->fill_buf = calloc(block_size, 1); |
| 563 | if (!out->fill_buf) { |
| 564 | error_errno("malloc fill_buf"); |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 565 | ret = -ENOMEM; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 566 | goto err_fill_buf; |
| 567 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 568 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 569 | if (sparse) { |
| 570 | out->sparse_ops = &sparse_file_ops; |
| 571 | } else { |
| 572 | out->sparse_ops = &normal_file_ops; |
| 573 | } |
| 574 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 575 | if (sparse) { |
| 576 | sparse_header_t sparse_header = { |
| 577 | .magic = SPARSE_HEADER_MAGIC, |
| 578 | .major_version = SPARSE_HEADER_MAJOR_VER, |
| 579 | .minor_version = SPARSE_HEADER_MINOR_VER, |
| 580 | .file_hdr_sz = SPARSE_HEADER_LEN, |
| 581 | .chunk_hdr_sz = CHUNK_HEADER_LEN, |
| 582 | .blk_sz = out->block_size, |
| 583 | .total_blks = out->len / out->block_size, |
| 584 | .total_chunks = chunks, |
| 585 | .image_checksum = 0 |
| 586 | }; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 587 | |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 588 | if (out->use_crc) { |
| 589 | sparse_header.total_chunks++; |
| 590 | } |
| 591 | |
| 592 | ret = out->ops->write(out, &sparse_header, sizeof(sparse_header)); |
| 593 | if (ret < 0) { |
| 594 | goto err_write; |
| 595 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 598 | return 0; |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 599 | |
| 600 | err_write: |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 601 | free(out->fill_buf); |
| 602 | err_fill_buf: |
| 603 | free(out->zero_buf); |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 604 | return ret; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 607 | static struct output_file *output_file_new_gz(void) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 608 | { |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 609 | struct output_file_gz *outgz = calloc(1, sizeof(struct output_file_gz)); |
| 610 | if (!outgz) { |
| 611 | error_errno("malloc struct outgz"); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 612 | return NULL; |
| 613 | } |
| 614 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 615 | outgz->out.ops = &gz_file_ops; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 616 | |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 617 | return &outgz->out; |
| 618 | } |
| 619 | |
| 620 | static struct output_file *output_file_new_normal(void) |
| 621 | { |
| 622 | struct output_file_normal *outn = calloc(1, sizeof(struct output_file_normal)); |
| 623 | if (!outn) { |
| 624 | error_errno("malloc struct outn"); |
| 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | outn->out.ops = &file_ops; |
| 629 | |
| 630 | return &outn->out; |
| 631 | } |
| 632 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 633 | struct output_file *output_file_open_callback(int (*write)(void *, const void *, int), |
Colin Cross | 1e17b31 | 2012-05-21 16:35:45 -0700 | [diff] [blame] | 634 | void *priv, unsigned int block_size, int64_t len, int gz, int sparse, |
| 635 | int chunks, int crc) |
| 636 | { |
| 637 | int ret; |
| 638 | struct output_file_callback *outc; |
| 639 | |
| 640 | outc = calloc(1, sizeof(struct output_file_callback)); |
| 641 | if (!outc) { |
| 642 | error_errno("malloc struct outc"); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | outc->out.ops = &callback_file_ops; |
| 647 | outc->priv = priv; |
| 648 | outc->write = write; |
| 649 | |
| 650 | ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc); |
| 651 | if (ret < 0) { |
| 652 | free(outc); |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | return &outc->out; |
| 657 | } |
| 658 | |
Colin Cross | b43828b | 2012-06-08 16:55:35 -0700 | [diff] [blame] | 659 | struct output_file *output_file_open_fd(int fd, unsigned int block_size, int64_t len, |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 660 | int gz, int sparse, int chunks, int crc) |
| 661 | { |
| 662 | int ret; |
| 663 | struct output_file *out; |
| 664 | |
| 665 | if (gz) { |
| 666 | out = output_file_new_gz(); |
| 667 | } else { |
| 668 | out = output_file_new_normal(); |
| 669 | } |
Hong-Mei Li | 83a6d36 | 2013-04-01 11:22:50 +0800 | [diff] [blame] | 670 | if (!out) { |
| 671 | return NULL; |
| 672 | } |
Colin Cross | b4cd267 | 2012-05-18 14:49:50 -0700 | [diff] [blame] | 673 | |
| 674 | out->ops->open(out, fd); |
| 675 | |
| 676 | ret = output_file_init(out, block_size, len, sparse, chunks, crc); |
| 677 | if (ret < 0) { |
| 678 | free(out); |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | return out; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 685 | /* Write a contiguous region of data blocks from a memory buffer */ |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 686 | int write_data_chunk(struct output_file *out, unsigned int len, void *data) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 687 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 688 | return out->sparse_ops->write_data_chunk(out, len, data); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* Write a contiguous region of data blocks with a fill value */ |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 692 | int write_fill_chunk(struct output_file *out, unsigned int len, |
| 693 | uint32_t fill_val) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 694 | { |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 695 | return out->sparse_ops->write_fill_chunk(out, len, fill_val); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 698 | int write_fd_chunk(struct output_file *out, unsigned int len, |
| 699 | int fd, int64_t offset) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 700 | { |
| 701 | int ret; |
| 702 | int64_t aligned_offset; |
| 703 | int aligned_diff; |
| 704 | int buffer_size; |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 705 | char *ptr; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 706 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 707 | aligned_offset = offset & ~(4096 - 1); |
| 708 | aligned_diff = offset - aligned_offset; |
| 709 | buffer_size = len + aligned_diff; |
| 710 | |
| 711 | #ifndef USE_MINGW |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 712 | char *data = mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 713 | aligned_offset); |
| 714 | if (data == MAP_FAILED) { |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 715 | return -errno; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 716 | } |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 717 | ptr = data + aligned_diff; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 718 | #else |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 719 | off64_t pos; |
| 720 | char *data = malloc(len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 721 | if (!data) { |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 722 | return -errno; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 723 | } |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 724 | pos = lseek64(fd, offset, SEEK_SET); |
| 725 | if (pos < 0) { |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 726 | free(data); |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 727 | return -errno; |
| 728 | } |
| 729 | ret = read_all(fd, data, len); |
| 730 | if (ret < 0) { |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 731 | free(data); |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 732 | return ret; |
| 733 | } |
| 734 | ptr = data; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 735 | #endif |
| 736 | |
Colin Cross | 13a5606 | 2012-06-19 16:45:48 -0700 | [diff] [blame] | 737 | ret = out->sparse_ops->write_data_chunk(out, len, ptr); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 738 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 739 | #ifndef USE_MINGW |
| 740 | munmap(data, buffer_size); |
| 741 | #else |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 742 | free(data); |
| 743 | #endif |
Colin Cross | 9e1f17e | 2012-04-25 18:31:39 -0700 | [diff] [blame] | 744 | |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | /* Write a contiguous region of data blocks from a file */ |
| 749 | int write_file_chunk(struct output_file *out, unsigned int len, |
| 750 | const char *file, int64_t offset) |
| 751 | { |
| 752 | int ret; |
| 753 | |
| 754 | int file_fd = open(file, O_RDONLY | O_BINARY); |
| 755 | if (file_fd < 0) { |
| 756 | return -errno; |
| 757 | } |
| 758 | |
| 759 | ret = write_fd_chunk(out, len, file_fd, offset); |
| 760 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 761 | close(file_fd); |
Colin Cross | b55dcee | 2012-04-24 23:07:49 -0700 | [diff] [blame] | 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | int write_skip_chunk(struct output_file *out, int64_t len) |
| 767 | { |
| 768 | return out->sparse_ops->write_skip_chunk(out, len); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 769 | } |