|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <sys/types.h> |
| 4 | +#include <xar/xar.h> |
| 5 | +#include <sys/fcntl.h> |
| 6 | + |
| 7 | +static int32_t err_callback(int32_t sev, int32_t err, xar_errctx_t ctx, void *usrctx) { |
| 8 | + fprintf(stderr, "err callback\n"); |
| 9 | +} |
| 10 | + |
| 11 | +int main(int argc, char *argv[]) { |
| 12 | + xar_t x; |
| 13 | + xar_iter_t i; |
| 14 | + xar_file_t f; |
| 15 | + xar_stream s; |
| 16 | + char buffer[8192]; |
| 17 | + |
| 18 | + x = xar_open(argv[1], READ); |
| 19 | + if( !x ) { |
| 20 | + fprintf(stderr, "Error opening archive\n"); |
| 21 | + exit(1); |
| 22 | + } |
| 23 | + |
| 24 | + xar_register_errhandler(x, err_callback, NULL); |
| 25 | + |
| 26 | + i = xar_iter_new(); |
| 27 | + if( !i ) { |
| 28 | + fprintf(stderr, "Error creating xar iterator\n"); |
| 29 | + exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + for(f = xar_file_first(x, i); f; f = xar_file_next(i)) { |
| 33 | + char *path; |
| 34 | + const char *type; |
| 35 | + int32_t ret; |
| 36 | + int fd; |
| 37 | + |
| 38 | + path = xar_get_path(f); |
| 39 | + |
| 40 | + xar_prop_get(f, "type", &type); |
| 41 | + if( !type ) { |
| 42 | + fprintf(stderr, "File has no type %s\n", path); |
| 43 | + free(path); |
| 44 | + continue; |
| 45 | + } |
| 46 | + if( strcmp(type, "file") != 0 ) { |
| 47 | + fprintf(stderr, "Skipping %s\n", path); |
| 48 | + free(path); |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + fprintf(stderr, "Extracting %s\n", path); |
| 53 | + if( xar_extract_tostream_init(x, f, &s) != XAR_STREAM_OK ) { |
| 54 | + fprintf(stderr, "Error initializing stream %s\n", path); |
| 55 | + free(path); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); |
| 60 | + if( !fd ) { |
| 61 | + fprintf(stderr, "Error opening output file %s\n", path); |
| 62 | + free(path); |
| 63 | + continue; |
| 64 | + } |
| 65 | + s.avail_out = sizeof(buffer); |
| 66 | + s.next_out = buffer; |
| 67 | + |
| 68 | + while( (ret = xar_extract_tostream(&s)) != XAR_STREAM_END ) { |
| 69 | + if( ret == XAR_STREAM_ERR ) { |
| 70 | + fprintf(stderr, "Error extracting stream %s\n", path); |
| 71 | + exit(2); |
| 72 | + } |
| 73 | + |
| 74 | + write(fd, buffer, sizeof(buffer)-s.avail_out); |
| 75 | + |
| 76 | + s.avail_out = sizeof(buffer); |
| 77 | + s.next_out = buffer; |
| 78 | + } |
| 79 | + |
| 80 | + if( xar_extract_tostream_end(&s) != XAR_STREAM_OK ) { |
| 81 | + fprintf(stderr, "Error ending stream %s\n", path); |
| 82 | + } |
| 83 | + |
| 84 | + close(fd); |
| 85 | + free(path); |
| 86 | + } |
| 87 | +} |
0 commit comments