blob: 58b05335b0969cc006e2af4a989dfa0685d43c3a [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 Vlasenko8a936cf2009-12-16 23:18:59 +010075 int flags = O_WRONLY | O_CREAT | O_EXCL;
76 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
77 flags = O_WRONLY | O_CREAT | O_TRUNC;
Denys Vlasenkod57d6262009-09-17 02:43:14 +020078 dst_fd = xopen3(file_header->name,
Denys Vlasenko8a936cf2009-12-16 23:18:59 +010079 flags,
Denys Vlasenkod57d6262009-09-17 02:43:14 +020080 file_header->mode
81 );
Denis Vlasenko714701c2006-12-22 00:21:07 +000082 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000083 close(dst_fd);
84 break;
Denis Vlasenko714701c2006-12-22 00:21:07 +000085 }
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000086 case S_IFDIR:
87 res = mkdir(file_header->name, file_header->mode);
Denis Vlasenkod83676e2008-10-17 14:03:56 +000088 if ((res == -1)
89 && (errno != EISDIR) /* btw, Linux doesn't return this */
90 && (errno != EEXIST)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000091 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000092 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +020093 bb_perror_msg("can't make dir %s", file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000094 }
95 break;
96 case S_IFLNK:
97 /* Symlink */
Denis Vlasenko75103842007-06-20 14:49:47 +000098 res = symlink(file_header->link_target, file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000099 if ((res == -1)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000100 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000101 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200102 bb_perror_msg("can't create %slink "
Denis Vlasenko75103842007-06-20 14:49:47 +0000103 "from %s to %s", "sym",
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000104 file_header->name,
Denis Vlasenko75103842007-06-20 14:49:47 +0000105 file_header->link_target);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000106 }
107 break;
108 case S_IFSOCK:
109 case S_IFBLK:
110 case S_IFCHR:
111 case S_IFIFO:
112 res = mknod(file_header->name, file_header->mode, file_header->device);
113 if ((res == -1)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000114 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000115 ) {
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200116 bb_perror_msg("can't create node %s", file_header->name);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +0000117 }
118 break;
119 default:
120 bb_error_msg_and_die("unrecognized file type");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000121 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000122 }
123
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200124 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
Mike Frysinger4f239b12009-05-05 07:00:27 -0400125#if ENABLE_FEATURE_TAR_UNAME_GNAME
126 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000127 uid_t uid = file_header->uid;
128 gid_t gid = file_header->gid;
Denis Vlasenkoe00e5022008-02-14 20:37:54 +0000129
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100130 if (file_header->tar__uname) {
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100131//TODO: cache last name/id pair?
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100132 struct passwd *pwd = getpwnam(file_header->tar__uname);
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000133 if (pwd) uid = pwd->pw_uid;
134 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100135 if (file_header->tar__gname) {
136 struct group *grp = getgrnam(file_header->tar__gname);
Denis Vlasenko8b814b42009-04-21 00:52:21 +0000137 if (grp) gid = grp->gr_gid;
138 }
Denys Vlasenkof94c9bf2009-11-29 07:45:33 +0100139 /* GNU tar 1.15.1 uses chown, not lchown */
Denys Vlasenko885583e2009-09-17 03:02:57 +0200140 chown(file_header->name, uid, gid);
Mike Frysinger4f239b12009-05-05 07:00:27 -0400141 } else
142#endif
Denys Vlasenko885583e2009-09-17 03:02:57 +0200143 chown(file_header->name, file_header->uid, file_header->gid);
Rob Landleyf3d6c942005-10-27 22:49:08 +0000144 }
Denys Vlasenkob9762922009-08-30 01:10:17 +0200145 if (!S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000146 /* uclibc has no lchmod, glibc is even stranger -
147 * it has lchmod which seems to do nothing!
148 * so we use chmod... */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200149 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000150 chmod(file_header->name, file_header->mode);
151 }
152 /* same for utime */
Denys Vlasenkod57d6262009-09-17 02:43:14 +0200153 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100154 struct timeval t[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100155
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100156 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
157 t[1].tv_usec = t[0].tv_usec = 0;
158 utimes(file_header->name, t);
Denis Vlasenko75103842007-06-20 14:49:47 +0000159 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000160 }
161}