blob: 08312e4d3c0e5dc955e24a49f0fc9cf3b48e94a8 [file] [log] [blame]
Colin Cross28fa5bc2012-05-20 13:28:05 -07001/*
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
cfig946da7c2019-08-15 10:07:07 +080020#include <algorithm>
Colin Cross28fa5bc2012-05-20 13:28:05 -070021#include <fcntl.h>
Elliott Hughesccecf142014-01-16 10:53:11 -080022#include <inttypes.h>
Colin Cross1e17b312012-05-21 16:35:45 -070023#include <limits.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070024#include <stdbool.h>
Colin Crossb4cd2672012-05-18 14:49:50 -070025#include <stddef.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070026#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 Salyzyn031a7482014-02-27 16:56:15 -080033#include "defs.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070034#include "output_file.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070035#include "sparse_crc32.h"
Mark Salyzyn8116c8c2014-05-01 09:15:02 -070036#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070037
Elliott Hughesc44f50c2020-05-14 16:52:18 -070038#include <android-base/mapped_file.h>
39
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070040#ifndef _WIN32
Colin Cross28fa5bc2012-05-20 13:28:05 -070041#define O_BINARY 0
Colin Crossb4cd2672012-05-18 14:49:50 -070042#else
43#define ftruncate64 ftruncate
Colin Cross28fa5bc2012-05-20 13:28:05 -070044#endif
45
46#if defined(__APPLE__) && defined(__MACH__)
47#define lseek64 lseek
48#define ftruncate64 ftruncate
Colin Cross28fa5bc2012-05-20 13:28:05 -070049#define off64_t off_t
50#endif
51
Colin Cross28fa5bc2012-05-20 13:28:05 -070052#define SPARSE_HEADER_MAJOR_VER 1
53#define SPARSE_HEADER_MINOR_VER 0
Jerry Zhang7b444f02018-06-12 16:42:09 -070054#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
Colin Cross28fa5bc2012-05-20 13:28:05 -070055#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
56
Keith Moka3b72062021-12-31 05:09:32 +000057#define FILL_ZERO_BUFSIZE (2 * 1024 * 1024)
58
Jerry Zhang7b444f02018-06-12 16:42:09 -070059#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
Colin Crossb4cd2672012-05-18 14:49:50 -070060
David Andersonf06debc2023-03-16 21:23:53 -070061static constexpr size_t kMaxMmapSize = 256 * 1024 * 1024;
62
Colin Cross28fa5bc2012-05-20 13:28:05 -070063struct output_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070064 int (*open)(struct output_file*, int fd);
65 int (*skip)(struct output_file*, int64_t);
66 int (*pad)(struct output_file*, int64_t);
67 int (*write)(struct output_file*, void*, size_t);
68 void (*close)(struct output_file*);
Colin Cross28fa5bc2012-05-20 13:28:05 -070069};
70
Colin Crossb55dcee2012-04-24 23:07:49 -070071struct sparse_file_ops {
Hyeongseok Kime8d02c52020-08-10 12:11:57 +090072 int (*write_data_chunk)(struct output_file* out, uint64_t len, void* data);
73 int (*write_fill_chunk)(struct output_file* out, uint64_t len, uint32_t fill_val);
74 int (*write_skip_chunk)(struct output_file* out, uint64_t len);
Jerry Zhang7b444f02018-06-12 16:42:09 -070075 int (*write_end_chunk)(struct output_file* out);
David Andersonf06debc2023-03-16 21:23:53 -070076 int (*write_fd_chunk)(struct output_file* out, uint64_t len, int fd, int64_t offset);
Colin Crossb55dcee2012-04-24 23:07:49 -070077};
78
Colin Cross28fa5bc2012-05-20 13:28:05 -070079struct output_file {
Jerry Zhang7b444f02018-06-12 16:42:09 -070080 int64_t cur_out_ptr;
81 unsigned int chunk_cnt;
82 uint32_t crc32;
83 struct output_file_ops* ops;
84 struct sparse_file_ops* sparse_ops;
85 int use_crc;
86 unsigned int block_size;
87 int64_t len;
88 char* zero_buf;
89 uint32_t* fill_buf;
90 char* buf;
Colin Cross28fa5bc2012-05-20 13:28:05 -070091};
92
Colin Crossb4cd2672012-05-18 14:49:50 -070093struct output_file_gz {
Jerry Zhang7b444f02018-06-12 16:42:09 -070094 struct output_file out;
95 gzFile gz_fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070096};
97
Jerry Zhang7b444f02018-06-12 16:42:09 -070098#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
Colin Crossb4cd2672012-05-18 14:49:50 -070099
100struct output_file_normal {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700101 struct output_file out;
102 int fd;
Colin Crossb4cd2672012-05-18 14:49:50 -0700103};
104
Jerry Zhang7b444f02018-06-12 16:42:09 -0700105#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700106
Colin Cross1e17b312012-05-21 16:35:45 -0700107struct output_file_callback {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700108 struct output_file out;
109 void* priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000110 int (*write)(void* priv, const void* buf, size_t len);
Colin Cross1e17b312012-05-21 16:35:45 -0700111};
112
Jerry Zhang7b444f02018-06-12 16:42:09 -0700113#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
Colin Cross1e17b312012-05-21 16:35:45 -0700114
Jerry Zhang7b444f02018-06-12 16:42:09 -0700115static int file_open(struct output_file* out, int fd) {
116 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700117
Jerry Zhang7b444f02018-06-12 16:42:09 -0700118 outn->fd = fd;
119 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700120}
121
Jerry Zhang7b444f02018-06-12 16:42:09 -0700122static int file_skip(struct output_file* out, int64_t cnt) {
123 off64_t ret;
124 struct output_file_normal* outn = to_output_file_normal(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700125
Jerry Zhang7b444f02018-06-12 16:42:09 -0700126 ret = lseek64(outn->fd, cnt, SEEK_CUR);
127 if (ret < 0) {
128 error_errno("lseek64");
129 return -1;
130 }
131 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700132}
133
Jerry Zhang7b444f02018-06-12 16:42:09 -0700134static int file_pad(struct output_file* out, int64_t len) {
135 int ret;
136 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700137
Jerry Zhang7b444f02018-06-12 16:42:09 -0700138 ret = ftruncate64(outn->fd, len);
139 if (ret < 0) {
140 return -errno;
141 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700142
Jerry Zhang7b444f02018-06-12 16:42:09 -0700143 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700144}
145
Jerry Zhang7b444f02018-06-12 16:42:09 -0700146static int file_write(struct output_file* out, void* data, size_t len) {
147 ssize_t ret;
148 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700149
Jerry Zhang7b444f02018-06-12 16:42:09 -0700150 while (len > 0) {
151 ret = write(outn->fd, data, len);
152 if (ret < 0) {
153 if (errno == EINTR) {
154 continue;
155 }
156 error_errno("write");
157 return -1;
158 }
Jeremy Compostellafca594c2016-09-30 14:54:25 +0200159
Jerry Zhang7b444f02018-06-12 16:42:09 -0700160 data = (char*)data + ret;
161 len -= ret;
162 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700163
Jerry Zhang7b444f02018-06-12 16:42:09 -0700164 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700165}
166
Jerry Zhang7b444f02018-06-12 16:42:09 -0700167static void file_close(struct output_file* out) {
168 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700169
Jerry Zhang7b444f02018-06-12 16:42:09 -0700170 free(outn);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700171}
172
Colin Cross28fa5bc2012-05-20 13:28:05 -0700173static struct output_file_ops file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700174 .open = file_open,
175 .skip = file_skip,
176 .pad = file_pad,
177 .write = file_write,
178 .close = file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700179};
180
Jerry Zhang7b444f02018-06-12 16:42:09 -0700181static int gz_file_open(struct output_file* out, int fd) {
182 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700183
Jerry Zhang7b444f02018-06-12 16:42:09 -0700184 outgz->gz_fd = gzdopen(fd, "wb9");
185 if (!outgz->gz_fd) {
186 error_errno("gzopen");
187 return -errno;
188 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700189
Jerry Zhang7b444f02018-06-12 16:42:09 -0700190 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700191}
192
Jerry Zhang7b444f02018-06-12 16:42:09 -0700193static int gz_file_skip(struct output_file* out, int64_t cnt) {
194 off64_t ret;
195 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700196
Jerry Zhang7b444f02018-06-12 16:42:09 -0700197 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
198 if (ret < 0) {
199 error_errno("gzseek");
200 return -1;
201 }
202 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700203}
204
Jerry Zhang7b444f02018-06-12 16:42:09 -0700205static int gz_file_pad(struct output_file* out, int64_t len) {
206 off64_t ret;
207 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700208
Jerry Zhang7b444f02018-06-12 16:42:09 -0700209 ret = gztell(outgz->gz_fd);
210 if (ret < 0) {
211 return -1;
212 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700213
Jerry Zhang7b444f02018-06-12 16:42:09 -0700214 if (ret >= len) {
215 return 0;
216 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700217
Jerry Zhang7b444f02018-06-12 16:42:09 -0700218 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
219 if (ret < 0) {
220 return -1;
221 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700222
Jerry Zhang7b444f02018-06-12 16:42:09 -0700223 gzwrite(outgz->gz_fd, "", 1);
Colin Crossb4cd2672012-05-18 14:49:50 -0700224
Jerry Zhang7b444f02018-06-12 16:42:09 -0700225 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700226}
227
Jerry Zhang7b444f02018-06-12 16:42:09 -0700228static int gz_file_write(struct output_file* out, void* data, size_t len) {
229 int ret;
230 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700231
Jerry Zhang7b444f02018-06-12 16:42:09 -0700232 while (len > 0) {
cfig946da7c2019-08-15 10:07:07 +0800233 ret = gzwrite(outgz->gz_fd, data, std::min<unsigned int>(len, (unsigned int)INT_MAX));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700234 if (ret == 0) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700235 error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700236 return -1;
237 }
238 len -= ret;
239 data = (char*)data + ret;
240 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700241
Jerry Zhang7b444f02018-06-12 16:42:09 -0700242 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700243}
244
Jerry Zhang7b444f02018-06-12 16:42:09 -0700245static void gz_file_close(struct output_file* out) {
246 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700247
Jerry Zhang7b444f02018-06-12 16:42:09 -0700248 gzclose(outgz->gz_fd);
249 free(outgz);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700250}
251
252static struct output_file_ops gz_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700253 .open = gz_file_open,
254 .skip = gz_file_skip,
255 .pad = gz_file_pad,
256 .write = gz_file_write,
257 .close = gz_file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700258};
259
Jerry Zhang7b444f02018-06-12 16:42:09 -0700260static int callback_file_open(struct output_file* out __unused, int fd __unused) {
261 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700262}
263
Jerry Zhang7b444f02018-06-12 16:42:09 -0700264static int callback_file_skip(struct output_file* out, int64_t off) {
265 struct output_file_callback* outc = to_output_file_callback(out);
266 int to_write;
267 int ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700268
Jerry Zhang7b444f02018-06-12 16:42:09 -0700269 while (off > 0) {
cfig946da7c2019-08-15 10:07:07 +0800270 to_write = std::min(off, (int64_t)INT_MAX);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000271 ret = outc->write(outc->priv, nullptr, to_write);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700272 if (ret < 0) {
273 return ret;
274 }
275 off -= to_write;
276 }
Colin Cross1e17b312012-05-21 16:35:45 -0700277
Jerry Zhang7b444f02018-06-12 16:42:09 -0700278 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700279}
280
Jerry Zhang7b444f02018-06-12 16:42:09 -0700281static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
282 return -1;
Colin Cross1e17b312012-05-21 16:35:45 -0700283}
284
Jerry Zhang7b444f02018-06-12 16:42:09 -0700285static int callback_file_write(struct output_file* out, void* data, size_t len) {
286 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700287
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000288 return outc->write(outc->priv, data, len);
Colin Cross1e17b312012-05-21 16:35:45 -0700289}
290
Jerry Zhang7b444f02018-06-12 16:42:09 -0700291static void callback_file_close(struct output_file* out) {
292 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700293
Jerry Zhang7b444f02018-06-12 16:42:09 -0700294 free(outc);
Colin Cross1e17b312012-05-21 16:35:45 -0700295}
296
297static struct output_file_ops callback_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700298 .open = callback_file_open,
299 .skip = callback_file_skip,
300 .pad = callback_file_pad,
301 .write = callback_file_write,
302 .close = callback_file_close,
Colin Cross1e17b312012-05-21 16:35:45 -0700303};
304
Jerry Zhang7b444f02018-06-12 16:42:09 -0700305int read_all(int fd, void* buf, size_t len) {
306 size_t total = 0;
307 int ret;
308 char* ptr = reinterpret_cast<char*>(buf);
Colin Cross13a56062012-06-19 16:45:48 -0700309
Jerry Zhang7b444f02018-06-12 16:42:09 -0700310 while (total < len) {
311 ret = read(fd, ptr, len - total);
Colin Cross13a56062012-06-19 16:45:48 -0700312
Jerry Zhang7b444f02018-06-12 16:42:09 -0700313 if (ret < 0) return -errno;
Colin Cross13a56062012-06-19 16:45:48 -0700314
Jerry Zhang7b444f02018-06-12 16:42:09 -0700315 if (ret == 0) return -EINVAL;
Colin Cross13a56062012-06-19 16:45:48 -0700316
Jerry Zhang7b444f02018-06-12 16:42:09 -0700317 ptr += ret;
318 total += ret;
319 }
Colin Cross13a56062012-06-19 16:45:48 -0700320
Jerry Zhang7b444f02018-06-12 16:42:09 -0700321 return 0;
Colin Cross13a56062012-06-19 16:45:48 -0700322}
323
David Andersonf06debc2023-03-16 21:23:53 -0700324template <typename T>
325static bool write_fd_chunk_range(int fd, int64_t offset, uint64_t len, T callback) {
326 uint64_t bytes_written = 0;
327 int64_t current_offset = offset;
328 while (bytes_written < len) {
329 size_t mmap_size = std::min(static_cast<uint64_t>(kMaxMmapSize), len - bytes_written);
330 auto m = android::base::MappedFile::FromFd(fd, current_offset, mmap_size, PROT_READ);
331 if (!m) {
332 error("failed to mmap region of length %zu", mmap_size);
333 return false;
334 }
335 if (!callback(m->data(), mmap_size)) {
336 return false;
337 }
338 bytes_written += mmap_size;
339 current_offset += mmap_size;
340 }
341 return true;
342}
343
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900344static int write_sparse_skip_chunk(struct output_file* out, uint64_t skip_len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700345 chunk_header_t chunk_header;
346 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700347
Jerry Zhang7b444f02018-06-12 16:42:09 -0700348 if (skip_len % out->block_size) {
349 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
350 out->block_size);
351 return -1;
352 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700353
Jerry Zhang7b444f02018-06-12 16:42:09 -0700354 /* We are skipping data, so emit a don't care chunk. */
355 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
356 chunk_header.reserved1 = 0;
357 chunk_header.chunk_sz = skip_len / out->block_size;
358 chunk_header.total_sz = CHUNK_HEADER_LEN;
359 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
360 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700361
Jerry Zhang7b444f02018-06-12 16:42:09 -0700362 out->cur_out_ptr += skip_len;
363 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700364
Jerry Zhang7b444f02018-06-12 16:42:09 -0700365 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700366}
367
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900368static int write_sparse_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700369 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900370 uint64_t rnd_up_len;
371 int count;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700372 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700373
Jerry Zhang7b444f02018-06-12 16:42:09 -0700374 /* Round up the fill length to a multiple of the block size */
375 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700376
Jerry Zhang7b444f02018-06-12 16:42:09 -0700377 /* 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);
382 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700383
Jerry Zhang7b444f02018-06-12 16:42:09 -0700384 if (ret < 0) return -1;
385 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
386 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700387
Jerry Zhang7b444f02018-06-12 16:42:09 -0700388 if (out->use_crc) {
389 count = out->block_size / sizeof(uint32_t);
390 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
391 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700392
Jerry Zhang7b444f02018-06-12 16:42:09 -0700393 out->cur_out_ptr += rnd_up_len;
394 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700395
Jerry Zhang7b444f02018-06-12 16:42:09 -0700396 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700397}
398
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900399static int write_sparse_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700400 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900401 uint64_t rnd_up_len, zero_len;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700402 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700403
Jerry Zhang7b444f02018-06-12 16:42:09 -0700404 /* Round up the data length to a multiple of the block size */
405 rnd_up_len = ALIGN(len, out->block_size);
406 zero_len = rnd_up_len - len;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700407
Jerry Zhang7b444f02018-06-12 16:42:09 -0700408 /* Finally we can safely emit a chunk of data */
409 chunk_header.chunk_type = CHUNK_TYPE_RAW;
410 chunk_header.reserved1 = 0;
411 chunk_header.chunk_sz = rnd_up_len / out->block_size;
412 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
413 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700414
Jerry Zhang7b444f02018-06-12 16:42:09 -0700415 if (ret < 0) return -1;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000416 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700417 if (ret < 0) return -1;
418 if (zero_len) {
Keith Moka3b72062021-12-31 05:09:32 +0000419 uint64_t len = zero_len;
420 uint64_t write_len;
421 while (len) {
422 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
423 ret = out->ops->write(out, out->zero_buf, write_len);
424 if (ret < 0) {
425 return ret;
426 }
427 len -= write_len;
428 }
Jerry Zhang7b444f02018-06-12 16:42:09 -0700429 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700430
Jerry Zhang7b444f02018-06-12 16:42:09 -0700431 if (out->use_crc) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000432 out->crc32 = sparse_crc32(out->crc32, data, len);
Keith Moka3b72062021-12-31 05:09:32 +0000433 if (zero_len) {
434 uint64_t len = zero_len;
435 uint64_t write_len;
436 while (len) {
437 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
438 out->crc32 = sparse_crc32(out->crc32, out->zero_buf, write_len);
439 len -= write_len;
440 }
441 }
Jerry Zhang7b444f02018-06-12 16:42:09 -0700442 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700443
Jerry Zhang7b444f02018-06-12 16:42:09 -0700444 out->cur_out_ptr += rnd_up_len;
445 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700446
Jerry Zhang7b444f02018-06-12 16:42:09 -0700447 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700448}
449
David Andersonf06debc2023-03-16 21:23:53 -0700450static int write_sparse_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
451 chunk_header_t chunk_header;
452 uint64_t rnd_up_len, zero_len;
453 int ret;
454
455 /* Round up the data length to a multiple of the block size */
456 rnd_up_len = ALIGN(len, out->block_size);
457 zero_len = rnd_up_len - len;
458
459 /* Finally we can safely emit a chunk of data */
460 chunk_header.chunk_type = CHUNK_TYPE_RAW;
461 chunk_header.reserved1 = 0;
462 chunk_header.chunk_sz = rnd_up_len / out->block_size;
463 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
464 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
465
466 if (ret < 0) return -1;
467 bool ok = write_fd_chunk_range(fd, offset, len, [&ret, out](char* data, size_t size) -> bool {
468 ret = out->ops->write(out, data, size);
469 if (ret < 0) return false;
470 if (out->use_crc) {
471 out->crc32 = sparse_crc32(out->crc32, data, size);
472 }
473 return true;
474 });
475 if (!ok) return -1;
476 if (zero_len) {
477 uint64_t len = zero_len;
478 uint64_t write_len;
479 while (len) {
480 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
481 ret = out->ops->write(out, out->zero_buf, write_len);
482 if (ret < 0) {
483 return ret;
484 }
485 len -= write_len;
486 }
487
488 if (out->use_crc) {
489 uint64_t len = zero_len;
490 uint64_t write_len;
491 while (len) {
492 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
493 out->crc32 = sparse_crc32(out->crc32, out->zero_buf, write_len);
494 len -= write_len;
495 }
496 }
497 }
498
499 out->cur_out_ptr += rnd_up_len;
500 out->chunk_cnt++;
501
502 return 0;
503}
504
Jerry Zhang7b444f02018-06-12 16:42:09 -0700505int write_sparse_end_chunk(struct output_file* out) {
506 chunk_header_t chunk_header;
507 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700508
Jerry Zhang7b444f02018-06-12 16:42:09 -0700509 if (out->use_crc) {
510 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
511 chunk_header.reserved1 = 0;
512 chunk_header.chunk_sz = 0;
513 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
Colin Crossb55dcee2012-04-24 23:07:49 -0700514
Jerry Zhang7b444f02018-06-12 16:42:09 -0700515 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
516 if (ret < 0) {
517 return ret;
518 }
519 out->ops->write(out, &out->crc32, 4);
520 if (ret < 0) {
521 return ret;
522 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700523
Jerry Zhang7b444f02018-06-12 16:42:09 -0700524 out->chunk_cnt++;
525 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700526
Jerry Zhang7b444f02018-06-12 16:42:09 -0700527 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700528}
529
530static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700531 .write_data_chunk = write_sparse_data_chunk,
532 .write_fill_chunk = write_sparse_fill_chunk,
533 .write_skip_chunk = write_sparse_skip_chunk,
534 .write_end_chunk = write_sparse_end_chunk,
David Andersonf06debc2023-03-16 21:23:53 -0700535 .write_fd_chunk = write_sparse_fd_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700536};
537
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900538static int write_normal_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700539 int ret;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900540 uint64_t rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700541
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000542 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700543 if (ret < 0) {
544 return ret;
545 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700546
Jerry Zhang7b444f02018-06-12 16:42:09 -0700547 if (rnd_up_len > len) {
548 ret = out->ops->skip(out, rnd_up_len - len);
549 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700550
Jerry Zhang7b444f02018-06-12 16:42:09 -0700551 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700552}
553
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900554static int write_normal_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000555 int ret;
556 unsigned int i;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900557 uint64_t write_len;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000558
559 /* Initialize fill_buf with the fill_val */
Keith Moka3b72062021-12-31 05:09:32 +0000560 for (i = 0; i < FILL_ZERO_BUFSIZE / sizeof(uint32_t); i++) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000561 out->fill_buf[i] = fill_val;
562 }
563
564 while (len) {
Keith Moka3b72062021-12-31 05:09:32 +0000565 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000566 ret = out->ops->write(out, out->fill_buf, write_len);
567 if (ret < 0) {
568 return ret;
569 }
570
571 len -= write_len;
572 }
573
574 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700575}
576
David Andersonf06debc2023-03-16 21:23:53 -0700577static int write_normal_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
578 int ret;
579 uint64_t rnd_up_len = ALIGN(len, out->block_size);
580
581 bool ok = write_fd_chunk_range(fd, offset, len, [&ret, out](char* data, size_t size) -> bool {
582 ret = out->ops->write(out, data, size);
583 return ret >= 0;
584 });
585 if (!ok) return ret;
586
587 if (rnd_up_len > len) {
588 ret = out->ops->skip(out, rnd_up_len - len);
589 }
590
591 return ret;
592}
593
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900594static int write_normal_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700595 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700596}
597
Jerry Zhang7b444f02018-06-12 16:42:09 -0700598int write_normal_end_chunk(struct output_file* out) {
599 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700600}
601
602static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700603 .write_data_chunk = write_normal_data_chunk,
604 .write_fill_chunk = write_normal_fill_chunk,
605 .write_skip_chunk = write_normal_skip_chunk,
606 .write_end_chunk = write_normal_end_chunk,
David Andersonf06debc2023-03-16 21:23:53 -0700607 .write_fd_chunk = write_normal_fd_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700608};
609
Jerry Zhang7b444f02018-06-12 16:42:09 -0700610void output_file_close(struct output_file* out) {
611 out->sparse_ops->write_end_chunk(out);
Robin Hsu48c9f612019-11-07 13:44:08 +0800612 free(out->zero_buf);
613 free(out->fill_buf);
614 out->zero_buf = nullptr;
615 out->fill_buf = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700616 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700617}
618
Jerry Zhang7b444f02018-06-12 16:42:09 -0700619static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
620 int chunks, bool crc) {
621 int ret;
Colin Crossb4cd2672012-05-18 14:49:50 -0700622
Jerry Zhang7b444f02018-06-12 16:42:09 -0700623 out->len = len;
624 out->block_size = block_size;
Chih-Hung Hsieh5d08f632018-12-11 10:38:27 -0800625 out->cur_out_ptr = 0LL;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700626 out->chunk_cnt = 0;
627 out->crc32 = 0;
628 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700629
Keith Moka3b72062021-12-31 05:09:32 +0000630 // don't use sparse format block size as it can takes up to 32GB
631 out->zero_buf = reinterpret_cast<char*>(calloc(FILL_ZERO_BUFSIZE, 1));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700632 if (!out->zero_buf) {
633 error_errno("malloc zero_buf");
634 return -ENOMEM;
635 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700636
Keith Moka3b72062021-12-31 05:09:32 +0000637 // don't use sparse format block size as it can takes up to 32GB
638 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(FILL_ZERO_BUFSIZE, 1));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700639 if (!out->fill_buf) {
640 error_errno("malloc fill_buf");
641 ret = -ENOMEM;
642 goto err_fill_buf;
643 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700644
Jerry Zhang7b444f02018-06-12 16:42:09 -0700645 if (sparse) {
646 out->sparse_ops = &sparse_file_ops;
647 } else {
648 out->sparse_ops = &normal_file_ops;
649 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700650
Jerry Zhang7b444f02018-06-12 16:42:09 -0700651 if (sparse) {
652 sparse_header_t sparse_header = {
653 .magic = SPARSE_HEADER_MAGIC,
654 .major_version = SPARSE_HEADER_MAJOR_VER,
655 .minor_version = SPARSE_HEADER_MINOR_VER,
656 .file_hdr_sz = SPARSE_HEADER_LEN,
657 .chunk_hdr_sz = CHUNK_HEADER_LEN,
658 .blk_sz = out->block_size,
659 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
660 .total_chunks = static_cast<unsigned>(chunks),
661 .image_checksum = 0};
Colin Cross28fa5bc2012-05-20 13:28:05 -0700662
Jerry Zhang7b444f02018-06-12 16:42:09 -0700663 if (out->use_crc) {
664 sparse_header.total_chunks++;
665 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700666
Jerry Zhang7b444f02018-06-12 16:42:09 -0700667 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
668 if (ret < 0) {
669 goto err_write;
670 }
671 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700672
Jerry Zhang7b444f02018-06-12 16:42:09 -0700673 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700674
675err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700676 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700677err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700678 free(out->zero_buf);
679 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700680}
681
Jerry Zhang7b444f02018-06-12 16:42:09 -0700682static struct output_file* output_file_new_gz(void) {
683 struct output_file_gz* outgz =
684 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
685 if (!outgz) {
686 error_errno("malloc struct outgz");
Yi Kong17ba95e2018-07-23 16:31:11 -0700687 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700688 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700689
Jerry Zhang7b444f02018-06-12 16:42:09 -0700690 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700691
Jerry Zhang7b444f02018-06-12 16:42:09 -0700692 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700693}
694
Jerry Zhang7b444f02018-06-12 16:42:09 -0700695static struct output_file* output_file_new_normal(void) {
696 struct output_file_normal* outn =
697 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
698 if (!outn) {
699 error_errno("malloc struct outn");
Yi Kong17ba95e2018-07-23 16:31:11 -0700700 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700701 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700702
Jerry Zhang7b444f02018-06-12 16:42:09 -0700703 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700704
Jerry Zhang7b444f02018-06-12 16:42:09 -0700705 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700706}
707
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000708struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
709 unsigned int block_size, int64_t len, int gz __unused,
710 int sparse, int chunks, int crc) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700711 int ret;
712 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700713
Jerry Zhang7b444f02018-06-12 16:42:09 -0700714 outc =
715 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
716 if (!outc) {
717 error_errno("malloc struct outc");
Yi Kong17ba95e2018-07-23 16:31:11 -0700718 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700719 }
Colin Cross1e17b312012-05-21 16:35:45 -0700720
Jerry Zhang7b444f02018-06-12 16:42:09 -0700721 outc->out.ops = &callback_file_ops;
722 outc->priv = priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000723 outc->write = write;
Colin Cross1e17b312012-05-21 16:35:45 -0700724
Jerry Zhang7b444f02018-06-12 16:42:09 -0700725 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
726 if (ret < 0) {
727 free(outc);
Yi Kong17ba95e2018-07-23 16:31:11 -0700728 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700729 }
Colin Cross1e17b312012-05-21 16:35:45 -0700730
Jerry Zhang7b444f02018-06-12 16:42:09 -0700731 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700732}
733
Jerry Zhang7b444f02018-06-12 16:42:09 -0700734struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
735 int sparse, int chunks, int crc) {
736 int ret;
737 struct output_file* out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700738
Jerry Zhang7b444f02018-06-12 16:42:09 -0700739 if (gz) {
740 out = output_file_new_gz();
741 } else {
742 out = output_file_new_normal();
743 }
744 if (!out) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700745 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700746 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700747
Jerry Zhang7b444f02018-06-12 16:42:09 -0700748 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700749
Jerry Zhang7b444f02018-06-12 16:42:09 -0700750 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
751 if (ret < 0) {
752 free(out);
Yi Kong17ba95e2018-07-23 16:31:11 -0700753 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700754 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700755
Jerry Zhang7b444f02018-06-12 16:42:09 -0700756 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700757}
758
Colin Cross28fa5bc2012-05-20 13:28:05 -0700759/* Write a contiguous region of data blocks from a memory buffer */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900760int write_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700761 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700762}
763
764/* Write a contiguous region of data blocks with a fill value */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900765int write_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700766 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700767}
768
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900769int write_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
David Andersonf06debc2023-03-16 21:23:53 -0700770 return out->sparse_ops->write_fd_chunk(out, len, fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700771}
772
773/* Write a contiguous region of data blocks from a file */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900774int write_file_chunk(struct output_file* out, uint64_t len, const char* file, int64_t offset) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700775 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700776
Jerry Zhang7b444f02018-06-12 16:42:09 -0700777 int file_fd = open(file, O_RDONLY | O_BINARY);
778 if (file_fd < 0) {
779 return -errno;
780 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700781
Jerry Zhang7b444f02018-06-12 16:42:09 -0700782 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700783
Jerry Zhang7b444f02018-06-12 16:42:09 -0700784 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700785
Jerry Zhang7b444f02018-06-12 16:42:09 -0700786 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700787}
788
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900789int write_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700790 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700791}