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