|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <stddef.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#include <archive.h> |
| 7 | +#include <archive_entry.h> |
| 8 | + |
| 9 | +#include "rootfs_archive.h" |
| 10 | + |
| 11 | +static |
| 12 | +struct archive * |
| 13 | +reader_init(void *buf, size_t size) |
| 14 | +{ |
| 15 | + struct archive *r; |
| 16 | + int ret; |
| 17 | + |
| 18 | + r = archive_read_new(); |
| 19 | + if (r == NULL) { |
| 20 | + printf("init reader failed\n"); |
| 21 | + return NULL; |
| 22 | + } |
| 23 | + |
| 24 | + ret = archive_read_support_format_tar(r); |
| 25 | + if (ret != ARCHIVE_OK) { |
| 26 | + printf("reader cannot support tar format\n"); |
| 27 | + return NULL; |
| 28 | + } |
| 29 | + |
| 30 | + ret = archive_read_open_memory(r, buf, size); |
| 31 | + if (ret != ARCHIVE_OK) { |
| 32 | + printf("reader cannot open file\ncause: %s\n", archive_error_string(r)); |
| 33 | + return NULL; |
| 34 | + } |
| 35 | + |
| 36 | + return r; |
| 37 | +} |
| 38 | + |
| 39 | +static |
| 40 | +struct archive * |
| 41 | +writer_init() |
| 42 | +{ |
| 43 | + struct archive *w; |
| 44 | + int ret; |
| 45 | + |
| 46 | + w = archive_write_disk_new(); |
| 47 | + if (w == NULL) { |
| 48 | + printf("init writer failed\n"); |
| 49 | + return NULL; |
| 50 | + } |
| 51 | + |
| 52 | + ret = archive_write_disk_set_options(w, ARCHIVE_EXTRACT_TIME); |
| 53 | + if (ret < 0) { |
| 54 | + printf("error setting writer disk options\n"); |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + |
| 58 | + return w; |
| 59 | +} |
| 60 | + |
| 61 | +static |
| 62 | +int |
| 63 | +unzip(struct archive *r, struct archive *w) |
| 64 | +{ |
| 65 | + struct archive_entry *entry; |
| 66 | + const void *buf; |
| 67 | + int64_t offset; |
| 68 | + size_t size; |
| 69 | + int ret; |
| 70 | + |
| 71 | + while ((ret = archive_read_next_header(r, &entry)) != ARCHIVE_EOF) { |
| 72 | + if (ret != ARCHIVE_OK) { |
| 73 | + printf("error reading archive entry\ncause: %s\n", archive_error_string(r)); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + ret = archive_write_header(w, entry); |
| 78 | + if (ret != ARCHIVE_OK) { |
| 79 | + printf("error writing %s entry\ncause: %s\n", archive_entry_pathname(entry), archive_error_string(w)); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + while ((ret = archive_read_data_block(r, &buf, &size, &offset)) != ARCHIVE_EOF) { |
| 84 | + if (ret != ARCHIVE_OK) { |
| 85 | + printf("error reading archive data block\ncause: %s\n", archive_error_string(r)); |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + ret = archive_write_data_block(w, buf, size, offset); |
| 90 | + if (ret != ARCHIVE_OK) { |
| 91 | + printf("error writing archive data block\ncause: %s\n", archive_error_string(w)); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + ret = archive_write_finish_entry(w); |
| 97 | + if (ret != ARCHIVE_OK) { |
| 98 | + printf("error finishing %s entry\ncause: %s\n", archive_entry_pathname(entry), archive_error_string(w)); |
| 99 | + return -1; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static |
| 107 | +void |
| 108 | +archive_cleanup(struct archive *r, struct archive *w) |
| 109 | +{ |
| 110 | + archive_read_close(r); |
| 111 | + archive_read_free(r); |
| 112 | + |
| 113 | + archive_write_close(w); |
| 114 | + archive_write_free(w); |
| 115 | +} |
| 116 | + |
| 117 | +int |
| 118 | +archive_unzip(void *buf, size_t size) |
| 119 | +{ |
| 120 | + struct archive *reader, *writer; |
| 121 | + int ret; |
| 122 | + |
| 123 | + reader = reader_init(buf, size); |
| 124 | + if (reader == NULL) |
| 125 | + return -1; |
| 126 | + |
| 127 | + writer = writer_init(); |
| 128 | + if (writer == NULL) |
| 129 | + return -1; |
| 130 | + |
| 131 | + ret = unzip(reader, writer); |
| 132 | + if (ret < 0) |
| 133 | + return -1; |
| 134 | + |
| 135 | + archive_cleanup(reader, writer); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments