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> */
410 || ERRNO_IS_EACCESS
) /* Turbo C mkdir gives a funny errno. */
411 && access (file_name
, W_OK
) == 0)
414 /* Some other error in the mkdir. We return to the caller. */
418 return did_something
; /* tell them to retry if we made one */
421 /* Prepare to extract a file.
422 Return zero if extraction should not proceed. */
425 prepare_to_extract (char const *file_name
, bool directory
)
427 if (to_stdout_option
)
430 if (old_files_option
== UNLINK_FIRST_OLD_FILES
431 && !remove_any_file (file_name
, recursive_unlink_option
)
432 && errno
&& errno
!= ENOENT
)
434 unlink_error (file_name
);
441 /* Attempt repairing what went wrong with the extraction. Delete an
442 already existing file or create missing intermediate directories.
443 Return nonzero if we somewhat increased our chances at a successful
444 extraction. errno is properly restored on zero return. */
446 maybe_recoverable (char *file_name
, int *interdir_made
)
454 /* Remove an old file, if the options allow this. */
456 switch (old_files_option
)
461 case DEFAULT_OLD_FILES
:
462 case NO_OVERWRITE_DIR_OLD_FILES
:
463 case OVERWRITE_OLD_FILES
:
465 int r
= remove_any_file (file_name
, 0);
472 /* Attempt creating missing intermediate directories. */
473 if (! make_directories (file_name
))
482 /* Just say we can't do anything about it... */
488 /* Translate the sparse information on the header, and in any
489 subsequent extended headers, into an array of structures with true
490 numbers, as opposed to character strings. Return nonzero if
493 This function invalidates current_header. */
496 fill_in_sparse_array (void)
498 off_t sparse_data_size
= current_stat_info
.stat
.st_size
;
499 off_t file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
502 union block
*h
= current_header
;
506 for (sparses
= 0; sparses
< SPARSES_IN_OLDGNU_HEADER
; sparses
++)
508 struct sparse
const *s
= &h
->oldgnu_header
.sp
[sparses
];
511 if (s
->numbytes
[0] == '\0')
513 sparsearray
[sparses
].offset
= offset
= OFF_FROM_HEADER (s
->offset
);
514 sparsearray
[sparses
].numbytes
= numbytes
=
515 SIZE_FROM_HEADER (s
->numbytes
);
516 sparse_data_size
-= numbytes
;
517 if (offset
< 0 || file_size
< offset
+ numbytes
|| sparse_data_size
< 0)
521 if (h
->oldgnu_header
.isextended
)
524 h
= find_next_block ();
527 ERROR ((0, 0, _("Unexpected EOF in archive")));
531 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
533 struct sparse
const *s
= &h
->sparse_header
.sp
[counter
];
536 if (s
->numbytes
[0] == '\0')
539 if (sparses
== sp_array_size
)
543 xrealloc (sparsearray
,
544 sp_array_size
* sizeof *sparsearray
);
547 sparsearray
[sparses
].offset
= offset
=
548 OFF_FROM_HEADER (s
->offset
);
549 sparsearray
[sparses
].numbytes
= numbytes
=
550 SIZE_FROM_HEADER (s
->numbytes
);
551 sparse_data_size
-= numbytes
;
552 if (offset
< 0 || file_size
< offset
+ numbytes
553 || sparse_data_size
< 0)
558 set_next_block_after (h
);
560 } while (h
->sparse_header
.isextended
);
565 ERROR ((0, 0, _("%s: invalid sparse archive member"),
566 current_stat_info
.file_name
));
572 extract_sparse_file (int fd
, char const *name
,
573 off_t sizeleft
, off_t file_size
)
577 while (sizeleft
!= 0)
581 union block
*data_block
= find_next_block ();
584 ERROR ((0, 0, _("Unexpected EOF in archive")));
587 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
589 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
592 written
= sparsearray
[sparse_ind
++].numbytes
;
593 while (written
> BLOCKSIZE
)
595 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
598 if (count
!= BLOCKSIZE
)
600 write_error_details (name
, count
, BLOCKSIZE
);
603 set_next_block_after (data_block
);
604 data_block
= find_next_block ();
607 ERROR ((0, 0, _("Unexpected EOF in archive")));
612 count
= full_write (fd
, data_block
->buffer
, written
);
615 if (count
!= written
)
617 write_error_details (name
, count
, written
);
621 set_next_block_after (data_block
);
624 if (ftruncate (fd
, file_size
) != 0)
625 truncate_error (name
);
630 /* Fix the statuses of all directories whose statuses need fixing, and
631 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
632 nonzero, do this for all such directories; otherwise, stop at the
633 first directory that is marked to be fixed up only after delayed
634 symlinks are applied. */
636 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
638 size_t file_name_len
= strlen (file_name
);
639 bool check_for_renamed_directories
= 0;
641 while (delayed_set_stat_head
)
643 struct delayed_set_stat
*data
= delayed_set_stat_head
;
644 bool skip_this_one
= 0;
646 struct stat
const *cur_info
= 0;
648 check_for_renamed_directories
|= data
->after_symlinks
;
650 if (after_symlinks
< data
->after_symlinks
651 || (data
->file_name_len
< file_name_len
652 && file_name
[data
->file_name_len
]
653 && (ISSLASH (file_name
[data
->file_name_len
])
654 || ISSLASH (file_name
[data
->file_name_len
- 1]))
655 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
658 if (check_for_renamed_directories
)
661 if (stat (data
->file_name
, &st
) != 0)
663 stat_error (data
->file_name
);
666 else if (! (st
.st_dev
== data
->stat_info
.st_dev
667 && (st
.st_ino
== data
->stat_info
.st_ino
)))
670 _("%s: Directory renamed before its status could be extracted"),
671 quotearg_colon (data
->file_name
)));
677 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
678 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
680 delayed_set_stat_head
= data
->next
;
685 /* Extract a file from the archive. */
687 extract_archive (void)
689 union block
*data_block
;
698 int interdir_made
= 0;
702 set_next_block_after (current_header
);
703 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
705 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
711 /* Print the block from current_header and current_stat. */
716 file_name
= safer_name_suffix (current_stat_info
.file_name
, 0);
717 if (strip_path_elements
)
719 size_t prefix_len
= stripped_prefix_len (file_name
, strip_path_elements
);
720 if (prefix_len
== (size_t) -1)
725 file_name
+= prefix_len
;
728 apply_nonancestor_delayed_set_stat (file_name
, 0);
730 /* Take a safety backup of a previously existing file. */
732 if (backup_option
&& !to_stdout_option
)
733 if (!maybe_backup_file (file_name
, 0))
736 ERROR ((0, e
, _("%s: Was unable to backup this file"),
737 quotearg_colon (file_name
)));
742 /* Extract the archive entry according to its type. */
744 typeflag
= current_header
->header
.typeflag
;
748 file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
749 if (! fill_in_sparse_array ())
757 /* Appears to be a file. But BSD tar uses the convention that a slash
758 suffix means a directory. */
760 if (current_stat_info
.had_trailing_slash
)
763 /* FIXME: deal with protection issues. */
766 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
767 | (old_files_option
== OVERWRITE_OLD_FILES
770 mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
772 if (to_stdout_option
)
778 if (! prepare_to_extract (file_name
, 0))
787 /* Contiguous files (on the Masscomp) have to specify the size in
788 the open call that creates them. */
790 if (typeflag
== CONTTYPE
)
791 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
793 fd
= open (file_name
, openflag
, mode
);
795 #else /* not O_CTG */
796 if (typeflag
== CONTTYPE
)
798 static int conttype_diagnosed
;
800 if (!conttype_diagnosed
)
802 conttype_diagnosed
= 1;
803 WARN ((0, 0, _("Extracting contiguous files as regular files")));
806 fd
= open (file_name
, openflag
, mode
);
808 #endif /* not O_CTG */
812 if (maybe_recoverable (file_name
, &interdir_made
))
815 open_error (file_name
);
823 if (typeflag
== GNUTYPE_SPARSE
)
826 size_t name_length_bis
;
828 /* Kludge alert. NAME is assigned to header.name because
829 during the extraction, the space that contains the header
830 will get scribbled on, and the name will get munged, so any
831 error messages that happen to contain the filename will look
832 REAL interesting unless we do this. */
834 name_length_bis
= strlen (file_name
) + 1;
835 name
= xmalloc (name_length_bis
);
836 memcpy (name
, file_name
, name_length_bis
);
837 size
= extract_sparse_file (fd
, name
,
838 current_stat_info
.stat
.st_size
, file_size
);
842 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
844 if (multi_volume_option
)
846 assign_string (&save_name
, current_stat_info
.file_name
);
847 save_totsize
= current_stat_info
.stat
.st_size
;
848 save_sizeleft
= size
;
851 /* Locate data, determine max length writeable, write it,
852 block that we have used the data, then check if the write
855 data_block
= find_next_block ();
858 ERROR ((0, 0, _("Unexpected EOF in archive")));
859 break; /* FIXME: What happens, then? */
862 written
= available_space_after (data_block
);
867 count
= full_write (fd
, data_block
->buffer
, written
);
870 set_next_block_after ((union block
*)
871 (data_block
->buffer
+ written
- 1));
872 if (count
!= written
)
874 write_error_details (file_name
, count
, written
);
881 if (multi_volume_option
)
882 assign_string (&save_name
, 0);
884 /* If writing to stdout, don't try to do anything to the filename;
885 it doesn't exist, or we don't want to touch it anyway. */
887 if (to_stdout_option
)
893 close_error (file_name
);
898 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
899 (old_files_option
== OVERWRITE_OLD_FILES
901 : ARCHIVED_PERMSTATUS
),
907 if (! prepare_to_extract (file_name
, 0))
910 if (absolute_names_option
911 || ! (ISSLASH (current_stat_info
.link_name
912 [FILESYSTEM_PREFIX_LEN (current_stat_info
.link_name
)])
913 || contains_dot_dot (current_stat_info
.link_name
)))
915 while (status
= symlink (current_stat_info
.link_name
, file_name
),
917 if (!maybe_recoverable (file_name
, &interdir_made
))
921 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
923 symlink_error (current_stat_info
.link_name
, file_name
);
927 /* This symbolic link is potentially dangerous. Don't
928 create it now; instead, create a placeholder file, which
929 will be replaced after other extraction is done. */
932 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
934 if (! maybe_recoverable (file_name
, &interdir_made
))
939 open_error (file_name
);
940 else if (fstat (fd
, &st
) != 0)
942 stat_error (file_name
);
945 else if (close (fd
) != 0)
946 close_error (file_name
);
949 struct delayed_set_stat
*h
;
950 struct delayed_symlink
*p
=
951 xmalloc (offsetof (struct delayed_symlink
, target
)
952 + strlen (current_stat_info
.link_name
) + 1);
953 p
->next
= delayed_symlink_head
;
954 delayed_symlink_head
= p
;
957 p
->mtime
= st
.st_mtime
;
958 p
->uid
= current_stat_info
.stat
.st_uid
;
959 p
->gid
= current_stat_info
.stat
.st_gid
;
960 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
961 + strlen (file_name
) + 1);
962 p
->sources
->next
= 0;
963 strcpy (p
->sources
->string
, file_name
);
964 strcpy (p
->target
, current_stat_info
.link_name
);
966 h
= delayed_set_stat_head
;
967 if (h
&& ! h
->after_symlinks
968 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
969 && ISSLASH (file_name
[h
->file_name_len
])
970 && (base_name (file_name
)
971 == file_name
+ h
->file_name_len
+ 1))
975 h
->after_symlinks
= 1;
977 if (stat (h
->file_name
, &st
) != 0)
978 stat_error (h
->file_name
);
981 h
->stat_info
.st_dev
= st
.st_dev
;
982 h
->stat_info
.st_ino
= st
.st_ino
;
985 while ((h
= h
->next
) && ! h
->after_symlinks
);
992 if (status
!= 0 && backup_option
)
998 static int warned_once
;
1004 _("Attempting extraction of symbolic links as hard links")));
1012 if (! prepare_to_extract (file_name
, 0))
1017 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
, 1);
1018 struct stat st1
, st2
;
1021 /* MSDOS does not implement links. However, djgpp's link() actually
1023 status
= link (link_name
, file_name
);
1027 struct delayed_symlink
*ds
= delayed_symlink_head
;
1028 if (ds
&& stat (link_name
, &st1
) == 0)
1029 for (; ds
; ds
= ds
->next
)
1030 if (ds
->dev
== st1
.st_dev
1031 && ds
->ino
== st1
.st_ino
1032 && ds
->mtime
== st1
.st_mtime
)
1034 struct string_list
*p
=
1035 xmalloc (offsetof (struct string_list
, string
)
1036 + strlen (file_name
) + 1);
1037 strcpy (p
->string
, file_name
);
1038 p
->next
= ds
->sources
;
1044 if (maybe_recoverable (file_name
, &interdir_made
))
1047 if (incremental_option
&& errno
== EEXIST
)
1050 if (stat (link_name
, &st1
) == 0
1051 && stat (file_name
, &st2
) == 0
1052 && st1
.st_dev
== st2
.st_dev
1053 && st1
.st_ino
== st2
.st_ino
)
1056 link_error (link_name
, file_name
);
1058 undo_last_backup ();
1064 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1070 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1073 #if S_IFCHR || S_IFBLK
1075 if (! prepare_to_extract (file_name
, 0))
1078 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
1079 current_stat_info
.stat
.st_rdev
);
1082 if (maybe_recoverable (file_name
, &interdir_made
))
1084 mknod_error (file_name
);
1086 undo_last_backup ();
1089 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
1090 ARCHIVED_PERMSTATUS
, typeflag
);
1094 #if HAVE_MKFIFO || defined mkfifo
1096 if (! prepare_to_extract (file_name
, 0))
1099 while (status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
),
1101 if (!maybe_recoverable (file_name
, &interdir_made
))
1105 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
1106 ARCHIVED_PERMSTATUS
, typeflag
);
1109 mkfifo_error (file_name
);
1111 undo_last_backup ();
1117 case GNUTYPE_DUMPDIR
:
1119 if (incremental_option
)
1121 /* Read the entry and delete files that aren't listed in the
1124 gnu_restore (file_name
);
1126 else if (typeflag
== GNUTYPE_DUMPDIR
)
1129 mode
= ((current_stat_info
.stat
.st_mode
1130 | (we_are_root
? 0 : MODE_WXUSR
))
1133 status
= prepare_to_extract (file_name
, 1);
1137 goto directory_exists
;
1140 status
= mkdir (file_name
, mode
);
1146 || old_files_option
== DEFAULT_OLD_FILES
1147 || old_files_option
== OVERWRITE_OLD_FILES
))
1150 if (stat (file_name
, &st
) == 0)
1154 repair_delayed_set_stat (file_name
, &st
);
1157 if (S_ISDIR (st
.st_mode
))
1159 mode
= st
.st_mode
& ~ current_umask
;
1160 goto directory_exists
;
1166 if (maybe_recoverable (file_name
, &interdir_made
))
1169 if (errno
!= EEXIST
)
1171 mkdir_error (file_name
);
1173 undo_last_backup ();
1180 || old_files_option
== DEFAULT_OLD_FILES
1181 || old_files_option
== OVERWRITE_OLD_FILES
)
1182 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
1183 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
1185 ? ARCHIVED_PERMSTATUS
1186 : UNKNOWN_PERMSTATUS
));
1189 case GNUTYPE_VOLHDR
:
1191 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1198 case GNUTYPE_MULTIVOL
:
1200 _("%s: Cannot extract -- file is continued from another volume"),
1201 quotearg_colon (current_stat_info
.file_name
)));
1204 undo_last_backup ();
1207 case GNUTYPE_LONGNAME
:
1208 case GNUTYPE_LONGLINK
:
1209 ERROR ((0, 0, _("Visible long name error")));
1212 undo_last_backup ();
1217 _("%s: Unknown file type '%c', extracted as normal file"),
1218 quotearg_colon (file_name
), typeflag
));
1223 /* Extract the symbolic links whose final extraction were delayed. */
1225 apply_delayed_symlinks (void)
1227 struct delayed_symlink
*ds
;
1229 for (ds
= delayed_symlink_head
; ds
; )
1231 struct string_list
*sources
= ds
->sources
;
1232 char const *valid_source
= 0;
1234 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1236 char const *source
= sources
->string
;
1239 /* Make sure the placeholder file is still there. If not,
1240 don't create a symlink, as the placeholder was probably
1241 removed by a later extraction. */
1242 if (lstat (source
, &st
) == 0
1243 && st
.st_dev
== ds
->dev
1244 && st
.st_ino
== ds
->ino
1245 && st
.st_mtime
== ds
->mtime
)
1247 /* Unlink the placeholder, then create a hard link if possible,
1248 a symbolic link otherwise. */
1249 if (unlink (source
) != 0)
1250 unlink_error (source
);
1251 else if (valid_source
&& link (valid_source
, source
) == 0)
1253 else if (symlink (ds
->target
, source
) != 0)
1254 symlink_error (ds
->target
, source
);
1257 valid_source
= source
;
1258 st
.st_uid
= ds
->uid
;
1259 st
.st_gid
= ds
->gid
;
1260 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1265 for (sources
= ds
->sources
; sources
; )
1267 struct string_list
*next
= sources
->next
;
1273 struct delayed_symlink
*next
= ds
->next
;
1279 delayed_symlink_head
= 0;
1282 /* Finish the extraction of an archive. */
1284 extract_finish (void)
1286 /* First, fix the status of ordinary directories that need fixing. */
1287 apply_nonancestor_delayed_set_stat ("", 0);
1289 /* Then, apply delayed symlinks, so that they don't affect delayed
1290 directory status-setting for ordinary directories. */
1291 apply_delayed_symlinks ();
1293 /* Finally, fix the status of directories that are ancestors
1294 of delayed symlinks. */
1295 apply_nonancestor_delayed_set_stat ("", 1);
1302 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));