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 size_t prefix_len
= stripped_prefix_len (file_name
, strip_path_elements
);
723 if (prefix_len
== (size_t) -1)
728 file_name
+= prefix_len
;
731 apply_nonancestor_delayed_set_stat (file_name
, 0);
733 /* Take a safety backup of a previously existing file. */
735 if (backup_option
&& !to_stdout_option
)
736 if (!maybe_backup_file (file_name
, 0))
739 ERROR ((0, e
, _("%s: Was unable to backup this file"),
740 quotearg_colon (file_name
)));
745 /* Extract the archive entry according to its type. */
747 typeflag
= current_header
->header
.typeflag
;
751 file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
752 if (! fill_in_sparse_array ())
760 /* Appears to be a file. But BSD tar uses the convention that a slash
761 suffix means a directory. */
763 if (current_stat_info
.had_trailing_slash
)
766 /* FIXME: deal with protection issues. */
769 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
770 | (old_files_option
== OVERWRITE_OLD_FILES
773 mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
775 if (to_stdout_option
)
781 if (! prepare_to_extract (file_name
, 0))
790 /* Contiguous files (on the Masscomp) have to specify the size in
791 the open call that creates them. */
793 if (typeflag
== CONTTYPE
)
794 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
796 fd
= open (file_name
, openflag
, mode
);
798 #else /* not O_CTG */
799 if (typeflag
== CONTTYPE
)
801 static int conttype_diagnosed
;
803 if (!conttype_diagnosed
)
805 conttype_diagnosed
= 1;
806 WARN ((0, 0, _("Extracting contiguous files as regular files")));
809 fd
= open (file_name
, openflag
, mode
);
811 #endif /* not O_CTG */
815 if (maybe_recoverable (file_name
, &interdir_made
))
818 open_error (file_name
);
826 if (typeflag
== GNUTYPE_SPARSE
)
829 size_t name_length_bis
;
831 /* Kludge alert. NAME is assigned to header.name because
832 during the extraction, the space that contains the header
833 will get scribbled on, and the name will get munged, so any
834 error messages that happen to contain the filename will look
835 REAL interesting unless we do this. */
837 name_length_bis
= strlen (file_name
) + 1;
838 name
= xmalloc (name_length_bis
);
839 memcpy (name
, file_name
, name_length_bis
);
840 size
= extract_sparse_file (fd
, name
,
841 current_stat_info
.stat
.st_size
, file_size
);
845 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
847 if (multi_volume_option
)
849 assign_string (&save_name
, current_stat_info
.file_name
);
850 save_totsize
= current_stat_info
.stat
.st_size
;
851 save_sizeleft
= size
;
854 /* Locate data, determine max length writeable, write it,
855 block that we have used the data, then check if the write
858 data_block
= find_next_block ();
861 ERROR ((0, 0, _("Unexpected EOF in archive")));
862 break; /* FIXME: What happens, then? */
865 written
= available_space_after (data_block
);
870 count
= full_write (fd
, data_block
->buffer
, written
);
873 set_next_block_after ((union block
*)
874 (data_block
->buffer
+ written
- 1));
875 if (count
!= written
)
877 write_error_details (file_name
, count
, written
);
884 if (multi_volume_option
)
885 assign_string (&save_name
, 0);
887 /* If writing to stdout, don't try to do anything to the filename;
888 it doesn't exist, or we don't want to touch it anyway. */
890 if (to_stdout_option
)
896 close_error (file_name
);
901 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
902 (old_files_option
== OVERWRITE_OLD_FILES
904 : ARCHIVED_PERMSTATUS
),
910 if (! prepare_to_extract (file_name
, 0))
913 if (absolute_names_option
914 || ! (ISSLASH (current_stat_info
.link_name
915 [FILESYSTEM_PREFIX_LEN (current_stat_info
.link_name
)])
916 || contains_dot_dot (current_stat_info
.link_name
)))
918 while (status
= symlink (current_stat_info
.link_name
, file_name
),
920 if (!maybe_recoverable (file_name
, &interdir_made
))
924 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
926 symlink_error (current_stat_info
.link_name
, file_name
);
930 /* This symbolic link is potentially dangerous. Don't
931 create it now; instead, create a placeholder file, which
932 will be replaced after other extraction is done. */
935 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
937 if (! maybe_recoverable (file_name
, &interdir_made
))
942 open_error (file_name
);
943 else if (fstat (fd
, &st
) != 0)
945 stat_error (file_name
);
948 else if (close (fd
) != 0)
949 close_error (file_name
);
952 struct delayed_set_stat
*h
;
953 struct delayed_symlink
*p
=
954 xmalloc (offsetof (struct delayed_symlink
, target
)
955 + strlen (current_stat_info
.link_name
) + 1);
956 p
->next
= delayed_symlink_head
;
957 delayed_symlink_head
= p
;
960 p
->mtime
= st
.st_mtime
;
961 p
->uid
= current_stat_info
.stat
.st_uid
;
962 p
->gid
= current_stat_info
.stat
.st_gid
;
963 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
964 + strlen (file_name
) + 1);
965 p
->sources
->next
= 0;
966 strcpy (p
->sources
->string
, file_name
);
967 strcpy (p
->target
, current_stat_info
.link_name
);
969 h
= delayed_set_stat_head
;
970 if (h
&& ! h
->after_symlinks
971 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
972 && ISSLASH (file_name
[h
->file_name_len
])
973 && (base_name (file_name
)
974 == file_name
+ h
->file_name_len
+ 1))
978 h
->after_symlinks
= 1;
980 if (stat (h
->file_name
, &st
) != 0)
981 stat_error (h
->file_name
);
984 h
->stat_info
.st_dev
= st
.st_dev
;
985 h
->stat_info
.st_ino
= st
.st_ino
;
988 while ((h
= h
->next
) && ! h
->after_symlinks
);
995 if (status
!= 0 && backup_option
)
1001 static int warned_once
;
1007 _("Attempting extraction of symbolic links as hard links")));
1015 if (! prepare_to_extract (file_name
, 0))
1020 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, 1);
1021 struct stat st1
, st2
;
1024 /* MSDOS does not implement links. However, djgpp's link() actually
1026 status
= link (link_name
, file_name
);
1030 struct delayed_symlink
*ds
= delayed_symlink_head
;
1031 if (ds
&& stat (link_name
, &st1
) == 0)
1032 for (; ds
; ds
= ds
->next
)
1033 if (ds
->dev
== st1
.st_dev
1034 && ds
->ino
== st1
.st_ino
1035 && ds
->mtime
== st1
.st_mtime
)
1037 struct string_list
*p
=
1038 xmalloc (offsetof (struct string_list
, string
)
1039 + strlen (file_name
) + 1);
1040 strcpy (p
->string
, file_name
);
1041 p
->next
= ds
->sources
;
1047 if (maybe_recoverable (file_name
, &interdir_made
))
1050 if (incremental_option
&& errno
== EEXIST
)
1053 if (stat (link_name
, &st1
) == 0
1054 && stat (file_name
, &st2
) == 0
1055 && st1
.st_dev
== st2
.st_dev
1056 && st1
.st_ino
== st2
.st_ino
)
1059 link_error (link_name
, file_name
);
1061 undo_last_backup ();
1067 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1073 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1076 #if S_IFCHR || S_IFBLK
1078 if (! prepare_to_extract (file_name
, 0))
1081 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
1082 current_stat_info
.stat
.st_rdev
);
1085 if (maybe_recoverable (file_name
, &interdir_made
))
1087 mknod_error (file_name
);
1089 undo_last_backup ();
1092 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
1093 ARCHIVED_PERMSTATUS
, typeflag
);
1097 #if HAVE_MKFIFO || defined mkfifo
1099 if (! prepare_to_extract (file_name
, 0))
1102 while (status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
),
1104 if (!maybe_recoverable (file_name
, &interdir_made
))
1108 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
1109 ARCHIVED_PERMSTATUS
, typeflag
);
1112 mkfifo_error (file_name
);
1114 undo_last_backup ();
1120 case GNUTYPE_DUMPDIR
:
1122 if (incremental_option
)
1124 /* Read the entry and delete files that aren't listed in the
1127 gnu_restore (file_name
);
1129 else if (typeflag
== GNUTYPE_DUMPDIR
)
1132 mode
= ((current_stat_info
.stat
.st_mode
1133 | (we_are_root
? 0 : MODE_WXUSR
))
1136 status
= prepare_to_extract (file_name
, 1);
1140 goto directory_exists
;
1143 status
= mkdir (file_name
, mode
);
1149 || old_files_option
== DEFAULT_OLD_FILES
1150 || old_files_option
== OVERWRITE_OLD_FILES
))
1153 if (stat (file_name
, &st
) == 0)
1157 repair_delayed_set_stat (file_name
, &st
);
1160 if (S_ISDIR (st
.st_mode
))
1162 mode
= st
.st_mode
& ~ current_umask
;
1163 goto directory_exists
;
1169 if (maybe_recoverable (file_name
, &interdir_made
))
1172 if (errno
!= EEXIST
)
1174 mkdir_error (file_name
);
1176 undo_last_backup ();
1183 || old_files_option
== DEFAULT_OLD_FILES
1184 || old_files_option
== OVERWRITE_OLD_FILES
)
1185 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
1186 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
1188 ? ARCHIVED_PERMSTATUS
1189 : UNKNOWN_PERMSTATUS
));
1192 case GNUTYPE_VOLHDR
:
1194 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1201 case GNUTYPE_MULTIVOL
:
1203 _("%s: Cannot extract -- file is continued from another volume"),
1204 quotearg_colon (current_stat_info
.file_name
)));
1207 undo_last_backup ();
1210 case GNUTYPE_LONGNAME
:
1211 case GNUTYPE_LONGLINK
:
1212 ERROR ((0, 0, _("Visible long name error")));
1215 undo_last_backup ();
1220 _("%s: Unknown file type '%c', extracted as normal file"),
1221 quotearg_colon (file_name
), typeflag
));
1226 /* Extract the symbolic links whose final extraction were delayed. */
1228 apply_delayed_symlinks (void)
1230 struct delayed_symlink
*ds
;
1232 for (ds
= delayed_symlink_head
; ds
; )
1234 struct string_list
*sources
= ds
->sources
;
1235 char const *valid_source
= 0;
1237 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1239 char const *source
= sources
->string
;
1242 /* Make sure the placeholder file is still there. If not,
1243 don't create a symlink, as the placeholder was probably
1244 removed by a later extraction. */
1245 if (lstat (source
, &st
) == 0
1246 && st
.st_dev
== ds
->dev
1247 && st
.st_ino
== ds
->ino
1248 && st
.st_mtime
== ds
->mtime
)
1250 /* Unlink the placeholder, then create a hard link if possible,
1251 a symbolic link otherwise. */
1252 if (unlink (source
) != 0)
1253 unlink_error (source
);
1254 else if (valid_source
&& link (valid_source
, source
) == 0)
1256 else if (symlink (ds
->target
, source
) != 0)
1257 symlink_error (ds
->target
, source
);
1260 valid_source
= source
;
1261 st
.st_uid
= ds
->uid
;
1262 st
.st_gid
= ds
->gid
;
1263 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1268 for (sources
= ds
->sources
; sources
; )
1270 struct string_list
*next
= sources
->next
;
1276 struct delayed_symlink
*next
= ds
->next
;
1282 delayed_symlink_head
= 0;
1285 /* Finish the extraction of an archive. */
1287 extract_finish (void)
1289 /* First, fix the status of ordinary directories that need fixing. */
1290 apply_nonancestor_delayed_set_stat ("", 0);
1292 /* Then, apply delayed symlinks, so that they don't affect delayed
1293 directory status-setting for ordinary directories. */
1294 apply_delayed_symlinks ();
1296 /* Finally, fix the status of directories that are ancestors
1297 of delayed symlinks. */
1298 apply_nonancestor_delayed_set_stat ("", 1);
1305 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));