1 /* Extract files from a tar archive.
3 Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2013
4 Free Software Foundation, Inc.
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Written by John Gilmore, on 1985-11-19. */
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 /* Return true if an error number ERR means the system call is
46 supported in this case. */
50 return ! (err
== ENOSYS
52 || (EOPNOTSUPP
!= ENOTSUP
&& err
== EOPNOTSUPP
));
55 /* List of directories whose statuses we need to extract after we've
56 finished extracting their subsidiary files. If you consider each
57 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
58 represents an element where AFTER_LINKS is nonzero and [^D]
59 represents an element where AFTER_LINKS is zero, then the head
60 of the subsequence has the longest name, and each non-head element
61 in the prefix is an ancestor (in the directory hierarchy) of the
64 struct delayed_set_stat
66 /* Next directory in list. */
67 struct delayed_set_stat
*next
;
69 /* Metadata for this directory. */
72 mode_t mode
; /* The desired mode is MODE & ~ current_umask. */
75 struct timespec atime
;
76 struct timespec mtime
;
78 /* An estimate of the directory's current mode, along with a mask
79 specifying which bits of this estimate are known to be correct.
80 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
83 mode_t current_mode_mask
;
85 /* This directory is an intermediate directory that was created
86 as an ancestor of some other directory; it was not mentioned
87 in the archive, so do not set its uid, gid, atime, or mtime,
88 and don't alter its mode outside of MODE_RWX. */
91 /* Whether symbolic links should be followed when accessing the
95 /* Do not set the status of this directory until after delayed
99 /* Directory that the name is relative to. */
102 /* extended attributes*/
108 size_t xattr_map_size
;
109 struct xattr_array
*xattr_map
;
110 /* Length and contents of name. */
111 size_t file_name_len
;
115 static struct delayed_set_stat
*delayed_set_stat_head
;
117 /* List of links whose creation we have delayed. */
120 /* The next delayed link in the list. */
121 struct delayed_link
*next
;
123 /* The device, inode number and birthtime of the placeholder.
124 birthtime.tv_nsec is negative if the birthtime is not available.
125 Don't use mtime as this would allow for false matches if some
126 other process removes the placeholder. Don't use ctime as
127 this would cause race conditions and other screwups, e.g.,
128 when restoring hard-linked symlinks. */
131 struct timespec birthtime
;
133 /* True if the link is symbolic. */
136 /* The desired metadata, valid only the link is symbolic. */
140 struct timespec atime
;
141 struct timespec mtime
;
143 /* The directory that the sources and target are relative to. */
146 /* A list of sources for this link. The sources are all to be
147 hard-linked together. */
148 struct string_list
*sources
;
150 /* SELinux context */
159 size_t xattr_map_size
;
160 struct xattr_array
*xattr_map
;
162 /* The desired target of the desired link. */
166 static struct delayed_link
*delayed_link_head
;
170 struct string_list
*next
;
174 /* Set up to extract files. */
178 we_are_root
= geteuid () == ROOT_UID
;
179 same_permissions_option
+= we_are_root
;
180 same_owner_option
+= we_are_root
;
182 /* Option -p clears the kernel umask, so it does not affect proper
183 restoration of file permissions. New intermediate directories will
184 comply with umask at start of program. */
186 newdir_umask
= umask (0);
187 if (0 < same_permissions_option
)
191 umask (newdir_umask
); /* restore the kernel umask */
192 current_umask
= newdir_umask
;
195 /* If the user wants to guarantee that everything is under one directory,
196 determine its name now and let it be created later. */
197 if (one_top_level_option
&& !one_top_level_dir
)
199 char *base
= base_name (archive_name_array
[0]);
201 one_top_level_dir
= strip_compression_suffix (base
);
204 if (!one_top_level_dir
)
205 USAGE_ERROR ((0, 0, _("Cannot deduce top-level directory name; please set it explicitly with --one-top-level=DIR")));
209 /* Use fchmod if possible, fchmodat otherwise. */
211 fd_chmod (int fd
, char const *file
, mode_t mode
, int atflag
)
215 int result
= fchmod (fd
, mode
);
216 if (result
== 0 || implemented (errno
))
219 return fchmodat (chdir_fd
, file
, mode
, atflag
);
222 /* Use fchown if possible, fchownat otherwise. */
224 fd_chown (int fd
, char const *file
, uid_t uid
, gid_t gid
, int atflag
)
228 int result
= fchown (fd
, uid
, gid
);
229 if (result
== 0 || implemented (errno
))
232 return fchownat (chdir_fd
, file
, uid
, gid
, atflag
);
235 /* Use fstat if possible, fstatat otherwise. */
237 fd_stat (int fd
, char const *file
, struct stat
*st
, int atflag
)
241 : fstatat (chdir_fd
, file
, st
, atflag
));
244 /* Set the mode for FILE_NAME to MODE.
245 MODE_MASK specifies the bits of MODE that we care about;
246 thus if MODE_MASK is zero, do nothing.
247 If FD is nonnegative, it is a file descriptor for the file.
248 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
249 the file's current mode, using the style of struct delayed_set_stat.
250 TYPEFLAG specifies the type of the file.
251 ATFLAG specifies the flag to use when statting the file. */
253 set_mode (char const *file_name
,
254 mode_t mode
, mode_t mode_mask
, int fd
,
255 mode_t current_mode
, mode_t current_mode_mask
,
256 char typeflag
, int atflag
)
258 if (((current_mode
^ mode
) | ~ current_mode_mask
) & mode_mask
)
260 if (MODE_ALL
& ~ mode_mask
& ~ current_mode_mask
)
263 if (fd_stat (fd
, file_name
, &st
, atflag
) != 0)
265 stat_error (file_name
);
268 current_mode
= st
.st_mode
;
271 current_mode
&= MODE_ALL
;
272 mode
= (current_mode
& ~ mode_mask
) | (mode
& mode_mask
);
274 if (current_mode
!= mode
)
277 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
279 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
280 setuid-root files would otherwise be a backdoor. See
281 http://opensolaris.org/jive/thread.jspa?threadID=95826
283 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
)
284 && priv_set_restore_linkdir () == 0)
287 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
288 priv_set_remove_linkdir ();
291 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
292 returns ENOTSUP even when operating on non-symlinks, try
293 again with the flag disabled if it does not appear to be
294 supported and if the file is not a symlink. This
295 introduces a race, alas. */
296 if (atflag
&& typeflag
!= SYMTYPE
&& ! implemented (chmod_errno
))
297 chmod_errno
= fd_chmod (fd
, file_name
, mode
, 0) == 0 ? 0 : errno
;
300 && (typeflag
!= SYMTYPE
|| implemented (chmod_errno
)))
303 chmod_error_details (file_name
, mode
);
309 /* Check time after successfully setting FILE_NAME's time stamp to T. */
311 check_time (char const *file_name
, struct timespec t
)
314 WARNOPT (WARN_TIMESTAMP
,
315 (0, 0, _("%s: implausibly old time stamp %s"),
316 file_name
, tartime (t
, true)));
317 else if (timespec_cmp (volume_start_time
, t
) < 0)
321 if (timespec_cmp (now
, t
) < 0)
323 char buf
[TIMESPEC_STRSIZE_BOUND
];
324 struct timespec diff
;
325 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
326 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
327 if (diff
.tv_nsec
< 0)
329 diff
.tv_nsec
+= BILLION
;
332 WARNOPT (WARN_TIMESTAMP
,
333 (0, 0, _("%s: time stamp %s is %s s in the future"),
334 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
339 /* Restore stat attributes (owner, group, mode and times) for
340 FILE_NAME, using information given in *ST.
341 If FD is nonnegative, it is a file descriptor for the file.
342 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
343 the file's current mode, using the style of struct delayed_set_stat.
344 TYPEFLAG specifies the type of the file.
345 If INTERDIR, this is an intermediate directory.
346 ATFLAG specifies the flag to use when statting the file. */
349 set_stat (char const *file_name
,
350 struct tar_stat_info
const *st
,
351 int fd
, mode_t current_mode
, mode_t current_mode_mask
,
352 char typeflag
, bool interdir
, int atflag
)
354 /* Do the utime before the chmod because some versions of utime are
355 broken and trash the modes of the file. */
357 if (! touch_option
&& ! interdir
)
359 struct timespec ts
[2];
360 if (incremental_option
)
363 ts
[0].tv_nsec
= UTIME_OMIT
;
366 if (fdutimensat (fd
, chdir_fd
, file_name
, ts
, atflag
) == 0)
368 if (incremental_option
)
369 check_time (file_name
, ts
[0]);
370 check_time (file_name
, ts
[1]);
372 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
373 utime_error (file_name
);
376 if (0 < same_owner_option
&& ! interdir
)
378 /* Some systems allow non-root users to give files away. Once this
379 done, it is not possible anymore to change file permissions.
380 However, setting file permissions now would be incorrect, since
381 they would apply to the wrong user, and there would be a race
382 condition. So, don't use systems that allow non-root users to
384 uid_t uid
= st
->stat
.st_uid
;
385 gid_t gid
= st
->stat
.st_gid
;
387 if (fd_chown (fd
, file_name
, uid
, gid
, atflag
) == 0)
389 /* Changing the owner can clear st_mode bits in some cases. */
390 if ((current_mode
| ~ current_mode_mask
) & S_IXUGO
)
391 current_mode_mask
&= ~ (current_mode
& (S_ISUID
| S_ISGID
));
393 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
394 chown_error_details (file_name
, uid
, gid
);
398 st
->stat
.st_mode
& ~ current_umask
,
399 0 < same_permissions_option
&& ! interdir
? MODE_ALL
: MODE_RWX
,
400 fd
, current_mode
, current_mode_mask
, typeflag
, atflag
);
402 /* these three calls must be done *after* fd_chown() call because fd_chown
403 causes that linux capabilities becomes cleared. */
404 xattrs_xattrs_set (st
, file_name
, typeflag
, 1);
405 xattrs_acls_set (st
, file_name
, typeflag
);
406 xattrs_selinux_set (st
, file_name
, typeflag
);
409 /* For each entry H in the leading prefix of entries in HEAD that do
410 not have after_links marked, mark H and fill in its dev and ino
411 members. Assume HEAD && ! HEAD->after_links. */
413 mark_after_links (struct delayed_set_stat
*head
)
415 struct delayed_set_stat
*h
= head
;
422 if (deref_stat (h
->file_name
, &st
) != 0)
423 stat_error (h
->file_name
);
430 while ((h
= h
->next
) && ! h
->after_links
);
433 /* Remember to restore stat attributes (owner, group, mode and times)
434 for the directory FILE_NAME, using information given in *ST,
435 once we stop extracting files into that directory.
437 If ST is null, merely create a placeholder node for an intermediate
438 directory that was created by make_directories.
440 NOTICE: this works only if the archive has usual member order, i.e.
441 directory, then the files in that directory. Incremental archive have
442 somewhat reversed order: first go subdirectories, then all other
443 members. To help cope with this case the variable
444 delay_directory_restore_option is set by prepare_to_extract.
446 If an archive was explicitely created so that its member order is
447 reversed, some directory timestamps can be restored incorrectly,
449 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
452 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
453 mode_t current_mode
, mode_t current_mode_mask
,
454 mode_t mode
, int atflag
)
456 size_t file_name_len
= strlen (file_name
);
457 struct delayed_set_stat
*data
=
458 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
459 + file_name_len
+ 1);
460 data
->next
= delayed_set_stat_head
;
464 data
->dev
= st
->stat
.st_dev
;
465 data
->ino
= st
->stat
.st_ino
;
466 data
->uid
= st
->stat
.st_uid
;
467 data
->gid
= st
->stat
.st_gid
;
468 data
->atime
= st
->atime
;
469 data
->mtime
= st
->mtime
;
471 data
->file_name_len
= file_name_len
;
472 data
->current_mode
= current_mode
;
473 data
->current_mode_mask
= current_mode_mask
;
474 data
->interdir
= ! st
;
475 data
->atflag
= atflag
;
476 data
->after_links
= 0;
477 data
->change_dir
= chdir_current
;
478 data
->cntx_name
= NULL
;
480 assign_string (&data
->cntx_name
, st
->cntx_name
);
481 if (st
&& st
->acls_a_ptr
)
483 data
->acls_a_ptr
= xmemdup (st
->acls_a_ptr
, st
->acls_a_len
+ 1);
484 data
->acls_a_len
= st
->acls_a_len
;
488 data
->acls_a_ptr
= NULL
;
489 data
->acls_a_len
= 0;
491 if (st
&& st
->acls_d_ptr
)
493 data
->acls_d_ptr
= xmemdup (st
->acls_d_ptr
, st
->acls_d_len
+ 1);
494 data
->acls_d_len
= st
->acls_d_len
;
498 data
->acls_d_ptr
= NULL
;
499 data
->acls_d_len
= 0;
502 xheader_xattr_copy (st
, &data
->xattr_map
, &data
->xattr_map_size
);
505 data
->xattr_map
= NULL
;
506 data
->xattr_map_size
= 0;
508 strcpy (data
->file_name
, file_name
);
509 delayed_set_stat_head
= data
;
510 if (must_be_dot_or_slash (file_name
))
511 mark_after_links (data
);
514 /* Update the delayed_set_stat info for an intermediate directory
515 created within the file name of DIR. The intermediate directory turned
516 out to be the same as this directory, e.g. due to ".." or symbolic
517 links. *DIR_STAT_INFO is the status of the directory. */
519 repair_delayed_set_stat (char const *dir
,
520 struct stat
const *dir_stat_info
)
522 struct delayed_set_stat
*data
;
523 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
526 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
528 stat_error (data
->file_name
);
532 if (st
.st_dev
== dir_stat_info
->st_dev
533 && st
.st_ino
== dir_stat_info
->st_ino
)
535 data
->dev
= current_stat_info
.stat
.st_dev
;
536 data
->ino
= current_stat_info
.stat
.st_ino
;
537 data
->mode
= current_stat_info
.stat
.st_mode
;
538 data
->uid
= current_stat_info
.stat
.st_uid
;
539 data
->gid
= current_stat_info
.stat
.st_gid
;
540 data
->atime
= current_stat_info
.atime
;
541 data
->mtime
= current_stat_info
.mtime
;
542 data
->current_mode
= st
.st_mode
;
543 data
->current_mode_mask
= ALL_MODE_BITS
;
544 data
->interdir
= false;
549 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
550 quotearg_colon (dir
)));
553 /* After a file/link/directory creation has failed, see if
554 it's because some required directory was not present, and if so,
555 create all required directories. Return zero if all the required
556 directories were created, nonzero (issuing a diagnostic) otherwise.
557 Set *INTERDIR_MADE if at least one directory was created. */
559 make_directories (char *file_name
, bool *interdir_made
)
561 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
562 char *cursor
; /* points into the file name */
564 for (cursor
= cursor0
; *cursor
; cursor
++)
570 if (! ISSLASH (*cursor
))
573 /* Avoid mkdir of empty string, if leading or double '/'. */
575 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
578 /* Avoid mkdir where last part of file name is "." or "..". */
580 if (cursor
[-1] == '.'
581 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
582 || (cursor
[-2] == '.'
583 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
586 *cursor
= '\0'; /* truncate the name there */
587 desired_mode
= MODE_RWX
& ~ newdir_umask
;
588 mode
= desired_mode
| (we_are_root
? 0 : MODE_WXUSR
);
589 status
= mkdirat (chdir_fd
, file_name
, mode
);
593 /* Create a struct delayed_set_stat even if
594 mode == desired_mode, because
595 repair_delayed_set_stat may need to update the struct. */
596 delay_set_stat (file_name
,
597 0, mode
& ~ current_umask
, MODE_RWX
,
598 desired_mode
, AT_SYMLINK_NOFOLLOW
);
600 print_for_mkdir (file_name
, cursor
- file_name
, desired_mode
);
601 *interdir_made
= true;
603 else if (errno
== EEXIST
)
607 /* Check whether the desired file exists. Even when the
608 file exists, mkdir can fail with some errno value E other
609 than EEXIST, so long as E describes an error condition
610 that also applies. */
613 status
= fstatat (chdir_fd
, file_name
, &st
, 0);
617 mkdir_error (file_name
);
629 /* Return true if FILE_NAME (with status *STP, if STP) is not a
630 directory, and has a time stamp newer than (or equal to) that of
633 file_newer_p (const char *file_name
, struct stat
const *stp
,
634 struct tar_stat_info
*tar_stat
)
640 if (deref_stat (file_name
, &st
) != 0)
644 stat_warn (file_name
);
645 /* Be safer: if the file exists, assume it is newer. */
653 return (! S_ISDIR (stp
->st_mode
)
654 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (stp
)) <= 0);
659 #define RECOVER_SKIP 2
661 /* Attempt repairing what went wrong with the extraction. Delete an
662 already existing file or create missing intermediate directories.
663 Return RECOVER_OK if we somewhat increased our chances at a successful
664 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
665 caller should skip extraction of that member. The value of errno is
666 properly restored on returning RECOVER_NO.
668 If REGULAR, the caller was trying to extract onto a regular file.
670 Set *INTERDIR_MADE if an intermediate directory is made as part of
671 the recovery process. */
674 maybe_recoverable (char *file_name
, bool regular
, bool *interdir_made
)
678 struct stat
const *stp
= 0;
687 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
688 but some operating systems do not conform to the standard. */
690 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
693 /* FreeBSD 8.1 uses errno == EMLINK. */
695 /* Tru64 5.1B uses errno == ENOTSUP. */
699 || old_files_option
!= OVERWRITE_OLD_FILES
|| dereference_option
)
701 if (strchr (file_name
, '/'))
703 if (deref_stat (file_name
, &st
) != 0)
708 /* The caller tried to open a symbolic link with O_NOFOLLOW.
709 Fall through, treating it as an already-existing file. */
712 /* Remove an old file, if the options allow this. */
714 switch (old_files_option
)
717 WARNOPT (WARN_EXISTING_FILE
,
718 (0, 0, _("%s: skipping existing file"), file_name
));
724 case KEEP_NEWER_FILES
:
725 if (file_newer_p (file_name
, stp
, ¤t_stat_info
))
729 case DEFAULT_OLD_FILES
:
730 case NO_OVERWRITE_DIR_OLD_FILES
:
731 case OVERWRITE_OLD_FILES
:
732 if (0 < remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
))
736 case UNLINK_FIRST_OLD_FILES
:
741 /* Attempt creating missing intermediate directories. */
742 if (make_directories (file_name
, interdir_made
) == 0 && *interdir_made
)
747 /* Just say we can't do anything about it... */
755 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
756 given in *ST. Restore before extraction because they may affect file layout
757 (e.g. on Lustre distributed parallel filesystem - setting info about how many
758 servers is this file striped over, stripe size, mirror copies, etc.
759 in advance dramatically improves the following performance of reading and
760 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
761 bits from the file's current permissions. TYPEFLAG specifies the type of the
762 file. FILE_CREATED indicates set_xattr has created the file */
764 set_xattr (char const *file_name
, struct tar_stat_info
const *st
,
765 mode_t invert_permissions
, char typeflag
, int *file_created
)
770 bool interdir_made
= false;
772 if ((xattrs_option
> 0) && st
->xattr_map_size
)
774 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
777 status
= mknodat (chdir_fd
, file_name
, mode
^ invert_permissions
, 0);
778 while (status
&& maybe_recoverable ((char *)file_name
, false,
781 xattrs_xattrs_set (st
, file_name
, typeflag
, 0);
789 /* Fix the statuses of all directories whose statuses need fixing, and
790 which are not ancestors of FILE_NAME. If AFTER_LINKS is
791 nonzero, do this for all such directories; otherwise, stop at the
792 first directory that is marked to be fixed up only after delayed
793 links are applied. */
795 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
797 size_t file_name_len
= strlen (file_name
);
798 bool check_for_renamed_directories
= 0;
800 while (delayed_set_stat_head
)
802 struct delayed_set_stat
*data
= delayed_set_stat_head
;
803 bool skip_this_one
= 0;
805 mode_t current_mode
= data
->current_mode
;
806 mode_t current_mode_mask
= data
->current_mode_mask
;
808 check_for_renamed_directories
|= data
->after_links
;
810 if (after_links
< data
->after_links
811 || (data
->file_name_len
< file_name_len
812 && file_name
[data
->file_name_len
]
813 && (ISSLASH (file_name
[data
->file_name_len
])
814 || ISSLASH (file_name
[data
->file_name_len
- 1]))
815 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
818 chdir_do (data
->change_dir
);
820 if (check_for_renamed_directories
)
822 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
824 stat_error (data
->file_name
);
829 current_mode
= st
.st_mode
;
830 current_mode_mask
= ALL_MODE_BITS
;
831 if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
834 _("%s: Directory renamed before its status could be extracted"),
835 quotearg_colon (data
->file_name
)));
843 struct tar_stat_info sb
;
844 sb
.stat
.st_mode
= data
->mode
;
845 sb
.stat
.st_uid
= data
->uid
;
846 sb
.stat
.st_gid
= data
->gid
;
847 sb
.atime
= data
->atime
;
848 sb
.mtime
= data
->mtime
;
849 sb
.cntx_name
= data
->cntx_name
;
850 sb
.acls_a_ptr
= data
->acls_a_ptr
;
851 sb
.acls_a_len
= data
->acls_a_len
;
852 sb
.acls_d_ptr
= data
->acls_d_ptr
;
853 sb
.acls_d_len
= data
->acls_d_len
;
854 sb
.xattr_map
= data
->xattr_map
;
855 sb
.xattr_map_size
= data
->xattr_map_size
;
856 set_stat (data
->file_name
, &sb
,
857 -1, current_mode
, current_mode_mask
,
858 DIRTYPE
, data
->interdir
, data
->atflag
);
861 delayed_set_stat_head
= data
->next
;
862 xheader_xattr_free (data
->xattr_map
, data
->xattr_map_size
);
863 free (data
->cntx_name
);
864 free (data
->acls_a_ptr
);
865 free (data
->acls_d_ptr
);
872 is_directory_link (const char *file_name
)
878 res
= (fstatat (chdir_fd
, file_name
, &st
, AT_SYMLINK_NOFOLLOW
) == 0 &&
879 S_ISLNK (st
.st_mode
) &&
880 fstatat (chdir_fd
, file_name
, &st
, 0) == 0 &&
881 S_ISDIR (st
.st_mode
));
886 /* Extractor functions for various member types */
889 extract_dir (char *file_name
, int typeflag
)
893 mode_t current_mode
= 0;
894 mode_t current_mode_mask
= 0;
896 bool interdir_made
= false;
898 /* Save 'root device' to avoid purging mount points. */
899 if (one_file_system_option
&& root_device
== 0)
903 if (fstatat (chdir_fd
, ".", &st
, 0) != 0)
906 root_device
= st
.st_dev
;
909 if (incremental_option
)
910 /* Read the entry and delete files that aren't listed in the archive. */
911 purge_directory (file_name
);
912 else if (typeflag
== GNUTYPE_DUMPDIR
)
915 /* If ownership or permissions will be restored later, create the
916 directory with restrictive permissions at first, so that in the
917 meantime processes owned by other users do not inadvertently
918 create files under this directory that inherit the wrong owner,
919 group, or permissions from the directory. If not root, though,
920 make the directory writeable and searchable at first, so that
921 files can be created under it. */
922 mode
= ((current_stat_info
.stat
.st_mode
923 & (0 < same_owner_option
|| 0 < same_permissions_option
926 | (we_are_root
? 0 : MODE_WXUSR
));
930 status
= mkdirat (chdir_fd
, file_name
, mode
);
933 current_mode
= mode
& ~ current_umask
;
934 current_mode_mask
= MODE_RWX
;
935 atflag
= AT_SYMLINK_NOFOLLOW
;
941 || keep_directory_symlink_option
942 || old_files_option
== DEFAULT_OLD_FILES
943 || old_files_option
== OVERWRITE_OLD_FILES
))
947 if (keep_directory_symlink_option
&& is_directory_link (file_name
))
950 if (deref_stat (file_name
, &st
) == 0)
952 current_mode
= st
.st_mode
;
953 current_mode_mask
= ALL_MODE_BITS
;
955 if (S_ISDIR (current_mode
))
959 repair_delayed_set_stat (file_name
, &st
);
968 switch (maybe_recoverable (file_name
, false, &interdir_made
))
979 mkdir_error (file_name
);
988 || old_files_option
== DEFAULT_OLD_FILES
989 || old_files_option
== OVERWRITE_OLD_FILES
)
990 delay_set_stat (file_name
, ¤t_stat_info
,
991 current_mode
, current_mode_mask
,
992 current_stat_info
.stat
.st_mode
, atflag
);
999 open_output_file (char const *file_name
, int typeflag
, mode_t mode
,
1000 int file_created
, mode_t
*current_mode
,
1001 mode_t
*current_mode_mask
)
1004 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
1005 int openflag
= (O_WRONLY
| O_BINARY
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
1007 | (overwriting_old_files
1008 ? O_TRUNC
| (dereference_option
? 0 : O_NOFOLLOW
)
1011 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
1013 openflag
= openflag
& ~O_EXCL
;
1015 if (typeflag
== CONTTYPE
)
1017 static int conttype_diagnosed
;
1019 if (!conttype_diagnosed
)
1021 conttype_diagnosed
= 1;
1022 WARNOPT (WARN_CONTIGUOUS_CAST
,
1023 (0, 0, _("Extracting contiguous files as regular files")));
1027 /* If O_NOFOLLOW is needed but does not work, check for a symlink
1028 separately. There's a race condition, but that cannot be avoided
1029 on hosts lacking O_NOFOLLOW. */
1030 if (! HAVE_WORKING_O_NOFOLLOW
1031 && overwriting_old_files
&& ! dereference_option
)
1034 if (fstatat (chdir_fd
, file_name
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
1035 && S_ISLNK (st
.st_mode
))
1042 fd
= openat (chdir_fd
, file_name
, openflag
, mode
);
1045 if (overwriting_old_files
)
1048 if (fstat (fd
, &st
) != 0)
1055 if (! S_ISREG (st
.st_mode
))
1061 *current_mode
= st
.st_mode
;
1062 *current_mode_mask
= ALL_MODE_BITS
;
1066 *current_mode
= mode
& ~ current_umask
;
1067 *current_mode_mask
= MODE_RWX
;
1075 extract_file (char *file_name
, int typeflag
)
1079 union block
*data_block
;
1083 bool interdir_made
= false;
1084 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1085 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1086 mode_t invert_permissions
= 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
)
1088 mode_t current_mode
= 0;
1089 mode_t current_mode_mask
= 0;
1091 if (to_stdout_option
)
1093 else if (to_command_option
)
1095 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
1104 int file_created
= 0;
1105 if (set_xattr (file_name
, ¤t_stat_info
, invert_permissions
,
1106 typeflag
, &file_created
))
1109 open_error (file_name
);
1113 while ((fd
= open_output_file (file_name
, typeflag
, mode
,
1114 file_created
, ¤t_mode
,
1115 ¤t_mode_mask
))
1118 int recover
= maybe_recoverable (file_name
, true, &interdir_made
);
1119 if (recover
!= RECOVER_OK
)
1122 if (recover
== RECOVER_SKIP
)
1124 open_error (file_name
);
1130 mv_begin_read (¤t_stat_info
);
1131 if (current_stat_info
.is_sparse
)
1132 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
1134 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
1136 mv_size_left (size
);
1138 /* Locate data, determine max length writeable, write it,
1139 block that we have used the data, then check if the write
1142 data_block
= find_next_block ();
1145 ERROR ((0, 0, _("Unexpected EOF in archive")));
1146 break; /* FIXME: What happens, then? */
1149 written
= available_space_after (data_block
);
1154 count
= blocking_write (fd
, data_block
->buffer
, written
);
1157 set_next_block_after ((union block
*)
1158 (data_block
->buffer
+ written
- 1));
1159 if (count
!= written
)
1161 if (!to_command_option
)
1162 write_error_details (file_name
, count
, written
);
1163 /* FIXME: shouldn't we restore from backup? */
1172 /* If writing to stdout, don't try to do anything to the filename;
1173 it doesn't exist, or we don't want to touch it anyway. */
1175 if (to_stdout_option
)
1178 if (! to_command_option
)
1179 set_stat (file_name
, ¤t_stat_info
, fd
,
1180 current_mode
, current_mode_mask
, typeflag
, false,
1181 (old_files_option
== OVERWRITE_OLD_FILES
1182 ? 0 : AT_SYMLINK_NOFOLLOW
));
1184 status
= close (fd
);
1186 close_error (file_name
);
1188 if (to_command_option
)
1189 sys_wait_command ();
1194 /* Create a placeholder file with name FILE_NAME, which will be
1195 replaced after other extraction is done by a symbolic link if
1196 IS_SYMLINK is true, and by a hard link otherwise. Set
1197 *INTERDIR_MADE if an intermediate directory is made in the
1201 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
1206 while ((fd
= openat (chdir_fd
, file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
1208 switch (maybe_recoverable (file_name
, false, interdir_made
))
1217 open_error (file_name
);
1222 if (fstat (fd
, &st
) != 0)
1224 stat_error (file_name
);
1227 else if (close (fd
) != 0)
1228 close_error (file_name
);
1231 struct delayed_set_stat
*h
;
1232 struct delayed_link
*p
=
1233 xmalloc (offsetof (struct delayed_link
, target
)
1234 + strlen (current_stat_info
.link_name
)
1236 p
->next
= delayed_link_head
;
1237 delayed_link_head
= p
;
1240 p
->birthtime
= get_stat_birthtime (&st
);
1241 p
->is_symlink
= is_symlink
;
1244 p
->mode
= current_stat_info
.stat
.st_mode
;
1245 p
->uid
= current_stat_info
.stat
.st_uid
;
1246 p
->gid
= current_stat_info
.stat
.st_gid
;
1247 p
->atime
= current_stat_info
.atime
;
1248 p
->mtime
= current_stat_info
.mtime
;
1250 p
->change_dir
= chdir_current
;
1251 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
1252 + strlen (file_name
) + 1);
1253 p
->sources
->next
= 0;
1254 strcpy (p
->sources
->string
, file_name
);
1255 p
->cntx_name
= NULL
;
1256 assign_string (&p
->cntx_name
, current_stat_info
.cntx_name
);
1257 p
->acls_a_ptr
= NULL
;
1259 p
->acls_d_ptr
= NULL
;
1261 xheader_xattr_copy (¤t_stat_info
, &p
->xattr_map
, &p
->xattr_map_size
);
1262 strcpy (p
->target
, current_stat_info
.link_name
);
1264 h
= delayed_set_stat_head
;
1265 if (h
&& ! h
->after_links
1266 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
1267 && ISSLASH (file_name
[h
->file_name_len
])
1268 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
1269 mark_after_links (h
);
1278 extract_link (char *file_name
, int typeflag
)
1280 bool interdir_made
= false;
1281 char const *link_name
;
1284 link_name
= current_stat_info
.link_name
;
1286 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1287 return create_placeholder_file (file_name
, false, &interdir_made
);
1291 struct stat st1
, st2
;
1293 int status
= linkat (chdir_fd
, link_name
, chdir_fd
, file_name
, 0);
1298 struct delayed_link
*ds
= delayed_link_head
;
1300 && fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
) == 0)
1301 for (; ds
; ds
= ds
->next
)
1302 if (ds
->change_dir
== chdir_current
1303 && ds
->dev
== st1
.st_dev
1304 && ds
->ino
== st1
.st_ino
1305 && (timespec_cmp (ds
->birthtime
, get_stat_birthtime (&st1
))
1308 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1309 + strlen (file_name
) + 1);
1310 strcpy (p
->string
, file_name
);
1311 p
->next
= ds
->sources
;
1317 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1318 || ((fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
)
1320 && (fstatat (chdir_fd
, file_name
, &st2
, AT_SYMLINK_NOFOLLOW
)
1322 && st1
.st_dev
== st2
.st_dev
1323 && st1
.st_ino
== st2
.st_ino
))
1328 while ((rc
= maybe_recoverable (file_name
, false, &interdir_made
))
1331 if (rc
== RECOVER_SKIP
)
1333 if (!(incremental_option
&& errno
== EEXIST
))
1335 link_error (link_name
, file_name
);
1342 extract_symlink (char *file_name
, int typeflag
)
1345 bool interdir_made
= false;
1347 if (! absolute_names_option
1348 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1349 || contains_dot_dot (current_stat_info
.link_name
)))
1350 return create_placeholder_file (file_name
, true, &interdir_made
);
1352 while (symlinkat (current_stat_info
.link_name
, chdir_fd
, file_name
) != 0)
1353 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1362 symlink_error (current_stat_info
.link_name
, file_name
);
1366 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1367 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1371 static int warned_once
;
1376 WARNOPT (WARN_SYMLINK_CAST
,
1378 _("Attempting extraction of symbolic links as hard links")));
1380 return extract_link (file_name
, typeflag
);
1384 #if S_IFCHR || S_IFBLK
1386 extract_node (char *file_name
, int typeflag
)
1388 bool interdir_made
= false;
1389 mode_t mode
= (current_stat_info
.stat
.st_mode
& (MODE_RWX
| S_IFBLK
| S_IFCHR
)
1390 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1392 while (mknodat (chdir_fd
, file_name
, mode
, current_stat_info
.stat
.st_rdev
)
1394 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1403 mknod_error (file_name
);
1407 set_stat (file_name
, ¤t_stat_info
, -1,
1408 mode
& ~ current_umask
, MODE_RWX
,
1409 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1414 #if HAVE_MKFIFO || defined mkfifo
1416 extract_fifo (char *file_name
, int typeflag
)
1418 bool interdir_made
= false;
1419 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1420 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1422 while (mkfifoat (chdir_fd
, file_name
, mode
) != 0)
1423 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1432 mkfifo_error (file_name
);
1436 set_stat (file_name
, ¤t_stat_info
, -1,
1437 mode
& ~ current_umask
, MODE_RWX
,
1438 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1444 extract_volhdr (char *file_name
, int typeflag
)
1451 extract_failure (char *file_name
, int typeflag
)
1457 extract_skip (char *file_name
, int typeflag
)
1463 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1467 /* Prepare to extract a file. Find extractor function.
1468 Return zero if extraction should not proceed. */
1471 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1475 if (EXTRACT_OVER_PIPE
)
1478 /* Select the extractor */
1481 case GNUTYPE_SPARSE
:
1482 *fun
= extract_file
;
1489 /* Appears to be a file. But BSD tar uses the convention that a slash
1490 suffix means a directory. */
1491 if (current_stat_info
.had_trailing_slash
)
1495 *fun
= extract_file
;
1501 *fun
= extract_symlink
;
1505 *fun
= extract_link
;
1510 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1511 *fun
= extract_node
;
1517 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1518 *fun
= extract_node
;
1522 #if HAVE_MKFIFO || defined mkfifo
1524 *fun
= extract_fifo
;
1529 case GNUTYPE_DUMPDIR
:
1531 if (current_stat_info
.is_dumpdir
)
1532 delay_directory_restore_option
= true;
1535 case GNUTYPE_VOLHDR
:
1536 *fun
= extract_volhdr
;
1539 case GNUTYPE_MULTIVOL
:
1541 _("%s: Cannot extract -- file is continued from another volume"),
1542 quotearg_colon (current_stat_info
.file_name
)));
1543 *fun
= extract_skip
;
1546 case GNUTYPE_LONGNAME
:
1547 case GNUTYPE_LONGLINK
:
1548 ERROR ((0, 0, _("Unexpected long name header")));
1549 *fun
= extract_failure
;
1553 WARNOPT (WARN_UNKNOWN_CAST
,
1555 _("%s: Unknown file type '%c', extracted as normal file"),
1556 quotearg_colon (file_name
), typeflag
));
1557 *fun
= extract_file
;
1560 /* Determine whether the extraction should proceed */
1564 switch (old_files_option
)
1566 case UNLINK_FIRST_OLD_FILES
:
1567 if (!remove_any_file (file_name
,
1568 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1569 : ORDINARY_REMOVE_OPTION
)
1570 && errno
&& errno
!= ENOENT
)
1572 unlink_error (file_name
);
1577 case KEEP_NEWER_FILES
:
1578 if (file_newer_p (file_name
, 0, ¤t_stat_info
))
1580 WARNOPT (WARN_IGNORE_NEWER
,
1581 (0, 0, _("Current %s is newer or same age"),
1582 quote (file_name
)));
1594 /* Extract a file from the archive. */
1596 extract_archive (void)
1599 tar_extractor_t fun
;
1601 fatal_exit_hook
= extract_finish
;
1603 set_next_block_after (current_header
);
1605 if (!current_stat_info
.file_name
[0]
1606 || (interactive_option
1607 && !confirm ("extract", current_stat_info
.file_name
)))
1613 /* Print the block from current_header and current_stat. */
1615 print_header (¤t_stat_info
, current_header
, -1);
1617 /* Restore stats for all non-ancestor directories, unless
1618 it is an incremental archive.
1619 (see NOTICE in the comment to delay_set_stat above) */
1620 if (!delay_directory_restore_option
)
1622 int dir
= chdir_current
;
1623 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1627 /* Take a safety backup of a previously existing file. */
1630 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1633 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1634 quotearg_colon (current_stat_info
.file_name
)));
1639 /* Extract the archive entry according to its type. */
1641 typeflag
= sparse_member_p (¤t_stat_info
) ?
1642 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1644 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1646 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1648 undo_last_backup ();
1655 /* Extract the links whose final extraction were delayed. */
1657 apply_delayed_links (void)
1659 struct delayed_link
*ds
;
1661 for (ds
= delayed_link_head
; ds
; )
1663 struct string_list
*sources
= ds
->sources
;
1664 char const *valid_source
= 0;
1666 chdir_do (ds
->change_dir
);
1668 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1670 char const *source
= sources
->string
;
1673 /* Make sure the placeholder file is still there. If not,
1674 don't create a link, as the placeholder was probably
1675 removed by a later extraction. */
1676 if (fstatat (chdir_fd
, source
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
1677 && st
.st_dev
== ds
->dev
1678 && st
.st_ino
== ds
->ino
1679 && timespec_cmp (get_stat_birthtime (&st
), ds
->birthtime
) == 0)
1681 /* Unlink the placeholder, then create a hard link if possible,
1682 a symbolic link otherwise. */
1683 if (unlinkat (chdir_fd
, source
, 0) != 0)
1684 unlink_error (source
);
1685 else if (valid_source
1686 && (linkat (chdir_fd
, valid_source
, chdir_fd
, source
, 0)
1689 else if (!ds
->is_symlink
)
1691 if (linkat (chdir_fd
, ds
->target
, chdir_fd
, source
, 0) != 0)
1692 link_error (ds
->target
, source
);
1694 else if (symlinkat (ds
->target
, chdir_fd
, source
) != 0)
1695 symlink_error (ds
->target
, source
);
1698 struct tar_stat_info st1
;
1699 st1
.stat
.st_mode
= ds
->mode
;
1700 st1
.stat
.st_uid
= ds
->uid
;
1701 st1
.stat
.st_gid
= ds
->gid
;
1702 st1
.atime
= ds
->atime
;
1703 st1
.mtime
= ds
->mtime
;
1704 st1
.cntx_name
= ds
->cntx_name
;
1705 st1
.acls_a_ptr
= ds
->acls_a_ptr
;
1706 st1
.acls_a_len
= ds
->acls_a_len
;
1707 st1
.acls_d_ptr
= ds
->acls_d_ptr
;
1708 st1
.acls_d_len
= ds
->acls_d_len
;
1709 st1
.xattr_map
= ds
->xattr_map
;
1710 st1
.xattr_map_size
= ds
->xattr_map_size
;
1711 set_stat (source
, &st1
, -1, 0, 0, SYMTYPE
,
1712 false, AT_SYMLINK_NOFOLLOW
);
1713 valid_source
= source
;
1718 for (sources
= ds
->sources
; sources
; )
1720 struct string_list
*next
= sources
->next
;
1725 xheader_xattr_free (ds
->xattr_map
, ds
->xattr_map_size
);
1726 free (ds
->cntx_name
);
1729 struct delayed_link
*next
= ds
->next
;
1735 delayed_link_head
= 0;
1738 /* Finish the extraction of an archive. */
1740 extract_finish (void)
1742 /* First, fix the status of ordinary directories that need fixing. */
1743 apply_nonancestor_delayed_set_stat ("", 0);
1745 /* Then, apply delayed links, so that they don't affect delayed
1746 directory status-setting for ordinary directories. */
1747 apply_delayed_links ();
1749 /* Finally, fix the status of directories that are ancestors
1750 of delayed links. */
1751 apply_nonancestor_delayed_set_stat ("", 1);
1755 rename_directory (char *src
, char *dst
)
1757 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) != 0)
1765 if (make_directories (dst
, &interdir_made
) == 0)
1767 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) == 0)
1774 /* FIXME: Fall back to recursive copying */
1780 ERROR ((0, e
, _("Cannot rename %s to %s"),