summaryrefslogtreecommitdiff
path: root/archival/libarchive/data_extract_all.c (plain)
blob: 2e18ffb5749a2a40b2c9f009c66892d91831dbfe
1/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4 */
5
6#include "libbb.h"
7#include "bb_archive.h"
8
9void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
10{
11 file_header_t *file_header = archive_handle->file_header;
12 int dst_fd;
13 int res;
14
15#if ENABLE_FEATURE_TAR_SELINUX
16 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
17#ifdef __BIONIC__
18 matchpathcon_init(NULL);
19#endif
20 if (!sctx)
21 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
22 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
23 setfscreatecon(sctx);
24 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
25 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
26 }
27#endif
28
29 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
30 char *slash = strrchr(file_header->name, '/');
31 if (slash) {
32 *slash = '\0';
33 bb_make_directory(file_header->name, -1, FILEUTILS_RECUR);
34 *slash = '/';
35 }
36 }
37
38 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
39 /* Remove the entry if it exists */
40 if (!S_ISDIR(file_header->mode)) {
41 /* Is it hardlink?
42 * We encode hard links as regular files of size 0 with a symlink */
43 if (S_ISREG(file_header->mode)
44 && file_header->link_target
45 && file_header->size == 0
46 ) {
47 /* Ugly special case:
48 * tar cf t.tar hardlink1 hardlink2 hardlink1
49 * results in this tarball structure:
50 * hardlink1
51 * hardlink2 -> hardlink1
52 * hardlink1 -> hardlink1 <== !!!
53 */
54 if (strcmp(file_header->link_target, file_header->name) == 0)
55 goto ret;
56 }
57 /* Proceed with deleting */
58 if (unlink(file_header->name) == -1
59 && errno != ENOENT
60 ) {
61 bb_perror_msg_and_die("can't remove old file %s",
62 file_header->name);
63 }
64 }
65 }
66 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
67 /* Remove the existing entry if its older than the extracted entry */
68 struct stat existing_sb;
69 if (lstat(file_header->name, &existing_sb) == -1) {
70 if (errno != ENOENT) {
71 bb_perror_msg_and_die("can't stat old file");
72 }
73 }
74 else if ((time_t) existing_sb.st_mtime >= (time_t) file_header->mtime) {
75 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
76 && !S_ISDIR(file_header->mode)
77 ) {
78 bb_error_msg("%s not created: newer or "
79 "same age file exists", file_header->name);
80 }
81 data_skip(archive_handle);
82 goto ret;
83 }
84 else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) {
85 bb_perror_msg_and_die("can't remove old file %s",
86 file_header->name);
87 }
88 }
89
90 /* Handle hard links separately
91 * We encode hard links as regular files of size 0 with a symlink */
92 if (S_ISREG(file_header->mode)
93 && file_header->link_target
94 && file_header->size == 0
95 ) {
96 /* hard link */
97 res = link(file_header->link_target, file_header->name);
98 if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
99 bb_perror_msg("can't create %slink "
100 "from %s to %s", "hard",
101 file_header->name,
102 file_header->link_target);
103 }
104 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
105 goto ret;
106 }
107
108 /* Create the filesystem entry */
109 switch (file_header->mode & S_IFMT) {
110 case S_IFREG: {
111 /* Regular file */
112 char *dst_name;
113 int flags = O_WRONLY | O_CREAT | O_EXCL;
114 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
115 flags = O_WRONLY | O_CREAT | O_TRUNC;
116 dst_name = file_header->name;
117#ifdef ARCHIVE_REPLACE_VIA_RENAME
118 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
119 /* rpm-style temp file name */
120 dst_name = xasprintf("%s;%x", dst_name, (int)getpid());
121#endif
122 dst_fd = xopen3(dst_name,
123 flags,
124 file_header->mode
125 );
126 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
127 close(dst_fd);
128#ifdef ARCHIVE_REPLACE_VIA_RENAME
129 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
130 xrename(dst_name, file_header->name);
131 free(dst_name);
132 }
133#endif
134 break;
135 }
136 case S_IFDIR:
137 res = mkdir(file_header->name, file_header->mode);
138 if ((res == -1)
139 && (errno != EISDIR) /* btw, Linux doesn't return this */
140 && (errno != EEXIST)
141 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
142 ) {
143 bb_perror_msg("can't make dir %s", file_header->name);
144 }
145 break;
146 case S_IFLNK:
147 /* Symlink */
148//TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
149 res = symlink(file_header->link_target, file_header->name);
150 if ((res == -1)
151 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
152 ) {
153 bb_perror_msg("can't create %slink "
154 "from %s to %s", "sym",
155 file_header->name,
156 file_header->link_target);
157 }
158 break;
159 case S_IFSOCK:
160 case S_IFBLK:
161 case S_IFCHR:
162 case S_IFIFO:
163 res = mknod(file_header->name, file_header->mode, file_header->device);
164 if ((res == -1)
165 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
166 ) {
167 bb_perror_msg("can't create node %s", file_header->name);
168 }
169 break;
170 default:
171 bb_error_msg_and_die("unrecognized file type");
172 }
173
174 if (!S_ISLNK(file_header->mode)) {
175 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
176 uid_t uid = file_header->uid;
177 gid_t gid = file_header->gid;
178#if ENABLE_FEATURE_TAR_UNAME_GNAME
179 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
180 if (file_header->tar__uname) {
181//TODO: cache last name/id pair?
182 struct passwd *pwd = getpwnam(file_header->tar__uname);
183 if (pwd) uid = pwd->pw_uid;
184 }
185 if (file_header->tar__gname) {
186 struct group *grp = getgrnam(file_header->tar__gname);
187 if (grp) gid = grp->gr_gid;
188 }
189 }
190#endif
191 /* GNU tar 1.15.1 uses chown, not lchown */
192 chown(file_header->name, uid, gid);
193 }
194 /* uclibc has no lchmod, glibc is even stranger -
195 * it has lchmod which seems to do nothing!
196 * so we use chmod... */
197 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
198 chmod(file_header->name, file_header->mode);
199 }
200 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
201 struct timeval t[2];
202
203 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
204 t[1].tv_usec = t[0].tv_usec = 0;
205 utimes(file_header->name, t);
206 }
207 }
208
209 ret: ;
210#if ENABLE_FEATURE_TAR_SELINUX
211 if (sctx) {
212 /* reset the context after creating an entry */
213 setfscreatecon(NULL);
214 }
215#endif
216}
217