blob: 8a21daba1c81e8c91a6449c618307907578741de [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
20#include <fcntl.h>
Elliott Hughesccecf142014-01-16 10:53:11 -080021#include <inttypes.h>
Colin Cross1e17b312012-05-21 16:35:45 -070022#include <limits.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070023#include <stdbool.h>
Colin Crossb4cd2672012-05-18 14:49:50 -070024#include <stddef.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070025#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30#include <zlib.h>
31
Jerry Zhangdb69f0d2018-06-14 16:58:58 -070032#include <algorithm>
33
Mark Salyzyn031a7482014-02-27 16:56:15 -080034#include "defs.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070035#include "output_file.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070036#include "sparse_crc32.h"
Mark Salyzyn8116c8c2014-05-01 09:15:02 -070037#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070038
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070039#ifndef _WIN32
Colin Cross28fa5bc2012-05-20 13:28:05 -070040#include <sys/mman.h>
41#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
49#define mmap64 mmap
50#define off64_t off_t
51#endif
52
Colin Cross28fa5bc2012-05-20 13:28:05 -070053#define SPARSE_HEADER_MAJOR_VER 1
54#define SPARSE_HEADER_MINOR_VER 0
Jerry Zhang7b444f02018-06-12 16:42:09 -070055#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
Colin Cross28fa5bc2012-05-20 13:28:05 -070056#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
57
Jerry Zhang7b444f02018-06-12 16:42:09 -070058#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
Colin Crossb4cd2672012-05-18 14:49:50 -070059
Colin Cross28fa5bc2012-05-20 13:28:05 -070060struct output_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070061 int (*open)(struct output_file*, int fd);
62 int (*skip)(struct output_file*, int64_t);
63 int (*pad)(struct output_file*, int64_t);
64 int (*write)(struct output_file*, void*, size_t);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -070065 int (*write_fd)(struct output_file*, int, size_t);
66 int (*write_fill)(struct output_file*, uint32_t, size_t);
Jerry Zhang7b444f02018-06-12 16:42:09 -070067 void (*close)(struct output_file*);
Colin Cross28fa5bc2012-05-20 13:28:05 -070068};
69
Colin Crossb55dcee2012-04-24 23:07:49 -070070struct sparse_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070071 int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -070072 int (*write_fd_chunk)(struct output_file* out, unsigned int len, int fd);
Jerry Zhang7b444f02018-06-12 16:42:09 -070073 int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
74 int (*write_skip_chunk)(struct output_file* out, int64_t len);
75 int (*write_end_chunk)(struct output_file* out);
Colin Crossb55dcee2012-04-24 23:07:49 -070076};
77
Colin Cross28fa5bc2012-05-20 13:28:05 -070078struct output_file {
Jerry Zhang7b444f02018-06-12 16:42:09 -070079 int64_t cur_out_ptr;
80 unsigned int chunk_cnt;
81 uint32_t crc32;
82 struct output_file_ops* ops;
83 struct sparse_file_ops* sparse_ops;
84 int use_crc;
85 unsigned int block_size;
86 int64_t len;
87 char* zero_buf;
88 uint32_t* fill_buf;
89 char* buf;
Colin Cross28fa5bc2012-05-20 13:28:05 -070090};
91
Colin Crossb4cd2672012-05-18 14:49:50 -070092struct output_file_gz {
Jerry Zhang7b444f02018-06-12 16:42:09 -070093 struct output_file out;
94 gzFile gz_fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070095};
96
Jerry Zhang7b444f02018-06-12 16:42:09 -070097#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
Colin Crossb4cd2672012-05-18 14:49:50 -070098
99struct output_file_normal {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700100 struct output_file out;
101 int fd;
Colin Crossb4cd2672012-05-18 14:49:50 -0700102};
103
Jerry Zhang7b444f02018-06-12 16:42:09 -0700104#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700105
Colin Cross1e17b312012-05-21 16:35:45 -0700106struct output_file_callback {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700107 struct output_file out;
108 void* priv;
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700109 int (*data_write)(void* priv, const void* data, size_t len);
110 int (*fd_write)(void* priv, int fd, size_t len);
111 int (*fill_write)(void* priv, uint32_t fill_val, size_t len);
112 int (*skip_write)(void* priv, off64_t len);
Colin Cross1e17b312012-05-21 16:35:45 -0700113};
114
Jerry Zhang7b444f02018-06-12 16:42:09 -0700115#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
Colin Cross1e17b312012-05-21 16:35:45 -0700116
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700117union handle_data {
118 void* data_ptr;
119 int fd;
120};
121
122static int default_file_write_fd(struct output_file* out, int fd, size_t len) {
123 int ret;
124 int64_t aligned_offset;
125 int aligned_diff;
126 uint64_t buffer_size;
127 char* ptr;
128 int64_t offset = lseek64(fd, 0, SEEK_CUR);
129
130 if (offset < 0) {
131 return -errno;
132 }
133
134 aligned_offset = offset & ~(4096 - 1);
135 aligned_diff = offset - aligned_offset;
136 buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
137
138#ifndef _WIN32
139 if (buffer_size > SIZE_MAX) {
140 return -E2BIG;
141 }
142 char* data =
143 reinterpret_cast<char*>(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
144 if (data == MAP_FAILED) {
145 return -errno;
146 }
147 ptr = data + aligned_diff;
148#else
149 char* data = reinterpret_cast<char*>(malloc(len));
150 if (!data) {
151 return -errno;
152 }
153 ret = read_all(fd, data, len);
154 if (ret < 0) {
155 free(data);
156 return -errno;
157 }
158 ptr = data;
159#endif
160
161 ret = out->ops->write(out, ptr, len);
162
163 if (out->use_crc) {
164 out->crc32 = sparse_crc32(out->crc32, ptr, len);
165 }
166
167#ifndef _WIN32
168 munmap(data, buffer_size);
169#else
170 free(data);
171#endif
172
173 return ret;
174}
175
176static int default_file_write_fill(struct output_file* out, uint32_t fill_val, size_t len) {
177 int ret;
178 unsigned int i;
179 unsigned int write_len;
180
181 /* Initialize fill_buf with the fill_val */
182 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
183 out->fill_buf[i] = fill_val;
184 }
185
186 while (len) {
187 write_len = std::min(len, static_cast<size_t>(out->block_size));
188 ret = out->ops->write(out, out->fill_buf, write_len);
189 if (ret < 0) {
190 return ret;
191 }
192
193 len -= write_len;
194 }
195
196 return 0;
197}
198
Jerry Zhang7b444f02018-06-12 16:42:09 -0700199static int file_open(struct output_file* out, int fd) {
200 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700201
Jerry Zhang7b444f02018-06-12 16:42:09 -0700202 outn->fd = fd;
203 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700204}
205
Jerry Zhang7b444f02018-06-12 16:42:09 -0700206static int file_skip(struct output_file* out, int64_t cnt) {
207 off64_t ret;
208 struct output_file_normal* outn = to_output_file_normal(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700209
Jerry Zhang7b444f02018-06-12 16:42:09 -0700210 ret = lseek64(outn->fd, cnt, SEEK_CUR);
211 if (ret < 0) {
212 error_errno("lseek64");
213 return -1;
214 }
215 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700216}
217
Jerry Zhang7b444f02018-06-12 16:42:09 -0700218static int file_pad(struct output_file* out, int64_t len) {
219 int ret;
220 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700221
Jerry Zhang7b444f02018-06-12 16:42:09 -0700222 ret = ftruncate64(outn->fd, len);
223 if (ret < 0) {
224 return -errno;
225 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700226
Jerry Zhang7b444f02018-06-12 16:42:09 -0700227 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700228}
229
Jerry Zhang7b444f02018-06-12 16:42:09 -0700230static int file_write(struct output_file* out, void* data, size_t len) {
231 ssize_t ret;
232 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700233
Jerry Zhang7b444f02018-06-12 16:42:09 -0700234 while (len > 0) {
235 ret = write(outn->fd, data, len);
236 if (ret < 0) {
237 if (errno == EINTR) {
238 continue;
239 }
240 error_errno("write");
241 return -1;
242 }
Jeremy Compostellafca594c2016-09-30 14:54:25 +0200243
Jerry Zhang7b444f02018-06-12 16:42:09 -0700244 data = (char*)data + ret;
245 len -= ret;
246 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700247
Jerry Zhang7b444f02018-06-12 16:42:09 -0700248 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700249}
250
Jerry Zhang7b444f02018-06-12 16:42:09 -0700251static void file_close(struct output_file* out) {
252 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700253
Jerry Zhang7b444f02018-06-12 16:42:09 -0700254 free(outn);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700255}
256
Colin Cross28fa5bc2012-05-20 13:28:05 -0700257static struct output_file_ops file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700258 .open = file_open,
259 .skip = file_skip,
260 .pad = file_pad,
261 .write = file_write,
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700262 .write_fd = default_file_write_fd,
263 .write_fill = default_file_write_fill,
Jerry Zhang7b444f02018-06-12 16:42:09 -0700264 .close = file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700265};
266
Jerry Zhang7b444f02018-06-12 16:42:09 -0700267static int gz_file_open(struct output_file* out, int fd) {
268 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700269
Jerry Zhang7b444f02018-06-12 16:42:09 -0700270 outgz->gz_fd = gzdopen(fd, "wb9");
271 if (!outgz->gz_fd) {
272 error_errno("gzopen");
273 return -errno;
274 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700275
Jerry Zhang7b444f02018-06-12 16:42:09 -0700276 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700277}
278
Jerry Zhang7b444f02018-06-12 16:42:09 -0700279static int gz_file_skip(struct output_file* out, int64_t cnt) {
280 off64_t ret;
281 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700282
Jerry Zhang7b444f02018-06-12 16:42:09 -0700283 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
284 if (ret < 0) {
285 error_errno("gzseek");
286 return -1;
287 }
288 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700289}
290
Jerry Zhang7b444f02018-06-12 16:42:09 -0700291static int gz_file_pad(struct output_file* out, int64_t len) {
292 off64_t ret;
293 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700294
Jerry Zhang7b444f02018-06-12 16:42:09 -0700295 ret = gztell(outgz->gz_fd);
296 if (ret < 0) {
297 return -1;
298 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700299
Jerry Zhang7b444f02018-06-12 16:42:09 -0700300 if (ret >= len) {
301 return 0;
302 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700303
Jerry Zhang7b444f02018-06-12 16:42:09 -0700304 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
305 if (ret < 0) {
306 return -1;
307 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700308
Jerry Zhang7b444f02018-06-12 16:42:09 -0700309 gzwrite(outgz->gz_fd, "", 1);
Colin Crossb4cd2672012-05-18 14:49:50 -0700310
Jerry Zhang7b444f02018-06-12 16:42:09 -0700311 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700312}
313
Jerry Zhang7b444f02018-06-12 16:42:09 -0700314static int gz_file_write(struct output_file* out, void* data, size_t len) {
315 int ret;
316 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700317
Jerry Zhang7b444f02018-06-12 16:42:09 -0700318 while (len > 0) {
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700319 ret = gzwrite(outgz->gz_fd, data, std::min(len, static_cast<size_t>(INT_MAX)));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700320 if (ret == 0) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700321 error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700322 return -1;
323 }
324 len -= ret;
325 data = (char*)data + ret;
326 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700327
Jerry Zhang7b444f02018-06-12 16:42:09 -0700328 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700329}
330
Jerry Zhang7b444f02018-06-12 16:42:09 -0700331static void gz_file_close(struct output_file* out) {
332 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700333
Jerry Zhang7b444f02018-06-12 16:42:09 -0700334 gzclose(outgz->gz_fd);
335 free(outgz);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700336}
337
338static struct output_file_ops gz_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700339 .open = gz_file_open,
340 .skip = gz_file_skip,
341 .pad = gz_file_pad,
342 .write = gz_file_write,
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700343 .write_fd = default_file_write_fd,
344 .write_fill = default_file_write_fill,
Jerry Zhang7b444f02018-06-12 16:42:09 -0700345 .close = gz_file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700346};
347
Jerry Zhang7b444f02018-06-12 16:42:09 -0700348static int callback_file_open(struct output_file* out __unused, int fd __unused) {
349 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700350}
351
Jerry Zhang7b444f02018-06-12 16:42:09 -0700352static int callback_file_skip(struct output_file* out, int64_t off) {
353 struct output_file_callback* outc = to_output_file_callback(out);
354 int to_write;
355 int ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700356
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700357 if (outc->skip_write) {
358 return outc->skip_write(outc->priv, off);
359 }
360
Jerry Zhang7b444f02018-06-12 16:42:09 -0700361 while (off > 0) {
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700362 to_write = std::min(off, static_cast<int64_t>(INT_MAX));
363 ret = outc->data_write(outc->priv, nullptr, to_write);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700364 if (ret < 0) {
365 return ret;
366 }
367 off -= to_write;
368 }
Colin Cross1e17b312012-05-21 16:35:45 -0700369
Jerry Zhang7b444f02018-06-12 16:42:09 -0700370 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700371}
372
Jerry Zhang7b444f02018-06-12 16:42:09 -0700373static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
374 return -1;
Colin Cross1e17b312012-05-21 16:35:45 -0700375}
376
Jerry Zhang7b444f02018-06-12 16:42:09 -0700377static int callback_file_write(struct output_file* out, void* data, size_t len) {
378 struct output_file_callback* outc = to_output_file_callback(out);
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700379 return outc->data_write(outc->priv, data, len);
380}
Colin Cross1e17b312012-05-21 16:35:45 -0700381
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700382static int callback_file_write_fd(struct output_file* out, int fd, size_t len) {
383 struct output_file_callback* outc = to_output_file_callback(out);
384 if (outc->fd_write) {
385 return outc->fd_write(outc->priv, fd, len);
386 }
387 return default_file_write_fd(out, fd, len);
388}
389
390static int callback_file_write_fill(struct output_file* out, uint32_t fill_val, size_t len) {
391 struct output_file_callback* outc = to_output_file_callback(out);
392 if (outc->fill_write) {
393 return outc->fill_write(outc->priv, fill_val, len);
394 }
395 return default_file_write_fill(out, fill_val, len);
Colin Cross1e17b312012-05-21 16:35:45 -0700396}
397
Jerry Zhang7b444f02018-06-12 16:42:09 -0700398static void callback_file_close(struct output_file* out) {
399 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700400
Jerry Zhang7b444f02018-06-12 16:42:09 -0700401 free(outc);
Colin Cross1e17b312012-05-21 16:35:45 -0700402}
403
404static struct output_file_ops callback_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700405 .open = callback_file_open,
406 .skip = callback_file_skip,
407 .pad = callback_file_pad,
408 .write = callback_file_write,
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700409 .write_fd = callback_file_write_fd,
410 .write_fill = callback_file_write_fill,
Jerry Zhang7b444f02018-06-12 16:42:09 -0700411 .close = callback_file_close,
Colin Cross1e17b312012-05-21 16:35:45 -0700412};
413
Jerry Zhang7b444f02018-06-12 16:42:09 -0700414int read_all(int fd, void* buf, size_t len) {
415 size_t total = 0;
416 int ret;
417 char* ptr = reinterpret_cast<char*>(buf);
Colin Cross13a56062012-06-19 16:45:48 -0700418
Jerry Zhang7b444f02018-06-12 16:42:09 -0700419 while (total < len) {
420 ret = read(fd, ptr, len - total);
Colin Cross13a56062012-06-19 16:45:48 -0700421
Jerry Zhang7b444f02018-06-12 16:42:09 -0700422 if (ret < 0) return -errno;
Colin Cross13a56062012-06-19 16:45:48 -0700423
Jerry Zhang7b444f02018-06-12 16:42:09 -0700424 if (ret == 0) return -EINVAL;
Colin Cross13a56062012-06-19 16:45:48 -0700425
Jerry Zhang7b444f02018-06-12 16:42:09 -0700426 ptr += ret;
427 total += ret;
428 }
Colin Cross13a56062012-06-19 16:45:48 -0700429
Jerry Zhang7b444f02018-06-12 16:42:09 -0700430 return 0;
Colin Cross13a56062012-06-19 16:45:48 -0700431}
432
Jerry Zhang7b444f02018-06-12 16:42:09 -0700433static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
434 chunk_header_t chunk_header;
435 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700436
Jerry Zhang7b444f02018-06-12 16:42:09 -0700437 if (skip_len % out->block_size) {
438 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
439 out->block_size);
440 return -1;
441 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700442
Jerry Zhang7b444f02018-06-12 16:42:09 -0700443 /* We are skipping data, so emit a don't care chunk. */
444 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
445 chunk_header.reserved1 = 0;
446 chunk_header.chunk_sz = skip_len / out->block_size;
447 chunk_header.total_sz = CHUNK_HEADER_LEN;
448 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
449 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700450
Jerry Zhang7b444f02018-06-12 16:42:09 -0700451 out->cur_out_ptr += skip_len;
452 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700453
Jerry Zhang7b444f02018-06-12 16:42:09 -0700454 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700455}
456
Jerry Zhang7b444f02018-06-12 16:42:09 -0700457static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
458 chunk_header_t chunk_header;
459 int rnd_up_len, count;
460 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700461
Jerry Zhang7b444f02018-06-12 16:42:09 -0700462 /* Round up the fill length to a multiple of the block size */
463 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700464
Jerry Zhang7b444f02018-06-12 16:42:09 -0700465 /* Finally we can safely emit a chunk of data */
466 chunk_header.chunk_type = CHUNK_TYPE_FILL;
467 chunk_header.reserved1 = 0;
468 chunk_header.chunk_sz = rnd_up_len / out->block_size;
469 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
470 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700471
Jerry Zhang7b444f02018-06-12 16:42:09 -0700472 if (ret < 0) return -1;
473 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
474 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700475
Jerry Zhang7b444f02018-06-12 16:42:09 -0700476 if (out->use_crc) {
477 count = out->block_size / sizeof(uint32_t);
478 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
479 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700480
Jerry Zhang7b444f02018-06-12 16:42:09 -0700481 out->cur_out_ptr += rnd_up_len;
482 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700483
Jerry Zhang7b444f02018-06-12 16:42:09 -0700484 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700485}
486
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700487static int write_sparse_data_chunk_variant(struct output_file* out, unsigned int len,
488 handle_data data, bool is_fd) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700489 chunk_header_t chunk_header;
490 int rnd_up_len, zero_len;
491 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700492
Jerry Zhang7b444f02018-06-12 16:42:09 -0700493 /* Round up the data length to a multiple of the block size */
494 rnd_up_len = ALIGN(len, out->block_size);
495 zero_len = rnd_up_len - len;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700496
Jerry Zhang7b444f02018-06-12 16:42:09 -0700497 /* Finally we can safely emit a chunk of data */
498 chunk_header.chunk_type = CHUNK_TYPE_RAW;
499 chunk_header.reserved1 = 0;
500 chunk_header.chunk_sz = rnd_up_len / out->block_size;
501 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
502 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700503
Jerry Zhang7b444f02018-06-12 16:42:09 -0700504 if (ret < 0) return -1;
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700505
506 if (is_fd) {
507 // CRC is handled by write_fd
508 ret = out->ops->write_fd(out, data.fd, len);
509 } else {
510 ret = out->ops->write(out, data.data_ptr, len);
511 if (out->use_crc) {
512 out->crc32 = sparse_crc32(out->crc32, data.data_ptr, len);
513 }
514 }
Jerry Zhang7b444f02018-06-12 16:42:09 -0700515 if (ret < 0) return -1;
516 if (zero_len) {
517 ret = out->ops->write(out, out->zero_buf, zero_len);
518 if (ret < 0) return -1;
519 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700520
Jerry Zhang7b444f02018-06-12 16:42:09 -0700521 if (out->use_crc) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700522 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
523 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700524
Jerry Zhang7b444f02018-06-12 16:42:09 -0700525 out->cur_out_ptr += rnd_up_len;
526 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700527
Jerry Zhang7b444f02018-06-12 16:42:09 -0700528 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700529}
530
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700531static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data_ptr) {
532 handle_data data = {data_ptr};
533 return write_sparse_data_chunk_variant(out, len, data, false /* isFd */);
534}
535
536static int write_sparse_fd_chunk(struct output_file* out, unsigned int len, int fd) {
537 handle_data data = {.fd = fd};
538 return write_sparse_data_chunk_variant(out, len, data, true /* isFd */);
539}
540
Jerry Zhang7b444f02018-06-12 16:42:09 -0700541int write_sparse_end_chunk(struct output_file* out) {
542 chunk_header_t chunk_header;
543 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700544
Jerry Zhang7b444f02018-06-12 16:42:09 -0700545 if (out->use_crc) {
546 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
547 chunk_header.reserved1 = 0;
548 chunk_header.chunk_sz = 0;
549 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
Colin Crossb55dcee2012-04-24 23:07:49 -0700550
Jerry Zhang7b444f02018-06-12 16:42:09 -0700551 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
552 if (ret < 0) {
553 return ret;
554 }
555 out->ops->write(out, &out->crc32, 4);
556 if (ret < 0) {
557 return ret;
558 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700559
Jerry Zhang7b444f02018-06-12 16:42:09 -0700560 out->chunk_cnt++;
561 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700562
Jerry Zhang7b444f02018-06-12 16:42:09 -0700563 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700564}
565
566static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700567 .write_data_chunk = write_sparse_data_chunk,
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700568 .write_fd_chunk = write_sparse_fd_chunk,
Jerry Zhang7b444f02018-06-12 16:42:09 -0700569 .write_fill_chunk = write_sparse_fill_chunk,
570 .write_skip_chunk = write_sparse_skip_chunk,
571 .write_end_chunk = write_sparse_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700572};
573
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700574static int write_normal_data_chunk_variant(struct output_file* out, unsigned int len,
575 handle_data data, bool isFd) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700576 int ret;
577 unsigned int rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700578
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700579 if (isFd) {
580 ret = out->ops->write_fd(out, data.fd, len);
581 } else {
582 ret = out->ops->write(out, data.data_ptr, len);
583 }
Jerry Zhang7b444f02018-06-12 16:42:09 -0700584 if (ret < 0) {
585 return ret;
586 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700587
Jerry Zhang7b444f02018-06-12 16:42:09 -0700588 if (rnd_up_len > len) {
589 ret = out->ops->skip(out, rnd_up_len - len);
590 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700591
Jerry Zhang7b444f02018-06-12 16:42:09 -0700592 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700593}
594
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700595static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data_ptr) {
596 handle_data data = {data_ptr};
597 return write_normal_data_chunk_variant(out, len, data, false /* isFd */);
598}
599
600static int write_normal_fd_chunk(struct output_file* out, unsigned int len, int fd) {
601 handle_data data = {.fd = fd};
602 return write_normal_data_chunk_variant(out, len, data, true /* isFd */);
603}
604
Jerry Zhang7b444f02018-06-12 16:42:09 -0700605static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700606 return out->ops->write_fill(out, fill_val, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700607}
608
Jerry Zhang7b444f02018-06-12 16:42:09 -0700609static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
610 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700611}
612
Jerry Zhang7b444f02018-06-12 16:42:09 -0700613int write_normal_end_chunk(struct output_file* out) {
614 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700615}
616
617static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700618 .write_data_chunk = write_normal_data_chunk,
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700619 .write_fd_chunk = write_normal_fd_chunk,
Jerry Zhang7b444f02018-06-12 16:42:09 -0700620 .write_fill_chunk = write_normal_fill_chunk,
621 .write_skip_chunk = write_normal_skip_chunk,
622 .write_end_chunk = write_normal_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700623};
624
Jerry Zhang7b444f02018-06-12 16:42:09 -0700625void output_file_close(struct output_file* out) {
626 out->sparse_ops->write_end_chunk(out);
627 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700628}
629
Jerry Zhang7b444f02018-06-12 16:42:09 -0700630static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
631 int chunks, bool crc) {
632 int ret;
Colin Crossb4cd2672012-05-18 14:49:50 -0700633
Jerry Zhang7b444f02018-06-12 16:42:09 -0700634 out->len = len;
635 out->block_size = block_size;
636 out->cur_out_ptr = 0ll;
637 out->chunk_cnt = 0;
638 out->crc32 = 0;
639 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700640
Jerry Zhang7b444f02018-06-12 16:42:09 -0700641 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
642 if (!out->zero_buf) {
643 error_errno("malloc zero_buf");
644 return -ENOMEM;
645 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700646
Jerry Zhang7b444f02018-06-12 16:42:09 -0700647 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
648 if (!out->fill_buf) {
649 error_errno("malloc fill_buf");
650 ret = -ENOMEM;
651 goto err_fill_buf;
652 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700653
Jerry Zhang7b444f02018-06-12 16:42:09 -0700654 if (sparse) {
655 out->sparse_ops = &sparse_file_ops;
656 } else {
657 out->sparse_ops = &normal_file_ops;
658 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700659
Jerry Zhang7b444f02018-06-12 16:42:09 -0700660 if (sparse) {
661 sparse_header_t sparse_header = {
662 .magic = SPARSE_HEADER_MAGIC,
663 .major_version = SPARSE_HEADER_MAJOR_VER,
664 .minor_version = SPARSE_HEADER_MINOR_VER,
665 .file_hdr_sz = SPARSE_HEADER_LEN,
666 .chunk_hdr_sz = CHUNK_HEADER_LEN,
667 .blk_sz = out->block_size,
668 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
669 .total_chunks = static_cast<unsigned>(chunks),
670 .image_checksum = 0};
Colin Cross28fa5bc2012-05-20 13:28:05 -0700671
Jerry Zhang7b444f02018-06-12 16:42:09 -0700672 if (out->use_crc) {
673 sparse_header.total_chunks++;
674 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700675
Jerry Zhang7b444f02018-06-12 16:42:09 -0700676 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
677 if (ret < 0) {
678 goto err_write;
679 }
680 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700681
Jerry Zhang7b444f02018-06-12 16:42:09 -0700682 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700683
684err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700685 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700686err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700687 free(out->zero_buf);
688 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700689}
690
Jerry Zhang7b444f02018-06-12 16:42:09 -0700691static struct output_file* output_file_new_gz(void) {
692 struct output_file_gz* outgz =
693 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
694 if (!outgz) {
695 error_errno("malloc struct outgz");
Yi Kong17ba95e2018-07-23 16:31:11 -0700696 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700697 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700698
Jerry Zhang7b444f02018-06-12 16:42:09 -0700699 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700700
Jerry Zhang7b444f02018-06-12 16:42:09 -0700701 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700702}
703
Jerry Zhang7b444f02018-06-12 16:42:09 -0700704static struct output_file* output_file_new_normal(void) {
705 struct output_file_normal* outn =
706 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
707 if (!outn) {
708 error_errno("malloc struct outn");
Yi Kong17ba95e2018-07-23 16:31:11 -0700709 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700710 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700711
Jerry Zhang7b444f02018-06-12 16:42:09 -0700712 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700713
Jerry Zhang7b444f02018-06-12 16:42:09 -0700714 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700715}
716
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700717struct output_file* output_file_open_callback(int (*data_write)(void*, const void*, size_t),
718 int (*fd_write)(void*, int, size_t),
719 int (*fill_write)(void*, uint32_t, size_t),
720 int (*skip_write)(void*, off64_t), void* priv,
721 unsigned int block_size, int64_t len, int sparse,
722 int chunks, int crc) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700723 int ret;
724 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700725
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700726 if (!data_write || (crc && (fd_write || fill_write))) {
727 errno = EINVAL;
728 return nullptr;
729 }
730
Jerry Zhang7b444f02018-06-12 16:42:09 -0700731 outc =
732 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
733 if (!outc) {
734 error_errno("malloc struct outc");
Yi Kong17ba95e2018-07-23 16:31:11 -0700735 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700736 }
Colin Cross1e17b312012-05-21 16:35:45 -0700737
Jerry Zhang7b444f02018-06-12 16:42:09 -0700738 outc->out.ops = &callback_file_ops;
739 outc->priv = priv;
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700740 outc->data_write = data_write;
741 outc->fd_write = fd_write;
742 outc->fill_write = fill_write;
743 outc->skip_write = skip_write;
Colin Cross1e17b312012-05-21 16:35:45 -0700744
Jerry Zhang7b444f02018-06-12 16:42:09 -0700745 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
746 if (ret < 0) {
747 free(outc);
Yi Kong17ba95e2018-07-23 16:31:11 -0700748 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700749 }
Colin Cross1e17b312012-05-21 16:35:45 -0700750
Jerry Zhang7b444f02018-06-12 16:42:09 -0700751 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700752}
753
Jerry Zhang7b444f02018-06-12 16:42:09 -0700754struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
755 int sparse, int chunks, int crc) {
756 int ret;
757 struct output_file* out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700758
Jerry Zhang7b444f02018-06-12 16:42:09 -0700759 if (gz) {
760 out = output_file_new_gz();
761 } else {
762 out = output_file_new_normal();
763 }
764 if (!out) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700765 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700766 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700767
Jerry Zhang7b444f02018-06-12 16:42:09 -0700768 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700769
Jerry Zhang7b444f02018-06-12 16:42:09 -0700770 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
771 if (ret < 0) {
772 free(out);
Yi Kong17ba95e2018-07-23 16:31:11 -0700773 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700774 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700775
Jerry Zhang7b444f02018-06-12 16:42:09 -0700776 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700777}
778
Colin Cross28fa5bc2012-05-20 13:28:05 -0700779/* Write a contiguous region of data blocks from a memory buffer */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700780int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
781 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700782}
783
784/* Write a contiguous region of data blocks with a fill value */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700785int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
786 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700787}
788
Jerry Zhang7b444f02018-06-12 16:42:09 -0700789int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
Jerry Zhangdb69f0d2018-06-14 16:58:58 -0700790 lseek64(fd, offset, SEEK_SET);
791 return out->sparse_ops->write_fd_chunk(out, len, fd);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700792}
793
794/* Write a contiguous region of data blocks from a file */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700795int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
796 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700797
Jerry Zhang7b444f02018-06-12 16:42:09 -0700798 int file_fd = open(file, O_RDONLY | O_BINARY);
799 if (file_fd < 0) {
800 return -errno;
801 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700802
Jerry Zhang7b444f02018-06-12 16:42:09 -0700803 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700804
Jerry Zhang7b444f02018-06-12 16:42:09 -0700805 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700806
Jerry Zhang7b444f02018-06-12 16:42:09 -0700807 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700808}
809
Jerry Zhang7b444f02018-06-12 16:42:09 -0700810int write_skip_chunk(struct output_file* out, int64_t len) {
811 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700812}