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