2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-03 19:35:15 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-11-03 19:35:15 +05:30
|
|
|
*/
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
#include "libbb.h"
|
2011-09-22 16:15:14 +05:30
|
|
|
#include "bb_archive.h"
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
2002-09-25 08:17:48 +05:30
|
|
|
{
|
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
|
|
|
int dst_fd;
|
|
|
|
int res;
|
|
|
|
|
2010-03-19 19:18:51 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_SELINUX
|
|
|
|
char *sctx = archive_handle->tar__next_file_sctx;
|
|
|
|
if (!sctx)
|
|
|
|
sctx = archive_handle->tar__global_sctx;
|
|
|
|
if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
|
|
|
|
setfscreatecon(sctx);
|
|
|
|
free(archive_handle->tar__next_file_sctx);
|
|
|
|
archive_handle->tar__next_file_sctx = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-28 10:34:09 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
2009-11-29 12:15:33 +05:30
|
|
|
char *slash = strrchr(file_header->name, '/');
|
|
|
|
if (slash) {
|
|
|
|
*slash = '\0';
|
|
|
|
bb_make_directory(file_header->name, -1, FILEUTILS_RECUR);
|
|
|
|
*slash = '/';
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
|
2009-09-17 06:13:14 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
|
2008-04-05 08:14:30 +05:30
|
|
|
/* Remove the entry if it exists */
|
2010-04-09 14:22:52 +05:30
|
|
|
if (!S_ISDIR(file_header->mode)) {
|
|
|
|
/* Is it hardlink?
|
|
|
|
* We encode hard links as regular files of size 0 with a symlink */
|
|
|
|
if (S_ISREG(file_header->mode)
|
|
|
|
&& file_header->link_target
|
|
|
|
&& file_header->size == 0
|
|
|
|
) {
|
|
|
|
/* Ugly special case:
|
|
|
|
* tar cf t.tar hardlink1 hardlink2 hardlink1
|
|
|
|
* results in this tarball structure:
|
|
|
|
* hardlink1
|
|
|
|
* hardlink2 -> hardlink1
|
|
|
|
* hardlink1 -> hardlink1 <== !!!
|
|
|
|
*/
|
|
|
|
if (strcmp(file_header->link_target, file_header->name) == 0)
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
/* Proceed with deleting */
|
|
|
|
if (unlink(file_header->name) == -1
|
|
|
|
&& errno != ENOENT
|
|
|
|
) {
|
|
|
|
bb_perror_msg_and_die("can't remove old file %s",
|
|
|
|
file_header->name);
|
|
|
|
}
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
}
|
2008-06-28 10:34:09 +05:30
|
|
|
else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
|
2003-08-29 00:42:23 +05:30
|
|
|
/* Remove the existing entry if its older than the extracted entry */
|
2009-11-24 05:46:12 +05:30
|
|
|
struct stat existing_sb;
|
|
|
|
if (lstat(file_header->name, &existing_sb) == -1) {
|
2003-11-16 05:14:31 +05:30
|
|
|
if (errno != ENOENT) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg_and_die("can't stat old file");
|
2003-11-16 05:14:31 +05:30
|
|
|
}
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
2009-11-24 05:46:12 +05:30
|
|
|
else if (existing_sb.st_mtime >= file_header->mtime) {
|
2010-10-18 04:24:51 +05:30
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
&& !S_ISDIR(file_header->mode)
|
|
|
|
) {
|
2006-10-11 02:30:47 +05:30
|
|
|
bb_error_msg("%s not created: newer or "
|
|
|
|
"same age file exists", file_header->name);
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
data_skip(archive_handle);
|
2010-03-19 19:18:51 +05:30
|
|
|
goto ret;
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
2003-11-16 05:14:31 +05:30
|
|
|
else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg_and_die("can't remove old file %s",
|
2006-10-11 02:30:47 +05:30
|
|
|
file_header->name);
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-14 23:21:38 +05:30
|
|
|
/* Handle hard links separately
|
2010-04-09 14:22:52 +05:30
|
|
|
* We encode hard links as regular files of size 0 with a symlink */
|
2009-11-29 12:15:33 +05:30
|
|
|
if (S_ISREG(file_header->mode)
|
|
|
|
&& file_header->link_target
|
|
|
|
&& file_header->size == 0
|
2006-10-11 02:30:47 +05:30
|
|
|
) {
|
2003-08-14 08:25:15 +05:30
|
|
|
/* hard link */
|
2007-06-20 20:19:47 +05:30
|
|
|
res = link(file_header->link_target, file_header->name);
|
2008-06-28 10:34:09 +05:30
|
|
|
if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg("can't create %slink "
|
2007-06-20 20:19:47 +05:30
|
|
|
"from %s to %s", "hard",
|
|
|
|
file_header->name,
|
|
|
|
file_header->link_target);
|
2003-08-14 08:25:15 +05:30
|
|
|
}
|
2010-04-09 17:41:45 +05:30
|
|
|
/* Hardlinks have no separate mode/ownership, skip chown/chmod */
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the filesystem entry */
|
|
|
|
switch (file_header->mode & S_IFMT) {
|
|
|
|
case S_IFREG: {
|
|
|
|
/* Regular file */
|
|
|
|
int flags = O_WRONLY | O_CREAT | O_EXCL;
|
|
|
|
if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
dst_fd = xopen3(file_header->name,
|
|
|
|
flags,
|
|
|
|
file_header->mode
|
|
|
|
);
|
|
|
|
bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
|
|
|
|
close(dst_fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_IFDIR:
|
|
|
|
res = mkdir(file_header->name, file_header->mode);
|
|
|
|
if ((res == -1)
|
|
|
|
&& (errno != EISDIR) /* btw, Linux doesn't return this */
|
|
|
|
&& (errno != EEXIST)
|
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("can't make dir %s", file_header->name);
|
2006-12-22 05:51:07 +05:30
|
|
|
}
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
/* Symlink */
|
2010-07-04 07:56:55 +05:30
|
|
|
//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
|
2010-04-09 17:41:45 +05:30
|
|
|
res = symlink(file_header->link_target, file_header->name);
|
|
|
|
if ((res == -1)
|
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("can't create %slink "
|
|
|
|
"from %s to %s", "sym",
|
|
|
|
file_header->name,
|
|
|
|
file_header->link_target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_IFSOCK:
|
|
|
|
case S_IFBLK:
|
|
|
|
case S_IFCHR:
|
|
|
|
case S_IFIFO:
|
|
|
|
res = mknod(file_header->name, file_header->mode, file_header->device);
|
|
|
|
if ((res == -1)
|
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("can't create node %s", file_header->name);
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
2010-04-09 17:41:45 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unrecognized file type");
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
|
2010-06-18 05:30:55 +05:30
|
|
|
if (!S_ISLNK(file_header->mode)) {
|
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
|
2009-04-21 06:22:21 +05:30
|
|
|
uid_t uid = file_header->uid;
|
|
|
|
gid_t gid = file_header->gid;
|
2010-06-18 05:30:55 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
|
|
|
|
if (file_header->tar__uname) {
|
2009-11-29 12:15:33 +05:30
|
|
|
//TODO: cache last name/id pair?
|
2010-06-18 05:30:55 +05:30
|
|
|
struct passwd *pwd = getpwnam(file_header->tar__uname);
|
|
|
|
if (pwd) uid = pwd->pw_uid;
|
|
|
|
}
|
|
|
|
if (file_header->tar__gname) {
|
|
|
|
struct group *grp = getgrnam(file_header->tar__gname);
|
|
|
|
if (grp) gid = grp->gr_gid;
|
|
|
|
}
|
2009-04-21 06:22:21 +05:30
|
|
|
}
|
2010-06-18 05:30:55 +05:30
|
|
|
#endif
|
2009-11-29 12:15:33 +05:30
|
|
|
/* GNU tar 1.15.1 uses chown, not lchown */
|
2009-09-17 06:32:57 +05:30
|
|
|
chown(file_header->name, uid, gid);
|
2010-06-18 05:30:55 +05:30
|
|
|
}
|
2007-06-20 20:19:47 +05:30
|
|
|
/* uclibc has no lchmod, glibc is even stranger -
|
|
|
|
* it has lchmod which seems to do nothing!
|
|
|
|
* so we use chmod... */
|
2009-09-17 06:13:14 +05:30
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
|
2007-06-20 20:19:47 +05:30
|
|
|
chmod(file_header->name, file_header->mode);
|
|
|
|
}
|
2009-09-17 06:13:14 +05:30
|
|
|
if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
|
2009-11-30 00:10:36 +05:30
|
|
|
struct timeval t[2];
|
2009-11-15 06:58:56 +05:30
|
|
|
|
2009-11-30 00:10:36 +05:30
|
|
|
t[1].tv_sec = t[0].tv_sec = file_header->mtime;
|
|
|
|
t[1].tv_usec = t[0].tv_usec = 0;
|
|
|
|
utimes(file_header->name, t);
|
2007-06-20 20:19:47 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
2010-03-19 19:18:51 +05:30
|
|
|
|
|
|
|
ret: ;
|
|
|
|
#if ENABLE_FEATURE_TAR_SELINUX
|
|
|
|
if (sctx) {
|
|
|
|
/* reset the context after creating an entry */
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|