blob: cb5d7305270d99ad120c5cfd9b25991ca6982574 [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 Mok0cf395c2021-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
Colin Cross28fa5bc2012-05-20 13:28:05 -070061struct output_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070062 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 Cross28fa5bc2012-05-20 13:28:05 -070067};
68
Colin Crossb55dcee2012-04-24 23:07:49 -070069struct sparse_file_ops {
Hyeongseok Kime8d02c52020-08-10 12:11:57 +090070 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 Zhang7b444f02018-06-12 16:42:09 -070073 int (*write_end_chunk)(struct output_file* out);
Colin Crossb55dcee2012-04-24 23:07:49 -070074};
75
Colin Cross28fa5bc2012-05-20 13:28:05 -070076struct output_file {
Jerry Zhang7b444f02018-06-12 16:42:09 -070077 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 Cross28fa5bc2012-05-20 13:28:05 -070088};
89
Colin Crossb4cd2672012-05-18 14:49:50 -070090struct output_file_gz {
Jerry Zhang7b444f02018-06-12 16:42:09 -070091 struct output_file out;
92 gzFile gz_fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070093};
94
Jerry Zhang7b444f02018-06-12 16:42:09 -070095#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
Colin Crossb4cd2672012-05-18 14:49:50 -070096
97struct output_file_normal {
Jerry Zhang7b444f02018-06-12 16:42:09 -070098 struct output_file out;
99 int fd;
Colin Crossb4cd2672012-05-18 14:49:50 -0700100};
101
Jerry Zhang7b444f02018-06-12 16:42:09 -0700102#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700103
Colin Cross1e17b312012-05-21 16:35:45 -0700104struct output_file_callback {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700105 struct output_file out;
106 void* priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000107 int (*write)(void* priv, const void* buf, size_t len);
Colin Cross1e17b312012-05-21 16:35:45 -0700108};
109
Jerry Zhang7b444f02018-06-12 16:42:09 -0700110#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
Colin Cross1e17b312012-05-21 16:35:45 -0700111
Jerry Zhang7b444f02018-06-12 16:42:09 -0700112static int file_open(struct output_file* out, int fd) {
113 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700114
Jerry Zhang7b444f02018-06-12 16:42:09 -0700115 outn->fd = fd;
116 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700117}
118
Jerry Zhang7b444f02018-06-12 16:42:09 -0700119static 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 Cross28fa5bc2012-05-20 13:28:05 -0700122
Jerry Zhang7b444f02018-06-12 16:42:09 -0700123 ret = lseek64(outn->fd, cnt, SEEK_CUR);
124 if (ret < 0) {
125 error_errno("lseek64");
126 return -1;
127 }
128 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700129}
130
Jerry Zhang7b444f02018-06-12 16:42:09 -0700131static 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 Crossb4cd2672012-05-18 14:49:50 -0700134
Jerry Zhang7b444f02018-06-12 16:42:09 -0700135 ret = ftruncate64(outn->fd, len);
136 if (ret < 0) {
137 return -errno;
138 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700139
Jerry Zhang7b444f02018-06-12 16:42:09 -0700140 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700141}
142
Jerry Zhang7b444f02018-06-12 16:42:09 -0700143static 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 Crossb4cd2672012-05-18 14:49:50 -0700146
Jerry Zhang7b444f02018-06-12 16:42:09 -0700147 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 Compostellafca594c2016-09-30 14:54:25 +0200156
Jerry Zhang7b444f02018-06-12 16:42:09 -0700157 data = (char*)data + ret;
158 len -= ret;
159 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700160
Jerry Zhang7b444f02018-06-12 16:42:09 -0700161 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700162}
163
Jerry Zhang7b444f02018-06-12 16:42:09 -0700164static void file_close(struct output_file* out) {
165 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700166
Jerry Zhang7b444f02018-06-12 16:42:09 -0700167 free(outn);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700168}
169
Colin Cross28fa5bc2012-05-20 13:28:05 -0700170static struct output_file_ops file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700171 .open = file_open,
172 .skip = file_skip,
173 .pad = file_pad,
174 .write = file_write,
175 .close = file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700176};
177
Jerry Zhang7b444f02018-06-12 16:42:09 -0700178static int gz_file_open(struct output_file* out, int fd) {
179 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700180
Jerry Zhang7b444f02018-06-12 16:42:09 -0700181 outgz->gz_fd = gzdopen(fd, "wb9");
182 if (!outgz->gz_fd) {
183 error_errno("gzopen");
184 return -errno;
185 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700186
Jerry Zhang7b444f02018-06-12 16:42:09 -0700187 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700188}
189
Jerry Zhang7b444f02018-06-12 16:42:09 -0700190static 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 Crossb4cd2672012-05-18 14:49:50 -0700193
Jerry Zhang7b444f02018-06-12 16:42:09 -0700194 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
195 if (ret < 0) {
196 error_errno("gzseek");
197 return -1;
198 }
199 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700200}
201
Jerry Zhang7b444f02018-06-12 16:42:09 -0700202static 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 Crossb4cd2672012-05-18 14:49:50 -0700205
Jerry Zhang7b444f02018-06-12 16:42:09 -0700206 ret = gztell(outgz->gz_fd);
207 if (ret < 0) {
208 return -1;
209 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700210
Jerry Zhang7b444f02018-06-12 16:42:09 -0700211 if (ret >= len) {
212 return 0;
213 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700214
Jerry Zhang7b444f02018-06-12 16:42:09 -0700215 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
216 if (ret < 0) {
217 return -1;
218 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700219
Jerry Zhang7b444f02018-06-12 16:42:09 -0700220 gzwrite(outgz->gz_fd, "", 1);
Colin Crossb4cd2672012-05-18 14:49:50 -0700221
Jerry Zhang7b444f02018-06-12 16:42:09 -0700222 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700223}
224
Jerry Zhang7b444f02018-06-12 16:42:09 -0700225static 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 Crossb4cd2672012-05-18 14:49:50 -0700228
Jerry Zhang7b444f02018-06-12 16:42:09 -0700229 while (len > 0) {
cfig946da7c2019-08-15 10:07:07 +0800230 ret = gzwrite(outgz->gz_fd, data, std::min<unsigned int>(len, (unsigned int)INT_MAX));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700231 if (ret == 0) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700232 error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700233 return -1;
234 }
235 len -= ret;
236 data = (char*)data + ret;
237 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700238
Jerry Zhang7b444f02018-06-12 16:42:09 -0700239 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700240}
241
Jerry Zhang7b444f02018-06-12 16:42:09 -0700242static void gz_file_close(struct output_file* out) {
243 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700244
Jerry Zhang7b444f02018-06-12 16:42:09 -0700245 gzclose(outgz->gz_fd);
246 free(outgz);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700247}
248
249static struct output_file_ops gz_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700250 .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 Cross28fa5bc2012-05-20 13:28:05 -0700255};
256
Jerry Zhang7b444f02018-06-12 16:42:09 -0700257static int callback_file_open(struct output_file* out __unused, int fd __unused) {
258 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700259}
260
Jerry Zhang7b444f02018-06-12 16:42:09 -0700261static 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 Cross1e17b312012-05-21 16:35:45 -0700265
Jerry Zhang7b444f02018-06-12 16:42:09 -0700266 while (off > 0) {
cfig946da7c2019-08-15 10:07:07 +0800267 to_write = std::min(off, (int64_t)INT_MAX);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000268 ret = outc->write(outc->priv, nullptr, to_write);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700269 if (ret < 0) {
270 return ret;
271 }
272 off -= to_write;
273 }
Colin Cross1e17b312012-05-21 16:35:45 -0700274
Jerry Zhang7b444f02018-06-12 16:42:09 -0700275 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700276}
277
Jerry Zhang7b444f02018-06-12 16:42:09 -0700278static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
279 return -1;
Colin Cross1e17b312012-05-21 16:35:45 -0700280}
281
Jerry Zhang7b444f02018-06-12 16:42:09 -0700282static 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 Cross1e17b312012-05-21 16:35:45 -0700284
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000285 return outc->write(outc->priv, data, len);
Colin Cross1e17b312012-05-21 16:35:45 -0700286}
287
Jerry Zhang7b444f02018-06-12 16:42:09 -0700288static void callback_file_close(struct output_file* out) {
289 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700290
Jerry Zhang7b444f02018-06-12 16:42:09 -0700291 free(outc);
Colin Cross1e17b312012-05-21 16:35:45 -0700292}
293
294static struct output_file_ops callback_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700295 .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 Cross1e17b312012-05-21 16:35:45 -0700300};
301
Jerry Zhang7b444f02018-06-12 16:42:09 -0700302int 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 Cross13a56062012-06-19 16:45:48 -0700306
Jerry Zhang7b444f02018-06-12 16:42:09 -0700307 while (total < len) {
308 ret = read(fd, ptr, len - total);
Colin Cross13a56062012-06-19 16:45:48 -0700309
Jerry Zhang7b444f02018-06-12 16:42:09 -0700310 if (ret < 0) return -errno;
Colin Cross13a56062012-06-19 16:45:48 -0700311
Jerry Zhang7b444f02018-06-12 16:42:09 -0700312 if (ret == 0) return -EINVAL;
Colin Cross13a56062012-06-19 16:45:48 -0700313
Jerry Zhang7b444f02018-06-12 16:42:09 -0700314 ptr += ret;
315 total += ret;
316 }
Colin Cross13a56062012-06-19 16:45:48 -0700317
Jerry Zhang7b444f02018-06-12 16:42:09 -0700318 return 0;
Colin Cross13a56062012-06-19 16:45:48 -0700319}
320
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900321static int write_sparse_skip_chunk(struct output_file* out, uint64_t skip_len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700322 chunk_header_t chunk_header;
323 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700324
Jerry Zhang7b444f02018-06-12 16:42:09 -0700325 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 Cross28fa5bc2012-05-20 13:28:05 -0700330
Jerry Zhang7b444f02018-06-12 16:42:09 -0700331 /* 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 Cross28fa5bc2012-05-20 13:28:05 -0700338
Jerry Zhang7b444f02018-06-12 16:42:09 -0700339 out->cur_out_ptr += skip_len;
340 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700341
Jerry Zhang7b444f02018-06-12 16:42:09 -0700342 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700343}
344
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900345static int write_sparse_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700346 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900347 uint64_t rnd_up_len;
348 int count;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700349 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700350
Jerry Zhang7b444f02018-06-12 16:42:09 -0700351 /* Round up the fill length to a multiple of the block size */
352 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700353
Jerry Zhang7b444f02018-06-12 16:42:09 -0700354 /* 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 Cross28fa5bc2012-05-20 13:28:05 -0700360
Jerry Zhang7b444f02018-06-12 16:42:09 -0700361 if (ret < 0) return -1;
362 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
363 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700364
Jerry Zhang7b444f02018-06-12 16:42:09 -0700365 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 Cross28fa5bc2012-05-20 13:28:05 -0700369
Jerry Zhang7b444f02018-06-12 16:42:09 -0700370 out->cur_out_ptr += rnd_up_len;
371 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700372
Jerry Zhang7b444f02018-06-12 16:42:09 -0700373 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700374}
375
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900376static int write_sparse_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700377 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900378 uint64_t rnd_up_len, zero_len;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700379 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700380
Jerry Zhang7b444f02018-06-12 16:42:09 -0700381 /* 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 Cross28fa5bc2012-05-20 13:28:05 -0700384
Jerry Zhang7b444f02018-06-12 16:42:09 -0700385 /* 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 Cross28fa5bc2012-05-20 13:28:05 -0700391
Jerry Zhang7b444f02018-06-12 16:42:09 -0700392 if (ret < 0) return -1;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000393 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700394 if (ret < 0) return -1;
395 if (zero_len) {
Keith Mok0cf395c2021-12-31 05:09:32 +0000396 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 Zhang7b444f02018-06-12 16:42:09 -0700406 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700407
Jerry Zhang7b444f02018-06-12 16:42:09 -0700408 if (out->use_crc) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000409 out->crc32 = sparse_crc32(out->crc32, data, len);
Keith Mok0cf395c2021-12-31 05:09:32 +0000410 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 Zhang7b444f02018-06-12 16:42:09 -0700419 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700420
Jerry Zhang7b444f02018-06-12 16:42:09 -0700421 out->cur_out_ptr += rnd_up_len;
422 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700423
Jerry Zhang7b444f02018-06-12 16:42:09 -0700424 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700425}
426
Jerry Zhang7b444f02018-06-12 16:42:09 -0700427int write_sparse_end_chunk(struct output_file* out) {
428 chunk_header_t chunk_header;
429 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700430
Jerry Zhang7b444f02018-06-12 16:42:09 -0700431 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 Crossb55dcee2012-04-24 23:07:49 -0700436
Jerry Zhang7b444f02018-06-12 16:42:09 -0700437 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 Crossb55dcee2012-04-24 23:07:49 -0700445
Jerry Zhang7b444f02018-06-12 16:42:09 -0700446 out->chunk_cnt++;
447 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700448
Jerry Zhang7b444f02018-06-12 16:42:09 -0700449 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700450}
451
452static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700453 .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 Crossb55dcee2012-04-24 23:07:49 -0700457};
458
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900459static int write_normal_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700460 int ret;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900461 uint64_t rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700462
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000463 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700464 if (ret < 0) {
465 return ret;
466 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700467
Jerry Zhang7b444f02018-06-12 16:42:09 -0700468 if (rnd_up_len > len) {
469 ret = out->ops->skip(out, rnd_up_len - len);
470 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700471
Jerry Zhang7b444f02018-06-12 16:42:09 -0700472 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700473}
474
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900475static int write_normal_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000476 int ret;
477 unsigned int i;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900478 uint64_t write_len;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000479
480 /* Initialize fill_buf with the fill_val */
Keith Mok0cf395c2021-12-31 05:09:32 +0000481 for (i = 0; i < FILL_ZERO_BUFSIZE / sizeof(uint32_t); i++) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000482 out->fill_buf[i] = fill_val;
483 }
484
485 while (len) {
Keith Mok0cf395c2021-12-31 05:09:32 +0000486 write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000487 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 Crossb55dcee2012-04-24 23:07:49 -0700496}
497
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900498static int write_normal_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700499 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700500}
501
Jerry Zhang7b444f02018-06-12 16:42:09 -0700502int write_normal_end_chunk(struct output_file* out) {
503 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700504}
505
506static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700507 .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 Crossb55dcee2012-04-24 23:07:49 -0700511};
512
Jerry Zhang7b444f02018-06-12 16:42:09 -0700513void output_file_close(struct output_file* out) {
514 out->sparse_ops->write_end_chunk(out);
Robin Hsu48c9f612019-11-07 13:44:08 +0800515 free(out->zero_buf);
516 free(out->fill_buf);
517 out->zero_buf = nullptr;
518 out->fill_buf = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700519 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700520}
521
Jerry Zhang7b444f02018-06-12 16:42:09 -0700522static 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 Crossb4cd2672012-05-18 14:49:50 -0700525
Jerry Zhang7b444f02018-06-12 16:42:09 -0700526 out->len = len;
527 out->block_size = block_size;
Chih-Hung Hsieh5d08f632018-12-11 10:38:27 -0800528 out->cur_out_ptr = 0LL;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700529 out->chunk_cnt = 0;
530 out->crc32 = 0;
531 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700532
Keith Mok0cf395c2021-12-31 05:09:32 +0000533 // 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 Zhang7b444f02018-06-12 16:42:09 -0700535 if (!out->zero_buf) {
536 error_errno("malloc zero_buf");
537 return -ENOMEM;
538 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700539
Keith Mok0cf395c2021-12-31 05:09:32 +0000540 // 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 Zhang7b444f02018-06-12 16:42:09 -0700542 if (!out->fill_buf) {
543 error_errno("malloc fill_buf");
544 ret = -ENOMEM;
545 goto err_fill_buf;
546 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700547
Jerry Zhang7b444f02018-06-12 16:42:09 -0700548 if (sparse) {
549 out->sparse_ops = &sparse_file_ops;
550 } else {
551 out->sparse_ops = &normal_file_ops;
552 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700553
Jerry Zhang7b444f02018-06-12 16:42:09 -0700554 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 Cross28fa5bc2012-05-20 13:28:05 -0700565
Jerry Zhang7b444f02018-06-12 16:42:09 -0700566 if (out->use_crc) {
567 sparse_header.total_chunks++;
568 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700569
Jerry Zhang7b444f02018-06-12 16:42:09 -0700570 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
571 if (ret < 0) {
572 goto err_write;
573 }
574 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700575
Jerry Zhang7b444f02018-06-12 16:42:09 -0700576 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700577
578err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700579 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700580err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700581 free(out->zero_buf);
582 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700583}
584
Jerry Zhang7b444f02018-06-12 16:42:09 -0700585static 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 Kong17ba95e2018-07-23 16:31:11 -0700590 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700591 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700592
Jerry Zhang7b444f02018-06-12 16:42:09 -0700593 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700594
Jerry Zhang7b444f02018-06-12 16:42:09 -0700595 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700596}
597
Jerry Zhang7b444f02018-06-12 16:42:09 -0700598static 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 Kong17ba95e2018-07-23 16:31:11 -0700603 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700604 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700605
Jerry Zhang7b444f02018-06-12 16:42:09 -0700606 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700607
Jerry Zhang7b444f02018-06-12 16:42:09 -0700608 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700609}
610
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000611struct 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 Zhang7b444f02018-06-12 16:42:09 -0700614 int ret;
615 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700616
Jerry Zhang7b444f02018-06-12 16:42:09 -0700617 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 Kong17ba95e2018-07-23 16:31:11 -0700621 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700622 }
Colin Cross1e17b312012-05-21 16:35:45 -0700623
Jerry Zhang7b444f02018-06-12 16:42:09 -0700624 outc->out.ops = &callback_file_ops;
625 outc->priv = priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000626 outc->write = write;
Colin Cross1e17b312012-05-21 16:35:45 -0700627
Jerry Zhang7b444f02018-06-12 16:42:09 -0700628 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
629 if (ret < 0) {
630 free(outc);
Yi Kong17ba95e2018-07-23 16:31:11 -0700631 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700632 }
Colin Cross1e17b312012-05-21 16:35:45 -0700633
Jerry Zhang7b444f02018-06-12 16:42:09 -0700634 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700635}
636
Jerry Zhang7b444f02018-06-12 16:42:09 -0700637struct 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 Crossb4cd2672012-05-18 14:49:50 -0700641
Jerry Zhang7b444f02018-06-12 16:42:09 -0700642 if (gz) {
643 out = output_file_new_gz();
644 } else {
645 out = output_file_new_normal();
646 }
647 if (!out) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700648 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700649 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700650
Jerry Zhang7b444f02018-06-12 16:42:09 -0700651 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700652
Jerry Zhang7b444f02018-06-12 16:42:09 -0700653 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
654 if (ret < 0) {
655 free(out);
Yi Kong17ba95e2018-07-23 16:31:11 -0700656 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700657 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700658
Jerry Zhang7b444f02018-06-12 16:42:09 -0700659 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700660}
661
Colin Cross28fa5bc2012-05-20 13:28:05 -0700662/* Write a contiguous region of data blocks from a memory buffer */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900663int write_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700664 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700665}
666
667/* Write a contiguous region of data blocks with a fill value */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900668int write_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700669 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700670}
671
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900672int write_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700673 auto m = android::base::MappedFile::FromFd(fd, offset, len, PROT_READ);
674 if (!m) return -errno;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000675
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700676 return out->sparse_ops->write_data_chunk(out, m->size(), m->data());
Colin Cross9e1f17e2012-04-25 18:31:39 -0700677}
678
679/* Write a contiguous region of data blocks from a file */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900680int write_file_chunk(struct output_file* out, uint64_t len, const char* file, int64_t offset) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700681 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700682
Jerry Zhang7b444f02018-06-12 16:42:09 -0700683 int file_fd = open(file, O_RDONLY | O_BINARY);
684 if (file_fd < 0) {
685 return -errno;
686 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700687
Jerry Zhang7b444f02018-06-12 16:42:09 -0700688 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700689
Jerry Zhang7b444f02018-06-12 16:42:09 -0700690 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700691
Jerry Zhang7b444f02018-06-12 16:42:09 -0700692 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700693}
694
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900695int write_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700696 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700697}