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