1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003 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. */
38 bool we_are_root
; /* true if our effective uid == 0 */
39 static mode_t newdir_umask
; /* umask when creating new directories */
40 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
42 /* Status of the permissions of a file that we are extracting. */
45 /* This file may have existed already; its permissions are unknown. */
48 /* This file was created using the permissions from the archive. */
51 /* This is an intermediate directory; the archive did not specify
56 /* List of directories whose statuses we need to extract after we've
57 finished extracting their subsidiary files. If you consider each
58 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
59 represents an element where AFTER_SYMLINKS is nonzero and [^D]
60 represents an element where AFTER_SYMLINKS is zero, then the head
61 of the subsequence has the longest name, and each non-head element
62 in the prefix is an ancestor (in the directory hierarchy) of the
65 struct delayed_set_stat
67 struct delayed_set_stat
*next
;
68 struct stat stat_info
;
70 mode_t invert_permissions
;
71 enum permstatus permstatus
;
76 static struct delayed_set_stat
*delayed_set_stat_head
;
78 /* List of symbolic links whose creation we have delayed. */
79 struct delayed_symlink
81 /* The next delayed symbolic link in the list. */
82 struct delayed_symlink
*next
;
84 /* The device, inode number and last-modified time of the placeholder. */
89 /* The desired owner and group of the symbolic link. */
93 /* A list of sources for this symlink. The sources are all to be
94 hard-linked together. */
95 struct string_list
*sources
;
97 /* The desired target of the desired link. */
101 static struct delayed_symlink
*delayed_symlink_head
;
105 struct string_list
*next
;
109 /* Set up to extract files. */
113 we_are_root
= geteuid () == 0;
114 same_permissions_option
+= we_are_root
;
115 same_owner_option
+= we_are_root
;
116 xalloc_fail_func
= extract_finish
;
118 /* Option -p clears the kernel umask, so it does not affect proper
119 restoration of file permissions. New intermediate directories will
120 comply with umask at start of program. */
122 newdir_umask
= umask (0);
123 if (0 < same_permissions_option
)
127 umask (newdir_umask
); /* restore the kernel umask */
128 current_umask
= newdir_umask
;
132 /* If restoring permissions, restore the mode for FILE_NAME from
133 information given in *STAT_INFO (where *CUR_INFO gives
134 the current status if CUR_INFO is nonzero); otherwise invert the
135 INVERT_PERMISSIONS bits from the file's current permissions.
136 PERMSTATUS specifies the status of the file's permissions.
137 TYPEFLAG specifies the type of the file. */
139 set_mode (char const *file_name
,
140 struct stat
const *stat_info
,
141 struct stat
const *cur_info
,
142 mode_t invert_permissions
, enum permstatus permstatus
,
147 if (0 < same_permissions_option
148 && permstatus
!= INTERDIR_PERMSTATUS
)
150 mode
= stat_info
->st_mode
;
152 /* If we created the file and it has a usual mode, then its mode
153 is normally set correctly already. But on many hosts, some
154 directories inherit the setgid bits from their parents, so we
155 we must set directories' modes explicitly. */
156 if (permstatus
== ARCHIVED_PERMSTATUS
157 && ! (mode
& ~ MODE_RWX
)
158 && typeflag
!= DIRTYPE
159 && typeflag
!= GNUTYPE_DUMPDIR
)
162 else if (! invert_permissions
)
166 /* We must inspect a directory's current permissions, since the
167 directory may have inherited its setgid bit from its parent.
169 INVERT_PERMISSIONS happens to be nonzero only for directories
170 that we created, so there's no point optimizing this code for
175 if (stat (file_name
, &st
) != 0)
177 stat_error (file_name
);
182 mode
= cur_info
->st_mode
^ invert_permissions
;
185 if (chmod (file_name
, mode
) != 0)
186 chmod_error_details (file_name
, mode
);
189 /* Check time after successfully setting FILE_NAME's time stamp to T. */
191 check_time (char const *file_name
, time_t t
)
195 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
196 file_name
, tartime (t
)));
197 else if (start_time
< t
&& (now
= time (0)) < t
)
198 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
199 file_name
, tartime (t
), (unsigned long) (t
- now
)));
202 /* Restore stat attributes (owner, group, mode and times) for
203 FILE_NAME, using information given in *STAT_INFO.
204 If CUR_INFO is nonzero, *CUR_INFO is the
205 file's currernt status.
206 If not restoring permissions, invert the
207 INVERT_PERMISSIONS bits from the file's current permissions.
208 PERMSTATUS specifies the status of the file's permissions.
209 TYPEFLAG specifies the type of the file. */
211 /* FIXME: About proper restoration of symbolic link attributes, we still do
212 not have it right. Pretesters' reports tell us we need further study and
213 probably more configuration. For now, just use lchown if it exists, and
214 punt for the rest. Sigh! */
217 set_stat (char const *file_name
,
218 struct stat
const *stat_info
,
219 struct stat
const *cur_info
,
220 mode_t invert_permissions
, enum permstatus permstatus
,
223 struct utimbuf utimbuf
;
225 if (typeflag
!= SYMTYPE
)
227 /* We do the utime before the chmod because some versions of utime are
228 broken and trash the modes of the file. */
230 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
232 /* We set the accessed time to `now', which is really the time we
233 started extracting files, unless incremental_option is used, in
234 which case .st_atime is used. */
236 /* FIXME: incremental_option should set ctime too, but how? */
238 if (incremental_option
)
239 utimbuf
.actime
= stat_info
->st_atime
;
241 utimbuf
.actime
= start_time
;
243 utimbuf
.modtime
= stat_info
->st_mtime
;
245 if (utime (file_name
, &utimbuf
) < 0)
246 utime_error (file_name
);
249 check_time (file_name
, utimbuf
.actime
);
250 check_time (file_name
, utimbuf
.modtime
);
254 /* Some systems allow non-root users to give files away. Once this
255 done, it is not possible anymore to change file permissions, so we
256 have to set permissions prior to possibly giving files away. */
258 set_mode (file_name
, stat_info
, cur_info
,
259 invert_permissions
, permstatus
, typeflag
);
262 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
264 /* When lchown exists, it should be used to change the attributes of
265 the symbolic link itself. In this case, a mere chown would change
266 the attributes of the file the symbolic link is pointing to, and
267 should be avoided. */
269 if (typeflag
== SYMTYPE
)
272 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
273 chown_error_details (file_name
,
274 stat_info
->st_uid
, stat_info
->st_gid
);
279 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
280 chown_error_details (file_name
,
281 stat_info
->st_uid
, stat_info
->st_gid
);
283 /* On a few systems, and in particular, those allowing to give files
284 away, changing the owner or group destroys the suid or sgid bits.
285 So let's attempt setting these bits once more. */
286 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
287 set_mode (file_name
, stat_info
, 0,
288 invert_permissions
, permstatus
, typeflag
);
293 /* Remember to restore stat attributes (owner, group, mode and times)
294 for the directory FILE_NAME, using information given in *STAT_INFO,
295 once we stop extracting files into that directory.
296 If not restoring permissions, remember to invert the
297 INVERT_PERMISSIONS bits from the file's current permissions.
298 PERMSTATUS specifies the status of the file's permissions. */
300 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
301 mode_t invert_permissions
, enum permstatus permstatus
)
303 size_t file_name_len
= strlen (file_name
);
304 struct delayed_set_stat
*data
=
305 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
306 + file_name_len
+ 1);
307 data
->file_name_len
= file_name_len
;
308 strcpy (data
->file_name
, file_name
);
309 data
->invert_permissions
= invert_permissions
;
310 data
->permstatus
= permstatus
;
311 data
->after_symlinks
= 0;
312 data
->stat_info
= *stat_info
;
313 data
->next
= delayed_set_stat_head
;
314 delayed_set_stat_head
= data
;
317 /* Update the delayed_set_stat info for an intermediate directory
318 created on the path to DIR_NAME. The intermediate directory turned
319 out to be the same as this directory, e.g. due to ".." or symbolic
320 links. *DIR_STAT_INFO is the status of the directory. */
322 repair_delayed_set_stat (char const *dir_name
,
323 struct stat
const *dir_stat_info
)
325 struct delayed_set_stat
*data
;
326 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
329 if (stat (data
->file_name
, &st
) != 0)
331 stat_error (data
->file_name
);
335 if (st
.st_dev
== dir_stat_info
->st_dev
336 && st
.st_ino
== dir_stat_info
->st_ino
)
338 data
->stat_info
= current_stat_info
.stat
;
339 data
->invert_permissions
=
340 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
341 data
->permstatus
= ARCHIVED_PERMSTATUS
;
346 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
347 quotearg_colon (dir_name
)));
350 /* After a file/link/symlink/directory creation has failed, see if
351 it's because some required directory was not present, and if so,
352 create all required directories. Return non-zero if a directory
355 make_directories (char *file_name
)
357 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
358 char *cursor
; /* points into path */
359 int did_something
= 0; /* did we do anything yet? */
361 int invert_permissions
;
365 for (cursor
= cursor0
; *cursor
; cursor
++)
367 if (! ISSLASH (*cursor
))
370 /* Avoid mkdir of empty string, if leading or double '/'. */
372 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
375 /* Avoid mkdir where last part of path is "." or "..". */
377 if (cursor
[-1] == '.'
378 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
379 || (cursor
[-2] == '.'
380 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
383 *cursor
= '\0'; /* truncate the path there */
384 mode
= MODE_RWX
& ~ newdir_umask
;
385 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
386 status
= mkdir (file_name
, mode
^ invert_permissions
);
390 /* Create a struct delayed_set_stat even if
391 invert_permissions is zero, because
392 repair_delayed_set_stat may need to update the struct. */
393 delay_set_stat (file_name
,
394 ¤t_stat_info
.stat
/* ignored */,
395 invert_permissions
, INTERDIR_PERMSTATUS
);
397 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
407 continue; /* Directory already exists. */
408 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
409 this. Reported by Warren Hyde
410 <Warren.Hyde@motorola.com> */
411 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
412 && access (file_name
, W_OK
) == 0)
415 /* Some other error in the mkdir. We return to the caller. */
419 return did_something
; /* tell them to retry if we made one */
422 /* Prepare to extract a file.
423 Return zero if extraction should not proceed. */
426 prepare_to_extract (char const *file_name
, bool directory
)
428 if (to_stdout_option
)
431 if (old_files_option
== UNLINK_FIRST_OLD_FILES
432 && !remove_any_file (file_name
, recursive_unlink_option
)
433 && errno
&& errno
!= ENOENT
)
435 unlink_error (file_name
);
442 /* Attempt repairing what went wrong with the extraction. Delete an
443 already existing file or create missing intermediate directories.
444 Return nonzero if we somewhat increased our chances at a successful
445 extraction. errno is properly restored on zero return. */
447 maybe_recoverable (char *file_name
, int *interdir_made
)
455 /* Remove an old file, if the options allow this. */
457 switch (old_files_option
)
462 case DEFAULT_OLD_FILES
:
463 case NO_OVERWRITE_DIR_OLD_FILES
:
464 case OVERWRITE_OLD_FILES
:
466 int r
= remove_any_file (file_name
, 0);
473 /* Attempt creating missing intermediate directories. */
474 if (! make_directories (file_name
))
483 /* Just say we can't do anything about it... */
489 /* Fix the statuses of all directories whose statuses need fixing, and
490 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
491 nonzero, do this for all such directories; otherwise, stop at the
492 first directory that is marked to be fixed up only after delayed
493 symlinks are applied. */
495 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
497 size_t file_name_len
= strlen (file_name
);
498 bool check_for_renamed_directories
= 0;
500 while (delayed_set_stat_head
)
502 struct delayed_set_stat
*data
= delayed_set_stat_head
;
503 bool skip_this_one
= 0;
505 struct stat
const *cur_info
= 0;
507 check_for_renamed_directories
|= data
->after_symlinks
;
509 if (after_symlinks
< data
->after_symlinks
510 || (data
->file_name_len
< file_name_len
511 && file_name
[data
->file_name_len
]
512 && (ISSLASH (file_name
[data
->file_name_len
])
513 || ISSLASH (file_name
[data
->file_name_len
- 1]))
514 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
517 if (check_for_renamed_directories
)
520 if (stat (data
->file_name
, &st
) != 0)
522 stat_error (data
->file_name
);
525 else if (! (st
.st_dev
== data
->stat_info
.st_dev
526 && (st
.st_ino
== data
->stat_info
.st_ino
)))
529 _("%s: Directory renamed before its status could be extracted"),
530 quotearg_colon (data
->file_name
)));
536 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
537 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
539 delayed_set_stat_head
= data
->next
;
544 /* Extract a file from the archive. */
546 extract_archive (void)
548 union block
*data_block
;
557 int interdir_made
= 0;
561 set_next_block_after (current_header
);
562 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
564 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
570 /* Print the block from current_header and current_stat. */
573 print_header (¤t_stat_info
, -1);
575 file_name
= safer_name_suffix (current_stat_info
.file_name
, 0);
576 if (strip_path_elements
)
578 size_t prefix_len
= stripped_prefix_len (file_name
, strip_path_elements
);
579 if (prefix_len
== (size_t) -1)
584 file_name
+= prefix_len
;
587 apply_nonancestor_delayed_set_stat (file_name
, 0);
589 /* Take a safety backup of a previously existing file. */
591 if (backup_option
&& !to_stdout_option
)
592 if (!maybe_backup_file (file_name
, 0))
595 ERROR ((0, e
, _("%s: Was unable to backup this file"),
596 quotearg_colon (file_name
)));
601 /* Extract the archive entry according to its type. */
603 typeflag
= current_header
->header
.typeflag
;
613 /* Appears to be a file. But BSD tar uses the convention that a slash
614 suffix means a directory. */
616 if (current_stat_info
.had_trailing_slash
)
619 /* FIXME: deal with protection issues. */
622 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
623 | (old_files_option
== OVERWRITE_OLD_FILES
626 mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
628 if (to_stdout_option
)
634 if (! prepare_to_extract (file_name
, 0))
643 /* Contiguous files (on the Masscomp) have to specify the size in
644 the open call that creates them. */
646 if (typeflag
== CONTTYPE
)
647 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
649 fd
= open (file_name
, openflag
, mode
);
651 #else /* not O_CTG */
652 if (typeflag
== CONTTYPE
)
654 static int conttype_diagnosed
;
656 if (!conttype_diagnosed
)
658 conttype_diagnosed
= 1;
659 WARN ((0, 0, _("Extracting contiguous files as regular files")));
662 fd
= open (file_name
, openflag
, mode
);
664 #endif /* not O_CTG */
668 if (maybe_recoverable (file_name
, &interdir_made
))
671 open_error (file_name
);
679 if (typeflag
== GNUTYPE_SPARSE
)
681 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
684 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
686 if (multi_volume_option
)
688 assign_string (&save_name
, current_stat_info
.file_name
);
689 save_totsize
= current_stat_info
.stat
.st_size
;
690 save_sizeleft
= size
;
693 /* Locate data, determine max length writeable, write it,
694 block that we have used the data, then check if the write
697 data_block
= find_next_block ();
700 ERROR ((0, 0, _("Unexpected EOF in archive")));
701 break; /* FIXME: What happens, then? */
704 written
= available_space_after (data_block
);
709 count
= full_write (fd
, data_block
->buffer
, written
);
712 set_next_block_after ((union block
*)
713 (data_block
->buffer
+ written
- 1));
714 if (count
!= written
)
716 write_error_details (file_name
, count
, written
);
723 if (multi_volume_option
)
724 assign_string (&save_name
, 0);
726 /* If writing to stdout, don't try to do anything to the filename;
727 it doesn't exist, or we don't want to touch it anyway. */
729 if (to_stdout_option
)
735 close_error (file_name
);
740 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
741 (old_files_option
== OVERWRITE_OLD_FILES
743 : ARCHIVED_PERMSTATUS
),
749 if (! prepare_to_extract (file_name
, 0))
752 if (absolute_names_option
753 || ! (ISSLASH (current_stat_info
.link_name
754 [FILESYSTEM_PREFIX_LEN (current_stat_info
.link_name
)])
755 || contains_dot_dot (current_stat_info
.link_name
)))
757 while (status
= symlink (current_stat_info
.link_name
, file_name
),
759 if (!maybe_recoverable (file_name
, &interdir_made
))
763 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
765 symlink_error (current_stat_info
.link_name
, file_name
);
769 /* This symbolic link is potentially dangerous. Don't
770 create it now; instead, create a placeholder file, which
771 will be replaced after other extraction is done. */
774 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
776 if (! maybe_recoverable (file_name
, &interdir_made
))
781 open_error (file_name
);
782 else if (fstat (fd
, &st
) != 0)
784 stat_error (file_name
);
787 else if (close (fd
) != 0)
788 close_error (file_name
);
791 struct delayed_set_stat
*h
;
792 struct delayed_symlink
*p
=
793 xmalloc (offsetof (struct delayed_symlink
, target
)
794 + strlen (current_stat_info
.link_name
) + 1);
795 p
->next
= delayed_symlink_head
;
796 delayed_symlink_head
= p
;
799 p
->mtime
= st
.st_mtime
;
800 p
->uid
= current_stat_info
.stat
.st_uid
;
801 p
->gid
= current_stat_info
.stat
.st_gid
;
802 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
803 + strlen (file_name
) + 1);
804 p
->sources
->next
= 0;
805 strcpy (p
->sources
->string
, file_name
);
806 strcpy (p
->target
, current_stat_info
.link_name
);
808 h
= delayed_set_stat_head
;
809 if (h
&& ! h
->after_symlinks
810 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
811 && ISSLASH (file_name
[h
->file_name_len
])
812 && (base_name (file_name
)
813 == file_name
+ h
->file_name_len
+ 1))
817 h
->after_symlinks
= 1;
819 if (stat (h
->file_name
, &st
) != 0)
820 stat_error (h
->file_name
);
823 h
->stat_info
.st_dev
= st
.st_dev
;
824 h
->stat_info
.st_ino
= st
.st_ino
;
827 while ((h
= h
->next
) && ! h
->after_symlinks
);
834 if (status
!= 0 && backup_option
)
840 static int warned_once
;
846 _("Attempting extraction of symbolic links as hard links")));
854 if (! prepare_to_extract (file_name
, 0))
859 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, 1);
860 struct stat st1
, st2
;
863 /* MSDOS does not implement links. However, djgpp's link() actually
865 status
= link (link_name
, file_name
);
869 struct delayed_symlink
*ds
= delayed_symlink_head
;
870 if (ds
&& stat (link_name
, &st1
) == 0)
871 for (; ds
; ds
= ds
->next
)
872 if (ds
->dev
== st1
.st_dev
873 && ds
->ino
== st1
.st_ino
874 && ds
->mtime
== st1
.st_mtime
)
876 struct string_list
*p
=
877 xmalloc (offsetof (struct string_list
, string
)
878 + strlen (file_name
) + 1);
879 strcpy (p
->string
, file_name
);
880 p
->next
= ds
->sources
;
886 if (maybe_recoverable (file_name
, &interdir_made
))
889 if (incremental_option
&& errno
== EEXIST
)
892 if (stat (link_name
, &st1
) == 0
893 && stat (file_name
, &st2
) == 0
894 && st1
.st_dev
== st2
.st_dev
895 && st1
.st_ino
== st2
.st_ino
)
898 link_error (link_name
, file_name
);
906 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
912 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
915 #if S_IFCHR || S_IFBLK
917 if (! prepare_to_extract (file_name
, 0))
920 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
921 current_stat_info
.stat
.st_rdev
);
924 if (maybe_recoverable (file_name
, &interdir_made
))
926 mknod_error (file_name
);
931 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
932 ARCHIVED_PERMSTATUS
, typeflag
);
936 #if HAVE_MKFIFO || defined mkfifo
938 if (! prepare_to_extract (file_name
, 0))
941 while (status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
),
943 if (!maybe_recoverable (file_name
, &interdir_made
))
947 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
948 ARCHIVED_PERMSTATUS
, typeflag
);
951 mkfifo_error (file_name
);
959 case GNUTYPE_DUMPDIR
:
961 if (incremental_option
)
963 /* Read the entry and delete files that aren't listed in the
966 gnu_restore (file_name
);
968 else if (typeflag
== GNUTYPE_DUMPDIR
)
971 mode
= ((current_stat_info
.stat
.st_mode
972 | (we_are_root
? 0 : MODE_WXUSR
))
975 status
= prepare_to_extract (file_name
, 1);
979 goto directory_exists
;
982 status
= mkdir (file_name
, mode
);
988 || old_files_option
== DEFAULT_OLD_FILES
989 || old_files_option
== OVERWRITE_OLD_FILES
))
992 if (stat (file_name
, &st
) == 0)
996 repair_delayed_set_stat (file_name
, &st
);
999 if (S_ISDIR (st
.st_mode
))
1001 mode
= st
.st_mode
& ~ current_umask
;
1002 goto directory_exists
;
1008 if (maybe_recoverable (file_name
, &interdir_made
))
1011 if (errno
!= EEXIST
)
1013 mkdir_error (file_name
);
1015 undo_last_backup ();
1022 || old_files_option
== DEFAULT_OLD_FILES
1023 || old_files_option
== OVERWRITE_OLD_FILES
)
1024 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
1025 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
1027 ? ARCHIVED_PERMSTATUS
1028 : UNKNOWN_PERMSTATUS
));
1031 case GNUTYPE_VOLHDR
:
1033 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1040 case GNUTYPE_MULTIVOL
:
1042 _("%s: Cannot extract -- file is continued from another volume"),
1043 quotearg_colon (current_stat_info
.file_name
)));
1046 undo_last_backup ();
1049 case GNUTYPE_LONGNAME
:
1050 case GNUTYPE_LONGLINK
:
1051 ERROR ((0, 0, _("Visible long name error")));
1054 undo_last_backup ();
1059 _("%s: Unknown file type '%c', extracted as normal file"),
1060 quotearg_colon (file_name
), typeflag
));
1065 /* Extract the symbolic links whose final extraction were delayed. */
1067 apply_delayed_symlinks (void)
1069 struct delayed_symlink
*ds
;
1071 for (ds
= delayed_symlink_head
; ds
; )
1073 struct string_list
*sources
= ds
->sources
;
1074 char const *valid_source
= 0;
1076 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1078 char const *source
= sources
->string
;
1081 /* Make sure the placeholder file is still there. If not,
1082 don't create a symlink, as the placeholder was probably
1083 removed by a later extraction. */
1084 if (lstat (source
, &st
) == 0
1085 && st
.st_dev
== ds
->dev
1086 && st
.st_ino
== ds
->ino
1087 && st
.st_mtime
== ds
->mtime
)
1089 /* Unlink the placeholder, then create a hard link if possible,
1090 a symbolic link otherwise. */
1091 if (unlink (source
) != 0)
1092 unlink_error (source
);
1093 else if (valid_source
&& link (valid_source
, source
) == 0)
1095 else if (symlink (ds
->target
, source
) != 0)
1096 symlink_error (ds
->target
, source
);
1099 valid_source
= source
;
1100 st
.st_uid
= ds
->uid
;
1101 st
.st_gid
= ds
->gid
;
1102 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1107 for (sources
= ds
->sources
; sources
; )
1109 struct string_list
*next
= sources
->next
;
1115 struct delayed_symlink
*next
= ds
->next
;
1121 delayed_symlink_head
= 0;
1124 /* Finish the extraction of an archive. */
1126 extract_finish (void)
1128 /* First, fix the status of ordinary directories that need fixing. */
1129 apply_nonancestor_delayed_set_stat ("", 0);
1131 /* Then, apply delayed symlinks, so that they don't affect delayed
1132 directory status-setting for ordinary directories. */
1133 apply_delayed_symlinks ();
1135 /* Finally, fix the status of directories that are ancestors
1136 of delayed symlinks. */
1137 apply_nonancestor_delayed_set_stat ("", 1);
1144 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));