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);
721 apply_nonancestor_delayed_set_stat (file_name
, 0);
723 /* Take a safety backup of a previously existing file. */
725 if (backup_option
&& !to_stdout_option
)
726 if (!maybe_backup_file (file_name
, 0))
729 ERROR ((0, e
, _("%s: Was unable to backup this file"),
730 quotearg_colon (file_name
)));
735 /* Extract the archive entry according to its type. */
737 typeflag
= current_header
->header
.typeflag
;
741 file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
742 if (! fill_in_sparse_array ())
750 /* Appears to be a file. But BSD tar uses the convention that a slash
751 suffix means a directory. */
753 if (current_stat_info
.had_trailing_slash
)
756 /* FIXME: deal with protection issues. */
759 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
760 | (old_files_option
== OVERWRITE_OLD_FILES
763 mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
765 if (to_stdout_option
)
771 if (! prepare_to_extract (file_name
, 0))
780 /* Contiguous files (on the Masscomp) have to specify the size in
781 the open call that creates them. */
783 if (typeflag
== CONTTYPE
)
784 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
786 fd
= open (file_name
, openflag
, mode
);
788 #else /* not O_CTG */
789 if (typeflag
== CONTTYPE
)
791 static int conttype_diagnosed
;
793 if (!conttype_diagnosed
)
795 conttype_diagnosed
= 1;
796 WARN ((0, 0, _("Extracting contiguous files as regular files")));
799 fd
= open (file_name
, openflag
, mode
);
801 #endif /* not O_CTG */
805 if (maybe_recoverable (file_name
, &interdir_made
))
808 open_error (file_name
);
816 if (typeflag
== GNUTYPE_SPARSE
)
819 size_t name_length_bis
;
821 /* Kludge alert. NAME is assigned to header.name because
822 during the extraction, the space that contains the header
823 will get scribbled on, and the name will get munged, so any
824 error messages that happen to contain the filename will look
825 REAL interesting unless we do this. */
827 name_length_bis
= strlen (file_name
) + 1;
828 name
= xmalloc (name_length_bis
);
829 memcpy (name
, file_name
, name_length_bis
);
830 size
= extract_sparse_file (fd
, name
,
831 current_stat_info
.stat
.st_size
, file_size
);
835 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
837 if (multi_volume_option
)
839 assign_string (&save_name
, current_stat_info
.file_name
);
840 save_totsize
= current_stat_info
.stat
.st_size
;
841 save_sizeleft
= size
;
844 /* Locate data, determine max length writeable, write it,
845 block that we have used the data, then check if the write
848 data_block
= find_next_block ();
851 ERROR ((0, 0, _("Unexpected EOF in archive")));
852 break; /* FIXME: What happens, then? */
855 written
= available_space_after (data_block
);
860 count
= full_write (fd
, data_block
->buffer
, written
);
863 set_next_block_after ((union block
*)
864 (data_block
->buffer
+ written
- 1));
865 if (count
!= written
)
867 write_error_details (file_name
, count
, written
);
874 if (multi_volume_option
)
875 assign_string (&save_name
, 0);
877 /* If writing to stdout, don't try to do anything to the filename;
878 it doesn't exist, or we don't want to touch it anyway. */
880 if (to_stdout_option
)
886 close_error (file_name
);
891 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
892 (old_files_option
== OVERWRITE_OLD_FILES
894 : ARCHIVED_PERMSTATUS
),
900 if (! prepare_to_extract (file_name
, 0))
903 if (absolute_names_option
904 || ! (ISSLASH (current_stat_info
.link_name
905 [FILESYSTEM_PREFIX_LEN (current_stat_info
.link_name
)])
906 || contains_dot_dot (current_stat_info
.link_name
)))
908 while (status
= symlink (current_stat_info
.link_name
, file_name
),
910 if (!maybe_recoverable (file_name
, &interdir_made
))
914 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
916 symlink_error (current_stat_info
.link_name
, file_name
);
920 /* This symbolic link is potentially dangerous. Don't
921 create it now; instead, create a placeholder file, which
922 will be replaced after other extraction is done. */
925 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
927 if (! maybe_recoverable (file_name
, &interdir_made
))
932 open_error (file_name
);
933 else if (fstat (fd
, &st
) != 0)
935 stat_error (file_name
);
938 else if (close (fd
) != 0)
939 close_error (file_name
);
942 struct delayed_set_stat
*h
;
943 struct delayed_symlink
*p
=
944 xmalloc (offsetof (struct delayed_symlink
, target
)
945 + strlen (current_stat_info
.link_name
) + 1);
946 p
->next
= delayed_symlink_head
;
947 delayed_symlink_head
= p
;
950 p
->mtime
= st
.st_mtime
;
951 p
->uid
= current_stat_info
.stat
.st_uid
;
952 p
->gid
= current_stat_info
.stat
.st_gid
;
953 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
954 + strlen (file_name
) + 1);
955 p
->sources
->next
= 0;
956 strcpy (p
->sources
->string
, file_name
);
957 strcpy (p
->target
, current_stat_info
.link_name
);
959 h
= delayed_set_stat_head
;
960 if (h
&& ! h
->after_symlinks
961 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
962 && ISSLASH (file_name
[h
->file_name_len
])
963 && (base_name (file_name
)
964 == file_name
+ h
->file_name_len
+ 1))
968 h
->after_symlinks
= 1;
970 if (stat (h
->file_name
, &st
) != 0)
971 stat_error (h
->file_name
);
974 h
->stat_info
.st_dev
= st
.st_dev
;
975 h
->stat_info
.st_ino
= st
.st_ino
;
978 while ((h
= h
->next
) && ! h
->after_symlinks
);
985 if (status
!= 0 && backup_option
)
991 static int warned_once
;
997 _("Attempting extraction of symbolic links as hard links")));
1005 if (! prepare_to_extract (file_name
, 0))
1010 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, 1);
1011 struct stat st1
, st2
;
1014 /* MSDOS does not implement links. However, djgpp's link() actually
1016 status
= link (link_name
, file_name
);
1020 struct delayed_symlink
*ds
= delayed_symlink_head
;
1021 if (ds
&& stat (link_name
, &st1
) == 0)
1022 for (; ds
; ds
= ds
->next
)
1023 if (ds
->dev
== st1
.st_dev
1024 && ds
->ino
== st1
.st_ino
1025 && ds
->mtime
== st1
.st_mtime
)
1027 struct string_list
*p
=
1028 xmalloc (offsetof (struct string_list
, string
)
1029 + strlen (file_name
) + 1);
1030 strcpy (p
->string
, file_name
);
1031 p
->next
= ds
->sources
;
1037 if (maybe_recoverable (file_name
, &interdir_made
))
1040 if (incremental_option
&& errno
== EEXIST
)
1043 if (stat (link_name
, &st1
) == 0
1044 && stat (file_name
, &st2
) == 0
1045 && st1
.st_dev
== st2
.st_dev
1046 && st1
.st_ino
== st2
.st_ino
)
1049 link_error (link_name
, file_name
);
1051 undo_last_backup ();
1057 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1063 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1066 #if S_IFCHR || S_IFBLK
1068 if (! prepare_to_extract (file_name
, 0))
1071 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
1072 current_stat_info
.stat
.st_rdev
);
1075 if (maybe_recoverable (file_name
, &interdir_made
))
1077 mknod_error (file_name
);
1079 undo_last_backup ();
1082 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
1083 ARCHIVED_PERMSTATUS
, typeflag
);
1087 #if HAVE_MKFIFO || defined mkfifo
1089 if (! prepare_to_extract (file_name
, 0))
1092 while (status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
),
1094 if (!maybe_recoverable (file_name
, &interdir_made
))
1098 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
1099 ARCHIVED_PERMSTATUS
, typeflag
);
1102 mkfifo_error (file_name
);
1104 undo_last_backup ();
1110 case GNUTYPE_DUMPDIR
:
1112 if (incremental_option
)
1114 /* Read the entry and delete files that aren't listed in the
1117 gnu_restore (file_name
);
1119 else if (typeflag
== GNUTYPE_DUMPDIR
)
1122 mode
= ((current_stat_info
.stat
.st_mode
1123 | (we_are_root
? 0 : MODE_WXUSR
))
1126 status
= prepare_to_extract (file_name
, 1);
1130 goto directory_exists
;
1133 status
= mkdir (file_name
, mode
);
1139 || old_files_option
== DEFAULT_OLD_FILES
1140 || old_files_option
== OVERWRITE_OLD_FILES
))
1143 if (stat (file_name
, &st
) == 0)
1147 repair_delayed_set_stat (file_name
, &st
);
1150 if (S_ISDIR (st
.st_mode
))
1152 mode
= st
.st_mode
& ~ current_umask
;
1153 goto directory_exists
;
1159 if (maybe_recoverable (file_name
, &interdir_made
))
1162 if (errno
!= EEXIST
)
1164 mkdir_error (file_name
);
1166 undo_last_backup ();
1173 || old_files_option
== DEFAULT_OLD_FILES
1174 || old_files_option
== OVERWRITE_OLD_FILES
)
1175 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
1176 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
1178 ? ARCHIVED_PERMSTATUS
1179 : UNKNOWN_PERMSTATUS
));
1182 case GNUTYPE_VOLHDR
:
1184 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1191 case GNUTYPE_MULTIVOL
:
1193 _("%s: Cannot extract -- file is continued from another volume"),
1194 quotearg_colon (current_stat_info
.file_name
)));
1197 undo_last_backup ();
1200 case GNUTYPE_LONGNAME
:
1201 case GNUTYPE_LONGLINK
:
1202 ERROR ((0, 0, _("Visible long name error")));
1205 undo_last_backup ();
1210 _("%s: Unknown file type '%c', extracted as normal file"),
1211 quotearg_colon (file_name
), typeflag
));
1216 /* Extract the symbolic links whose final extraction were delayed. */
1218 apply_delayed_symlinks (void)
1220 struct delayed_symlink
*ds
;
1222 for (ds
= delayed_symlink_head
; ds
; )
1224 struct string_list
*sources
= ds
->sources
;
1225 char const *valid_source
= 0;
1227 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1229 char const *source
= sources
->string
;
1232 /* Make sure the placeholder file is still there. If not,
1233 don't create a symlink, as the placeholder was probably
1234 removed by a later extraction. */
1235 if (lstat (source
, &st
) == 0
1236 && st
.st_dev
== ds
->dev
1237 && st
.st_ino
== ds
->ino
1238 && st
.st_mtime
== ds
->mtime
)
1240 /* Unlink the placeholder, then create a hard link if possible,
1241 a symbolic link otherwise. */
1242 if (unlink (source
) != 0)
1243 unlink_error (source
);
1244 else if (valid_source
&& link (valid_source
, source
) == 0)
1246 else if (symlink (ds
->target
, source
) != 0)
1247 symlink_error (ds
->target
, source
);
1250 valid_source
= source
;
1251 st
.st_uid
= ds
->uid
;
1252 st
.st_gid
= ds
->gid
;
1253 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1258 for (sources
= ds
->sources
; sources
; )
1260 struct string_list
*next
= sources
->next
;
1266 struct delayed_symlink
*next
= ds
->next
;
1272 delayed_symlink_head
= 0;
1275 /* Finish the extraction of an archive. */
1277 extract_finish (void)
1279 /* First, fix the status of ordinary directories that need fixing. */
1280 apply_nonancestor_delayed_set_stat ("", 0);
1282 /* Then, apply delayed symlinks, so that they don't affect delayed
1283 directory status-setting for ordinary directories. */
1284 apply_delayed_symlinks ();
1286 /* Finally, fix the status of directories that are ancestors
1287 of delayed symlinks. */
1288 apply_nonancestor_delayed_set_stat ("", 1);
1295 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));