1#include <stdlib.h> 2#include <stdio.h> 3#include <sys/stat.h> 4#include <sys/fcntl.h> 5#include <string.h> 6#include <xar/xar.h> 7 8int32_t err_callback(int32_t sev, int32_t err, xar_errctx_t ctx, void *usrctx) 9{ 10 printf("error callback invoked\n"); 11 return 0; 12} 13 14int main(int argc, char *argv[]) 15{ 16 int fd; 17 unsigned char *buffer; 18 struct stat sb; 19 ssize_t red; 20 xar_t x; 21 xar_file_t f, f2; 22 23 if( argc < 2 ) { 24 fprintf(stderr, "usage: %s <filename>\n", argv[0]); 25 exit(1); 26 } 27 28 fd = open(argv[1], O_RDONLY); 29 if( fd < 0 ) { 30 fprintf(stderr, "Unable to open file %s\n", argv[1]); 31 exit(2); 32 } 33 34 if( fstat(fd, &sb) < 0 ) { 35 fprintf(stderr, "Unable to stat file %s\n", argv[1]); 36 exit(3); 37 } 38 39 buffer = malloc(sb.st_size); 40 if( buffer == NULL ) { 41 fprintf(stderr, "Unable to allocate memory\n"); 42 exit(4); 43 } 44 45 red = read(fd, buffer, sb.st_size); 46 if( red <= 0 ) { 47 fprintf(stderr, "Error reading from file\n"); 48 exit(5); 49 } 50 if( red < sb.st_size ) 51 fprintf(stderr, "Incomplete read\n"); 52 53 x = xar_open("/tmp/test.xar", WRITE); 54 if( x == NULL ) { 55 fprintf(stderr, "Error creating xarchive\n"); 56 exit(6); 57 } 58 59 xar_register_errhandler(x, err_callback, NULL); 60 61 memset(&sb, 0, sizeof(sb)); 62 63 sb.st_mode = S_IFDIR | S_IRWXU; 64 f = xar_add_folder(x, NULL, "mydir", &sb); 65 if( !f ) { 66 fprintf(stderr, "Error adding parent to archive\n"); 67 exit(7); 68 } 69 70 f2 = xar_add_frombuffer(x, f, "secondfile", buffer, red); 71 if( !f ) { 72 fprintf(stderr, "Error adding child to archive\n"); 73 exit(8); 74 } 75 76 xar_close(x); 77 close(fd); 78 exit(0); 79} 80