2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-03 19:35:15 +05:30
|
|
|
/*
|
2006-07-12 13:26:04 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-03 19:35:15 +05:30
|
|
|
*/
|
|
|
|
|
2002-09-25 08:17:48 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include "unarchive.h"
|
|
|
|
|
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;
|
|
|
|
|
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 */
|
2009-07-05 08:20:36 +05:30
|
|
|
if ((!S_ISDIR(file_header->mode))
|
2006-10-11 02:30:47 +05:30
|
|
|
&& (unlink(file_header->name) == -1)
|
|
|
|
&& (errno != ENOENT)
|
|
|
|
) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg_and_die("can't remove old file %s",
|
2007-06-20 20:19:47 +05:30
|
|
|
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) {
|
2008-06-28 10:34:09 +05:30
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
|
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);
|
|
|
|
return;
|
|
|
|
}
|
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
|
2003-11-27 05:31:43 +05:30
|
|
|
* We identified 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
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Create the filesystem entry */
|
2006-09-28 01:21:06 +05:30
|
|
|
switch (file_header->mode & S_IFMT) {
|
2006-10-11 02:30:47 +05:30
|
|
|
case S_IFREG: {
|
|
|
|
/* Regular file */
|
2009-12-17 03:48:59 +05:30
|
|
|
int flags = O_WRONLY | O_CREAT | O_EXCL;
|
|
|
|
if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
|
|
|
|
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
2009-09-17 06:13:14 +05:30
|
|
|
dst_fd = xopen3(file_header->name,
|
2009-12-17 03:48:59 +05:30
|
|
|
flags,
|
2009-09-17 06:13:14 +05:30
|
|
|
file_header->mode
|
|
|
|
);
|
2006-12-22 05:51:07 +05:30
|
|
|
bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
|
2006-10-11 02:30:47 +05:30
|
|
|
close(dst_fd);
|
|
|
|
break;
|
2006-12-22 05:51:07 +05:30
|
|
|
}
|
2006-10-11 02:30:47 +05:30
|
|
|
case S_IFDIR:
|
|
|
|
res = mkdir(file_header->name, file_header->mode);
|
2008-10-17 19:33:56 +05:30
|
|
|
if ((res == -1)
|
|
|
|
&& (errno != EISDIR) /* btw, Linux doesn't return this */
|
|
|
|
&& (errno != EEXIST)
|
2008-06-28 10:34:09 +05:30
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
2006-10-11 02:30:47 +05:30
|
|
|
) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg("can't make dir %s", file_header->name);
|
2006-10-11 02:30:47 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
/* Symlink */
|
2007-06-20 20:19:47 +05:30
|
|
|
res = symlink(file_header->link_target, file_header->name);
|
2006-10-11 02:30:47 +05:30
|
|
|
if ((res == -1)
|
2008-06-28 10:34:09 +05:30
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
2006-10-11 02:30:47 +05:30
|
|
|
) {
|
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", "sym",
|
2006-10-11 02:30:47 +05:30
|
|
|
file_header->name,
|
2007-06-20 20:19:47 +05:30
|
|
|
file_header->link_target);
|
2006-10-11 02:30:47 +05:30
|
|
|
}
|
|
|
|
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)
|
2008-06-28 10:34:09 +05:30
|
|
|
&& !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
|
2006-10-11 02:30:47 +05:30
|
|
|
) {
|
2009-09-17 06:13:14 +05:30
|
|
|
bb_perror_msg("can't create node %s", file_header->name);
|
2006-10-11 02:30:47 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unrecognized file type");
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 06:13:14 +05:30
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
|
2009-05-05 16:30:27 +05:30
|
|
|
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
|
|
|
if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
|
2009-04-21 06:22:21 +05:30
|
|
|
uid_t uid = file_header->uid;
|
|
|
|
gid_t gid = file_header->gid;
|
2008-02-15 02:07:54 +05:30
|
|
|
|
2010-01-06 15:23:17 +05:30
|
|
|
if (file_header->tar__uname) {
|
2009-11-29 12:15:33 +05:30
|
|
|
//TODO: cache last name/id pair?
|
2010-01-06 15:23:17 +05:30
|
|
|
struct passwd *pwd = getpwnam(file_header->tar__uname);
|
2009-04-21 06:22:21 +05:30
|
|
|
if (pwd) uid = pwd->pw_uid;
|
|
|
|
}
|
2010-01-06 15:23:17 +05:30
|
|
|
if (file_header->tar__gname) {
|
|
|
|
struct group *grp = getgrnam(file_header->tar__gname);
|
2009-04-21 06:22:21 +05:30
|
|
|
if (grp) gid = grp->gr_gid;
|
|
|
|
}
|
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);
|
2009-05-05 16:30:27 +05:30
|
|
|
} else
|
|
|
|
#endif
|
2009-09-17 06:32:57 +05:30
|
|
|
chown(file_header->name, file_header->uid, file_header->gid);
|
2005-10-28 04:19:08 +05:30
|
|
|
}
|
2009-08-30 04:40:17 +05:30
|
|
|
if (!S_ISLNK(file_header->mode)) {
|
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);
|
|
|
|
}
|
|
|
|
/* same for utime */
|
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
|
|
|
}
|
|
|
|
}
|