Version 1.31
[tar.git] / src / extract.c
blob090b866ce246836eac96e417094044c3f0fda21f
1 /* Extract files from a tar archive.
3 Copyright 1988-2019 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Written by John Gilmore, on 1985-11-19. */
22 #include <system.h>
23 #include <quotearg.h>
24 #include <errno.h>
25 #include <priv-set.h>
26 #include <root-uid.h>
27 #include <utimens.h>
29 #include "common.h"
31 static bool we_are_root; /* true if our effective uid == 0 */
32 static mode_t newdir_umask; /* umask when creating new directories */
33 static mode_t current_umask; /* current umask (which is set to 0 if -p) */
35 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
37 #if ! HAVE_FCHMOD && ! defined fchmod
38 # define fchmod(fd, mode) (errno = ENOSYS, -1)
39 #endif
40 #if ! HAVE_FCHOWN && ! defined fchown
41 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
42 #endif
44 /* Return true if an error number ERR means the system call is
45 supported in this case. */
46 static bool
47 implemented (int err)
49 return ! (err == ENOSYS
50 || err == ENOTSUP
51 || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
54 /* List of directories whose statuses we need to extract after we've
55 finished extracting their subsidiary files. If you consider each
56 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
57 represents an element where AFTER_LINKS is nonzero and [^D]
58 represents an element where AFTER_LINKS is zero, then the head
59 of the subsequence has the longest name, and each non-head element
60 in the prefix is an ancestor (in the directory hierarchy) of the
61 preceding element. */
63 struct delayed_set_stat
65 /* Next directory in list. */
66 struct delayed_set_stat *next;
68 /* Metadata for this directory. */
69 dev_t dev;
70 ino_t ino;
71 mode_t mode; /* The desired mode is MODE & ~ current_umask. */
72 uid_t uid;
73 gid_t gid;
74 struct timespec atime;
75 struct timespec mtime;
77 /* An estimate of the directory's current mode, along with a mask
78 specifying which bits of this estimate are known to be correct.
79 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
80 matter. */
81 mode_t current_mode;
82 mode_t current_mode_mask;
84 /* This directory is an intermediate directory that was created
85 as an ancestor of some other directory; it was not mentioned
86 in the archive, so do not set its uid, gid, atime, or mtime,
87 and don't alter its mode outside of MODE_RWX. */
88 bool interdir;
90 /* Whether symbolic links should be followed when accessing the
91 directory. */
92 int atflag;
94 /* Do not set the status of this directory until after delayed
95 links are created. */
96 bool after_links;
98 /* Directory that the name is relative to. */
99 int change_dir;
101 /* extended attributes*/
102 char *cntx_name;
103 char *acls_a_ptr;
104 size_t acls_a_len;
105 char *acls_d_ptr;
106 size_t acls_d_len;
107 size_t xattr_map_size;
108 struct xattr_array *xattr_map;
109 /* Length and contents of name. */
110 size_t file_name_len;
111 char *file_name;
114 static struct delayed_set_stat *delayed_set_stat_head;
116 /* List of links whose creation we have delayed. */
117 struct delayed_link
119 /* The next delayed link in the list. */
120 struct delayed_link *next;
122 /* The device, inode number and birthtime of the placeholder.
123 birthtime.tv_nsec is negative if the birthtime is not available.
124 Don't use mtime as this would allow for false matches if some
125 other process removes the placeholder. Don't use ctime as
126 this would cause race conditions and other screwups, e.g.,
127 when restoring hard-linked symlinks. */
128 dev_t dev;
129 ino_t ino;
130 struct timespec birthtime;
132 /* True if the link is symbolic. */
133 bool is_symlink;
135 /* The desired metadata, valid only the link is symbolic. */
136 mode_t mode;
137 uid_t uid;
138 gid_t gid;
139 struct timespec atime;
140 struct timespec mtime;
142 /* The directory that the sources and target are relative to. */
143 int change_dir;
145 /* A list of sources for this link. The sources are all to be
146 hard-linked together. */
147 struct string_list *sources;
149 /* SELinux context */
150 char *cntx_name;
152 /* ACLs */
153 char *acls_a_ptr;
154 size_t acls_a_len;
155 char *acls_d_ptr;
156 size_t acls_d_len;
158 size_t xattr_map_size;
159 struct xattr_array *xattr_map;
161 /* The desired target of the desired link. */
162 char target[1];
165 static struct delayed_link *delayed_link_head;
167 struct string_list
169 struct string_list *next;
170 char string[1];
173 /* Set up to extract files. */
174 void
175 extr_init (void)
177 we_are_root = geteuid () == ROOT_UID;
178 same_permissions_option += we_are_root;
179 same_owner_option += we_are_root;
181 /* Option -p clears the kernel umask, so it does not affect proper
182 restoration of file permissions. New intermediate directories will
183 comply with umask at start of program. */
185 newdir_umask = umask (0);
186 if (0 < same_permissions_option)
187 current_umask = 0;
188 else
190 umask (newdir_umask); /* restore the kernel umask */
191 current_umask = newdir_umask;
195 /* Use fchmod if possible, fchmodat otherwise. */
196 static int
197 fd_chmod (int fd, char const *file, mode_t mode, int atflag)
199 if (0 <= fd)
201 int result = fchmod (fd, mode);
202 if (result == 0 || implemented (errno))
203 return result;
205 return fchmodat (chdir_fd, file, mode, atflag);
208 /* Use fchown if possible, fchownat otherwise. */
209 static int
210 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
212 if (0 <= fd)
214 int result = fchown (fd, uid, gid);
215 if (result == 0 || implemented (errno))
216 return result;
218 return fchownat (chdir_fd, file, uid, gid, atflag);
221 /* Use fstat if possible, fstatat otherwise. */
222 static int
223 fd_stat (int fd, char const *file, struct stat *st, int atflag)
225 return (0 <= fd
226 ? fstat (fd, st)
227 : fstatat (chdir_fd, file, st, atflag));
230 /* Set the mode for FILE_NAME to MODE.
231 MODE_MASK specifies the bits of MODE that we care about;
232 thus if MODE_MASK is zero, do nothing.
233 If FD is nonnegative, it is a file descriptor for the file.
234 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
235 the file's current mode, using the style of struct delayed_set_stat.
236 TYPEFLAG specifies the type of the file.
237 ATFLAG specifies the flag to use when statting the file. */
238 static void
239 set_mode (char const *file_name,
240 mode_t mode, mode_t mode_mask, int fd,
241 mode_t current_mode, mode_t current_mode_mask,
242 char typeflag, int atflag)
244 if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
246 if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
248 struct stat st;
249 if (fd_stat (fd, file_name, &st, atflag) != 0)
251 stat_error (file_name);
252 return;
254 current_mode = st.st_mode;
257 current_mode &= MODE_ALL;
258 mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
260 if (current_mode != mode)
262 int chmod_errno =
263 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
265 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
266 setuid-root files would otherwise be a backdoor. See
267 http://opensolaris.org/jive/thread.jspa?threadID=95826
268 (2009-09-03). */
269 if (chmod_errno == EPERM && (mode & S_ISUID)
270 && priv_set_restore_linkdir () == 0)
272 chmod_errno =
273 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
274 priv_set_remove_linkdir ();
277 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
278 returns ENOTSUP even when operating on non-symlinks, try
279 again with the flag disabled if it does not appear to be
280 supported and if the file is not a symlink. This
281 introduces a race, alas. */
282 if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
283 chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
285 if (chmod_errno
286 && (typeflag != SYMTYPE || implemented (chmod_errno)))
288 errno = chmod_errno;
289 chmod_error_details (file_name, mode);
295 /* Check time after successfully setting FILE_NAME's time stamp to T. */
296 static void
297 check_time (char const *file_name, struct timespec t)
299 if (t.tv_sec < 0)
300 WARNOPT (WARN_TIMESTAMP,
301 (0, 0, _("%s: implausibly old time stamp %s"),
302 file_name, tartime (t, true)));
303 else if (timespec_cmp (volume_start_time, t) < 0)
305 struct timespec now;
306 gettime (&now);
307 if (timespec_cmp (now, t) < 0)
309 char buf[TIMESPEC_STRSIZE_BOUND];
310 struct timespec diff;
311 diff.tv_sec = t.tv_sec - now.tv_sec;
312 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
313 if (diff.tv_nsec < 0)
315 diff.tv_nsec += BILLION;
316 diff.tv_sec--;
318 WARNOPT (WARN_TIMESTAMP,
319 (0, 0, _("%s: time stamp %s is %s s in the future"),
320 file_name, tartime (t, true), code_timespec (diff, buf)));
325 /* Restore stat attributes (owner, group, mode and times) for
326 FILE_NAME, using information given in *ST.
327 If FD is nonnegative, it is a file descriptor for the file.
328 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
329 the file's current mode, using the style of struct delayed_set_stat.
330 TYPEFLAG specifies the type of the file.
331 If INTERDIR, this is an intermediate directory.
332 ATFLAG specifies the flag to use when statting the file. */
334 static void
335 set_stat (char const *file_name,
336 struct tar_stat_info const *st,
337 int fd, mode_t current_mode, mode_t current_mode_mask,
338 char typeflag, bool interdir, int atflag)
340 /* Do the utime before the chmod because some versions of utime are
341 broken and trash the modes of the file. */
343 if (! touch_option && ! interdir)
345 struct timespec ts[2];
346 if (incremental_option)
347 ts[0] = st->atime;
348 else
349 ts[0].tv_nsec = UTIME_OMIT;
350 ts[1] = st->mtime;
352 if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
354 if (incremental_option)
355 check_time (file_name, ts[0]);
356 check_time (file_name, ts[1]);
358 else if (typeflag != SYMTYPE || implemented (errno))
359 utime_error (file_name);
362 if (0 < same_owner_option && ! interdir)
364 /* Some systems allow non-root users to give files away. Once this
365 done, it is not possible anymore to change file permissions.
366 However, setting file permissions now would be incorrect, since
367 they would apply to the wrong user, and there would be a race
368 condition. So, don't use systems that allow non-root users to
369 give files away. */
370 uid_t uid = st->stat.st_uid;
371 gid_t gid = st->stat.st_gid;
373 if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
375 /* Changing the owner can clear st_mode bits in some cases. */
376 if ((current_mode | ~ current_mode_mask) & S_IXUGO)
377 current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
379 else if (typeflag != SYMTYPE || implemented (errno))
380 chown_error_details (file_name, uid, gid);
383 set_mode (file_name,
384 st->stat.st_mode & ~ current_umask,
385 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
386 fd, current_mode, current_mode_mask, typeflag, atflag);
388 /* these three calls must be done *after* fd_chown() call because fd_chown
389 causes that linux capabilities becomes cleared. */
390 xattrs_xattrs_set (st, file_name, typeflag, 1);
391 xattrs_acls_set (st, file_name, typeflag);
392 xattrs_selinux_set (st, file_name, typeflag);
395 /* Find the direct ancestor of FILE_NAME in the delayed_set_stat list.
397 static struct delayed_set_stat *
398 find_direct_ancestor (char const *file_name)
400 struct delayed_set_stat *h = delayed_set_stat_head;
401 while (h)
403 if (h && ! h->after_links
404 && strncmp (file_name, h->file_name, h->file_name_len) == 0
405 && ISSLASH (file_name[h->file_name_len])
406 && (last_component (file_name) == file_name + h->file_name_len + 1))
407 break;
408 h = h->next;
410 return h;
413 /* For each entry H in the leading prefix of entries in HEAD that do
414 not have after_links marked, mark H and fill in its dev and ino
415 members. Assume HEAD && ! HEAD->after_links. */
416 static void
417 mark_after_links (struct delayed_set_stat *head)
419 struct delayed_set_stat *h = head;
423 struct stat st;
424 h->after_links = 1;
426 if (deref_stat (h->file_name, &st) != 0)
427 stat_error (h->file_name);
428 else
430 h->dev = st.st_dev;
431 h->ino = st.st_ino;
434 while ((h = h->next) && ! h->after_links);
437 /* Remember to restore stat attributes (owner, group, mode and times)
438 for the directory FILE_NAME, using information given in *ST,
439 once we stop extracting files into that directory.
441 If ST is null, merely create a placeholder node for an intermediate
442 directory that was created by make_directories.
444 NOTICE: this works only if the archive has usual member order, i.e.
445 directory, then the files in that directory. Incremental archive have
446 somewhat reversed order: first go subdirectories, then all other
447 members. To help cope with this case the variable
448 delay_directory_restore_option is set by prepare_to_extract.
450 If an archive was explicitely created so that its member order is
451 reversed, some directory timestamps can be restored incorrectly,
452 e.g.:
453 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
455 static void
456 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
457 mode_t current_mode, mode_t current_mode_mask,
458 mode_t mode, int atflag)
460 size_t file_name_len = strlen (file_name);
461 struct delayed_set_stat *data = xmalloc (sizeof (*data));
462 data->next = delayed_set_stat_head;
463 data->mode = mode;
464 if (st)
466 data->dev = st->stat.st_dev;
467 data->ino = st->stat.st_ino;
468 data->uid = st->stat.st_uid;
469 data->gid = st->stat.st_gid;
470 data->atime = st->atime;
471 data->mtime = st->mtime;
473 data->file_name_len = file_name_len;
474 data->file_name = xstrdup (file_name);
475 data->current_mode = current_mode;
476 data->current_mode_mask = current_mode_mask;
477 data->interdir = ! st;
478 data->atflag = atflag;
479 data->after_links = 0;
480 data->change_dir = chdir_current;
481 data->cntx_name = NULL;
482 if (st)
483 assign_string (&data->cntx_name, st->cntx_name);
484 if (st && st->acls_a_ptr)
486 data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
487 data->acls_a_len = st->acls_a_len;
489 else
491 data->acls_a_ptr = NULL;
492 data->acls_a_len = 0;
494 if (st && st->acls_d_ptr)
496 data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
497 data->acls_d_len = st->acls_d_len;
499 else
501 data->acls_d_ptr = NULL;
502 data->acls_d_len = 0;
504 if (st)
505 xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
506 else
508 data->xattr_map = NULL;
509 data->xattr_map_size = 0;
511 strcpy (data->file_name, file_name);
512 delayed_set_stat_head = data;
513 if (must_be_dot_or_slash (file_name))
514 mark_after_links (data);
517 /* Update the delayed_set_stat info for an intermediate directory
518 created within the file name of DIR. The intermediate directory turned
519 out to be the same as this directory, e.g. due to ".." or symbolic
520 links. *DIR_STAT_INFO is the status of the directory. */
521 static void
522 repair_delayed_set_stat (char const *dir,
523 struct stat const *dir_stat_info)
525 struct delayed_set_stat *data;
526 for (data = delayed_set_stat_head; data; data = data->next)
528 struct stat st;
529 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
531 stat_error (data->file_name);
532 return;
535 if (st.st_dev == dir_stat_info->st_dev
536 && st.st_ino == dir_stat_info->st_ino)
538 data->dev = current_stat_info.stat.st_dev;
539 data->ino = current_stat_info.stat.st_ino;
540 data->mode = current_stat_info.stat.st_mode;
541 data->uid = current_stat_info.stat.st_uid;
542 data->gid = current_stat_info.stat.st_gid;
543 data->atime = current_stat_info.atime;
544 data->mtime = current_stat_info.mtime;
545 data->current_mode = st.st_mode;
546 data->current_mode_mask = ALL_MODE_BITS;
547 data->interdir = false;
548 return;
552 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
553 quotearg_colon (dir)));
556 static void
557 free_delayed_set_stat (struct delayed_set_stat *data)
559 free (data->file_name);
560 xheader_xattr_free (data->xattr_map, data->xattr_map_size);
561 free (data->cntx_name);
562 free (data->acls_a_ptr);
563 free (data->acls_d_ptr);
564 free (data);
567 void
568 remove_delayed_set_stat (const char *fname)
570 struct delayed_set_stat *data, *next, *prev = NULL;
571 for (data = delayed_set_stat_head; data; data = next)
573 next = data->next;
574 if (chdir_current == data->change_dir
575 && strcmp (data->file_name, fname) == 0)
577 free_delayed_set_stat (data);
578 if (prev)
579 prev->next = next;
580 else
581 delayed_set_stat_head = next;
582 return;
584 else
585 prev = data;
589 static void
590 fixup_delayed_set_stat (char const *src, char const *dst)
592 struct delayed_set_stat *data;
593 for (data = delayed_set_stat_head; data; data = data->next)
595 if (chdir_current == data->change_dir
596 && strcmp (data->file_name, src) == 0)
598 free (data->file_name);
599 data->file_name = xstrdup (dst);
600 data->file_name_len = strlen (dst);
601 return;
606 /* After a file/link/directory creation has failed, see if
607 it's because some required directory was not present, and if so,
608 create all required directories. Return zero if all the required
609 directories were created, nonzero (issuing a diagnostic) otherwise.
610 Set *INTERDIR_MADE if at least one directory was created. */
611 static int
612 make_directories (char *file_name, bool *interdir_made)
614 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
615 char *cursor; /* points into the file name */
617 for (cursor = cursor0; *cursor; cursor++)
619 mode_t mode;
620 mode_t desired_mode;
621 int status;
623 if (! ISSLASH (*cursor))
624 continue;
626 /* Avoid mkdir of empty string, if leading or double '/'. */
628 if (cursor == cursor0 || ISSLASH (cursor[-1]))
629 continue;
631 /* Avoid mkdir where last part of file name is "." or "..". */
633 if (cursor[-1] == '.'
634 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
635 || (cursor[-2] == '.'
636 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
637 continue;
639 *cursor = '\0'; /* truncate the name there */
640 desired_mode = MODE_RWX & ~ newdir_umask;
641 mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
642 status = mkdirat (chdir_fd, file_name, mode);
644 if (status == 0)
646 /* Create a struct delayed_set_stat even if
647 mode == desired_mode, because
648 repair_delayed_set_stat may need to update the struct. */
649 delay_set_stat (file_name,
650 0, mode & ~ current_umask, MODE_RWX,
651 desired_mode, AT_SYMLINK_NOFOLLOW);
653 print_for_mkdir (file_name, cursor - file_name, desired_mode);
654 *interdir_made = true;
656 else if (errno == EEXIST)
657 status = 0;
658 else
660 /* Check whether the desired file exists. Even when the
661 file exists, mkdir can fail with some errno value E other
662 than EEXIST, so long as E describes an error condition
663 that also applies. */
664 int e = errno;
665 struct stat st;
666 status = fstatat (chdir_fd, file_name, &st, 0);
667 if (status)
669 errno = e;
670 mkdir_error (file_name);
674 *cursor = '/';
675 if (status)
676 return status;
679 return 0;
682 /* Return true if FILE_NAME (with status *STP, if STP) is not a
683 directory, and has a time stamp newer than (or equal to) that of
684 TAR_STAT. */
685 static bool
686 file_newer_p (const char *file_name, struct stat const *stp,
687 struct tar_stat_info *tar_stat)
689 struct stat st;
691 if (!stp)
693 if (deref_stat (file_name, &st) != 0)
695 if (errno != ENOENT)
697 stat_warn (file_name);
698 /* Be safer: if the file exists, assume it is newer. */
699 return true;
701 return false;
703 stp = &st;
706 return (! S_ISDIR (stp->st_mode)
707 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
710 #define RECOVER_NO 0
711 #define RECOVER_OK 1
712 #define RECOVER_SKIP 2
714 /* Attempt repairing what went wrong with the extraction. Delete an
715 already existing file or create missing intermediate directories.
716 Return RECOVER_OK if we somewhat increased our chances at a successful
717 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
718 caller should skip extraction of that member. The value of errno is
719 properly restored on returning RECOVER_NO.
721 If REGULAR, the caller was trying to extract onto a regular file.
723 Set *INTERDIR_MADE if an intermediate directory is made as part of
724 the recovery process. */
726 static int
727 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
729 int e = errno;
730 struct stat st;
731 struct stat const *stp = 0;
733 if (*interdir_made)
734 return RECOVER_NO;
736 switch (e)
738 case ELOOP:
740 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
741 but some operating systems do not conform to the standard. */
742 #ifdef EFTYPE
743 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
744 case EFTYPE:
745 #endif
746 /* FreeBSD 8.1 uses errno == EMLINK. */
747 case EMLINK:
748 /* Tru64 5.1B uses errno == ENOTSUP. */
749 case ENOTSUP:
751 if (! regular
752 || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
753 break;
754 if (strchr (file_name, '/'))
756 if (deref_stat (file_name, &st) != 0)
757 break;
758 stp = &st;
760 /* The caller tried to open a symbolic link with O_NOFOLLOW.
761 Fall through, treating it as an already-existing file. */
762 FALLTHROUGH;
763 case EEXIST:
764 /* Remove an old file, if the options allow this. */
766 switch (old_files_option)
768 case SKIP_OLD_FILES:
769 WARNOPT (WARN_EXISTING_FILE,
770 (0, 0, _("%s: skipping existing file"), file_name));
771 return RECOVER_SKIP;
773 case KEEP_OLD_FILES:
774 return RECOVER_NO;
776 case KEEP_NEWER_FILES:
777 if (file_newer_p (file_name, stp, &current_stat_info))
778 break;
779 FALLTHROUGH;
780 case DEFAULT_OLD_FILES:
781 case NO_OVERWRITE_DIR_OLD_FILES:
782 case OVERWRITE_OLD_FILES:
783 if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
784 return RECOVER_OK;
785 break;
787 case UNLINK_FIRST_OLD_FILES:
788 break;
790 abort (); /* notreached */
792 case ENOENT:
793 /* Attempt creating missing intermediate directories. */
794 if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
795 return RECOVER_OK;
796 break;
798 default:
799 /* Just say we can't do anything about it... */
800 break;
803 errno = e;
804 return RECOVER_NO;
807 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
808 given in *ST. Restore before extraction because they may affect file layout
809 (e.g. on Lustre distributed parallel filesystem - setting info about how many
810 servers is this file striped over, stripe size, mirror copies, etc.
811 in advance dramatically improves the following performance of reading and
812 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
813 bits from the file's current permissions. TYPEFLAG specifies the type of the
814 file. Returns non-zero when error occurs (while un-available xattrs is not
815 an error, rather no-op). Non-zero FILE_CREATED indicates set_xattr has
816 created the file. */
817 static int
818 set_xattr (char const *file_name, struct tar_stat_info const *st,
819 mode_t invert_permissions, char typeflag, int *file_created)
821 #ifdef HAVE_XATTRS
822 bool interdir_made = false;
824 if ((xattrs_option > 0) && st->xattr_map_size)
826 mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
828 for (;;)
830 if (!mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0))
832 /* Successfully created file */
833 xattrs_xattrs_set (st, file_name, typeflag, 0);
834 *file_created = 1;
835 return 0;
838 switch (maybe_recoverable ((char *)file_name, false, &interdir_made))
840 case RECOVER_OK:
841 continue;
842 case RECOVER_NO:
843 skip_member ();
844 open_error (file_name);
845 return 1;
846 case RECOVER_SKIP:
847 return 0;
851 #endif
853 return 0;
856 /* Fix the statuses of all directories whose statuses need fixing, and
857 which are not ancestors of FILE_NAME. If AFTER_LINKS is
858 nonzero, do this for all such directories; otherwise, stop at the
859 first directory that is marked to be fixed up only after delayed
860 links are applied. */
861 static void
862 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
864 size_t file_name_len = strlen (file_name);
865 bool check_for_renamed_directories = 0;
867 while (delayed_set_stat_head)
869 struct delayed_set_stat *data = delayed_set_stat_head;
870 bool skip_this_one = 0;
871 struct stat st;
872 mode_t current_mode = data->current_mode;
873 mode_t current_mode_mask = data->current_mode_mask;
875 check_for_renamed_directories |= data->after_links;
877 if (after_links < data->after_links
878 || (data->file_name_len < file_name_len
879 && file_name[data->file_name_len]
880 && (ISSLASH (file_name[data->file_name_len])
881 || ISSLASH (file_name[data->file_name_len - 1]))
882 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
883 break;
885 chdir_do (data->change_dir);
887 if (check_for_renamed_directories)
889 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
891 stat_error (data->file_name);
892 skip_this_one = 1;
894 else
896 current_mode = st.st_mode;
897 current_mode_mask = ALL_MODE_BITS;
898 if (! (st.st_dev == data->dev && st.st_ino == data->ino))
900 ERROR ((0, 0,
901 _("%s: Directory renamed before its status could be extracted"),
902 quotearg_colon (data->file_name)));
903 skip_this_one = 1;
908 if (! skip_this_one)
910 struct tar_stat_info sb;
911 sb.stat.st_mode = data->mode;
912 sb.stat.st_uid = data->uid;
913 sb.stat.st_gid = data->gid;
914 sb.atime = data->atime;
915 sb.mtime = data->mtime;
916 sb.cntx_name = data->cntx_name;
917 sb.acls_a_ptr = data->acls_a_ptr;
918 sb.acls_a_len = data->acls_a_len;
919 sb.acls_d_ptr = data->acls_d_ptr;
920 sb.acls_d_len = data->acls_d_len;
921 sb.xattr_map = data->xattr_map;
922 sb.xattr_map_size = data->xattr_map_size;
923 set_stat (data->file_name, &sb,
924 -1, current_mode, current_mode_mask,
925 DIRTYPE, data->interdir, data->atflag);
928 delayed_set_stat_head = data->next;
929 free_delayed_set_stat (data);
934 static bool
935 is_directory_link (const char *file_name)
937 struct stat st;
938 int e = errno;
939 int res;
941 res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
942 S_ISLNK (st.st_mode) &&
943 fstatat (chdir_fd, file_name, &st, 0) == 0 &&
944 S_ISDIR (st.st_mode));
945 errno = e;
946 return res;
949 /* Extractor functions for various member types */
951 static int
952 extract_dir (char *file_name, int typeflag)
954 int status;
955 mode_t mode;
956 mode_t current_mode = 0;
957 mode_t current_mode_mask = 0;
958 int atflag = 0;
959 bool interdir_made = false;
961 /* Save 'root device' to avoid purging mount points. */
962 if (one_file_system_option && root_device == 0)
964 struct stat st;
966 if (fstatat (chdir_fd, ".", &st, 0) != 0)
967 stat_diag (".");
968 else
969 root_device = st.st_dev;
972 if (incremental_option)
973 /* Read the entry and delete files that aren't listed in the archive. */
974 purge_directory (file_name);
975 else if (typeflag == GNUTYPE_DUMPDIR)
976 skip_member ();
978 /* If ownership or permissions will be restored later, create the
979 directory with restrictive permissions at first, so that in the
980 meantime processes owned by other users do not inadvertently
981 create files under this directory that inherit the wrong owner,
982 group, or permissions from the directory. If not root, though,
983 make the directory writeable and searchable at first, so that
984 files can be created under it. */
985 mode = ((current_stat_info.stat.st_mode
986 & (0 < same_owner_option || 0 < same_permissions_option
987 ? S_IRWXU
988 : MODE_RWX))
989 | (we_are_root ? 0 : MODE_WXUSR));
991 for (;;)
993 status = mkdirat (chdir_fd, file_name, mode);
994 if (status == 0)
996 current_mode = mode & ~ current_umask;
997 current_mode_mask = MODE_RWX;
998 atflag = AT_SYMLINK_NOFOLLOW;
999 break;
1002 if (errno == EEXIST
1003 && (interdir_made
1004 || keep_directory_symlink_option
1005 || old_files_option == DEFAULT_OLD_FILES
1006 || old_files_option == OVERWRITE_OLD_FILES))
1008 struct stat st;
1010 if (keep_directory_symlink_option && is_directory_link (file_name))
1011 return 0;
1013 if (deref_stat (file_name, &st) == 0)
1015 current_mode = st.st_mode;
1016 current_mode_mask = ALL_MODE_BITS;
1018 if (S_ISDIR (current_mode))
1020 if (interdir_made)
1022 repair_delayed_set_stat (file_name, &st);
1023 return 0;
1025 break;
1028 errno = EEXIST;
1031 switch (maybe_recoverable (file_name, false, &interdir_made))
1033 case RECOVER_OK:
1034 continue;
1036 case RECOVER_SKIP:
1037 break;
1039 case RECOVER_NO:
1040 if (errno != EEXIST)
1042 mkdir_error (file_name);
1043 return 1;
1045 break;
1047 break;
1050 if (status == 0
1051 || old_files_option == DEFAULT_OLD_FILES
1052 || old_files_option == OVERWRITE_OLD_FILES)
1053 delay_set_stat (file_name, &current_stat_info,
1054 current_mode, current_mode_mask,
1055 current_stat_info.stat.st_mode, atflag);
1056 return status;
1061 static int
1062 open_output_file (char const *file_name, int typeflag, mode_t mode,
1063 int file_created, mode_t *current_mode,
1064 mode_t *current_mode_mask)
1066 int fd;
1067 bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
1068 int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
1069 | O_CREAT
1070 | (overwriting_old_files
1071 ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
1072 : O_EXCL));
1074 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
1075 if (file_created)
1076 openflag = openflag & ~O_EXCL;
1078 if (typeflag == CONTTYPE)
1080 static int conttype_diagnosed;
1082 if (!conttype_diagnosed)
1084 conttype_diagnosed = 1;
1085 WARNOPT (WARN_CONTIGUOUS_CAST,
1086 (0, 0, _("Extracting contiguous files as regular files")));
1090 /* If O_NOFOLLOW is needed but does not work, check for a symlink
1091 separately. There's a race condition, but that cannot be avoided
1092 on hosts lacking O_NOFOLLOW. */
1093 if (! HAVE_WORKING_O_NOFOLLOW
1094 && overwriting_old_files && ! dereference_option)
1096 struct stat st;
1097 if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
1098 && S_ISLNK (st.st_mode))
1100 errno = ELOOP;
1101 return -1;
1105 fd = openat (chdir_fd, file_name, openflag, mode);
1106 if (0 <= fd)
1108 if (overwriting_old_files)
1110 struct stat st;
1111 if (fstat (fd, &st) != 0)
1113 int e = errno;
1114 close (fd);
1115 errno = e;
1116 return -1;
1118 if (! S_ISREG (st.st_mode))
1120 close (fd);
1121 errno = EEXIST;
1122 return -1;
1124 *current_mode = st.st_mode;
1125 *current_mode_mask = ALL_MODE_BITS;
1127 else
1129 *current_mode = mode & ~ current_umask;
1130 *current_mode_mask = MODE_RWX;
1134 return fd;
1137 static int
1138 extract_file (char *file_name, int typeflag)
1140 int fd;
1141 off_t size;
1142 union block *data_block;
1143 int status;
1144 size_t count;
1145 size_t written;
1146 bool interdir_made = false;
1147 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1148 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1149 mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
1150 : 0;
1151 mode_t current_mode = 0;
1152 mode_t current_mode_mask = 0;
1154 if (to_stdout_option)
1155 fd = STDOUT_FILENO;
1156 else if (to_command_option)
1158 fd = sys_exec_command (file_name, 'f', &current_stat_info);
1159 if (fd < 0)
1161 skip_member ();
1162 return 0;
1165 else
1167 int file_created = 0;
1168 if (set_xattr (file_name, &current_stat_info, invert_permissions,
1169 typeflag, &file_created))
1170 return 1;
1172 while ((fd = open_output_file (file_name, typeflag, mode,
1173 file_created, &current_mode,
1174 &current_mode_mask))
1175 < 0)
1177 int recover = maybe_recoverable (file_name, true, &interdir_made);
1178 if (recover != RECOVER_OK)
1180 skip_member ();
1181 if (recover == RECOVER_SKIP)
1182 return 0;
1183 open_error (file_name);
1184 return 1;
1189 mv_begin_read (&current_stat_info);
1190 if (current_stat_info.is_sparse)
1191 sparse_extract_file (fd, &current_stat_info, &size);
1192 else
1193 for (size = current_stat_info.stat.st_size; size > 0; )
1195 mv_size_left (size);
1197 /* Locate data, determine max length writeable, write it,
1198 block that we have used the data, then check if the write
1199 worked. */
1201 data_block = find_next_block ();
1202 if (! data_block)
1204 ERROR ((0, 0, _("Unexpected EOF in archive")));
1205 break; /* FIXME: What happens, then? */
1208 written = available_space_after (data_block);
1210 if (written > size)
1211 written = size;
1212 errno = 0;
1213 count = blocking_write (fd, data_block->buffer, written);
1214 size -= written;
1216 set_next_block_after ((union block *)
1217 (data_block->buffer + written - 1));
1218 if (count != written)
1220 if (!to_command_option)
1221 write_error_details (file_name, count, written);
1222 /* FIXME: shouldn't we restore from backup? */
1223 break;
1227 skip_file (size);
1229 mv_end ();
1231 /* If writing to stdout, don't try to do anything to the filename;
1232 it doesn't exist, or we don't want to touch it anyway. */
1234 if (to_stdout_option)
1235 return 0;
1237 if (! to_command_option)
1238 set_stat (file_name, &current_stat_info, fd,
1239 current_mode, current_mode_mask, typeflag, false,
1240 (old_files_option == OVERWRITE_OLD_FILES
1241 ? 0 : AT_SYMLINK_NOFOLLOW));
1243 status = close (fd);
1244 if (status < 0)
1245 close_error (file_name);
1247 if (to_command_option)
1248 sys_wait_command ();
1250 return status;
1253 /* Create a placeholder file with name FILE_NAME, which will be
1254 replaced after other extraction is done by a symbolic link if
1255 IS_SYMLINK is true, and by a hard link otherwise. Set
1256 *INTERDIR_MADE if an intermediate directory is made in the
1257 process. */
1259 static int
1260 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
1262 int fd;
1263 struct stat st;
1265 while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1267 switch (maybe_recoverable (file_name, false, interdir_made))
1269 case RECOVER_OK:
1270 continue;
1272 case RECOVER_SKIP:
1273 return 0;
1275 case RECOVER_NO:
1276 open_error (file_name);
1277 return -1;
1281 if (fstat (fd, &st) != 0)
1283 stat_error (file_name);
1284 close (fd);
1286 else if (close (fd) != 0)
1287 close_error (file_name);
1288 else
1290 struct delayed_set_stat *h;
1291 struct delayed_link *p =
1292 xmalloc (offsetof (struct delayed_link, target)
1293 + strlen (current_stat_info.link_name)
1294 + 1);
1295 p->next = delayed_link_head;
1296 delayed_link_head = p;
1297 p->dev = st.st_dev;
1298 p->ino = st.st_ino;
1299 p->birthtime = get_stat_birthtime (&st);
1300 p->is_symlink = is_symlink;
1301 if (is_symlink)
1303 p->mode = current_stat_info.stat.st_mode;
1304 p->uid = current_stat_info.stat.st_uid;
1305 p->gid = current_stat_info.stat.st_gid;
1306 p->atime = current_stat_info.atime;
1307 p->mtime = current_stat_info.mtime;
1309 p->change_dir = chdir_current;
1310 p->sources = xmalloc (offsetof (struct string_list, string)
1311 + strlen (file_name) + 1);
1312 p->sources->next = 0;
1313 strcpy (p->sources->string, file_name);
1314 p->cntx_name = NULL;
1315 assign_string (&p->cntx_name, current_stat_info.cntx_name);
1316 p->acls_a_ptr = NULL;
1317 p->acls_a_len = 0;
1318 p->acls_d_ptr = NULL;
1319 p->acls_d_len = 0;
1320 xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
1321 strcpy (p->target, current_stat_info.link_name);
1323 if ((h = find_direct_ancestor (file_name)) != NULL)
1324 mark_after_links (h);
1326 return 0;
1329 return -1;
1332 static int
1333 extract_link (char *file_name, int typeflag)
1335 bool interdir_made = false;
1336 char const *link_name;
1337 int rc;
1339 link_name = current_stat_info.link_name;
1341 if (! absolute_names_option && contains_dot_dot (link_name))
1342 return create_placeholder_file (file_name, false, &interdir_made);
1346 struct stat st1, st2;
1347 int e;
1348 int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1349 e = errno;
1351 if (status == 0)
1353 struct delayed_link *ds = delayed_link_head;
1354 if (ds
1355 && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1356 for (; ds; ds = ds->next)
1357 if (ds->change_dir == chdir_current
1358 && ds->dev == st1.st_dev
1359 && ds->ino == st1.st_ino
1360 && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
1361 == 0))
1363 struct string_list *p = xmalloc (offsetof (struct string_list, string)
1364 + strlen (file_name) + 1);
1365 strcpy (p->string, file_name);
1366 p->next = ds->sources;
1367 ds->sources = p;
1368 break;
1370 return 0;
1372 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1373 || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1374 == 0)
1375 && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1376 == 0)
1377 && st1.st_dev == st2.st_dev
1378 && st1.st_ino == st2.st_ino))
1379 return 0;
1381 errno = e;
1383 while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1384 == RECOVER_OK);
1386 if (rc == RECOVER_SKIP)
1387 return 0;
1388 if (!(incremental_option && errno == EEXIST))
1390 link_error (link_name, file_name);
1391 return 1;
1393 return 0;
1396 static int
1397 extract_symlink (char *file_name, int typeflag)
1399 #ifdef HAVE_SYMLINK
1400 bool interdir_made = false;
1402 if (! absolute_names_option
1403 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1404 || contains_dot_dot (current_stat_info.link_name)))
1405 return create_placeholder_file (file_name, true, &interdir_made);
1407 while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1408 switch (maybe_recoverable (file_name, false, &interdir_made))
1410 case RECOVER_OK:
1411 continue;
1413 case RECOVER_SKIP:
1414 return 0;
1416 case RECOVER_NO:
1417 symlink_error (current_stat_info.link_name, file_name);
1418 return -1;
1421 set_stat (file_name, &current_stat_info, -1, 0, 0,
1422 SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1423 return 0;
1425 #else
1426 static int warned_once;
1428 if (!warned_once)
1430 warned_once = 1;
1431 WARNOPT (WARN_SYMLINK_CAST,
1432 (0, 0,
1433 _("Attempting extraction of symbolic links as hard links")));
1435 return extract_link (file_name, typeflag);
1436 #endif
1439 #if S_IFCHR || S_IFBLK
1440 static int
1441 extract_node (char *file_name, int typeflag)
1443 bool interdir_made = false;
1444 mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1445 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1447 while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1448 != 0)
1449 switch (maybe_recoverable (file_name, false, &interdir_made))
1451 case RECOVER_OK:
1452 continue;
1454 case RECOVER_SKIP:
1455 return 0;
1457 case RECOVER_NO:
1458 mknod_error (file_name);
1459 return -1;
1462 set_stat (file_name, &current_stat_info, -1,
1463 mode & ~ current_umask, MODE_RWX,
1464 typeflag, false, AT_SYMLINK_NOFOLLOW);
1465 return 0;
1467 #endif
1469 #if HAVE_MKFIFO || defined mkfifo
1470 static int
1471 extract_fifo (char *file_name, int typeflag)
1473 bool interdir_made = false;
1474 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1475 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1477 while (mkfifoat (chdir_fd, file_name, mode) != 0)
1478 switch (maybe_recoverable (file_name, false, &interdir_made))
1480 case RECOVER_OK:
1481 continue;
1483 case RECOVER_SKIP:
1484 return 0;
1486 case RECOVER_NO:
1487 mkfifo_error (file_name);
1488 return -1;
1491 set_stat (file_name, &current_stat_info, -1,
1492 mode & ~ current_umask, MODE_RWX,
1493 typeflag, false, AT_SYMLINK_NOFOLLOW);
1494 return 0;
1496 #endif
1498 static int
1499 extract_volhdr (char *file_name, int typeflag)
1501 skip_member ();
1502 return 0;
1505 static int
1506 extract_failure (char *file_name, int typeflag)
1508 return 1;
1511 static int
1512 extract_skip (char *file_name, int typeflag)
1514 skip_member ();
1515 return 0;
1518 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1522 /* Prepare to extract a file. Find extractor function.
1523 Return zero if extraction should not proceed. */
1525 static int
1526 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1528 int rc = 1;
1530 if (EXTRACT_OVER_PIPE)
1531 rc = 0;
1533 /* Select the extractor */
1534 switch (typeflag)
1536 case GNUTYPE_SPARSE:
1537 *fun = extract_file;
1538 rc = 1;
1539 break;
1541 case AREGTYPE:
1542 case REGTYPE:
1543 case CONTTYPE:
1544 /* Appears to be a file. But BSD tar uses the convention that a slash
1545 suffix means a directory. */
1546 if (current_stat_info.had_trailing_slash)
1547 *fun = extract_dir;
1548 else
1550 *fun = extract_file;
1551 rc = 1;
1553 break;
1555 case SYMTYPE:
1556 *fun = extract_symlink;
1557 break;
1559 case LNKTYPE:
1560 *fun = extract_link;
1561 break;
1563 #if S_IFCHR
1564 case CHRTYPE:
1565 current_stat_info.stat.st_mode |= S_IFCHR;
1566 *fun = extract_node;
1567 break;
1568 #endif
1570 #if S_IFBLK
1571 case BLKTYPE:
1572 current_stat_info.stat.st_mode |= S_IFBLK;
1573 *fun = extract_node;
1574 break;
1575 #endif
1577 #if HAVE_MKFIFO || defined mkfifo
1578 case FIFOTYPE:
1579 *fun = extract_fifo;
1580 break;
1581 #endif
1583 case DIRTYPE:
1584 case GNUTYPE_DUMPDIR:
1585 *fun = extract_dir;
1586 if (current_stat_info.is_dumpdir)
1587 delay_directory_restore_option = true;
1588 break;
1590 case GNUTYPE_VOLHDR:
1591 *fun = extract_volhdr;
1592 break;
1594 case GNUTYPE_MULTIVOL:
1595 ERROR ((0, 0,
1596 _("%s: Cannot extract -- file is continued from another volume"),
1597 quotearg_colon (current_stat_info.file_name)));
1598 *fun = extract_skip;
1599 break;
1601 case GNUTYPE_LONGNAME:
1602 case GNUTYPE_LONGLINK:
1603 ERROR ((0, 0, _("Unexpected long name header")));
1604 *fun = extract_failure;
1605 break;
1607 default:
1608 WARNOPT (WARN_UNKNOWN_CAST,
1609 (0, 0,
1610 _("%s: Unknown file type '%c', extracted as normal file"),
1611 quotearg_colon (file_name), typeflag));
1612 *fun = extract_file;
1615 /* Determine whether the extraction should proceed */
1616 if (rc == 0)
1617 return 0;
1619 switch (old_files_option)
1621 case UNLINK_FIRST_OLD_FILES:
1622 if (!remove_any_file (file_name,
1623 recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1624 : ORDINARY_REMOVE_OPTION)
1625 && errno && errno != ENOENT)
1627 unlink_error (file_name);
1628 return 0;
1630 break;
1632 case KEEP_NEWER_FILES:
1633 if (file_newer_p (file_name, 0, &current_stat_info))
1635 WARNOPT (WARN_IGNORE_NEWER,
1636 (0, 0, _("Current %s is newer or same age"),
1637 quote (file_name)));
1638 return 0;
1640 break;
1642 default:
1643 break;
1646 return 1;
1649 /* Extract a file from the archive. */
1650 void
1651 extract_archive (void)
1653 char typeflag;
1654 tar_extractor_t fun;
1655 bool skip_dotdot_name;
1657 fatal_exit_hook = extract_finish;
1659 set_next_block_after (current_header);
1661 skip_dotdot_name = (!absolute_names_option
1662 && contains_dot_dot (current_stat_info.orig_file_name));
1663 if (skip_dotdot_name)
1664 ERROR ((0, 0, _("%s: Member name contains '..'"),
1665 quotearg_colon (current_stat_info.orig_file_name)));
1667 if (!current_stat_info.file_name[0]
1668 || skip_dotdot_name
1669 || (interactive_option
1670 && !confirm ("extract", current_stat_info.file_name)))
1672 skip_member ();
1673 return;
1676 /* Print the block from current_header and current_stat. */
1677 if (verbose_option)
1678 print_header (&current_stat_info, current_header, -1);
1680 /* Restore stats for all non-ancestor directories, unless
1681 it is an incremental archive.
1682 (see NOTICE in the comment to delay_set_stat above) */
1683 if (!delay_directory_restore_option)
1685 int dir = chdir_current;
1686 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1687 chdir_do (dir);
1690 /* Take a safety backup of a previously existing file. */
1692 if (backup_option)
1693 if (!maybe_backup_file (current_stat_info.file_name, 0))
1695 int e = errno;
1696 ERROR ((0, e, _("%s: Was unable to backup this file"),
1697 quotearg_colon (current_stat_info.file_name)));
1698 skip_member ();
1699 return;
1702 /* Extract the archive entry according to its type. */
1703 /* KLUDGE */
1704 typeflag = sparse_member_p (&current_stat_info) ?
1705 GNUTYPE_SPARSE : current_header->header.typeflag;
1707 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1709 if (fun && (*fun) (current_stat_info.file_name, typeflag)
1710 && backup_option)
1711 undo_last_backup ();
1713 else
1714 skip_member ();
1718 /* Extract the links whose final extraction were delayed. */
1719 static void
1720 apply_delayed_links (void)
1722 struct delayed_link *ds;
1724 for (ds = delayed_link_head; ds; )
1726 struct string_list *sources = ds->sources;
1727 char const *valid_source = 0;
1729 chdir_do (ds->change_dir);
1731 for (sources = ds->sources; sources; sources = sources->next)
1733 char const *source = sources->string;
1734 struct stat st;
1736 /* Make sure the placeholder file is still there. If not,
1737 don't create a link, as the placeholder was probably
1738 removed by a later extraction. */
1739 if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1740 && st.st_dev == ds->dev
1741 && st.st_ino == ds->ino
1742 && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
1744 /* Unlink the placeholder, then create a hard link if possible,
1745 a symbolic link otherwise. */
1746 if (unlinkat (chdir_fd, source, 0) != 0)
1747 unlink_error (source);
1748 else if (valid_source
1749 && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1750 == 0))
1752 else if (!ds->is_symlink)
1754 if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1755 link_error (ds->target, source);
1757 else if (symlinkat (ds->target, chdir_fd, source) != 0)
1758 symlink_error (ds->target, source);
1759 else
1761 struct tar_stat_info st1;
1762 st1.stat.st_mode = ds->mode;
1763 st1.stat.st_uid = ds->uid;
1764 st1.stat.st_gid = ds->gid;
1765 st1.atime = ds->atime;
1766 st1.mtime = ds->mtime;
1767 st1.cntx_name = ds->cntx_name;
1768 st1.acls_a_ptr = ds->acls_a_ptr;
1769 st1.acls_a_len = ds->acls_a_len;
1770 st1.acls_d_ptr = ds->acls_d_ptr;
1771 st1.acls_d_len = ds->acls_d_len;
1772 st1.xattr_map = ds->xattr_map;
1773 st1.xattr_map_size = ds->xattr_map_size;
1774 set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1775 false, AT_SYMLINK_NOFOLLOW);
1776 valid_source = source;
1781 for (sources = ds->sources; sources; )
1783 struct string_list *next = sources->next;
1784 free (sources);
1785 sources = next;
1788 xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
1789 free (ds->cntx_name);
1792 struct delayed_link *next = ds->next;
1793 free (ds);
1794 ds = next;
1798 delayed_link_head = 0;
1801 /* Finish the extraction of an archive. */
1802 void
1803 extract_finish (void)
1805 /* First, fix the status of ordinary directories that need fixing. */
1806 apply_nonancestor_delayed_set_stat ("", 0);
1808 /* Then, apply delayed links, so that they don't affect delayed
1809 directory status-setting for ordinary directories. */
1810 apply_delayed_links ();
1812 /* Finally, fix the status of directories that are ancestors
1813 of delayed links. */
1814 apply_nonancestor_delayed_set_stat ("", 1);
1817 bool
1818 rename_directory (char *src, char *dst)
1820 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1821 fixup_delayed_set_stat (src, dst);
1822 else
1824 int e = errno;
1825 bool interdir_made;
1827 switch (e)
1829 case ENOENT:
1830 if (make_directories (dst, &interdir_made) == 0)
1832 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1833 return true;
1834 e = errno;
1836 break;
1838 case EXDEV:
1839 /* FIXME: Fall back to recursive copying */
1841 default:
1842 break;
1845 ERROR ((0, e, _("Cannot rename %s to %s"),
1846 quote_n (0, src),
1847 quote_n (1, dst)));
1848 return false;
1850 return true;