/* add file contents to a tarchive */ int tar_append_regfile(TAR *t, const char *realname) { char block[T_BLOCKSIZE]; int filefd; int i, j; size_t size; int rv = -1; #if defined(O_BINARY) filefd = open(realname, O_RDONLY|O_BINARY); #else filefd = open(realname, O_RDONLY); #endif if (filefd == -1) { #ifdef DEBUG perror("open()"); #endif return -1; } size = th_get_size(t); for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE) { j = read(filefd, &block, T_BLOCKSIZE); if (j != T_BLOCKSIZE) { if (j != -1) errno = EINVAL; goto fail; } if (tar_block_write(t, &block) == -1) goto fail; } if (i > 0) { j = read(filefd, &block, i); if (j == -1) goto fail; memset(&(block[i]), 0, T_BLOCKSIZE - i); if (tar_block_write(t, &block) == -1) goto fail; } /* success! */ rv = 0; fail: close(filefd); return rv; }
/* add file contents to a tarchive */ int tar_append_regfile(TAR *t, char *realname) { char block[T_BLOCKSIZE]; int filefd; int j; size_t size, i; filefd = open(realname, O_RDONLY); if (filefd == -1) { #ifdef DEBUG perror("open()"); #endif return -1; } size = th_get_size(t); for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE) { j = read(filefd, &block, T_BLOCKSIZE); if (j != T_BLOCKSIZE) { if (j != -1) errno = EINVAL; return -1; } if (tar_block_write(t, &block) == -1) return -1; } if (i > 0) { j = read(filefd, &block, i); if (j == -1) return -1; memset(&(block[i]), 0, T_BLOCKSIZE - i); if (tar_block_write(t, &block) == -1) return -1; } close(filefd); return 0; }
/* write EOF indicator */ int tar_append_eof(TAR *t) { int i, j; char block[T_BLOCKSIZE]; memset(&block, 0, T_BLOCKSIZE); for (j = 0; j < 2; j++) { i = tar_block_write(t, &block); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } } return 0; }
int main(int argc, char **argv) { struct eub eub; struct eubfile file; int opt_P = 0; TAR *tarp; char buf[512]; libtar_hash_t *lthash = NULL; eub_init(&eub); ARGBEGIN { case 'P' : opt_P = 1; break; default : usage(); } ARGEND; if (argc == 0) usage(); if (eub_open(&eub, argv[0], "r")) return(eub.err); if (argc > 1) { lthash = libtar_hash_new(3, (libtar_hashfunc_t) hash_path); while (--argc) libtar_hash_add(lthash, ++argv); } if (tar_fdopen(&tarp, 1, "/dev/stdout", NULL, O_WRONLY, 0600, TAR_GNU) != 0) return(eub_err(&eub, errno, "Can't start tar output")); while (eub_read_meta(&eub, &file)) { unsigned long long len, remlen; struct libtar_node lp = { file.path, 0, 0 }; libtar_hashptr_t hp; hp.bucket = -1; hp.node = NULL; /* if (lthash && !libtar_hash_search(lthash, &hp, &lp, (libtar_matchfunc_t) compare_paths)) */ if (lthash && !libtar_hash_getkey(lthash, &hp, &lp, (libtar_matchfunc_t) compare_paths)) continue; if (eub_meta_to_stat(&eub, &file)) return(eub.err); th_set_from_stat(tarp, &file.stat); th_set_path(tarp, file.path); if (file.typechar == 'f') { /* XXX Hack! len = strlen(tarp->th_buf.name); if (tarp->th_buf.name[len-1] == '/') tarp->th_buf.name[len-1] = 0; */ if (eub_read_dataref(&eub, &file)) return(eub.err); } else if (file.typechar == 'l') { if (eub_read_dataref(&eub, &file)) return(eub.err); if (eub_seek_data(&eub, &file)) return(eub.err); if (eub_read_data(&eub, &file, eub.linkbuf, file.size)) return(eub_err(&eub, errno, "Can't read link: %s", file.path)); eub.linkbuf[file.size] = 0; th_set_link(tarp, eub.linkbuf); } th_finish(tarp); if (th_write(tarp)) return(eub_err(&eub, errno, "Can't write tar header for %s", file.path)); fflush(stdout); if (file.typechar == 'f') { if (eub_seek_data(&eub, &file)) return(eub.err); for (remlen = file.size; remlen; remlen -= len) { len = remlen < sizeof(buf) ? remlen : sizeof(buf); if (eub_read_data(&eub, &file, buf, len)) return(eub_err(&eub, errno, "Can't read file contents: %s", file.path)); if (len < sizeof(buf)) bzero(buf+len, sizeof(buf)-len); if (!tar_block_write(tarp, buf)) return(eub_err(&eub, errno, "Can't write to tar: %s", file.path)); fflush(stdout); } if (eub.err) return(eub.err); } } if (tar_append_eof(tarp)) return(eub_err(&eub, errno, "Can't finish tar")); fflush(stdout); return(eub.err); }
/* write a header block */ int th_write(TAR *t) { int i, j; char type2; size_t sz, sz2; char *ptr; char buf[T_BLOCKSIZE]; #ifdef DEBUG printf("==> th_write(TAR=\"%s\")\n", t->pathname); th_print(t); #endif if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL) { #ifdef DEBUG printf("th_write(): using gnu_longlink (\"%s\")\n", t->th_buf.gnu_longlink); #endif /* save old size and type */ type2 = t->th_buf.typeflag; sz2 = th_get_size(t); /* write out initial header block with fake size and type */ t->th_buf.typeflag = GNU_LONGLINK_TYPE; sz = strlen(t->th_buf.gnu_longlink); th_set_size(t, sz); th_finish(t); i = tar_block_write(t, &(t->th_buf)); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } /* write out extra blocks containing long name */ for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), ptr = t->th_buf.gnu_longlink; j > 1; j--, ptr += T_BLOCKSIZE) { i = tar_block_write(t, ptr); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } } memset(buf, 0, T_BLOCKSIZE); strncpy(buf, ptr, T_BLOCKSIZE); i = tar_block_write(t, &buf); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } /* reset type and size to original values */ t->th_buf.typeflag = type2; th_set_size(t, sz2); } if ((t->options & TAR_GNU) && t->th_buf.gnu_longname != NULL) { #ifdef DEBUG printf("th_write(): using gnu_longname (\"%s\")\n", t->th_buf.gnu_longname); #endif /* save old size and type */ type2 = t->th_buf.typeflag; sz2 = th_get_size(t); /* write out initial header block with fake size and type */ t->th_buf.typeflag = GNU_LONGNAME_TYPE; sz = strlen(t->th_buf.gnu_longname); th_set_size(t, sz); th_finish(t); i = tar_block_write(t, &(t->th_buf)); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } /* write out extra blocks containing long name */ for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), ptr = t->th_buf.gnu_longname; j > 1; j--, ptr += T_BLOCKSIZE) { i = tar_block_write(t, ptr); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } } memset(buf, 0, T_BLOCKSIZE); strncpy(buf, ptr, T_BLOCKSIZE); i = tar_block_write(t, &buf); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } /* reset type and size to original values */ t->th_buf.typeflag = type2; th_set_size(t, sz2); } th_finish(t); #ifdef DEBUG /* print tar header */ th_print(t); #endif i = tar_block_write(t, &(t->th_buf)); if (i != T_BLOCKSIZE) { if (i != -1) errno = EINVAL; return -1; } #ifdef DEBUG puts("th_write(): returning 0"); #endif return 0; }