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
)) {
130 bb_perror_msg("can't create %slink "
131 "from %s to %s", "hard",
135 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
139 /* Create the filesystem entry */
140 switch (file_header
->mode
& S_IFMT
) {
144 int flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
145 if (archive_handle
->ah_flags
& ARCHIVE_O_TRUNC
)
146 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
147 dst_nameN
= dst_name
;
148 #ifdef ARCHIVE_REPLACE_VIA_RENAME
149 if (archive_handle
->ah_flags
& ARCHIVE_REPLACE_VIA_RENAME
)
150 /* rpm-style temp file name */
151 dst_nameN
= xasprintf("%s;%x", dst_name
, (int)getpid());
153 dst_fd
= xopen3(dst_nameN
,
157 bb_copyfd_exact_size(archive_handle
->src_fd
, dst_fd
, file_header
->size
);
159 #ifdef ARCHIVE_REPLACE_VIA_RENAME
160 if (archive_handle
->ah_flags
& ARCHIVE_REPLACE_VIA_RENAME
) {
161 xrename(dst_nameN
, dst_name
);
168 res
= mkdir(dst_name
, file_header
->mode
);
170 && (errno
!= EISDIR
) /* btw, Linux doesn't return this */
172 && !(archive_handle
->ah_flags
& ARCHIVE_EXTRACT_QUIET
)
174 bb_perror_msg("can't make dir %s", dst_name
);
179 //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
180 res
= symlink(file_header
->link_target
, dst_name
);
182 && !(archive_handle
->ah_flags
& ARCHIVE_EXTRACT_QUIET
)
184 bb_perror_msg("can't create %slink "
185 "from %s to %s", "sym",
187 file_header
->link_target
);
194 res
= mknod(dst_name
, file_header
->mode
, file_header
->device
);
196 && !(archive_handle
->ah_flags
& ARCHIVE_EXTRACT_QUIET
)
198 bb_perror_msg("can't create node %s", dst_name
);
202 bb_error_msg_and_die("unrecognized file type");
205 if (!S_ISLNK(file_header
->mode
)) {
206 if (!(archive_handle
->ah_flags
& ARCHIVE_DONT_RESTORE_OWNER
)) {
207 uid_t uid
= file_header
->uid
;
208 gid_t gid
= file_header
->gid
;
209 #if ENABLE_FEATURE_TAR_UNAME_GNAME
210 if (!(archive_handle
->ah_flags
& ARCHIVE_NUMERIC_OWNER
)) {
211 if (file_header
->tar__uname
) {
212 //TODO: cache last name/id pair?
213 struct passwd
*pwd
= getpwnam(file_header
->tar__uname
);
214 if (pwd
) uid
= pwd
->pw_uid
;
216 if (file_header
->tar__gname
) {
217 struct group
*grp
= getgrnam(file_header
->tar__gname
);
218 if (grp
) gid
= grp
->gr_gid
;
222 /* GNU tar 1.15.1 uses chown, not lchown */
223 chown(dst_name
, uid
, gid
);
225 /* uclibc has no lchmod, glibc is even stranger -
226 * it has lchmod which seems to do nothing!
227 * so we use chmod... */
228 if (!(archive_handle
->ah_flags
& ARCHIVE_DONT_RESTORE_PERM
)) {
229 chmod(dst_name
, file_header
->mode
);
231 if (archive_handle
->ah_flags
& ARCHIVE_RESTORE_DATE
) {
234 t
[1].tv_sec
= t
[0].tv_sec
= file_header
->mtime
;
235 t
[1].tv_usec
= t
[0].tv_usec
= 0;
241 #if ENABLE_FEATURE_TAR_SELINUX
243 /* reset the context after creating an entry */
244 setfscreatecon(NULL
);