| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895 |
- /*
- ** Copyright 1998-2003 University of Illinois Board of Trustees
- ** Copyright 1998-2003 Mark D. Roth
- ** All rights reserved.
- **
- ** extract.c - libtar code to extract a file from a tar archive
- **
- ** Mark D. Roth <[email protected]>
- ** Campus Information Technologies and Educational Services
- ** University of Illinois at Urbana-Champaign
- */
- #include <libtarint/internal.h>
- #include <stdio.h>
- #include <libtar/compat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <errno.h>
- #if defined(_WIN32) && !defined(__CYGWIN__)
- # ifdef _MSC_VER
- # include <sys/utime.h>
- # else
- # include <utime.h>
- # endif
- # include <io.h>
- # include <direct.h>
- #else
- # include <utime.h>
- # include <sys/param.h>
- #endif
- #ifdef STDC_HEADERS
- # include <stdlib.h>
- # include <string.h>
- #endif
- #ifdef HAVE_UNISTD_H
- # include <unistd.h>
- #endif
- #ifdef HAVE_SYS_MKDEV_H
- # include <sys/mkdev.h>
- #endif
- struct linkname
- {
- char ln_save[TAR_MAXPATHLEN];
- char ln_real[TAR_MAXPATHLEN];
- };
- typedef struct linkname linkname_t;
- static int
- tar_set_file_perms(TAR *t, char *realname)
- {
- mode_t mode;
- uid_t uid;
- gid_t gid;
- struct utimbuf ut;
- char *filename;
- char *pathname = 0;
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- uid = th_get_uid(t);
- gid = th_get_gid(t);
- ut.modtime = ut.actime = th_get_mtime(t);
- /* change owner/group */
- #ifndef WIN32
- if (geteuid() == 0)
- #ifdef HAVE_LCHOWN
- if (lchown(filename, uid, gid) == -1)
- {
- # ifdef DEBUG
- fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n",
- filename, uid, gid, strerror(errno));
- # endif
- #else /* ! HAVE_LCHOWN */
- if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1)
- {
- # ifdef DEBUG
- fprintf(stderr, "chown(\"%s\", %d, %d): %s\n",
- filename, uid, gid, strerror(errno));
- # endif
- #endif /* HAVE_LCHOWN */
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- /* change access/modification time */
- if (!TH_ISSYM(t) && utime(filename, &ut) == -1)
- {
- #ifdef DEBUG
- perror("utime()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- /* change permissions */
- if (!TH_ISSYM(t) && chmod(filename, mode & 07777) == -1)
- {
- #ifdef DEBUG
- perror("chmod()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #else /* WIN32 */
- (void)filename;
- (void)gid;
- (void)uid;
- (void)mode;
- #endif /* WIN32 */
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- }
- /* switchboard */
- int
- tar_extract_file(TAR *t, char *realname)
- {
- int i;
- linkname_t *lnp;
- char *pathname;
- if (t->options & TAR_NOOVERWRITE)
- {
- struct stat s;
- #ifdef WIN32
- if (stat(realname, &s) == 0 || errno != ENOENT)
- #else
- if (lstat(realname, &s) == 0 || errno != ENOENT)
- #endif
- {
- errno = EEXIST;
- return -1;
- }
- }
- if (TH_ISDIR(t))
- {
- i = tar_extract_dir(t, realname);
- if (i == 1)
- i = 0;
- }
- #ifndef _WIN32
- else if (TH_ISLNK(t))
- i = tar_extract_hardlink(t, realname);
- else if (TH_ISSYM(t))
- i = tar_extract_symlink(t, realname);
- else if (TH_ISCHR(t))
- i = tar_extract_chardev(t, realname);
- else if (TH_ISBLK(t))
- i = tar_extract_blockdev(t, realname);
- else if (TH_ISFIFO(t))
- i = tar_extract_fifo(t, realname);
- #endif
- else /* if (TH_ISREG(t)) */
- i = tar_extract_regfile(t, realname);
- if (i != 0)
- return i;
- i = tar_set_file_perms(t, realname);
- if (i != 0)
- return i;
- lnp = (linkname_t *)calloc(1, sizeof(linkname_t));
- if (lnp == NULL)
- return -1;
- pathname = th_get_pathname(t);
- strlcpy(lnp->ln_save, pathname, sizeof(lnp->ln_save));
- strlcpy(lnp->ln_real, realname, sizeof(lnp->ln_real));
- #ifdef DEBUG
- printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", "
- "value=\"%s\"\n", pathname, realname);
- #endif
- if (pathname)
- {
- free(pathname);
- }
- if (libtar_hash_add(t->h, lnp) != 0)
- return -1;
- return 0;
- }
- /* extract regular file */
- int
- tar_extract_regfile(TAR *t, char *realname)
- {
- mode_t mode;
- size_t size;
- uid_t uid;
- gid_t gid;
- int fdout;
- ssize_t i, k;
- char buf[T_BLOCKSIZE];
- char *filename;
- char *pathname = 0;
- #ifdef DEBUG
- printf("==> tar_extract_regfile(t=0x%lx, realname=\"%s\")\n", t,
- realname);
- #endif
- if (!TH_ISREG(t))
- {
- errno = EINVAL;
- return -1;
- }
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- size = th_get_size(t);
- uid = th_get_uid(t);
- gid = th_get_gid(t);
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (mode %04o, uid %d, gid %d, %d bytes)\n",
- filename, mode, uid, gid, size);
- #endif
- fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC
- #ifdef O_BINARY
- | O_BINARY
- #endif
- , 0666);
- if (fdout == -1)
- {
- #ifdef DEBUG
- perror("open()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #if 0
- /* change the owner. (will only work if run as root) */
- if (fchown(fdout, uid, gid) == -1 && errno != EPERM)
- {
- #ifdef DEBUG
- perror("fchown()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- /* make sure the mode isn't inheritted from a file we're overwriting */
- if (fchmod(fdout, mode & 07777) == -1)
- {
- #ifdef DEBUG
- perror("fchmod()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #endif
- /* extract the file */
- for (i = size; i > 0; i -= T_BLOCKSIZE)
- {
- k = tar_block_read(t, buf);
- if (k != T_BLOCKSIZE)
- {
- if (k != -1)
- errno = EINVAL;
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- /* write block to output file */
- if (write(fdout, buf,
- ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : (unsigned int)i)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- }
- /* close output file */
- if (close(fdout) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf("### done extracting %s\n", filename);
- #endif
- (void)filename;
- (void)gid;
- (void)uid;
- (void)mode;
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- }
- /* skip regfile */
- int
- tar_skip_regfile(TAR *t)
- {
- ssize_t i, k;
- size_t size;
- char buf[T_BLOCKSIZE];
- if (!TH_ISREG(t))
- {
- errno = EINVAL;
- return -1;
- }
- size = th_get_size(t);
- for (i = size; i > 0; i -= T_BLOCKSIZE)
- {
- k = tar_block_read(t, buf);
- if (k != T_BLOCKSIZE)
- {
- if (k != -1)
- errno = EINVAL;
- return -1;
- }
- }
- return 0;
- }
- /* hardlink */
- int
- tar_extract_hardlink(TAR * t, char *realname)
- {
- char *filename;
- char *linktgt;
- linkname_t *lnp;
- libtar_hashptr_t hp;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- if (!TH_ISLNK(t))
- {
- errno = EINVAL;
- return -1;
- }
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- libtar_hashptr_reset(&hp);
- if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t),
- (libtar_matchfunc_t)libtar_str_match) != 0)
- {
- lnp = (linkname_t *)libtar_hashptr_data(&hp);
- linktgt = lnp->ln_real;
- }
- else
- linktgt = th_get_linkname(t);
- #ifdef DEBUG
- printf(" ==> extracting: %s (link to %s)\n", filename, linktgt);
- #endif
- #ifndef WIN32
- if (link(linktgt, filename) == -1)
- #else
- (void)linktgt;
- #endif
- {
- #ifdef DEBUG
- perror("link()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifndef WIN32
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- #endif
- }
- /* symlink */
- int
- tar_extract_symlink(TAR *t, char *realname)
- {
- char *filename;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- #ifndef _WIN32
- if (!TH_ISSYM(t))
- {
- errno = EINVAL;
- return -1;
- }
- #endif
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- if (unlink(filename) == -1 && errno != ENOENT)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (symlink to %s)\n",
- filename, th_get_linkname(t));
- #endif
- #ifndef WIN32
- if (symlink(th_get_linkname(t), filename) == -1)
- #endif
- {
- #ifdef DEBUG
- perror("symlink()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifndef WIN32
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- #endif
- }
- /* character device */
- int
- tar_extract_chardev(TAR *t, char *realname)
- {
- mode_t mode;
- unsigned long devmaj, devmin;
- char *filename;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- #ifndef _WIN32
- if (!TH_ISCHR(t))
- {
- errno = EINVAL;
- return -1;
- }
- #endif
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- devmaj = th_get_devmajor(t);
- devmin = th_get_devminor(t);
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (character device %ld,%ld)\n",
- filename, devmaj, devmin);
- #endif
- #ifndef WIN32
- if (mknod(filename, mode | S_IFCHR,
- compat_makedev(devmaj, devmin)) == -1)
- #else
- (void)devmin;
- (void)devmaj;
- (void)mode;
- #endif
- {
- #ifdef DEBUG
- perror("mknod()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifndef WIN32
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- #endif
- }
- /* block device */
- int
- tar_extract_blockdev(TAR *t, char *realname)
- {
- mode_t mode;
- unsigned long devmaj, devmin;
- char *filename;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- if (!TH_ISBLK(t))
- {
- errno = EINVAL;
- return -1;
- }
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- devmaj = th_get_devmajor(t);
- devmin = th_get_devminor(t);
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (block device %ld,%ld)\n",
- filename, devmaj, devmin);
- #endif
- #ifndef WIN32
- if (mknod(filename, mode | S_IFBLK,
- compat_makedev(devmaj, devmin)) == -1)
- #else
- (void)devmin;
- (void)devmaj;
- (void)mode;
- #endif
- {
- #ifdef DEBUG
- perror("mknod()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifndef WIN32
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- #endif
- }
- /* directory */
- int
- tar_extract_dir(TAR *t, char *realname)
- {
- mode_t mode;
- char *filename;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- size_t len = 0;
- if (!TH_ISDIR(t))
- {
- errno = EINVAL;
- return -1;
- }
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- /* Strip trailing '/'...it confuses some Unixes (and BeOS)... */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- len = strlen(buf);
- if ((len > 0) && (buf[len-1] == '/'))
- {
- buf[len-1] = '\0';
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (mode %04o, directory)\n", filename,
- mode);
- #endif
- #ifdef WIN32
- if (mkdir(buf) == -1)
- #else
- if (mkdir(buf, mode & 07777) == -1)
- #endif
- {
- #ifdef __BORLANDC__
- /* There is a bug in the Borland Run time library which makes MKDIR
- return EACCES when it should return EEXIST
- if it is some other error besides directory exists
- then return false */
- if ( errno == EACCES)
- {
- errno = EEXIST;
- }
- #endif
- if (errno == EEXIST)
- {
- if (chmod(filename, mode & 07777) == -1)
- {
- #ifdef DEBUG
- perror("chmod()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- else
- {
- #ifdef DEBUG
- puts(" *** using existing directory");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return 1;
- }
- }
- else
- {
- #ifdef DEBUG
- perror("mkdir()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- }
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- }
- /* FIFO */
- int
- tar_extract_fifo(TAR *t, char *realname)
- {
- mode_t mode;
- char *filename;
- char buf[T_BLOCKSIZE];
- char *pathname = 0;
- if (!TH_ISFIFO(t))
- {
- errno = EINVAL;
- return -1;
- }
- if (realname)
- {
- filename = realname;
- }
- else
- {
- pathname = th_get_pathname(t);
- filename = pathname;
- }
- mode = th_get_mode(t);
- /* Make a copy of the string because dirname and mkdirhier may modify the
- * string */
- strncpy(buf, filename, sizeof(buf)-1);
- buf[sizeof(buf)-1] = 0;
- if (mkdirhier(dirname(buf)) == -1)
- {
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifdef DEBUG
- printf(" ==> extracting: %s (fifo)\n", filename);
- #endif
- #ifndef WIN32
- if (mkfifo(filename, mode & 07777) == -1)
- #else
- (void)mode;
- #endif
- {
- #ifdef DEBUG
- perror("mkfifo()");
- #endif
- if (pathname)
- {
- free(pathname);
- }
- return -1;
- }
- #ifndef WIN32
- if (pathname)
- {
- free(pathname);
- }
- return 0;
- #endif
- }
|