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. */
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)
41 #if ! HAVE_FCHOWN && ! defined fchown
42 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
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
51 # define HAVE_BIRTHTIME 0
55 # define BIRTHTIME_EQ(a, b) (timespec_cmp (a, b) == 0)
57 # define BIRTHTIME_EQ(a, b) true
60 /* Return true if an error number ERR means the system call is
61 supported in this case. */
65 return ! (err
== ENOSYS
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
79 struct delayed_set_stat
81 /* Next directory in list. */
82 struct delayed_set_stat
*next
;
84 /* Metadata for this directory. */
87 mode_t mode
; /* The desired mode is MODE & ~ current_umask. */
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
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. */
106 /* Whether symbolic links should be followed when accessing the
110 /* Do not set the status of this directory until after delayed
111 links are created. */
114 /* Directory that the name is relative to. */
117 /* extended attributes*/
123 size_t xattr_map_size
;
124 struct xattr_map xattr_map
;
125 /* Length and contents of name. */
126 size_t file_name_len
;
130 static struct delayed_set_stat
*delayed_set_stat_head
;
132 /* A link whose creation we have delayed. */
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. */
151 struct timespec birthtime
;
154 /* True if the link is symbolic. */
157 /* The desired metadata, valid only the link is symbolic. */
161 struct timespec atime
;
162 struct timespec mtime
;
164 /* The directory that the sources and target are relative to. */
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 */
180 struct xattr_map xattr_map
;
182 /* The desired target of the desired link. */
186 static Hash_table
*delayed_link_table
;
190 struct string_list
*next
;
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
);
203 return n
% table_size
;
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. */
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
)
230 umask (newdir_umask
); /* restore the kernel umask */
231 current_umask
= newdir_umask
;
235 /* Use fchmod if possible, fchmodat otherwise. */
237 fd_i_chmod (int fd
, char const *file
, mode_t mode
, int atflag
)
241 int result
= fchmod (fd
, mode
);
242 if (result
== 0 || implemented (errno
))
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
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
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
)))
284 /* Use fchown if possible, fchownat otherwise. */
286 fd_chown (int fd
, char const *file
, uid_t uid
, gid_t gid
, int atflag
)
290 int result
= fchown (fd
, uid
, gid
);
291 if (result
== 0 || implemented (errno
))
294 return fchownat (chdir_fd
, file
, uid
, gid
, atflag
);
297 /* Use fstat if possible, fstatat otherwise. */
299 fd_stat (int fd
, char const *file
, struct stat
*st
, int atflag
)
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. */
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
)
325 if (fd_stat (fd
, file_name
, &st
, atflag
) != 0)
327 stat_error (file_name
);
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. */
346 check_time (char const *file_name
, struct timespec t
)
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)
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
;
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. */
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
)
398 ts
[0].tv_nsec
= UTIME_OMIT
;
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
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
);
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
;
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))
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. */
466 mark_after_links (struct delayed_set_stat
*head
)
468 struct delayed_set_stat
*h
= head
;
475 if (deref_stat (h
->file_name
, &st
) != 0)
476 stat_error (h
->file_name
);
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,
502 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
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)
521 if (fstatat (chdir_fd
, data
->file_name
,
522 &real_st
, data
->atflag
) != 0)
524 stat_error (data
->file_name
);
528 data
->dev
= real_st
.st_dev
;
529 data
->ino
= real_st
.st_ino
;
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;
543 data
->dev
= st
->stat
.st_dev
;
544 data
->ino
= st
->stat
.st_ino
;
546 xattr_map_init (&data
->xattr_map
);
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
;
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
;
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
;
582 data
->acls_d_ptr
= NULL
;
583 data
->acls_d_len
= 0;
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. */
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
)
603 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
605 stat_error (data
->file_name
);
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;
626 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
627 quotearg_colon (dir
)));
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
);
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
)
648 if (chdir_current
== data
->change_dir
649 && strcmp (data
->file_name
, fname
) == 0)
651 free_delayed_set_stat (data
);
655 delayed_set_stat_head
= next
;
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
);
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. */
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
;
692 for (cursor
= cursor0
; *cursor
; cursor
++)
698 if (! ISSLASH (*cursor
))
701 /* Avoid mkdir of empty string, if leading or double '/'. */
703 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
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])))))
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
);
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
);
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
);
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. */
745 parent_errno
= errno
;
755 /* Although we did not create the parent directory, some other
756 process may have created it, so check whether it exists now. */
759 int stat_status
= fstatat (chdir_fd
, file_name
, &st
, 0);
760 if (!stat_status
&& !S_ISDIR (st
.st_mode
))
764 errno
= parent_errno
;
765 mkdir_error (file_name
);
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
776 file_newer_p (const char *file_name
, struct stat
const *stp
,
777 struct tar_stat_info
*tar_stat
)
783 if (deref_stat (file_name
, &st
) != 0)
787 stat_warn (file_name
);
788 /* Be safer: if the file exists, assume it is newer. */
796 return (! S_ISDIR (stp
->st_mode
)
797 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (stp
)) <= 0);
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. */
817 maybe_recoverable (char *file_name
, bool regular
, bool *interdir_made
)
821 struct stat
const *stp
= 0;
830 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
831 but some operating systems do not conform to the standard. */
833 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
836 /* FreeBSD 8.1 uses errno == EMLINK. */
838 /* Tru64 5.1B uses errno == ENOTSUP. */
842 || old_files_option
!= OVERWRITE_OLD_FILES
|| dereference_option
)
844 if (strchr (file_name
, '/'))
846 if (deref_stat (file_name
, &st
) != 0)
850 /* The caller tried to open a symbolic link with O_NOFOLLOW.
851 Fall through, treating it as an already-existing file. */
854 /* Remove an old file, if the options allow this. */
856 switch (old_files_option
)
859 WARNOPT (WARN_EXISTING_FILE
,
860 (0, 0, _("%s: skipping existing file"), file_name
));
866 case KEEP_NEWER_FILES
:
867 if (file_newer_p (file_name
, stp
, ¤t_stat_info
))
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
))
877 case UNLINK_FIRST_OLD_FILES
:
883 /* Attempt creating missing intermediate directories. */
884 if (make_directories (file_name
) == 0)
886 *interdir_made
= true;
892 /* Just say we can't do anything about it... */
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. */
911 set_xattr (char const *file_name
, struct tar_stat_info
const *st
,
912 mode_t mode
, char typeflag
)
915 if ((xattrs_option
> 0) && st
->xattr_map
.xm_size
)
917 int r
= mknodat (chdir_fd
, file_name
, mode
, 0);
920 xattrs_xattrs_set (st
, file_name
, typeflag
, 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. */
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;
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))
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
);
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
))
973 _("%s: Directory renamed before its status could be extracted"),
974 quotearg_colon (data
->file_name
)));
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
);
1006 is_directory_link (char const *file_name
, struct stat
*st
)
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
1021 If not root, though, make the directory writeable and searchable at first,
1022 so that files can be created under it.
1025 safe_dir_mode (struct stat
const *st
)
1027 return ((st
->st_mode
1028 & (0 < same_owner_option
|| 0 < same_permissions_option
1031 | (we_are_root
? 0 : MODE_WXUSR
));
1034 /* Extractor functions for various member types */
1037 extract_dir (char *file_name
, int typeflag
)
1041 mode_t current_mode
= 0;
1042 mode_t current_mode_mask
= 0;
1044 bool interdir_made
= false;
1046 /* Save 'root device' to avoid purging mount points. */
1047 if (one_file_system_option
&& root_device
== 0)
1051 if (fstatat (chdir_fd
, ".", &st
, 0) != 0)
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
)
1063 mode
= safe_dir_mode (¤t_stat_info
.stat
);
1067 status
= mkdirat (chdir_fd
, file_name
, mode
);
1070 current_mode
= mode
& ~ current_umask
;
1071 current_mode_mask
= MODE_RWX
;
1072 atflag
= AT_SYMLINK_NOFOLLOW
;
1076 if (errno
== EEXIST
)
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
)
1087 if (keep_directory_symlink_option
1088 && is_directory_link (file_name
, &st
))
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
))
1101 repair_delayed_set_stat (file_name
, &st
);
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
1110 mode
= safe_dir_mode (&st
);
1111 status
= fd_chmod (-1, file_name
, mode
,
1112 AT_SYMLINK_NOFOLLOW
, DIRTYPE
);
1115 /* Store the actual directory mode, to be restored
1118 current_stat_info
.stat
= st
;
1119 current_mode
= mode
& ~ current_umask
;
1120 current_mode_mask
= MODE_RWX
;
1121 atflag
= AT_SYMLINK_NOFOLLOW
;
1126 chmod_error_details (file_name
, mode
);
1133 else if (old_files_option
== UNLINK_FIRST_OLD_FILES
)
1142 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1151 if (errno
!= EEXIST
)
1153 mkdir_error (file_name
);
1162 || old_files_option
== DEFAULT_OLD_FILES
1163 || old_files_option
== OVERWRITE_OLD_FILES
)
1164 delay_set_stat (file_name
, ¤t_stat_info
,
1165 current_mode
, current_mode_mask
,
1166 current_stat_info
.stat
.st_mode
, atflag
);
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
)
1178 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
1179 int openflag
= (O_WRONLY
| O_BINARY
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
1183 | (overwriting_old_files
1184 ? O_TRUNC
| (dereference_option
? 0 : O_NOFOLLOW
)
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
)
1206 if (0 <= readlinkat (chdir_fd
, file_name
, buf
, sizeof buf
))
1213 fd
= openat (chdir_fd
, file_name
, openflag
, mode
);
1216 if (openflag
& O_EXCL
)
1218 *current_mode
= mode
& ~ current_umask
;
1219 *current_mode_mask
= MODE_RWX
;
1224 if (fstat (fd
, &st
) != 0)
1231 if (! S_ISREG (st
.st_mode
))
1237 *current_mode
= st
.st_mode
;
1238 *current_mode_mask
= ALL_MODE_BITS
;
1246 extract_file (char *file_name
, int typeflag
)
1250 union block
*data_block
;
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
)
1262 else if (to_command_option
)
1264 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
1274 while (((file_created
= set_xattr (file_name
, ¤t_stat_info
,
1275 mode
| S_IWUSR
, typeflag
))
1277 || ((fd
= open_output_file (file_name
, typeflag
, mode
,
1278 file_created
, ¤t_mode
,
1279 ¤t_mode_mask
))
1282 int recover
= maybe_recoverable (file_name
, true, &interdir_made
);
1283 if (recover
!= RECOVER_OK
)
1286 if (recover
== RECOVER_SKIP
)
1288 open_error (file_name
);
1294 mv_begin_read (¤t_stat_info
);
1295 if (current_stat_info
.is_sparse
)
1296 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
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
1306 data_block
= find_next_block ();
1309 ERROR ((0, 0, _("Unexpected EOF in archive")));
1310 break; /* FIXME: What happens, then? */
1313 written
= available_space_after (data_block
);
1318 count
= blocking_write (fd
, data_block
->buffer
, 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? */
1332 skim_file (size
, false);
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
)
1342 if (! to_command_option
)
1343 set_stat (file_name
, ¤t_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
);
1350 close_error (file_name
);
1352 if (to_command_option
)
1353 sys_wait_command ();
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
)
1373 if (!delayed_link_table
)
1376 if (fstatat (chdir_fd
, name
, &st
, AT_SYMLINK_NOFOLLOW
))
1378 if (errno
!= ENOENT
)
1383 struct delayed_link dl
;
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
1394 If PREV, install the created struct delayed_link after PREV.
1398 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
,
1399 struct delayed_link
*prev
)
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.
1415 switch (maybe_recoverable (file_name
, false, interdir_made
))
1424 open_error (file_name
);
1429 if (fstat (fd
, &st
) != 0)
1431 stat_error (file_name
);
1434 else if (close (fd
) != 0)
1435 close_error (file_name
);
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
)
1445 p
->next
= prev
->next
;
1447 p
->has_predecessor
= true;
1452 p
->has_predecessor
= false;
1458 p
->birthtime
= get_stat_birthtime (&st
);
1460 p
->is_symlink
= 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
;
1478 p
->acls_d_ptr
= NULL
;
1480 xattr_map_init (&p
->xattr_map
);
1481 xattr_map_copy (&p
->xattr_map
, ¤t_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
,
1487 && hash_insert (delayed_link_table
, p
)))
1490 if ((h
= find_direct_ancestor (file_name
)) != NULL
)
1491 mark_after_links (h
);
1500 extract_link (char *file_name
, int typeflag
)
1502 bool interdir_made
= false;
1503 char const *link_name
;
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
);
1513 return create_placeholder_file (file_name
, false, &interdir_made
, dl
);
1517 struct stat st1
, st2
;
1519 int status
= linkat (chdir_fd
, link_name
, chdir_fd
, file_name
, 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
;
1544 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1545 || ((fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
)
1547 && (fstatat (chdir_fd
, file_name
, &st2
, AT_SYMLINK_NOFOLLOW
)
1549 && st1
.st_dev
== st2
.st_dev
1550 && st1
.st_ino
== st2
.st_ino
))
1555 while ((rc
= maybe_recoverable (file_name
, false, &interdir_made
))
1558 if (rc
== RECOVER_SKIP
)
1560 if (!(incremental_option
&& errno
== EEXIST
))
1562 link_error (link_name
, file_name
);
1569 extract_symlink (char *file_name
, int typeflag
)
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
))
1589 symlink_error (current_stat_info
.link_name
, file_name
);
1593 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1594 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1598 static int warned_once
;
1603 WARNOPT (WARN_SYMLINK_CAST
,
1605 _("Attempting extraction of symbolic links as hard links")));
1607 return extract_link (file_name
, typeflag
);
1611 #if S_IFCHR || S_IFBLK
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
)
1621 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1630 mknod_error (file_name
);
1634 set_stat (file_name
, ¤t_stat_info
, -1,
1635 mode
& ~ current_umask
, MODE_RWX
,
1636 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1641 #if HAVE_MKFIFO || defined mkfifo
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
))
1659 mkfifo_error (file_name
);
1663 set_stat (file_name
, ¤t_stat_info
, -1,
1664 mode
& ~ current_umask
, MODE_RWX
,
1665 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
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
1678 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1680 tar_extractor_t extractor
= NULL
;
1682 /* Select the extractor */
1685 case GNUTYPE_SPARSE
:
1686 extractor
= extract_file
;
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
;
1697 extractor
= extract_file
;
1701 extractor
= extract_symlink
;
1705 extractor
= extract_link
;
1710 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1711 extractor
= extract_node
;
1717 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1718 extractor
= extract_node
;
1722 #if HAVE_MKFIFO || defined mkfifo
1724 extractor
= extract_fifo
;
1729 case GNUTYPE_DUMPDIR
:
1730 extractor
= extract_dir
;
1731 if (current_stat_info
.is_dumpdir
)
1732 delay_directory_restore_option
= true;
1735 case GNUTYPE_VOLHDR
:
1738 case GNUTYPE_MULTIVOL
:
1740 _("%s: Cannot extract -- file is continued from another volume"),
1741 quotearg_colon (current_stat_info
.file_name
)));
1744 case GNUTYPE_LONGNAME
:
1745 case GNUTYPE_LONGLINK
:
1746 ERROR ((0, 0, _("Unexpected long name header")));
1750 WARNOPT (WARN_UNKNOWN_CAST
,
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
)
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
);
1778 case KEEP_NEWER_FILES
:
1779 if (file_newer_p (file_name
, 0, ¤t_stat_info
))
1781 WARNOPT (WARN_IGNORE_NEWER
,
1782 (0, 0, _("Current %s is newer or same age"),
1783 quote (file_name
)));
1797 /* Extract a file from the archive. */
1799 extract_archive (void)
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]
1817 || (interactive_option
1818 && !confirm ("extract", current_stat_info
.file_name
)))
1824 /* Print the block from current_header and current_stat. */
1826 print_header (¤t_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);
1838 /* Take a safety backup of a previously existing file. */
1841 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1844 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1845 quotearg_colon (current_stat_info
.file_name
)));
1850 /* Extract the archive entry according to its type. */
1852 typeflag
= sparse_member_p (¤t_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)
1864 undo_last_backup ();
1867 /* Extract the link DS whose final extraction was delayed. */
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
;
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)
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
);
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
;
1932 xattr_map_free (&ds
->xattr_map
);
1933 free (ds
->cntx_name
);
1936 /* Extract the links whose final extraction were delayed. */
1938 apply_delayed_links (void)
1940 if (!delayed_link_table
)
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
);
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. */
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);
1983 rename_directory (char *src
, char *dst
)
1985 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) == 0)
1986 fixup_delayed_set_stat (src
, dst
);
1994 if (make_directories (dst
) == 0)
1996 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) == 0)
2003 /* FIXME: Fall back to recursive copying */
2009 ERROR ((0, e
, _("Cannot rename %s to %s"),