• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FILL_ZERO_BUFSIZE (2 * 1024 * 1024)
58 
59 #define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
60 
61 struct output_file_ops {
62   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*);
67 };
68 
69 struct sparse_file_ops {
70   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);
73   int (*write_end_chunk)(struct output_file* out);
74 };
75 
76 struct output_file {
77   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;
88 };
89 
90 struct output_file_gz {
91   struct output_file out;
92   gzFile gz_fd;
93 };
94 
95 #define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
96 
97 struct output_file_normal {
98   struct output_file out;
99   int fd;
100 };
101 
102 #define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
103 
104 struct output_file_callback {
105   struct output_file out;
106   void* priv;
107   int (*write)(void* priv, const void* buf, size_t len);
108 };
109 
110 #define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
111 
file_open(struct output_file * out,int fd)112 static int file_open(struct output_file* out, int fd) {
113   struct output_file_normal* outn = to_output_file_normal(out);
114 
115   outn->fd = fd;
116   return 0;
117 }
118 
file_skip(struct output_file * out,int64_t cnt)119 static 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);
122 
123   ret = lseek64(outn->fd, cnt, SEEK_CUR);
124   if (ret < 0) {
125     error_errno("lseek64");
126     return -1;
127   }
128   return 0;
129 }
130 
file_pad(struct output_file * out,int64_t len)131 static int file_pad(struct output_file* out, int64_t len) {
132   int ret;
133   struct output_file_normal* outn = to_output_file_normal(out);
134 
135   ret = ftruncate64(outn->fd, len);
136   if (ret < 0) {
137     return -errno;
138   }
139 
140   return 0;
141 }
142 
file_write(struct output_file * out,void * data,size_t len)143 static 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);
146 
147   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     }
156 
157     data = (char*)data + ret;
158     len -= ret;
159   }
160 
161   return 0;
162 }
163 
file_close(struct output_file * out)164 static void file_close(struct output_file* out) {
165   struct output_file_normal* outn = to_output_file_normal(out);
166 
167   free(outn);
168 }
169 
170 static struct output_file_ops file_ops = {
171     .open = file_open,
172     .skip = file_skip,
173     .pad = file_pad,
174     .write = file_write,
175     .close = file_close,
176 };
177 
gz_file_open(struct output_file * out,int fd)178 static int gz_file_open(struct output_file* out, int fd) {
179   struct output_file_gz* outgz = to_output_file_gz(out);
180 
181   outgz->gz_fd = gzdopen(fd, "wb9");
182   if (!outgz->gz_fd) {
183     error_errno("gzopen");
184     return -errno;
185   }
186 
187   return 0;
188 }
189 
gz_file_skip(struct output_file * out,int64_t cnt)190 static 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);
193 
194   ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
195   if (ret < 0) {
196     error_errno("gzseek");
197     return -1;
198   }
199   return 0;
200 }
201 
gz_file_pad(struct output_file * out,int64_t len)202 static 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);
205 
206   ret = gztell(outgz->gz_fd);
207   if (ret < 0) {
208     return -1;
209   }
210 
211   if (ret >= len) {
212     return 0;
213   }
214 
215   ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
216   if (ret < 0) {
217     return -1;
218   }
219 
220   gzwrite(outgz->gz_fd, "", 1);
221 
222   return 0;
223 }
224 
gz_file_write(struct output_file * out,void * data,size_t len)225 static 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);
228 
229   while (len > 0) {
230     ret = gzwrite(outgz->gz_fd, data, std::min<unsigned int>(len, (unsigned int)INT_MAX));
231     if (ret == 0) {
232       error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
233       return -1;
234     }
235     len -= ret;
236     data = (char*)data + ret;
237   }
238 
239   return 0;
240 }
241 
gz_file_close(struct output_file * out)242 static void gz_file_close(struct output_file* out) {
243   struct output_file_gz* outgz = to_output_file_gz(out);
244 
245   gzclose(outgz->gz_fd);
246   free(outgz);
247 }
248 
249 static struct output_file_ops gz_file_ops = {
250     .open = gz_file_open,
251     .skip = gz_file_skip,
252     .pad = gz_file_pad,
253     .write = gz_file_write,
254     .close = gz_file_close,
255 };
256 
callback_file_open(struct output_file * out __unused,int fd __unused)257 static int callback_file_open(struct output_file* out __unused, int fd __unused) {
258   return 0;
259 }
260 
callback_file_skip(struct output_file * out,int64_t off)261 static 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;
265 
266   while (off > 0) {
267     to_write = std::min(off, (int64_t)INT_MAX);
268     ret = outc->write(outc->priv, nullptr, to_write);
269     if (ret < 0) {
270       return ret;
271     }
272     off -= to_write;
273   }
274 
275   return 0;
276 }
277 
callback_file_pad(struct output_file * out __unused,int64_t len __unused)278 static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
279   return -1;
280 }
281 
callback_file_write(struct output_file * out,void * data,size_t len)282 static int callback_file_write(struct output_file* out, void* data, size_t len) {
283   struct output_file_callback* outc = to_output_file_callback(out);
284 
285   return outc->write(outc->priv, data, len);
286 }
287 
callback_file_close(struct output_file * out)288 static void callback_file_close(struct output_file* out) {
289   struct output_file_callback* outc = to_output_file_callback(out);
290 
291   free(outc);
292 }
293 
294 static struct output_file_ops callback_file_ops = {
295     .open = callback_file_open,
296     .skip = callback_file_skip,
297     .pad = callback_file_pad,
298     .write = callback_file_write,
299     .close = callback_file_close,
300 };
301 
read_all(int fd,void * buf,size_t len)302 int read_all(int fd, void* buf, size_t len) {
303   size_t total = 0;
304   int ret;
305   char* ptr = reinterpret_cast<char*>(buf);
306 
307   while (total < len) {
308     ret = read(fd, ptr, len - total);
309 
310     if (ret < 0) return -errno;
311 
312     if (ret == 0) return -EINVAL;
313 
314     ptr += ret;
315     total += ret;
316   }
317 
318   return 0;
319 }
320 
write_sparse_skip_chunk(struct output_file * out,uint64_t skip_len)321 static int write_sparse_skip_chunk(struct output_file* out, uint64_t skip_len) {
322   chunk_header_t chunk_header;
323   int ret;
324 
325   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   }
330 
331   /* 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;
338 
339   out->cur_out_ptr += skip_len;
340   out->chunk_cnt++;
341 
342   return 0;
343 }
344 
write_sparse_fill_chunk(struct output_file * out,uint64_t len,uint32_t fill_val)345 static int write_sparse_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
346   chunk_header_t chunk_header;
347   uint64_t rnd_up_len;
348   int count;
349   int ret;
350 
351   /* Round up the fill length to a multiple of the block size */
352   rnd_up_len = ALIGN(len, out->block_size);
353 
354   /* 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));
360 
361   if (ret < 0) return -1;
362   ret = out->ops->write(out, &fill_val, sizeof(fill_val));
363   if (ret < 0) return -1;
364 
365   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   }
369 
370   out->cur_out_ptr += rnd_up_len;
371   out->chunk_cnt++;
372 
373   return 0;
374 }
375 
write_sparse_data_chunk(struct output_file * out,uint64_t len,void * data)376 static int write_sparse_data_chunk(struct output_file* out, uint64_t len, void* data) {
377   chunk_header_t chunk_header;
378   uint64_t rnd_up_len, zero_len;
379   int ret;
380 
381   /* 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;
384 
385   /* 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));
391 
392   if (ret < 0) return -1;
393   ret = out->ops->write(out, data, len);
394   if (ret < 0) return -1;
395   if (zero_len) {
396     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     }
406   }
407 
408   if (out->use_crc) {
409     out->crc32 = sparse_crc32(out->crc32, data, len);
410     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     }
419   }
420 
421   out->cur_out_ptr += rnd_up_len;
422   out->chunk_cnt++;
423 
424   return 0;
425 }
426 
write_sparse_end_chunk(struct output_file * out)427 int write_sparse_end_chunk(struct output_file* out) {
428   chunk_header_t chunk_header;
429   int ret;
430 
431   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;
436 
437     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     }
445 
446     out->chunk_cnt++;
447   }
448 
449   return 0;
450 }
451 
452 static struct sparse_file_ops sparse_file_ops = {
453     .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,
457 };
458 
write_normal_data_chunk(struct output_file * out,uint64_t len,void * data)459 static int write_normal_data_chunk(struct output_file* out, uint64_t len, void* data) {
460   int ret;
461   uint64_t rnd_up_len = ALIGN(len, out->block_size);
462 
463   ret = out->ops->write(out, data, len);
464   if (ret < 0) {
465     return ret;
466   }
467 
468   if (rnd_up_len > len) {
469     ret = out->ops->skip(out, rnd_up_len - len);
470   }
471 
472   return ret;
473 }
474 
write_normal_fill_chunk(struct output_file * out,uint64_t len,uint32_t fill_val)475 static int write_normal_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
476   int ret;
477   unsigned int i;
478   uint64_t write_len;
479 
480   /* Initialize fill_buf with the fill_val */
481   for (i = 0; i < FILL_ZERO_BUFSIZE / sizeof(uint32_t); i++) {
482     out->fill_buf[i] = fill_val;
483   }
484 
485   while (len) {
486     write_len = std::min(len, (uint64_t)FILL_ZERO_BUFSIZE);
487     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;
496 }
497 
write_normal_skip_chunk(struct output_file * out,uint64_t len)498 static int write_normal_skip_chunk(struct output_file* out, uint64_t len) {
499   return out->ops->skip(out, len);
500 }
501 
write_normal_end_chunk(struct output_file * out)502 int write_normal_end_chunk(struct output_file* out) {
503   return out->ops->pad(out, out->len);
504 }
505 
506 static struct sparse_file_ops normal_file_ops = {
507     .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,
511 };
512 
output_file_close(struct output_file * out)513 void output_file_close(struct output_file* out) {
514   out->sparse_ops->write_end_chunk(out);
515   free(out->zero_buf);
516   free(out->fill_buf);
517   out->zero_buf = nullptr;
518   out->fill_buf = nullptr;
519   out->ops->close(out);
520 }
521 
output_file_init(struct output_file * out,int block_size,int64_t len,bool sparse,int chunks,bool crc)522 static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
523                             int chunks, bool crc) {
524   int ret;
525 
526   out->len = len;
527   out->block_size = block_size;
528   out->cur_out_ptr = 0LL;
529   out->chunk_cnt = 0;
530   out->crc32 = 0;
531   out->use_crc = crc;
532 
533   // 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));
535   if (!out->zero_buf) {
536     error_errno("malloc zero_buf");
537     return -ENOMEM;
538   }
539 
540   // 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));
542   if (!out->fill_buf) {
543     error_errno("malloc fill_buf");
544     ret = -ENOMEM;
545     goto err_fill_buf;
546   }
547 
548   if (sparse) {
549     out->sparse_ops = &sparse_file_ops;
550   } else {
551     out->sparse_ops = &normal_file_ops;
552   }
553 
554   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};
565 
566     if (out->use_crc) {
567       sparse_header.total_chunks++;
568     }
569 
570     ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
571     if (ret < 0) {
572       goto err_write;
573     }
574   }
575 
576   return 0;
577 
578 err_write:
579   free(out->fill_buf);
580 err_fill_buf:
581   free(out->zero_buf);
582   return ret;
583 }
584 
output_file_new_gz(void)585 static 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");
590     return nullptr;
591   }
592 
593   outgz->out.ops = &gz_file_ops;
594 
595   return &outgz->out;
596 }
597 
output_file_new_normal(void)598 static 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");
603     return nullptr;
604   }
605 
606   outn->out.ops = &file_ops;
607 
608   return &outn->out;
609 }
610 
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)611 struct 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) {
614   int ret;
615   struct output_file_callback* outc;
616 
617   outc =
618       reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
619   if (!outc) {
620     error_errno("malloc struct outc");
621     return nullptr;
622   }
623 
624   outc->out.ops = &callback_file_ops;
625   outc->priv = priv;
626   outc->write = write;
627 
628   ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
629   if (ret < 0) {
630     free(outc);
631     return nullptr;
632   }
633 
634   return &outc->out;
635 }
636 
output_file_open_fd(int fd,unsigned int block_size,int64_t len,int gz,int sparse,int chunks,int crc)637 struct 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;
641 
642   if (gz) {
643     out = output_file_new_gz();
644   } else {
645     out = output_file_new_normal();
646   }
647   if (!out) {
648     return nullptr;
649   }
650 
651   out->ops->open(out, fd);
652 
653   ret = output_file_init(out, block_size, len, sparse, chunks, crc);
654   if (ret < 0) {
655     free(out);
656     return nullptr;
657   }
658 
659   return out;
660 }
661 
662 /* Write a contiguous region of data blocks from a memory buffer */
write_data_chunk(struct output_file * out,uint64_t len,void * data)663 int write_data_chunk(struct output_file* out, uint64_t len, void* data) {
664   return out->sparse_ops->write_data_chunk(out, len, data);
665 }
666 
667 /* 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)668 int write_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
669   return out->sparse_ops->write_fill_chunk(out, len, fill_val);
670 }
671 
write_fd_chunk(struct output_file * out,uint64_t len,int fd,int64_t offset)672 int write_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
673   auto m = android::base::MappedFile::FromFd(fd, offset, len, PROT_READ);
674   if (!m) return -errno;
675 
676   return out->sparse_ops->write_data_chunk(out, m->size(), m->data());
677 }
678 
679 /* 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)680 int write_file_chunk(struct output_file* out, uint64_t len, const char* file, int64_t offset) {
681   int ret;
682 
683   int file_fd = open(file, O_RDONLY | O_BINARY);
684   if (file_fd < 0) {
685     return -errno;
686   }
687 
688   ret = write_fd_chunk(out, len, file_fd, offset);
689 
690   close(file_fd);
691 
692   return ret;
693 }
694 
write_skip_chunk(struct output_file * out,uint64_t len)695 int write_skip_chunk(struct output_file* out, uint64_t len) {
696   return out->sparse_ops->write_skip_chunk(out, len);
697 }
698