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
;
196 /* Use fchmod if possible, fchmodat otherwise. */
198 fd_chmod (int fd
, char const *file
, mode_t mode
, int atflag
)
202 int result
= fchmod (fd
, mode
);
203 if (result
== 0 || implemented (errno
))
206 return fchmodat (chdir_fd
, file
, mode
, atflag
);
209 /* Use fchown if possible, fchownat otherwise. */
211 fd_chown (int fd
, char const *file
, uid_t uid
, gid_t gid
, int atflag
)
215 int result
= fchown (fd
, uid
, gid
);
216 if (result
== 0 || implemented (errno
))
219 return fchownat (chdir_fd
, file
, uid
, gid
, atflag
);
222 /* Use fstat if possible, fstatat otherwise. */
224 fd_stat (int fd
, char const *file
, struct stat
*st
, int atflag
)
228 : fstatat (chdir_fd
, file
, st
, atflag
));
231 /* Set the mode for FILE_NAME to MODE.
232 MODE_MASK specifies the bits of MODE that we care about;
233 thus if MODE_MASK is zero, do nothing.
234 If FD is nonnegative, it is a file descriptor for the file.
235 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
236 the file's current mode, using the style of struct delayed_set_stat.
237 TYPEFLAG specifies the type of the file.
238 ATFLAG specifies the flag to use when statting the file. */
240 set_mode (char const *file_name
,
241 mode_t mode
, mode_t mode_mask
, int fd
,
242 mode_t current_mode
, mode_t current_mode_mask
,
243 char typeflag
, int atflag
)
245 if (((current_mode
^ mode
) | ~ current_mode_mask
) & mode_mask
)
247 if (MODE_ALL
& ~ mode_mask
& ~ current_mode_mask
)
250 if (fd_stat (fd
, file_name
, &st
, atflag
) != 0)
252 stat_error (file_name
);
255 current_mode
= st
.st_mode
;
258 current_mode
&= MODE_ALL
;
259 mode
= (current_mode
& ~ mode_mask
) | (mode
& mode_mask
);
261 if (current_mode
!= mode
)
264 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
266 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
267 setuid-root files would otherwise be a backdoor. See
268 http://opensolaris.org/jive/thread.jspa?threadID=95826
270 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
)
271 && priv_set_restore_linkdir () == 0)
274 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
275 priv_set_remove_linkdir ();
278 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
279 returns ENOTSUP even when operating on non-symlinks, try
280 again with the flag disabled if it does not appear to be
281 supported and if the file is not a symlink. This
282 introduces a race, alas. */
283 if (atflag
&& typeflag
!= SYMTYPE
&& ! implemented (chmod_errno
))
284 chmod_errno
= fd_chmod (fd
, file_name
, mode
, 0) == 0 ? 0 : errno
;
287 && (typeflag
!= SYMTYPE
|| implemented (chmod_errno
)))
290 chmod_error_details (file_name
, mode
);
296 /* Check time after successfully setting FILE_NAME's time stamp to T. */
298 check_time (char const *file_name
, struct timespec t
)
301 WARNOPT (WARN_TIMESTAMP
,
302 (0, 0, _("%s: implausibly old time stamp %s"),
303 file_name
, tartime (t
, true)));
304 else if (timespec_cmp (volume_start_time
, t
) < 0)
308 if (timespec_cmp (now
, t
) < 0)
310 char buf
[TIMESPEC_STRSIZE_BOUND
];
311 struct timespec diff
;
312 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
313 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
314 if (diff
.tv_nsec
< 0)
316 diff
.tv_nsec
+= BILLION
;
319 WARNOPT (WARN_TIMESTAMP
,
320 (0, 0, _("%s: time stamp %s is %s s in the future"),
321 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
326 /* Restore stat attributes (owner, group, mode and times) for
327 FILE_NAME, using information given in *ST.
328 If FD is nonnegative, it is a file descriptor for the file.
329 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
330 the file's current mode, using the style of struct delayed_set_stat.
331 TYPEFLAG specifies the type of the file.
332 If INTERDIR, this is an intermediate directory.
333 ATFLAG specifies the flag to use when statting the file. */
336 set_stat (char const *file_name
,
337 struct tar_stat_info
const *st
,
338 int fd
, mode_t current_mode
, mode_t current_mode_mask
,
339 char typeflag
, bool interdir
, int atflag
)
341 /* Do the utime before the chmod because some versions of utime are
342 broken and trash the modes of the file. */
344 if (! touch_option
&& ! interdir
)
346 struct timespec ts
[2];
347 if (incremental_option
)
350 ts
[0].tv_nsec
= UTIME_OMIT
;
353 if (fdutimensat (fd
, chdir_fd
, file_name
, ts
, atflag
) == 0)
355 if (incremental_option
)
356 check_time (file_name
, ts
[0]);
357 check_time (file_name
, ts
[1]);
359 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
360 utime_error (file_name
);
363 if (0 < same_owner_option
&& ! interdir
)
365 /* Some systems allow non-root users to give files away. Once this
366 done, it is not possible anymore to change file permissions.
367 However, setting file permissions now would be incorrect, since
368 they would apply to the wrong user, and there would be a race
369 condition. So, don't use systems that allow non-root users to
371 uid_t uid
= st
->stat
.st_uid
;
372 gid_t gid
= st
->stat
.st_gid
;
374 if (fd_chown (fd
, file_name
, uid
, gid
, atflag
) == 0)
376 /* Changing the owner can clear st_mode bits in some cases. */
377 if ((current_mode
| ~ current_mode_mask
) & S_IXUGO
)
378 current_mode_mask
&= ~ (current_mode
& (S_ISUID
| S_ISGID
));
380 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
381 chown_error_details (file_name
, uid
, gid
);
385 st
->stat
.st_mode
& ~ current_umask
,
386 0 < same_permissions_option
&& ! interdir
? MODE_ALL
: MODE_RWX
,
387 fd
, current_mode
, current_mode_mask
, typeflag
, atflag
);
389 /* these three calls must be done *after* fd_chown() call because fd_chown
390 causes that linux capabilities becomes cleared. */
391 xattrs_xattrs_set (st
, file_name
, typeflag
, 1);
392 xattrs_acls_set (st
, file_name
, typeflag
);
393 xattrs_selinux_set (st
, file_name
, typeflag
);
396 /* For each entry H in the leading prefix of entries in HEAD that do
397 not have after_links marked, mark H and fill in its dev and ino
398 members. Assume HEAD && ! HEAD->after_links. */
400 mark_after_links (struct delayed_set_stat
*head
)
402 struct delayed_set_stat
*h
= head
;
409 if (deref_stat (h
->file_name
, &st
) != 0)
410 stat_error (h
->file_name
);
417 while ((h
= h
->next
) && ! h
->after_links
);
420 /* Remember to restore stat attributes (owner, group, mode and times)
421 for the directory FILE_NAME, using information given in *ST,
422 once we stop extracting files into that directory.
424 If ST is null, merely create a placeholder node for an intermediate
425 directory that was created by make_directories.
427 NOTICE: this works only if the archive has usual member order, i.e.
428 directory, then the files in that directory. Incremental archive have
429 somewhat reversed order: first go subdirectories, then all other
430 members. To help cope with this case the variable
431 delay_directory_restore_option is set by prepare_to_extract.
433 If an archive was explicitely created so that its member order is
434 reversed, some directory timestamps can be restored incorrectly,
436 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
439 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
440 mode_t current_mode
, mode_t current_mode_mask
,
441 mode_t mode
, int atflag
)
443 size_t file_name_len
= strlen (file_name
);
444 struct delayed_set_stat
*data
=
445 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
446 + file_name_len
+ 1);
447 data
->next
= delayed_set_stat_head
;
451 data
->dev
= st
->stat
.st_dev
;
452 data
->ino
= st
->stat
.st_ino
;
453 data
->uid
= st
->stat
.st_uid
;
454 data
->gid
= st
->stat
.st_gid
;
455 data
->atime
= st
->atime
;
456 data
->mtime
= st
->mtime
;
458 data
->file_name_len
= file_name_len
;
459 data
->current_mode
= current_mode
;
460 data
->current_mode_mask
= current_mode_mask
;
461 data
->interdir
= ! st
;
462 data
->atflag
= atflag
;
463 data
->after_links
= 0;
464 data
->change_dir
= chdir_current
;
465 data
->cntx_name
= NULL
;
467 assign_string (&data
->cntx_name
, st
->cntx_name
);
468 if (st
&& st
->acls_a_ptr
)
470 data
->acls_a_ptr
= xmemdup (st
->acls_a_ptr
, st
->acls_a_len
+ 1);
471 data
->acls_a_len
= st
->acls_a_len
;
475 data
->acls_a_ptr
= NULL
;
476 data
->acls_a_len
= 0;
478 if (st
&& st
->acls_d_ptr
)
480 data
->acls_d_ptr
= xmemdup (st
->acls_d_ptr
, st
->acls_d_len
+ 1);
481 data
->acls_d_len
= st
->acls_d_len
;
485 data
->acls_d_ptr
= NULL
;
486 data
->acls_d_len
= 0;
489 xheader_xattr_copy (st
, &data
->xattr_map
, &data
->xattr_map_size
);
492 data
->xattr_map
= NULL
;
493 data
->xattr_map_size
= 0;
495 strcpy (data
->file_name
, file_name
);
496 delayed_set_stat_head
= data
;
497 if (must_be_dot_or_slash (file_name
))
498 mark_after_links (data
);
501 /* Update the delayed_set_stat info for an intermediate directory
502 created within the file name of DIR. The intermediate directory turned
503 out to be the same as this directory, e.g. due to ".." or symbolic
504 links. *DIR_STAT_INFO is the status of the directory. */
506 repair_delayed_set_stat (char const *dir
,
507 struct stat
const *dir_stat_info
)
509 struct delayed_set_stat
*data
;
510 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
513 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
515 stat_error (data
->file_name
);
519 if (st
.st_dev
== dir_stat_info
->st_dev
520 && st
.st_ino
== dir_stat_info
->st_ino
)
522 data
->dev
= current_stat_info
.stat
.st_dev
;
523 data
->ino
= current_stat_info
.stat
.st_ino
;
524 data
->mode
= current_stat_info
.stat
.st_mode
;
525 data
->uid
= current_stat_info
.stat
.st_uid
;
526 data
->gid
= current_stat_info
.stat
.st_gid
;
527 data
->atime
= current_stat_info
.atime
;
528 data
->mtime
= current_stat_info
.mtime
;
529 data
->current_mode
= st
.st_mode
;
530 data
->current_mode_mask
= ALL_MODE_BITS
;
531 data
->interdir
= false;
536 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
537 quotearg_colon (dir
)));
540 /* After a file/link/directory creation has failed, see if
541 it's because some required directory was not present, and if so,
542 create all required directories. Return zero if all the required
543 directories were created, nonzero (issuing a diagnostic) otherwise.
544 Set *INTERDIR_MADE if at least one directory was created. */
546 make_directories (char *file_name
, bool *interdir_made
)
548 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
549 char *cursor
; /* points into the file name */
551 for (cursor
= cursor0
; *cursor
; cursor
++)
557 if (! ISSLASH (*cursor
))
560 /* Avoid mkdir of empty string, if leading or double '/'. */
562 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
565 /* Avoid mkdir where last part of file name is "." or "..". */
567 if (cursor
[-1] == '.'
568 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
569 || (cursor
[-2] == '.'
570 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
573 *cursor
= '\0'; /* truncate the name there */
574 desired_mode
= MODE_RWX
& ~ newdir_umask
;
575 mode
= desired_mode
| (we_are_root
? 0 : MODE_WXUSR
);
576 status
= mkdirat (chdir_fd
, file_name
, mode
);
580 /* Create a struct delayed_set_stat even if
581 mode == desired_mode, because
582 repair_delayed_set_stat may need to update the struct. */
583 delay_set_stat (file_name
,
584 0, mode
& ~ current_umask
, MODE_RWX
,
585 desired_mode
, AT_SYMLINK_NOFOLLOW
);
587 print_for_mkdir (file_name
, cursor
- file_name
, desired_mode
);
588 *interdir_made
= true;
590 else if (errno
== EEXIST
)
594 /* Check whether the desired file exists. Even when the
595 file exists, mkdir can fail with some errno value E other
596 than EEXIST, so long as E describes an error condition
597 that also applies. */
600 status
= fstatat (chdir_fd
, file_name
, &st
, 0);
604 mkdir_error (file_name
);
616 /* Return true if FILE_NAME (with status *STP, if STP) is not a
617 directory, and has a time stamp newer than (or equal to) that of
620 file_newer_p (const char *file_name
, struct stat
const *stp
,
621 struct tar_stat_info
*tar_stat
)
627 if (deref_stat (file_name
, &st
) != 0)
631 stat_warn (file_name
);
632 /* Be safer: if the file exists, assume it is newer. */
640 return (! S_ISDIR (stp
->st_mode
)
641 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (stp
)) <= 0);
646 #define RECOVER_SKIP 2
648 /* Attempt repairing what went wrong with the extraction. Delete an
649 already existing file or create missing intermediate directories.
650 Return RECOVER_OK if we somewhat increased our chances at a successful
651 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
652 caller should skip extraction of that member. The value of errno is
653 properly restored on returning RECOVER_NO.
655 If REGULAR, the caller was trying to extract onto a regular file.
657 Set *INTERDIR_MADE if an intermediate directory is made as part of
658 the recovery process. */
661 maybe_recoverable (char *file_name
, bool regular
, bool *interdir_made
)
665 struct stat
const *stp
= 0;
674 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
675 but some operating systems do not conform to the standard. */
677 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
680 /* FreeBSD 8.1 uses errno == EMLINK. */
682 /* Tru64 5.1B uses errno == ENOTSUP. */
686 || old_files_option
!= OVERWRITE_OLD_FILES
|| dereference_option
)
688 if (strchr (file_name
, '/'))
690 if (deref_stat (file_name
, &st
) != 0)
695 /* The caller tried to open a symbolic link with O_NOFOLLOW.
696 Fall through, treating it as an already-existing file. */
699 /* Remove an old file, if the options allow this. */
701 switch (old_files_option
)
704 WARNOPT (WARN_EXISTING_FILE
,
705 (0, 0, _("%s: skipping existing file"), file_name
));
711 case KEEP_NEWER_FILES
:
712 if (file_newer_p (file_name
, stp
, ¤t_stat_info
))
716 case DEFAULT_OLD_FILES
:
717 case NO_OVERWRITE_DIR_OLD_FILES
:
718 case OVERWRITE_OLD_FILES
:
719 if (0 < remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
))
723 case UNLINK_FIRST_OLD_FILES
:
728 /* Attempt creating missing intermediate directories. */
729 if (make_directories (file_name
, interdir_made
) == 0 && *interdir_made
)
734 /* Just say we can't do anything about it... */
742 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
743 given in *ST. Restore before extraction because they may affect file layout
744 (e.g. on Lustre distributed parallel filesystem - setting info about how many
745 servers is this file striped over, stripe size, mirror copies, etc.
746 in advance dramatically improves the following performance of reading and
747 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
748 bits from the file's current permissions. TYPEFLAG specifies the type of the
749 file. FILE_CREATED indicates set_xattr has created the file */
751 set_xattr (char const *file_name
, struct tar_stat_info
const *st
,
752 mode_t invert_permissions
, char typeflag
, int *file_created
)
757 bool interdir_made
= false;
759 if ((xattrs_option
> 0) && st
->xattr_map_size
)
761 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
764 status
= mknodat (chdir_fd
, file_name
, mode
^ invert_permissions
, 0);
765 while (status
&& maybe_recoverable ((char *)file_name
, false,
768 xattrs_xattrs_set (st
, file_name
, typeflag
, 0);
776 /* Fix the statuses of all directories whose statuses need fixing, and
777 which are not ancestors of FILE_NAME. If AFTER_LINKS is
778 nonzero, do this for all such directories; otherwise, stop at the
779 first directory that is marked to be fixed up only after delayed
780 links are applied. */
782 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
784 size_t file_name_len
= strlen (file_name
);
785 bool check_for_renamed_directories
= 0;
787 while (delayed_set_stat_head
)
789 struct delayed_set_stat
*data
= delayed_set_stat_head
;
790 bool skip_this_one
= 0;
792 mode_t current_mode
= data
->current_mode
;
793 mode_t current_mode_mask
= data
->current_mode_mask
;
795 check_for_renamed_directories
|= data
->after_links
;
797 if (after_links
< data
->after_links
798 || (data
->file_name_len
< file_name_len
799 && file_name
[data
->file_name_len
]
800 && (ISSLASH (file_name
[data
->file_name_len
])
801 || ISSLASH (file_name
[data
->file_name_len
- 1]))
802 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
805 chdir_do (data
->change_dir
);
807 if (check_for_renamed_directories
)
809 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
811 stat_error (data
->file_name
);
816 current_mode
= st
.st_mode
;
817 current_mode_mask
= ALL_MODE_BITS
;
818 if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
821 _("%s: Directory renamed before its status could be extracted"),
822 quotearg_colon (data
->file_name
)));
830 struct tar_stat_info sb
;
831 sb
.stat
.st_mode
= data
->mode
;
832 sb
.stat
.st_uid
= data
->uid
;
833 sb
.stat
.st_gid
= data
->gid
;
834 sb
.atime
= data
->atime
;
835 sb
.mtime
= data
->mtime
;
836 sb
.cntx_name
= data
->cntx_name
;
837 sb
.acls_a_ptr
= data
->acls_a_ptr
;
838 sb
.acls_a_len
= data
->acls_a_len
;
839 sb
.acls_d_ptr
= data
->acls_d_ptr
;
840 sb
.acls_d_len
= data
->acls_d_len
;
841 sb
.xattr_map
= data
->xattr_map
;
842 sb
.xattr_map_size
= data
->xattr_map_size
;
843 set_stat (data
->file_name
, &sb
,
844 -1, current_mode
, current_mode_mask
,
845 DIRTYPE
, data
->interdir
, data
->atflag
);
848 delayed_set_stat_head
= data
->next
;
849 xheader_xattr_free (data
->xattr_map
, data
->xattr_map_size
);
850 free (data
->cntx_name
);
851 free (data
->acls_a_ptr
);
852 free (data
->acls_d_ptr
);
859 /* Extractor functions for various member types */
862 extract_dir (char *file_name
, int typeflag
)
866 mode_t current_mode
= 0;
867 mode_t current_mode_mask
= 0;
869 bool interdir_made
= false;
871 /* Save 'root device' to avoid purging mount points. */
872 if (one_file_system_option
&& root_device
== 0)
876 if (fstatat (chdir_fd
, ".", &st
, 0) != 0)
879 root_device
= st
.st_dev
;
882 if (incremental_option
)
883 /* Read the entry and delete files that aren't listed in the archive. */
884 purge_directory (file_name
);
885 else if (typeflag
== GNUTYPE_DUMPDIR
)
888 /* If ownership or permissions will be restored later, create the
889 directory with restrictive permissions at first, so that in the
890 meantime processes owned by other users do not inadvertently
891 create files under this directory that inherit the wrong owner,
892 group, or permissions from the directory. If not root, though,
893 make the directory writeable and searchable at first, so that
894 files can be created under it. */
895 mode
= ((current_stat_info
.stat
.st_mode
896 & (0 < same_owner_option
|| 0 < same_permissions_option
899 | (we_are_root
? 0 : MODE_WXUSR
));
903 status
= mkdirat (chdir_fd
, file_name
, mode
);
906 current_mode
= mode
& ~ current_umask
;
907 current_mode_mask
= MODE_RWX
;
908 atflag
= AT_SYMLINK_NOFOLLOW
;
914 || old_files_option
== DEFAULT_OLD_FILES
915 || old_files_option
== OVERWRITE_OLD_FILES
))
918 if (deref_stat (file_name
, &st
) == 0)
920 current_mode
= st
.st_mode
;
921 current_mode_mask
= ALL_MODE_BITS
;
923 if (S_ISDIR (current_mode
))
927 repair_delayed_set_stat (file_name
, &st
);
936 switch (maybe_recoverable (file_name
, false, &interdir_made
))
947 mkdir_error (file_name
);
956 || old_files_option
== DEFAULT_OLD_FILES
957 || old_files_option
== OVERWRITE_OLD_FILES
)
958 delay_set_stat (file_name
, ¤t_stat_info
,
959 current_mode
, current_mode_mask
,
960 current_stat_info
.stat
.st_mode
, atflag
);
967 open_output_file (char const *file_name
, int typeflag
, mode_t mode
,
968 int file_created
, mode_t
*current_mode
,
969 mode_t
*current_mode_mask
)
972 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
973 int openflag
= (O_WRONLY
| O_BINARY
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
975 | (overwriting_old_files
976 ? O_TRUNC
| (dereference_option
? 0 : O_NOFOLLOW
)
979 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
981 openflag
= openflag
& ~O_EXCL
;
983 if (typeflag
== CONTTYPE
)
985 static int conttype_diagnosed
;
987 if (!conttype_diagnosed
)
989 conttype_diagnosed
= 1;
990 WARNOPT (WARN_CONTIGUOUS_CAST
,
991 (0, 0, _("Extracting contiguous files as regular files")));
995 /* If O_NOFOLLOW is needed but does not work, check for a symlink
996 separately. There's a race condition, but that cannot be avoided
997 on hosts lacking O_NOFOLLOW. */
998 if (! HAVE_WORKING_O_NOFOLLOW
999 && overwriting_old_files
&& ! dereference_option
)
1002 if (fstatat (chdir_fd
, file_name
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
1003 && S_ISLNK (st
.st_mode
))
1010 fd
= openat (chdir_fd
, file_name
, openflag
, mode
);
1013 if (overwriting_old_files
)
1016 if (fstat (fd
, &st
) != 0)
1023 if (! S_ISREG (st
.st_mode
))
1029 *current_mode
= st
.st_mode
;
1030 *current_mode_mask
= ALL_MODE_BITS
;
1034 *current_mode
= mode
& ~ current_umask
;
1035 *current_mode_mask
= MODE_RWX
;
1043 extract_file (char *file_name
, int typeflag
)
1047 union block
*data_block
;
1051 bool interdir_made
= false;
1052 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1053 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1054 mode_t invert_permissions
= 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
)
1056 mode_t current_mode
= 0;
1057 mode_t current_mode_mask
= 0;
1059 if (to_stdout_option
)
1061 else if (to_command_option
)
1063 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
1072 int file_created
= 0;
1073 if (set_xattr (file_name
, ¤t_stat_info
, invert_permissions
,
1074 typeflag
, &file_created
))
1077 open_error (file_name
);
1081 while ((fd
= open_output_file (file_name
, typeflag
, mode
,
1082 file_created
, ¤t_mode
,
1083 ¤t_mode_mask
))
1086 int recover
= maybe_recoverable (file_name
, true, &interdir_made
);
1087 if (recover
!= RECOVER_OK
)
1090 if (recover
== RECOVER_SKIP
)
1092 open_error (file_name
);
1098 mv_begin_read (¤t_stat_info
);
1099 if (current_stat_info
.is_sparse
)
1100 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
1102 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
1104 mv_size_left (size
);
1106 /* Locate data, determine max length writeable, write it,
1107 block that we have used the data, then check if the write
1110 data_block
= find_next_block ();
1113 ERROR ((0, 0, _("Unexpected EOF in archive")));
1114 break; /* FIXME: What happens, then? */
1117 written
= available_space_after (data_block
);
1122 count
= blocking_write (fd
, data_block
->buffer
, written
);
1125 set_next_block_after ((union block
*)
1126 (data_block
->buffer
+ written
- 1));
1127 if (count
!= written
)
1129 if (!to_command_option
)
1130 write_error_details (file_name
, count
, written
);
1131 /* FIXME: shouldn't we restore from backup? */
1140 /* If writing to stdout, don't try to do anything to the filename;
1141 it doesn't exist, or we don't want to touch it anyway. */
1143 if (to_stdout_option
)
1146 if (! to_command_option
)
1147 set_stat (file_name
, ¤t_stat_info
, fd
,
1148 current_mode
, current_mode_mask
, typeflag
, false,
1149 (old_files_option
== OVERWRITE_OLD_FILES
1150 ? 0 : AT_SYMLINK_NOFOLLOW
));
1152 status
= close (fd
);
1154 close_error (file_name
);
1156 if (to_command_option
)
1157 sys_wait_command ();
1162 /* Create a placeholder file with name FILE_NAME, which will be
1163 replaced after other extraction is done by a symbolic link if
1164 IS_SYMLINK is true, and by a hard link otherwise. Set
1165 *INTERDIR_MADE if an intermediate directory is made in the
1169 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
1174 while ((fd
= openat (chdir_fd
, file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
1176 switch (maybe_recoverable (file_name
, false, interdir_made
))
1185 open_error (file_name
);
1190 if (fstat (fd
, &st
) != 0)
1192 stat_error (file_name
);
1195 else if (close (fd
) != 0)
1196 close_error (file_name
);
1199 struct delayed_set_stat
*h
;
1200 struct delayed_link
*p
=
1201 xmalloc (offsetof (struct delayed_link
, target
)
1202 + strlen (current_stat_info
.link_name
)
1204 p
->next
= delayed_link_head
;
1205 delayed_link_head
= p
;
1208 p
->birthtime
= get_stat_birthtime (&st
);
1209 p
->is_symlink
= is_symlink
;
1212 p
->mode
= current_stat_info
.stat
.st_mode
;
1213 p
->uid
= current_stat_info
.stat
.st_uid
;
1214 p
->gid
= current_stat_info
.stat
.st_gid
;
1215 p
->atime
= current_stat_info
.atime
;
1216 p
->mtime
= current_stat_info
.mtime
;
1218 p
->change_dir
= chdir_current
;
1219 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
1220 + strlen (file_name
) + 1);
1221 p
->sources
->next
= 0;
1222 strcpy (p
->sources
->string
, file_name
);
1223 p
->cntx_name
= NULL
;
1224 assign_string (&p
->cntx_name
, current_stat_info
.cntx_name
);
1225 p
->acls_a_ptr
= NULL
;
1227 p
->acls_d_ptr
= NULL
;
1229 xheader_xattr_copy (¤t_stat_info
, &p
->xattr_map
, &p
->xattr_map_size
);
1230 strcpy (p
->target
, current_stat_info
.link_name
);
1232 h
= delayed_set_stat_head
;
1233 if (h
&& ! h
->after_links
1234 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
1235 && ISSLASH (file_name
[h
->file_name_len
])
1236 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
1237 mark_after_links (h
);
1246 extract_link (char *file_name
, int typeflag
)
1248 bool interdir_made
= false;
1249 char const *link_name
;
1252 link_name
= current_stat_info
.link_name
;
1254 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1255 return create_placeholder_file (file_name
, false, &interdir_made
);
1259 struct stat st1
, st2
;
1261 int status
= linkat (chdir_fd
, link_name
, chdir_fd
, file_name
, 0);
1266 struct delayed_link
*ds
= delayed_link_head
;
1268 && fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
) == 0)
1269 for (; ds
; ds
= ds
->next
)
1270 if (ds
->change_dir
== chdir_current
1271 && ds
->dev
== st1
.st_dev
1272 && ds
->ino
== st1
.st_ino
1273 && (timespec_cmp (ds
->birthtime
, get_stat_birthtime (&st1
))
1276 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1277 + strlen (file_name
) + 1);
1278 strcpy (p
->string
, file_name
);
1279 p
->next
= ds
->sources
;
1285 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1286 || ((fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
)
1288 && (fstatat (chdir_fd
, file_name
, &st2
, AT_SYMLINK_NOFOLLOW
)
1290 && st1
.st_dev
== st2
.st_dev
1291 && st1
.st_ino
== st2
.st_ino
))
1296 while ((rc
= maybe_recoverable (file_name
, false, &interdir_made
))
1299 if (rc
== RECOVER_SKIP
)
1301 if (!(incremental_option
&& errno
== EEXIST
))
1303 link_error (link_name
, file_name
);
1310 extract_symlink (char *file_name
, int typeflag
)
1313 bool interdir_made
= false;
1315 if (! absolute_names_option
1316 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1317 || contains_dot_dot (current_stat_info
.link_name
)))
1318 return create_placeholder_file (file_name
, true, &interdir_made
);
1320 while (symlinkat (current_stat_info
.link_name
, chdir_fd
, file_name
) != 0)
1321 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1330 symlink_error (current_stat_info
.link_name
, file_name
);
1334 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1335 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1339 static int warned_once
;
1344 WARNOPT (WARN_SYMLINK_CAST
,
1346 _("Attempting extraction of symbolic links as hard links")));
1348 return extract_link (file_name
, typeflag
);
1352 #if S_IFCHR || S_IFBLK
1354 extract_node (char *file_name
, int typeflag
)
1356 bool interdir_made
= false;
1357 mode_t mode
= (current_stat_info
.stat
.st_mode
& (MODE_RWX
| S_IFBLK
| S_IFCHR
)
1358 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1360 while (mknodat (chdir_fd
, file_name
, mode
, current_stat_info
.stat
.st_rdev
)
1362 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1371 mknod_error (file_name
);
1375 set_stat (file_name
, ¤t_stat_info
, -1,
1376 mode
& ~ current_umask
, MODE_RWX
,
1377 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1382 #if HAVE_MKFIFO || defined mkfifo
1384 extract_fifo (char *file_name
, int typeflag
)
1386 bool interdir_made
= false;
1387 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1388 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1390 while (mkfifoat (chdir_fd
, file_name
, mode
) != 0)
1391 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1400 mkfifo_error (file_name
);
1404 set_stat (file_name
, ¤t_stat_info
, -1,
1405 mode
& ~ current_umask
, MODE_RWX
,
1406 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1412 extract_volhdr (char *file_name
, int typeflag
)
1419 extract_failure (char *file_name
, int typeflag
)
1425 extract_skip (char *file_name
, int typeflag
)
1431 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1435 /* Prepare to extract a file. Find extractor function.
1436 Return zero if extraction should not proceed. */
1439 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1443 if (EXTRACT_OVER_PIPE
)
1446 /* Select the extractor */
1449 case GNUTYPE_SPARSE
:
1450 *fun
= extract_file
;
1457 /* Appears to be a file. But BSD tar uses the convention that a slash
1458 suffix means a directory. */
1459 if (current_stat_info
.had_trailing_slash
)
1463 *fun
= extract_file
;
1469 *fun
= extract_symlink
;
1473 *fun
= extract_link
;
1478 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1479 *fun
= extract_node
;
1485 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1486 *fun
= extract_node
;
1490 #if HAVE_MKFIFO || defined mkfifo
1492 *fun
= extract_fifo
;
1497 case GNUTYPE_DUMPDIR
:
1499 if (current_stat_info
.is_dumpdir
)
1500 delay_directory_restore_option
= true;
1503 case GNUTYPE_VOLHDR
:
1504 *fun
= extract_volhdr
;
1507 case GNUTYPE_MULTIVOL
:
1509 _("%s: Cannot extract -- file is continued from another volume"),
1510 quotearg_colon (current_stat_info
.file_name
)));
1511 *fun
= extract_skip
;
1514 case GNUTYPE_LONGNAME
:
1515 case GNUTYPE_LONGLINK
:
1516 ERROR ((0, 0, _("Unexpected long name header")));
1517 *fun
= extract_failure
;
1521 WARNOPT (WARN_UNKNOWN_CAST
,
1523 _("%s: Unknown file type '%c', extracted as normal file"),
1524 quotearg_colon (file_name
), typeflag
));
1525 *fun
= extract_file
;
1528 /* Determine whether the extraction should proceed */
1532 switch (old_files_option
)
1534 case UNLINK_FIRST_OLD_FILES
:
1535 if (!remove_any_file (file_name
,
1536 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1537 : ORDINARY_REMOVE_OPTION
)
1538 && errno
&& errno
!= ENOENT
)
1540 unlink_error (file_name
);
1545 case KEEP_NEWER_FILES
:
1546 if (file_newer_p (file_name
, 0, ¤t_stat_info
))
1548 WARNOPT (WARN_IGNORE_NEWER
,
1549 (0, 0, _("Current %s is newer or same age"),
1550 quote (file_name
)));
1562 /* Extract a file from the archive. */
1564 extract_archive (void)
1567 tar_extractor_t fun
;
1569 fatal_exit_hook
= extract_finish
;
1571 set_next_block_after (current_header
);
1573 if (!current_stat_info
.file_name
[0]
1574 || (interactive_option
1575 && !confirm ("extract", current_stat_info
.file_name
)))
1581 /* Print the block from current_header and current_stat. */
1583 print_header (¤t_stat_info
, current_header
, -1);
1585 /* Restore stats for all non-ancestor directories, unless
1586 it is an incremental archive.
1587 (see NOTICE in the comment to delay_set_stat above) */
1588 if (!delay_directory_restore_option
)
1590 int dir
= chdir_current
;
1591 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1595 /* Take a safety backup of a previously existing file. */
1598 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1601 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1602 quotearg_colon (current_stat_info
.file_name
)));
1607 /* Extract the archive entry according to its type. */
1609 typeflag
= sparse_member_p (¤t_stat_info
) ?
1610 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1612 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1614 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1616 undo_last_backup ();
1623 /* Extract the links whose final extraction were delayed. */
1625 apply_delayed_links (void)
1627 struct delayed_link
*ds
;
1629 for (ds
= delayed_link_head
; ds
; )
1631 struct string_list
*sources
= ds
->sources
;
1632 char const *valid_source
= 0;
1634 chdir_do (ds
->change_dir
);
1636 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1638 char const *source
= sources
->string
;
1641 /* Make sure the placeholder file is still there. If not,
1642 don't create a link, as the placeholder was probably
1643 removed by a later extraction. */
1644 if (fstatat (chdir_fd
, source
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
1645 && st
.st_dev
== ds
->dev
1646 && st
.st_ino
== ds
->ino
1647 && timespec_cmp (get_stat_birthtime (&st
), ds
->birthtime
) == 0)
1649 /* Unlink the placeholder, then create a hard link if possible,
1650 a symbolic link otherwise. */
1651 if (unlinkat (chdir_fd
, source
, 0) != 0)
1652 unlink_error (source
);
1653 else if (valid_source
1654 && (linkat (chdir_fd
, valid_source
, chdir_fd
, source
, 0)
1657 else if (!ds
->is_symlink
)
1659 if (linkat (chdir_fd
, ds
->target
, chdir_fd
, source
, 0) != 0)
1660 link_error (ds
->target
, source
);
1662 else if (symlinkat (ds
->target
, chdir_fd
, source
) != 0)
1663 symlink_error (ds
->target
, source
);
1666 struct tar_stat_info st1
;
1667 st1
.stat
.st_mode
= ds
->mode
;
1668 st1
.stat
.st_uid
= ds
->uid
;
1669 st1
.stat
.st_gid
= ds
->gid
;
1670 st1
.atime
= ds
->atime
;
1671 st1
.mtime
= ds
->mtime
;
1672 st1
.cntx_name
= ds
->cntx_name
;
1673 st1
.acls_a_ptr
= ds
->acls_a_ptr
;
1674 st1
.acls_a_len
= ds
->acls_a_len
;
1675 st1
.acls_d_ptr
= ds
->acls_d_ptr
;
1676 st1
.acls_d_len
= ds
->acls_d_len
;
1677 st1
.xattr_map
= ds
->xattr_map
;
1678 st1
.xattr_map_size
= ds
->xattr_map_size
;
1679 set_stat (source
, &st1
, -1, 0, 0, SYMTYPE
,
1680 false, AT_SYMLINK_NOFOLLOW
);
1681 valid_source
= source
;
1686 for (sources
= ds
->sources
; sources
; )
1688 struct string_list
*next
= sources
->next
;
1693 xheader_xattr_free (ds
->xattr_map
, ds
->xattr_map_size
);
1694 free (ds
->cntx_name
);
1697 struct delayed_link
*next
= ds
->next
;
1703 delayed_link_head
= 0;
1706 /* Finish the extraction of an archive. */
1708 extract_finish (void)
1710 /* First, fix the status of ordinary directories that need fixing. */
1711 apply_nonancestor_delayed_set_stat ("", 0);
1713 /* Then, apply delayed links, so that they don't affect delayed
1714 directory status-setting for ordinary directories. */
1715 apply_delayed_links ();
1717 /* Finally, fix the status of directories that are ancestors
1718 of delayed links. */
1719 apply_nonancestor_delayed_set_stat ("", 1);
1723 rename_directory (char *src
, char *dst
)
1725 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) != 0)
1733 if (make_directories (dst
, &interdir_made
) == 0)
1735 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) == 0)
1742 /* FIXME: Fall back to recursive copying */
1748 ERROR ((0, e
, _("Cannot rename %s to %s"),