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. */
37 bool we_are_root
; /* true if our effective uid == 0 */
38 static mode_t newdir_umask
; /* umask when creating new directories */
39 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
41 /* Status of the permissions of a file that we are extracting. */
44 /* This file may have existed already; its permissions are unknown. */
47 /* This file was created using the permissions from the archive. */
50 /* This is an intermediate directory; the archive did not specify
55 /* List of directories whose statuses we need to extract after we've
56 finished extracting their subsidiary files. If you consider each
57 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
58 represents an element where AFTER_SYMLINKS is nonzero and [^D]
59 represents an element where AFTER_SYMLINKS is zero, then the head
60 of the subsequence has the longest name, and each non-head element
61 in the prefix is an ancestor (in the directory hierarchy) of the
64 struct delayed_set_stat
66 struct delayed_set_stat
*next
;
67 struct stat stat_info
;
69 mode_t invert_permissions
;
70 enum permstatus permstatus
;
75 static struct delayed_set_stat
*delayed_set_stat_head
;
77 /* List of symbolic links whose creation we have delayed. */
78 struct delayed_symlink
80 /* The next delayed symbolic link in the list. */
81 struct delayed_symlink
*next
;
83 /* The device, inode number and last-modified time of the placeholder. */
88 /* The desired owner and group of the symbolic link. */
92 /* A list of sources for this symlink. The sources are all to be
93 hard-linked together. */
94 struct string_list
*sources
;
96 /* The desired target of the desired link. */
100 static struct delayed_symlink
*delayed_symlink_head
;
104 struct string_list
*next
;
108 /* Set up to extract files. */
112 we_are_root
= geteuid () == 0;
113 same_permissions_option
+= we_are_root
;
114 same_owner_option
+= we_are_root
;
115 xalloc_fail_func
= extract_finish
;
117 /* Option -p clears the kernel umask, so it does not affect proper
118 restoration of file permissions. New intermediate directories will
119 comply with umask at start of program. */
121 newdir_umask
= umask (0);
122 if (0 < same_permissions_option
)
126 umask (newdir_umask
); /* restore the kernel umask */
127 current_umask
= newdir_umask
;
131 /* If restoring permissions, restore the mode for FILE_NAME from
132 information given in *STAT_INFO (where *CUR_INFO gives
133 the current status if CUR_INFO is nonzero); otherwise invert the
134 INVERT_PERMISSIONS bits from the file's current permissions.
135 PERMSTATUS specifies the status of the file's permissions.
136 TYPEFLAG specifies the type of the file. */
138 set_mode (char const *file_name
,
139 struct stat
const *stat_info
,
140 struct stat
const *cur_info
,
141 mode_t invert_permissions
, enum permstatus permstatus
,
146 if (0 < same_permissions_option
147 && permstatus
!= INTERDIR_PERMSTATUS
)
149 mode
= stat_info
->st_mode
;
151 /* If we created the file and it has a usual mode, then its mode
152 is normally set correctly already. But on many hosts, some
153 directories inherit the setgid bits from their parents, so we
154 we must set directories' modes explicitly. */
155 if (permstatus
== ARCHIVED_PERMSTATUS
156 && ! (mode
& ~ MODE_RWX
)
157 && typeflag
!= DIRTYPE
158 && typeflag
!= GNUTYPE_DUMPDIR
)
161 else if (! invert_permissions
)
165 /* We must inspect a directory's current permissions, since the
166 directory may have inherited its setgid bit from its parent.
168 INVERT_PERMISSIONS happens to be nonzero only for directories
169 that we created, so there's no point optimizing this code for
174 if (stat (file_name
, &st
) != 0)
176 stat_error (file_name
);
181 mode
= cur_info
->st_mode
^ invert_permissions
;
184 if (chmod (file_name
, mode
) != 0)
185 chmod_error_details (file_name
, mode
);
188 /* Check time after successfully setting FILE_NAME's time stamp to T. */
190 check_time (char const *file_name
, time_t t
)
194 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
195 file_name
, tartime (t
)));
196 else if (start_time
< t
&& (now
= time (0)) < t
)
197 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
198 file_name
, tartime (t
), (unsigned long) (t
- now
)));
201 /* Restore stat attributes (owner, group, mode and times) for
202 FILE_NAME, using information given in *STAT_INFO.
203 If CUR_INFO is nonzero, *CUR_INFO is the
204 file's currernt status.
205 If not restoring permissions, invert the
206 INVERT_PERMISSIONS bits from the file's current permissions.
207 PERMSTATUS specifies the status of the file's permissions.
208 TYPEFLAG specifies the type of the file. */
210 /* FIXME: About proper restoration of symbolic link attributes, we still do
211 not have it right. Pretesters' reports tell us we need further study and
212 probably more configuration. For now, just use lchown if it exists, and
213 punt for the rest. Sigh! */
216 set_stat (char const *file_name
,
217 struct stat
const *stat_info
,
218 struct stat
const *cur_info
,
219 mode_t invert_permissions
, enum permstatus permstatus
,
222 struct utimbuf utimbuf
;
224 if (typeflag
!= SYMTYPE
)
226 /* We do the utime before the chmod because some versions of utime are
227 broken and trash the modes of the file. */
229 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
231 /* We set the accessed time to `now', which is really the time we
232 started extracting files, unless incremental_option is used, in
233 which case .st_atime is used. */
235 /* FIXME: incremental_option should set ctime too, but how? */
237 if (incremental_option
)
238 utimbuf
.actime
= stat_info
->st_atime
;
240 utimbuf
.actime
= start_time
;
242 utimbuf
.modtime
= stat_info
->st_mtime
;
244 if (utime (file_name
, &utimbuf
) < 0)
245 utime_error (file_name
);
248 check_time (file_name
, utimbuf
.actime
);
249 check_time (file_name
, utimbuf
.modtime
);
253 /* Some systems allow non-root users to give files away. Once this
254 done, it is not possible anymore to change file permissions, so we
255 have to set permissions prior to possibly giving files away. */
257 set_mode (file_name
, stat_info
, cur_info
,
258 invert_permissions
, permstatus
, typeflag
);
261 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
263 /* When lchown exists, it should be used to change the attributes of
264 the symbolic link itself. In this case, a mere chown would change
265 the attributes of the file the symbolic link is pointing to, and
266 should be avoided. */
268 if (typeflag
== SYMTYPE
)
271 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
272 chown_error_details (file_name
,
273 stat_info
->st_uid
, stat_info
->st_gid
);
278 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
279 chown_error_details (file_name
,
280 stat_info
->st_uid
, stat_info
->st_gid
);
282 /* On a few systems, and in particular, those allowing to give files
283 away, changing the owner or group destroys the suid or sgid bits.
284 So let's attempt setting these bits once more. */
285 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
286 set_mode (file_name
, stat_info
, 0,
287 invert_permissions
, permstatus
, typeflag
);
292 /* Remember to restore stat attributes (owner, group, mode and times)
293 for the directory FILE_NAME, using information given in *STAT_INFO,
294 once we stop extracting files into that directory.
295 If not restoring permissions, remember to invert the
296 INVERT_PERMISSIONS bits from the file's current permissions.
297 PERMSTATUS specifies the status of the file's permissions. */
299 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
300 mode_t invert_permissions
, enum permstatus permstatus
)
302 size_t file_name_len
= strlen (file_name
);
303 struct delayed_set_stat
*data
=
304 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
305 + file_name_len
+ 1);
306 data
->file_name_len
= file_name_len
;
307 strcpy (data
->file_name
, file_name
);
308 data
->invert_permissions
= invert_permissions
;
309 data
->permstatus
= permstatus
;
310 data
->after_symlinks
= 0;
311 data
->stat_info
= *stat_info
;
312 data
->next
= delayed_set_stat_head
;
313 delayed_set_stat_head
= data
;
316 /* Update the delayed_set_stat info for an intermediate directory
317 created on the path to DIR_NAME. The intermediate directory turned
318 out to be the same as this directory, e.g. due to ".." or symbolic
319 links. *DIR_STAT_INFO is the status of the directory. */
321 repair_delayed_set_stat (char const *dir_name
,
322 struct stat
const *dir_stat_info
)
324 struct delayed_set_stat
*data
;
325 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
328 if (stat (data
->file_name
, &st
) != 0)
330 stat_error (data
->file_name
);
334 if (st
.st_dev
== dir_stat_info
->st_dev
335 && st
.st_ino
== dir_stat_info
->st_ino
)
337 data
->stat_info
= current_stat_info
.stat
;
338 data
->invert_permissions
=
339 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
340 data
->permstatus
= ARCHIVED_PERMSTATUS
;
345 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
346 quotearg_colon (dir_name
)));
349 /* After a file/link/symlink/directory creation has failed, see if
350 it's because some required directory was not present, and if so,
351 create all required directories. Return non-zero if a directory
354 make_directories (char *file_name
)
356 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
357 char *cursor
; /* points into path */
358 int did_something
= 0; /* did we do anything yet? */
360 int invert_permissions
;
364 for (cursor
= cursor0
; *cursor
; cursor
++)
366 if (! ISSLASH (*cursor
))
369 /* Avoid mkdir of empty string, if leading or double '/'. */
371 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
374 /* Avoid mkdir where last part of path is "." or "..". */
376 if (cursor
[-1] == '.'
377 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
378 || (cursor
[-2] == '.'
379 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
382 *cursor
= '\0'; /* truncate the path there */
383 mode
= MODE_RWX
& ~ newdir_umask
;
384 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
385 status
= mkdir (file_name
, mode
^ invert_permissions
);
389 /* Create a struct delayed_set_stat even if
390 invert_permissions is zero, because
391 repair_delayed_set_stat may need to update the struct. */
392 delay_set_stat (file_name
,
393 ¤t_stat_info
.stat
/* ignored */,
394 invert_permissions
, INTERDIR_PERMSTATUS
);
396 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
406 continue; /* Directory already exists. */
407 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
408 this. Reported by Warren Hyde
409 <Warren.Hyde@motorola.com> */
411 || errno
== EACCES
/* Turbo C mkdir gives a funny errno. */
414 && access (file_name
, W_OK
) == 0)
417 /* Some other error in the mkdir. We return to the caller. */
421 return did_something
; /* tell them to retry if we made one */
424 /* Prepare to extract a file.
425 Return zero if extraction should not proceed. */
428 prepare_to_extract (char const *file_name
, bool directory
)
430 if (to_stdout_option
)
433 if (old_files_option
== UNLINK_FIRST_OLD_FILES
434 && !remove_any_file (file_name
, recursive_unlink_option
)
435 && errno
&& errno
!= ENOENT
)
437 unlink_error (file_name
);
444 /* Attempt repairing what went wrong with the extraction. Delete an
445 already existing file or create missing intermediate directories.
446 Return nonzero if we somewhat increased our chances at a successful
447 extraction. errno is properly restored on zero return. */
449 maybe_recoverable (char *file_name
, int *interdir_made
)
457 /* Remove an old file, if the options allow this. */
459 switch (old_files_option
)
464 case DEFAULT_OLD_FILES
:
465 case NO_OVERWRITE_DIR_OLD_FILES
:
466 case OVERWRITE_OLD_FILES
:
468 int r
= remove_any_file (file_name
, 0);
475 /* Attempt creating missing intermediate directories. */
476 if (! make_directories (file_name
))
485 /* Just say we can't do anything about it... */
491 /* Translate the sparse information on the header, and in any
492 subsequent extended headers, into an array of structures with true
493 numbers, as opposed to character strings. Return nonzero if
496 This function invalidates current_header. */
499 fill_in_sparse_array (void)
501 off_t sparse_data_size
= current_stat_info
.stat
.st_size
;
502 off_t file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
505 union block
*h
= current_header
;
509 for (sparses
= 0; sparses
< SPARSES_IN_OLDGNU_HEADER
; sparses
++)
511 struct sparse
const *s
= &h
->oldgnu_header
.sp
[sparses
];
514 if (s
->numbytes
[0] == '\0')
516 sparsearray
[sparses
].offset
= offset
= OFF_FROM_HEADER (s
->offset
);
517 sparsearray
[sparses
].numbytes
= numbytes
=
518 SIZE_FROM_HEADER (s
->numbytes
);
519 sparse_data_size
-= numbytes
;
520 if (offset
< 0 || file_size
< offset
+ numbytes
|| sparse_data_size
< 0)
524 if (h
->oldgnu_header
.isextended
)
527 h
= find_next_block ();
530 ERROR ((0, 0, _("Unexpected EOF in archive")));
534 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
536 struct sparse
const *s
= &h
->sparse_header
.sp
[counter
];
539 if (s
->numbytes
[0] == '\0')
542 if (sparses
== sp_array_size
)
546 xrealloc (sparsearray
,
547 sp_array_size
* sizeof *sparsearray
);
550 sparsearray
[sparses
].offset
= offset
=
551 OFF_FROM_HEADER (s
->offset
);
552 sparsearray
[sparses
].numbytes
= numbytes
=
553 SIZE_FROM_HEADER (s
->numbytes
);
554 sparse_data_size
-= numbytes
;
555 if (offset
< 0 || file_size
< offset
+ numbytes
556 || sparse_data_size
< 0)
561 set_next_block_after (h
);
563 } while (h
->sparse_header
.isextended
);
568 ERROR ((0, 0, _("%s: invalid sparse archive member"),
569 current_stat_info
.file_name
));
575 extract_sparse_file (int fd
, char const *name
,
576 off_t sizeleft
, off_t file_size
)
580 while (sizeleft
!= 0)
584 union block
*data_block
= find_next_block ();
587 ERROR ((0, 0, _("Unexpected EOF in archive")));
590 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
592 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
595 written
= sparsearray
[sparse_ind
++].numbytes
;
596 while (written
> BLOCKSIZE
)
598 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
601 if (count
!= BLOCKSIZE
)
603 write_error_details (name
, count
, BLOCKSIZE
);
606 set_next_block_after (data_block
);
607 data_block
= find_next_block ();
610 ERROR ((0, 0, _("Unexpected EOF in archive")));
615 count
= full_write (fd
, data_block
->buffer
, written
);
618 if (count
!= written
)
620 write_error_details (name
, count
, written
);
624 set_next_block_after (data_block
);
627 if (ftruncate (fd
, file_size
) != 0)
628 truncate_error (name
);
633 /* Fix the statuses of all directories whose statuses need fixing, and
634 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
635 nonzero, do this for all such directories; otherwise, stop at the
636 first directory that is marked to be fixed up only after delayed
637 symlinks are applied. */
639 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
641 size_t file_name_len
= strlen (file_name
);
642 bool check_for_renamed_directories
= 0;
644 while (delayed_set_stat_head
)
646 struct delayed_set_stat
*data
= delayed_set_stat_head
;
647 bool skip_this_one
= 0;
649 struct stat
const *cur_info
= 0;
651 check_for_renamed_directories
|= data
->after_symlinks
;
653 if (after_symlinks
< data
->after_symlinks
654 || (data
->file_name_len
< file_name_len
655 && file_name
[data
->file_name_len
]
656 && (ISSLASH (file_name
[data
->file_name_len
])
657 || ISSLASH (file_name
[data
->file_name_len
- 1]))
658 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
661 if (check_for_renamed_directories
)
664 if (stat (data
->file_name
, &st
) != 0)
666 stat_error (data
->file_name
);
669 else if (! (st
.st_dev
== data
->stat_info
.st_dev
670 && (st
.st_ino
== data
->stat_info
.st_ino
)))
673 _("%s: Directory renamed before its status could be extracted"),
674 quotearg_colon (data
->file_name
)));
680 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
681 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
683 delayed_set_stat_head
= data
->next
;
688 /* Extract a file from the archive. */
690 extract_archive (void)
692 union block
*data_block
;
701 int interdir_made
= 0;
705 set_next_block_after (current_header
);
706 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
708 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
714 /* Print the block from current_header and current_stat. */
719 file_name
= safer_name_suffix (current_stat_info
.file_name
, 0);
720 if (strip_path_elements
)
722 file_name
= cut_path_elements (file_name
, strip_path_elements
);
730 apply_nonancestor_delayed_set_stat (file_name
, 0);
732 /* Take a safety backup of a previously existing file. */
734 if (backup_option
&& !to_stdout_option
)
735 if (!maybe_backup_file (file_name
, 0))
738 ERROR ((0, e
, _("%s: Was unable to backup this file"),
739 quotearg_colon (file_name
)));
744 /* Extract the archive entry according to its type. */
746 typeflag
= current_header
->header
.typeflag
;
750 file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
751 if (! fill_in_sparse_array ())
759 /* Appears to be a file. But BSD tar uses the convention that a slash
760 suffix means a directory. */
762 if (current_stat_info
.had_trailing_slash
)
765 /* FIXME: deal with protection issues. */
768 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
769 | (old_files_option
== OVERWRITE_OLD_FILES
772 mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
774 if (to_stdout_option
)
780 if (! prepare_to_extract (file_name
, 0))
789 /* Contiguous files (on the Masscomp) have to specify the size in
790 the open call that creates them. */
792 if (typeflag
== CONTTYPE
)
793 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
795 fd
= open (file_name
, openflag
, mode
);
797 #else /* not O_CTG */
798 if (typeflag
== CONTTYPE
)
800 static int conttype_diagnosed
;
802 if (!conttype_diagnosed
)
804 conttype_diagnosed
= 1;
805 WARN ((0, 0, _("Extracting contiguous files as regular files")));
808 fd
= open (file_name
, openflag
, mode
);
810 #endif /* not O_CTG */
814 if (maybe_recoverable (file_name
, &interdir_made
))
817 open_error (file_name
);
825 if (typeflag
== GNUTYPE_SPARSE
)
828 size_t name_length_bis
;
830 /* Kludge alert. NAME is assigned to header.name because
831 during the extraction, the space that contains the header
832 will get scribbled on, and the name will get munged, so any
833 error messages that happen to contain the filename will look
834 REAL interesting unless we do this. */
836 name_length_bis
= strlen (file_name
) + 1;
837 name
= xmalloc (name_length_bis
);
838 memcpy (name
, file_name
, name_length_bis
);
839 size
= extract_sparse_file (fd
, name
,
840 current_stat_info
.stat
.st_size
, file_size
);
844 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
846 if (multi_volume_option
)
848 assign_string (&save_name
, current_stat_info
.file_name
);
849 save_totsize
= current_stat_info
.stat
.st_size
;
850 save_sizeleft
= size
;
853 /* Locate data, determine max length writeable, write it,
854 block that we have used the data, then check if the write
857 data_block
= find_next_block ();
860 ERROR ((0, 0, _("Unexpected EOF in archive")));
861 break; /* FIXME: What happens, then? */
864 written
= available_space_after (data_block
);
869 count
= full_write (fd
, data_block
->buffer
, written
);
872 set_next_block_after ((union block
*)
873 (data_block
->buffer
+ written
- 1));
874 if (count
!= written
)
876 write_error_details (file_name
, count
, written
);
883 if (multi_volume_option
)
884 assign_string (&save_name
, 0);
886 /* If writing to stdout, don't try to do anything to the filename;
887 it doesn't exist, or we don't want to touch it anyway. */
889 if (to_stdout_option
)
895 close_error (file_name
);
900 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
901 (old_files_option
== OVERWRITE_OLD_FILES
903 : ARCHIVED_PERMSTATUS
),
909 if (! prepare_to_extract (file_name
, 0))
912 if (absolute_names_option
913 || ! (ISSLASH (current_stat_info
.link_name
914 [FILESYSTEM_PREFIX_LEN (current_stat_info
.link_name
)])
915 || contains_dot_dot (current_stat_info
.link_name
)))
917 while (status
= symlink (current_stat_info
.link_name
, file_name
),
919 if (!maybe_recoverable (file_name
, &interdir_made
))
923 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
925 symlink_error (current_stat_info
.link_name
, file_name
);
929 /* This symbolic link is potentially dangerous. Don't
930 create it now; instead, create a placeholder file, which
931 will be replaced after other extraction is done. */
934 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
936 if (! maybe_recoverable (file_name
, &interdir_made
))
941 open_error (file_name
);
942 else if (fstat (fd
, &st
) != 0)
944 stat_error (file_name
);
947 else if (close (fd
) != 0)
948 close_error (file_name
);
951 struct delayed_set_stat
*h
;
952 struct delayed_symlink
*p
=
953 xmalloc (offsetof (struct delayed_symlink
, target
)
954 + strlen (current_stat_info
.link_name
) + 1);
955 p
->next
= delayed_symlink_head
;
956 delayed_symlink_head
= p
;
959 p
->mtime
= st
.st_mtime
;
960 p
->uid
= current_stat_info
.stat
.st_uid
;
961 p
->gid
= current_stat_info
.stat
.st_gid
;
962 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
963 + strlen (file_name
) + 1);
964 p
->sources
->next
= 0;
965 strcpy (p
->sources
->string
, file_name
);
966 strcpy (p
->target
, current_stat_info
.link_name
);
968 h
= delayed_set_stat_head
;
969 if (h
&& ! h
->after_symlinks
970 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
971 && ISSLASH (file_name
[h
->file_name_len
])
972 && (base_name (file_name
)
973 == file_name
+ h
->file_name_len
+ 1))
977 h
->after_symlinks
= 1;
979 if (stat (h
->file_name
, &st
) != 0)
980 stat_error (h
->file_name
);
983 h
->stat_info
.st_dev
= st
.st_dev
;
984 h
->stat_info
.st_ino
= st
.st_ino
;
987 while ((h
= h
->next
) && ! h
->after_symlinks
);
994 if (status
!= 0 && backup_option
)
1000 static int warned_once
;
1006 _("Attempting extraction of symbolic links as hard links")));
1014 if (! prepare_to_extract (file_name
, 0))
1019 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, 1);
1020 struct stat st1
, st2
;
1023 /* MSDOS does not implement links. However, djgpp's link() actually
1025 status
= link (link_name
, file_name
);
1029 struct delayed_symlink
*ds
= delayed_symlink_head
;
1030 if (ds
&& stat (link_name
, &st1
) == 0)
1031 for (; ds
; ds
= ds
->next
)
1032 if (ds
->dev
== st1
.st_dev
1033 && ds
->ino
== st1
.st_ino
1034 && ds
->mtime
== st1
.st_mtime
)
1036 struct string_list
*p
=
1037 xmalloc (offsetof (struct string_list
, string
)
1038 + strlen (file_name
) + 1);
1039 strcpy (p
->string
, file_name
);
1040 p
->next
= ds
->sources
;
1046 if (maybe_recoverable (file_name
, &interdir_made
))
1049 if (incremental_option
&& errno
== EEXIST
)
1052 if (stat (link_name
, &st1
) == 0
1053 && stat (file_name
, &st2
) == 0
1054 && st1
.st_dev
== st2
.st_dev
1055 && st1
.st_ino
== st2
.st_ino
)
1058 link_error (link_name
, file_name
);
1060 undo_last_backup ();
1066 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1072 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1075 #if S_IFCHR || S_IFBLK
1077 if (! prepare_to_extract (file_name
, 0))
1080 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
1081 current_stat_info
.stat
.st_rdev
);
1084 if (maybe_recoverable (file_name
, &interdir_made
))
1086 mknod_error (file_name
);
1088 undo_last_backup ();
1091 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
1092 ARCHIVED_PERMSTATUS
, typeflag
);
1096 #if HAVE_MKFIFO || defined mkfifo
1098 if (! prepare_to_extract (file_name
, 0))
1101 while (status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
),
1103 if (!maybe_recoverable (file_name
, &interdir_made
))
1107 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
1108 ARCHIVED_PERMSTATUS
, typeflag
);
1111 mkfifo_error (file_name
);
1113 undo_last_backup ();
1119 case GNUTYPE_DUMPDIR
:
1121 if (incremental_option
)
1123 /* Read the entry and delete files that aren't listed in the
1126 gnu_restore (file_name
);
1128 else if (typeflag
== GNUTYPE_DUMPDIR
)
1131 mode
= ((current_stat_info
.stat
.st_mode
1132 | (we_are_root
? 0 : MODE_WXUSR
))
1135 status
= prepare_to_extract (file_name
, 1);
1139 goto directory_exists
;
1142 status
= mkdir (file_name
, mode
);
1148 || old_files_option
== DEFAULT_OLD_FILES
1149 || old_files_option
== OVERWRITE_OLD_FILES
))
1152 if (stat (file_name
, &st
) == 0)
1156 repair_delayed_set_stat (file_name
, &st
);
1159 if (S_ISDIR (st
.st_mode
))
1161 mode
= st
.st_mode
& ~ current_umask
;
1162 goto directory_exists
;
1168 if (maybe_recoverable (file_name
, &interdir_made
))
1171 if (errno
!= EEXIST
)
1173 mkdir_error (file_name
);
1175 undo_last_backup ();
1182 || old_files_option
== DEFAULT_OLD_FILES
1183 || old_files_option
== OVERWRITE_OLD_FILES
)
1184 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
1185 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
1187 ? ARCHIVED_PERMSTATUS
1188 : UNKNOWN_PERMSTATUS
));
1191 case GNUTYPE_VOLHDR
:
1193 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1200 case GNUTYPE_MULTIVOL
:
1202 _("%s: Cannot extract -- file is continued from another volume"),
1203 quotearg_colon (current_stat_info
.file_name
)));
1206 undo_last_backup ();
1209 case GNUTYPE_LONGNAME
:
1210 case GNUTYPE_LONGLINK
:
1211 ERROR ((0, 0, _("Visible long name error")));
1214 undo_last_backup ();
1219 _("%s: Unknown file type '%c', extracted as normal file"),
1220 quotearg_colon (file_name
), typeflag
));
1225 /* Extract the symbolic links whose final extraction were delayed. */
1227 apply_delayed_symlinks (void)
1229 struct delayed_symlink
*ds
;
1231 for (ds
= delayed_symlink_head
; ds
; )
1233 struct string_list
*sources
= ds
->sources
;
1234 char const *valid_source
= 0;
1236 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1238 char const *source
= sources
->string
;
1241 /* Make sure the placeholder file is still there. If not,
1242 don't create a symlink, as the placeholder was probably
1243 removed by a later extraction. */
1244 if (lstat (source
, &st
) == 0
1245 && st
.st_dev
== ds
->dev
1246 && st
.st_ino
== ds
->ino
1247 && st
.st_mtime
== ds
->mtime
)
1249 /* Unlink the placeholder, then create a hard link if possible,
1250 a symbolic link otherwise. */
1251 if (unlink (source
) != 0)
1252 unlink_error (source
);
1253 else if (valid_source
&& link (valid_source
, source
) == 0)
1255 else if (symlink (ds
->target
, source
) != 0)
1256 symlink_error (ds
->target
, source
);
1259 valid_source
= source
;
1260 st
.st_uid
= ds
->uid
;
1261 st
.st_gid
= ds
->gid
;
1262 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1267 for (sources
= ds
->sources
; sources
; )
1269 struct string_list
*next
= sources
->next
;
1275 struct delayed_symlink
*next
= ds
->next
;
1281 delayed_symlink_head
= 0;
1284 /* Finish the extraction of an archive. */
1286 extract_finish (void)
1288 /* First, fix the status of ordinary directories that need fixing. */
1289 apply_nonancestor_delayed_set_stat ("", 0);
1291 /* Then, apply delayed symlinks, so that they don't affect delayed
1292 directory status-setting for ordinary directories. */
1293 apply_delayed_symlinks ();
1295 /* Finally, fix the status of directories that are ancestors
1296 of delayed symlinks. */
1297 apply_nonancestor_delayed_set_stat ("", 1);
1304 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));