2005-12-28 18:18:37 +03:00
|
|
|
/*
|
|
|
|
** Copyright 1998-2003 University of Illinois Board of Trustees
|
|
|
|
** Copyright 1998-2003 Mark D. Roth
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** wrapper.c - libtar high-level wrapper code
|
|
|
|
**
|
|
|
|
** Mark D. Roth <roth@uiuc.edu>
|
|
|
|
** Campus Information Technologies and Educational Services
|
|
|
|
** University of Illinois at Urbana-Champaign
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtarint/internal.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-02-06 17:31:55 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
#include <libtar/compat.h>
|
2005-12-30 18:35:23 +03:00
|
|
|
#if defined(HAVE_SYS_PARAM_H)
|
2009-06-10 19:49:02 +04:00
|
|
|
# include <sys/param.h>
|
2005-12-30 18:35:23 +03:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_DIRENT_H)
|
2005-12-28 18:18:37 +03:00
|
|
|
#include <dirent.h>
|
2005-12-30 18:35:23 +03:00
|
|
|
#else
|
|
|
|
#include <libtarint/filesystem.h>
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_FNMATCH_H
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
tar_extract_glob(TAR *t, char *globname, char *prefix)
|
|
|
|
{
|
|
|
|
char *filename;
|
2005-12-28 22:58:07 +03:00
|
|
|
char buf[TAR_MAXPATHLEN];
|
2005-12-28 18:18:37 +03:00
|
|
|
int i;
|
2006-02-06 17:31:55 +03:00
|
|
|
char *pathname;
|
2005-12-28 18:18:37 +03:00
|
|
|
|
|
|
|
while ((i = th_read(t)) == 0)
|
|
|
|
{
|
2006-02-03 19:48:44 +03:00
|
|
|
pathname = th_get_pathname(t);
|
|
|
|
filename = pathname;
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
|
|
|
|
{
|
2006-02-03 19:48:44 +03:00
|
|
|
if (pathname)
|
|
|
|
{
|
|
|
|
free(pathname);
|
|
|
|
}
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (TH_ISREG(t) && tar_skip_regfile(t))
|
|
|
|
return -1;
|
|
|
|
continue;
|
|
|
|
}
|
2006-02-03 19:48:44 +03:00
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (t->options & TAR_VERBOSE)
|
|
|
|
th_print_long_ls(t);
|
2006-02-03 19:48:44 +03:00
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (prefix != NULL)
|
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
|
|
|
|
else
|
|
|
|
strlcpy(buf, filename, sizeof(buf));
|
2006-02-03 19:48:44 +03:00
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (tar_extract_file(t, filename) != 0)
|
2006-02-03 19:48:44 +03:00
|
|
|
{
|
|
|
|
if (pathname)
|
|
|
|
{
|
|
|
|
free(pathname);
|
|
|
|
}
|
2005-12-28 18:18:37 +03:00
|
|
|
return -1;
|
2006-02-03 19:48:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pathname)
|
|
|
|
{
|
|
|
|
free(pathname);
|
|
|
|
}
|
2005-12-28 18:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (i == 1 ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
tar_extract_all(TAR *t, char *prefix)
|
|
|
|
{
|
|
|
|
char *filename;
|
2005-12-28 22:58:07 +03:00
|
|
|
char buf[TAR_MAXPATHLEN];
|
2005-12-28 18:18:37 +03:00
|
|
|
int i;
|
2006-02-06 17:31:55 +03:00
|
|
|
char *pathname;
|
2005-12-28 18:18:37 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("==> tar_extract_all(TAR *t, \"%s\")\n",
|
|
|
|
(prefix ? prefix : "(null)"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while ((i = th_read(t)) == 0)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_extract_all(): calling th_get_pathname()");
|
|
|
|
#endif
|
2006-02-03 19:48:44 +03:00
|
|
|
|
|
|
|
pathname = th_get_pathname(t);
|
|
|
|
filename = pathname;
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (t->options & TAR_VERBOSE)
|
|
|
|
th_print_long_ls(t);
|
|
|
|
if (prefix != NULL)
|
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", prefix, filename);
|
|
|
|
else
|
|
|
|
strlcpy(buf, filename, sizeof(buf));
|
2006-02-03 19:48:44 +03:00
|
|
|
|
|
|
|
if (pathname)
|
|
|
|
{
|
|
|
|
free(pathname);
|
|
|
|
}
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" tar_extract_all(): calling tar_extract_file(t, "
|
|
|
|
"\"%s\")\n", buf);
|
|
|
|
#endif
|
2006-02-03 19:48:44 +03:00
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
if (tar_extract_file(t, buf) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (i == 1 ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
tar_append_tree(TAR *t, char *realdir, char *savedir)
|
|
|
|
{
|
2005-12-28 22:58:07 +03:00
|
|
|
char realpath[TAR_MAXPATHLEN];
|
|
|
|
char savepath[TAR_MAXPATHLEN];
|
2005-12-30 18:35:23 +03:00
|
|
|
size_t plen;
|
|
|
|
#if defined(HAVE_DIRENT_H)
|
2005-12-28 18:18:37 +03:00
|
|
|
struct dirent *dent;
|
|
|
|
DIR *dp;
|
|
|
|
#else
|
|
|
|
kwDirEntry * dent;
|
|
|
|
kwDirectory *dp;
|
|
|
|
#endif
|
|
|
|
struct stat s;
|
2005-12-30 18:35:23 +03:00
|
|
|
strncpy(realpath, realdir, sizeof(realpath));
|
|
|
|
realpath[sizeof(realpath)-1] = 0;
|
|
|
|
plen = strlen(realpath);
|
|
|
|
if ( realpath[plen-1] == '/' )
|
|
|
|
{
|
|
|
|
realpath[plen-1] = 0;
|
|
|
|
}
|
|
|
|
|
2005-12-28 18:18:37 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
|
|
|
|
t, realdir, (savedir ? savedir : "[NULL]"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (tar_append_file(t, realdir, savedir) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts(" tar_append_tree(): done with tar_append_file()...");
|
|
|
|
#endif
|
|
|
|
|
2005-12-30 18:35:23 +03:00
|
|
|
if ( stat(realpath, &s) != 0 )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (
|
2005-12-28 22:58:07 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2005-12-30 18:35:23 +03:00
|
|
|
(s.st_mode & _S_IFDIR) == 0
|
2005-12-28 18:18:37 +03:00
|
|
|
#else
|
2005-12-30 18:35:23 +03:00
|
|
|
!S_ISDIR(s.st_mode)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return 0;
|
|
|
|
#if defined(HAVE_DIRENT_H)
|
2005-12-28 18:18:37 +03:00
|
|
|
dp = opendir(realdir);
|
2005-12-30 18:35:23 +03:00
|
|
|
#else
|
|
|
|
dp = kwOpenDir(realdir);
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (dp == NULL)
|
|
|
|
{
|
|
|
|
if (errno == ENOTDIR)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
2005-12-30 18:35:23 +03:00
|
|
|
#if defined(HAVE_DIRENT_H)
|
2005-12-28 18:18:37 +03:00
|
|
|
while ((dent = readdir(dp)) != NULL)
|
2005-12-30 18:35:23 +03:00
|
|
|
#else
|
|
|
|
while ((dent = kwReadDir(dp)) != NULL)
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (strcmp(dent->d_name, ".") == 0 ||
|
|
|
|
strcmp(dent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
2005-12-28 22:58:07 +03:00
|
|
|
snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir,
|
2005-12-28 18:18:37 +03:00
|
|
|
dent->d_name);
|
|
|
|
if (savedir)
|
2005-12-28 22:58:07 +03:00
|
|
|
snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir,
|
2005-12-28 18:18:37 +03:00
|
|
|
dent->d_name);
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
if (lstat(realpath, &s) != 0)
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
if (stat(realpath, &s) != 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
if (S_ISDIR(s.st_mode))
|
|
|
|
{
|
|
|
|
if (tar_append_tree(t, realpath,
|
|
|
|
(savedir ? savepath : NULL)) != 0)
|
|
|
|
return -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_append_file(t, realpath,
|
|
|
|
(savedir ? savepath : NULL)) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-30 18:35:23 +03:00
|
|
|
#if defined(HAVE_DIRENT_H)
|
2005-12-28 18:18:37 +03:00
|
|
|
closedir(dp);
|
2005-12-30 18:35:23 +03:00
|
|
|
#else
|
|
|
|
kwCloseDir(dp);
|
2005-12-28 18:18:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|