1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004 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 2, 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
39 bool we_are_root
; /* true if our effective uid == 0 */
40 static mode_t newdir_umask
; /* umask when creating new directories */
41 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
43 /* Status of the permissions of a file that we are extracting. */
46 /* This file may have existed already; its permissions are unknown. */
49 /* This file was created using the permissions from the archive. */
52 /* This is an intermediate directory; the archive did not specify
57 /* List of directories whose statuses we need to extract after we've
58 finished extracting their subsidiary files. If you consider each
59 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
60 represents an element where AFTER_SYMLINKS is nonzero and [^D]
61 represents an element where AFTER_SYMLINKS is zero, then the head
62 of the subsequence has the longest name, and each non-head element
63 in the prefix is an ancestor (in the directory hierarchy) of the
66 struct delayed_set_stat
68 struct delayed_set_stat
*next
;
69 struct stat stat_info
;
71 mode_t invert_permissions
;
72 enum permstatus permstatus
;
77 static struct delayed_set_stat
*delayed_set_stat_head
;
79 /* List of symbolic links whose creation we have delayed. */
80 struct delayed_symlink
82 /* The next delayed symbolic link in the list. */
83 struct delayed_symlink
*next
;
85 /* The device, inode number and last-modified time of the placeholder. */
90 /* The desired owner and group of the symbolic link. */
94 /* A list of sources for this symlink. The sources are all to be
95 hard-linked together. */
96 struct string_list
*sources
;
98 /* The desired target of the desired link. */
102 static struct delayed_symlink
*delayed_symlink_head
;
106 struct string_list
*next
;
110 /* Set up to extract files. */
114 we_are_root
= geteuid () == 0;
115 same_permissions_option
+= we_are_root
;
116 same_owner_option
+= we_are_root
;
118 /* Save 'root device' to avoid purging mount points.
119 FIXME: Should the same be done after handling -C option ? */
120 if (one_file_system_option
)
123 char *dir
= xgetcwd ();
125 if (deref_stat (true, dir
, &st
))
128 root_device
= st
.st_dev
;
131 /* Option -p clears the kernel umask, so it does not affect proper
132 restoration of file permissions. New intermediate directories will
133 comply with umask at start of program. */
135 newdir_umask
= umask (0);
136 if (0 < same_permissions_option
)
140 umask (newdir_umask
); /* restore the kernel umask */
141 current_umask
= newdir_umask
;
145 /* If restoring permissions, restore the mode for FILE_NAME from
146 information given in *STAT_INFO (where *CUR_INFO gives
147 the current status if CUR_INFO is nonzero); otherwise invert the
148 INVERT_PERMISSIONS bits from the file's current permissions.
149 PERMSTATUS specifies the status of the file's permissions.
150 TYPEFLAG specifies the type of the file. */
152 set_mode (char const *file_name
,
153 struct stat
const *stat_info
,
154 struct stat
const *cur_info
,
155 mode_t invert_permissions
, enum permstatus permstatus
,
160 if (0 < same_permissions_option
161 && permstatus
!= INTERDIR_PERMSTATUS
)
163 mode
= stat_info
->st_mode
;
165 /* If we created the file and it has a usual mode, then its mode
166 is normally set correctly already. But on many hosts, some
167 directories inherit the setgid bits from their parents, so we
168 we must set directories' modes explicitly. */
169 if (permstatus
== ARCHIVED_PERMSTATUS
170 && ! (mode
& ~ MODE_RWX
)
171 && typeflag
!= DIRTYPE
172 && typeflag
!= GNUTYPE_DUMPDIR
)
175 else if (! invert_permissions
)
179 /* We must inspect a directory's current permissions, since the
180 directory may have inherited its setgid bit from its parent.
182 INVERT_PERMISSIONS happens to be nonzero only for directories
183 that we created, so there's no point optimizing this code for
188 if (stat (file_name
, &st
) != 0)
190 stat_error (file_name
);
195 mode
= cur_info
->st_mode
^ invert_permissions
;
198 if (chmod (file_name
, mode
) != 0)
199 chmod_error_details (file_name
, mode
);
202 /* Check time after successfully setting FILE_NAME's time stamp to T. */
204 check_time (char const *file_name
, time_t t
)
208 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
209 file_name
, tartime (t
)));
210 else if (start_time
< t
&& (now
= time (0)) < t
)
211 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
212 file_name
, tartime (t
), (unsigned long) (t
- now
)));
215 /* Restore stat attributes (owner, group, mode and times) for
216 FILE_NAME, using information given in *STAT_INFO.
217 If CUR_INFO is nonzero, *CUR_INFO is the
218 file's currernt status.
219 If not restoring permissions, invert the
220 INVERT_PERMISSIONS bits from the file's current permissions.
221 PERMSTATUS specifies the status of the file's permissions.
222 TYPEFLAG specifies the type of the file. */
224 /* FIXME: About proper restoration of symbolic link attributes, we still do
225 not have it right. Pretesters' reports tell us we need further study and
226 probably more configuration. For now, just use lchown if it exists, and
227 punt for the rest. Sigh! */
230 set_stat (char const *file_name
,
231 struct stat
const *stat_info
,
232 struct stat
const *cur_info
,
233 mode_t invert_permissions
, enum permstatus permstatus
,
236 struct utimbuf utimbuf
;
238 if (typeflag
!= SYMTYPE
)
240 /* We do the utime before the chmod because some versions of utime are
241 broken and trash the modes of the file. */
243 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
245 /* We set the accessed time to `now', which is really the time we
246 started extracting files, unless incremental_option is used, in
247 which case .st_atime is used. */
249 /* FIXME: incremental_option should set ctime too, but how? */
251 if (incremental_option
)
252 utimbuf
.actime
= stat_info
->st_atime
;
254 utimbuf
.actime
= start_time
;
256 utimbuf
.modtime
= stat_info
->st_mtime
;
258 if (utime (file_name
, &utimbuf
) < 0)
259 utime_error (file_name
);
262 check_time (file_name
, utimbuf
.actime
);
263 check_time (file_name
, utimbuf
.modtime
);
267 /* Some systems allow non-root users to give files away. Once this
268 done, it is not possible anymore to change file permissions, so we
269 have to set permissions prior to possibly giving files away. */
271 set_mode (file_name
, stat_info
, cur_info
,
272 invert_permissions
, permstatus
, typeflag
);
275 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
277 /* When lchown exists, it should be used to change the attributes of
278 the symbolic link itself. In this case, a mere chown would change
279 the attributes of the file the symbolic link is pointing to, and
280 should be avoided. */
282 if (typeflag
== SYMTYPE
)
285 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
286 chown_error_details (file_name
,
287 stat_info
->st_uid
, stat_info
->st_gid
);
292 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
293 chown_error_details (file_name
,
294 stat_info
->st_uid
, stat_info
->st_gid
);
296 /* On a few systems, and in particular, those allowing to give files
297 away, changing the owner or group destroys the suid or sgid bits.
298 So let's attempt setting these bits once more. */
299 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
300 set_mode (file_name
, stat_info
, 0,
301 invert_permissions
, permstatus
, typeflag
);
306 /* Remember to restore stat attributes (owner, group, mode and times)
307 for the directory FILE_NAME, using information given in *STAT_INFO,
308 once we stop extracting files into that directory.
309 If not restoring permissions, remember to invert the
310 INVERT_PERMISSIONS bits from the file's current permissions.
311 PERMSTATUS specifies the status of the file's permissions. */
313 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
314 mode_t invert_permissions
, enum permstatus permstatus
)
316 size_t file_name_len
= strlen (file_name
);
317 struct delayed_set_stat
*data
=
318 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
319 + file_name_len
+ 1);
320 data
->file_name_len
= file_name_len
;
321 strcpy (data
->file_name
, file_name
);
322 data
->invert_permissions
= invert_permissions
;
323 data
->permstatus
= permstatus
;
324 data
->after_symlinks
= 0;
325 data
->stat_info
= *stat_info
;
326 data
->next
= delayed_set_stat_head
;
327 delayed_set_stat_head
= data
;
330 /* Update the delayed_set_stat info for an intermediate directory
331 created within the file name of DIR. The intermediate directory turned
332 out to be the same as this directory, e.g. due to ".." or symbolic
333 links. *DIR_STAT_INFO is the status of the directory. */
335 repair_delayed_set_stat (char const *dir
,
336 struct stat
const *dir_stat_info
)
338 struct delayed_set_stat
*data
;
339 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
342 if (stat (data
->file_name
, &st
) != 0)
344 stat_error (data
->file_name
);
348 if (st
.st_dev
== dir_stat_info
->st_dev
349 && st
.st_ino
== dir_stat_info
->st_ino
)
351 data
->stat_info
= current_stat_info
.stat
;
352 data
->invert_permissions
=
353 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
354 data
->permstatus
= ARCHIVED_PERMSTATUS
;
359 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
360 quotearg_colon (dir
)));
363 /* After a file/link/symlink/directory creation has failed, see if
364 it's because some required directory was not present, and if so,
365 create all required directories. Return non-zero if a directory
368 make_directories (char *file_name
)
370 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
371 char *cursor
; /* points into the file name */
372 int did_something
= 0; /* did we do anything yet? */
374 int invert_permissions
;
378 for (cursor
= cursor0
; *cursor
; cursor
++)
380 if (! ISSLASH (*cursor
))
383 /* Avoid mkdir of empty string, if leading or double '/'. */
385 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
388 /* Avoid mkdir where last part of file name is "." or "..". */
390 if (cursor
[-1] == '.'
391 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
392 || (cursor
[-2] == '.'
393 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
396 *cursor
= '\0'; /* truncate the name there */
397 mode
= MODE_RWX
& ~ newdir_umask
;
398 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
399 status
= mkdir (file_name
, mode
^ invert_permissions
);
403 /* Create a struct delayed_set_stat even if
404 invert_permissions is zero, because
405 repair_delayed_set_stat may need to update the struct. */
406 delay_set_stat (file_name
,
407 ¤t_stat_info
.stat
/* ignored */,
408 invert_permissions
, INTERDIR_PERMSTATUS
);
410 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
420 continue; /* Directory already exists. */
421 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
422 this. Reported by Warren Hyde
423 <Warren.Hyde@motorola.com> */
424 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
425 && access (file_name
, W_OK
) == 0)
428 /* Some other error in the mkdir. We return to the caller. */
432 return did_something
; /* tell them to retry if we made one */
436 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
440 if (stat (file_name
, &st
))
442 stat_warn (file_name
);
443 return true; /* Be on the safe side */
445 if (!S_ISDIR (st
.st_mode
)
446 && st
.st_mtime
>= tar_stat
->stat
.st_mtime
)
453 /* Attempt repairing what went wrong with the extraction. Delete an
454 already existing file or create missing intermediate directories.
455 Return nonzero if we somewhat increased our chances at a successful
456 extraction. errno is properly restored on zero return. */
458 maybe_recoverable (char *file_name
, int *interdir_made
)
468 /* Remove an old file, if the options allow this. */
470 switch (old_files_option
)
475 case KEEP_NEWER_FILES
:
476 if (file_newer_p (file_name
, ¤t_stat_info
))
483 case DEFAULT_OLD_FILES
:
484 case NO_OVERWRITE_DIR_OLD_FILES
:
485 case OVERWRITE_OLD_FILES
:
487 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
492 case UNLINK_FIRST_OLD_FILES
:
497 /* Attempt creating missing intermediate directories. */
498 if (! make_directories (file_name
))
507 /* Just say we can't do anything about it... */
513 /* Fix the statuses of all directories whose statuses need fixing, and
514 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
515 nonzero, do this for all such directories; otherwise, stop at the
516 first directory that is marked to be fixed up only after delayed
517 symlinks are applied. */
519 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
521 size_t file_name_len
= strlen (file_name
);
522 bool check_for_renamed_directories
= 0;
524 while (delayed_set_stat_head
)
526 struct delayed_set_stat
*data
= delayed_set_stat_head
;
527 bool skip_this_one
= 0;
529 struct stat
const *cur_info
= 0;
531 check_for_renamed_directories
|= data
->after_symlinks
;
533 if (after_symlinks
< data
->after_symlinks
534 || (data
->file_name_len
< file_name_len
535 && file_name
[data
->file_name_len
]
536 && (ISSLASH (file_name
[data
->file_name_len
])
537 || ISSLASH (file_name
[data
->file_name_len
- 1]))
538 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
541 if (check_for_renamed_directories
)
544 if (stat (data
->file_name
, &st
) != 0)
546 stat_error (data
->file_name
);
549 else if (! (st
.st_dev
== data
->stat_info
.st_dev
550 && (st
.st_ino
== data
->stat_info
.st_ino
)))
553 _("%s: Directory renamed before its status could be extracted"),
554 quotearg_colon (data
->file_name
)));
560 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
561 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
563 delayed_set_stat_head
= data
->next
;
570 /* Extractor functions for various member types */
573 extract_dir (char *file_name
, int typeflag
)
577 int interdir_made
= 0;
579 if (incremental_option
)
580 /* Read the entry and delete files that aren't listed in the archive. */
581 purge_directory (file_name
);
582 else if (typeflag
== GNUTYPE_DUMPDIR
)
585 mode
= (current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
)) & MODE_RWX
;
587 while ((status
= mkdir (file_name
, mode
)))
591 || old_files_option
== DEFAULT_OLD_FILES
592 || old_files_option
== OVERWRITE_OLD_FILES
))
595 if (stat (file_name
, &st
) == 0)
599 repair_delayed_set_stat (file_name
, &st
);
602 if (S_ISDIR (st
.st_mode
))
604 mode
= st
.st_mode
& ~ current_umask
;
611 if (maybe_recoverable (file_name
, &interdir_made
))
616 mkdir_error (file_name
);
623 || old_files_option
== DEFAULT_OLD_FILES
624 || old_files_option
== OVERWRITE_OLD_FILES
)
625 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
626 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
628 ? ARCHIVED_PERMSTATUS
629 : UNKNOWN_PERMSTATUS
));
636 open_output_file (char *file_name
, int typeflag
)
639 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
640 | (old_files_option
== OVERWRITE_OLD_FILES
643 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
646 /* Contiguous files (on the Masscomp) have to specify the size in
647 the open call that creates them. */
649 if (typeflag
== CONTTYPE
)
650 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
652 fd
= open (file_name
, openflag
, mode
);
654 #else /* not O_CTG */
655 if (typeflag
== CONTTYPE
)
657 static int conttype_diagnosed
;
659 if (!conttype_diagnosed
)
661 conttype_diagnosed
= 1;
662 WARN ((0, 0, _("Extracting contiguous files as regular files")));
665 fd
= open (file_name
, openflag
, mode
);
667 #endif /* not O_CTG */
673 extract_file (char *file_name
, int typeflag
)
677 union block
*data_block
;
681 int interdir_made
= 0;
683 /* FIXME: deal with protection issues. */
685 if (to_stdout_option
)
687 else if (to_command_option
)
689 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
699 fd
= open_output_file (file_name
, typeflag
);
700 while (fd
< 0 && maybe_recoverable (file_name
, &interdir_made
));
704 open_error (file_name
);
709 if (current_stat_info
.is_sparse
)
710 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
712 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
714 if (multi_volume_option
)
716 assign_string (&save_name
, current_stat_info
.orig_file_name
);
717 save_totsize
= current_stat_info
.stat
.st_size
;
718 save_sizeleft
= size
;
721 /* Locate data, determine max length writeable, write it,
722 block that we have used the data, then check if the write
725 data_block
= find_next_block ();
728 ERROR ((0, 0, _("Unexpected EOF in archive")));
729 break; /* FIXME: What happens, then? */
732 written
= available_space_after (data_block
);
737 count
= full_write (fd
, data_block
->buffer
, written
);
740 set_next_block_after ((union block
*)
741 (data_block
->buffer
+ written
- 1));
742 if (count
!= written
)
744 if (!to_command_option
)
745 write_error_details (file_name
, count
, written
);
746 /* FIXME: shouldn't we restore from backup? */
753 if (multi_volume_option
)
754 assign_string (&save_name
, 0);
756 /* If writing to stdout, don't try to do anything to the filename;
757 it doesn't exist, or we don't want to touch it anyway. */
759 if (to_stdout_option
)
764 close_error (file_name
);
766 if (to_command_option
)
769 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
770 (old_files_option
== OVERWRITE_OLD_FILES
?
771 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
778 extract_link (char *file_name
, int typeflag
)
780 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, true);
781 int interdir_made
= 0;
785 struct stat st1
, st2
;
787 int status
= link (link_name
, file_name
);
792 struct delayed_symlink
*ds
= delayed_symlink_head
;
793 if (ds
&& lstat (link_name
, &st1
) == 0)
794 for (; ds
; ds
= ds
->next
)
795 if (ds
->dev
== st1
.st_dev
796 && ds
->ino
== st1
.st_ino
797 && ds
->mtime
== st1
.st_mtime
)
799 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
800 + strlen (file_name
) + 1);
801 strcpy (p
->string
, file_name
);
802 p
->next
= ds
->sources
;
808 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
809 || (lstat (link_name
, &st1
) == 0
810 && lstat (file_name
, &st2
) == 0
811 && st1
.st_dev
== st2
.st_dev
812 && st1
.st_ino
== st2
.st_ino
))
817 while (maybe_recoverable (file_name
, &interdir_made
));
819 if (!(incremental_option
&& errno
== EEXIST
))
821 link_error (link_name
, file_name
);
828 extract_symlink (char *file_name
, int typeflag
)
832 int interdir_made
= 0;
834 if (absolute_names_option
835 || ! (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
836 || contains_dot_dot (current_stat_info
.link_name
)))
838 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
839 if (!maybe_recoverable (file_name
, &interdir_made
))
843 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
845 symlink_error (current_stat_info
.link_name
, file_name
);
849 /* This symbolic link is potentially dangerous. Don't
850 create it now; instead, create a placeholder file, which
851 will be replaced after other extraction is done. */
854 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
855 if (! maybe_recoverable (file_name
, &interdir_made
))
860 open_error (file_name
);
861 else if (fstat (fd
, &st
) != 0)
863 stat_error (file_name
);
866 else if (close (fd
) != 0)
867 close_error (file_name
);
870 struct delayed_set_stat
*h
;
871 struct delayed_symlink
*p
= xmalloc (offsetof (struct delayed_symlink
, target
)
872 + strlen (current_stat_info
.link_name
)
874 p
->next
= delayed_symlink_head
;
875 delayed_symlink_head
= p
;
878 p
->mtime
= st
.st_mtime
;
879 p
->uid
= current_stat_info
.stat
.st_uid
;
880 p
->gid
= current_stat_info
.stat
.st_gid
;
881 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
882 + strlen (file_name
) + 1);
883 p
->sources
->next
= 0;
884 strcpy (p
->sources
->string
, file_name
);
885 strcpy (p
->target
, current_stat_info
.link_name
);
887 h
= delayed_set_stat_head
;
888 if (h
&& ! h
->after_symlinks
889 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
890 && ISSLASH (file_name
[h
->file_name_len
])
891 && (base_name (file_name
) == file_name
+ h
->file_name_len
+ 1))
895 h
->after_symlinks
= 1;
897 if (stat (h
->file_name
, &st
) != 0)
898 stat_error (h
->file_name
);
901 h
->stat_info
.st_dev
= st
.st_dev
;
902 h
->stat_info
.st_ino
= st
.st_ino
;
905 while ((h
= h
->next
) && ! h
->after_symlinks
);
915 static int warned_once
;
920 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
922 return extract_link (file_name
, typeflag
);
926 #if S_IFCHR || S_IFBLK
928 extract_node (char *file_name
, int typeflag
)
931 int interdir_made
= 0;
934 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
935 current_stat_info
.stat
.st_rdev
);
936 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
939 mknod_error (file_name
);
941 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, ARCHIVED_PERMSTATUS
, typeflag
);
946 #if HAVE_MKFIFO || defined mkfifo
948 extract_fifo (char *file_name
, int typeflag
)
951 int interdir_made
= 0;
953 while ((status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
)))
954 if (!maybe_recoverable (file_name
, &interdir_made
))
958 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
959 ARCHIVED_PERMSTATUS
, typeflag
);
961 mkfifo_error (file_name
);
967 extract_mangle_wrapper (char *file_name
, int typeflag
)
975 extract_failure (char *file_name
, int typeflag
)
980 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
984 /* Prepare to extract a file. Find extractor function.
985 Return zero if extraction should not proceed. */
988 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
992 if (EXTRACT_OVER_PIPE
)
995 /* Select the extractor */
1006 /* Appears to be a file. But BSD tar uses the convention that a slash
1007 suffix means a directory. */
1008 if (current_stat_info
.had_trailing_slash
)
1012 *fun
= extract_file
;
1018 *fun
= extract_symlink
;
1022 *fun
= extract_link
;
1027 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1028 *fun
= extract_node
;
1034 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1035 *fun
= extract_node
;
1039 #if HAVE_MKFIFO || defined mkfifo
1041 *fun
= extract_fifo
;
1046 case GNUTYPE_DUMPDIR
:
1050 case GNUTYPE_VOLHDR
:
1052 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1057 *fun
= extract_mangle_wrapper
;
1060 case GNUTYPE_MULTIVOL
:
1062 _("%s: Cannot extract -- file is continued from another volume"),
1063 quotearg_colon (current_stat_info
.file_name
)));
1064 *fun
= extract_failure
;
1067 case GNUTYPE_LONGNAME
:
1068 case GNUTYPE_LONGLINK
:
1069 ERROR ((0, 0, _("Unexpected long name header")));
1070 *fun
= extract_failure
;
1075 _("%s: Unknown file type `%c', extracted as normal file"),
1076 quotearg_colon (file_name
), typeflag
));
1077 *fun
= extract_file
;
1080 /* Determine whether the extraction should proceed */
1084 switch (old_files_option
)
1086 case UNLINK_FIRST_OLD_FILES
:
1087 if (!remove_any_file (file_name
,
1088 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1089 : ORDINARY_REMOVE_OPTION
)
1090 && errno
&& errno
!= ENOENT
)
1092 unlink_error (file_name
);
1097 case KEEP_NEWER_FILES
:
1098 if (file_newer_p (file_name
, ¤t_stat_info
))
1100 WARN ((0, 0, _("Current %s is newer"), quote (file_name
)));
1112 /* Extract a file from the archive. */
1114 extract_archive (void)
1118 tar_extractor_t fun
;
1120 set_next_block_after (current_header
);
1121 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1123 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
1129 /* Print the block from current_header and current_stat. */
1132 print_header (¤t_stat_info
, -1);
1134 file_name
= safer_name_suffix (current_stat_info
.file_name
, false);
1135 if (strip_name_components
)
1137 size_t prefix_len
= stripped_prefix_len (file_name
, strip_name_components
);
1138 if (prefix_len
== (size_t) -1)
1143 file_name
+= prefix_len
;
1146 apply_nonancestor_delayed_set_stat (file_name
, 0);
1148 /* Take a safety backup of a previously existing file. */
1151 if (!maybe_backup_file (file_name
, 0))
1154 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1155 quotearg_colon (file_name
)));
1160 /* Extract the archive entry according to its type. */
1163 typeflag
= sparse_member_p (¤t_stat_info
) ?
1164 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1166 if (prepare_to_extract (file_name
, typeflag
, &fun
))
1168 if (fun
&& (*fun
) (file_name
, typeflag
) && backup_option
)
1169 undo_last_backup ();
1176 /* Extract the symbolic links whose final extraction were delayed. */
1178 apply_delayed_symlinks (void)
1180 struct delayed_symlink
*ds
;
1182 for (ds
= delayed_symlink_head
; ds
; )
1184 struct string_list
*sources
= ds
->sources
;
1185 char const *valid_source
= 0;
1187 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1189 char const *source
= sources
->string
;
1192 /* Make sure the placeholder file is still there. If not,
1193 don't create a symlink, as the placeholder was probably
1194 removed by a later extraction. */
1195 if (lstat (source
, &st
) == 0
1196 && st
.st_dev
== ds
->dev
1197 && st
.st_ino
== ds
->ino
1198 && st
.st_mtime
== ds
->mtime
)
1200 /* Unlink the placeholder, then create a hard link if possible,
1201 a symbolic link otherwise. */
1202 if (unlink (source
) != 0)
1203 unlink_error (source
);
1204 else if (valid_source
&& link (valid_source
, source
) == 0)
1206 else if (symlink (ds
->target
, source
) != 0)
1207 symlink_error (ds
->target
, source
);
1210 valid_source
= source
;
1211 st
.st_uid
= ds
->uid
;
1212 st
.st_gid
= ds
->gid
;
1213 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1218 for (sources
= ds
->sources
; sources
; )
1220 struct string_list
*next
= sources
->next
;
1226 struct delayed_symlink
*next
= ds
->next
;
1232 delayed_symlink_head
= 0;
1235 /* Finish the extraction of an archive. */
1237 extract_finish (void)
1239 /* First, fix the status of ordinary directories that need fixing. */
1240 apply_nonancestor_delayed_set_stat ("", 0);
1242 /* Then, apply delayed symlinks, so that they don't affect delayed
1243 directory status-setting for ordinary directories. */
1244 apply_delayed_symlinks ();
1246 /* Finally, fix the status of directories that are ancestors
1247 of delayed symlinks. */
1248 apply_nonancestor_delayed_set_stat ("", 1);
1255 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1262 error (0, 0, "%s", _("memory exhausted"));