int32_t xar_hash_toheap_done(xar_t x, xar_file_t f, xar_prop_t p, void **context) { const char *archived_style = NULL, *unarchived_style = NULL; size_t archived_length = -1, unarchived_length = -1; void *archived_hash = NULL, *unarchived_hash = NULL; if( ! CONTEXT(context) ) return 0; else if( CONTEXT(context)->count == 0 ) goto DONE; archived_style = strdup(xar_hash_get_digest_name(CONTEXT(context)->archived)); unarchived_style = strdup(xar_hash_get_digest_name(CONTEXT(context)->unarchived)); archived_hash = xar_hash_finish(CONTEXT(context)->archived, &archived_length); unarchived_hash = xar_hash_finish(CONTEXT(context)->unarchived, &unarchived_length); char *str; xar_prop_t tmpp; str = _xar_format_hash(archived_hash, archived_length); if( f ) { tmpp = xar_prop_pset(f, p, "archived-checksum", str); if( tmpp ) xar_attr_pset(f, tmpp, "style", archived_style); } free(str); str = _xar_format_hash(unarchived_hash, unarchived_length); if( f ) { tmpp = xar_prop_pset(f, p, "unarchived-checksum", str); if( tmpp ) xar_attr_pset(f, tmpp, "style", unarchived_style); } free(str); DONE: free((void *)archived_style); free((void *)unarchived_style); free(archived_hash); free(unarchived_hash); free(*context); *context = NULL; return 0; }
/* xar_data_archive * This is the arcmod archival entry point for archiving the file's * data into the heap file. */ int32_t xar_data_archive(xar_t x, xar_file_t f, const char *file, const char *buffer, size_t len) { const char *opt; int32_t retval = 0; struct _data_context context; xar_prop_t tmpp; memset(&context,0,sizeof(struct _data_context)); if( !xar_check_prop(x, "data") ) return 0; xar_prop_get(f, "type", &opt); if(!opt) return 0; if( strcmp(opt, "file") != 0 ) { if( strcmp(opt, "hardlink") == 0 ) { opt = xar_attr_get(f, "type", "link"); if( !opt ) return 0; if( strcmp(opt, "original") != 0 ) return 0; /* else, we're an original hardlink, so keep going */ } else return 0; } if( 0 == len ){ context.fd = open(file, O_RDONLY); if( context.fd < 0 ) { xar_err_new(x); xar_err_set_file(x, f); xar_err_set_string(x, "io: Could not open file"); xar_err_callback(x, XAR_SEVERITY_NONFATAL, XAR_ERR_ARCHIVE_CREATION); return -1; } }else{ context.buffer = (void *)buffer; context.length = len; context.offset = 0; } #ifdef F_NOCACHE fcntl(context.fd, F_NOCACHE, 1); #endif tmpp = xar_prop_pset(f, NULL, "data", NULL); retval = xar_attrcopy_to_heap(x, f, tmpp, xar_data_read,(void *)(&context)); if( context.total == 0 ) xar_prop_unset(f, "data"); if(context.fd > 0){ close(context.fd); context.fd = -1; } return retval; }
int xar_gzip_toheap_done(xar_t x, xar_file_t f, xar_prop_t p, void **context) { xar_prop_t tmpp; if( GZIP_CONTEXT(context)->gzipcompressed){ deflateEnd(&GZIP_CONTEXT(context)->z); if( GZIP_CONTEXT(context)->count ) { tmpp = xar_prop_pset(f, p, "encoding", NULL); if( tmpp ) xar_attr_pset(f, tmpp, "style", "application/x-gzip"); } } /* free the context */ free(GZIP_CONTEXT(context)); *context = NULL; return 0; }
xar_ea_t xar_ea_new(xar_file_t f, const char *name) { xar_ea_t ret; ret = calloc(sizeof(struct __xar_ea_t), 1); if( !ret ) return NULL; XAR_EA(ret)->prop = xar_prop_new(f, NULL); if( !XAR_EA(ret)->prop ) { free(ret); return NULL; } xar_prop_setkey(XAR_EA(ret)->prop, "ea"); xar_prop_setvalue(XAR_EA(ret)->prop, NULL); XAR_PROP(XAR_EA(ret)->prop)->attrs = xar_attr_new(); XAR_ATTR(XAR_PROP(XAR_EA(ret)->prop)->attrs)->key = strdup("id"); asprintf((char **)&XAR_ATTR(XAR_PROP(XAR_EA(ret)->prop)->attrs)->value, "%lld", XAR_FILE(f)->nexteaid++); xar_prop_pset(f, XAR_EA(ret)->prop, "name", name); return ret; }
/* xar_attrcopy_from_heap_to_heap * This does a simple copy of the heap data from one head (read-only) to another heap (write only). * This does not set any properties or attributes of the file, so this should not be used alone. */ int32_t xar_attrcopy_from_heap_to_heap(xar_t xsource, xar_file_t fsource, xar_prop_t p, xar_t xdest, xar_file_t fdest){ int r, off; size_t bsize; int64_t fsize, inc = 0, seekoff, writesize=0; off_t orig_heap_offset = XAR(xdest)->heap_offset; void *inbuf; const char *opt; char *tmpstr = NULL; xar_prop_t tmpp; opt = xar_opt_get(xsource, "rsize"); if( !opt ) { bsize = 4096; } else { bsize = strtol(opt, NULL, 0); if( ((bsize == LONG_MAX) || (bsize == LONG_MIN)) && (errno == ERANGE) ) { bsize = 4096; } } tmpp = xar_prop_pget(p, "offset"); if( tmpp ) opt = xar_prop_getvalue(tmpp); seekoff = strtoll(opt, NULL, 0); if( ((seekoff == LLONG_MAX) || (seekoff == LLONG_MIN)) && (errno == ERANGE) ) { return -1; } seekoff += XAR(xsource)->toc_count + sizeof(xar_header_t); if( XAR(xsource)->fd > 1 ) { r = lseek(XAR(xsource)->fd, seekoff, SEEK_SET); if( r == -1 ) { if( errno == ESPIPE ) { ssize_t rr; char *buf; unsigned int len; len = seekoff - XAR(xsource)->toc_count; len -= sizeof(xar_header_t); if( XAR(xsource)->heap_offset > len ) { xar_err_new(xsource); xar_err_set_file(xsource, fsource); xar_err_set_string(xsource, "Unable to seek"); xar_err_callback(xsource, XAR_SEVERITY_NONFATAL, XAR_ERR_ARCHIVE_EXTRACTION); } else { len -= XAR(xsource)->heap_offset; buf = malloc(len); assert(buf); rr = read(XAR(xsource)->fd, buf, len); if( rr < len ) { xar_err_new(xsource); xar_err_set_file(xsource, fsource); xar_err_set_string(xsource, "Unable to seek"); xar_err_callback(xsource, XAR_SEVERITY_NONFATAL, XAR_ERR_ARCHIVE_EXTRACTION); } free(buf); } } else { xar_err_new(xsource); xar_err_set_file(xsource, fsource); xar_err_set_string(xsource, "Unable to seek"); xar_err_callback(xsource, XAR_SEVERITY_NONFATAL, XAR_ERR_ARCHIVE_EXTRACTION); } } } opt = NULL; tmpp = xar_prop_pget(p, "length"); if( tmpp ) opt = xar_prop_getvalue(tmpp); if( !opt ) { return 0; } else { fsize = strtoll(opt, NULL, 10); if( ((fsize == LLONG_MAX) || (fsize == LLONG_MIN)) && (errno == ERANGE) ) { return -1; } } inbuf = malloc(bsize); if( !inbuf ) { return -1; } while(1) { /* Size has been reached */ if( fsize == inc ) break; if( (fsize - inc) < bsize ) bsize = fsize - inc; r = read(XAR(xsource)->fd, inbuf, bsize); if( r == 0 ) break; if( (r < 0) && (errno == EINTR) ) continue; if( r < 0 ) { free(inbuf); return -1; } XAR(xsource)->heap_offset += r; inc += r; bsize = r; off = 0; do { r = write(XAR(xdest)->heap_fd, inbuf+off, r-off ); off += r; writesize += r; } while( off < r ); XAR(xdest)->heap_offset += off; XAR(xdest)->heap_len += off; } asprintf(&tmpstr, "%"PRIu64, (uint64_t)orig_heap_offset); opt = xar_prop_getkey(p); tmpp = xar_prop_pfirst(fdest); if( tmpp ) tmpp = xar_prop_find(tmpp, opt); if( tmpp ) xar_prop_pset(fdest, tmpp, "offset", tmpstr); free(tmpstr); free(inbuf); /* It is the caller's responsibility to copy the attributes of the file, etc, this only copies the data in the heap */ return 0; }
int32_t xar_attrcopy_to_heap(xar_t x, xar_file_t f, xar_prop_t p, read_callback rcb, void *context) { int modulecount = (sizeof(xar_datamods)/sizeof(struct datamod)); void *modulecontext[modulecount]; int r, off, i; size_t bsize, rsize; int64_t readsize=0, writesize=0, inc = 0; void *inbuf; char *tmpstr = NULL; const char *opt, *csum; off_t orig_heap_offset = XAR(x)->heap_offset; xar_file_t tmpf = NULL; xar_prop_t tmpp = NULL; memset(modulecontext, 0, sizeof(void*)*modulecount); opt = xar_opt_get(x, XAR_OPT_RSIZE); if( !opt ) { bsize = 4096; } else { bsize = strtol(opt, NULL, 0); if( ((bsize == LONG_MAX) || (bsize == LONG_MIN)) && (errno == ERANGE) ) { bsize = 4096; } } r = 1; while(r != 0) { inbuf = malloc(bsize); if( !inbuf ) return -1; r = rcb(x, f, inbuf, bsize, context); if( r < 0 ) { free(inbuf); return -1; } readsize+=r; inc += r; rsize = r; /* filter the data through the in modules */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_in ) { xar_datamods[i].th_in(x, f, p, &inbuf, &rsize, &(modulecontext[i])); } } /* filter the data through the out modules */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_out ) xar_datamods[i].th_out(x, f, p, inbuf, rsize, &(modulecontext[i])); } off = 0; if( rsize != 0 ) { do { r = write(XAR(x)->heap_fd, inbuf+off, rsize-off); if( (r < 0) && (errno != EINTR) ) return -1; off += r; writesize += r; } while( off < rsize ); } XAR(x)->heap_offset += off; free(inbuf); } /* If size is 0, don't bother having anything in the heap */ if( readsize == 0 ) { XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_done ) xar_datamods[i].th_done(x, f, p, &(modulecontext[i])); } return 0; } /* finish up anything that still needs doing */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_done ) xar_datamods[i].th_done(x, f, p, &(modulecontext[i])); } XAR(x)->heap_len += writesize; tmpp = xar_prop_pget(p, "archived-checksum"); if( tmpp ) csum = xar_prop_getvalue(tmpp); tmpf = xmlHashLookup(XAR(x)->csum_hash, BAD_CAST(csum)); if( tmpf ) { const char *attr = xar_prop_getkey(p); opt = xar_opt_get(x, XAR_OPT_LINKSAME); if( opt && (strcmp(attr, "data") == 0) ) { const char *id = xar_attr_pget(tmpf, NULL, "id"); xar_prop_pset(f, NULL, "type", "hardlink"); tmpp = xar_prop_pfirst(f); if( tmpp ) tmpp = xar_prop_find(tmpp, "type"); if( tmpp ) xar_attr_pset(f, tmpp, "link", id); xar_prop_pset(tmpf, NULL, "type", "hardlink"); tmpp = xar_prop_pfirst(tmpf); if( tmpp ) tmpp = xar_prop_find(tmpp, "type"); if( tmpp ) xar_attr_pset(tmpf, tmpp, "link", "original"); tmpp = xar_prop_pfirst(f); if( tmpp ) tmpp = xar_prop_find(tmpp, "data"); xar_prop_punset(f, tmpp); XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); XAR(x)->heap_len -= writesize; return 0; } opt = xar_opt_get(x, XAR_OPT_COALESCE); if( opt ) { long long tmpoff; const char *offstr = NULL; tmpp = xar_prop_pfirst(tmpf); if( tmpp ) { const char *key; key = xar_prop_getkey(p); tmpp = xar_prop_find(tmpp, key); } if( tmpp ) tmpp = xar_prop_pget(tmpp, "offset"); if( tmpp ) offstr = xar_prop_getvalue(tmpp); if( offstr ) { tmpoff = strtoll(offstr, NULL, 10); XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); orig_heap_offset = tmpoff; XAR(x)->heap_len -= writesize; } } } else { xmlHashAddEntry(XAR(x)->csum_hash, BAD_CAST(csum), XAR_FILE(f)); } asprintf(&tmpstr, "%"PRIu64, readsize); xar_prop_pset(f, p, "size", tmpstr); free(tmpstr); asprintf(&tmpstr, "%"PRIu64, (uint64_t)orig_heap_offset); xar_prop_pset(f, p, "offset", tmpstr); free(tmpstr); tmpstr = (char *)xar_opt_get(x, XAR_OPT_COMPRESSION); if( tmpstr && (strcmp(tmpstr, XAR_OPT_VAL_NONE) == 0) ) { xar_prop_pset(f, p, "encoding", NULL); tmpp = xar_prop_pget(p, "encoding"); if( tmpp ) xar_attr_pset(f, tmpp, "style", "application/octet-stream"); } asprintf(&tmpstr, "%"PRIu64, writesize); xar_prop_pset(f, p, "length", tmpstr); free(tmpstr); return 0; }
/* xar_attrcopy_from_heap_to_heap * This does a simple copy of the heap data from one head (read-only) to another heap (write only). * This does not set any properties or attributes of the file, so this should not be used alone. */ int32_t xar_attrcopy_from_heap_to_heap(xar_t xsource, xar_file_t fsource, xar_prop_t p, xar_t xdest, xar_file_t fdest){ int r, off; size_t bsize; int64_t fsize, inc = 0, seekoff, writesize=0; off_t orig_heap_offset = XAR(xdest)->heap_offset; void *inbuf; const char *opt; char *tmpstr = NULL; xar_prop_t tmpp; bsize = xar_io_get_rsize(xsource); seekoff = xar_io_get_file_offset(xsource, fsource, p); if( seekoff < 0 ) return -1; seekoff += XAR(xsource)->toc_count + sizeof(xar_header_t); xar_io_seek(xsource, fsource, seekoff); fsize = xar_io_get_length(p); if( fsize == 0 ) return 0; if( fsize < 0 ) return -1; inbuf = malloc(bsize); if( !inbuf ) { return -1; } while(1) { /* Size has been reached */ if( fsize == inc ) break; if( (fsize - inc) < bsize ) bsize = fsize - inc; r = read(XAR(xsource)->fd, inbuf, bsize); if( r == 0 ) break; if( (r < 0) && (errno == EINTR) ) continue; if( r < 0 ) { free(inbuf); return -1; } XAR(xsource)->heap_offset += r; inc += r; bsize = r; off = 0; do { r = write(XAR(xdest)->heap_fd, ((char *)inbuf)+off, r-off ); off += r; writesize += r; } while( off < r ); XAR(xdest)->heap_offset += off; XAR(xdest)->heap_len += off; } asprintf(&tmpstr, "%"PRIu64, (uint64_t)orig_heap_offset); opt = xar_prop_getkey(p); tmpp = xar_prop_pfirst(fdest); if( tmpp ) tmpp = xar_prop_find(tmpp, opt); if( tmpp ) xar_prop_pset(fdest, tmpp, "offset", tmpstr); free(tmpstr); free(inbuf); /* It is the caller's responsibility to copy the attributes of the file, etc, this only copies the data in the heap */ return 0; }
int32_t xar_attrcopy_to_heap(xar_t x, xar_file_t f, xar_prop_t p, read_callback rcb, void *context) { int modulecount = (sizeof(xar_datamods)/sizeof(struct datamod)); void *modulecontext[modulecount]; int r, i; size_t bsize, rsize; int64_t readsize=0, writesize=0, inc = 0, this_write=0; void *inbuf; char *tmpstr = NULL; const char *opt = NULL, *csum = NULL; off_t orig_heap_offset = XAR(x)->heap_offset; xar_file_t tmpf = NULL; xar_prop_t tmpp = NULL; memset(modulecontext, 0, sizeof(void*)*modulecount); bsize = xar_io_get_rsize(x); r = 1; // (Apple) allocate once inbuf = malloc(bsize); if( !inbuf ) return -1; while(r != 0) { r = rcb(x, f, inbuf, bsize, context); if( r < 0 ) { free(inbuf); return -1; } readsize+=r; inc += r; rsize = r; /* filter the data through the in modules */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_in ) { xar_datamods[i].th_in(x, f, p, &inbuf, &rsize, &(modulecontext[i])); } } /* filter the data through the out modules */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_out ) xar_datamods[i].th_out(x, f, p, inbuf, rsize, &(modulecontext[i])); } size_t written = 0; if( rsize != 0 ) { while(written < rsize) { this_write = xar_write_fd(XAR(x)->heap_fd, inbuf, rsize); if( this_write < 0 ) { xar_err_new(x); xar_err_set_string(x, "write(2) error when writing to heap"); xar_err_set_errno(x, errno); xar_err_callback(x, XAR_SEVERITY_FATAL, XAR_ERR_ARCHIVE_CREATION); free(inbuf); return -1; } written += this_write; } } XAR(x)->heap_offset += written; writesize += written; } free(inbuf); /* If size is 0, don't bother having anything in the heap */ if( readsize == 0 ) { XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_done ) xar_datamods[i].th_done(x, f, p, &(modulecontext[i])); } return 0; } /* finish up anything that still needs doing */ for( i = 0; i < modulecount; i++) { if( xar_datamods[i].th_done ) xar_datamods[i].th_done(x, f, p, &(modulecontext[i])); } XAR(x)->heap_len += writesize; tmpp = xar_prop_pget(p, "archived-checksum"); if( tmpp ) csum = xar_prop_getvalue(tmpp); if( csum ) tmpf = xmlHashLookup(XAR(x)->csum_hash, BAD_CAST(csum)); if( tmpf ) { const char *attr = xar_prop_getkey(p); opt = xar_opt_get(x, XAR_OPT_LINKSAME); if( opt && (strcmp(attr, "data") == 0) ) { const char *id = xar_attr_pget(tmpf, NULL, "id"); xar_prop_pset(f, NULL, "type", "hardlink"); tmpp = xar_prop_pfirst(f); if( tmpp ) tmpp = xar_prop_find(tmpp, "type"); if( tmpp ) xar_attr_pset(f, tmpp, "link", id); xar_prop_pset(tmpf, NULL, "type", "hardlink"); tmpp = xar_prop_pfirst(tmpf); if( tmpp ) tmpp = xar_prop_find(tmpp, "type"); if( tmpp ) xar_attr_pset(tmpf, tmpp, "link", "original"); tmpp = xar_prop_pfirst(f); if( tmpp ) tmpp = xar_prop_find(tmpp, "data"); xar_prop_punset(f, tmpp); XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); XAR(x)->heap_len -= writesize; return 0; } opt = xar_opt_get(x, XAR_OPT_COALESCE); if( opt ) { long long tmpoff; const char *offstr = NULL; tmpp = xar_prop_pfirst(tmpf); if( tmpp ) { const char *key; key = xar_prop_getkey(p); tmpp = xar_prop_find(tmpp, key); } if( tmpp ) tmpp = xar_prop_pget(tmpp, "offset"); if( tmpp ) offstr = xar_prop_getvalue(tmpp); if( offstr ) { tmpoff = strtoll(offstr, NULL, 10); XAR(x)->heap_offset = orig_heap_offset; lseek(XAR(x)->heap_fd, -writesize, SEEK_CUR); orig_heap_offset = tmpoff; XAR(x)->heap_len -= writesize; } } } else if( csum ) { xmlHashAddEntry(XAR(x)->csum_hash, BAD_CAST(csum), XAR_FILE(f)); } else { xar_err_new(x); xar_err_set_file(x, f); xar_err_set_string(x, "No archived-checksum"); xar_err_callback(x, XAR_SEVERITY_WARNING, XAR_ERR_ARCHIVE_CREATION); } asprintf(&tmpstr, "%"PRIu64, readsize); xar_prop_pset(f, p, "size", tmpstr); free(tmpstr); asprintf(&tmpstr, "%"PRIu64, (uint64_t)orig_heap_offset); xar_prop_pset(f, p, "offset", tmpstr); free(tmpstr); tmpstr = (char *)xar_opt_get(x, XAR_OPT_COMPRESSION); if( tmpstr && (strcmp(tmpstr, XAR_OPT_VAL_NONE) == 0) ) { xar_prop_pset(f, p, "encoding", NULL); tmpp = xar_prop_pget(p, "encoding"); if( tmpp ) xar_attr_pset(f, tmpp, "style", "application/octet-stream"); } asprintf(&tmpstr, "%"PRIu64, writesize); xar_prop_pset(f, p, "length", tmpstr); free(tmpstr); return 0; }
int32_t xar_ea_pset(xar_file_t f, xar_ea_t e, const char *key, const char *value){ if( xar_prop_pset(f, XAR_EA(e)->prop, key, value) ) return 0; return -1; }