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"
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
void 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;
|
|
|
|
|
|
|
|
if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
2006-08-03 21:11:12 +05:30
|
|
|
char *name = xstrdup(file_header->name);
|
2004-10-13 11:55:52 +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
|
|
|
|
2003-08-29 00:42:23 +05:30
|
|
|
/* Check if the file already exists */
|
|
|
|
if (archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) {
|
|
|
|
/* Remove the existing entry if it exists */
|
2003-11-20 13:30:38 +05:30
|
|
|
if (((file_header->mode & S_IFMT) != S_IFDIR) && (unlink(file_header->name) == -1) && (errno != ENOENT)) {
|
2003-08-29 00:42:23 +05:30
|
|
|
bb_perror_msg_and_die("Couldnt remove old file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (archive_handle->flags & ARCHIVE_EXTRACT_NEWER) {
|
|
|
|
/* 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) {
|
|
|
|
bb_perror_msg_and_die("Couldnt stat old file");
|
|
|
|
}
|
2003-08-29 00:42:23 +05:30
|
|
|
}
|
2003-11-16 05:14:31 +05:30
|
|
|
else if (statbuf.st_mtime <= file_header->mtime) {
|
2003-08-29 00:42:23 +05:30
|
|
|
if (!(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
|
|
|
|
bb_error_msg("%s not created: newer or same age file exists", file_header->name);
|
|
|
|
}
|
|
|
|
data_skip(archive_handle);
|
|
|
|
return;
|
|
|
|
}
|
2003-11-16 05:14:31 +05:30
|
|
|
else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
|
|
|
|
bb_perror_msg_and_die("Couldnt remove old file %s", 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 */
|
|
|
|
if (S_ISREG(file_header->mode) && (file_header->link_name) && (file_header->size == 0)) {
|
2003-08-14 08:25:15 +05:30
|
|
|
/* hard link */
|
|
|
|
res = link(file_header->link_name, file_header->name);
|
|
|
|
if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
|
|
|
|
bb_perror_msg("Couldnt create hard link");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Create the filesystem entry */
|
2006-09-28 01:21:06 +05:30
|
|
|
switch (file_header->mode & S_IFMT) {
|
2003-08-14 08:25:15 +05:30
|
|
|
case S_IFREG: {
|
2002-09-25 08:17:48 +05:30
|
|
|
/* Regular file */
|
2006-08-04 22:56:58 +05:30
|
|
|
dst_fd = xopen3(file_header->name, O_WRONLY | O_CREAT | O_EXCL,
|
|
|
|
file_header->mode);
|
2003-11-22 03:54:57 +05:30
|
|
|
bb_copyfd_size(archive_handle->src_fd, dst_fd, file_header->size);
|
2002-09-25 08:17:48 +05:30
|
|
|
close(dst_fd);
|
2003-08-14 08:25:15 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
case S_IFDIR:
|
|
|
|
res = mkdir(file_header->name, file_header->mode);
|
2003-11-16 05:14:31 +05:30
|
|
|
if ((errno != EISDIR) && (res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
|
2003-08-14 08:25:15 +05:30
|
|
|
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("Unrecognised file type");
|
2002-09-25 08:17:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-28 04:19:08 +05:30
|
|
|
if (!(archive_handle->flags & ARCHIVE_NOPRESERVE_OWN)) {
|
|
|
|
lchown(file_header->name, file_header->uid, file_header->gid);
|
|
|
|
}
|
2002-09-25 08:17:48 +05:30
|
|
|
|
|
|
|
if (archive_handle->flags & ARCHIVE_PRESERVE_DATE) {
|
|
|
|
struct utimbuf t;
|
|
|
|
t.actime = t.modtime = file_header->mtime;
|
|
|
|
utime(file_header->name, &t);
|
|
|
|
}
|
|
|
|
}
|