All updates and fixes applied from upstream libtar as of March 1, 2016. Debug flag is disabled, however non-debug output now provides 1 line of useful output per object extracted. I've also merged some fixes from CyanogenMod's fork of libtar: From: Tom Marshall <tdm@cyngn.com> Date: Thu, 11 Feb 2016 16:24:40 -0800 Subject: libtar: Cleanup, secure, and extend numeric fields Commit: e18b457ea1cbf6be1adc3b75450ed1c737cd82ea From: Tom Marshall <tdm@cyngn.com> Date: Thu, 11 Feb 2016 12:49:30 -0800 Subject: libtar: Make file sizes 64-bit clean Commit: e628c2025549a24018bc568351465130a05daafb From: Tom Marshall <tdm@cyngn.com> Date: Thu, 17 Apr 2014 09:39:25 -0700 Subject: libtar: Add methods for in-memory files Commit: 8ec5627a8ff0a91724c6d5b344f0e887da922527 From: Tom Marshall <tdm@cyngn.com> Date: Wed, 2 Jul 2014 09:34:40 -0700 Subject: libtar: Fix hardlink extract Commit: 166d83a51e0c51abcea37694dbd7df92d03c1f56 From: philz-cwm6 <phytowardt@gmail.com> Date: Sat, 26 Apr 2014 01:11:35 +0200 Subject: libtar: Various bug fixes and enhancements Commit: a271d763e94235ccee9ecaabdb52bf4b9b2f8c06 (Some of this was not merged in, as better solutions were available from upstream libtar) From: Tom Marshall <tdm@cyngn.com> Date: Wed, 9 Apr 2014 09:35:54 -0700 Subject: libtar: Add const qualifiers to reduce compile warnings Commit: 0600afa19fe827d06d3fcf24a7aabd52dbf487b4 Change-Id: I6d008cb6fdf950f835bbed63aeb8727cc5c86083
182 lines
3.5 KiB
C
182 lines
3.5 KiB
C
/*
|
|
** 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 <internal.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/param.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
|
|
#ifdef STDC_HEADERS
|
|
# include <string.h>
|
|
#endif
|
|
|
|
|
|
int
|
|
tar_extract_glob(TAR *t, char *globname, char *prefix)
|
|
{
|
|
char *filename;
|
|
char buf[MAXPATHLEN];
|
|
int i, fd = 0;
|
|
|
|
while ((i = th_read(t)) == 0)
|
|
{
|
|
filename = th_get_pathname(t);
|
|
if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD))
|
|
{
|
|
if (TH_ISREG(t) && tar_skip_regfile(t))
|
|
return -1;
|
|
continue;
|
|
}
|
|
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));
|
|
if (tar_extract_file(t, buf, prefix, &fd) != 0)
|
|
return -1;
|
|
}
|
|
|
|
return (i == 1 ? 0 : -1);
|
|
}
|
|
|
|
|
|
int
|
|
tar_extract_all(TAR *t, char *prefix, const int *progress_fd)
|
|
{
|
|
char *filename;
|
|
char buf[MAXPATHLEN];
|
|
int i;
|
|
|
|
#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
|
|
filename = th_get_pathname(t);
|
|
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));
|
|
#ifdef DEBUG
|
|
printf(" tar_extract_all(): calling tar_extract_file(t, "
|
|
"\"%s\")\n", buf);
|
|
#endif
|
|
if (tar_extract_file(t, buf, prefix, progress_fd) != 0)
|
|
return -1;
|
|
}
|
|
|
|
return (i == 1 ? 0 : -1);
|
|
}
|
|
|
|
|
|
int
|
|
tar_append_tree(TAR *t, char *realdir, char *savedir)
|
|
{
|
|
char realpath[MAXPATHLEN];
|
|
char savepath[MAXPATHLEN];
|
|
struct dirent *dent;
|
|
DIR *dp;
|
|
struct stat s;
|
|
|
|
#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
|
|
|
|
dp = opendir(realdir);
|
|
if (dp == NULL)
|
|
{
|
|
if (errno == ENOTDIR)
|
|
return 0;
|
|
return -1;
|
|
}
|
|
while ((dent = readdir(dp)) != NULL)
|
|
{
|
|
if (strcmp(dent->d_name, ".") == 0 ||
|
|
strcmp(dent->d_name, "..") == 0)
|
|
continue;
|
|
|
|
snprintf(realpath, MAXPATHLEN, "%s/%s", realdir,
|
|
dent->d_name);
|
|
if (savedir)
|
|
snprintf(savepath, MAXPATHLEN, "%s/%s", savedir,
|
|
dent->d_name);
|
|
|
|
if (lstat(realpath, &s) != 0)
|
|
return -1;
|
|
|
|
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;
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int
|
|
tar_find(TAR *t, char *searchstr)
|
|
{
|
|
if (!searchstr)
|
|
return 0;
|
|
|
|
char *filename;
|
|
int i, entryfound = 0;
|
|
#ifdef DEBUG
|
|
printf("==> tar_find(0x%lx, %s)\n", (long unsigned int)t, searchstr);
|
|
#endif
|
|
while ((i = th_read(t)) == 0) {
|
|
filename = th_get_pathname(t);
|
|
if (fnmatch(searchstr, filename, FNM_FILE_NAME | FNM_PERIOD) == 0) {
|
|
entryfound++;
|
|
#ifdef DEBUG
|
|
printf("Found matching entry: %s\n", filename);
|
|
#endif
|
|
break;
|
|
}
|
|
}
|
|
#ifdef DEBUG
|
|
if (!entryfound)
|
|
printf("No matching entry found.\n");
|
|
#endif
|
|
|
|
return entryfound;
|
|
}
|