blob: ae242df64256309db007d63fe655e3e2635aa090 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath237ae422002-11-03 14:05:15 +00002/*
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath237ae422002-11-03 14:05:15 +00004 */
5
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00006#include "libbb.h"
7#include "unarchive.h"
8
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00009void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010{
11 file_header_t *file_header = archive_handle->file_header;
12 int dst_fd;
13 int res;
14
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000015 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010016 char *slash = strrchr(file_header->name, '/');
17 if (slash) {
18 *slash = '\0';
19 bb_make_directory(file_header->name, -1, FILEUTILS_RECUR);
20 *slash = '/';
21 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000022 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023
Denys Vlasenkod57d6262009-09-17 02:43:14 +020024 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000025 /* Remove the entry if it exists */
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020026 if ((!S_ISDIR(file_header->mode))
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000027 && (unlink(file_header->name) == -1)
28 && (errno != ENOENT)
29 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020030 bb_perror_msg_and_die("can't remove old file %s",
Denis Vlasenko75103842007-06-20 14:49:47 +000031 file_header->name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000032 }
33 }
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000034 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000035 /* Remove the existing entry if its older than the extracted entry */
Denys Vlasenko6ccaa232009-11-24 01:16:12 +010036 struct stat existing_sb;
37 if (lstat(file_header->name, &existing_sb) == -1) {
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +000038 if (errno != ENOENT) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020039 bb_perror_msg_and_die("can't stat old file");
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +000040 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000041 }
Denys Vlasenko6ccaa232009-11-24 01:16:12 +010042 else if (existing_sb.st_mtime >= file_header->mtime) {
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000043 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000044 bb_error_msg("%s not created: newer or "
45 "same age file exists", file_header->name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000046 }
47 data_skip(archive_handle);
48 return;
49 }
Glenn L McGrath8dc8cb12003-11-15 23:44:31 +000050 else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020051 bb_perror_msg_and_die("can't remove old file %s",
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000052 file_header->name);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000053 }
54 }
55
Eric Andersenaff114c2004-04-14 17:51:38 +000056 /* Handle hard links separately
Glenn L McGrathe39ee012003-11-27 00:01:43 +000057 * We identified hard links as regular files of size 0 with a symlink */
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +010058 if (S_ISREG(file_header->mode)
59 && file_header->link_target
60 && file_header->size == 0
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000061 ) {
Glenn L McGrath3d5828f2003-08-14 02:55:15 +000062 /* hard link */
Denis Vlasenko75103842007-06-20 14:49:47 +000063 res = link(file_header->link_target, file_header->name);
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000064 if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020065 bb_perror_msg("can't create %slink "
Denis Vlasenko75103842007-06-20 14:49:47 +000066 "from %s to %s", "hard",
67 file_header->name,
68 file_header->link_target);
Glenn L McGrath3d5828f2003-08-14 02:55:15 +000069 }
70 } else {
71 /* Create the filesystem entry */
Denis Vlasenkoc16bd212006-09-27 19:51:06 +000072 switch (file_header->mode & S_IFMT) {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000073 case S_IFREG: {
74 /* Regular file */
Denys Vlasenkod57d6262009-09-17 02:43:14 +020075 dst_fd = xopen3(file_header->name,
76 O_WRONLY | O_CREAT | O_EXCL,
77 file_header->mode
78 );
Denis Vlasenko714701c2006-12-22 00:21:07 +000079 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000080 close(dst_fd);
81 break;
Denis Vlasenko714701c2006-12-22 00:21:07 +000082 }
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000083 case S_IFDIR:
84 res = mkdir(file_header->name, file_header->mode);
Denis Vlasenkod83676e2008-10-17 14:03:56 +000085 if ((res == -1)
86 && (errno != EISDIR) /* btw, Linux doesn't return this */
87 && (errno != EEXIST)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000088 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000089 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020090 bb_perror_msg("can't make dir %s", file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000091 }
92 break;
93 case S_IFLNK:
94 /* Symlink */
Denis Vlasenko75103842007-06-20 14:49:47 +000095 res = symlink(file_header->link_target, file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000096 if ((res == -1)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000097 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000098 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020099 bb_perror_msg("can't create %slink "
Denis Vlasenko75103842007-06-20 14:49:47 +0000100 "from %s to %s", "sym",
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000101 file_header->name,
Denis Vlasenko75103842007-06-20 14:49:47 +0000102 file_header->link_target);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000103 }
104 break;
105 case S_IFSOCK:
106 case S_IFBLK:
107 case S_IFCHR:
108 case S_IFIFO:
109 res = mknod(file_header->name, file_header->mode, file_header->device);
110 if ((res == -1)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000111 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000112 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200113 bb_perror_msg("can't create node %s", file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000114 }
115 break;
116 default:
117 bb_error_msg_and_die("unrecognized file type");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000118 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000119 }
120
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200121 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
Mike Frysinger4f239b12009-05-05 07:00:27 -0400122#if ENABLE_FEATURE_TAR_UNAME_GNAME
123 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000124 uid_t uid = file_header->uid;
125 gid_t gid = file_header->gid;
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000126
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000127 if (file_header->uname) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100128//TODO: cache last name/id pair?
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000129 struct passwd *pwd = getpwnam(file_header->uname);
130 if (pwd) uid = pwd->pw_uid;
131 }
132 if (file_header->gname) {
133 struct group *grp = getgrnam(file_header->gname);
134 if (grp) gid = grp->gr_gid;
135 }
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100136 /* GNU tar 1.15.1 uses chown, not lchown */
Denys Vlasenko885583e2009-09-17 03:02:57 +0200137 chown(file_header->name, uid, gid);
Mike Frysinger4f239b12009-05-05 07:00:27 -0400138 } else
139#endif
Denys Vlasenko885583e2009-09-17 03:02:57 +0200140 chown(file_header->name, file_header->uid, file_header->gid);
Rob Landleyf3d6c942005-10-27 22:49:08 +0000141 }
Denys Vlasenkob9762922009-08-30 01:10:17 +0200142 if (!S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000143 /* uclibc has no lchmod, glibc is even stranger -
144 * it has lchmod which seems to do nothing!
145 * so we use chmod... */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200146 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000147 chmod(file_header->name, file_header->mode);
148 }
149 /* same for utime */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200150 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100151 struct timeval t[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100152
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100153 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
154 t[1].tv_usec = t[0].tv_usec = 0;
155 utimes(file_header->name, t);
Denis Vlasenko75103842007-06-20 14:49:47 +0000156 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000157 }
158}