]> Dogcows Code - chaz/tar/blob - src/extract.c
Detect tarbombs while extracting
[chaz/tar] / src / extract.c
1 /* Extract files from a tar archive.
2
3 Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2013
4 Free Software Foundation, Inc.
5
6 This file is part of GNU tar.
7
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 Written by John Gilmore, on 1985-11-19. */
22
23 #include <system.h>
24 #include <quotearg.h>
25 #include <errno.h>
26 #include <priv-set.h>
27 #include <root-uid.h>
28 #include <utimens.h>
29
30 #include "common.h"
31
32 static bool we_are_root; /* true if our effective uid == 0 */
33 static mode_t newdir_umask; /* umask when creating new directories */
34 static mode_t current_umask; /* current umask (which is set to 0 if -p) */
35
36 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
37
38 #if ! HAVE_FCHMOD && ! defined fchmod
39 # define fchmod(fd, mode) (errno = ENOSYS, -1)
40 #endif
41 #if ! HAVE_FCHOWN && ! defined fchown
42 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
43 #endif
44
45 /* Return true if an error number ERR means the system call is
46 supported in this case. */
47 static bool
48 implemented (int err)
49 {
50 return ! (err == ENOSYS
51 || err == ENOTSUP
52 || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
53 }
54
55 /* List of directories whose statuses we need to extract after we've
56 finished extracting their subsidiary files. If you consider each
57 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
58 represents an element where AFTER_LINKS is nonzero and [^D]
59 represents an element where AFTER_LINKS is zero, then the head
60 of the subsequence has the longest name, and each non-head element
61 in the prefix is an ancestor (in the directory hierarchy) of the
62 preceding element. */
63
64 struct delayed_set_stat
65 {
66 /* Next directory in list. */
67 struct delayed_set_stat *next;
68
69 /* Metadata for this directory. */
70 dev_t dev;
71 ino_t ino;
72 mode_t mode; /* The desired mode is MODE & ~ current_umask. */
73 uid_t uid;
74 gid_t gid;
75 struct timespec atime;
76 struct timespec mtime;
77
78 /* An estimate of the directory's current mode, along with a mask
79 specifying which bits of this estimate are known to be correct.
80 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
81 matter. */
82 mode_t current_mode;
83 mode_t current_mode_mask;
84
85 /* This directory is an intermediate directory that was created
86 as an ancestor of some other directory; it was not mentioned
87 in the archive, so do not set its uid, gid, atime, or mtime,
88 and don't alter its mode outside of MODE_RWX. */
89 bool interdir;
90
91 /* Whether symbolic links should be followed when accessing the
92 directory. */
93 int atflag;
94
95 /* Do not set the status of this directory until after delayed
96 links are created. */
97 bool after_links;
98
99 /* Directory that the name is relative to. */
100 int change_dir;
101
102 /* extended attributes*/
103 char *cntx_name;
104 char *acls_a_ptr;
105 size_t acls_a_len;
106 char *acls_d_ptr;
107 size_t acls_d_len;
108 size_t xattr_map_size;
109 struct xattr_array *xattr_map;
110 /* Length and contents of name. */
111 size_t file_name_len;
112 char file_name[1];
113 };
114
115 static struct delayed_set_stat *delayed_set_stat_head;
116
117 /* List of links whose creation we have delayed. */
118 struct delayed_link
119 {
120 /* The next delayed link in the list. */
121 struct delayed_link *next;
122
123 /* The device, inode number and birthtime of the placeholder.
124 birthtime.tv_nsec is negative if the birthtime is not available.
125 Don't use mtime as this would allow for false matches if some
126 other process removes the placeholder. Don't use ctime as
127 this would cause race conditions and other screwups, e.g.,
128 when restoring hard-linked symlinks. */
129 dev_t dev;
130 ino_t ino;
131 struct timespec birthtime;
132
133 /* True if the link is symbolic. */
134 bool is_symlink;
135
136 /* The desired metadata, valid only the link is symbolic. */
137 mode_t mode;
138 uid_t uid;
139 gid_t gid;
140 struct timespec atime;
141 struct timespec mtime;
142
143 /* The directory that the sources and target are relative to. */
144 int change_dir;
145
146 /* A list of sources for this link. The sources are all to be
147 hard-linked together. */
148 struct string_list *sources;
149
150 /* SELinux context */
151 char *cntx_name;
152
153 /* ACLs */
154 char *acls_a_ptr;
155 size_t acls_a_len;
156 char *acls_d_ptr;
157 size_t acls_d_len;
158
159 size_t xattr_map_size;
160 struct xattr_array *xattr_map;
161
162 /* The desired target of the desired link. */
163 char target[1];
164 };
165
166 static struct delayed_link *delayed_link_head;
167
168 struct string_list
169 {
170 struct string_list *next;
171 char string[1];
172 };
173
174 /* Set up to extract files. */
175 void
176 extr_init (void)
177 {
178 we_are_root = geteuid () == ROOT_UID;
179 same_permissions_option += we_are_root;
180 same_owner_option += we_are_root;
181
182 /* Option -p clears the kernel umask, so it does not affect proper
183 restoration of file permissions. New intermediate directories will
184 comply with umask at start of program. */
185
186 newdir_umask = umask (0);
187 if (0 < same_permissions_option)
188 current_umask = 0;
189 else
190 {
191 umask (newdir_umask); /* restore the kernel umask */
192 current_umask = newdir_umask;
193 }
194
195 /* If the user wants to guarantee that everything is under one directory,
196 determine its name now and let it be created later. */
197 if (one_top_level_option)
198 {
199 int i;
200 char *base = base_name (archive_name_array[0]);
201
202 for (i = strlen (base) - 1; i > 2; i--)
203 if (!strncmp (base + i - 3, ".tar", 4) ||
204 !strncmp (base + i - 3, ".taz", 4) ||
205 !strncmp (base + i - 3, ".tbz", 4) ||
206 !strncmp (base + i - 3, ".tb2", 4) ||
207 !strncmp (base + i - 3, ".tgz", 4) ||
208 !strncmp (base + i - 3, ".tlz", 4) ||
209 !strncmp (base + i - 3, ".txz", 4)) break;
210
211 if (i <= 3)
212 {
213 one_top_level_option = false;
214 free (base);
215 return;
216 }
217
218 one_top_level = xmalloc (i - 2);
219 strncpy (one_top_level, base, i - 3);
220 one_top_level[i - 3] = '\0';
221 free (base);
222 }
223 }
224
225 /* Use fchmod if possible, fchmodat otherwise. */
226 static int
227 fd_chmod (int fd, char const *file, mode_t mode, int atflag)
228 {
229 if (0 <= fd)
230 {
231 int result = fchmod (fd, mode);
232 if (result == 0 || implemented (errno))
233 return result;
234 }
235 return fchmodat (chdir_fd, file, mode, atflag);
236 }
237
238 /* Use fchown if possible, fchownat otherwise. */
239 static int
240 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
241 {
242 if (0 <= fd)
243 {
244 int result = fchown (fd, uid, gid);
245 if (result == 0 || implemented (errno))
246 return result;
247 }
248 return fchownat (chdir_fd, file, uid, gid, atflag);
249 }
250
251 /* Use fstat if possible, fstatat otherwise. */
252 static int
253 fd_stat (int fd, char const *file, struct stat *st, int atflag)
254 {
255 return (0 <= fd
256 ? fstat (fd, st)
257 : fstatat (chdir_fd, file, st, atflag));
258 }
259
260 /* Set the mode for FILE_NAME to MODE.
261 MODE_MASK specifies the bits of MODE that we care about;
262 thus if MODE_MASK is zero, do nothing.
263 If FD is nonnegative, it is a file descriptor for the file.
264 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
265 the file's current mode, using the style of struct delayed_set_stat.
266 TYPEFLAG specifies the type of the file.
267 ATFLAG specifies the flag to use when statting the file. */
268 static void
269 set_mode (char const *file_name,
270 mode_t mode, mode_t mode_mask, int fd,
271 mode_t current_mode, mode_t current_mode_mask,
272 char typeflag, int atflag)
273 {
274 if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
275 {
276 if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
277 {
278 struct stat st;
279 if (fd_stat (fd, file_name, &st, atflag) != 0)
280 {
281 stat_error (file_name);
282 return;
283 }
284 current_mode = st.st_mode;
285 }
286
287 current_mode &= MODE_ALL;
288 mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
289
290 if (current_mode != mode)
291 {
292 int chmod_errno =
293 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
294
295 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
296 setuid-root files would otherwise be a backdoor. See
297 http://opensolaris.org/jive/thread.jspa?threadID=95826
298 (2009-09-03). */
299 if (chmod_errno == EPERM && (mode & S_ISUID)
300 && priv_set_restore_linkdir () == 0)
301 {
302 chmod_errno =
303 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
304 priv_set_remove_linkdir ();
305 }
306
307 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
308 returns ENOTSUP even when operating on non-symlinks, try
309 again with the flag disabled if it does not appear to be
310 supported and if the file is not a symlink. This
311 introduces a race, alas. */
312 if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
313 chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
314
315 if (chmod_errno
316 && (typeflag != SYMTYPE || implemented (chmod_errno)))
317 {
318 errno = chmod_errno;
319 chmod_error_details (file_name, mode);
320 }
321 }
322 }
323 }
324
325 /* Check time after successfully setting FILE_NAME's time stamp to T. */
326 static void
327 check_time (char const *file_name, struct timespec t)
328 {
329 if (t.tv_sec < 0)
330 WARNOPT (WARN_TIMESTAMP,
331 (0, 0, _("%s: implausibly old time stamp %s"),
332 file_name, tartime (t, true)));
333 else if (timespec_cmp (volume_start_time, t) < 0)
334 {
335 struct timespec now;
336 gettime (&now);
337 if (timespec_cmp (now, t) < 0)
338 {
339 char buf[TIMESPEC_STRSIZE_BOUND];
340 struct timespec diff;
341 diff.tv_sec = t.tv_sec - now.tv_sec;
342 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
343 if (diff.tv_nsec < 0)
344 {
345 diff.tv_nsec += BILLION;
346 diff.tv_sec--;
347 }
348 WARNOPT (WARN_TIMESTAMP,
349 (0, 0, _("%s: time stamp %s is %s s in the future"),
350 file_name, tartime (t, true), code_timespec (diff, buf)));
351 }
352 }
353 }
354
355 /* Restore stat attributes (owner, group, mode and times) for
356 FILE_NAME, using information given in *ST.
357 If FD is nonnegative, it is a file descriptor for the file.
358 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
359 the file's current mode, using the style of struct delayed_set_stat.
360 TYPEFLAG specifies the type of the file.
361 If INTERDIR, this is an intermediate directory.
362 ATFLAG specifies the flag to use when statting the file. */
363
364 static void
365 set_stat (char const *file_name,
366 struct tar_stat_info const *st,
367 int fd, mode_t current_mode, mode_t current_mode_mask,
368 char typeflag, bool interdir, int atflag)
369 {
370 /* Do the utime before the chmod because some versions of utime are
371 broken and trash the modes of the file. */
372
373 if (! touch_option && ! interdir)
374 {
375 struct timespec ts[2];
376 if (incremental_option)
377 ts[0] = st->atime;
378 else
379 ts[0].tv_nsec = UTIME_OMIT;
380 ts[1] = st->mtime;
381
382 if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
383 {
384 if (incremental_option)
385 check_time (file_name, ts[0]);
386 check_time (file_name, ts[1]);
387 }
388 else if (typeflag != SYMTYPE || implemented (errno))
389 utime_error (file_name);
390 }
391
392 if (0 < same_owner_option && ! interdir)
393 {
394 /* Some systems allow non-root users to give files away. Once this
395 done, it is not possible anymore to change file permissions.
396 However, setting file permissions now would be incorrect, since
397 they would apply to the wrong user, and there would be a race
398 condition. So, don't use systems that allow non-root users to
399 give files away. */
400 uid_t uid = st->stat.st_uid;
401 gid_t gid = st->stat.st_gid;
402
403 if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
404 {
405 /* Changing the owner can clear st_mode bits in some cases. */
406 if ((current_mode | ~ current_mode_mask) & S_IXUGO)
407 current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
408 }
409 else if (typeflag != SYMTYPE || implemented (errno))
410 chown_error_details (file_name, uid, gid);
411 }
412
413 set_mode (file_name,
414 st->stat.st_mode & ~ current_umask,
415 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
416 fd, current_mode, current_mode_mask, typeflag, atflag);
417
418 /* these three calls must be done *after* fd_chown() call because fd_chown
419 causes that linux capabilities becomes cleared. */
420 xattrs_xattrs_set (st, file_name, typeflag, 1);
421 xattrs_acls_set (st, file_name, typeflag);
422 xattrs_selinux_set (st, file_name, typeflag);
423 }
424
425 /* For each entry H in the leading prefix of entries in HEAD that do
426 not have after_links marked, mark H and fill in its dev and ino
427 members. Assume HEAD && ! HEAD->after_links. */
428 static void
429 mark_after_links (struct delayed_set_stat *head)
430 {
431 struct delayed_set_stat *h = head;
432
433 do
434 {
435 struct stat st;
436 h->after_links = 1;
437
438 if (deref_stat (h->file_name, &st) != 0)
439 stat_error (h->file_name);
440 else
441 {
442 h->dev = st.st_dev;
443 h->ino = st.st_ino;
444 }
445 }
446 while ((h = h->next) && ! h->after_links);
447 }
448
449 /* Remember to restore stat attributes (owner, group, mode and times)
450 for the directory FILE_NAME, using information given in *ST,
451 once we stop extracting files into that directory.
452
453 If ST is null, merely create a placeholder node for an intermediate
454 directory that was created by make_directories.
455
456 NOTICE: this works only if the archive has usual member order, i.e.
457 directory, then the files in that directory. Incremental archive have
458 somewhat reversed order: first go subdirectories, then all other
459 members. To help cope with this case the variable
460 delay_directory_restore_option is set by prepare_to_extract.
461
462 If an archive was explicitely created so that its member order is
463 reversed, some directory timestamps can be restored incorrectly,
464 e.g.:
465 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
466 */
467 static void
468 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
469 mode_t current_mode, mode_t current_mode_mask,
470 mode_t mode, int atflag)
471 {
472 size_t file_name_len = strlen (file_name);
473 struct delayed_set_stat *data =
474 xmalloc (offsetof (struct delayed_set_stat, file_name)
475 + file_name_len + 1);
476 data->next = delayed_set_stat_head;
477 data->mode = mode;
478 if (st)
479 {
480 data->dev = st->stat.st_dev;
481 data->ino = st->stat.st_ino;
482 data->uid = st->stat.st_uid;
483 data->gid = st->stat.st_gid;
484 data->atime = st->atime;
485 data->mtime = st->mtime;
486 }
487 data->file_name_len = file_name_len;
488 data->current_mode = current_mode;
489 data->current_mode_mask = current_mode_mask;
490 data->interdir = ! st;
491 data->atflag = atflag;
492 data->after_links = 0;
493 data->change_dir = chdir_current;
494 data->cntx_name = NULL;
495 if (st)
496 assign_string (&data->cntx_name, st->cntx_name);
497 if (st && st->acls_a_ptr)
498 {
499 data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
500 data->acls_a_len = st->acls_a_len;
501 }
502 else
503 {
504 data->acls_a_ptr = NULL;
505 data->acls_a_len = 0;
506 }
507 if (st && st->acls_d_ptr)
508 {
509 data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
510 data->acls_d_len = st->acls_d_len;
511 }
512 else
513 {
514 data->acls_d_ptr = NULL;
515 data->acls_d_len = 0;
516 }
517 if (st)
518 xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
519 else
520 {
521 data->xattr_map = NULL;
522 data->xattr_map_size = 0;
523 }
524 strcpy (data->file_name, file_name);
525 delayed_set_stat_head = data;
526 if (must_be_dot_or_slash (file_name))
527 mark_after_links (data);
528 }
529
530 /* Update the delayed_set_stat info for an intermediate directory
531 created within the file name of DIR. The intermediate directory turned
532 out to be the same as this directory, e.g. due to ".." or symbolic
533 links. *DIR_STAT_INFO is the status of the directory. */
534 static void
535 repair_delayed_set_stat (char const *dir,
536 struct stat const *dir_stat_info)
537 {
538 struct delayed_set_stat *data;
539 for (data = delayed_set_stat_head; data; data = data->next)
540 {
541 struct stat st;
542 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
543 {
544 stat_error (data->file_name);
545 return;
546 }
547
548 if (st.st_dev == dir_stat_info->st_dev
549 && st.st_ino == dir_stat_info->st_ino)
550 {
551 data->dev = current_stat_info.stat.st_dev;
552 data->ino = current_stat_info.stat.st_ino;
553 data->mode = current_stat_info.stat.st_mode;
554 data->uid = current_stat_info.stat.st_uid;
555 data->gid = current_stat_info.stat.st_gid;
556 data->atime = current_stat_info.atime;
557 data->mtime = current_stat_info.mtime;
558 data->current_mode = st.st_mode;
559 data->current_mode_mask = ALL_MODE_BITS;
560 data->interdir = false;
561 return;
562 }
563 }
564
565 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
566 quotearg_colon (dir)));
567 }
568
569 /* After a file/link/directory creation has failed, see if
570 it's because some required directory was not present, and if so,
571 create all required directories. Return zero if all the required
572 directories were created, nonzero (issuing a diagnostic) otherwise.
573 Set *INTERDIR_MADE if at least one directory was created. */
574 static int
575 make_directories (char *file_name, bool *interdir_made)
576 {
577 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
578 char *cursor; /* points into the file name */
579
580 for (cursor = cursor0; *cursor; cursor++)
581 {
582 mode_t mode;
583 mode_t desired_mode;
584 int status;
585
586 if (! ISSLASH (*cursor))
587 continue;
588
589 /* Avoid mkdir of empty string, if leading or double '/'. */
590
591 if (cursor == cursor0 || ISSLASH (cursor[-1]))
592 continue;
593
594 /* Avoid mkdir where last part of file name is "." or "..". */
595
596 if (cursor[-1] == '.'
597 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
598 || (cursor[-2] == '.'
599 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
600 continue;
601
602 *cursor = '\0'; /* truncate the name there */
603 desired_mode = MODE_RWX & ~ newdir_umask;
604 mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
605 status = mkdirat (chdir_fd, file_name, mode);
606
607 if (status == 0)
608 {
609 /* Create a struct delayed_set_stat even if
610 mode == desired_mode, because
611 repair_delayed_set_stat may need to update the struct. */
612 delay_set_stat (file_name,
613 0, mode & ~ current_umask, MODE_RWX,
614 desired_mode, AT_SYMLINK_NOFOLLOW);
615
616 print_for_mkdir (file_name, cursor - file_name, desired_mode);
617 *interdir_made = true;
618 }
619 else if (errno == EEXIST)
620 status = 0;
621 else
622 {
623 /* Check whether the desired file exists. Even when the
624 file exists, mkdir can fail with some errno value E other
625 than EEXIST, so long as E describes an error condition
626 that also applies. */
627 int e = errno;
628 struct stat st;
629 status = fstatat (chdir_fd, file_name, &st, 0);
630 if (status)
631 {
632 errno = e;
633 mkdir_error (file_name);
634 }
635 }
636
637 *cursor = '/';
638 if (status)
639 return status;
640 }
641
642 return 0;
643 }
644
645 /* Return true if FILE_NAME (with status *STP, if STP) is not a
646 directory, and has a time stamp newer than (or equal to) that of
647 TAR_STAT. */
648 static bool
649 file_newer_p (const char *file_name, struct stat const *stp,
650 struct tar_stat_info *tar_stat)
651 {
652 struct stat st;
653
654 if (!stp)
655 {
656 if (deref_stat (file_name, &st) != 0)
657 {
658 if (errno != ENOENT)
659 {
660 stat_warn (file_name);
661 /* Be safer: if the file exists, assume it is newer. */
662 return true;
663 }
664 return false;
665 }
666 stp = &st;
667 }
668
669 return (! S_ISDIR (stp->st_mode)
670 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
671 }
672
673 #define RECOVER_NO 0
674 #define RECOVER_OK 1
675 #define RECOVER_SKIP 2
676
677 /* Attempt repairing what went wrong with the extraction. Delete an
678 already existing file or create missing intermediate directories.
679 Return RECOVER_OK if we somewhat increased our chances at a successful
680 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
681 caller should skip extraction of that member. The value of errno is
682 properly restored on returning RECOVER_NO.
683
684 If REGULAR, the caller was trying to extract onto a regular file.
685
686 Set *INTERDIR_MADE if an intermediate directory is made as part of
687 the recovery process. */
688
689 static int
690 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
691 {
692 int e = errno;
693 struct stat st;
694 struct stat const *stp = 0;
695
696 if (*interdir_made)
697 return RECOVER_NO;
698
699 switch (e)
700 {
701 case ELOOP:
702
703 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
704 but some operating systems do not conform to the standard. */
705 #ifdef EFTYPE
706 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
707 case EFTYPE:
708 #endif
709 /* FreeBSD 8.1 uses errno == EMLINK. */
710 case EMLINK:
711 /* Tru64 5.1B uses errno == ENOTSUP. */
712 case ENOTSUP:
713
714 if (! regular
715 || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
716 break;
717 if (strchr (file_name, '/'))
718 {
719 if (deref_stat (file_name, &st) != 0)
720 break;
721 stp = &st;
722 }
723
724 /* The caller tried to open a symbolic link with O_NOFOLLOW.
725 Fall through, treating it as an already-existing file. */
726
727 case EEXIST:
728 /* Remove an old file, if the options allow this. */
729
730 switch (old_files_option)
731 {
732 case SKIP_OLD_FILES:
733 WARNOPT (WARN_EXISTING_FILE,
734 (0, 0, _("%s: skipping existing file"), file_name));
735 return RECOVER_SKIP;
736
737 case KEEP_OLD_FILES:
738 return RECOVER_NO;
739
740 case KEEP_NEWER_FILES:
741 if (file_newer_p (file_name, stp, &current_stat_info))
742 break;
743 /* FALL THROUGH */
744
745 case DEFAULT_OLD_FILES:
746 case NO_OVERWRITE_DIR_OLD_FILES:
747 case OVERWRITE_OLD_FILES:
748 if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
749 return RECOVER_OK;
750 break;
751
752 case UNLINK_FIRST_OLD_FILES:
753 break;
754 }
755
756 case ENOENT:
757 /* Attempt creating missing intermediate directories. */
758 if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
759 return RECOVER_OK;
760 break;
761
762 default:
763 /* Just say we can't do anything about it... */
764 break;
765 }
766
767 errno = e;
768 return RECOVER_NO;
769 }
770
771 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
772 given in *ST. Restore before extraction because they may affect file layout
773 (e.g. on Lustre distributed parallel filesystem - setting info about how many
774 servers is this file striped over, stripe size, mirror copies, etc.
775 in advance dramatically improves the following performance of reading and
776 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
777 bits from the file's current permissions. TYPEFLAG specifies the type of the
778 file. FILE_CREATED indicates set_xattr has created the file */
779 static int
780 set_xattr (char const *file_name, struct tar_stat_info const *st,
781 mode_t invert_permissions, char typeflag, int *file_created)
782 {
783 int status = 0;
784
785 #ifdef HAVE_XATTRS
786 bool interdir_made = false;
787
788 if ((xattrs_option > 0) && st->xattr_map_size)
789 {
790 mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
791
792 do
793 status = mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0);
794 while (status && maybe_recoverable ((char *)file_name, false,
795 &interdir_made));
796
797 xattrs_xattrs_set (st, file_name, typeflag, 0);
798 *file_created = 1;
799 }
800 #endif
801
802 return(status);
803 }
804
805 /* Fix the statuses of all directories whose statuses need fixing, and
806 which are not ancestors of FILE_NAME. If AFTER_LINKS is
807 nonzero, do this for all such directories; otherwise, stop at the
808 first directory that is marked to be fixed up only after delayed
809 links are applied. */
810 static void
811 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
812 {
813 size_t file_name_len = strlen (file_name);
814 bool check_for_renamed_directories = 0;
815
816 while (delayed_set_stat_head)
817 {
818 struct delayed_set_stat *data = delayed_set_stat_head;
819 bool skip_this_one = 0;
820 struct stat st;
821 mode_t current_mode = data->current_mode;
822 mode_t current_mode_mask = data->current_mode_mask;
823
824 check_for_renamed_directories |= data->after_links;
825
826 if (after_links < data->after_links
827 || (data->file_name_len < file_name_len
828 && file_name[data->file_name_len]
829 && (ISSLASH (file_name[data->file_name_len])
830 || ISSLASH (file_name[data->file_name_len - 1]))
831 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
832 break;
833
834 chdir_do (data->change_dir);
835
836 if (check_for_renamed_directories)
837 {
838 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
839 {
840 stat_error (data->file_name);
841 skip_this_one = 1;
842 }
843 else
844 {
845 current_mode = st.st_mode;
846 current_mode_mask = ALL_MODE_BITS;
847 if (! (st.st_dev == data->dev && st.st_ino == data->ino))
848 {
849 ERROR ((0, 0,
850 _("%s: Directory renamed before its status could be extracted"),
851 quotearg_colon (data->file_name)));
852 skip_this_one = 1;
853 }
854 }
855 }
856
857 if (! skip_this_one)
858 {
859 struct tar_stat_info sb;
860 sb.stat.st_mode = data->mode;
861 sb.stat.st_uid = data->uid;
862 sb.stat.st_gid = data->gid;
863 sb.atime = data->atime;
864 sb.mtime = data->mtime;
865 sb.cntx_name = data->cntx_name;
866 sb.acls_a_ptr = data->acls_a_ptr;
867 sb.acls_a_len = data->acls_a_len;
868 sb.acls_d_ptr = data->acls_d_ptr;
869 sb.acls_d_len = data->acls_d_len;
870 sb.xattr_map = data->xattr_map;
871 sb.xattr_map_size = data->xattr_map_size;
872 set_stat (data->file_name, &sb,
873 -1, current_mode, current_mode_mask,
874 DIRTYPE, data->interdir, data->atflag);
875 }
876
877 delayed_set_stat_head = data->next;
878 xheader_xattr_free (data->xattr_map, data->xattr_map_size);
879 free (data->cntx_name);
880 free (data->acls_a_ptr);
881 free (data->acls_d_ptr);
882 free (data);
883 }
884 }
885
886 \f
887 static bool
888 is_directory_link (const char *file_name)
889 {
890 struct stat st;
891 int e = errno;
892 int res;
893
894 res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
895 S_ISLNK (st.st_mode) &&
896 fstatat (chdir_fd, file_name, &st, 0) == 0 &&
897 S_ISDIR (st.st_mode));
898 errno = e;
899 return res;
900 }
901 \f
902 /* Extractor functions for various member types */
903
904 static int
905 extract_dir (char *file_name, int typeflag)
906 {
907 int status;
908 mode_t mode;
909 mode_t current_mode = 0;
910 mode_t current_mode_mask = 0;
911 int atflag = 0;
912 bool interdir_made = false;
913
914 /* Save 'root device' to avoid purging mount points. */
915 if (one_file_system_option && root_device == 0)
916 {
917 struct stat st;
918
919 if (fstatat (chdir_fd, ".", &st, 0) != 0)
920 stat_diag (".");
921 else
922 root_device = st.st_dev;
923 }
924
925 if (incremental_option)
926 /* Read the entry and delete files that aren't listed in the archive. */
927 purge_directory (file_name);
928 else if (typeflag == GNUTYPE_DUMPDIR)
929 skip_member ();
930
931 /* If ownership or permissions will be restored later, create the
932 directory with restrictive permissions at first, so that in the
933 meantime processes owned by other users do not inadvertently
934 create files under this directory that inherit the wrong owner,
935 group, or permissions from the directory. If not root, though,
936 make the directory writeable and searchable at first, so that
937 files can be created under it. */
938 mode = ((current_stat_info.stat.st_mode
939 & (0 < same_owner_option || 0 < same_permissions_option
940 ? S_IRWXU
941 : MODE_RWX))
942 | (we_are_root ? 0 : MODE_WXUSR));
943
944 for (;;)
945 {
946 status = mkdirat (chdir_fd, file_name, mode);
947 if (status == 0)
948 {
949 current_mode = mode & ~ current_umask;
950 current_mode_mask = MODE_RWX;
951 atflag = AT_SYMLINK_NOFOLLOW;
952 break;
953 }
954
955 if (errno == EEXIST
956 && (interdir_made
957 || keep_directory_symlink_option
958 || old_files_option == DEFAULT_OLD_FILES
959 || old_files_option == OVERWRITE_OLD_FILES))
960 {
961 struct stat st;
962
963 if (keep_directory_symlink_option && is_directory_link (file_name))
964 return 0;
965
966 if (deref_stat (file_name, &st) == 0)
967 {
968 current_mode = st.st_mode;
969 current_mode_mask = ALL_MODE_BITS;
970
971 if (S_ISDIR (current_mode))
972 {
973 if (interdir_made)
974 {
975 repair_delayed_set_stat (file_name, &st);
976 return 0;
977 }
978 break;
979 }
980 }
981 errno = EEXIST;
982 }
983
984 switch (maybe_recoverable (file_name, false, &interdir_made))
985 {
986 case RECOVER_OK:
987 continue;
988
989 case RECOVER_SKIP:
990 break;
991
992 case RECOVER_NO:
993 if (errno != EEXIST)
994 {
995 mkdir_error (file_name);
996 return 1;
997 }
998 break;
999 }
1000 break;
1001 }
1002
1003 if (status == 0
1004 || old_files_option == DEFAULT_OLD_FILES
1005 || old_files_option == OVERWRITE_OLD_FILES)
1006 delay_set_stat (file_name, &current_stat_info,
1007 current_mode, current_mode_mask,
1008 current_stat_info.stat.st_mode, atflag);
1009 return status;
1010 }
1011
1012
1013
1014 static int
1015 open_output_file (char const *file_name, int typeflag, mode_t mode,
1016 int file_created, mode_t *current_mode,
1017 mode_t *current_mode_mask)
1018 {
1019 int fd;
1020 bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
1021 int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
1022 | O_CREAT
1023 | (overwriting_old_files
1024 ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
1025 : O_EXCL));
1026
1027 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
1028 if (file_created)
1029 openflag = openflag & ~O_EXCL;
1030
1031 if (typeflag == CONTTYPE)
1032 {
1033 static int conttype_diagnosed;
1034
1035 if (!conttype_diagnosed)
1036 {
1037 conttype_diagnosed = 1;
1038 WARNOPT (WARN_CONTIGUOUS_CAST,
1039 (0, 0, _("Extracting contiguous files as regular files")));
1040 }
1041 }
1042
1043 /* If O_NOFOLLOW is needed but does not work, check for a symlink
1044 separately. There's a race condition, but that cannot be avoided
1045 on hosts lacking O_NOFOLLOW. */
1046 if (! HAVE_WORKING_O_NOFOLLOW
1047 && overwriting_old_files && ! dereference_option)
1048 {
1049 struct stat st;
1050 if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
1051 && S_ISLNK (st.st_mode))
1052 {
1053 errno = ELOOP;
1054 return -1;
1055 }
1056 }
1057
1058 fd = openat (chdir_fd, file_name, openflag, mode);
1059 if (0 <= fd)
1060 {
1061 if (overwriting_old_files)
1062 {
1063 struct stat st;
1064 if (fstat (fd, &st) != 0)
1065 {
1066 int e = errno;
1067 close (fd);
1068 errno = e;
1069 return -1;
1070 }
1071 if (! S_ISREG (st.st_mode))
1072 {
1073 close (fd);
1074 errno = EEXIST;
1075 return -1;
1076 }
1077 *current_mode = st.st_mode;
1078 *current_mode_mask = ALL_MODE_BITS;
1079 }
1080 else
1081 {
1082 *current_mode = mode & ~ current_umask;
1083 *current_mode_mask = MODE_RWX;
1084 }
1085 }
1086
1087 return fd;
1088 }
1089
1090 static int
1091 extract_file (char *file_name, int typeflag)
1092 {
1093 int fd;
1094 off_t size;
1095 union block *data_block;
1096 int status;
1097 size_t count;
1098 size_t written;
1099 bool interdir_made = false;
1100 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1101 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1102 mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
1103 : 0;
1104 mode_t current_mode = 0;
1105 mode_t current_mode_mask = 0;
1106
1107 if (to_stdout_option)
1108 fd = STDOUT_FILENO;
1109 else if (to_command_option)
1110 {
1111 fd = sys_exec_command (file_name, 'f', &current_stat_info);
1112 if (fd < 0)
1113 {
1114 skip_member ();
1115 return 0;
1116 }
1117 }
1118 else
1119 {
1120 int file_created = 0;
1121 if (set_xattr (file_name, &current_stat_info, invert_permissions,
1122 typeflag, &file_created))
1123 {
1124 skip_member ();
1125 open_error (file_name);
1126 return 1;
1127 }
1128
1129 while ((fd = open_output_file (file_name, typeflag, mode,
1130 file_created, &current_mode,
1131 &current_mode_mask))
1132 < 0)
1133 {
1134 int recover = maybe_recoverable (file_name, true, &interdir_made);
1135 if (recover != RECOVER_OK)
1136 {
1137 skip_member ();
1138 if (recover == RECOVER_SKIP)
1139 return 0;
1140 open_error (file_name);
1141 return 1;
1142 }
1143 }
1144 }
1145
1146 mv_begin_read (&current_stat_info);
1147 if (current_stat_info.is_sparse)
1148 sparse_extract_file (fd, &current_stat_info, &size);
1149 else
1150 for (size = current_stat_info.stat.st_size; size > 0; )
1151 {
1152 mv_size_left (size);
1153
1154 /* Locate data, determine max length writeable, write it,
1155 block that we have used the data, then check if the write
1156 worked. */
1157
1158 data_block = find_next_block ();
1159 if (! data_block)
1160 {
1161 ERROR ((0, 0, _("Unexpected EOF in archive")));
1162 break; /* FIXME: What happens, then? */
1163 }
1164
1165 written = available_space_after (data_block);
1166
1167 if (written > size)
1168 written = size;
1169 errno = 0;
1170 count = blocking_write (fd, data_block->buffer, written);
1171 size -= written;
1172
1173 set_next_block_after ((union block *)
1174 (data_block->buffer + written - 1));
1175 if (count != written)
1176 {
1177 if (!to_command_option)
1178 write_error_details (file_name, count, written);
1179 /* FIXME: shouldn't we restore from backup? */
1180 break;
1181 }
1182 }
1183
1184 skip_file (size);
1185
1186 mv_end ();
1187
1188 /* If writing to stdout, don't try to do anything to the filename;
1189 it doesn't exist, or we don't want to touch it anyway. */
1190
1191 if (to_stdout_option)
1192 return 0;
1193
1194 if (! to_command_option)
1195 set_stat (file_name, &current_stat_info, fd,
1196 current_mode, current_mode_mask, typeflag, false,
1197 (old_files_option == OVERWRITE_OLD_FILES
1198 ? 0 : AT_SYMLINK_NOFOLLOW));
1199
1200 status = close (fd);
1201 if (status < 0)
1202 close_error (file_name);
1203
1204 if (to_command_option)
1205 sys_wait_command ();
1206
1207 return status;
1208 }
1209
1210 /* Create a placeholder file with name FILE_NAME, which will be
1211 replaced after other extraction is done by a symbolic link if
1212 IS_SYMLINK is true, and by a hard link otherwise. Set
1213 *INTERDIR_MADE if an intermediate directory is made in the
1214 process. */
1215
1216 static int
1217 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
1218 {
1219 int fd;
1220 struct stat st;
1221
1222 while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1223 {
1224 switch (maybe_recoverable (file_name, false, interdir_made))
1225 {
1226 case RECOVER_OK:
1227 continue;
1228
1229 case RECOVER_SKIP:
1230 return 0;
1231
1232 case RECOVER_NO:
1233 open_error (file_name);
1234 return -1;
1235 }
1236 }
1237
1238 if (fstat (fd, &st) != 0)
1239 {
1240 stat_error (file_name);
1241 close (fd);
1242 }
1243 else if (close (fd) != 0)
1244 close_error (file_name);
1245 else
1246 {
1247 struct delayed_set_stat *h;
1248 struct delayed_link *p =
1249 xmalloc (offsetof (struct delayed_link, target)
1250 + strlen (current_stat_info.link_name)
1251 + 1);
1252 p->next = delayed_link_head;
1253 delayed_link_head = p;
1254 p->dev = st.st_dev;
1255 p->ino = st.st_ino;
1256 p->birthtime = get_stat_birthtime (&st);
1257 p->is_symlink = is_symlink;
1258 if (is_symlink)
1259 {
1260 p->mode = current_stat_info.stat.st_mode;
1261 p->uid = current_stat_info.stat.st_uid;
1262 p->gid = current_stat_info.stat.st_gid;
1263 p->atime = current_stat_info.atime;
1264 p->mtime = current_stat_info.mtime;
1265 }
1266 p->change_dir = chdir_current;
1267 p->sources = xmalloc (offsetof (struct string_list, string)
1268 + strlen (file_name) + 1);
1269 p->sources->next = 0;
1270 strcpy (p->sources->string, file_name);
1271 p->cntx_name = NULL;
1272 assign_string (&p->cntx_name, current_stat_info.cntx_name);
1273 p->acls_a_ptr = NULL;
1274 p->acls_a_len = 0;
1275 p->acls_d_ptr = NULL;
1276 p->acls_d_len = 0;
1277 xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
1278 strcpy (p->target, current_stat_info.link_name);
1279
1280 h = delayed_set_stat_head;
1281 if (h && ! h->after_links
1282 && strncmp (file_name, h->file_name, h->file_name_len) == 0
1283 && ISSLASH (file_name[h->file_name_len])
1284 && (last_component (file_name) == file_name + h->file_name_len + 1))
1285 mark_after_links (h);
1286
1287 return 0;
1288 }
1289
1290 return -1;
1291 }
1292
1293 static int
1294 extract_link (char *file_name, int typeflag)
1295 {
1296 bool interdir_made = false;
1297 char const *link_name;
1298 int rc;
1299
1300 link_name = current_stat_info.link_name;
1301
1302 if (! absolute_names_option && contains_dot_dot (link_name))
1303 return create_placeholder_file (file_name, false, &interdir_made);
1304
1305 do
1306 {
1307 struct stat st1, st2;
1308 int e;
1309 int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1310 e = errno;
1311
1312 if (status == 0)
1313 {
1314 struct delayed_link *ds = delayed_link_head;
1315 if (ds
1316 && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1317 for (; ds; ds = ds->next)
1318 if (ds->change_dir == chdir_current
1319 && ds->dev == st1.st_dev
1320 && ds->ino == st1.st_ino
1321 && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
1322 == 0))
1323 {
1324 struct string_list *p = xmalloc (offsetof (struct string_list, string)
1325 + strlen (file_name) + 1);
1326 strcpy (p->string, file_name);
1327 p->next = ds->sources;
1328 ds->sources = p;
1329 break;
1330 }
1331 return 0;
1332 }
1333 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1334 || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1335 == 0)
1336 && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1337 == 0)
1338 && st1.st_dev == st2.st_dev
1339 && st1.st_ino == st2.st_ino))
1340 return 0;
1341
1342 errno = e;
1343 }
1344 while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1345 == RECOVER_OK);
1346
1347 if (rc == RECOVER_SKIP)
1348 return 0;
1349 if (!(incremental_option && errno == EEXIST))
1350 {
1351 link_error (link_name, file_name);
1352 return 1;
1353 }
1354 return 0;
1355 }
1356
1357 static int
1358 extract_symlink (char *file_name, int typeflag)
1359 {
1360 #ifdef HAVE_SYMLINK
1361 bool interdir_made = false;
1362
1363 if (! absolute_names_option
1364 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1365 || contains_dot_dot (current_stat_info.link_name)))
1366 return create_placeholder_file (file_name, true, &interdir_made);
1367
1368 while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1369 switch (maybe_recoverable (file_name, false, &interdir_made))
1370 {
1371 case RECOVER_OK:
1372 continue;
1373
1374 case RECOVER_SKIP:
1375 return 0;
1376
1377 case RECOVER_NO:
1378 symlink_error (current_stat_info.link_name, file_name);
1379 return -1;
1380 }
1381
1382 set_stat (file_name, &current_stat_info, -1, 0, 0,
1383 SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1384 return 0;
1385
1386 #else
1387 static int warned_once;
1388
1389 if (!warned_once)
1390 {
1391 warned_once = 1;
1392 WARNOPT (WARN_SYMLINK_CAST,
1393 (0, 0,
1394 _("Attempting extraction of symbolic links as hard links")));
1395 }
1396 return extract_link (file_name, typeflag);
1397 #endif
1398 }
1399
1400 #if S_IFCHR || S_IFBLK
1401 static int
1402 extract_node (char *file_name, int typeflag)
1403 {
1404 bool interdir_made = false;
1405 mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1406 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1407
1408 while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1409 != 0)
1410 switch (maybe_recoverable (file_name, false, &interdir_made))
1411 {
1412 case RECOVER_OK:
1413 continue;
1414
1415 case RECOVER_SKIP:
1416 return 0;
1417
1418 case RECOVER_NO:
1419 mknod_error (file_name);
1420 return -1;
1421 }
1422
1423 set_stat (file_name, &current_stat_info, -1,
1424 mode & ~ current_umask, MODE_RWX,
1425 typeflag, false, AT_SYMLINK_NOFOLLOW);
1426 return 0;
1427 }
1428 #endif
1429
1430 #if HAVE_MKFIFO || defined mkfifo
1431 static int
1432 extract_fifo (char *file_name, int typeflag)
1433 {
1434 bool interdir_made = false;
1435 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1436 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1437
1438 while (mkfifoat (chdir_fd, file_name, mode) != 0)
1439 switch (maybe_recoverable (file_name, false, &interdir_made))
1440 {
1441 case RECOVER_OK:
1442 continue;
1443
1444 case RECOVER_SKIP:
1445 return 0;
1446
1447 case RECOVER_NO:
1448 mkfifo_error (file_name);
1449 return -1;
1450 }
1451
1452 set_stat (file_name, &current_stat_info, -1,
1453 mode & ~ current_umask, MODE_RWX,
1454 typeflag, false, AT_SYMLINK_NOFOLLOW);
1455 return 0;
1456 }
1457 #endif
1458
1459 static int
1460 extract_volhdr (char *file_name, int typeflag)
1461 {
1462 skip_member ();
1463 return 0;
1464 }
1465
1466 static int
1467 extract_failure (char *file_name, int typeflag)
1468 {
1469 return 1;
1470 }
1471
1472 static int
1473 extract_skip (char *file_name, int typeflag)
1474 {
1475 skip_member ();
1476 return 0;
1477 }
1478
1479 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1480
1481 \f
1482
1483 /* Prepare to extract a file. Find extractor function.
1484 Return zero if extraction should not proceed. */
1485
1486 static int
1487 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1488 {
1489 int rc = 1;
1490
1491 if (EXTRACT_OVER_PIPE)
1492 rc = 0;
1493
1494 /* Select the extractor */
1495 switch (typeflag)
1496 {
1497 case GNUTYPE_SPARSE:
1498 *fun = extract_file;
1499 rc = 1;
1500 break;
1501
1502 case AREGTYPE:
1503 case REGTYPE:
1504 case CONTTYPE:
1505 /* Appears to be a file. But BSD tar uses the convention that a slash
1506 suffix means a directory. */
1507 if (current_stat_info.had_trailing_slash)
1508 *fun = extract_dir;
1509 else
1510 {
1511 *fun = extract_file;
1512 rc = 1;
1513 }
1514 break;
1515
1516 case SYMTYPE:
1517 *fun = extract_symlink;
1518 break;
1519
1520 case LNKTYPE:
1521 *fun = extract_link;
1522 break;
1523
1524 #if S_IFCHR
1525 case CHRTYPE:
1526 current_stat_info.stat.st_mode |= S_IFCHR;
1527 *fun = extract_node;
1528 break;
1529 #endif
1530
1531 #if S_IFBLK
1532 case BLKTYPE:
1533 current_stat_info.stat.st_mode |= S_IFBLK;
1534 *fun = extract_node;
1535 break;
1536 #endif
1537
1538 #if HAVE_MKFIFO || defined mkfifo
1539 case FIFOTYPE:
1540 *fun = extract_fifo;
1541 break;
1542 #endif
1543
1544 case DIRTYPE:
1545 case GNUTYPE_DUMPDIR:
1546 *fun = extract_dir;
1547 if (current_stat_info.is_dumpdir)
1548 delay_directory_restore_option = true;
1549 break;
1550
1551 case GNUTYPE_VOLHDR:
1552 *fun = extract_volhdr;
1553 break;
1554
1555 case GNUTYPE_MULTIVOL:
1556 ERROR ((0, 0,
1557 _("%s: Cannot extract -- file is continued from another volume"),
1558 quotearg_colon (current_stat_info.file_name)));
1559 *fun = extract_skip;
1560 break;
1561
1562 case GNUTYPE_LONGNAME:
1563 case GNUTYPE_LONGLINK:
1564 ERROR ((0, 0, _("Unexpected long name header")));
1565 *fun = extract_failure;
1566 break;
1567
1568 default:
1569 WARNOPT (WARN_UNKNOWN_CAST,
1570 (0, 0,
1571 _("%s: Unknown file type '%c', extracted as normal file"),
1572 quotearg_colon (file_name), typeflag));
1573 *fun = extract_file;
1574 }
1575
1576 /* Determine whether the extraction should proceed */
1577 if (rc == 0)
1578 return 0;
1579
1580 switch (old_files_option)
1581 {
1582 case UNLINK_FIRST_OLD_FILES:
1583 if (!remove_any_file (file_name,
1584 recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1585 : ORDINARY_REMOVE_OPTION)
1586 && errno && errno != ENOENT)
1587 {
1588 unlink_error (file_name);
1589 return 0;
1590 }
1591 break;
1592
1593 case KEEP_NEWER_FILES:
1594 if (file_newer_p (file_name, 0, &current_stat_info))
1595 {
1596 WARNOPT (WARN_IGNORE_NEWER,
1597 (0, 0, _("Current %s is newer or same age"),
1598 quote (file_name)));
1599 return 0;
1600 }
1601 break;
1602
1603 default:
1604 break;
1605 }
1606
1607 return 1;
1608 }
1609
1610 void
1611 maybe_prepend_name (char **file_name)
1612 {
1613 int i;
1614
1615 for (i = 0; i < strlen (*file_name); i++)
1616 if (!ISSLASH ((*file_name)[i]) && (*file_name)[i] != '.') break;
1617
1618 if (i == strlen (*file_name))
1619 return;
1620
1621 if (!strncmp (*file_name + i, one_top_level, strlen (one_top_level)))
1622 {
1623 int pos = i + strlen (one_top_level);
1624 if (ISSLASH ((*file_name)[pos]) || (*file_name)[pos] == '\0') return;
1625 }
1626
1627 char *new_name = xmalloc (strlen (one_top_level) + strlen (*file_name) + 2);
1628
1629 strcpy (new_name, one_top_level);
1630 strcat (new_name, "/");
1631 strcat (new_name, *file_name);
1632
1633 free (*file_name);
1634 *file_name = new_name;
1635 }
1636
1637 /* Extract a file from the archive. */
1638 void
1639 extract_archive (void)
1640 {
1641 char typeflag;
1642 tar_extractor_t fun;
1643
1644 fatal_exit_hook = extract_finish;
1645
1646 set_next_block_after (current_header);
1647
1648 if (!current_stat_info.file_name[0]
1649 || (interactive_option
1650 && !confirm ("extract", current_stat_info.file_name)))
1651 {
1652 skip_member ();
1653 return;
1654 }
1655
1656 /* Print the block from current_header and current_stat. */
1657 if (verbose_option)
1658 print_header (&current_stat_info, current_header, -1);
1659
1660 /* Restore stats for all non-ancestor directories, unless
1661 it is an incremental archive.
1662 (see NOTICE in the comment to delay_set_stat above) */
1663 if (!delay_directory_restore_option)
1664 {
1665 int dir = chdir_current;
1666 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1667 chdir_do (dir);
1668 }
1669
1670 /* Take a safety backup of a previously existing file. */
1671
1672 if (backup_option)
1673 if (!maybe_backup_file (current_stat_info.file_name, 0))
1674 {
1675 int e = errno;
1676 ERROR ((0, e, _("%s: Was unable to backup this file"),
1677 quotearg_colon (current_stat_info.file_name)));
1678 skip_member ();
1679 return;
1680 }
1681
1682 /* Extract the archive entry according to its type. */
1683 /* KLUDGE */
1684 typeflag = sparse_member_p (&current_stat_info) ?
1685 GNUTYPE_SPARSE : current_header->header.typeflag;
1686
1687 if (one_top_level_option)
1688 maybe_prepend_name (&current_stat_info.file_name);
1689
1690 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1691 {
1692 if (fun && (*fun) (current_stat_info.file_name, typeflag)
1693 && backup_option)
1694 undo_last_backup ();
1695 }
1696 else
1697 skip_member ();
1698
1699 }
1700
1701 /* Extract the links whose final extraction were delayed. */
1702 static void
1703 apply_delayed_links (void)
1704 {
1705 struct delayed_link *ds;
1706
1707 for (ds = delayed_link_head; ds; )
1708 {
1709 struct string_list *sources = ds->sources;
1710 char const *valid_source = 0;
1711
1712 chdir_do (ds->change_dir);
1713
1714 for (sources = ds->sources; sources; sources = sources->next)
1715 {
1716 char const *source = sources->string;
1717 struct stat st;
1718
1719 /* Make sure the placeholder file is still there. If not,
1720 don't create a link, as the placeholder was probably
1721 removed by a later extraction. */
1722 if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1723 && st.st_dev == ds->dev
1724 && st.st_ino == ds->ino
1725 && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
1726 {
1727 /* Unlink the placeholder, then create a hard link if possible,
1728 a symbolic link otherwise. */
1729 if (unlinkat (chdir_fd, source, 0) != 0)
1730 unlink_error (source);
1731 else if (valid_source
1732 && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1733 == 0))
1734 ;
1735 else if (!ds->is_symlink)
1736 {
1737 if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1738 link_error (ds->target, source);
1739 }
1740 else if (symlinkat (ds->target, chdir_fd, source) != 0)
1741 symlink_error (ds->target, source);
1742 else
1743 {
1744 struct tar_stat_info st1;
1745 st1.stat.st_mode = ds->mode;
1746 st1.stat.st_uid = ds->uid;
1747 st1.stat.st_gid = ds->gid;
1748 st1.atime = ds->atime;
1749 st1.mtime = ds->mtime;
1750 st1.cntx_name = ds->cntx_name;
1751 st1.acls_a_ptr = ds->acls_a_ptr;
1752 st1.acls_a_len = ds->acls_a_len;
1753 st1.acls_d_ptr = ds->acls_d_ptr;
1754 st1.acls_d_len = ds->acls_d_len;
1755 st1.xattr_map = ds->xattr_map;
1756 st1.xattr_map_size = ds->xattr_map_size;
1757 set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1758 false, AT_SYMLINK_NOFOLLOW);
1759 valid_source = source;
1760 }
1761 }
1762 }
1763
1764 for (sources = ds->sources; sources; )
1765 {
1766 struct string_list *next = sources->next;
1767 free (sources);
1768 sources = next;
1769 }
1770
1771 xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
1772 free (ds->cntx_name);
1773
1774 {
1775 struct delayed_link *next = ds->next;
1776 free (ds);
1777 ds = next;
1778 }
1779 }
1780
1781 delayed_link_head = 0;
1782 }
1783
1784 /* Finish the extraction of an archive. */
1785 void
1786 extract_finish (void)
1787 {
1788 /* First, fix the status of ordinary directories that need fixing. */
1789 apply_nonancestor_delayed_set_stat ("", 0);
1790
1791 /* Then, apply delayed links, so that they don't affect delayed
1792 directory status-setting for ordinary directories. */
1793 apply_delayed_links ();
1794
1795 /* Finally, fix the status of directories that are ancestors
1796 of delayed links. */
1797 apply_nonancestor_delayed_set_stat ("", 1);
1798 }
1799
1800 bool
1801 rename_directory (char *src, char *dst)
1802 {
1803 if (renameat (chdir_fd, src, chdir_fd, dst) != 0)
1804 {
1805 int e = errno;
1806 bool interdir_made;
1807
1808 switch (e)
1809 {
1810 case ENOENT:
1811 if (make_directories (dst, &interdir_made) == 0)
1812 {
1813 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1814 return true;
1815 e = errno;
1816 }
1817 break;
1818
1819 case EXDEV:
1820 /* FIXME: Fall back to recursive copying */
1821
1822 default:
1823 break;
1824 }
1825
1826 ERROR ((0, e, _("Cannot rename %s to %s"),
1827 quote_n (0, src),
1828 quote_n (1, dst)));
1829 return false;
1830 }
1831 return true;
1832 }
This page took 0.114584 seconds and 4 git commands to generate.