tar: shrink hardlink name handling code
[oweals/busybox.git] / archival / libarchive / data_extract_all.c
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
9 void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
10 {
11
12         file_header_t *file_header = archive_handle->file_header;
13         int dst_fd;
14         int res;
15         char *hard_link;
16 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
17         char *dst_name;
18 #else
19 # define dst_name (file_header->name)
20 #endif
21
22 #if ENABLE_FEATURE_TAR_SELINUX
23         char *sctx = archive_handle->tar__sctx[PAX_NEXT_FILE];
24         if (!sctx)
25                 sctx = archive_handle->tar__sctx[PAX_GLOBAL];
26         if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
27                 setfscreatecon(sctx);
28                 free(archive_handle->tar__sctx[PAX_NEXT_FILE]);
29                 archive_handle->tar__sctx[PAX_NEXT_FILE] = NULL;
30         }
31 #endif
32
33         /* Hard links are encoded as regular files of size 0
34          * with a nonempty link field */
35         hard_link = NULL;
36         if (S_ISREG(file_header->mode) && file_header->size == 0)
37                 hard_link = file_header->link_target;
38
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;
43                 do {
44                         dst_name = strchr(dst_name, '/');
45                         if (!dst_name || dst_name[1] == '\0') {
46                                 data_skip(archive_handle);
47                                 return;
48                         }
49                         dst_name++;
50                         /*
51                          * Link target is shortened only for hardlinks:
52                          * softlinks restored unchanged.
53                          */
54                         if (hard_link) {
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);
63                                         return;
64                                 }
65                                 hard_link++;
66                         }
67                 } while (--n != 0);
68         }
69 #endif
70
71         if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
72                 char *slash = strrchr(dst_name, '/');
73                 if (slash) {
74                         *slash = '\0';
75                         bb_make_directory(dst_name, -1, FILEUTILS_RECUR);
76                         *slash = '/';
77                 }
78         }
79
80         if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
81                 /* Remove the entry if it exists */
82                 if (!S_ISDIR(file_header->mode)) {
83                         if (hard_link) {
84                                 /* Ugly special case:
85                                  * tar cf t.tar hardlink1 hardlink2 hardlink1
86                                  * results in this tarball structure:
87                                  * hardlink1
88                                  * hardlink2 -> hardlink1
89                                  * hardlink1 -> hardlink1 <== !!!
90                                  */
91                                 if (strcmp(hard_link, dst_name) == 0)
92                                         goto ret;
93                         }
94                         /* Proceed with deleting */
95                         if (unlink(dst_name) == -1
96                          && errno != ENOENT
97                         ) {
98                                 bb_perror_msg_and_die("can't remove old file %s",
99                                                 dst_name);
100                         }
101                 }
102         }
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");
109                         }
110                 }
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)
114                         ) {
115                                 bb_error_msg("%s not created: newer or "
116                                         "same age file exists", dst_name);
117                         }
118                         data_skip(archive_handle);
119                         goto ret;
120                 }
121                 else if ((unlink(dst_name) == -1) && (errno != EISDIR)) {
122                         bb_perror_msg_and_die("can't remove old file %s",
123                                         dst_name);
124                 }
125         }
126
127         /* Handle hard links separately */
128         if (hard_link) {
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",
133                                         dst_name,
134                                         hard_link);
135                 }
136                 /* Hardlinks have no separate mode/ownership, skip chown/chmod */
137                 goto ret;
138         }
139
140         /* Create the filesystem entry */
141         switch (file_header->mode & S_IFMT) {
142         case S_IFREG: {
143                 /* Regular file */
144                 char *dst_nameN;
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());
153 #endif
154                 dst_fd = xopen3(dst_nameN,
155                         flags,
156                         file_header->mode
157                         );
158                 bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size);
159                 close(dst_fd);
160 #ifdef ARCHIVE_REPLACE_VIA_RENAME
161                 if (archive_handle->ah_flags & ARCHIVE_REPLACE_VIA_RENAME) {
162                         xrename(dst_nameN, dst_name);
163                         free(dst_nameN);
164                 }
165 #endif
166                 break;
167         }
168         case S_IFDIR:
169                 res = mkdir(dst_name, file_header->mode);
170                 if ((res == -1)
171                  && (errno != EISDIR) /* btw, Linux doesn't return this */
172                  && (errno != EEXIST)
173                  && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
174                 ) {
175                         bb_perror_msg("can't make dir %s", dst_name);
176                 }
177                 break;
178         case S_IFLNK:
179                 /* Symlink */
180 //TODO: what if file_header->link_target == NULL (say, corrupted tarball?)
181                 res = symlink(file_header->link_target, dst_name);
182                 if (res != 0
183                  && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
184                 ) {
185                         bb_perror_msg("can't create %slink "
186                                 "from %s to %s", "sym",
187                                 dst_name,
188                                 file_header->link_target);
189                 }
190                 break;
191         case S_IFSOCK:
192         case S_IFBLK:
193         case S_IFCHR:
194         case S_IFIFO:
195                 res = mknod(dst_name, file_header->mode, file_header->device);
196                 if ((res == -1)
197                  && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
198                 ) {
199                         bb_perror_msg("can't create node %s", dst_name);
200                 }
201                 break;
202         default:
203                 bb_error_msg_and_die("unrecognized file type");
204         }
205
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;
216                                 }
217                                 if (file_header->tar__gname) {
218                                         struct group *grp = getgrnam(file_header->tar__gname);
219                                         if (grp) gid = grp->gr_gid;
220                                 }
221                         }
222 #endif
223                         /* GNU tar 1.15.1 uses chown, not lchown */
224                         chown(dst_name, uid, gid);
225                 }
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);
231                 }
232                 if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) {
233                         struct timeval t[2];
234
235                         t[1].tv_sec = t[0].tv_sec = file_header->mtime;
236                         t[1].tv_usec = t[0].tv_usec = 0;
237                         utimes(dst_name, t);
238                 }
239         }
240
241  ret: ;
242 #if ENABLE_FEATURE_TAR_SELINUX
243         if (sctx) {
244                 /* reset the context after creating an entry */
245                 setfscreatecon(NULL);
246         }
247 #endif
248 }