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)
12 file_header_t *file_header = archive_handle->file_header;
16 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
19 # define dst_name (file_header->name)
22 #if ENABLE_FEATURE_TAR_SELINUX
23 char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
25 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
26 if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
28 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
29 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
33 /* Hard links are encoded as regular files of size 0
34 * with a nonempty link field */
36 if (S_ISREG(file_header->mode) && file_header->size == 0)
37 hard_link = file_header->link_target;
39 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
40 dst_name = file_header->name;
41 if (archive_handle->tar__strip_components) {
42 unsigned n = archive_handle->tar__strip_components;
44 dst_name = strchr(dst_name, '/');
45 if (!dst_name || dst_name[1] == '\0') {
46 data_skip(archive_handle);
51 * Link target is shortened only for hardlinks:
52 * softlinks restored unchanged.
55 // GNU tar 1.26 does not check that we reached end of link name:
56 // if "dir/hardlink" is hardlinked to "file",
57 // tar xvf a.tar --strip-components=1 says:
58 // tar: hardlink: Cannot hard link to '': No such file or directory
59 // and continues processing. We silently skip such entries.
60 hard_link = strchr(hard_link, '/');
61 if (!hard_link || hard_link[1] == '\0') {
62 data_skip(archive_handle);
71 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
72 char *slash = strrchr(dst_name, '/');
75 bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
80 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
81 /* Remove the entry if it exists */
82 if (!S_ISDIR(file_header->mode)) {
85 * tar cf t.tar hardlink1 hardlink2 hardlink1
86 * results in this tarball structure:
88 * hardlink2 -> hardlink1
89 * hardlink1 -> hardlink1 <== !!!
91 if (strcmp(hard_link, dst_name) == 0)
94 /* Proceed with deleting */
95 if (unlink(dst_name) == -1
98 bb_perror_msg_and_die("can't remove old file %s",
103 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
104 /* Remove the existing entry if its older than the extracted entry */
105 struct stat existing_sb;
106 if (lstat(dst_name, &existing_sb) == -1) {
107 if (errno != ENOENT) {
108 bb_perror_msg_and_die("can't stat old file");
111 else if (existing_sb.st_mtime >= file_header->mtime) {
112 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
113 && !S_ISDIR(file_header->mode)
115 bb_error_msg("%s not created: newer or "
116 "same age file exists", dst_name);
118 data_skip(archive_handle);
121 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
122 bb_perror_msg_and_die("can't remove old file %s",
127 /* Handle hard links separately */
129 res = link(hard_link, dst_name);
130 if (res != 0 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
131 bb_perror_msg("can't create %slink "
132 "from %s to %s", "hard",
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)
185 bb_perror_msg("can't create %slink "
186 "from %s to %s", "sym",
188 file_header->link_target);
195 res = mknod(dst_name, file_header->mode, file_header->device);
197 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
199 bb_perror_msg("can't create node %s", dst_name);
203 bb_error_msg_and_die("unrecognized file type");
206 if (!S_ISLNK(file_header->mode)) {
207 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
208 uid_t uid = file_header->uid;
209 gid_t gid = file_header->gid;
210 #if ENABLE_FEATURE_TAR_UNAME_GNAME
211 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
212 if (file_header->tar__uname) {
213 //TODO: cache last name/id pair?
214 struct passwd *pwd = getpwnam(file_header->tar__uname);
215 if (pwd) uid = pwd->pw_uid;
217 if (file_header->tar__gname) {
218 struct group *grp = getgrnam(file_header->tar__gname);
219 if (grp) gid = grp->gr_gid;
223 /* GNU tar 1.15.1 uses chown, not lchown */
224 chown(dst_name, uid, gid);
226 /* uclibc has no lchmod, glibc is even stranger -
227 * it has lchmod which seems to do nothing!
228 * so we use chmod... */
229 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
230 chmod(dst_name, file_header->mode);
232 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
235 t[1].tv_sec = t[0].tv_sec = file_header->mtime;
236 t[1].tv_usec = t[0].tv_usec = 0;
242 #if ENABLE_FEATURE_TAR_SELINUX
244 /* reset the context after creating an entry */
245 setfscreatecon(NULL);