Exclude VCS directory with writing from an archive
[tar.git] / src / extract.c
blobaec5de6990a3d15529c666e2c2df5daa4e0ce1a3
1 /* Extract files from a tar archive.
3 Copyright 1988-2023 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 <hash.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 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
46 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
47 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC \
48 || (defined _WIN32 && ! defined __CYGWIN__))
49 # define HAVE_BIRTHTIME 1
50 #else
51 # define HAVE_BIRTHTIME 0
52 #endif
54 #if HAVE_BIRTHTIME
55 # define BIRTHTIME_EQ(a, b) (timespec_cmp (a, b) == 0)
56 #else
57 # define BIRTHTIME_EQ(a, b) true
58 #endif
60 /* Return true if an error number ERR means the system call is
61 supported in this case. */
62 static bool
63 implemented (int err)
65 return ! (err == ENOSYS
66 || err == ENOTSUP
67 || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
70 /* List of directories whose statuses we need to extract after we've
71 finished extracting their subsidiary files. If you consider each
72 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
73 represents an element where AFTER_LINKS is nonzero and [^D]
74 represents an element where AFTER_LINKS is zero, then the head
75 of the subsequence has the longest name, and each non-head element
76 in the prefix is an ancestor (in the directory hierarchy) of the
77 preceding element. */
79 struct delayed_set_stat
81 /* Next directory in list. */
82 struct delayed_set_stat *next;
84 /* Metadata for this directory. */
85 dev_t dev;
86 ino_t ino;
87 mode_t mode; /* The desired mode is MODE & ~ current_umask. */
88 uid_t uid;
89 gid_t gid;
90 struct timespec atime;
91 struct timespec mtime;
93 /* An estimate of the directory's current mode, along with a mask
94 specifying which bits of this estimate are known to be correct.
95 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
96 matter. */
97 mode_t current_mode;
98 mode_t current_mode_mask;
100 /* This directory is an intermediate directory that was created
101 as an ancestor of some other directory; it was not mentioned
102 in the archive, so do not set its uid, gid, atime, or mtime,
103 and don't alter its mode outside of MODE_RWX. */
104 bool interdir;
106 /* Whether symbolic links should be followed when accessing the
107 directory. */
108 int atflag;
110 /* Do not set the status of this directory until after delayed
111 links are created. */
112 bool after_links;
114 /* Directory that the name is relative to. */
115 int change_dir;
117 /* extended attributes*/
118 char *cntx_name;
119 char *acls_a_ptr;
120 size_t acls_a_len;
121 char *acls_d_ptr;
122 size_t acls_d_len;
123 size_t xattr_map_size;
124 struct xattr_map xattr_map;
125 /* Length and contents of name. */
126 size_t file_name_len;
127 char *file_name;
130 static struct delayed_set_stat *delayed_set_stat_head;
132 /* A link whose creation we have delayed. */
133 struct delayed_link
135 /* The next in a list of delayed links that should be made after
136 this delayed link. */
137 struct delayed_link *next;
139 /* Whether this delayed link has a predecessor in the NEXT list. */
140 bool has_predecessor;
142 /* The device, inode number and birthtime of the placeholder.
143 birthtime.tv_nsec is negative if the birthtime is not available.
144 Don't use mtime as this would allow for false matches if some
145 other process removes the placeholder. Don't use ctime as
146 this would cause race conditions and other screwups, e.g.,
147 when restoring hard-linked symlinks. */
148 dev_t dev;
149 ino_t ino;
150 #if HAVE_BIRTHTIME
151 struct timespec birthtime;
152 #endif
154 /* True if the link is symbolic. */
155 bool is_symlink;
157 /* The desired metadata, valid only the link is symbolic. */
158 mode_t mode;
159 uid_t uid;
160 gid_t gid;
161 struct timespec atime;
162 struct timespec mtime;
164 /* The directory that the sources and target are relative to. */
165 int change_dir;
167 /* A list of sources for this link. The sources are all to be
168 hard-linked together. */
169 struct string_list *sources;
171 /* SELinux context */
172 char *cntx_name;
174 /* ACLs */
175 char *acls_a_ptr;
176 size_t acls_a_len;
177 char *acls_d_ptr;
178 size_t acls_d_len;
180 struct xattr_map xattr_map;
182 /* The desired target of the desired link. */
183 char target[1];
186 static Hash_table *delayed_link_table;
188 struct string_list
190 struct string_list *next;
191 char string[1];
194 static size_t
195 dl_hash (void const *entry, size_t table_size)
197 struct delayed_link const *dl = entry;
198 uintmax_t n = dl->dev;
199 int nshift = TYPE_WIDTH (n) - TYPE_WIDTH (dl->dev);
200 if (0 < nshift)
201 n <<= nshift;
202 n ^= dl->ino;
203 return n % table_size;
206 static bool
207 dl_compare (void const *a, void const *b)
209 struct delayed_link const *da = a, *db = b;
210 return (da->dev == db->dev) & (da->ino == db->ino);
213 /* Set up to extract files. */
214 void
215 extr_init (void)
217 we_are_root = geteuid () == ROOT_UID;
218 same_permissions_option += we_are_root;
219 same_owner_option += we_are_root;
221 /* Option -p clears the kernel umask, so it does not affect proper
222 restoration of file permissions. New intermediate directories will
223 comply with umask at start of program. */
225 newdir_umask = umask (0);
226 if (0 < same_permissions_option)
227 current_umask = 0;
228 else
230 umask (newdir_umask); /* restore the kernel umask */
231 current_umask = newdir_umask;
235 /* Use fchmod if possible, fchmodat otherwise. */
236 static int
237 fd_i_chmod (int fd, char const *file, mode_t mode, int atflag)
239 if (0 <= fd)
241 int result = fchmod (fd, mode);
242 if (result == 0 || implemented (errno))
243 return result;
245 return fchmodat (chdir_fd, file, mode, atflag);
248 /* A version of fd_i_chmod which gracefully handles several common error
249 conditions. Additional argument TYPEFLAG is the type of file in tar
250 notation.
252 static int
253 fd_chmod(int fd, char const *file_name, int mode, int atflag, int typeflag)
255 int chmod_errno = fd_i_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
257 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
258 setuid-root files would otherwise be a backdoor. See
259 http://opensolaris.org/jive/thread.jspa?threadID=95826
260 (2009-09-03). */
261 if (chmod_errno == EPERM && (mode & S_ISUID)
262 && priv_set_restore_linkdir () == 0)
264 chmod_errno = fd_i_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
265 priv_set_remove_linkdir ();
268 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
269 returns ENOTSUP even when operating on non-symlinks, try
270 again with the flag disabled if it does not appear to be
271 supported and if the file is not a symlink. This
272 introduces a race, alas. */
273 if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
274 chmod_errno = fd_i_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
276 if (chmod_errno && (typeflag != SYMTYPE || implemented (chmod_errno)))
278 errno = chmod_errno;
279 return -1;
281 return 0;
284 /* Use fchown if possible, fchownat otherwise. */
285 static int
286 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
288 if (0 <= fd)
290 int result = fchown (fd, uid, gid);
291 if (result == 0 || implemented (errno))
292 return result;
294 return fchownat (chdir_fd, file, uid, gid, atflag);
297 /* Use fstat if possible, fstatat otherwise. */
298 static int
299 fd_stat (int fd, char const *file, struct stat *st, int atflag)
301 return (0 <= fd
302 ? fstat (fd, st)
303 : fstatat (chdir_fd, file, st, atflag));
306 /* Set the mode for FILE_NAME to MODE.
307 MODE_MASK specifies the bits of MODE that we care about;
308 thus if MODE_MASK is zero, do nothing.
309 If FD is nonnegative, it is a file descriptor for the file.
310 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
311 the file's current mode, using the style of struct delayed_set_stat.
312 TYPEFLAG specifies the type of the file.
313 ATFLAG specifies the flag to use when statting the file. */
314 static void
315 set_mode (char const *file_name,
316 mode_t mode, mode_t mode_mask, int fd,
317 mode_t current_mode, mode_t current_mode_mask,
318 char typeflag, int atflag)
320 if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
322 if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
324 struct stat st;
325 if (fd_stat (fd, file_name, &st, atflag) != 0)
327 stat_error (file_name);
328 return;
330 current_mode = st.st_mode;
333 current_mode &= MODE_ALL;
334 mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
336 if (current_mode != mode)
338 if (fd_chmod (fd, file_name, mode, atflag, typeflag))
339 chmod_error_details (file_name, mode);
344 /* Check time after successfully setting FILE_NAME's time stamp to T. */
345 static void
346 check_time (char const *file_name, struct timespec t)
348 if (t.tv_sec < 0)
349 WARNOPT (WARN_TIMESTAMP,
350 (0, 0, _("%s: implausibly old time stamp %s"),
351 file_name, tartime (t, true)));
352 else if (timespec_cmp (volume_start_time, t) < 0)
354 struct timespec now;
355 gettime (&now);
356 if (timespec_cmp (now, t) < 0)
358 char buf[TIMESPEC_STRSIZE_BOUND];
359 struct timespec diff;
360 diff.tv_sec = t.tv_sec - now.tv_sec;
361 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
362 if (diff.tv_nsec < 0)
364 diff.tv_nsec += BILLION;
365 diff.tv_sec--;
367 WARNOPT (WARN_TIMESTAMP,
368 (0, 0, _("%s: time stamp %s is %s s in the future"),
369 file_name, tartime (t, true), code_timespec (diff, buf)));
374 /* Restore stat attributes (owner, group, mode and times) for
375 FILE_NAME, using information given in *ST.
376 If FD is nonnegative, it is a file descriptor for the file.
377 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
378 the file's current mode, using the style of struct delayed_set_stat.
379 TYPEFLAG specifies the type of the file.
380 If INTERDIR, this is an intermediate directory.
381 ATFLAG specifies the flag to use when statting the file. */
383 static void
384 set_stat (char const *file_name,
385 struct tar_stat_info const *st,
386 int fd, mode_t current_mode, mode_t current_mode_mask,
387 char typeflag, bool interdir, int atflag)
389 /* Do the utime before the chmod because some versions of utime are
390 broken and trash the modes of the file. */
392 if (! touch_option && ! interdir)
394 struct timespec ts[2];
395 if (incremental_option)
396 ts[0] = st->atime;
397 else
398 ts[0].tv_nsec = UTIME_OMIT;
399 ts[1] = st->mtime;
401 if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
403 if (incremental_option)
404 check_time (file_name, ts[0]);
405 check_time (file_name, ts[1]);
407 else if (typeflag != SYMTYPE || implemented (errno))
408 utime_error (file_name);
411 if (0 < same_owner_option && ! interdir)
413 /* Some systems allow non-root users to give files away. Once this
414 done, it is not possible anymore to change file permissions.
415 However, setting file permissions now would be incorrect, since
416 they would apply to the wrong user, and there would be a race
417 condition. So, don't use systems that allow non-root users to
418 give files away. */
419 uid_t uid = st->stat.st_uid;
420 gid_t gid = st->stat.st_gid;
422 if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
424 /* Changing the owner can clear st_mode bits in some cases. */
425 if ((current_mode | ~ current_mode_mask) & S_IXUGO)
426 current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
428 else if (typeflag != SYMTYPE || implemented (errno))
429 chown_error_details (file_name, uid, gid);
432 set_mode (file_name,
433 st->stat.st_mode & ~ current_umask,
434 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
435 fd, current_mode, current_mode_mask, typeflag, atflag);
437 /* these three calls must be done *after* fd_chown() call because fd_chown
438 causes that linux capabilities becomes cleared. */
439 xattrs_xattrs_set (st, file_name, typeflag, 1);
440 xattrs_acls_set (st, file_name, typeflag);
441 xattrs_selinux_set (st, file_name, typeflag);
444 /* Find the direct ancestor of FILE_NAME in the delayed_set_stat list.
446 static struct delayed_set_stat *
447 find_direct_ancestor (char const *file_name)
449 struct delayed_set_stat *h = delayed_set_stat_head;
450 while (h)
452 if (! h->after_links
453 && strncmp (file_name, h->file_name, h->file_name_len) == 0
454 && ISSLASH (file_name[h->file_name_len])
455 && (last_component (file_name) == file_name + h->file_name_len + 1))
456 break;
457 h = h->next;
459 return h;
462 /* For each entry H in the leading prefix of entries in HEAD that do
463 not have after_links marked, mark H and fill in its dev and ino
464 members. Assume HEAD && ! HEAD->after_links. */
465 static void
466 mark_after_links (struct delayed_set_stat *head)
468 struct delayed_set_stat *h = head;
472 struct stat st;
473 h->after_links = 1;
475 if (deref_stat (h->file_name, &st) != 0)
476 stat_error (h->file_name);
477 else
479 h->dev = st.st_dev;
480 h->ino = st.st_ino;
483 while ((h = h->next) && ! h->after_links);
486 /* Remember to restore stat attributes (owner, group, mode and times)
487 for the directory FILE_NAME, using information given in *ST,
488 once we stop extracting files into that directory.
490 If ST is null, merely create a placeholder node for an intermediate
491 directory that was created by make_directories.
493 NOTICE: this works only if the archive has usual member order, i.e.
494 directory, then the files in that directory. Incremental archive have
495 somewhat reversed order: first go subdirectories, then all other
496 members. To help cope with this case the variable
497 delay_directory_restore_option is set by prepare_to_extract.
499 If an archive was explicitly created so that its member order is
500 reversed, some directory timestamps can be restored incorrectly,
501 e.g.:
502 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
504 static void
505 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
506 mode_t current_mode, mode_t current_mode_mask,
507 mode_t mode, int atflag)
509 size_t file_name_len = strlen (file_name);
510 struct delayed_set_stat *data;
512 for (data = delayed_set_stat_head; data; data = data->next)
513 if (strcmp (data->file_name, file_name) == 0)
514 break;
516 if (data)
518 if (data->interdir)
520 struct stat real_st;
521 if (fstatat (chdir_fd, data->file_name,
522 &real_st, data->atflag) != 0)
524 stat_error (data->file_name);
526 else
528 data->dev = real_st.st_dev;
529 data->ino = real_st.st_ino;
533 else
535 data = xmalloc (sizeof (*data));
536 data->next = delayed_set_stat_head;
537 delayed_set_stat_head = data;
538 data->file_name_len = file_name_len;
539 data->file_name = xstrdup (file_name);
540 data->after_links = false;
541 if (st)
543 data->dev = st->stat.st_dev;
544 data->ino = st->stat.st_ino;
546 xattr_map_init (&data->xattr_map);
549 data->mode = mode;
550 if (st)
552 data->uid = st->stat.st_uid;
553 data->gid = st->stat.st_gid;
554 data->atime = st->atime;
555 data->mtime = st->mtime;
557 data->current_mode = current_mode;
558 data->current_mode_mask = current_mode_mask;
559 data->interdir = ! st;
560 data->atflag = atflag;
561 data->change_dir = chdir_current;
562 data->cntx_name = NULL;
563 if (st)
564 assign_string_or_null (&data->cntx_name, st->cntx_name);
565 if (st && st->acls_a_ptr)
567 data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
568 data->acls_a_len = st->acls_a_len;
570 else
572 data->acls_a_ptr = NULL;
573 data->acls_a_len = 0;
575 if (st && st->acls_d_ptr)
577 data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
578 data->acls_d_len = st->acls_d_len;
580 else
582 data->acls_d_ptr = NULL;
583 data->acls_d_len = 0;
585 if (st)
586 xattr_map_copy (&data->xattr_map, &st->xattr_map);
587 if (must_be_dot_or_slash (file_name))
588 mark_after_links (data);
591 /* Update the delayed_set_stat info for an intermediate directory
592 created within the file name of DIR. The intermediate directory turned
593 out to be the same as this directory, e.g. due to ".." or symbolic
594 links. *DIR_STAT_INFO is the status of the directory. */
595 static void
596 repair_delayed_set_stat (char const *dir,
597 struct stat const *dir_stat_info)
599 struct delayed_set_stat *data;
600 for (data = delayed_set_stat_head; data; data = data->next)
602 struct stat st;
603 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
605 stat_error (data->file_name);
606 return;
609 if (st.st_dev == dir_stat_info->st_dev
610 && st.st_ino == dir_stat_info->st_ino)
612 data->dev = current_stat_info.stat.st_dev;
613 data->ino = current_stat_info.stat.st_ino;
614 data->mode = current_stat_info.stat.st_mode;
615 data->uid = current_stat_info.stat.st_uid;
616 data->gid = current_stat_info.stat.st_gid;
617 data->atime = current_stat_info.atime;
618 data->mtime = current_stat_info.mtime;
619 data->current_mode = st.st_mode;
620 data->current_mode_mask = ALL_MODE_BITS;
621 data->interdir = false;
622 return;
626 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
627 quotearg_colon (dir)));
630 static void
631 free_delayed_set_stat (struct delayed_set_stat *data)
633 free (data->file_name);
634 xattr_map_free (&data->xattr_map);
635 free (data->cntx_name);
636 free (data->acls_a_ptr);
637 free (data->acls_d_ptr);
638 free (data);
641 void
642 remove_delayed_set_stat (const char *fname)
644 struct delayed_set_stat *data, *next, *prev = NULL;
645 for (data = delayed_set_stat_head; data; data = next)
647 next = data->next;
648 if (chdir_current == data->change_dir
649 && strcmp (data->file_name, fname) == 0)
651 free_delayed_set_stat (data);
652 if (prev)
653 prev->next = next;
654 else
655 delayed_set_stat_head = next;
656 return;
658 else
659 prev = data;
663 static void
664 fixup_delayed_set_stat (char const *src, char const *dst)
666 struct delayed_set_stat *data;
667 for (data = delayed_set_stat_head; data; data = data->next)
669 if (chdir_current == data->change_dir
670 && strcmp (data->file_name, src) == 0)
672 free (data->file_name);
673 data->file_name = xstrdup (dst);
674 data->file_name_len = strlen (dst);
675 return;
680 /* After a file/link/directory creation has failed due to ENOENT,
681 create all required directories. Return zero if all the required
682 directories were created, nonzero (issuing a diagnostic) otherwise.
683 Set *INTERDIR_MADE if at least one directory was created. */
684 static int
685 make_directories (char *file_name)
687 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
688 char *cursor; /* points into the file name */
689 char *parent_end = NULL;
690 int parent_errno;
692 for (cursor = cursor0; *cursor; cursor++)
694 mode_t mode;
695 mode_t desired_mode;
696 int status;
698 if (! ISSLASH (*cursor))
699 continue;
701 /* Avoid mkdir of empty string, if leading or double '/'. */
703 if (cursor == cursor0 || ISSLASH (cursor[-1]))
704 continue;
706 /* Avoid mkdir where last part of file name is "." or "..". */
708 if (cursor[-1] == '.'
709 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
710 || (cursor[-2] == '.'
711 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
712 continue;
714 *cursor = '\0'; /* truncate the name there */
715 desired_mode = MODE_RWX & ~ newdir_umask;
716 mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
717 status = mkdirat (chdir_fd, file_name, mode);
719 if (status == 0)
721 /* Create a struct delayed_set_stat even if
722 mode == desired_mode, because
723 repair_delayed_set_stat may need to update the struct. */
724 delay_set_stat (file_name,
725 0, mode & ~ current_umask, MODE_RWX,
726 desired_mode, AT_SYMLINK_NOFOLLOW);
728 print_for_mkdir (file_name, cursor - file_name, desired_mode);
729 parent_end = NULL;
731 else
732 switch (errno)
734 case ELOOP: case ENAMETOOLONG: case ENOENT: case ENOTDIR:
735 /* FILE_NAME doesn't exist and couldn't be created; fail now. */
736 mkdir_error (file_name);
737 *cursor = '/';
738 return status;
740 default:
741 /* FILE_NAME may be an existing directory so do not fail now.
742 Instead, arrange to check at loop exit, assuming this is
743 the last loop iteration. */
744 parent_end = cursor;
745 parent_errno = errno;
746 break;
749 *cursor = '/';
752 if (!parent_end)
753 return 0;
755 /* Although we did not create the parent directory, some other
756 process may have created it, so check whether it exists now. */
757 *parent_end = '\0';
758 struct stat st;
759 int stat_status = fstatat (chdir_fd, file_name, &st, 0);
760 if (!stat_status && !S_ISDIR (st.st_mode))
761 stat_status = -1;
762 if (stat_status)
764 errno = parent_errno;
765 mkdir_error (file_name);
767 *parent_end = '/';
769 return stat_status;
772 /* Return true if FILE_NAME (with status *STP, if STP) is not a
773 directory, and has a time stamp newer than (or equal to) that of
774 TAR_STAT. */
775 static bool
776 file_newer_p (const char *file_name, struct stat const *stp,
777 struct tar_stat_info *tar_stat)
779 struct stat st;
781 if (!stp)
783 if (deref_stat (file_name, &st) != 0)
785 if (errno != ENOENT)
787 stat_warn (file_name);
788 /* Be safer: if the file exists, assume it is newer. */
789 return true;
791 return false;
793 stp = &st;
796 return (! S_ISDIR (stp->st_mode)
797 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
800 #define RECOVER_NO 0
801 #define RECOVER_OK 1
802 #define RECOVER_SKIP 2
804 /* Attempt repairing what went wrong with the extraction. Delete an
805 already existing file or create missing intermediate directories.
806 Return RECOVER_OK if we somewhat increased our chances at a successful
807 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
808 caller should skip extraction of that member. The value of errno is
809 properly restored on returning RECOVER_NO.
811 If REGULAR, the caller was trying to extract onto a regular file.
813 Set *INTERDIR_MADE if an intermediate directory is made as part of
814 the recovery process. */
816 static int
817 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
819 int e = errno;
820 struct stat st;
821 struct stat const *stp = 0;
823 if (*interdir_made)
824 return RECOVER_NO;
826 switch (e)
828 case ELOOP:
830 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
831 but some operating systems do not conform to the standard. */
832 #ifdef EFTYPE
833 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
834 case EFTYPE:
835 #endif
836 /* FreeBSD 8.1 uses errno == EMLINK. */
837 case EMLINK:
838 /* Tru64 5.1B uses errno == ENOTSUP. */
839 case ENOTSUP:
841 if (! regular
842 || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
843 break;
844 if (strchr (file_name, '/'))
846 if (deref_stat (file_name, &st) != 0)
847 break;
848 stp = &st;
850 /* The caller tried to open a symbolic link with O_NOFOLLOW.
851 Fall through, treating it as an already-existing file. */
852 FALLTHROUGH;
853 case EEXIST:
854 /* Remove an old file, if the options allow this. */
856 switch (old_files_option)
858 case SKIP_OLD_FILES:
859 WARNOPT (WARN_EXISTING_FILE,
860 (0, 0, _("%s: skipping existing file"), file_name));
861 return RECOVER_SKIP;
863 case KEEP_OLD_FILES:
864 return RECOVER_NO;
866 case KEEP_NEWER_FILES:
867 if (file_newer_p (file_name, stp, &current_stat_info))
868 break;
869 FALLTHROUGH;
870 case DEFAULT_OLD_FILES:
871 case NO_OVERWRITE_DIR_OLD_FILES:
872 case OVERWRITE_OLD_FILES:
873 if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
874 return RECOVER_OK;
875 break;
877 case UNLINK_FIRST_OLD_FILES:
878 break;
880 FALLTHROUGH;
882 case ENOENT:
883 /* Attempt creating missing intermediate directories. */
884 if (make_directories (file_name) == 0)
886 *interdir_made = true;
887 return RECOVER_OK;
889 break;
891 default:
892 /* Just say we can't do anything about it... */
893 break;
896 errno = e;
897 return RECOVER_NO;
900 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
901 given in *ST. Restore before extraction because they may affect file layout
902 (e.g. on Lustre distributed parallel filesystem - setting info about how many
903 servers is this file striped over, stripe size, mirror copies, etc.
904 in advance dramatically improves the following performance of reading and
905 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
906 bits from the file's current permissions. TYPEFLAG specifies the type of the
907 file. Return a negative number (setting errno) on failure, zero if
908 successful but FILE_NAME was not created (e.g., xattrs not
909 available), and a positive number if FILE_NAME was created. */
910 static int
911 set_xattr (char const *file_name, struct tar_stat_info const *st,
912 mode_t mode, char typeflag)
914 #ifdef HAVE_XATTRS
915 if ((xattrs_option > 0) && st->xattr_map.xm_size)
917 int r = mknodat (chdir_fd, file_name, mode, 0);
918 if (r < 0)
919 return r;
920 xattrs_xattrs_set (st, file_name, typeflag, 0);
921 return 1;
923 #endif
925 return 0;
928 /* Fix the statuses of all directories whose statuses need fixing, and
929 which are not ancestors of FILE_NAME. If AFTER_LINKS is
930 nonzero, do this for all such directories; otherwise, stop at the
931 first directory that is marked to be fixed up only after delayed
932 links are applied. */
933 static void
934 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
936 size_t file_name_len = strlen (file_name);
937 bool check_for_renamed_directories = 0;
939 while (delayed_set_stat_head)
941 struct delayed_set_stat *data = delayed_set_stat_head;
942 bool skip_this_one = 0;
943 struct stat st;
944 mode_t current_mode = data->current_mode;
945 mode_t current_mode_mask = data->current_mode_mask;
947 check_for_renamed_directories |= data->after_links;
949 if (after_links < data->after_links
950 || (data->file_name_len < file_name_len
951 && file_name[data->file_name_len]
952 && (ISSLASH (file_name[data->file_name_len])
953 || ISSLASH (file_name[data->file_name_len - 1]))
954 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
955 break;
957 chdir_do (data->change_dir);
959 if (check_for_renamed_directories)
961 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
963 stat_error (data->file_name);
964 skip_this_one = 1;
966 else
968 current_mode = st.st_mode;
969 current_mode_mask = ALL_MODE_BITS;
970 if (! (st.st_dev == data->dev && st.st_ino == data->ino))
972 ERROR ((0, 0,
973 _("%s: Directory renamed before its status could be extracted"),
974 quotearg_colon (data->file_name)));
975 skip_this_one = 1;
980 if (! skip_this_one)
982 struct tar_stat_info sb;
983 sb.stat.st_mode = data->mode;
984 sb.stat.st_uid = data->uid;
985 sb.stat.st_gid = data->gid;
986 sb.atime = data->atime;
987 sb.mtime = data->mtime;
988 sb.cntx_name = data->cntx_name;
989 sb.acls_a_ptr = data->acls_a_ptr;
990 sb.acls_a_len = data->acls_a_len;
991 sb.acls_d_ptr = data->acls_d_ptr;
992 sb.acls_d_len = data->acls_d_len;
993 sb.xattr_map = data->xattr_map;
994 set_stat (data->file_name, &sb,
995 -1, current_mode, current_mode_mask,
996 DIRTYPE, data->interdir, data->atflag);
999 delayed_set_stat_head = data->next;
1000 free_delayed_set_stat (data);
1005 static bool
1006 is_directory_link (char const *file_name, struct stat *st)
1008 char buf[1];
1009 return (0 <= readlinkat (chdir_fd, file_name, buf, sizeof buf)
1010 && fstatat (chdir_fd, file_name, st, 0) == 0
1011 && S_ISDIR (st->st_mode));
1014 /* Given struct stat of a directory (or directory member) whose ownership
1015 or permissions of will be restored later, return the temporary permissions
1016 for that directory, sufficiently restrictive so that in the meantime
1017 processes owned by other users do not inadvertently create files under this
1018 directory that inherit the wrong owner, group, or permissions from the
1019 directory.
1021 If not root, though, make the directory writeable and searchable at first,
1022 so that files can be created under it.
1024 static int
1025 safe_dir_mode (struct stat const *st)
1027 return ((st->st_mode
1028 & (0 < same_owner_option || 0 < same_permissions_option
1029 ? S_IRWXU
1030 : MODE_RWX))
1031 | (we_are_root ? 0 : MODE_WXUSR));
1034 /* Extractor functions for various member types */
1036 static int
1037 extract_dir (char *file_name, int typeflag)
1039 int status;
1040 mode_t mode;
1041 mode_t current_mode = 0;
1042 mode_t current_mode_mask = 0;
1043 int atflag = 0;
1044 bool interdir_made = false;
1046 /* Save 'root device' to avoid purging mount points. */
1047 if (one_file_system_option && root_device == 0)
1049 struct stat st;
1051 if (fstatat (chdir_fd, ".", &st, 0) != 0)
1052 stat_diag (".");
1053 else
1054 root_device = st.st_dev;
1057 if (incremental_option)
1058 /* Read the entry and delete files that aren't listed in the archive. */
1059 purge_directory (file_name);
1060 else if (typeflag == GNUTYPE_DUMPDIR)
1061 skip_member ();
1063 mode = safe_dir_mode (&current_stat_info.stat);
1065 for (;;)
1067 status = mkdirat (chdir_fd, file_name, mode);
1068 if (status == 0)
1070 current_mode = mode & ~ current_umask;
1071 current_mode_mask = MODE_RWX;
1072 atflag = AT_SYMLINK_NOFOLLOW;
1073 break;
1076 if (errno == EEXIST)
1078 if (interdir_made
1079 || keep_directory_symlink_option
1080 || old_files_option == NO_OVERWRITE_DIR_OLD_FILES
1081 || old_files_option == DEFAULT_OLD_FILES
1082 || old_files_option == OVERWRITE_OLD_FILES)
1084 struct stat st;
1085 st.st_mode = 0;
1087 if (keep_directory_symlink_option
1088 && is_directory_link (file_name, &st))
1089 return 0;
1091 if ((st.st_mode != 0 && fstatat_flags == 0)
1092 || deref_stat (file_name, &st) == 0)
1094 current_mode = st.st_mode;
1095 current_mode_mask = ALL_MODE_BITS;
1097 if (S_ISDIR (current_mode))
1099 if (interdir_made)
1101 repair_delayed_set_stat (file_name, &st);
1102 return 0;
1104 else if (old_files_option == NO_OVERWRITE_DIR_OLD_FILES)
1106 /* Temporarily change the directory mode to a safe
1107 value, to be able to create files in it, should
1108 the need be.
1110 mode = safe_dir_mode (&st);
1111 status = fd_chmod (-1, file_name, mode,
1112 AT_SYMLINK_NOFOLLOW, DIRTYPE);
1113 if (status == 0)
1115 /* Store the actual directory mode, to be restored
1116 later.
1118 current_stat_info.stat = st;
1119 current_mode = mode & ~ current_umask;
1120 current_mode_mask = MODE_RWX;
1121 atflag = AT_SYMLINK_NOFOLLOW;
1122 break;
1124 else
1126 chmod_error_details (file_name, mode);
1129 break;
1133 else if (old_files_option == UNLINK_FIRST_OLD_FILES)
1135 status = 0;
1136 break;
1139 errno = EEXIST;
1142 switch (maybe_recoverable (file_name, false, &interdir_made))
1144 case RECOVER_OK:
1145 continue;
1147 case RECOVER_SKIP:
1148 break;
1150 case RECOVER_NO:
1151 if (errno != EEXIST)
1153 mkdir_error (file_name);
1154 return 1;
1156 break;
1158 break;
1161 if (status == 0
1162 || old_files_option == DEFAULT_OLD_FILES
1163 || old_files_option == OVERWRITE_OLD_FILES)
1164 delay_set_stat (file_name, &current_stat_info,
1165 current_mode, current_mode_mask,
1166 current_stat_info.stat.st_mode, atflag);
1167 return status;
1172 static int
1173 open_output_file (char const *file_name, int typeflag, mode_t mode,
1174 int file_created, mode_t *current_mode,
1175 mode_t *current_mode_mask)
1177 int fd;
1178 bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
1179 int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
1180 | (file_created
1181 ? O_NOFOLLOW
1182 : (O_CREAT
1183 | (overwriting_old_files
1184 ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
1185 : O_EXCL))));
1187 if (typeflag == CONTTYPE)
1189 static int conttype_diagnosed;
1191 if (!conttype_diagnosed)
1193 conttype_diagnosed = 1;
1194 WARNOPT (WARN_CONTIGUOUS_CAST,
1195 (0, 0, _("Extracting contiguous files as regular files")));
1199 /* If O_NOFOLLOW is needed but does not work, check for a symlink
1200 separately. There's a race condition, but that cannot be avoided
1201 on hosts lacking O_NOFOLLOW. */
1202 if (! HAVE_WORKING_O_NOFOLLOW
1203 && overwriting_old_files && ! dereference_option)
1205 char buf[1];
1206 if (0 <= readlinkat (chdir_fd, file_name, buf, sizeof buf))
1208 errno = ELOOP;
1209 return -1;
1213 fd = openat (chdir_fd, file_name, openflag, mode);
1214 if (0 <= fd)
1216 if (openflag & O_EXCL)
1218 *current_mode = mode & ~ current_umask;
1219 *current_mode_mask = MODE_RWX;
1221 else
1223 struct stat st;
1224 if (fstat (fd, &st) != 0)
1226 int e = errno;
1227 close (fd);
1228 errno = e;
1229 return -1;
1231 if (! S_ISREG (st.st_mode))
1233 close (fd);
1234 errno = EEXIST;
1235 return -1;
1237 *current_mode = st.st_mode;
1238 *current_mode_mask = ALL_MODE_BITS;
1242 return fd;
1245 static int
1246 extract_file (char *file_name, int typeflag)
1248 int fd;
1249 off_t size;
1250 union block *data_block;
1251 int status;
1252 size_t count;
1253 size_t written;
1254 bool interdir_made = false;
1255 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1256 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1257 mode_t current_mode = 0;
1258 mode_t current_mode_mask = 0;
1260 if (to_stdout_option)
1261 fd = STDOUT_FILENO;
1262 else if (to_command_option)
1264 fd = sys_exec_command (file_name, 'f', &current_stat_info);
1265 if (fd < 0)
1267 skip_member ();
1268 return 0;
1271 else
1273 int file_created;
1274 while (((file_created = set_xattr (file_name, &current_stat_info,
1275 mode | S_IWUSR, typeflag))
1276 < 0)
1277 || ((fd = open_output_file (file_name, typeflag, mode,
1278 file_created, &current_mode,
1279 &current_mode_mask))
1280 < 0))
1282 int recover = maybe_recoverable (file_name, true, &interdir_made);
1283 if (recover != RECOVER_OK)
1285 skip_member ();
1286 if (recover == RECOVER_SKIP)
1287 return 0;
1288 open_error (file_name);
1289 return 1;
1294 mv_begin_read (&current_stat_info);
1295 if (current_stat_info.is_sparse)
1296 sparse_extract_file (fd, &current_stat_info, &size);
1297 else
1298 for (size = current_stat_info.stat.st_size; size > 0; )
1300 mv_size_left (size);
1302 /* Locate data, determine max length writeable, write it,
1303 block that we have used the data, then check if the write
1304 worked. */
1306 data_block = find_next_block ();
1307 if (! data_block)
1309 ERROR ((0, 0, _("Unexpected EOF in archive")));
1310 break; /* FIXME: What happens, then? */
1313 written = available_space_after (data_block);
1315 if (written > size)
1316 written = size;
1317 errno = 0;
1318 count = blocking_write (fd, data_block->buffer, written);
1319 size -= written;
1321 set_next_block_after ((union block *)
1322 (data_block->buffer + written - 1));
1323 if (count != written)
1325 if (!to_command_option)
1326 write_error_details (file_name, count, written);
1327 /* FIXME: shouldn't we restore from backup? */
1328 break;
1332 skim_file (size, false);
1334 mv_end ();
1336 /* If writing to stdout, don't try to do anything to the filename;
1337 it doesn't exist, or we don't want to touch it anyway. */
1339 if (to_stdout_option)
1340 return 0;
1342 if (! to_command_option)
1343 set_stat (file_name, &current_stat_info, fd,
1344 current_mode, current_mode_mask, typeflag, false,
1345 (old_files_option == OVERWRITE_OLD_FILES
1346 ? 0 : AT_SYMLINK_NOFOLLOW));
1348 status = close (fd);
1349 if (status < 0)
1350 close_error (file_name);
1352 if (to_command_option)
1353 sys_wait_command ();
1355 return status;
1358 /* Find a delayed_link structure corresponding to the source NAME.
1359 Such a structure exists in the delayed link table only if the link
1360 placeholder file has been created. Therefore, try to stat the NAME
1361 first. If it doesn't exist, there is no matching entry in the table.
1362 Otherwise, look for the entry in the table that has the matching dev
1363 and ino numbers. Return a null pointer if not found.
1365 Do not rely on comparing file names, which may differ for
1366 various reasons (e.g. relative vs. absolute file names).
1368 static struct delayed_link *
1369 find_delayed_link_source (char const *name)
1371 struct stat st;
1373 if (!delayed_link_table)
1374 return NULL;
1376 if (fstatat (chdir_fd, name, &st, AT_SYMLINK_NOFOLLOW))
1378 if (errno != ENOENT)
1379 stat_error (name);
1380 return NULL;
1383 struct delayed_link dl;
1384 dl.dev = st.st_dev;
1385 dl.ino = st.st_ino;
1386 return hash_lookup (delayed_link_table, &dl);
1389 /* Create a placeholder file with name FILE_NAME, which will be
1390 replaced after other extraction is done by a symbolic link if
1391 IS_SYMLINK is true, and by a hard link otherwise. Set
1392 *INTERDIR_MADE if an intermediate directory is made in the
1393 process.
1394 If PREV, install the created struct delayed_link after PREV.
1397 static int
1398 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made,
1399 struct delayed_link *prev)
1401 int fd;
1402 struct stat st;
1404 while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1406 if (errno == EEXIST && find_delayed_link_source (file_name))
1408 /* The placeholder file has already been created. This means
1409 that the link being extracted is a duplicate of an already
1410 processed one. Skip it.
1412 return 0;
1415 switch (maybe_recoverable (file_name, false, interdir_made))
1417 case RECOVER_OK:
1418 continue;
1420 case RECOVER_SKIP:
1421 return 0;
1423 case RECOVER_NO:
1424 open_error (file_name);
1425 return -1;
1429 if (fstat (fd, &st) != 0)
1431 stat_error (file_name);
1432 close (fd);
1434 else if (close (fd) != 0)
1435 close_error (file_name);
1436 else
1438 struct delayed_set_stat *h;
1439 struct delayed_link *p =
1440 xmalloc (offsetof (struct delayed_link, target)
1441 + strlen (current_stat_info.link_name)
1442 + 1);
1443 if (prev)
1445 p->next = prev->next;
1446 prev->next = p;
1447 p->has_predecessor = true;
1449 else
1451 p->next = NULL;
1452 p->has_predecessor = false;
1455 p->dev = st.st_dev;
1456 p->ino = st.st_ino;
1457 #if HAVE_BIRTHTIME
1458 p->birthtime = get_stat_birthtime (&st);
1459 #endif
1460 p->is_symlink = is_symlink;
1461 if (is_symlink)
1463 p->mode = current_stat_info.stat.st_mode;
1464 p->uid = current_stat_info.stat.st_uid;
1465 p->gid = current_stat_info.stat.st_gid;
1466 p->atime = current_stat_info.atime;
1467 p->mtime = current_stat_info.mtime;
1469 p->change_dir = chdir_current;
1470 p->sources = xmalloc (offsetof (struct string_list, string)
1471 + strlen (file_name) + 1);
1472 p->sources->next = 0;
1473 strcpy (p->sources->string, file_name);
1474 p->cntx_name = NULL;
1475 assign_string_or_null (&p->cntx_name, current_stat_info.cntx_name);
1476 p->acls_a_ptr = NULL;
1477 p->acls_a_len = 0;
1478 p->acls_d_ptr = NULL;
1479 p->acls_d_len = 0;
1480 xattr_map_init (&p->xattr_map);
1481 xattr_map_copy (&p->xattr_map, &current_stat_info.xattr_map);
1482 strcpy (p->target, current_stat_info.link_name);
1484 if (! ((delayed_link_table
1485 || (delayed_link_table = hash_initialize (0, 0, dl_hash,
1486 dl_compare, free)))
1487 && hash_insert (delayed_link_table, p)))
1488 xalloc_die ();
1490 if ((h = find_direct_ancestor (file_name)) != NULL)
1491 mark_after_links (h);
1493 return 0;
1496 return -1;
1499 static int
1500 extract_link (char *file_name, int typeflag)
1502 bool interdir_made = false;
1503 char const *link_name;
1504 int rc;
1505 struct delayed_link *dl;
1507 link_name = current_stat_info.link_name;
1509 if (! absolute_names_option && contains_dot_dot (link_name))
1510 return create_placeholder_file (file_name, false, &interdir_made, NULL);
1511 dl = find_delayed_link_source (link_name);
1512 if (dl)
1513 return create_placeholder_file (file_name, false, &interdir_made, dl);
1517 struct stat st1, st2;
1518 int e;
1519 int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1520 e = errno;
1522 if (status == 0)
1524 if (delayed_link_table
1525 && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1527 struct delayed_link dl1;
1528 dl1.ino = st1.st_ino;
1529 dl1.dev = st1.st_dev;
1530 struct delayed_link *ds = hash_lookup (delayed_link_table, &dl1);
1531 if (ds && ds->change_dir == chdir_current
1532 && BIRTHTIME_EQ (ds->birthtime, get_stat_birthtime (&st1)))
1534 struct string_list *p = xmalloc (offsetof (struct string_list, string)
1535 + strlen (file_name) + 1);
1536 strcpy (p->string, file_name);
1537 p->next = ds->sources;
1538 ds->sources = p;
1542 return 0;
1544 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1545 || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1546 == 0)
1547 && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1548 == 0)
1549 && st1.st_dev == st2.st_dev
1550 && st1.st_ino == st2.st_ino))
1551 return 0;
1553 errno = e;
1555 while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1556 == RECOVER_OK);
1558 if (rc == RECOVER_SKIP)
1559 return 0;
1560 if (!(incremental_option && errno == EEXIST))
1562 link_error (link_name, file_name);
1563 return 1;
1565 return 0;
1568 static int
1569 extract_symlink (char *file_name, int typeflag)
1571 #ifdef HAVE_SYMLINK
1572 bool interdir_made = false;
1574 if (! absolute_names_option
1575 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1576 || contains_dot_dot (current_stat_info.link_name)))
1577 return create_placeholder_file (file_name, true, &interdir_made, NULL);
1579 while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1580 switch (maybe_recoverable (file_name, false, &interdir_made))
1582 case RECOVER_OK:
1583 continue;
1585 case RECOVER_SKIP:
1586 return 0;
1588 case RECOVER_NO:
1589 symlink_error (current_stat_info.link_name, file_name);
1590 return -1;
1593 set_stat (file_name, &current_stat_info, -1, 0, 0,
1594 SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1595 return 0;
1597 #else
1598 static int warned_once;
1600 if (!warned_once)
1602 warned_once = 1;
1603 WARNOPT (WARN_SYMLINK_CAST,
1604 (0, 0,
1605 _("Attempting extraction of symbolic links as hard links")));
1607 return extract_link (file_name, typeflag);
1608 #endif
1611 #if S_IFCHR || S_IFBLK
1612 static int
1613 extract_node (char *file_name, int typeflag)
1615 bool interdir_made = false;
1616 mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1617 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1619 while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1620 != 0)
1621 switch (maybe_recoverable (file_name, false, &interdir_made))
1623 case RECOVER_OK:
1624 continue;
1626 case RECOVER_SKIP:
1627 return 0;
1629 case RECOVER_NO:
1630 mknod_error (file_name);
1631 return -1;
1634 set_stat (file_name, &current_stat_info, -1,
1635 mode & ~ current_umask, MODE_RWX,
1636 typeflag, false, AT_SYMLINK_NOFOLLOW);
1637 return 0;
1639 #endif
1641 #if HAVE_MKFIFO || defined mkfifo
1642 static int
1643 extract_fifo (char *file_name, int typeflag)
1645 bool interdir_made = false;
1646 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1647 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1649 while (mkfifoat (chdir_fd, file_name, mode) != 0)
1650 switch (maybe_recoverable (file_name, false, &interdir_made))
1652 case RECOVER_OK:
1653 continue;
1655 case RECOVER_SKIP:
1656 return 0;
1658 case RECOVER_NO:
1659 mkfifo_error (file_name);
1660 return -1;
1663 set_stat (file_name, &current_stat_info, -1,
1664 mode & ~ current_umask, MODE_RWX,
1665 typeflag, false, AT_SYMLINK_NOFOLLOW);
1666 return 0;
1668 #endif
1670 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1673 /* Prepare to extract a file. Find extractor function.
1674 Return true to proceed with the extraction, false to skip the current
1675 member. */
1677 static bool
1678 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1680 tar_extractor_t extractor = NULL;
1682 /* Select the extractor */
1683 switch (typeflag)
1685 case GNUTYPE_SPARSE:
1686 extractor = extract_file;
1687 break;
1689 case AREGTYPE:
1690 case REGTYPE:
1691 case CONTTYPE:
1692 /* Appears to be a file. But BSD tar uses the convention that a slash
1693 suffix means a directory. */
1694 if (current_stat_info.had_trailing_slash)
1695 extractor = extract_dir;
1696 else
1697 extractor = extract_file;
1698 break;
1700 case SYMTYPE:
1701 extractor = extract_symlink;
1702 break;
1704 case LNKTYPE:
1705 extractor = extract_link;
1706 break;
1708 #if S_IFCHR
1709 case CHRTYPE:
1710 current_stat_info.stat.st_mode |= S_IFCHR;
1711 extractor = extract_node;
1712 break;
1713 #endif
1715 #if S_IFBLK
1716 case BLKTYPE:
1717 current_stat_info.stat.st_mode |= S_IFBLK;
1718 extractor = extract_node;
1719 break;
1720 #endif
1722 #if HAVE_MKFIFO || defined mkfifo
1723 case FIFOTYPE:
1724 extractor = extract_fifo;
1725 break;
1726 #endif
1728 case DIRTYPE:
1729 case GNUTYPE_DUMPDIR:
1730 extractor = extract_dir;
1731 if (current_stat_info.is_dumpdir)
1732 delay_directory_restore_option = true;
1733 break;
1735 case GNUTYPE_VOLHDR:
1736 return false;
1738 case GNUTYPE_MULTIVOL:
1739 ERROR ((0, 0,
1740 _("%s: Cannot extract -- file is continued from another volume"),
1741 quotearg_colon (current_stat_info.file_name)));
1742 return false;
1744 case GNUTYPE_LONGNAME:
1745 case GNUTYPE_LONGLINK:
1746 ERROR ((0, 0, _("Unexpected long name header")));
1747 return false;
1749 default:
1750 WARNOPT (WARN_UNKNOWN_CAST,
1751 (0, 0,
1752 _("%s: Unknown file type '%c', extracted as normal file"),
1753 quotearg_colon (file_name), typeflag));
1754 extractor = extract_file;
1757 if (EXTRACT_OVER_PIPE)
1759 if (extractor != extract_file)
1760 return false;
1762 else
1764 switch (old_files_option)
1766 case UNLINK_FIRST_OLD_FILES:
1767 if (!remove_any_file (file_name,
1768 recursive_unlink_option
1769 ? RECURSIVE_REMOVE_OPTION
1770 : ORDINARY_REMOVE_OPTION)
1771 && errno && errno != ENOENT)
1773 unlink_error (file_name);
1774 return false;
1776 break;
1778 case KEEP_NEWER_FILES:
1779 if (file_newer_p (file_name, 0, &current_stat_info))
1781 WARNOPT (WARN_IGNORE_NEWER,
1782 (0, 0, _("Current %s is newer or same age"),
1783 quote (file_name)));
1784 return false;
1786 break;
1788 default:
1789 break;
1792 *fun = extractor;
1794 return true;
1797 /* Extract a file from the archive. */
1798 void
1799 extract_archive (void)
1801 char typeflag;
1802 tar_extractor_t fun;
1803 bool skip_dotdot_name;
1805 fatal_exit_hook = extract_finish;
1807 set_next_block_after (current_header);
1809 skip_dotdot_name = (!absolute_names_option
1810 && contains_dot_dot (current_stat_info.orig_file_name));
1811 if (skip_dotdot_name)
1812 ERROR ((0, 0, _("%s: Member name contains '..'"),
1813 quotearg_colon (current_stat_info.orig_file_name)));
1815 if (!current_stat_info.file_name[0]
1816 || skip_dotdot_name
1817 || (interactive_option
1818 && !confirm ("extract", current_stat_info.file_name)))
1820 skip_member ();
1821 return;
1824 /* Print the block from current_header and current_stat. */
1825 if (verbose_option)
1826 print_header (&current_stat_info, current_header, -1);
1828 /* Restore stats for all non-ancestor directories, unless
1829 it is an incremental archive.
1830 (see NOTICE in the comment to delay_set_stat above) */
1831 if (!delay_directory_restore_option)
1833 int dir = chdir_current;
1834 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1835 chdir_do (dir);
1838 /* Take a safety backup of a previously existing file. */
1840 if (backup_option)
1841 if (!maybe_backup_file (current_stat_info.file_name, 0))
1843 int e = errno;
1844 ERROR ((0, e, _("%s: Was unable to backup this file"),
1845 quotearg_colon (current_stat_info.file_name)));
1846 skip_member ();
1847 return;
1850 /* Extract the archive entry according to its type. */
1851 /* KLUDGE */
1852 typeflag = sparse_member_p (&current_stat_info) ?
1853 GNUTYPE_SPARSE : current_header->header.typeflag;
1855 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1857 if (fun (current_stat_info.file_name, typeflag) == 0)
1858 return;
1860 else
1861 skip_member ();
1863 if (backup_option)
1864 undo_last_backup ();
1867 /* Extract the link DS whose final extraction was delayed. */
1868 static void
1869 apply_delayed_link (struct delayed_link *ds)
1871 struct string_list *sources = ds->sources;
1872 char const *valid_source = 0;
1874 chdir_do (ds->change_dir);
1876 for (sources = ds->sources; sources; sources = sources->next)
1878 char const *source = sources->string;
1879 struct stat st;
1881 /* Make sure the placeholder file is still there. If not,
1882 don't create a link, as the placeholder was probably
1883 removed by a later extraction. */
1884 if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1885 && st.st_dev == ds->dev
1886 && st.st_ino == ds->ino
1887 && BIRTHTIME_EQ (get_stat_birthtime (&st), ds->birthtime))
1889 /* Unlink the placeholder, then create a hard link if possible,
1890 a symbolic link otherwise. */
1891 if (unlinkat (chdir_fd, source, 0) != 0)
1892 unlink_error (source);
1893 else if (valid_source
1894 && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1895 == 0))
1897 else if (!ds->is_symlink)
1899 if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1900 link_error (ds->target, source);
1902 else if (symlinkat (ds->target, chdir_fd, source) != 0)
1903 symlink_error (ds->target, source);
1904 else
1906 struct tar_stat_info st1;
1907 st1.stat.st_mode = ds->mode;
1908 st1.stat.st_uid = ds->uid;
1909 st1.stat.st_gid = ds->gid;
1910 st1.atime = ds->atime;
1911 st1.mtime = ds->mtime;
1912 st1.cntx_name = ds->cntx_name;
1913 st1.acls_a_ptr = ds->acls_a_ptr;
1914 st1.acls_a_len = ds->acls_a_len;
1915 st1.acls_d_ptr = ds->acls_d_ptr;
1916 st1.acls_d_len = ds->acls_d_len;
1917 st1.xattr_map = ds->xattr_map;
1918 set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1919 false, AT_SYMLINK_NOFOLLOW);
1920 valid_source = source;
1925 for (sources = ds->sources; sources; )
1927 struct string_list *next = sources->next;
1928 free (sources);
1929 sources = next;
1932 xattr_map_free (&ds->xattr_map);
1933 free (ds->cntx_name);
1936 /* Extract the links whose final extraction were delayed. */
1937 static void
1938 apply_delayed_links (void)
1940 if (!delayed_link_table)
1941 return;
1943 for (struct delayed_link *dl = hash_get_first (delayed_link_table);
1945 dl = dl->next ? dl->next : hash_get_next (delayed_link_table, dl))
1946 if (!dl->has_predecessor)
1948 struct delayed_link *ds = dl;
1951 apply_delayed_link (ds);
1952 ds = ds->next;
1954 while (ds);
1957 if (false)
1959 /* There is little point to freeing, as we are about to exit,
1960 and freeing is more likely to cause than cure trouble. */
1961 hash_free (delayed_link_table);
1962 delayed_link_table = NULL;
1966 /* Finish the extraction of an archive. */
1967 void
1968 extract_finish (void)
1970 /* First, fix the status of ordinary directories that need fixing. */
1971 apply_nonancestor_delayed_set_stat ("", 0);
1973 /* Then, apply delayed links, so that they don't affect delayed
1974 directory status-setting for ordinary directories. */
1975 apply_delayed_links ();
1977 /* Finally, fix the status of directories that are ancestors
1978 of delayed links. */
1979 apply_nonancestor_delayed_set_stat ("", 1);
1982 bool
1983 rename_directory (char *src, char *dst)
1985 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1986 fixup_delayed_set_stat (src, dst);
1987 else
1989 int e = errno;
1991 switch (e)
1993 case ENOENT:
1994 if (make_directories (dst) == 0)
1996 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1997 return true;
1998 e = errno;
2000 break;
2002 case EXDEV:
2003 /* FIXME: Fall back to recursive copying */
2005 default:
2006 break;
2009 ERROR ((0, e, _("Cannot rename %s to %s"),
2010 quote_n (0, src),
2011 quote_n (1, dst)));
2012 return false;
2014 return true;