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) {
|
2006-08-03 21:11:12 +05:30
|
|
|
char *name = xstrdup(file_header->name);
|
2006-10-11 02:30:47 +05:30
|
|
|
bb_make_directory(dirname(name), -1, FILEUTILS_RECUR);
|
2002-10-01 01:44:57 +05:30
|
|
|
free(name);
|
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 */
|
|
|
|
struct stat statbuf;
|
2003-11-16 05:14:31 +05:30
|
|
|
if (lstat(file_header->name, &statbuf) == -1) {
|
|
|
|
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
|
|
|
}
|
2003-11-16 05:14:31 +05:30
|
|
|
else if (statbuf.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 */
|
2007-06-20 20:19:47 +05:30
|
|
|
if (S_ISREG(file_header->mode) && (file_header->link_target)
|
2006-10-11 02:30:47 +05:30
|
|
|
&& (file_header->size == 0)
|
|
|
|
) {
|
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-09-17 06:13:14 +05:30
|
|
|
dst_fd = xopen3(file_header->name,
|
|
|
|
O_WRONLY | O_CREAT | O_EXCL,
|
|
|
|
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
|
|
|
|
2009-04-21 06:22:21 +05:30
|
|
|
if (file_header->uname) {
|
|
|
|
struct passwd *pwd = getpwnam(file_header->uname);
|
|
|
|
if (pwd) uid = pwd->pw_uid;
|
|
|
|
}
|
|
|
|
if (file_header->gname) {
|
|
|
|
struct group *grp = getgrnam(file_header->gname);
|
|
|
|
if (grp) gid = grp->gr_gid;
|
|
|
|
}
|
2009-09-17 06:32:57 +05:30
|
|
|
/* GNU tar 1.15.1 use chown, not lchown */
|
|
|
|
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-15 06:58:56 +05:30
|
|
|
struct timeval t;
|
|
|
|
|
|
|
|
t.tv_sec = file_header->mtime;
|
|
|
|
t.tv_usec = 0;
|
2009-11-15 04:42:53 +05:30
|
|
|
utimes(file_header->name, &t);
|
2007-06-20 20:19:47 +05:30
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
}
|