1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2010 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-11-19.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 static bool we_are_root
; /* true if our effective uid == 0 */
31 static mode_t newdir_umask
; /* umask when creating new directories */
32 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
34 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
36 #if ! HAVE_FCHMOD && ! defined fchmod
37 # define fchmod(fd, mode) (errno = ENOSYS, -1)
39 #if ! HAVE_FCHOWN && ! defined fchown
40 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
43 /* Return true if an error number ERR means the system call is
44 supported in this case. */
48 return ! (err
== ENOSYS
50 || (EOPNOTSUPP
!= ENOTSUP
&& err
== EOPNOTSUPP
));
53 /* List of directories whose statuses we need to extract after we've
54 finished extracting their subsidiary files. If you consider each
55 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
56 represents an element where AFTER_LINKS is nonzero and [^D]
57 represents an element where AFTER_LINKS is zero, then the head
58 of the subsequence has the longest name, and each non-head element
59 in the prefix is an ancestor (in the directory hierarchy) of the
62 struct delayed_set_stat
64 /* Next directory in list. */
65 struct delayed_set_stat
*next
;
67 /* Metadata for this directory. */
70 mode_t mode
; /* The desired mode is MODE & ~ current_umask. */
73 struct timespec atime
;
74 struct timespec mtime
;
76 /* An estimate of the directory's current mode, along with a mask
77 specifying which bits of this estimate are known to be correct.
78 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
81 mode_t current_mode_mask
;
83 /* This directory is an intermediate directory that was created
84 as an ancestor of some other directory; it was not mentioned
85 in the archive, so do not set its uid, gid, atime, or mtime,
86 and don't alter its mode outside of MODE_RWX. */
89 /* Whether symbolic links should be followed when accessing the
93 /* Do not set the status of this directory until after delayed
97 /* Directory that the name is relative to. */
100 /* Length and contents of name. */
101 size_t file_name_len
;
105 static struct delayed_set_stat
*delayed_set_stat_head
;
107 /* List of links whose creation we have delayed. */
110 /* The next delayed link in the list. */
111 struct delayed_link
*next
;
113 /* The device, inode number and birthtime of the placeholder.
114 birthtime.tv_nsec is negative if the birthtime is not available.
115 Don't use mtime as this would allow for false matches if some
116 other process removes the placeholder. Don't use ctime as
117 this would cause race conditions and other screwups, e.g.,
118 when restoring hard-linked symlinks. */
121 struct timespec birthtime
;
123 /* True if the link is symbolic. */
126 /* The desired metadata, valid only the link is symbolic. */
130 struct timespec atime
;
131 struct timespec mtime
;
133 /* The directory that the sources and target are relative to. */
136 /* A list of sources for this link. The sources are all to be
137 hard-linked together. */
138 struct string_list
*sources
;
140 /* The desired target of the desired link. */
144 static struct delayed_link
*delayed_link_head
;
148 struct string_list
*next
;
152 /* Set up to extract files. */
156 we_are_root
= geteuid () == 0;
157 same_permissions_option
+= we_are_root
;
158 same_owner_option
+= we_are_root
;
160 /* Option -p clears the kernel umask, so it does not affect proper
161 restoration of file permissions. New intermediate directories will
162 comply with umask at start of program. */
164 newdir_umask
= umask (0);
165 if (0 < same_permissions_option
)
169 umask (newdir_umask
); /* restore the kernel umask */
170 current_umask
= newdir_umask
;
174 /* Use fchmod if possible, fchmodat otherwise. */
176 fd_chmod (int fd
, char const *file
, mode_t mode
, int atflag
)
180 int result
= fchmod (fd
, mode
);
181 if (result
== 0 || implemented (errno
))
184 return fchmodat (chdir_fd
, file
, mode
, atflag
);
187 /* Use fchown if possible, fchownat otherwise. */
189 fd_chown (int fd
, char const *file
, uid_t uid
, gid_t gid
, int atflag
)
193 int result
= fchown (fd
, uid
, gid
);
194 if (result
== 0 || implemented (errno
))
197 return fchownat (chdir_fd
, file
, uid
, gid
, atflag
);
200 /* Use fstat if possible, fstatat otherwise. */
202 fd_stat (int fd
, char const *file
, struct stat
*st
, int atflag
)
206 : fstatat (chdir_fd
, file
, st
, atflag
));
209 /* Set the mode for FILE_NAME to MODE.
210 MODE_MASK specifies the bits of MODE that we care about;
211 thus if MODE_MASK is zero, do nothing.
212 If FD is nonnegative, it is a file descriptor for the file.
213 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
214 the file's current mode, using the style of struct delayed_set_stat.
215 TYPEFLAG specifies the type of the file.
216 ATFLAG specifies the flag to use when statting the file. */
218 set_mode (char const *file_name
,
219 mode_t mode
, mode_t mode_mask
, int fd
,
220 mode_t current_mode
, mode_t current_mode_mask
,
221 char typeflag
, int atflag
)
223 if (((current_mode
^ mode
) | ~ current_mode_mask
) & mode_mask
)
225 if (MODE_ALL
& ~ mode_mask
& ~ current_mode_mask
)
228 if (fd_stat (fd
, file_name
, &st
, atflag
) != 0)
230 stat_error (file_name
);
233 current_mode
= st
.st_mode
;
236 current_mode
&= MODE_ALL
;
237 mode
= (current_mode
& ~ mode_mask
) | (mode
& mode_mask
);
239 if (current_mode
!= mode
)
242 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
244 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
245 setuid-root files would otherwise be a backdoor. See
246 http://opensolaris.org/jive/thread.jspa?threadID=95826
248 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
)
249 && priv_set_restore_linkdir () == 0)
252 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
253 priv_set_remove_linkdir ();
256 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
257 returns ENOTSUP even when operating on non-symlinks, try
258 again with the flag disabled if it does not appear to be
259 supported and if the file is not a symlink. This
260 introduces a race, alas. */
261 if (atflag
&& typeflag
!= SYMTYPE
&& ! implemented (chmod_errno
))
262 chmod_errno
= fd_chmod (fd
, file_name
, mode
, 0) == 0 ? 0 : errno
;
265 && (typeflag
!= SYMTYPE
|| implemented (chmod_errno
)))
268 chmod_error_details (file_name
, mode
);
274 /* Check time after successfully setting FILE_NAME's time stamp to T. */
276 check_time (char const *file_name
, struct timespec t
)
279 WARNOPT (WARN_TIMESTAMP
,
280 (0, 0, _("%s: implausibly old time stamp %s"),
281 file_name
, tartime (t
, true)));
282 else if (timespec_cmp (volume_start_time
, t
) < 0)
286 if (timespec_cmp (now
, t
) < 0)
288 char buf
[TIMESPEC_STRSIZE_BOUND
];
289 struct timespec diff
;
290 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
291 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
292 if (diff
.tv_nsec
< 0)
294 diff
.tv_nsec
+= BILLION
;
297 WARNOPT (WARN_TIMESTAMP
,
298 (0, 0, _("%s: time stamp %s is %s s in the future"),
299 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
304 /* Restore stat attributes (owner, group, mode and times) for
305 FILE_NAME, using information given in *ST.
306 If FD is nonnegative, it is a file descriptor for the file.
307 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
308 the file's current mode, using the style of struct delayed_set_stat.
309 TYPEFLAG specifies the type of the file.
310 If INTERDIR, this is an intermediate directory.
311 ATFLAG specifies the flag to use when statting the file. */
314 set_stat (char const *file_name
,
315 struct tar_stat_info
const *st
,
316 int fd
, mode_t current_mode
, mode_t current_mode_mask
,
317 char typeflag
, bool interdir
, int atflag
)
319 /* Do the utime before the chmod because some versions of utime are
320 broken and trash the modes of the file. */
322 if (! touch_option
&& ! interdir
)
324 struct timespec ts
[2];
325 if (incremental_option
)
328 ts
[0].tv_nsec
= UTIME_OMIT
;
331 if (fdutimensat (fd
, chdir_fd
, file_name
, ts
, atflag
) == 0)
333 if (incremental_option
)
334 check_time (file_name
, ts
[0]);
335 check_time (file_name
, ts
[1]);
337 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
338 utime_error (file_name
);
341 if (0 < same_owner_option
&& ! interdir
)
343 /* Some systems allow non-root users to give files away. Once this
344 done, it is not possible anymore to change file permissions.
345 However, setting file permissions now would be incorrect, since
346 they would apply to the wrong user, and there would be a race
347 condition. So, don't use systems that allow non-root users to
349 uid_t uid
= st
->stat
.st_uid
;
350 gid_t gid
= st
->stat
.st_gid
;
352 if (fd_chown (fd
, file_name
, uid
, gid
, atflag
) == 0)
354 /* Changing the owner can clear st_mode bits in some cases. */
355 if ((current_mode
| ~ current_mode_mask
) & S_IXUGO
)
356 current_mode_mask
&= ~ (current_mode
& (S_ISUID
| S_ISGID
));
358 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
359 chown_error_details (file_name
, uid
, gid
);
363 st
->stat
.st_mode
& ~ current_umask
,
364 0 < same_permissions_option
&& ! interdir
? MODE_ALL
: MODE_RWX
,
365 fd
, current_mode
, current_mode_mask
, typeflag
, atflag
);
368 /* For each entry H in the leading prefix of entries in HEAD that do
369 not have after_links marked, mark H and fill in its dev and ino
370 members. Assume HEAD && ! HEAD->after_links. */
372 mark_after_links (struct delayed_set_stat
*head
)
374 struct delayed_set_stat
*h
= head
;
381 if (deref_stat (h
->file_name
, &st
) != 0)
382 stat_error (h
->file_name
);
389 while ((h
= h
->next
) && ! h
->after_links
);
392 /* Remember to restore stat attributes (owner, group, mode and times)
393 for the directory FILE_NAME, using information given in *ST,
394 once we stop extracting files into that directory.
396 If ST is null, merely create a placeholder node for an intermediate
397 directory that was created by make_directories.
399 NOTICE: this works only if the archive has usual member order, i.e.
400 directory, then the files in that directory. Incremental archive have
401 somewhat reversed order: first go subdirectories, then all other
402 members. To help cope with this case the variable
403 delay_directory_restore_option is set by prepare_to_extract.
405 If an archive was explicitely created so that its member order is
406 reversed, some directory timestamps can be restored incorrectly,
408 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
411 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
412 mode_t current_mode
, mode_t current_mode_mask
,
413 mode_t mode
, int atflag
)
415 size_t file_name_len
= strlen (file_name
);
416 struct delayed_set_stat
*data
=
417 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
418 + file_name_len
+ 1);
419 data
->next
= delayed_set_stat_head
;
423 data
->dev
= st
->stat
.st_dev
;
424 data
->ino
= st
->stat
.st_ino
;
425 data
->uid
= st
->stat
.st_uid
;
426 data
->gid
= st
->stat
.st_gid
;
427 data
->atime
= st
->atime
;
428 data
->mtime
= st
->mtime
;
430 data
->file_name_len
= file_name_len
;
431 data
->current_mode
= current_mode
;
432 data
->current_mode_mask
= current_mode_mask
;
433 data
->interdir
= ! st
;
434 data
->atflag
= atflag
;
435 data
->after_links
= 0;
436 data
->change_dir
= chdir_current
;
437 strcpy (data
->file_name
, file_name
);
438 delayed_set_stat_head
= data
;
439 if (must_be_dot_or_slash (file_name
))
440 mark_after_links (data
);
443 /* Update the delayed_set_stat info for an intermediate directory
444 created within the file name of DIR. The intermediate directory turned
445 out to be the same as this directory, e.g. due to ".." or symbolic
446 links. *DIR_STAT_INFO is the status of the directory. */
448 repair_delayed_set_stat (char const *dir
,
449 struct stat
const *dir_stat_info
)
451 struct delayed_set_stat
*data
;
452 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
455 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
457 stat_error (data
->file_name
);
461 if (st
.st_dev
== dir_stat_info
->st_dev
462 && st
.st_ino
== dir_stat_info
->st_ino
)
464 data
->dev
= current_stat_info
.stat
.st_dev
;
465 data
->ino
= current_stat_info
.stat
.st_ino
;
466 data
->mode
= current_stat_info
.stat
.st_mode
;
467 data
->uid
= current_stat_info
.stat
.st_uid
;
468 data
->gid
= current_stat_info
.stat
.st_gid
;
469 data
->atime
= current_stat_info
.atime
;
470 data
->mtime
= current_stat_info
.mtime
;
471 data
->current_mode
= st
.st_mode
;
472 data
->current_mode_mask
= ALL_MODE_BITS
;
473 data
->interdir
= false;
478 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
479 quotearg_colon (dir
)));
482 /* After a file/link/directory creation has failed, see if
483 it's because some required directory was not present, and if so,
484 create all required directories. Return zero if all the required
485 directories were created, nonzero (issuing a diagnostic) otherwise.
486 Set *INTERDIR_MADE if at least one directory was created. */
488 make_directories (char *file_name
, bool *interdir_made
)
490 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
491 char *cursor
; /* points into the file name */
493 for (cursor
= cursor0
; *cursor
; cursor
++)
499 if (! ISSLASH (*cursor
))
502 /* Avoid mkdir of empty string, if leading or double '/'. */
504 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
507 /* Avoid mkdir where last part of file name is "." or "..". */
509 if (cursor
[-1] == '.'
510 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
511 || (cursor
[-2] == '.'
512 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
515 *cursor
= '\0'; /* truncate the name there */
516 desired_mode
= MODE_RWX
& ~ newdir_umask
;
517 mode
= desired_mode
| (we_are_root
? 0 : MODE_WXUSR
);
518 status
= mkdirat (chdir_fd
, file_name
, mode
);
522 /* Create a struct delayed_set_stat even if
523 mode == desired_mode, because
524 repair_delayed_set_stat may need to update the struct. */
525 delay_set_stat (file_name
,
526 0, mode
& ~ current_umask
, MODE_RWX
,
527 desired_mode
, AT_SYMLINK_NOFOLLOW
);
529 print_for_mkdir (file_name
, cursor
- file_name
, desired_mode
);
530 *interdir_made
= true;
532 else if (errno
== EEXIST
)
536 /* Check whether the desired file exists. Even when the
537 file exists, mkdir can fail with some errno value E other
538 than EEXIST, so long as E describes an error condition
539 that also applies. */
542 status
= fstatat (chdir_fd
, file_name
, &st
, 0);
546 mkdir_error (file_name
);
558 /* Return true if FILE_NAME (with status *STP, if STP) is not a
559 directory, and has a time stamp newer than (or equal to) that of
562 file_newer_p (const char *file_name
, struct stat
const *stp
,
563 struct tar_stat_info
*tar_stat
)
569 if (deref_stat (file_name
, &st
) != 0)
573 stat_warn (file_name
);
574 /* Be safer: if the file exists, assume it is newer. */
582 return (! S_ISDIR (stp
->st_mode
)
583 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (stp
)) <= 0);
588 #define RECOVER_SKIP 2
590 /* Attempt repairing what went wrong with the extraction. Delete an
591 already existing file or create missing intermediate directories.
592 Return RECOVER_OK if we somewhat increased our chances at a successful
593 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
594 caller should skip extraction of that member. The value of errno is
595 properly restored on returning RECOVER_NO.
597 If REGULAR, the caller was trying to extract onto a regular file.
599 Set *INTERDIR_MADE if an intermediate directory is made as part of
600 the recovery process. */
603 maybe_recoverable (char *file_name
, bool regular
, bool *interdir_made
)
607 struct stat
const *stp
= 0;
616 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
617 but some operating systems do not conform to the standard. */
619 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
622 /* FreeBSD 8.1 uses errno == EMLINK. */
624 /* Tru64 5.1B uses errno == ENOTSUP. */
628 || old_files_option
!= OVERWRITE_OLD_FILES
|| dereference_option
)
630 if (strchr (file_name
, '/'))
632 if (deref_stat (file_name
, &st
) != 0)
637 /* The caller tried to open a symbolic link with O_NOFOLLOW.
638 Fall through, treating it as an already-existing file. */
641 /* Remove an old file, if the options allow this. */
643 switch (old_files_option
)
646 WARNOPT (WARN_EXISTING_FILE
,
647 (0, 0, _("%s: skipping existing file"), file_name
));
653 case KEEP_NEWER_FILES
:
654 if (file_newer_p (file_name
, stp
, ¤t_stat_info
))
658 case DEFAULT_OLD_FILES
:
659 case NO_OVERWRITE_DIR_OLD_FILES
:
660 case OVERWRITE_OLD_FILES
:
661 if (0 < remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
))
665 case UNLINK_FIRST_OLD_FILES
:
670 /* Attempt creating missing intermediate directories. */
671 if (make_directories (file_name
, interdir_made
) == 0 && *interdir_made
)
676 /* Just say we can't do anything about it... */
684 /* Fix the statuses of all directories whose statuses need fixing, and
685 which are not ancestors of FILE_NAME. If AFTER_LINKS is
686 nonzero, do this for all such directories; otherwise, stop at the
687 first directory that is marked to be fixed up only after delayed
688 links are applied. */
690 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
692 size_t file_name_len
= strlen (file_name
);
693 bool check_for_renamed_directories
= 0;
695 while (delayed_set_stat_head
)
697 struct delayed_set_stat
*data
= delayed_set_stat_head
;
698 bool skip_this_one
= 0;
700 mode_t current_mode
= data
->current_mode
;
701 mode_t current_mode_mask
= data
->current_mode_mask
;
703 check_for_renamed_directories
|= data
->after_links
;
705 if (after_links
< data
->after_links
706 || (data
->file_name_len
< file_name_len
707 && file_name
[data
->file_name_len
]
708 && (ISSLASH (file_name
[data
->file_name_len
])
709 || ISSLASH (file_name
[data
->file_name_len
- 1]))
710 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
713 chdir_do (data
->change_dir
);
715 if (check_for_renamed_directories
)
717 if (fstatat (chdir_fd
, data
->file_name
, &st
, data
->atflag
) != 0)
719 stat_error (data
->file_name
);
724 current_mode
= st
.st_mode
;
725 current_mode_mask
= ALL_MODE_BITS
;
726 if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
729 _("%s: Directory renamed before its status could be extracted"),
730 quotearg_colon (data
->file_name
)));
738 struct tar_stat_info sb
;
739 sb
.stat
.st_mode
= data
->mode
;
740 sb
.stat
.st_uid
= data
->uid
;
741 sb
.stat
.st_gid
= data
->gid
;
742 sb
.atime
= data
->atime
;
743 sb
.mtime
= data
->mtime
;
744 set_stat (data
->file_name
, &sb
,
745 -1, current_mode
, current_mode_mask
,
746 DIRTYPE
, data
->interdir
, data
->atflag
);
749 delayed_set_stat_head
= data
->next
;
756 /* Extractor functions for various member types */
759 extract_dir (char *file_name
, int typeflag
)
763 mode_t current_mode
= 0;
764 mode_t current_mode_mask
= 0;
766 bool interdir_made
= false;
768 /* Save 'root device' to avoid purging mount points. */
769 if (one_file_system_option
&& root_device
== 0)
773 if (fstatat (chdir_fd
, ".", &st
, 0) != 0)
776 root_device
= st
.st_dev
;
779 if (incremental_option
)
780 /* Read the entry and delete files that aren't listed in the archive. */
781 purge_directory (file_name
);
782 else if (typeflag
== GNUTYPE_DUMPDIR
)
785 /* If ownership or permissions will be restored later, create the
786 directory with restrictive permissions at first, so that in the
787 meantime processes owned by other users do not inadvertently
788 create files under this directory that inherit the wrong owner,
789 group, or permissions from the directory. If not root, though,
790 make the directory writeable and searchable at first, so that
791 files can be created under it. */
792 mode
= ((current_stat_info
.stat
.st_mode
793 & (0 < same_owner_option
|| 0 < same_permissions_option
796 | (we_are_root
? 0 : MODE_WXUSR
));
800 status
= mkdirat (chdir_fd
, file_name
, mode
);
803 current_mode
= mode
& ~ current_umask
;
804 current_mode_mask
= MODE_RWX
;
805 atflag
= AT_SYMLINK_NOFOLLOW
;
811 || old_files_option
== DEFAULT_OLD_FILES
812 || old_files_option
== OVERWRITE_OLD_FILES
))
815 if (deref_stat (file_name
, &st
) == 0)
817 current_mode
= st
.st_mode
;
818 current_mode_mask
= ALL_MODE_BITS
;
820 if (S_ISDIR (current_mode
))
824 repair_delayed_set_stat (file_name
, &st
);
833 switch (maybe_recoverable (file_name
, false, &interdir_made
))
844 mkdir_error (file_name
);
853 || old_files_option
== DEFAULT_OLD_FILES
854 || old_files_option
== OVERWRITE_OLD_FILES
)
855 delay_set_stat (file_name
, ¤t_stat_info
,
856 current_mode
, current_mode_mask
,
857 current_stat_info
.stat
.st_mode
, atflag
);
864 open_output_file (char const *file_name
, int typeflag
, mode_t mode
,
865 mode_t
*current_mode
, mode_t
*current_mode_mask
)
868 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
869 int openflag
= (O_WRONLY
| O_BINARY
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
871 | (overwriting_old_files
872 ? O_TRUNC
| (dereference_option
? 0 : O_NOFOLLOW
)
875 if (typeflag
== CONTTYPE
)
877 static int conttype_diagnosed
;
879 if (!conttype_diagnosed
)
881 conttype_diagnosed
= 1;
882 WARNOPT (WARN_CONTIGUOUS_CAST
,
883 (0, 0, _("Extracting contiguous files as regular files")));
887 /* If O_NOFOLLOW is needed but does not work, check for a symlink
888 separately. There's a race condition, but that cannot be avoided
889 on hosts lacking O_NOFOLLOW. */
890 if (! HAVE_WORKING_O_NOFOLLOW
891 && overwriting_old_files
&& ! dereference_option
)
894 if (fstatat (chdir_fd
, file_name
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
895 && S_ISLNK (st
.st_mode
))
902 fd
= openat (chdir_fd
, file_name
, openflag
, mode
);
905 if (overwriting_old_files
)
908 if (fstat (fd
, &st
) != 0)
915 if (! S_ISREG (st
.st_mode
))
921 *current_mode
= st
.st_mode
;
922 *current_mode_mask
= ALL_MODE_BITS
;
926 *current_mode
= mode
& ~ current_umask
;
927 *current_mode_mask
= MODE_RWX
;
935 extract_file (char *file_name
, int typeflag
)
939 union block
*data_block
;
943 bool interdir_made
= false;
944 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
945 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
946 mode_t current_mode
= 0;
947 mode_t current_mode_mask
= 0;
949 if (to_stdout_option
)
951 else if (to_command_option
)
953 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
962 while ((fd
= open_output_file (file_name
, typeflag
, mode
,
963 ¤t_mode
, ¤t_mode_mask
))
966 int recover
= maybe_recoverable (file_name
, true, &interdir_made
);
967 if (recover
!= RECOVER_OK
)
970 if (recover
== RECOVER_SKIP
)
972 open_error (file_name
);
978 mv_begin_read (¤t_stat_info
);
979 if (current_stat_info
.is_sparse
)
980 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
982 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
986 /* Locate data, determine max length writeable, write it,
987 block that we have used the data, then check if the write
990 data_block
= find_next_block ();
993 ERROR ((0, 0, _("Unexpected EOF in archive")));
994 break; /* FIXME: What happens, then? */
997 written
= available_space_after (data_block
);
1002 count
= blocking_write (fd
, data_block
->buffer
, written
);
1005 set_next_block_after ((union block
*)
1006 (data_block
->buffer
+ written
- 1));
1007 if (count
!= written
)
1009 if (!to_command_option
)
1010 write_error_details (file_name
, count
, written
);
1011 /* FIXME: shouldn't we restore from backup? */
1020 /* If writing to stdout, don't try to do anything to the filename;
1021 it doesn't exist, or we don't want to touch it anyway. */
1023 if (to_stdout_option
)
1026 if (! to_command_option
)
1027 set_stat (file_name
, ¤t_stat_info
, fd
,
1028 current_mode
, current_mode_mask
, typeflag
, false,
1029 (old_files_option
== OVERWRITE_OLD_FILES
1030 ? 0 : AT_SYMLINK_NOFOLLOW
));
1032 status
= close (fd
);
1034 close_error (file_name
);
1036 if (to_command_option
)
1037 sys_wait_command ();
1042 /* Create a placeholder file with name FILE_NAME, which will be
1043 replaced after other extraction is done by a symbolic link if
1044 IS_SYMLINK is true, and by a hard link otherwise. Set
1045 *INTERDIR_MADE if an intermediate directory is made in the
1049 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
1054 while ((fd
= openat (chdir_fd
, file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
1056 switch (maybe_recoverable (file_name
, false, interdir_made
))
1065 open_error (file_name
);
1070 if (fstat (fd
, &st
) != 0)
1072 stat_error (file_name
);
1075 else if (close (fd
) != 0)
1076 close_error (file_name
);
1079 struct delayed_set_stat
*h
;
1080 struct delayed_link
*p
=
1081 xmalloc (offsetof (struct delayed_link
, target
)
1082 + strlen (current_stat_info
.link_name
)
1084 p
->next
= delayed_link_head
;
1085 delayed_link_head
= p
;
1088 p
->birthtime
= get_stat_birthtime (&st
);
1089 p
->is_symlink
= is_symlink
;
1092 p
->mode
= current_stat_info
.stat
.st_mode
;
1093 p
->uid
= current_stat_info
.stat
.st_uid
;
1094 p
->gid
= current_stat_info
.stat
.st_gid
;
1095 p
->atime
= current_stat_info
.atime
;
1096 p
->mtime
= current_stat_info
.mtime
;
1098 p
->change_dir
= chdir_current
;
1099 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
1100 + strlen (file_name
) + 1);
1101 p
->sources
->next
= 0;
1102 strcpy (p
->sources
->string
, file_name
);
1103 strcpy (p
->target
, current_stat_info
.link_name
);
1105 h
= delayed_set_stat_head
;
1106 if (h
&& ! h
->after_links
1107 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
1108 && ISSLASH (file_name
[h
->file_name_len
])
1109 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
1110 mark_after_links (h
);
1119 extract_link (char *file_name
, int typeflag
)
1121 bool interdir_made
= false;
1122 char const *link_name
;
1125 link_name
= current_stat_info
.link_name
;
1127 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1128 return create_placeholder_file (file_name
, false, &interdir_made
);
1132 struct stat st1
, st2
;
1134 int status
= linkat (chdir_fd
, link_name
, chdir_fd
, file_name
, 0);
1139 struct delayed_link
*ds
= delayed_link_head
;
1141 && fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
) == 0)
1142 for (; ds
; ds
= ds
->next
)
1143 if (ds
->change_dir
== chdir_current
1144 && ds
->dev
== st1
.st_dev
1145 && ds
->ino
== st1
.st_ino
1146 && (timespec_cmp (ds
->birthtime
, get_stat_birthtime (&st1
))
1149 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1150 + strlen (file_name
) + 1);
1151 strcpy (p
->string
, file_name
);
1152 p
->next
= ds
->sources
;
1158 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1159 || ((fstatat (chdir_fd
, link_name
, &st1
, AT_SYMLINK_NOFOLLOW
)
1161 && (fstatat (chdir_fd
, file_name
, &st2
, AT_SYMLINK_NOFOLLOW
)
1163 && st1
.st_dev
== st2
.st_dev
1164 && st1
.st_ino
== st2
.st_ino
))
1169 while ((rc
= maybe_recoverable (file_name
, false, &interdir_made
))
1172 if (rc
== RECOVER_SKIP
)
1174 if (!(incremental_option
&& errno
== EEXIST
))
1176 link_error (link_name
, file_name
);
1183 extract_symlink (char *file_name
, int typeflag
)
1186 bool interdir_made
= false;
1188 if (! absolute_names_option
1189 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1190 || contains_dot_dot (current_stat_info
.link_name
)))
1191 return create_placeholder_file (file_name
, true, &interdir_made
);
1193 while (symlinkat (current_stat_info
.link_name
, chdir_fd
, file_name
) != 0)
1194 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1203 symlink_error (current_stat_info
.link_name
, file_name
);
1207 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1208 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1212 static int warned_once
;
1217 WARNOPT (WARN_SYMBOLIC_CAST
,
1219 _("Attempting extraction of symbolic links as hard links")));
1221 return extract_link (file_name
, typeflag
);
1225 #if S_IFCHR || S_IFBLK
1227 extract_node (char *file_name
, int typeflag
)
1229 bool interdir_made
= false;
1230 mode_t mode
= (current_stat_info
.stat
.st_mode
& (MODE_RWX
| S_IFBLK
| S_IFCHR
)
1231 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1233 while (mknodat (chdir_fd
, file_name
, mode
, current_stat_info
.stat
.st_rdev
)
1235 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1244 mknod_error (file_name
);
1248 set_stat (file_name
, ¤t_stat_info
, -1,
1249 mode
& ~ current_umask
, MODE_RWX
,
1250 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1255 #if HAVE_MKFIFO || defined mkfifo
1257 extract_fifo (char *file_name
, int typeflag
)
1259 bool interdir_made
= false;
1260 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1261 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1263 while (mkfifoat (chdir_fd
, file_name
, mode
) != 0)
1264 switch (maybe_recoverable (file_name
, false, &interdir_made
))
1273 mkfifo_error (file_name
);
1277 set_stat (file_name
, ¤t_stat_info
, -1,
1278 mode
& ~ current_umask
, MODE_RWX
,
1279 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1285 extract_volhdr (char *file_name
, int typeflag
)
1292 extract_failure (char *file_name
, int typeflag
)
1297 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1301 /* Prepare to extract a file. Find extractor function.
1302 Return zero if extraction should not proceed. */
1305 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1309 if (EXTRACT_OVER_PIPE
)
1312 /* Select the extractor */
1315 case GNUTYPE_SPARSE
:
1316 *fun
= extract_file
;
1323 /* Appears to be a file. But BSD tar uses the convention that a slash
1324 suffix means a directory. */
1325 if (current_stat_info
.had_trailing_slash
)
1329 *fun
= extract_file
;
1335 *fun
= extract_symlink
;
1339 *fun
= extract_link
;
1344 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1345 *fun
= extract_node
;
1351 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1352 *fun
= extract_node
;
1356 #if HAVE_MKFIFO || defined mkfifo
1358 *fun
= extract_fifo
;
1363 case GNUTYPE_DUMPDIR
:
1365 if (current_stat_info
.is_dumpdir
)
1366 delay_directory_restore_option
= true;
1369 case GNUTYPE_VOLHDR
:
1370 *fun
= extract_volhdr
;
1373 case GNUTYPE_MULTIVOL
:
1375 _("%s: Cannot extract -- file is continued from another volume"),
1376 quotearg_colon (current_stat_info
.file_name
)));
1377 *fun
= extract_failure
;
1380 case GNUTYPE_LONGNAME
:
1381 case GNUTYPE_LONGLINK
:
1382 ERROR ((0, 0, _("Unexpected long name header")));
1383 *fun
= extract_failure
;
1387 WARNOPT (WARN_UNKNOWN_CAST
,
1389 _("%s: Unknown file type '%c', extracted as normal file"),
1390 quotearg_colon (file_name
), typeflag
));
1391 *fun
= extract_file
;
1394 /* Determine whether the extraction should proceed */
1398 switch (old_files_option
)
1400 case UNLINK_FIRST_OLD_FILES
:
1401 if (!remove_any_file (file_name
,
1402 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1403 : ORDINARY_REMOVE_OPTION
)
1404 && errno
&& errno
!= ENOENT
)
1406 unlink_error (file_name
);
1411 case KEEP_NEWER_FILES
:
1412 if (file_newer_p (file_name
, 0, ¤t_stat_info
))
1414 WARNOPT (WARN_IGNORE_NEWER
,
1415 (0, 0, _("Current %s is newer or same age"),
1416 quote (file_name
)));
1428 /* Extract a file from the archive. */
1430 extract_archive (void)
1433 tar_extractor_t fun
;
1435 fatal_exit_hook
= extract_finish
;
1437 set_next_block_after (current_header
);
1439 if (!current_stat_info
.file_name
[0]
1440 || (interactive_option
1441 && !confirm ("extract", current_stat_info
.file_name
)))
1447 /* Print the block from current_header and current_stat. */
1449 print_header (¤t_stat_info
, current_header
, -1);
1451 /* Restore stats for all non-ancestor directories, unless
1452 it is an incremental archive.
1453 (see NOTICE in the comment to delay_set_stat above) */
1454 if (!delay_directory_restore_option
)
1456 int dir
= chdir_current
;
1457 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1461 /* Take a safety backup of a previously existing file. */
1464 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1467 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1468 quotearg_colon (current_stat_info
.file_name
)));
1473 /* Extract the archive entry according to its type. */
1475 typeflag
= sparse_member_p (¤t_stat_info
) ?
1476 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1478 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1480 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1482 undo_last_backup ();
1489 /* Extract the links whose final extraction were delayed. */
1491 apply_delayed_links (void)
1493 struct delayed_link
*ds
;
1495 for (ds
= delayed_link_head
; ds
; )
1497 struct string_list
*sources
= ds
->sources
;
1498 char const *valid_source
= 0;
1500 chdir_do (ds
->change_dir
);
1502 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1504 char const *source
= sources
->string
;
1507 /* Make sure the placeholder file is still there. If not,
1508 don't create a link, as the placeholder was probably
1509 removed by a later extraction. */
1510 if (fstatat (chdir_fd
, source
, &st
, AT_SYMLINK_NOFOLLOW
) == 0
1511 && st
.st_dev
== ds
->dev
1512 && st
.st_ino
== ds
->ino
1513 && timespec_cmp (get_stat_birthtime (&st
), ds
->birthtime
) == 0)
1515 /* Unlink the placeholder, then create a hard link if possible,
1516 a symbolic link otherwise. */
1517 if (unlinkat (chdir_fd
, source
, 0) != 0)
1518 unlink_error (source
);
1519 else if (valid_source
1520 && (linkat (chdir_fd
, valid_source
, chdir_fd
, source
, 0)
1523 else if (!ds
->is_symlink
)
1525 if (linkat (chdir_fd
, ds
->target
, chdir_fd
, source
, 0) != 0)
1526 link_error (ds
->target
, source
);
1528 else if (symlinkat (ds
->target
, chdir_fd
, source
) != 0)
1529 symlink_error (ds
->target
, source
);
1532 struct tar_stat_info st1
;
1533 st1
.stat
.st_mode
= ds
->mode
;
1534 st1
.stat
.st_uid
= ds
->uid
;
1535 st1
.stat
.st_gid
= ds
->gid
;
1536 st1
.atime
= ds
->atime
;
1537 st1
.mtime
= ds
->mtime
;
1538 set_stat (source
, &st1
, -1, 0, 0, SYMTYPE
,
1539 false, AT_SYMLINK_NOFOLLOW
);
1540 valid_source
= source
;
1545 for (sources
= ds
->sources
; sources
; )
1547 struct string_list
*next
= sources
->next
;
1553 struct delayed_link
*next
= ds
->next
;
1559 delayed_link_head
= 0;
1562 /* Finish the extraction of an archive. */
1564 extract_finish (void)
1566 /* First, fix the status of ordinary directories that need fixing. */
1567 apply_nonancestor_delayed_set_stat ("", 0);
1569 /* Then, apply delayed links, so that they don't affect delayed
1570 directory status-setting for ordinary directories. */
1571 apply_delayed_links ();
1573 /* Finally, fix the status of directories that are ancestors
1574 of delayed links. */
1575 apply_nonancestor_delayed_set_stat ("", 1);
1579 rename_directory (char *src
, char *dst
)
1581 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) != 0)
1589 if (make_directories (dst
, &interdir_made
) == 0)
1591 if (renameat (chdir_fd
, src
, chdir_fd
, dst
) == 0)
1598 /* FIXME: Fall back to recursive copying */
1604 ERROR ((0, e
, _("Cannot rename %s to %s"),