blob: 5388e77423f1ebe8f73e28c833ff75385e031ad7 [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
Mark Salyzyn031a7482014-02-27 16:56:15 -080032#include "defs.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070033#include "output_file.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070034#include "sparse_crc32.h"
Mark Salyzyn8116c8c2014-05-01 09:15:02 -070035#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070036
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070037#ifndef _WIN32
Colin Cross28fa5bc2012-05-20 13:28:05 -070038#include <sys/mman.h>
39#define O_BINARY 0
Colin Crossb4cd2672012-05-18 14:49:50 -070040#else
41#define ftruncate64 ftruncate
Colin Cross28fa5bc2012-05-20 13:28:05 -070042#endif
43
44#if defined(__APPLE__) && defined(__MACH__)
45#define lseek64 lseek
46#define ftruncate64 ftruncate
47#define mmap64 mmap
48#define off64_t off_t
49#endif
50
Jerry Zhang7b444f02018-06-12 16:42:09 -070051#define min(a, b) \
52 ({ \
53 typeof(a) _a = (a); \
54 typeof(b) _b = (b); \
55 (_a < _b) ? _a : _b; \
56 })
Colin Crossb55dcee2012-04-24 23:07:49 -070057
Colin Cross28fa5bc2012-05-20 13:28:05 -070058#define SPARSE_HEADER_MAJOR_VER 1
59#define SPARSE_HEADER_MINOR_VER 0
Jerry Zhang7b444f02018-06-12 16:42:09 -070060#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
Colin Cross28fa5bc2012-05-20 13:28:05 -070061#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
62
Jerry Zhang7b444f02018-06-12 16:42:09 -070063#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
Colin Crossb4cd2672012-05-18 14:49:50 -070064
Colin Cross28fa5bc2012-05-20 13:28:05 -070065struct output_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070066 int (*open)(struct output_file*, int fd);
67 int (*skip)(struct output_file*, int64_t);
68 int (*pad)(struct output_file*, int64_t);
69 int (*write)(struct output_file*, void*, size_t);
70 void (*close)(struct output_file*);
Colin Cross28fa5bc2012-05-20 13:28:05 -070071};
72
Colin Crossb55dcee2012-04-24 23:07:49 -070073struct sparse_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070074 int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
75 int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
76 int (*write_skip_chunk)(struct output_file* out, int64_t len);
77 int (*write_end_chunk)(struct output_file* out);
Colin Crossb55dcee2012-04-24 23:07:49 -070078};
79
Colin Cross28fa5bc2012-05-20 13:28:05 -070080struct output_file {
Jerry Zhang7b444f02018-06-12 16:42:09 -070081 int64_t cur_out_ptr;
82 unsigned int chunk_cnt;
83 uint32_t crc32;
84 struct output_file_ops* ops;
85 struct sparse_file_ops* sparse_ops;
86 int use_crc;
87 unsigned int block_size;
88 int64_t len;
89 char* zero_buf;
90 uint32_t* fill_buf;
91 char* buf;
Colin Cross28fa5bc2012-05-20 13:28:05 -070092};
93
Colin Crossb4cd2672012-05-18 14:49:50 -070094struct output_file_gz {
Jerry Zhang7b444f02018-06-12 16:42:09 -070095 struct output_file out;
96 gzFile gz_fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070097};
98
Jerry Zhang7b444f02018-06-12 16:42:09 -070099#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700100
101struct output_file_normal {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700102 struct output_file out;
103 int fd;
Colin Crossb4cd2672012-05-18 14:49:50 -0700104};
105
Jerry Zhang7b444f02018-06-12 16:42:09 -0700106#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700107
Colin Cross1e17b312012-05-21 16:35:45 -0700108struct output_file_callback {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700109 struct output_file out;
110 void* priv;
111 int (*write)(void* priv, const void* buf, size_t len);
Colin Cross1e17b312012-05-21 16:35:45 -0700112};
113
Jerry Zhang7b444f02018-06-12 16:42:09 -0700114#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
Colin Cross1e17b312012-05-21 16:35:45 -0700115
Jerry Zhang7b444f02018-06-12 16:42:09 -0700116static int file_open(struct output_file* out, int fd) {
117 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700118
Jerry Zhang7b444f02018-06-12 16:42:09 -0700119 outn->fd = fd;
120 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700121}
122
Jerry Zhang7b444f02018-06-12 16:42:09 -0700123static int file_skip(struct output_file* out, int64_t cnt) {
124 off64_t ret;
125 struct output_file_normal* outn = to_output_file_normal(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700126
Jerry Zhang7b444f02018-06-12 16:42:09 -0700127 ret = lseek64(outn->fd, cnt, SEEK_CUR);
128 if (ret < 0) {
129 error_errno("lseek64");
130 return -1;
131 }
132 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700133}
134
Jerry Zhang7b444f02018-06-12 16:42:09 -0700135static int file_pad(struct output_file* out, int64_t len) {
136 int ret;
137 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700138
Jerry Zhang7b444f02018-06-12 16:42:09 -0700139 ret = ftruncate64(outn->fd, len);
140 if (ret < 0) {
141 return -errno;
142 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700143
Jerry Zhang7b444f02018-06-12 16:42:09 -0700144 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700145}
146
Jerry Zhang7b444f02018-06-12 16:42:09 -0700147static int file_write(struct output_file* out, void* data, size_t len) {
148 ssize_t ret;
149 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700150
Jerry Zhang7b444f02018-06-12 16:42:09 -0700151 while (len > 0) {
152 ret = write(outn->fd, data, len);
153 if (ret < 0) {
154 if (errno == EINTR) {
155 continue;
156 }
157 error_errno("write");
158 return -1;
159 }
Jeremy Compostellafca594c2016-09-30 14:54:25 +0200160
Jerry Zhang7b444f02018-06-12 16:42:09 -0700161 data = (char*)data + ret;
162 len -= ret;
163 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700164
Jerry Zhang7b444f02018-06-12 16:42:09 -0700165 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700166}
167
Jerry Zhang7b444f02018-06-12 16:42:09 -0700168static void file_close(struct output_file* out) {
169 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700170
Jerry Zhang7b444f02018-06-12 16:42:09 -0700171 free(outn);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700172}
173
Colin Cross28fa5bc2012-05-20 13:28:05 -0700174static struct output_file_ops file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700175 .open = file_open,
176 .skip = file_skip,
177 .pad = file_pad,
178 .write = file_write,
179 .close = file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700180};
181
Jerry Zhang7b444f02018-06-12 16:42:09 -0700182static int gz_file_open(struct output_file* out, int fd) {
183 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700184
Jerry Zhang7b444f02018-06-12 16:42:09 -0700185 outgz->gz_fd = gzdopen(fd, "wb9");
186 if (!outgz->gz_fd) {
187 error_errno("gzopen");
188 return -errno;
189 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700190
Jerry Zhang7b444f02018-06-12 16:42:09 -0700191 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700192}
193
Jerry Zhang7b444f02018-06-12 16:42:09 -0700194static int gz_file_skip(struct output_file* out, int64_t cnt) {
195 off64_t ret;
196 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700197
Jerry Zhang7b444f02018-06-12 16:42:09 -0700198 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
199 if (ret < 0) {
200 error_errno("gzseek");
201 return -1;
202 }
203 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700204}
205
Jerry Zhang7b444f02018-06-12 16:42:09 -0700206static int gz_file_pad(struct output_file* out, int64_t len) {
207 off64_t ret;
208 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700209
Jerry Zhang7b444f02018-06-12 16:42:09 -0700210 ret = gztell(outgz->gz_fd);
211 if (ret < 0) {
212 return -1;
213 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700214
Jerry Zhang7b444f02018-06-12 16:42:09 -0700215 if (ret >= len) {
216 return 0;
217 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700218
Jerry Zhang7b444f02018-06-12 16:42:09 -0700219 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
220 if (ret < 0) {
221 return -1;
222 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700223
Jerry Zhang7b444f02018-06-12 16:42:09 -0700224 gzwrite(outgz->gz_fd, "", 1);
Colin Crossb4cd2672012-05-18 14:49:50 -0700225
Jerry Zhang7b444f02018-06-12 16:42:09 -0700226 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700227}
228
Jerry Zhang7b444f02018-06-12 16:42:09 -0700229static int gz_file_write(struct output_file* out, void* data, size_t len) {
230 int ret;
231 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700232
Jerry Zhang7b444f02018-06-12 16:42:09 -0700233 while (len > 0) {
234 ret = gzwrite(outgz->gz_fd, data, min(len, (unsigned int)INT_MAX));
235 if (ret == 0) {
236 error("gzwrite %s", gzerror(outgz->gz_fd, NULL));
237 return -1;
238 }
239 len -= ret;
240 data = (char*)data + ret;
241 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700242
Jerry Zhang7b444f02018-06-12 16:42:09 -0700243 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700244}
245
Jerry Zhang7b444f02018-06-12 16:42:09 -0700246static void gz_file_close(struct output_file* out) {
247 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700248
Jerry Zhang7b444f02018-06-12 16:42:09 -0700249 gzclose(outgz->gz_fd);
250 free(outgz);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700251}
252
253static struct output_file_ops gz_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700254 .open = gz_file_open,
255 .skip = gz_file_skip,
256 .pad = gz_file_pad,
257 .write = gz_file_write,
258 .close = gz_file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700259};
260
Jerry Zhang7b444f02018-06-12 16:42:09 -0700261static int callback_file_open(struct output_file* out __unused, int fd __unused) {
262 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700263}
264
Jerry Zhang7b444f02018-06-12 16:42:09 -0700265static int callback_file_skip(struct output_file* out, int64_t off) {
266 struct output_file_callback* outc = to_output_file_callback(out);
267 int to_write;
268 int ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700269
Jerry Zhang7b444f02018-06-12 16:42:09 -0700270 while (off > 0) {
271 to_write = min(off, (int64_t)INT_MAX);
272 ret = outc->write(outc->priv, NULL, to_write);
273 if (ret < 0) {
274 return ret;
275 }
276 off -= to_write;
277 }
Colin Cross1e17b312012-05-21 16:35:45 -0700278
Jerry Zhang7b444f02018-06-12 16:42:09 -0700279 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700280}
281
Jerry Zhang7b444f02018-06-12 16:42:09 -0700282static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
283 return -1;
Colin Cross1e17b312012-05-21 16:35:45 -0700284}
285
Jerry Zhang7b444f02018-06-12 16:42:09 -0700286static int callback_file_write(struct output_file* out, void* data, size_t len) {
287 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700288
Jerry Zhang7b444f02018-06-12 16:42:09 -0700289 return outc->write(outc->priv, data, len);
Colin Cross1e17b312012-05-21 16:35:45 -0700290}
291
Jerry Zhang7b444f02018-06-12 16:42:09 -0700292static void callback_file_close(struct output_file* out) {
293 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700294
Jerry Zhang7b444f02018-06-12 16:42:09 -0700295 free(outc);
Colin Cross1e17b312012-05-21 16:35:45 -0700296}
297
298static struct output_file_ops callback_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700299 .open = callback_file_open,
300 .skip = callback_file_skip,
301 .pad = callback_file_pad,
302 .write = callback_file_write,
303 .close = callback_file_close,
Colin Cross1e17b312012-05-21 16:35:45 -0700304};
305
Jerry Zhang7b444f02018-06-12 16:42:09 -0700306int read_all(int fd, void* buf, size_t len) {
307 size_t total = 0;
308 int ret;
309 char* ptr = reinterpret_cast<char*>(buf);
Colin Cross13a56062012-06-19 16:45:48 -0700310
Jerry Zhang7b444f02018-06-12 16:42:09 -0700311 while (total < len) {
312 ret = read(fd, ptr, len - total);
Colin Cross13a56062012-06-19 16:45:48 -0700313
Jerry Zhang7b444f02018-06-12 16:42:09 -0700314 if (ret < 0) return -errno;
Colin Cross13a56062012-06-19 16:45:48 -0700315
Jerry Zhang7b444f02018-06-12 16:42:09 -0700316 if (ret == 0) return -EINVAL;
Colin Cross13a56062012-06-19 16:45:48 -0700317
Jerry Zhang7b444f02018-06-12 16:42:09 -0700318 ptr += ret;
319 total += ret;
320 }
Colin Cross13a56062012-06-19 16:45:48 -0700321
Jerry Zhang7b444f02018-06-12 16:42:09 -0700322 return 0;
Colin Cross13a56062012-06-19 16:45:48 -0700323}
324
Jerry Zhang7b444f02018-06-12 16:42:09 -0700325static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
326 chunk_header_t chunk_header;
327 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700328
Jerry Zhang7b444f02018-06-12 16:42:09 -0700329 if (skip_len % out->block_size) {
330 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
331 out->block_size);
332 return -1;
333 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700334
Jerry Zhang7b444f02018-06-12 16:42:09 -0700335 /* We are skipping data, so emit a don't care chunk. */
336 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
337 chunk_header.reserved1 = 0;
338 chunk_header.chunk_sz = skip_len / out->block_size;
339 chunk_header.total_sz = CHUNK_HEADER_LEN;
340 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
341 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700342
Jerry Zhang7b444f02018-06-12 16:42:09 -0700343 out->cur_out_ptr += skip_len;
344 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700345
Jerry Zhang7b444f02018-06-12 16:42:09 -0700346 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700347}
348
Jerry Zhang7b444f02018-06-12 16:42:09 -0700349static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
350 chunk_header_t chunk_header;
351 int rnd_up_len, count;
352 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700353
Jerry Zhang7b444f02018-06-12 16:42:09 -0700354 /* Round up the fill length to a multiple of the block size */
355 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700356
Jerry Zhang7b444f02018-06-12 16:42:09 -0700357 /* Finally we can safely emit a chunk of data */
358 chunk_header.chunk_type = CHUNK_TYPE_FILL;
359 chunk_header.reserved1 = 0;
360 chunk_header.chunk_sz = rnd_up_len / out->block_size;
361 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
362 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700363
Jerry Zhang7b444f02018-06-12 16:42:09 -0700364 if (ret < 0) return -1;
365 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
366 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700367
Jerry Zhang7b444f02018-06-12 16:42:09 -0700368 if (out->use_crc) {
369 count = out->block_size / sizeof(uint32_t);
370 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
371 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700372
Jerry Zhang7b444f02018-06-12 16:42:09 -0700373 out->cur_out_ptr += rnd_up_len;
374 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700375
Jerry Zhang7b444f02018-06-12 16:42:09 -0700376 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700377}
378
Jerry Zhang7b444f02018-06-12 16:42:09 -0700379static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data) {
380 chunk_header_t chunk_header;
381 int rnd_up_len, zero_len;
382 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700383
Jerry Zhang7b444f02018-06-12 16:42:09 -0700384 /* Round up the data length to a multiple of the block size */
385 rnd_up_len = ALIGN(len, out->block_size);
386 zero_len = rnd_up_len - len;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700387
Jerry Zhang7b444f02018-06-12 16:42:09 -0700388 /* Finally we can safely emit a chunk of data */
389 chunk_header.chunk_type = CHUNK_TYPE_RAW;
390 chunk_header.reserved1 = 0;
391 chunk_header.chunk_sz = rnd_up_len / out->block_size;
392 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
393 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700394
Jerry Zhang7b444f02018-06-12 16:42:09 -0700395 if (ret < 0) return -1;
396 ret = out->ops->write(out, data, len);
397 if (ret < 0) return -1;
398 if (zero_len) {
399 ret = out->ops->write(out, out->zero_buf, zero_len);
400 if (ret < 0) return -1;
401 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700402
Jerry Zhang7b444f02018-06-12 16:42:09 -0700403 if (out->use_crc) {
404 out->crc32 = sparse_crc32(out->crc32, data, len);
405 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
406 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700407
Jerry Zhang7b444f02018-06-12 16:42:09 -0700408 out->cur_out_ptr += rnd_up_len;
409 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700410
Jerry Zhang7b444f02018-06-12 16:42:09 -0700411 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700412}
413
Jerry Zhang7b444f02018-06-12 16:42:09 -0700414int write_sparse_end_chunk(struct output_file* out) {
415 chunk_header_t chunk_header;
416 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700417
Jerry Zhang7b444f02018-06-12 16:42:09 -0700418 if (out->use_crc) {
419 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
420 chunk_header.reserved1 = 0;
421 chunk_header.chunk_sz = 0;
422 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
Colin Crossb55dcee2012-04-24 23:07:49 -0700423
Jerry Zhang7b444f02018-06-12 16:42:09 -0700424 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
425 if (ret < 0) {
426 return ret;
427 }
428 out->ops->write(out, &out->crc32, 4);
429 if (ret < 0) {
430 return ret;
431 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700432
Jerry Zhang7b444f02018-06-12 16:42:09 -0700433 out->chunk_cnt++;
434 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700435
Jerry Zhang7b444f02018-06-12 16:42:09 -0700436 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700437}
438
439static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700440 .write_data_chunk = write_sparse_data_chunk,
441 .write_fill_chunk = write_sparse_fill_chunk,
442 .write_skip_chunk = write_sparse_skip_chunk,
443 .write_end_chunk = write_sparse_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700444};
445
Jerry Zhang7b444f02018-06-12 16:42:09 -0700446static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data) {
447 int ret;
448 unsigned int rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700449
Jerry Zhang7b444f02018-06-12 16:42:09 -0700450 ret = out->ops->write(out, data, len);
451 if (ret < 0) {
452 return ret;
453 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700454
Jerry Zhang7b444f02018-06-12 16:42:09 -0700455 if (rnd_up_len > len) {
456 ret = out->ops->skip(out, rnd_up_len - len);
457 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700458
Jerry Zhang7b444f02018-06-12 16:42:09 -0700459 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700460}
461
Jerry Zhang7b444f02018-06-12 16:42:09 -0700462static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
463 int ret;
464 unsigned int i;
465 unsigned int write_len;
Colin Crossb55dcee2012-04-24 23:07:49 -0700466
Jerry Zhang7b444f02018-06-12 16:42:09 -0700467 /* Initialize fill_buf with the fill_val */
468 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
469 out->fill_buf[i] = fill_val;
470 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700471
Jerry Zhang7b444f02018-06-12 16:42:09 -0700472 while (len) {
473 write_len = min(len, out->block_size);
474 ret = out->ops->write(out, out->fill_buf, write_len);
475 if (ret < 0) {
476 return ret;
477 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700478
Jerry Zhang7b444f02018-06-12 16:42:09 -0700479 len -= write_len;
480 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700481
Jerry Zhang7b444f02018-06-12 16:42:09 -0700482 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700483}
484
Jerry Zhang7b444f02018-06-12 16:42:09 -0700485static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
486 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700487}
488
Jerry Zhang7b444f02018-06-12 16:42:09 -0700489int write_normal_end_chunk(struct output_file* out) {
490 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700491}
492
493static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700494 .write_data_chunk = write_normal_data_chunk,
495 .write_fill_chunk = write_normal_fill_chunk,
496 .write_skip_chunk = write_normal_skip_chunk,
497 .write_end_chunk = write_normal_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700498};
499
Jerry Zhang7b444f02018-06-12 16:42:09 -0700500void output_file_close(struct output_file* out) {
501 out->sparse_ops->write_end_chunk(out);
502 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700503}
504
Jerry Zhang7b444f02018-06-12 16:42:09 -0700505static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
506 int chunks, bool crc) {
507 int ret;
Colin Crossb4cd2672012-05-18 14:49:50 -0700508
Jerry Zhang7b444f02018-06-12 16:42:09 -0700509 out->len = len;
510 out->block_size = block_size;
511 out->cur_out_ptr = 0ll;
512 out->chunk_cnt = 0;
513 out->crc32 = 0;
514 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700515
Jerry Zhang7b444f02018-06-12 16:42:09 -0700516 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
517 if (!out->zero_buf) {
518 error_errno("malloc zero_buf");
519 return -ENOMEM;
520 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700521
Jerry Zhang7b444f02018-06-12 16:42:09 -0700522 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
523 if (!out->fill_buf) {
524 error_errno("malloc fill_buf");
525 ret = -ENOMEM;
526 goto err_fill_buf;
527 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700528
Jerry Zhang7b444f02018-06-12 16:42:09 -0700529 if (sparse) {
530 out->sparse_ops = &sparse_file_ops;
531 } else {
532 out->sparse_ops = &normal_file_ops;
533 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700534
Jerry Zhang7b444f02018-06-12 16:42:09 -0700535 if (sparse) {
536 sparse_header_t sparse_header = {
537 .magic = SPARSE_HEADER_MAGIC,
538 .major_version = SPARSE_HEADER_MAJOR_VER,
539 .minor_version = SPARSE_HEADER_MINOR_VER,
540 .file_hdr_sz = SPARSE_HEADER_LEN,
541 .chunk_hdr_sz = CHUNK_HEADER_LEN,
542 .blk_sz = out->block_size,
543 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
544 .total_chunks = static_cast<unsigned>(chunks),
545 .image_checksum = 0};
Colin Cross28fa5bc2012-05-20 13:28:05 -0700546
Jerry Zhang7b444f02018-06-12 16:42:09 -0700547 if (out->use_crc) {
548 sparse_header.total_chunks++;
549 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700550
Jerry Zhang7b444f02018-06-12 16:42:09 -0700551 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
552 if (ret < 0) {
553 goto err_write;
554 }
555 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700556
Jerry Zhang7b444f02018-06-12 16:42:09 -0700557 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700558
559err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700560 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700561err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700562 free(out->zero_buf);
563 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700564}
565
Jerry Zhang7b444f02018-06-12 16:42:09 -0700566static struct output_file* output_file_new_gz(void) {
567 struct output_file_gz* outgz =
568 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
569 if (!outgz) {
570 error_errno("malloc struct outgz");
571 return NULL;
572 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700573
Jerry Zhang7b444f02018-06-12 16:42:09 -0700574 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700575
Jerry Zhang7b444f02018-06-12 16:42:09 -0700576 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700577}
578
Jerry Zhang7b444f02018-06-12 16:42:09 -0700579static struct output_file* output_file_new_normal(void) {
580 struct output_file_normal* outn =
581 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
582 if (!outn) {
583 error_errno("malloc struct outn");
584 return NULL;
585 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700586
Jerry Zhang7b444f02018-06-12 16:42:09 -0700587 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700588
Jerry Zhang7b444f02018-06-12 16:42:09 -0700589 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700590}
591
Jerry Zhang7b444f02018-06-12 16:42:09 -0700592struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
593 unsigned int block_size, int64_t len, int gz __unused,
594 int sparse, int chunks, int crc) {
595 int ret;
596 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700597
Jerry Zhang7b444f02018-06-12 16:42:09 -0700598 outc =
599 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
600 if (!outc) {
601 error_errno("malloc struct outc");
602 return NULL;
603 }
Colin Cross1e17b312012-05-21 16:35:45 -0700604
Jerry Zhang7b444f02018-06-12 16:42:09 -0700605 outc->out.ops = &callback_file_ops;
606 outc->priv = priv;
607 outc->write = write;
Colin Cross1e17b312012-05-21 16:35:45 -0700608
Jerry Zhang7b444f02018-06-12 16:42:09 -0700609 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
610 if (ret < 0) {
611 free(outc);
612 return NULL;
613 }
Colin Cross1e17b312012-05-21 16:35:45 -0700614
Jerry Zhang7b444f02018-06-12 16:42:09 -0700615 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700616}
617
Jerry Zhang7b444f02018-06-12 16:42:09 -0700618struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
619 int sparse, int chunks, int crc) {
620 int ret;
621 struct output_file* out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700622
Jerry Zhang7b444f02018-06-12 16:42:09 -0700623 if (gz) {
624 out = output_file_new_gz();
625 } else {
626 out = output_file_new_normal();
627 }
628 if (!out) {
629 return NULL;
630 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700631
Jerry Zhang7b444f02018-06-12 16:42:09 -0700632 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700633
Jerry Zhang7b444f02018-06-12 16:42:09 -0700634 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
635 if (ret < 0) {
636 free(out);
637 return NULL;
638 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700639
Jerry Zhang7b444f02018-06-12 16:42:09 -0700640 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700641}
642
Colin Cross28fa5bc2012-05-20 13:28:05 -0700643/* Write a contiguous region of data blocks from a memory buffer */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700644int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
645 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700646}
647
648/* Write a contiguous region of data blocks with a fill value */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700649int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
650 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700651}
652
Jerry Zhang7b444f02018-06-12 16:42:09 -0700653int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
654 int ret;
655 int64_t aligned_offset;
656 int aligned_diff;
657 uint64_t buffer_size;
658 char* ptr;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700659
Jerry Zhang7b444f02018-06-12 16:42:09 -0700660 aligned_offset = offset & ~(4096 - 1);
661 aligned_diff = offset - aligned_offset;
662 buffer_size = (uint64_t)len + (uint64_t)aligned_diff;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700663
Elliott Hughes34a4f0b2016-10-05 09:37:18 -0700664#ifndef _WIN32
Jerry Zhang7b444f02018-06-12 16:42:09 -0700665 if (buffer_size > SIZE_MAX) return -E2BIG;
666 char* data =
667 reinterpret_cast<char*>(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
668 if (data == MAP_FAILED) {
669 return -errno;
670 }
671 ptr = data + aligned_diff;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700672#else
Jerry Zhang7b444f02018-06-12 16:42:09 -0700673 off64_t pos;
674 char* data = reinterpret_cast<char*>(malloc(len));
675 if (!data) {
676 return -errno;
677 }
678 pos = lseek64(fd, offset, SEEK_SET);
679 if (pos < 0) {
680 free(data);
681 return -errno;
682 }
683 ret = read_all(fd, data, len);
684 if (ret < 0) {
685 free(data);
686 return ret;
687 }
688 ptr = data;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700689#endif
690
Jerry Zhang7b444f02018-06-12 16:42:09 -0700691 ret = out->sparse_ops->write_data_chunk(out, len, ptr);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700692
Elliott Hughes34a4f0b2016-10-05 09:37:18 -0700693#ifndef _WIN32
Jerry Zhang7b444f02018-06-12 16:42:09 -0700694 munmap(data, buffer_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700695#else
Jerry Zhang7b444f02018-06-12 16:42:09 -0700696 free(data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700697#endif
Colin Cross9e1f17e2012-04-25 18:31:39 -0700698
Jerry Zhang7b444f02018-06-12 16:42:09 -0700699 return ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700700}
701
702/* Write a contiguous region of data blocks from a file */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700703int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
704 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700705
Jerry Zhang7b444f02018-06-12 16:42:09 -0700706 int file_fd = open(file, O_RDONLY | O_BINARY);
707 if (file_fd < 0) {
708 return -errno;
709 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700710
Jerry Zhang7b444f02018-06-12 16:42:09 -0700711 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700712
Jerry Zhang7b444f02018-06-12 16:42:09 -0700713 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700714
Jerry Zhang7b444f02018-06-12 16:42:09 -0700715 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700716}
717
Jerry Zhang7b444f02018-06-12 16:42:09 -0700718int write_skip_chunk(struct output_file* out, int64_t len) {
719 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700720}