Implement the "wait" checkpoint action.
[tar.git] / src / extract.c
blob74987bbaab4a1f20bb5badadeec8eebf46b71ba7
1 /* Extract files from a tar archive.
3 Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2014,
4 2016-2017 Free Software Foundation, Inc.
6 This file is part of GNU tar.
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.
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.
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/>.
21 Written by John Gilmore, on 1985-11-19. */
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>
30 #include "common.h"
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) */
36 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
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
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)
50 return ! (err == ENOSYS
51 || err == ENOTSUP
52 || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
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. */
64 struct delayed_set_stat
66 /* Next directory in list. */
67 struct delayed_set_stat *next;
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;
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;
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;
91 /* Whether symbolic links should be followed when accessing the
92 directory. */
93 int atflag;
95 /* Do not set the status of this directory until after delayed
96 links are created. */
97 bool after_links;
99 /* Directory that the name is relative to. */
100 int change_dir;
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;
115 static struct delayed_set_stat *delayed_set_stat_head;
117 /* List of links whose creation we have delayed. */
118 struct delayed_link
120 /* The next delayed link in the list. */
121 struct delayed_link *next;
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;
133 /* True if the link is symbolic. */
134 bool is_symlink;
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;
143 /* The directory that the sources and target are relative to. */
144 int change_dir;
146 /* A list of sources for this link. The sources are all to be
147 hard-linked together. */
148 struct string_list *sources;
150 /* SELinux context */
151 char *cntx_name;
153 /* ACLs */
154 char *acls_a_ptr;
155 size_t acls_a_len;
156 char *acls_d_ptr;
157 size_t acls_d_len;
159 size_t xattr_map_size;
160 struct xattr_array *xattr_map;
162 /* The desired target of the desired link. */
163 char target[1];
166 static struct delayed_link *delayed_link_head;
168 struct string_list
170 struct string_list *next;
171 char string[1];
174 /* Set up to extract files. */
175 void
176 extr_init (void)
178 we_are_root = geteuid () == ROOT_UID;
179 same_permissions_option += we_are_root;
180 same_owner_option += we_are_root;
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. */
186 newdir_umask = umask (0);
187 if (0 < same_permissions_option)
188 current_umask = 0;
189 else
191 umask (newdir_umask); /* restore the kernel umask */
192 current_umask = newdir_umask;
196 /* Use fchmod if possible, fchmodat otherwise. */
197 static int
198 fd_chmod (int fd, char const *file, mode_t mode, int atflag)
200 if (0 <= fd)
202 int result = fchmod (fd, mode);
203 if (result == 0 || implemented (errno))
204 return result;
206 return fchmodat (chdir_fd, file, mode, atflag);
209 /* Use fchown if possible, fchownat otherwise. */
210 static int
211 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
213 if (0 <= fd)
215 int result = fchown (fd, uid, gid);
216 if (result == 0 || implemented (errno))
217 return result;
219 return fchownat (chdir_fd, file, uid, gid, atflag);
222 /* Use fstat if possible, fstatat otherwise. */
223 static int
224 fd_stat (int fd, char const *file, struct stat *st, int atflag)
226 return (0 <= fd
227 ? fstat (fd, st)
228 : fstatat (chdir_fd, file, st, atflag));
231 /* Set the mode for FILE_NAME to MODE.
232 MODE_MASK specifies the bits of MODE that we care about;
233 thus if MODE_MASK is zero, do nothing.
234 If FD is nonnegative, it is a file descriptor for the file.
235 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
236 the file's current mode, using the style of struct delayed_set_stat.
237 TYPEFLAG specifies the type of the file.
238 ATFLAG specifies the flag to use when statting the file. */
239 static void
240 set_mode (char const *file_name,
241 mode_t mode, mode_t mode_mask, int fd,
242 mode_t current_mode, mode_t current_mode_mask,
243 char typeflag, int atflag)
245 if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
247 if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
249 struct stat st;
250 if (fd_stat (fd, file_name, &st, atflag) != 0)
252 stat_error (file_name);
253 return;
255 current_mode = st.st_mode;
258 current_mode &= MODE_ALL;
259 mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
261 if (current_mode != mode)
263 int chmod_errno =
264 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
266 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
267 setuid-root files would otherwise be a backdoor. See
268 http://opensolaris.org/jive/thread.jspa?threadID=95826
269 (2009-09-03). */
270 if (chmod_errno == EPERM && (mode & S_ISUID)
271 && priv_set_restore_linkdir () == 0)
273 chmod_errno =
274 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
275 priv_set_remove_linkdir ();
278 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
279 returns ENOTSUP even when operating on non-symlinks, try
280 again with the flag disabled if it does not appear to be
281 supported and if the file is not a symlink. This
282 introduces a race, alas. */
283 if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
284 chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
286 if (chmod_errno
287 && (typeflag != SYMTYPE || implemented (chmod_errno)))
289 errno = chmod_errno;
290 chmod_error_details (file_name, mode);
296 /* Check time after successfully setting FILE_NAME's time stamp to T. */
297 static void
298 check_time (char const *file_name, struct timespec t)
300 if (t.tv_sec < 0)
301 WARNOPT (WARN_TIMESTAMP,
302 (0, 0, _("%s: implausibly old time stamp %s"),
303 file_name, tartime (t, true)));
304 else if (timespec_cmp (volume_start_time, t) < 0)
306 struct timespec now;
307 gettime (&now);
308 if (timespec_cmp (now, t) < 0)
310 char buf[TIMESPEC_STRSIZE_BOUND];
311 struct timespec diff;
312 diff.tv_sec = t.tv_sec - now.tv_sec;
313 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
314 if (diff.tv_nsec < 0)
316 diff.tv_nsec += BILLION;
317 diff.tv_sec--;
319 WARNOPT (WARN_TIMESTAMP,
320 (0, 0, _("%s: time stamp %s is %s s in the future"),
321 file_name, tartime (t, true), code_timespec (diff, buf)));
326 /* Restore stat attributes (owner, group, mode and times) for
327 FILE_NAME, using information given in *ST.
328 If FD is nonnegative, it is a file descriptor for the file.
329 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
330 the file's current mode, using the style of struct delayed_set_stat.
331 TYPEFLAG specifies the type of the file.
332 If INTERDIR, this is an intermediate directory.
333 ATFLAG specifies the flag to use when statting the file. */
335 static void
336 set_stat (char const *file_name,
337 struct tar_stat_info const *st,
338 int fd, mode_t current_mode, mode_t current_mode_mask,
339 char typeflag, bool interdir, int atflag)
341 /* Do the utime before the chmod because some versions of utime are
342 broken and trash the modes of the file. */
344 if (! touch_option && ! interdir)
346 struct timespec ts[2];
347 if (incremental_option)
348 ts[0] = st->atime;
349 else
350 ts[0].tv_nsec = UTIME_OMIT;
351 ts[1] = st->mtime;
353 if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
355 if (incremental_option)
356 check_time (file_name, ts[0]);
357 check_time (file_name, ts[1]);
359 else if (typeflag != SYMTYPE || implemented (errno))
360 utime_error (file_name);
363 if (0 < same_owner_option && ! interdir)
365 /* Some systems allow non-root users to give files away. Once this
366 done, it is not possible anymore to change file permissions.
367 However, setting file permissions now would be incorrect, since
368 they would apply to the wrong user, and there would be a race
369 condition. So, don't use systems that allow non-root users to
370 give files away. */
371 uid_t uid = st->stat.st_uid;
372 gid_t gid = st->stat.st_gid;
374 if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
376 /* Changing the owner can clear st_mode bits in some cases. */
377 if ((current_mode | ~ current_mode_mask) & S_IXUGO)
378 current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
380 else if (typeflag != SYMTYPE || implemented (errno))
381 chown_error_details (file_name, uid, gid);
384 set_mode (file_name,
385 st->stat.st_mode & ~ current_umask,
386 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
387 fd, current_mode, current_mode_mask, typeflag, atflag);
389 /* these three calls must be done *after* fd_chown() call because fd_chown
390 causes that linux capabilities becomes cleared. */
391 xattrs_xattrs_set (st, file_name, typeflag, 1);
392 xattrs_acls_set (st, file_name, typeflag);
393 xattrs_selinux_set (st, file_name, typeflag);
396 /* Find the direct ancestor of FILE_NAME in the delayed_set_stat list.
398 static struct delayed_set_stat *
399 find_direct_ancestor (char const *file_name)
401 struct delayed_set_stat *h = delayed_set_stat_head;
402 while (h)
404 if (h && ! h->after_links
405 && strncmp (file_name, h->file_name, h->file_name_len) == 0
406 && ISSLASH (file_name[h->file_name_len])
407 && (last_component (file_name) == file_name + h->file_name_len + 1))
408 break;
409 h = h->next;
411 return h;
414 /* For each entry H in the leading prefix of entries in HEAD that do
415 not have after_links marked, mark H and fill in its dev and ino
416 members. Assume HEAD && ! HEAD->after_links. */
417 static void
418 mark_after_links (struct delayed_set_stat *head)
420 struct delayed_set_stat *h = head;
424 struct stat st;
425 h->after_links = 1;
427 if (deref_stat (h->file_name, &st) != 0)
428 stat_error (h->file_name);
429 else
431 h->dev = st.st_dev;
432 h->ino = st.st_ino;
435 while ((h = h->next) && ! h->after_links);
438 /* Remember to restore stat attributes (owner, group, mode and times)
439 for the directory FILE_NAME, using information given in *ST,
440 once we stop extracting files into that directory.
442 If ST is null, merely create a placeholder node for an intermediate
443 directory that was created by make_directories.
445 NOTICE: this works only if the archive has usual member order, i.e.
446 directory, then the files in that directory. Incremental archive have
447 somewhat reversed order: first go subdirectories, then all other
448 members. To help cope with this case the variable
449 delay_directory_restore_option is set by prepare_to_extract.
451 If an archive was explicitely created so that its member order is
452 reversed, some directory timestamps can be restored incorrectly,
453 e.g.:
454 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
456 static void
457 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
458 mode_t current_mode, mode_t current_mode_mask,
459 mode_t mode, int atflag)
461 size_t file_name_len = strlen (file_name);
462 struct delayed_set_stat *data = xmalloc (sizeof (*data));
463 data->next = delayed_set_stat_head;
464 data->mode = mode;
465 if (st)
467 data->dev = st->stat.st_dev;
468 data->ino = st->stat.st_ino;
469 data->uid = st->stat.st_uid;
470 data->gid = st->stat.st_gid;
471 data->atime = st->atime;
472 data->mtime = st->mtime;
474 data->file_name_len = file_name_len;
475 data->file_name = xstrdup (file_name);
476 data->current_mode = current_mode;
477 data->current_mode_mask = current_mode_mask;
478 data->interdir = ! st;
479 data->atflag = atflag;
480 data->after_links = 0;
481 data->change_dir = chdir_current;
482 data->cntx_name = NULL;
483 if (st)
484 assign_string (&data->cntx_name, st->cntx_name);
485 if (st && st->acls_a_ptr)
487 data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
488 data->acls_a_len = st->acls_a_len;
490 else
492 data->acls_a_ptr = NULL;
493 data->acls_a_len = 0;
495 if (st && st->acls_d_ptr)
497 data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
498 data->acls_d_len = st->acls_d_len;
500 else
502 data->acls_d_ptr = NULL;
503 data->acls_d_len = 0;
505 if (st)
506 xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
507 else
509 data->xattr_map = NULL;
510 data->xattr_map_size = 0;
512 strcpy (data->file_name, file_name);
513 delayed_set_stat_head = data;
514 if (must_be_dot_or_slash (file_name))
515 mark_after_links (data);
518 /* Update the delayed_set_stat info for an intermediate directory
519 created within the file name of DIR. The intermediate directory turned
520 out to be the same as this directory, e.g. due to ".." or symbolic
521 links. *DIR_STAT_INFO is the status of the directory. */
522 static void
523 repair_delayed_set_stat (char const *dir,
524 struct stat const *dir_stat_info)
526 struct delayed_set_stat *data;
527 for (data = delayed_set_stat_head; data; data = data->next)
529 struct stat st;
530 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
532 stat_error (data->file_name);
533 return;
536 if (st.st_dev == dir_stat_info->st_dev
537 && st.st_ino == dir_stat_info->st_ino)
539 data->dev = current_stat_info.stat.st_dev;
540 data->ino = current_stat_info.stat.st_ino;
541 data->mode = current_stat_info.stat.st_mode;
542 data->uid = current_stat_info.stat.st_uid;
543 data->gid = current_stat_info.stat.st_gid;
544 data->atime = current_stat_info.atime;
545 data->mtime = current_stat_info.mtime;
546 data->current_mode = st.st_mode;
547 data->current_mode_mask = ALL_MODE_BITS;
548 data->interdir = false;
549 return;
553 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
554 quotearg_colon (dir)));
557 static void
558 free_delayed_set_stat (struct delayed_set_stat *data)
560 free (data->file_name);
561 xheader_xattr_free (data->xattr_map, data->xattr_map_size);
562 free (data->cntx_name);
563 free (data->acls_a_ptr);
564 free (data->acls_d_ptr);
565 free (data);
568 void
569 remove_delayed_set_stat (const char *fname)
571 struct delayed_set_stat *data, *next, *prev = NULL;
572 for (data = delayed_set_stat_head; data; data = next)
574 next = data->next;
575 if (chdir_current == data->change_dir
576 && strcmp (data->file_name, fname) == 0)
578 free_delayed_set_stat (data);
579 if (prev)
580 prev->next = next;
581 else
582 delayed_set_stat_head = next;
583 return;
585 else
586 prev = data;
590 static void
591 fixup_delayed_set_stat (char const *src, char const *dst)
593 struct delayed_set_stat *data;
594 for (data = delayed_set_stat_head; data; data = data->next)
596 if (chdir_current == data->change_dir
597 && strcmp (data->file_name, src) == 0)
599 free (data->file_name);
600 data->file_name = xstrdup (dst);
601 data->file_name_len = strlen (dst);
602 return;
607 /* After a file/link/directory creation has failed, see if
608 it's because some required directory was not present, and if so,
609 create all required directories. Return zero if all the required
610 directories were created, nonzero (issuing a diagnostic) otherwise.
611 Set *INTERDIR_MADE if at least one directory was created. */
612 static int
613 make_directories (char *file_name, bool *interdir_made)
615 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
616 char *cursor; /* points into the file name */
618 for (cursor = cursor0; *cursor; cursor++)
620 mode_t mode;
621 mode_t desired_mode;
622 int status;
624 if (! ISSLASH (*cursor))
625 continue;
627 /* Avoid mkdir of empty string, if leading or double '/'. */
629 if (cursor == cursor0 || ISSLASH (cursor[-1]))
630 continue;
632 /* Avoid mkdir where last part of file name is "." or "..". */
634 if (cursor[-1] == '.'
635 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
636 || (cursor[-2] == '.'
637 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
638 continue;
640 *cursor = '\0'; /* truncate the name there */
641 desired_mode = MODE_RWX & ~ newdir_umask;
642 mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
643 status = mkdirat (chdir_fd, file_name, mode);
645 if (status == 0)
647 /* Create a struct delayed_set_stat even if
648 mode == desired_mode, because
649 repair_delayed_set_stat may need to update the struct. */
650 delay_set_stat (file_name,
651 0, mode & ~ current_umask, MODE_RWX,
652 desired_mode, AT_SYMLINK_NOFOLLOW);
654 print_for_mkdir (file_name, cursor - file_name, desired_mode);
655 *interdir_made = true;
657 else if (errno == EEXIST)
658 status = 0;
659 else
661 /* Check whether the desired file exists. Even when the
662 file exists, mkdir can fail with some errno value E other
663 than EEXIST, so long as E describes an error condition
664 that also applies. */
665 int e = errno;
666 struct stat st;
667 status = fstatat (chdir_fd, file_name, &st, 0);
668 if (status)
670 errno = e;
671 mkdir_error (file_name);
675 *cursor = '/';
676 if (status)
677 return status;
680 return 0;
683 /* Return true if FILE_NAME (with status *STP, if STP) is not a
684 directory, and has a time stamp newer than (or equal to) that of
685 TAR_STAT. */
686 static bool
687 file_newer_p (const char *file_name, struct stat const *stp,
688 struct tar_stat_info *tar_stat)
690 struct stat st;
692 if (!stp)
694 if (deref_stat (file_name, &st) != 0)
696 if (errno != ENOENT)
698 stat_warn (file_name);
699 /* Be safer: if the file exists, assume it is newer. */
700 return true;
702 return false;
704 stp = &st;
707 return (! S_ISDIR (stp->st_mode)
708 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
711 #define RECOVER_NO 0
712 #define RECOVER_OK 1
713 #define RECOVER_SKIP 2
715 /* Attempt repairing what went wrong with the extraction. Delete an
716 already existing file or create missing intermediate directories.
717 Return RECOVER_OK if we somewhat increased our chances at a successful
718 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
719 caller should skip extraction of that member. The value of errno is
720 properly restored on returning RECOVER_NO.
722 If REGULAR, the caller was trying to extract onto a regular file.
724 Set *INTERDIR_MADE if an intermediate directory is made as part of
725 the recovery process. */
727 static int
728 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
730 int e = errno;
731 struct stat st;
732 struct stat const *stp = 0;
734 if (*interdir_made)
735 return RECOVER_NO;
737 switch (e)
739 case ELOOP:
741 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
742 but some operating systems do not conform to the standard. */
743 #ifdef EFTYPE
744 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
745 case EFTYPE:
746 #endif
747 /* FreeBSD 8.1 uses errno == EMLINK. */
748 case EMLINK:
749 /* Tru64 5.1B uses errno == ENOTSUP. */
750 case ENOTSUP:
752 if (! regular
753 || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
754 break;
755 if (strchr (file_name, '/'))
757 if (deref_stat (file_name, &st) != 0)
758 break;
759 stp = &st;
761 /* The caller tried to open a symbolic link with O_NOFOLLOW.
762 Fall through, treating it as an already-existing file. */
763 FALLTHROUGH;
764 case EEXIST:
765 /* Remove an old file, if the options allow this. */
767 switch (old_files_option)
769 case SKIP_OLD_FILES:
770 WARNOPT (WARN_EXISTING_FILE,
771 (0, 0, _("%s: skipping existing file"), file_name));
772 return RECOVER_SKIP;
774 case KEEP_OLD_FILES:
775 return RECOVER_NO;
777 case KEEP_NEWER_FILES:
778 if (file_newer_p (file_name, stp, &current_stat_info))
779 break;
780 FALLTHROUGH;
781 case DEFAULT_OLD_FILES:
782 case NO_OVERWRITE_DIR_OLD_FILES:
783 case OVERWRITE_OLD_FILES:
784 if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
785 return RECOVER_OK;
786 break;
788 case UNLINK_FIRST_OLD_FILES:
789 break;
791 abort (); /* notreached */
793 case ENOENT:
794 /* Attempt creating missing intermediate directories. */
795 if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
796 return RECOVER_OK;
797 break;
799 default:
800 /* Just say we can't do anything about it... */
801 break;
804 errno = e;
805 return RECOVER_NO;
808 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
809 given in *ST. Restore before extraction because they may affect file layout
810 (e.g. on Lustre distributed parallel filesystem - setting info about how many
811 servers is this file striped over, stripe size, mirror copies, etc.
812 in advance dramatically improves the following performance of reading and
813 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
814 bits from the file's current permissions. TYPEFLAG specifies the type of the
815 file. Returns non-zero when error occurs (while un-available xattrs is not
816 an error, rather no-op). Non-zero FILE_CREATED indicates set_xattr has
817 created the file. */
818 static int
819 set_xattr (char const *file_name, struct tar_stat_info const *st,
820 mode_t invert_permissions, char typeflag, int *file_created)
822 #ifdef HAVE_XATTRS
823 bool interdir_made = false;
825 if ((xattrs_option > 0) && st->xattr_map_size)
827 mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
829 for (;;)
831 if (!mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0))
833 /* Successfully created file */
834 xattrs_xattrs_set (st, file_name, typeflag, 0);
835 *file_created = 1;
836 return 0;
839 switch (maybe_recoverable ((char *)file_name, false, &interdir_made))
841 case RECOVER_OK:
842 continue;
843 case RECOVER_NO:
844 skip_member ();
845 open_error (file_name);
846 return 1;
847 case RECOVER_SKIP:
848 return 0;
852 #endif
854 return 0;
857 /* Fix the statuses of all directories whose statuses need fixing, and
858 which are not ancestors of FILE_NAME. If AFTER_LINKS is
859 nonzero, do this for all such directories; otherwise, stop at the
860 first directory that is marked to be fixed up only after delayed
861 links are applied. */
862 static void
863 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
865 size_t file_name_len = strlen (file_name);
866 bool check_for_renamed_directories = 0;
868 while (delayed_set_stat_head)
870 struct delayed_set_stat *data = delayed_set_stat_head;
871 bool skip_this_one = 0;
872 struct stat st;
873 mode_t current_mode = data->current_mode;
874 mode_t current_mode_mask = data->current_mode_mask;
876 check_for_renamed_directories |= data->after_links;
878 if (after_links < data->after_links
879 || (data->file_name_len < file_name_len
880 && file_name[data->file_name_len]
881 && (ISSLASH (file_name[data->file_name_len])
882 || ISSLASH (file_name[data->file_name_len - 1]))
883 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
884 break;
886 chdir_do (data->change_dir);
888 if (check_for_renamed_directories)
890 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
892 stat_error (data->file_name);
893 skip_this_one = 1;
895 else
897 current_mode = st.st_mode;
898 current_mode_mask = ALL_MODE_BITS;
899 if (! (st.st_dev == data->dev && st.st_ino == data->ino))
901 ERROR ((0, 0,
902 _("%s: Directory renamed before its status could be extracted"),
903 quotearg_colon (data->file_name)));
904 skip_this_one = 1;
909 if (! skip_this_one)
911 struct tar_stat_info sb;
912 sb.stat.st_mode = data->mode;
913 sb.stat.st_uid = data->uid;
914 sb.stat.st_gid = data->gid;
915 sb.atime = data->atime;
916 sb.mtime = data->mtime;
917 sb.cntx_name = data->cntx_name;
918 sb.acls_a_ptr = data->acls_a_ptr;
919 sb.acls_a_len = data->acls_a_len;
920 sb.acls_d_ptr = data->acls_d_ptr;
921 sb.acls_d_len = data->acls_d_len;
922 sb.xattr_map = data->xattr_map;
923 sb.xattr_map_size = data->xattr_map_size;
924 set_stat (data->file_name, &sb,
925 -1, current_mode, current_mode_mask,
926 DIRTYPE, data->interdir, data->atflag);
929 delayed_set_stat_head = data->next;
930 free_delayed_set_stat (data);
935 static bool
936 is_directory_link (const char *file_name)
938 struct stat st;
939 int e = errno;
940 int res;
942 res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
943 S_ISLNK (st.st_mode) &&
944 fstatat (chdir_fd, file_name, &st, 0) == 0 &&
945 S_ISDIR (st.st_mode));
946 errno = e;
947 return res;
950 /* Extractor functions for various member types */
952 static int
953 extract_dir (char *file_name, int typeflag)
955 int status;
956 mode_t mode;
957 mode_t current_mode = 0;
958 mode_t current_mode_mask = 0;
959 int atflag = 0;
960 bool interdir_made = false;
962 /* Save 'root device' to avoid purging mount points. */
963 if (one_file_system_option && root_device == 0)
965 struct stat st;
967 if (fstatat (chdir_fd, ".", &st, 0) != 0)
968 stat_diag (".");
969 else
970 root_device = st.st_dev;
973 if (incremental_option)
974 /* Read the entry and delete files that aren't listed in the archive. */
975 purge_directory (file_name);
976 else if (typeflag == GNUTYPE_DUMPDIR)
977 skip_member ();
979 /* If ownership or permissions will be restored later, create the
980 directory with restrictive permissions at first, so that in the
981 meantime processes owned by other users do not inadvertently
982 create files under this directory that inherit the wrong owner,
983 group, or permissions from the directory. If not root, though,
984 make the directory writeable and searchable at first, so that
985 files can be created under it. */
986 mode = ((current_stat_info.stat.st_mode
987 & (0 < same_owner_option || 0 < same_permissions_option
988 ? S_IRWXU
989 : MODE_RWX))
990 | (we_are_root ? 0 : MODE_WXUSR));
992 for (;;)
994 status = mkdirat (chdir_fd, file_name, mode);
995 if (status == 0)
997 current_mode = mode & ~ current_umask;
998 current_mode_mask = MODE_RWX;
999 atflag = AT_SYMLINK_NOFOLLOW;
1000 break;
1003 if (errno == EEXIST
1004 && (interdir_made
1005 || keep_directory_symlink_option
1006 || old_files_option == DEFAULT_OLD_FILES
1007 || old_files_option == OVERWRITE_OLD_FILES))
1009 struct stat st;
1011 if (keep_directory_symlink_option && is_directory_link (file_name))
1012 return 0;
1014 if (deref_stat (file_name, &st) == 0)
1016 current_mode = st.st_mode;
1017 current_mode_mask = ALL_MODE_BITS;
1019 if (S_ISDIR (current_mode))
1021 if (interdir_made)
1023 repair_delayed_set_stat (file_name, &st);
1024 return 0;
1026 break;
1029 errno = EEXIST;
1032 switch (maybe_recoverable (file_name, false, &interdir_made))
1034 case RECOVER_OK:
1035 continue;
1037 case RECOVER_SKIP:
1038 break;
1040 case RECOVER_NO:
1041 if (errno != EEXIST)
1043 mkdir_error (file_name);
1044 return 1;
1046 break;
1048 break;
1051 if (status == 0
1052 || old_files_option == DEFAULT_OLD_FILES
1053 || old_files_option == OVERWRITE_OLD_FILES)
1054 delay_set_stat (file_name, &current_stat_info,
1055 current_mode, current_mode_mask,
1056 current_stat_info.stat.st_mode, atflag);
1057 return status;
1062 static int
1063 open_output_file (char const *file_name, int typeflag, mode_t mode,
1064 int file_created, mode_t *current_mode,
1065 mode_t *current_mode_mask)
1067 int fd;
1068 bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
1069 int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
1070 | O_CREAT
1071 | (overwriting_old_files
1072 ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
1073 : O_EXCL));
1075 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
1076 if (file_created)
1077 openflag = openflag & ~O_EXCL;
1079 if (typeflag == CONTTYPE)
1081 static int conttype_diagnosed;
1083 if (!conttype_diagnosed)
1085 conttype_diagnosed = 1;
1086 WARNOPT (WARN_CONTIGUOUS_CAST,
1087 (0, 0, _("Extracting contiguous files as regular files")));
1091 /* If O_NOFOLLOW is needed but does not work, check for a symlink
1092 separately. There's a race condition, but that cannot be avoided
1093 on hosts lacking O_NOFOLLOW. */
1094 if (! HAVE_WORKING_O_NOFOLLOW
1095 && overwriting_old_files && ! dereference_option)
1097 struct stat st;
1098 if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
1099 && S_ISLNK (st.st_mode))
1101 errno = ELOOP;
1102 return -1;
1106 fd = openat (chdir_fd, file_name, openflag, mode);
1107 if (0 <= fd)
1109 if (overwriting_old_files)
1111 struct stat st;
1112 if (fstat (fd, &st) != 0)
1114 int e = errno;
1115 close (fd);
1116 errno = e;
1117 return -1;
1119 if (! S_ISREG (st.st_mode))
1121 close (fd);
1122 errno = EEXIST;
1123 return -1;
1125 *current_mode = st.st_mode;
1126 *current_mode_mask = ALL_MODE_BITS;
1128 else
1130 *current_mode = mode & ~ current_umask;
1131 *current_mode_mask = MODE_RWX;
1135 return fd;
1138 static int
1139 extract_file (char *file_name, int typeflag)
1141 int fd;
1142 off_t size;
1143 union block *data_block;
1144 int status;
1145 size_t count;
1146 size_t written;
1147 bool interdir_made = false;
1148 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1149 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1150 mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
1151 : 0;
1152 mode_t current_mode = 0;
1153 mode_t current_mode_mask = 0;
1155 if (to_stdout_option)
1156 fd = STDOUT_FILENO;
1157 else if (to_command_option)
1159 fd = sys_exec_command (file_name, 'f', &current_stat_info);
1160 if (fd < 0)
1162 skip_member ();
1163 return 0;
1166 else
1168 int file_created = 0;
1169 if (set_xattr (file_name, &current_stat_info, invert_permissions,
1170 typeflag, &file_created))
1171 return 1;
1173 while ((fd = open_output_file (file_name, typeflag, mode,
1174 file_created, &current_mode,
1175 &current_mode_mask))
1176 < 0)
1178 int recover = maybe_recoverable (file_name, true, &interdir_made);
1179 if (recover != RECOVER_OK)
1181 skip_member ();
1182 if (recover == RECOVER_SKIP)
1183 return 0;
1184 open_error (file_name);
1185 return 1;
1190 mv_begin_read (&current_stat_info);
1191 if (current_stat_info.is_sparse)
1192 sparse_extract_file (fd, &current_stat_info, &size);
1193 else
1194 for (size = current_stat_info.stat.st_size; size > 0; )
1196 mv_size_left (size);
1198 /* Locate data, determine max length writeable, write it,
1199 block that we have used the data, then check if the write
1200 worked. */
1202 data_block = find_next_block ();
1203 if (! data_block)
1205 ERROR ((0, 0, _("Unexpected EOF in archive")));
1206 break; /* FIXME: What happens, then? */
1209 written = available_space_after (data_block);
1211 if (written > size)
1212 written = size;
1213 errno = 0;
1214 count = blocking_write (fd, data_block->buffer, written);
1215 size -= written;
1217 set_next_block_after ((union block *)
1218 (data_block->buffer + written - 1));
1219 if (count != written)
1221 if (!to_command_option)
1222 write_error_details (file_name, count, written);
1223 /* FIXME: shouldn't we restore from backup? */
1224 break;
1228 skip_file (size);
1230 mv_end ();
1232 /* If writing to stdout, don't try to do anything to the filename;
1233 it doesn't exist, or we don't want to touch it anyway. */
1235 if (to_stdout_option)
1236 return 0;
1238 if (! to_command_option)
1239 set_stat (file_name, &current_stat_info, fd,
1240 current_mode, current_mode_mask, typeflag, false,
1241 (old_files_option == OVERWRITE_OLD_FILES
1242 ? 0 : AT_SYMLINK_NOFOLLOW));
1244 status = close (fd);
1245 if (status < 0)
1246 close_error (file_name);
1248 if (to_command_option)
1249 sys_wait_command ();
1251 return status;
1254 /* Create a placeholder file with name FILE_NAME, which will be
1255 replaced after other extraction is done by a symbolic link if
1256 IS_SYMLINK is true, and by a hard link otherwise. Set
1257 *INTERDIR_MADE if an intermediate directory is made in the
1258 process. */
1260 static int
1261 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
1263 int fd;
1264 struct stat st;
1266 while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1268 switch (maybe_recoverable (file_name, false, interdir_made))
1270 case RECOVER_OK:
1271 continue;
1273 case RECOVER_SKIP:
1274 return 0;
1276 case RECOVER_NO:
1277 open_error (file_name);
1278 return -1;
1282 if (fstat (fd, &st) != 0)
1284 stat_error (file_name);
1285 close (fd);
1287 else if (close (fd) != 0)
1288 close_error (file_name);
1289 else
1291 struct delayed_set_stat *h;
1292 struct delayed_link *p =
1293 xmalloc (offsetof (struct delayed_link, target)
1294 + strlen (current_stat_info.link_name)
1295 + 1);
1296 p->next = delayed_link_head;
1297 delayed_link_head = p;
1298 p->dev = st.st_dev;
1299 p->ino = st.st_ino;
1300 p->birthtime = get_stat_birthtime (&st);
1301 p->is_symlink = is_symlink;
1302 if (is_symlink)
1304 p->mode = current_stat_info.stat.st_mode;
1305 p->uid = current_stat_info.stat.st_uid;
1306 p->gid = current_stat_info.stat.st_gid;
1307 p->atime = current_stat_info.atime;
1308 p->mtime = current_stat_info.mtime;
1310 p->change_dir = chdir_current;
1311 p->sources = xmalloc (offsetof (struct string_list, string)
1312 + strlen (file_name) + 1);
1313 p->sources->next = 0;
1314 strcpy (p->sources->string, file_name);
1315 p->cntx_name = NULL;
1316 assign_string (&p->cntx_name, current_stat_info.cntx_name);
1317 p->acls_a_ptr = NULL;
1318 p->acls_a_len = 0;
1319 p->acls_d_ptr = NULL;
1320 p->acls_d_len = 0;
1321 xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
1322 strcpy (p->target, current_stat_info.link_name);
1324 if ((h = find_direct_ancestor (file_name)) != NULL)
1325 mark_after_links (h);
1327 return 0;
1330 return -1;
1333 static int
1334 extract_link (char *file_name, int typeflag)
1336 bool interdir_made = false;
1337 char const *link_name;
1338 int rc;
1340 link_name = current_stat_info.link_name;
1342 if (! absolute_names_option && contains_dot_dot (link_name))
1343 return create_placeholder_file (file_name, false, &interdir_made);
1347 struct stat st1, st2;
1348 int e;
1349 int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1350 e = errno;
1352 if (status == 0)
1354 struct delayed_link *ds = delayed_link_head;
1355 if (ds
1356 && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1357 for (; ds; ds = ds->next)
1358 if (ds->change_dir == chdir_current
1359 && ds->dev == st1.st_dev
1360 && ds->ino == st1.st_ino
1361 && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
1362 == 0))
1364 struct string_list *p = xmalloc (offsetof (struct string_list, string)
1365 + strlen (file_name) + 1);
1366 strcpy (p->string, file_name);
1367 p->next = ds->sources;
1368 ds->sources = p;
1369 break;
1371 return 0;
1373 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1374 || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1375 == 0)
1376 && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1377 == 0)
1378 && st1.st_dev == st2.st_dev
1379 && st1.st_ino == st2.st_ino))
1380 return 0;
1382 errno = e;
1384 while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1385 == RECOVER_OK);
1387 if (rc == RECOVER_SKIP)
1388 return 0;
1389 if (!(incremental_option && errno == EEXIST))
1391 link_error (link_name, file_name);
1392 return 1;
1394 return 0;
1397 static int
1398 extract_symlink (char *file_name, int typeflag)
1400 #ifdef HAVE_SYMLINK
1401 bool interdir_made = false;
1403 if (! absolute_names_option
1404 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1405 || contains_dot_dot (current_stat_info.link_name)))
1406 return create_placeholder_file (file_name, true, &interdir_made);
1408 while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1409 switch (maybe_recoverable (file_name, false, &interdir_made))
1411 case RECOVER_OK:
1412 continue;
1414 case RECOVER_SKIP:
1415 return 0;
1417 case RECOVER_NO:
1418 symlink_error (current_stat_info.link_name, file_name);
1419 return -1;
1422 set_stat (file_name, &current_stat_info, -1, 0, 0,
1423 SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1424 return 0;
1426 #else
1427 static int warned_once;
1429 if (!warned_once)
1431 warned_once = 1;
1432 WARNOPT (WARN_SYMLINK_CAST,
1433 (0, 0,
1434 _("Attempting extraction of symbolic links as hard links")));
1436 return extract_link (file_name, typeflag);
1437 #endif
1440 #if S_IFCHR || S_IFBLK
1441 static int
1442 extract_node (char *file_name, int typeflag)
1444 bool interdir_made = false;
1445 mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1446 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1448 while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1449 != 0)
1450 switch (maybe_recoverable (file_name, false, &interdir_made))
1452 case RECOVER_OK:
1453 continue;
1455 case RECOVER_SKIP:
1456 return 0;
1458 case RECOVER_NO:
1459 mknod_error (file_name);
1460 return -1;
1463 set_stat (file_name, &current_stat_info, -1,
1464 mode & ~ current_umask, MODE_RWX,
1465 typeflag, false, AT_SYMLINK_NOFOLLOW);
1466 return 0;
1468 #endif
1470 #if HAVE_MKFIFO || defined mkfifo
1471 static int
1472 extract_fifo (char *file_name, int typeflag)
1474 bool interdir_made = false;
1475 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1476 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1478 while (mkfifoat (chdir_fd, file_name, mode) != 0)
1479 switch (maybe_recoverable (file_name, false, &interdir_made))
1481 case RECOVER_OK:
1482 continue;
1484 case RECOVER_SKIP:
1485 return 0;
1487 case RECOVER_NO:
1488 mkfifo_error (file_name);
1489 return -1;
1492 set_stat (file_name, &current_stat_info, -1,
1493 mode & ~ current_umask, MODE_RWX,
1494 typeflag, false, AT_SYMLINK_NOFOLLOW);
1495 return 0;
1497 #endif
1499 static int
1500 extract_volhdr (char *file_name, int typeflag)
1502 skip_member ();
1503 return 0;
1506 static int
1507 extract_failure (char *file_name, int typeflag)
1509 return 1;
1512 static int
1513 extract_skip (char *file_name, int typeflag)
1515 skip_member ();
1516 return 0;
1519 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1523 /* Prepare to extract a file. Find extractor function.
1524 Return zero if extraction should not proceed. */
1526 static int
1527 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1529 int rc = 1;
1531 if (EXTRACT_OVER_PIPE)
1532 rc = 0;
1534 /* Select the extractor */
1535 switch (typeflag)
1537 case GNUTYPE_SPARSE:
1538 *fun = extract_file;
1539 rc = 1;
1540 break;
1542 case AREGTYPE:
1543 case REGTYPE:
1544 case CONTTYPE:
1545 /* Appears to be a file. But BSD tar uses the convention that a slash
1546 suffix means a directory. */
1547 if (current_stat_info.had_trailing_slash)
1548 *fun = extract_dir;
1549 else
1551 *fun = extract_file;
1552 rc = 1;
1554 break;
1556 case SYMTYPE:
1557 *fun = extract_symlink;
1558 break;
1560 case LNKTYPE:
1561 *fun = extract_link;
1562 break;
1564 #if S_IFCHR
1565 case CHRTYPE:
1566 current_stat_info.stat.st_mode |= S_IFCHR;
1567 *fun = extract_node;
1568 break;
1569 #endif
1571 #if S_IFBLK
1572 case BLKTYPE:
1573 current_stat_info.stat.st_mode |= S_IFBLK;
1574 *fun = extract_node;
1575 break;
1576 #endif
1578 #if HAVE_MKFIFO || defined mkfifo
1579 case FIFOTYPE:
1580 *fun = extract_fifo;
1581 break;
1582 #endif
1584 case DIRTYPE:
1585 case GNUTYPE_DUMPDIR:
1586 *fun = extract_dir;
1587 if (current_stat_info.is_dumpdir)
1588 delay_directory_restore_option = true;
1589 break;
1591 case GNUTYPE_VOLHDR:
1592 *fun = extract_volhdr;
1593 break;
1595 case GNUTYPE_MULTIVOL:
1596 ERROR ((0, 0,
1597 _("%s: Cannot extract -- file is continued from another volume"),
1598 quotearg_colon (current_stat_info.file_name)));
1599 *fun = extract_skip;
1600 break;
1602 case GNUTYPE_LONGNAME:
1603 case GNUTYPE_LONGLINK:
1604 ERROR ((0, 0, _("Unexpected long name header")));
1605 *fun = extract_failure;
1606 break;
1608 default:
1609 WARNOPT (WARN_UNKNOWN_CAST,
1610 (0, 0,
1611 _("%s: Unknown file type '%c', extracted as normal file"),
1612 quotearg_colon (file_name), typeflag));
1613 *fun = extract_file;
1616 /* Determine whether the extraction should proceed */
1617 if (rc == 0)
1618 return 0;
1620 switch (old_files_option)
1622 case UNLINK_FIRST_OLD_FILES:
1623 if (!remove_any_file (file_name,
1624 recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1625 : ORDINARY_REMOVE_OPTION)
1626 && errno && errno != ENOENT)
1628 unlink_error (file_name);
1629 return 0;
1631 break;
1633 case KEEP_NEWER_FILES:
1634 if (file_newer_p (file_name, 0, &current_stat_info))
1636 WARNOPT (WARN_IGNORE_NEWER,
1637 (0, 0, _("Current %s is newer or same age"),
1638 quote (file_name)));
1639 return 0;
1641 break;
1643 default:
1644 break;
1647 return 1;
1650 /* Extract a file from the archive. */
1651 void
1652 extract_archive (void)
1654 char typeflag;
1655 tar_extractor_t fun;
1656 bool skip_dotdot_name;
1658 fatal_exit_hook = extract_finish;
1660 set_next_block_after (current_header);
1662 skip_dotdot_name = (!absolute_names_option
1663 && contains_dot_dot (current_stat_info.orig_file_name));
1664 if (skip_dotdot_name)
1665 ERROR ((0, 0, _("%s: Member name contains '..'"),
1666 quotearg_colon (current_stat_info.orig_file_name)));
1668 if (!current_stat_info.file_name[0]
1669 || skip_dotdot_name
1670 || (interactive_option
1671 && !confirm ("extract", current_stat_info.file_name)))
1673 skip_member ();
1674 return;
1677 /* Print the block from current_header and current_stat. */
1678 if (verbose_option)
1679 print_header (&current_stat_info, current_header, -1);
1681 /* Restore stats for all non-ancestor directories, unless
1682 it is an incremental archive.
1683 (see NOTICE in the comment to delay_set_stat above) */
1684 if (!delay_directory_restore_option)
1686 int dir = chdir_current;
1687 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1688 chdir_do (dir);
1691 /* Take a safety backup of a previously existing file. */
1693 if (backup_option)
1694 if (!maybe_backup_file (current_stat_info.file_name, 0))
1696 int e = errno;
1697 ERROR ((0, e, _("%s: Was unable to backup this file"),
1698 quotearg_colon (current_stat_info.file_name)));
1699 skip_member ();
1700 return;
1703 /* Extract the archive entry according to its type. */
1704 /* KLUDGE */
1705 typeflag = sparse_member_p (&current_stat_info) ?
1706 GNUTYPE_SPARSE : current_header->header.typeflag;
1708 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1710 if (fun && (*fun) (current_stat_info.file_name, typeflag)
1711 && backup_option)
1712 undo_last_backup ();
1714 else
1715 skip_member ();
1719 /* Extract the links whose final extraction were delayed. */
1720 static void
1721 apply_delayed_links (void)
1723 struct delayed_link *ds;
1725 for (ds = delayed_link_head; ds; )
1727 struct string_list *sources = ds->sources;
1728 char const *valid_source = 0;
1730 chdir_do (ds->change_dir);
1732 for (sources = ds->sources; sources; sources = sources->next)
1734 char const *source = sources->string;
1735 struct stat st;
1737 /* Make sure the placeholder file is still there. If not,
1738 don't create a link, as the placeholder was probably
1739 removed by a later extraction. */
1740 if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1741 && st.st_dev == ds->dev
1742 && st.st_ino == ds->ino
1743 && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
1745 /* Unlink the placeholder, then create a hard link if possible,
1746 a symbolic link otherwise. */
1747 if (unlinkat (chdir_fd, source, 0) != 0)
1748 unlink_error (source);
1749 else if (valid_source
1750 && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1751 == 0))
1753 else if (!ds->is_symlink)
1755 if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1756 link_error (ds->target, source);
1758 else if (symlinkat (ds->target, chdir_fd, source) != 0)
1759 symlink_error (ds->target, source);
1760 else
1762 struct tar_stat_info st1;
1763 st1.stat.st_mode = ds->mode;
1764 st1.stat.st_uid = ds->uid;
1765 st1.stat.st_gid = ds->gid;
1766 st1.atime = ds->atime;
1767 st1.mtime = ds->mtime;
1768 st1.cntx_name = ds->cntx_name;
1769 st1.acls_a_ptr = ds->acls_a_ptr;
1770 st1.acls_a_len = ds->acls_a_len;
1771 st1.acls_d_ptr = ds->acls_d_ptr;
1772 st1.acls_d_len = ds->acls_d_len;
1773 st1.xattr_map = ds->xattr_map;
1774 st1.xattr_map_size = ds->xattr_map_size;
1775 set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1776 false, AT_SYMLINK_NOFOLLOW);
1777 valid_source = source;
1782 for (sources = ds->sources; sources; )
1784 struct string_list *next = sources->next;
1785 free (sources);
1786 sources = next;
1789 xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
1790 free (ds->cntx_name);
1793 struct delayed_link *next = ds->next;
1794 free (ds);
1795 ds = next;
1799 delayed_link_head = 0;
1802 /* Finish the extraction of an archive. */
1803 void
1804 extract_finish (void)
1806 /* First, fix the status of ordinary directories that need fixing. */
1807 apply_nonancestor_delayed_set_stat ("", 0);
1809 /* Then, apply delayed links, so that they don't affect delayed
1810 directory status-setting for ordinary directories. */
1811 apply_delayed_links ();
1813 /* Finally, fix the status of directories that are ancestors
1814 of delayed links. */
1815 apply_nonancestor_delayed_set_stat ("", 1);
1818 bool
1819 rename_directory (char *src, char *dst)
1821 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1822 fixup_delayed_set_stat (src, dst);
1823 else
1825 int e = errno;
1826 bool interdir_made;
1828 switch (e)
1830 case ENOENT:
1831 if (make_directories (dst, &interdir_made) == 0)
1833 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1834 return true;
1835 e = errno;
1837 break;
1839 case EXDEV:
1840 /* FIXME: Fall back to recursive copying */
1842 default:
1843 break;
1846 ERROR ((0, e, _("Cannot rename %s to %s"),
1847 quote_n (0, src),
1848 quote_n (1, dst)));
1849 return false;
1851 return true;