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 /* Status of the permissions of a file that we are extracting. */
37 /* This file may have existed already; its permissions are unknown. */
40 /* This file was created using the permissions from the archive,
41 except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
44 /* This is an intermediate directory; the archive did not specify
49 /* List of directories whose statuses we need to extract after we've
50 finished extracting their subsidiary files. If you consider each
51 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
52 represents an element where AFTER_LINKS is nonzero and [^D]
53 represents an element where AFTER_LINKS is zero, then the head
54 of the subsequence has the longest name, and each non-head element
55 in the prefix is an ancestor (in the directory hierarchy) of the
58 struct delayed_set_stat
60 struct delayed_set_stat
*next
;
66 struct timespec atime
;
67 struct timespec mtime
;
69 mode_t invert_permissions
;
70 enum permstatus permstatus
;
76 static struct delayed_set_stat
*delayed_set_stat_head
;
78 /* List of links whose creation we have delayed. */
81 /* The next delayed link in the list. */
82 struct delayed_link
*next
;
84 /* The device, inode number and ctime of the placeholder. Use
85 ctime, not mtime, to make false matches less likely if some
86 other process removes the placeholder. */
89 struct timespec ctime
;
91 /* True if the link is symbolic. */
94 /* The desired owner and group of the link, if it is a symlink. */
98 /* The directory that the sources and target are relative to. */
101 /* A list of sources for this link. The sources are all to be
102 hard-linked together. */
103 struct string_list
*sources
;
105 /* The desired target of the desired link. */
109 static struct delayed_link
*delayed_link_head
;
113 struct string_list
*next
;
117 /* Set up to extract files. */
121 we_are_root
= geteuid () == 0;
122 same_permissions_option
+= we_are_root
;
123 same_owner_option
+= we_are_root
;
125 /* Option -p clears the kernel umask, so it does not affect proper
126 restoration of file permissions. New intermediate directories will
127 comply with umask at start of program. */
129 newdir_umask
= umask (0);
130 if (0 < same_permissions_option
)
134 umask (newdir_umask
); /* restore the kernel umask */
135 current_umask
= newdir_umask
;
139 /* If restoring permissions, restore the mode for FILE_NAME from
140 information given in *STAT_INFO (where *CUR_INFO gives
141 the current status if CUR_INFO is nonzero); otherwise invert the
142 INVERT_PERMISSIONS bits from the file's current permissions.
143 PERMSTATUS specifies the status of the file's permissions.
144 TYPEFLAG specifies the type of the file. */
146 set_mode (char const *file_name
,
147 struct stat
const *stat_info
,
148 struct stat
const *cur_info
,
149 mode_t invert_permissions
, enum permstatus permstatus
,
155 if (0 < same_permissions_option
156 && permstatus
!= INTERDIR_PERMSTATUS
)
158 mode
= stat_info
->st_mode
;
160 /* If we created the file and it has a mode that we set already
161 with O_CREAT, then its mode is often set correctly already.
162 But if we are changing ownership, the mode's group and and
163 other permission bits were omitted originally, so it's less
164 likely that the mode is OK now. Also, on many hosts, some
165 directories inherit the setgid bits from their parents, so we
166 we must set directories' modes explicitly. */
167 if ((permstatus
== ARCHIVED_PERMSTATUS
168 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
169 && typeflag
!= DIRTYPE
170 && typeflag
!= GNUTYPE_DUMPDIR
)
173 else if (! invert_permissions
)
177 /* We must inspect a directory's current permissions, since the
178 directory may have inherited its setgid bit from its parent.
180 INVERT_PERMISSIONS happens to be nonzero only for directories
181 that we created, so there's no point optimizing this code for
186 if (stat (file_name
, &st
) != 0)
188 stat_error (file_name
);
193 mode
= cur_info
->st_mode
^ invert_permissions
;
196 chmod_errno
= chmod (file_name
, mode
) == 0 ? 0 : errno
;
197 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
) != 0)
199 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
200 setuid-root files would otherwise be a backdoor. See
201 http://opensolaris.org/jive/thread.jspa?threadID=95826
203 if (priv_set_restore_linkdir () == 0)
205 chmod_errno
= chmod (file_name
, mode
) == 0 ? 0 : errno
;
206 priv_set_remove_linkdir ();
212 chmod_error_details (file_name
, mode
);
216 /* Check time after successfully setting FILE_NAME's time stamp to T. */
218 check_time (char const *file_name
, struct timespec t
)
221 WARNOPT (WARN_TIMESTAMP
,
222 (0, 0, _("%s: implausibly old time stamp %s"),
223 file_name
, tartime (t
, true)));
224 else if (timespec_cmp (volume_start_time
, t
) < 0)
228 if (timespec_cmp (now
, t
) < 0)
230 char buf
[TIMESPEC_STRSIZE_BOUND
];
231 struct timespec diff
;
232 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
233 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
234 if (diff
.tv_nsec
< 0)
236 diff
.tv_nsec
+= BILLION
;
239 WARNOPT (WARN_TIMESTAMP
,
240 (0, 0, _("%s: time stamp %s is %s s in the future"),
241 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
246 /* Restore stat attributes (owner, group, mode and times) for
247 FILE_NAME, using information given in *ST.
248 If CUR_INFO is nonzero, *CUR_INFO is the
249 file's current status.
250 If not restoring permissions, invert the
251 INVERT_PERMISSIONS bits from the file's current permissions.
252 PERMSTATUS specifies the status of the file's permissions.
253 TYPEFLAG specifies the type of the file. */
255 /* FIXME: About proper restoration of symbolic link attributes, we still do
256 not have it right. Pretesters' reports tell us we need further study and
257 probably more configuration. For now, just use lchown if it exists, and
258 punt for the rest. Sigh! */
261 set_stat (char const *file_name
,
262 struct tar_stat_info
const *st
,
263 struct stat
const *cur_info
,
264 mode_t invert_permissions
, enum permstatus permstatus
,
267 if (typeflag
!= SYMTYPE
)
269 /* We do the utime before the chmod because some versions of utime are
270 broken and trash the modes of the file. */
272 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
274 /* We set the accessed time to `now', which is really the time we
275 started extracting files, unless incremental_option is used, in
276 which case .st_atime is used. */
278 /* FIXME: incremental_option should set ctime too, but how? */
280 struct timespec ts
[2];
281 if (incremental_option
)
287 if (utimens (file_name
, ts
) != 0)
288 utime_error (file_name
);
291 check_time (file_name
, ts
[0]);
292 check_time (file_name
, ts
[1]);
296 /* Some systems allow non-root users to give files away. Once this
297 done, it is not possible anymore to change file permissions.
298 However, setting file permissions now would be incorrect, since
299 they would apply to the wrong user, and there would be a race
300 condition. So, don't use systems that allow non-root users to
304 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
306 /* When lchown exists, it should be used to change the attributes of
307 the symbolic link itself. In this case, a mere chown would change
308 the attributes of the file the symbolic link is pointing to, and
309 should be avoided. */
310 int chown_result
= 1;
312 if (typeflag
== SYMTYPE
)
315 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
320 chown_result
= chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
323 if (chown_result
== 0)
325 /* Changing the owner can flip st_mode bits in some cases, so
326 ignore cur_info if it might be obsolete now. */
328 && cur_info
->st_mode
& S_IXUGO
329 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
332 else if (chown_result
< 0)
333 chown_error_details (file_name
,
334 st
->stat
.st_uid
, st
->stat
.st_gid
);
337 if (typeflag
!= SYMTYPE
)
338 set_mode (file_name
, &st
->stat
, cur_info
,
339 invert_permissions
, permstatus
, typeflag
);
342 /* Remember to restore stat attributes (owner, group, mode and times)
343 for the directory FILE_NAME, using information given in *ST,
344 once we stop extracting files into that directory.
345 If not restoring permissions, remember to invert the
346 INVERT_PERMISSIONS bits from the file's current permissions.
347 PERMSTATUS specifies the status of the file's permissions.
349 NOTICE: this works only if the archive has usual member order, i.e.
350 directory, then the files in that directory. Incremental archive have
351 somewhat reversed order: first go subdirectories, then all other
352 members. To help cope with this case the variable
353 delay_directory_restore_option is set by prepare_to_extract.
355 If an archive was explicitely created so that its member order is
356 reversed, some directory timestamps can be restored incorrectly,
358 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
361 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
362 mode_t invert_permissions
, enum permstatus permstatus
)
364 size_t file_name_len
= strlen (file_name
);
365 struct delayed_set_stat
*data
=
366 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
367 + file_name_len
+ 1);
368 data
->next
= delayed_set_stat_head
;
369 data
->dev
= st
->stat
.st_dev
;
370 data
->ino
= st
->stat
.st_ino
;
371 data
->mode
= st
->stat
.st_mode
;
372 data
->uid
= st
->stat
.st_uid
;
373 data
->gid
= st
->stat
.st_gid
;
374 data
->atime
= st
->atime
;
375 data
->mtime
= st
->mtime
;
376 data
->file_name_len
= file_name_len
;
377 data
->invert_permissions
= invert_permissions
;
378 data
->permstatus
= permstatus
;
379 data
->after_links
= 0;
380 data
->change_dir
= chdir_current
;
381 strcpy (data
->file_name
, file_name
);
382 delayed_set_stat_head
= data
;
385 /* Update the delayed_set_stat info for an intermediate directory
386 created within the file name of DIR. The intermediate directory turned
387 out to be the same as this directory, e.g. due to ".." or symbolic
388 links. *DIR_STAT_INFO is the status of the directory. */
390 repair_delayed_set_stat (char const *dir
,
391 struct stat
const *dir_stat_info
)
393 struct delayed_set_stat
*data
;
394 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
397 if (stat (data
->file_name
, &st
) != 0)
399 stat_error (data
->file_name
);
403 if (st
.st_dev
== dir_stat_info
->st_dev
404 && st
.st_ino
== dir_stat_info
->st_ino
)
406 data
->dev
= current_stat_info
.stat
.st_dev
;
407 data
->ino
= current_stat_info
.stat
.st_ino
;
408 data
->mode
= current_stat_info
.stat
.st_mode
;
409 data
->uid
= current_stat_info
.stat
.st_uid
;
410 data
->gid
= current_stat_info
.stat
.st_gid
;
411 data
->atime
= current_stat_info
.atime
;
412 data
->mtime
= current_stat_info
.mtime
;
413 data
->invert_permissions
=
414 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
415 & MODE_RWX
& ~ current_umask
);
416 data
->permstatus
= ARCHIVED_PERMSTATUS
;
421 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
422 quotearg_colon (dir
)));
425 /* After a file/link/directory creation has failed, see if
426 it's because some required directory was not present, and if so,
427 create all required directories. Return non-zero if a directory
430 make_directories (char *file_name
)
432 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
433 char *cursor
; /* points into the file name */
434 int did_something
= 0; /* did we do anything yet? */
436 int invert_permissions
;
439 for (cursor
= cursor0
; *cursor
; cursor
++)
441 if (! ISSLASH (*cursor
))
444 /* Avoid mkdir of empty string, if leading or double '/'. */
446 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
449 /* Avoid mkdir where last part of file name is "." or "..". */
451 if (cursor
[-1] == '.'
452 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
453 || (cursor
[-2] == '.'
454 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
457 *cursor
= '\0'; /* truncate the name there */
458 mode
= MODE_RWX
& ~ newdir_umask
;
459 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
460 status
= mkdir (file_name
, mode
^ invert_permissions
);
464 /* Create a struct delayed_set_stat even if
465 invert_permissions is zero, because
466 repair_delayed_set_stat may need to update the struct. */
467 delay_set_stat (file_name
,
469 invert_permissions
, INTERDIR_PERMSTATUS
);
471 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
481 continue; /* Directory already exists. */
482 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
483 this. Reported by Warren Hyde
484 <Warren.Hyde@motorola.com> */
485 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
486 && access (file_name
, W_OK
) == 0)
489 /* Some other error in the mkdir. We return to the caller. */
493 return did_something
; /* tell them to retry if we made one */
497 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
501 if (stat (file_name
, &st
))
505 stat_warn (file_name
);
506 /* Be on the safe side: if the file does exist assume it is newer */
511 if (!S_ISDIR (st
.st_mode
)
512 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
521 #define RECOVER_SKIP 2
523 /* Attempt repairing what went wrong with the extraction. Delete an
524 already existing file or create missing intermediate directories.
525 Return RECOVER_OK if we somewhat increased our chances at a successful
526 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
527 caller should skip extraction of that member. The value of errno is
528 properly restored on returning RECOVER_NO. */
531 maybe_recoverable (char *file_name
, bool *interdir_made
)
541 /* Remove an old file, if the options allow this. */
543 switch (old_files_option
)
548 case KEEP_NEWER_FILES
:
549 if (file_newer_p (file_name
, ¤t_stat_info
))
556 case DEFAULT_OLD_FILES
:
557 case NO_OVERWRITE_DIR_OLD_FILES
:
558 case OVERWRITE_OLD_FILES
:
560 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
562 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
565 case UNLINK_FIRST_OLD_FILES
:
570 /* Attempt creating missing intermediate directories. */
571 if (! make_directories (file_name
))
576 *interdir_made
= true;
580 /* Just say we can't do anything about it... */
586 /* Fix the statuses of all directories whose statuses need fixing, and
587 which are not ancestors of FILE_NAME. If AFTER_LINKS is
588 nonzero, do this for all such directories; otherwise, stop at the
589 first directory that is marked to be fixed up only after delayed
590 links are applied. */
592 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
594 size_t file_name_len
= strlen (file_name
);
595 bool check_for_renamed_directories
= 0;
597 while (delayed_set_stat_head
)
599 struct delayed_set_stat
*data
= delayed_set_stat_head
;
600 bool skip_this_one
= 0;
602 struct stat
const *cur_info
= 0;
604 check_for_renamed_directories
|= data
->after_links
;
606 if (after_links
< data
->after_links
607 || (data
->file_name_len
< file_name_len
608 && file_name
[data
->file_name_len
]
609 && (ISSLASH (file_name
[data
->file_name_len
])
610 || ISSLASH (file_name
[data
->file_name_len
- 1]))
611 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
614 chdir_do (data
->change_dir
);
616 if (check_for_renamed_directories
)
619 if (stat (data
->file_name
, &st
) != 0)
621 stat_error (data
->file_name
);
624 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
627 _("%s: Directory renamed before its status could be extracted"),
628 quotearg_colon (data
->file_name
)));
635 struct tar_stat_info sb
;
636 sb
.stat
.st_mode
= data
->mode
;
637 sb
.stat
.st_uid
= data
->uid
;
638 sb
.stat
.st_gid
= data
->gid
;
639 sb
.atime
= data
->atime
;
640 sb
.mtime
= data
->mtime
;
641 set_stat (data
->file_name
, &sb
, cur_info
,
642 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
645 delayed_set_stat_head
= data
->next
;
652 /* Extractor functions for various member types */
655 extract_dir (char *file_name
, int typeflag
)
659 bool interdir_made
= false;
661 /* Save 'root device' to avoid purging mount points. */
662 if (one_file_system_option
&& root_device
== 0)
666 if (stat (".", &st
) != 0)
669 root_device
= st
.st_dev
;
672 if (incremental_option
)
673 /* Read the entry and delete files that aren't listed in the archive. */
674 purge_directory (file_name
);
675 else if (typeflag
== GNUTYPE_DUMPDIR
)
678 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
679 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
682 while ((status
= mkdir (file_name
, mode
)))
686 || old_files_option
== DEFAULT_OLD_FILES
687 || old_files_option
== OVERWRITE_OLD_FILES
))
690 if (stat (file_name
, &st
) == 0)
694 repair_delayed_set_stat (file_name
, &st
);
697 if (S_ISDIR (st
.st_mode
))
707 switch (maybe_recoverable (file_name
, &interdir_made
))
718 mkdir_error (file_name
);
727 || old_files_option
== DEFAULT_OLD_FILES
728 || old_files_option
== OVERWRITE_OLD_FILES
)
731 delay_set_stat (file_name
, ¤t_stat_info
,
732 ((mode
^ current_stat_info
.stat
.st_mode
)
733 & MODE_RWX
& ~ current_umask
),
734 ARCHIVED_PERMSTATUS
);
735 else /* For an already existing directory, invert_perms must be 0 */
736 delay_set_stat (file_name
, ¤t_stat_info
,
745 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
748 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
749 | (old_files_option
== OVERWRITE_OLD_FILES
754 /* Contiguous files (on the Masscomp) have to specify the size in
755 the open call that creates them. */
757 if (typeflag
== CONTTYPE
)
758 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
760 fd
= open (file_name
, openflag
, mode
);
762 #else /* not O_CTG */
763 if (typeflag
== CONTTYPE
)
765 static int conttype_diagnosed
;
767 if (!conttype_diagnosed
)
769 conttype_diagnosed
= 1;
770 WARNOPT (WARN_CONTIGUOUS_CAST
,
771 (0, 0, _("Extracting contiguous files as regular files")));
774 fd
= open (file_name
, openflag
, mode
);
776 #endif /* not O_CTG */
782 extract_file (char *file_name
, int typeflag
)
786 union block
*data_block
;
790 bool interdir_made
= false;
791 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
792 mode_t invert_permissions
=
793 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
795 /* FIXME: deal with protection issues. */
797 if (to_stdout_option
)
799 else if (to_command_option
)
801 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
810 int recover
= RECOVER_NO
;
812 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
814 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
820 if (recover
== RECOVER_SKIP
)
822 open_error (file_name
);
827 mv_begin_read (¤t_stat_info
);
828 if (current_stat_info
.is_sparse
)
829 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
831 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
835 /* Locate data, determine max length writeable, write it,
836 block that we have used the data, then check if the write
839 data_block
= find_next_block ();
842 ERROR ((0, 0, _("Unexpected EOF in archive")));
843 break; /* FIXME: What happens, then? */
846 written
= available_space_after (data_block
);
851 count
= full_write (fd
, data_block
->buffer
, written
);
854 set_next_block_after ((union block
*)
855 (data_block
->buffer
+ written
- 1));
856 if (count
!= written
)
858 if (!to_command_option
)
859 write_error_details (file_name
, count
, written
);
860 /* FIXME: shouldn't we restore from backup? */
869 /* If writing to stdout, don't try to do anything to the filename;
870 it doesn't exist, or we don't want to touch it anyway. */
872 if (to_stdout_option
)
877 close_error (file_name
);
879 if (to_command_option
)
882 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
883 (old_files_option
== OVERWRITE_OLD_FILES
?
884 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
890 /* Create a placeholder file with name FILE_NAME, which will be
891 replaced after other extraction is done by a symbolic link if
892 IS_SYMLINK is true, and by a hard link otherwise. Set
893 *INTERDIR_MADE if an intermediate directory is made in the
897 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
902 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
904 switch (maybe_recoverable (file_name
, interdir_made
))
913 open_error (file_name
);
918 if (fstat (fd
, &st
) != 0)
920 stat_error (file_name
);
923 else if (close (fd
) != 0)
924 close_error (file_name
);
927 struct delayed_set_stat
*h
;
928 struct delayed_link
*p
=
929 xmalloc (offsetof (struct delayed_link
, target
)
930 + strlen (current_stat_info
.link_name
)
932 p
->next
= delayed_link_head
;
933 delayed_link_head
= p
;
936 p
->ctime
= get_stat_ctime (&st
);
937 p
->is_symlink
= is_symlink
;
940 p
->uid
= current_stat_info
.stat
.st_uid
;
941 p
->gid
= current_stat_info
.stat
.st_gid
;
943 p
->change_dir
= chdir_current
;
944 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
945 + strlen (file_name
) + 1);
946 p
->sources
->next
= 0;
947 strcpy (p
->sources
->string
, file_name
);
948 strcpy (p
->target
, current_stat_info
.link_name
);
950 h
= delayed_set_stat_head
;
951 if (h
&& ! h
->after_links
952 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
953 && ISSLASH (file_name
[h
->file_name_len
])
954 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
960 if (stat (h
->file_name
, &st
) != 0)
961 stat_error (h
->file_name
);
968 while ((h
= h
->next
) && ! h
->after_links
);
978 extract_link (char *file_name
, int typeflag
)
980 bool interdir_made
= false;
981 char const *link_name
;
984 link_name
= current_stat_info
.link_name
;
986 if (! absolute_names_option
&& contains_dot_dot (link_name
))
987 return create_placeholder_file (file_name
, false, &interdir_made
);
991 struct stat st1
, st2
;
993 int status
= link (link_name
, file_name
);
998 struct delayed_link
*ds
= delayed_link_head
;
999 if (ds
&& lstat (link_name
, &st1
) == 0)
1000 for (; ds
; ds
= ds
->next
)
1001 if (ds
->change_dir
== chdir_current
1002 && ds
->dev
== st1
.st_dev
1003 && ds
->ino
== st1
.st_ino
1004 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
1006 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1007 + strlen (file_name
) + 1);
1008 strcpy (p
->string
, file_name
);
1009 p
->next
= ds
->sources
;
1015 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1016 || (lstat (link_name
, &st1
) == 0
1017 && lstat (file_name
, &st2
) == 0
1018 && st1
.st_dev
== st2
.st_dev
1019 && st1
.st_ino
== st2
.st_ino
))
1024 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1026 if (rc
== RECOVER_SKIP
)
1028 if (!(incremental_option
&& errno
== EEXIST
))
1030 link_error (link_name
, file_name
);
1037 extract_symlink (char *file_name
, int typeflag
)
1040 bool interdir_made
= false;
1042 if (! absolute_names_option
1043 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1044 || contains_dot_dot (current_stat_info
.link_name
)))
1045 return create_placeholder_file (file_name
, true, &interdir_made
);
1047 while (symlink (current_stat_info
.link_name
, file_name
))
1048 switch (maybe_recoverable (file_name
, &interdir_made
))
1057 symlink_error (current_stat_info
.link_name
, file_name
);
1061 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
1065 static int warned_once
;
1070 WARNOPT (WARN_SYMBOLIC_CAST
,
1072 _("Attempting extraction of symbolic links as hard links")));
1074 return extract_link (file_name
, typeflag
);
1078 #if S_IFCHR || S_IFBLK
1080 extract_node (char *file_name
, int typeflag
)
1082 bool interdir_made
= false;
1083 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1084 mode_t invert_permissions
=
1085 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1087 while (mknod (file_name
, mode
^ invert_permissions
,
1088 current_stat_info
.stat
.st_rdev
))
1089 switch (maybe_recoverable (file_name
, &interdir_made
))
1098 mknod_error (file_name
);
1102 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1103 ARCHIVED_PERMSTATUS
, typeflag
);
1108 #if HAVE_MKFIFO || defined mkfifo
1110 extract_fifo (char *file_name
, int typeflag
)
1113 bool interdir_made
= false;
1114 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1115 mode_t invert_permissions
=
1116 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1118 while ((status
= mkfifo (file_name
, mode
)) != 0)
1119 switch (maybe_recoverable (file_name
, &interdir_made
))
1128 mkfifo_error (file_name
);
1132 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1133 ARCHIVED_PERMSTATUS
, typeflag
);
1139 extract_volhdr (char *file_name
, int typeflag
)
1146 extract_failure (char *file_name
, int typeflag
)
1151 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1155 /* Prepare to extract a file. Find extractor function.
1156 Return zero if extraction should not proceed. */
1159 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1163 if (EXTRACT_OVER_PIPE
)
1166 /* Select the extractor */
1169 case GNUTYPE_SPARSE
:
1170 *fun
= extract_file
;
1177 /* Appears to be a file. But BSD tar uses the convention that a slash
1178 suffix means a directory. */
1179 if (current_stat_info
.had_trailing_slash
)
1183 *fun
= extract_file
;
1189 *fun
= extract_symlink
;
1193 *fun
= extract_link
;
1198 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1199 *fun
= extract_node
;
1205 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1206 *fun
= extract_node
;
1210 #if HAVE_MKFIFO || defined mkfifo
1212 *fun
= extract_fifo
;
1217 case GNUTYPE_DUMPDIR
:
1219 if (current_stat_info
.is_dumpdir
)
1220 delay_directory_restore_option
= true;
1223 case GNUTYPE_VOLHDR
:
1224 *fun
= extract_volhdr
;
1227 case GNUTYPE_MULTIVOL
:
1229 _("%s: Cannot extract -- file is continued from another volume"),
1230 quotearg_colon (current_stat_info
.file_name
)));
1231 *fun
= extract_failure
;
1234 case GNUTYPE_LONGNAME
:
1235 case GNUTYPE_LONGLINK
:
1236 ERROR ((0, 0, _("Unexpected long name header")));
1237 *fun
= extract_failure
;
1241 WARNOPT (WARN_UNKNOWN_CAST
,
1243 _("%s: Unknown file type `%c', extracted as normal file"),
1244 quotearg_colon (file_name
), typeflag
));
1245 *fun
= extract_file
;
1248 /* Determine whether the extraction should proceed */
1252 switch (old_files_option
)
1254 case UNLINK_FIRST_OLD_FILES
:
1255 if (!remove_any_file (file_name
,
1256 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1257 : ORDINARY_REMOVE_OPTION
)
1258 && errno
&& errno
!= ENOENT
)
1260 unlink_error (file_name
);
1265 case KEEP_NEWER_FILES
:
1266 if (file_newer_p (file_name
, ¤t_stat_info
))
1268 WARNOPT (WARN_IGNORE_NEWER
,
1269 (0, 0, _("Current %s is newer or same age"),
1270 quote (file_name
)));
1282 /* Extract a file from the archive. */
1284 extract_archive (void)
1287 tar_extractor_t fun
;
1289 fatal_exit_hook
= extract_finish
;
1291 set_next_block_after (current_header
);
1293 if (!current_stat_info
.file_name
[0]
1294 || (interactive_option
1295 && !confirm ("extract", current_stat_info
.file_name
)))
1301 /* Print the block from current_header and current_stat. */
1303 print_header (¤t_stat_info
, current_header
, -1);
1305 /* Restore stats for all non-ancestor directories, unless
1306 it is an incremental archive.
1307 (see NOTICE in the comment to delay_set_stat above) */
1308 if (!delay_directory_restore_option
)
1310 int dir
= chdir_current
;
1311 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1315 /* Take a safety backup of a previously existing file. */
1318 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1321 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1322 quotearg_colon (current_stat_info
.file_name
)));
1327 /* Extract the archive entry according to its type. */
1329 typeflag
= sparse_member_p (¤t_stat_info
) ?
1330 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1332 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1334 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1336 undo_last_backup ();
1343 /* Extract the links whose final extraction were delayed. */
1345 apply_delayed_links (void)
1347 struct delayed_link
*ds
;
1349 for (ds
= delayed_link_head
; ds
; )
1351 struct string_list
*sources
= ds
->sources
;
1352 char const *valid_source
= 0;
1354 chdir_do (ds
->change_dir
);
1356 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1358 char const *source
= sources
->string
;
1361 /* Make sure the placeholder file is still there. If not,
1362 don't create a link, as the placeholder was probably
1363 removed by a later extraction. */
1364 if (lstat (source
, &st
) == 0
1365 && st
.st_dev
== ds
->dev
1366 && st
.st_ino
== ds
->ino
1367 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1369 /* Unlink the placeholder, then create a hard link if possible,
1370 a symbolic link otherwise. */
1371 if (unlink (source
) != 0)
1372 unlink_error (source
);
1373 else if (valid_source
&& link (valid_source
, source
) == 0)
1375 else if (!ds
->is_symlink
)
1377 if (link (ds
->target
, source
) != 0)
1378 link_error (ds
->target
, source
);
1380 else if (symlink (ds
->target
, source
) != 0)
1381 symlink_error (ds
->target
, source
);
1384 struct tar_stat_info st1
;
1385 st1
.stat
.st_uid
= ds
->uid
;
1386 st1
.stat
.st_gid
= ds
->gid
;
1387 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1388 valid_source
= source
;
1393 for (sources
= ds
->sources
; sources
; )
1395 struct string_list
*next
= sources
->next
;
1401 struct delayed_link
*next
= ds
->next
;
1407 delayed_link_head
= 0;
1410 /* Finish the extraction of an archive. */
1412 extract_finish (void)
1414 /* First, fix the status of ordinary directories that need fixing. */
1415 apply_nonancestor_delayed_set_stat ("", 0);
1417 /* Then, apply delayed links, so that they don't affect delayed
1418 directory status-setting for ordinary directories. */
1419 apply_delayed_links ();
1421 /* Finally, fix the status of directories that are ancestors
1422 of delayed links. */
1423 apply_nonancestor_delayed_set_stat ("", 1);
1427 rename_directory (char *src
, char *dst
)
1429 if (rename (src
, dst
))
1436 if (make_directories (dst
))
1438 if (rename (src
, dst
) == 0)
1445 /* FIXME: Fall back to recursive copying */
1451 ERROR ((0, e
, _("Cannot rename %s to %s"),