2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-03 14:05:15 +00:00
|
|
|
/*
|
2006-07-12 07:56:04 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2002-11-03 14:05:15 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-25 02:47:48 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
#include "unarchive.h"
|
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
void data_extract_all(archive_handle_t *archive_handle)
|
2002-09-25 02:47:48 +00:00
|
|
|
{
|
|
|
|
file_header_t *file_header = archive_handle->file_header;
|
|
|
|
int dst_fd;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
2006-08-03 15:41:12 +00:00
|
|
|
char *name = xstrdup(file_header->name);
|
2006-10-10 21:00:47 +00:00
|
|
|
bb_make_directory(dirname(name), -1, FILEUTILS_RECUR);
|
2002-09-30 20:14:57 +00:00
|
|
|
free(name);
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2002-09-25 02:47:48 +00:00
|
|
|
|
2003-08-28 19:12:23 +00:00
|
|
|
/* Check if the file already exists */
|
|
|
|
if (archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) {
|
|
|
|
/* Remove the existing entry if it exists */
|
2006-10-10 21:00:47 +00:00
|
|
|
if (((file_header->mode & S_IFMT) != S_IFDIR)
|
|
|
|
&& (unlink(file_header->name) == -1)
|
|
|
|
&& (errno != ENOENT)
|
|
|
|
) {
|
|
|
|
bb_perror_msg_and_die("cannot remove old file");
|
2003-08-28 19:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (archive_handle->flags & ARCHIVE_EXTRACT_NEWER) {
|
|
|
|
/* Remove the existing entry if its older than the extracted entry */
|
|
|
|
struct stat statbuf;
|
2003-11-15 23:44:31 +00:00
|
|
|
if (lstat(file_header->name, &statbuf) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2006-10-10 21:00:47 +00:00
|
|
|
bb_perror_msg_and_die("cannot stat old file");
|
2003-11-15 23:44:31 +00:00
|
|
|
}
|
2003-08-28 19:12:23 +00:00
|
|
|
}
|
2003-11-15 23:44:31 +00:00
|
|
|
else if (statbuf.st_mtime <= file_header->mtime) {
|
2003-08-28 19:12:23 +00:00
|
|
|
if (!(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
|
2006-10-10 21:00:47 +00:00
|
|
|
bb_error_msg("%s not created: newer or "
|
|
|
|
"same age file exists", file_header->name);
|
2003-08-28 19:12:23 +00:00
|
|
|
}
|
|
|
|
data_skip(archive_handle);
|
|
|
|
return;
|
|
|
|
}
|
2003-11-15 23:44:31 +00:00
|
|
|
else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
|
2006-10-10 21:00:47 +00:00
|
|
|
bb_perror_msg_and_die("cannot remove old file %s",
|
|
|
|
file_header->name);
|
2003-08-28 19:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-14 17:51:38 +00:00
|
|
|
/* Handle hard links separately
|
2003-11-27 00:01:43 +00:00
|
|
|
* We identified hard links as regular files of size 0 with a symlink */
|
2006-10-10 21:00:47 +00:00
|
|
|
if (S_ISREG(file_header->mode) && (file_header->link_name)
|
|
|
|
&& (file_header->size == 0)
|
|
|
|
) {
|
2003-08-14 02:55:15 +00:00
|
|
|
/* hard link */
|
|
|
|
res = link(file_header->link_name, file_header->name);
|
|
|
|
if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
|
2006-10-10 21:00:47 +00:00
|
|
|
bb_perror_msg("cannot create hard link");
|
2003-08-14 02:55:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Create the filesystem entry */
|
2006-09-27 19:51:06 +00:00
|
|
|
switch (file_header->mode & S_IFMT) {
|
2006-10-10 21:00:47 +00:00
|
|
|
case S_IFREG: {
|
|
|
|
/* Regular file */
|
|
|
|
dst_fd = xopen3(file_header->name, O_WRONLY | O_CREAT | O_EXCL,
|
|
|
|
file_header->mode);
|
2006-12-22 00:21:07 +00:00
|
|
|
bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
|
2006-10-10 21:00:47 +00:00
|
|
|
close(dst_fd);
|
|
|
|
break;
|
2006-12-22 00:21:07 +00:00
|
|
|
}
|
2006-10-10 21:00:47 +00:00
|
|
|
case S_IFDIR:
|
|
|
|
res = mkdir(file_header->name, file_header->mode);
|
|
|
|
if ((errno != EISDIR) && (res == -1)
|
|
|
|
&& !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("extract_archive: %s", file_header->name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_IFLNK:
|
|
|
|
/* Symlink */
|
|
|
|
res = symlink(file_header->link_name, file_header->name);
|
|
|
|
if ((res == -1)
|
|
|
|
&& !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("cannot create symlink "
|
|
|
|
"from %s to '%s'",
|
|
|
|
file_header->name,
|
|
|
|
file_header->link_name);
|
|
|
|
}
|
|
|
|
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->flags & ARCHIVE_EXTRACT_QUIET)
|
|
|
|
) {
|
|
|
|
bb_perror_msg("cannot create node %s", file_header->name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unrecognized file type");
|
2002-09-25 02:47:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 22:49:08 +00:00
|
|
|
if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_OWN)) {
|
|
|
|
lchown(file_header->name, file_header->uid, file_header->gid);
|
|
|
|
}
|
2007-02-12 22:06:56 +00:00
|
|
|
/* uclibc has no lchmod, glibc is even stranger -
|
|
|
|
* it has lchmod which seems to do nothing!
|
|
|
|
* so we use chmod... */
|
|
|
|
if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_PERM)
|
|
|
|
&& (file_header->mode & S_IFMT) != S_IFLNK
|
|
|
|
) {
|
|
|
|
chmod(file_header->name, file_header->mode);
|
|
|
|
}
|
2002-09-25 02:47:48 +00:00
|
|
|
|
|
|
|
if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
|
|
|
|
struct utimbuf t;
|
|
|
|
t.actime = t.modtime = file_header->mtime;
|
|
|
|
utime(file_header->name, &t);
|
|
|
|
}
|
|
|
|
}
|