1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 #include "bb_archive.h"
9 void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
11 file_header_t *file_header = archive_handle->file_header;
15 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
18 # define dst_name (file_header->name)
21 #if ENABLE_FEATURE_TAR_SELINUX
22 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
24 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
25 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
27 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
28 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
32 /* Hard links are encoded as regular files of size 0
33 * with a nonempty link field */
35 if (S_ISREG(file_header->mode) && file_header->size == 0)
36 hard_link = file_header->link_target;
38 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
39 dst_name = file_header->name;
40 if (archive_handle->tar__strip_components) {
41 unsigned n = archive_handle->tar__strip_components;
43 dst_name = strchr(dst_name, '/');
44 if (!dst_name || dst_name[1] == '\0') {
45 data_skip(archive_handle);
50 * Link target is shortened only for hardlinks:
51 * softlinks restored unchanged.
54 // GNU tar 1.26 does not check that we reached end of link name:
55 // if "dir/hardlink" is hardlinked to "file",
56 // tar xvf a.tar --strip-components=1 says:
57 // tar: hardlink: Cannot hard link to '': No such file or directory
58 // and continues processing. We silently skip such entries.
59 hard_link = strchr(hard_link, '/');
60 if (!hard_link || hard_link[1] == '\0') {
61 data_skip(archive_handle);
70 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
71 char *slash = strrchr(dst_name, '/');
74 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
79 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
80 /* Remove the entry if it exists */
81 if (!S_ISDIR(file_header->mode)) {
84 * tar cf t.tar hardlink1 hardlink2 hardlink1
85 * results in this tarball structure:
87 * hardlink2 -> hardlink1
88 * hardlink1 -> hardlink1 <== !!!
90 if (strcmp(hard_link, dst_name) == 0)
93 /* Proceed with deleting */
94 if (unlink(dst_name) == -1
97 bb_perror_msg_and_die("can't remove old file %s",
102 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
103 /* Remove the existing entry if its older than the extracted entry */
104 struct stat existing_sb;
105 if (lstat(dst_name, &existing_sb) == -1) {
106 if (errno != ENOENT) {
107 bb_perror_msg_and_die("can't stat old file");
110 else if (existing_sb.st_mtime >= file_header->mtime) {
111 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
112 && !S_ISDIR(file_header->mode)
114 bb_error_msg("%s not created: newer or "
115 "same age file exists", dst_name);
117 data_skip(archive_handle);
120 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
121 bb_perror_msg_and_die("can't remove old file %s",
126 /* Handle hard links separately */
128 res = link(hard_link, dst_name);
129 if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
131 bb_perror_msg("can't create %slink "
136 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
140 /* Create the filesystem entry */
141 switch (file_header->mode & S_IFMT) {
145 int flags = O_WRONLY | O_CREAT | O_EXCL;
146 if (archive_handle->ah_flags & ARCHIVE_O_TRUNC)
147 flags = O_WRONLY | O_CREAT | O_TRUNC;
148 dst_nameN = dst_name;
149 #ifdef ARCHIVE_REPLACE_VIA_RENAME
150 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME)
151 /* rpm-style temp file name */
152 dst_nameN = xasprintf("%s;%x", dst_name, (int)getpid());
154 dst_fd = xopen3(dst_nameN,
158 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
160 #ifdef ARCHIVE_REPLACE_VIA_RENAME
161 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
162 xrename(dst_nameN, dst_name);
169 res = mkdir(dst_name, file_header->mode);
171 && (errno != EISDIR) /* btw, Linux doesn't return this */
173 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
175 bb_perror_msg("can't make dir %s", dst_name);
180 //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
181 res = symlink(file_header->link_target, dst_name);
183 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
186 bb_perror_msg("can't create %slink "
189 file_header->link_target);
196 res = mknod(dst_name, file_header->mode, file_header->device);
198 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
200 bb_perror_msg("can't create node %s", dst_name);
204 bb_error_msg_and_die("unrecognized file type");
207 if (!S_ISLNK(file_header->mode)) {
208 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
209 uid_t uid = file_header->uid;
210 gid_t gid = file_header->gid;
211 #if ENABLE_FEATURE_TAR_UNAME_GNAME
212 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
213 if (file_header->tar__uname) {
214 //TODO: cache last name/id pair?
215 struct passwd *pwd = getpwnam(file_header->tar__uname);
216 if (pwd) uid = pwd->pw_uid;
218 if (file_header->tar__gname) {
219 struct group *grp = getgrnam(file_header->tar__gname);
220 if (grp) gid = grp->gr_gid;
224 /* GNU tar 1.15.1 uses chown, not lchown */
225 chown(dst_name, uid, gid);
227 /* uclibc has no lchmod, glibc is even stranger -
228 * it has lchmod which seems to do nothing!
229 * so we use chmod... */
230 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
231 chmod(dst_name, file_header->mode);
233 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
236 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
237 t[1].tv_usec = t[0].tv_usec = 0;
243 #if ENABLE_FEATURE_TAR_SELINUX
245 /* reset the context after creating an entry */
246 setfscreatecon(NULL);