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 *CURRENT_STAT_INFO gives
133 the current status if CURRENT_STAT_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
, struct stat
const *stat_info
,
139 struct stat
const *current_stat_info
,
140 mode_t invert_permissions
, enum permstatus permstatus
,
145 if (0 < same_permissions_option
146 && permstatus
!= INTERDIR_PERMSTATUS
)
148 mode
= stat_info
->st_mode
;
150 /* If we created the file and it has a usual mode, then its mode
151 is normally set correctly already. But on many hosts, some
152 directories inherit the setgid bits from their parents, so we
153 we must set directories' modes explicitly. */
154 if (permstatus
== ARCHIVED_PERMSTATUS
155 && ! (mode
& ~ MODE_RWX
)
156 && typeflag
!= DIRTYPE
157 && typeflag
!= GNUTYPE_DUMPDIR
)
160 else if (! invert_permissions
)
164 /* We must inspect a directory's current permissions, since the
165 directory may have inherited its setgid bit from its parent.
167 INVERT_PERMISSIONS happens to be nonzero only for directories
168 that we created, so there's no point optimizing this code for
171 if (! current_stat_info
)
173 if (stat (file_name
, &st
) != 0)
175 stat_error (file_name
);
178 current_stat_info
= &st
;
180 mode
= current_stat_info
->st_mode
^ invert_permissions
;
183 if (chmod (file_name
, mode
) != 0)
184 chmod_error_details (file_name
, mode
);
187 /* Check time after successfully setting FILE_NAME's time stamp to T. */
189 check_time (char const *file_name
, time_t t
)
193 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
194 file_name
, tartime (t
)));
195 else if (start_time
< t
&& (now
= time (0)) < t
)
196 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
197 file_name
, tartime (t
), (unsigned long) (t
- now
)));
200 /* Restore stat attributes (owner, group, mode and times) for
201 FILE_NAME, using information given in *STAT_INFO.
202 If CURRENT_STAT_INFO is nonzero, *CURRENT_STAT_INFO is the
203 file's currernt status.
204 If not restoring permissions, invert the
205 INVERT_PERMISSIONS bits from the file's current permissions.
206 PERMSTATUS specifies the status of the file's permissions.
207 TYPEFLAG specifies the type of the file. */
209 /* FIXME: About proper restoration of symbolic link attributes, we still do
210 not have it right. Pretesters' reports tell us we need further study and
211 probably more configuration. For now, just use lchown if it exists, and
212 punt for the rest. Sigh! */
215 set_stat (char const *file_name
, struct stat
const *stat_info
,
216 struct stat
const *current_stat_info
,
217 mode_t invert_permissions
, enum permstatus permstatus
,
220 struct utimbuf utimbuf
;
222 if (typeflag
!= SYMTYPE
)
224 /* We do the utime before the chmod because some versions of utime are
225 broken and trash the modes of the file. */
227 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
229 /* We set the accessed time to `now', which is really the time we
230 started extracting files, unless incremental_option is used, in
231 which case .st_atime is used. */
233 /* FIXME: incremental_option should set ctime too, but how? */
235 if (incremental_option
)
236 utimbuf
.actime
= stat_info
->st_atime
;
238 utimbuf
.actime
= start_time
;
240 utimbuf
.modtime
= stat_info
->st_mtime
;
242 if (utime (file_name
, &utimbuf
) < 0)
243 utime_error (file_name
);
246 check_time (file_name
, utimbuf
.actime
);
247 check_time (file_name
, utimbuf
.modtime
);
251 /* Some systems allow non-root users to give files away. Once this
252 done, it is not possible anymore to change file permissions, so we
253 have to set permissions prior to possibly giving files away. */
255 set_mode (file_name
, stat_info
, current_stat_info
,
256 invert_permissions
, permstatus
, typeflag
);
259 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
261 /* When lchown exists, it should be used to change the attributes of
262 the symbolic link itself. In this case, a mere chown would change
263 the attributes of the file the symbolic link is pointing to, and
264 should be avoided. */
266 if (typeflag
== SYMTYPE
)
269 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
270 chown_error_details (file_name
,
271 stat_info
->st_uid
, stat_info
->st_gid
);
276 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
277 chown_error_details (file_name
,
278 stat_info
->st_uid
, stat_info
->st_gid
);
280 /* On a few systems, and in particular, those allowing to give files
281 away, changing the owner or group destroys the suid or sgid bits.
282 So let's attempt setting these bits once more. */
283 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
284 set_mode (file_name
, stat_info
, 0,
285 invert_permissions
, permstatus
, typeflag
);
290 /* Remember to restore stat attributes (owner, group, mode and times)
291 for the directory FILE_NAME, using information given in *STAT_INFO,
292 once we stop extracting files into that directory.
293 If not restoring permissions, remember to invert the
294 INVERT_PERMISSIONS bits from the file's current permissions.
295 PERMSTATUS specifies the status of the file's permissions. */
297 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
298 mode_t invert_permissions
, enum permstatus permstatus
)
300 size_t file_name_len
= strlen (file_name
);
301 struct delayed_set_stat
*data
=
302 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
303 + file_name_len
+ 1);
304 data
->file_name_len
= file_name_len
;
305 strcpy (data
->file_name
, file_name
);
306 data
->invert_permissions
= invert_permissions
;
307 data
->permstatus
= permstatus
;
308 data
->after_symlinks
= 0;
309 data
->stat_info
= *stat_info
;
310 data
->next
= delayed_set_stat_head
;
311 delayed_set_stat_head
= data
;
314 /* Update the delayed_set_stat info for an intermediate directory
315 created on the path to DIR_NAME. The intermediate directory turned
316 out to be the same as this directory, e.g. due to ".." or symbolic
317 links. *DIR_STAT_INFO is the status of the directory. */
319 repair_delayed_set_stat (char const *dir_name
,
320 struct stat
const *dir_stat_info
)
322 struct delayed_set_stat
*data
;
323 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
326 if (stat (data
->file_name
, &st
) != 0)
328 stat_error (data
->file_name
);
332 if (st
.st_dev
== dir_stat_info
->st_dev
333 && st
.st_ino
== dir_stat_info
->st_ino
)
335 data
->stat_info
= current_stat
;
336 data
->invert_permissions
= (MODE_RWX
337 & (current_stat
.st_mode
^ st
.st_mode
));
338 data
->permstatus
= ARCHIVED_PERMSTATUS
;
343 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
344 quotearg_colon (dir_name
)));
347 /* After a file/link/symlink/directory creation has failed, see if
348 it's because some required directory was not present, and if so,
349 create all required directories. Return non-zero if a directory
352 make_directories (char *file_name
)
354 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
355 char *cursor
; /* points into path */
356 int did_something
= 0; /* did we do anything yet? */
358 int invert_permissions
;
362 for (cursor
= cursor0
; *cursor
; cursor
++)
364 if (! ISSLASH (*cursor
))
367 /* Avoid mkdir of empty string, if leading or double '/'. */
369 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
372 /* Avoid mkdir where last part of path is "." or "..". */
374 if (cursor
[-1] == '.'
375 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
376 || (cursor
[-2] == '.'
377 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
380 *cursor
= '\0'; /* truncate the path there */
381 mode
= MODE_RWX
& ~ newdir_umask
;
382 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
383 status
= mkdir (file_name
, mode
^ invert_permissions
);
387 /* Create a struct delayed_set_stat even if
388 invert_permissions is zero, because
389 repair_delayed_set_stat may need to update the struct. */
390 delay_set_stat (file_name
,
391 ¤t_stat
/* ignored */,
392 invert_permissions
, INTERDIR_PERMSTATUS
);
394 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
405 /* Turbo C mkdir gives a funny errno. */
409 /* Directory already exists. */
412 /* Some other error in the mkdir. We return to the caller. */
416 return did_something
; /* tell them to retry if we made one */
419 /* Prepare to extract a file.
420 Return zero if extraction should not proceed. */
423 prepare_to_extract (char const *file_name
, bool directory
)
425 if (to_stdout_option
)
428 if (old_files_option
== UNLINK_FIRST_OLD_FILES
429 && !remove_any_file (file_name
, recursive_unlink_option
)
430 && errno
&& errno
!= ENOENT
)
432 unlink_error (file_name
);
439 /* Attempt repairing what went wrong with the extraction. Delete an
440 already existing file or create missing intermediate directories.
441 Return nonzero if we somewhat increased our chances at a successful
442 extraction. errno is properly restored on zero return. */
444 maybe_recoverable (char *file_name
, int *interdir_made
)
452 /* Remove an old file, if the options allow this. */
454 switch (old_files_option
)
459 case DEFAULT_OLD_FILES
:
460 case NO_OVERWRITE_DIR_OLD_FILES
:
461 case OVERWRITE_OLD_FILES
:
463 int r
= remove_any_file (file_name
, 0);
470 /* Attempt creating missing intermediate directories. */
471 if (! make_directories (file_name
))
480 /* Just say we can't do anything about it... */
486 /* Translate the sparse information on the header, and in any
487 subsequent extended headers, into an array of structures with true
488 numbers, as opposed to character strings. Return nonzero if
491 This function invalidates current_header. */
494 fill_in_sparse_array (void)
496 off_t sparse_data_size
= current_stat
.st_size
;
497 off_t file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
500 union block
*h
= current_header
;
504 for (sparses
= 0; sparses
< SPARSES_IN_OLDGNU_HEADER
; sparses
++)
506 struct sparse
const *s
= &h
->oldgnu_header
.sp
[sparses
];
509 if (s
->numbytes
[0] == '\0')
511 sparsearray
[sparses
].offset
= offset
= OFF_FROM_HEADER (s
->offset
);
512 sparsearray
[sparses
].numbytes
= numbytes
=
513 SIZE_FROM_HEADER (s
->numbytes
);
514 sparse_data_size
-= numbytes
;
515 if (offset
< 0 || file_size
< offset
+ numbytes
|| sparse_data_size
< 0)
519 if (h
->oldgnu_header
.isextended
)
522 h
= find_next_block ();
525 ERROR ((0, 0, _("Unexpected EOF in archive")));
529 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
531 struct sparse
const *s
= &h
->sparse_header
.sp
[counter
];
534 if (s
->numbytes
[0] == '\0')
537 if (sparses
== sp_array_size
)
541 xrealloc (sparsearray
,
542 sp_array_size
* sizeof *sparsearray
);
545 sparsearray
[sparses
].offset
= offset
=
546 OFF_FROM_HEADER (s
->offset
);
547 sparsearray
[sparses
].numbytes
= numbytes
=
548 SIZE_FROM_HEADER (s
->numbytes
);
549 sparse_data_size
-= numbytes
;
550 if (offset
< 0 || file_size
< offset
+ numbytes
551 || sparse_data_size
< 0)
556 set_next_block_after (h
);
558 } while (h
->sparse_header
.isextended
);
563 ERROR ((0, 0, "%s: invalid sparse archive member", current_file_name
));
569 extract_sparse_file (int fd
, char const *name
,
570 off_t sizeleft
, off_t file_size
)
574 while (sizeleft
!= 0)
578 union block
*data_block
= find_next_block ();
581 ERROR ((0, 0, _("Unexpected EOF in archive")));
584 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
586 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
589 written
= sparsearray
[sparse_ind
++].numbytes
;
590 while (written
> BLOCKSIZE
)
592 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
595 if (count
!= BLOCKSIZE
)
597 write_error_details (name
, count
, BLOCKSIZE
);
600 set_next_block_after (data_block
);
601 data_block
= find_next_block ();
604 ERROR ((0, 0, _("Unexpected EOF in archive")));
609 count
= full_write (fd
, data_block
->buffer
, written
);
612 if (count
!= written
)
614 write_error_details (name
, count
, written
);
618 set_next_block_after (data_block
);
621 if (ftruncate (fd
, file_size
) != 0)
622 truncate_error (name
);
627 /* Fix the statuses of all directories whose statuses need fixing, and
628 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
629 nonzero, do this for all such directories; otherwise, stop at the
630 first directory that is marked to be fixed up only after delayed
631 symlinks are applied. */
633 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
635 size_t file_name_len
= strlen (file_name
);
636 bool check_for_renamed_directories
= 0;
638 while (delayed_set_stat_head
)
640 struct delayed_set_stat
*data
= delayed_set_stat_head
;
641 bool skip_this_one
= 0;
643 struct stat
const *current_stat_info
= 0;
645 check_for_renamed_directories
|= data
->after_symlinks
;
647 if (after_symlinks
< data
->after_symlinks
648 || (data
->file_name_len
< file_name_len
649 && file_name
[data
->file_name_len
]
650 && (ISSLASH (file_name
[data
->file_name_len
])
651 || ISSLASH (file_name
[data
->file_name_len
- 1]))
652 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
655 if (check_for_renamed_directories
)
657 current_stat_info
= &st
;
658 if (stat (data
->file_name
, &st
) != 0)
660 stat_error (data
->file_name
);
663 else if (! (st
.st_dev
== data
->stat_info
.st_dev
664 && (st
.st_ino
== data
->stat_info
.st_ino
)))
667 _("%s: Directory renamed before its status could be extracted"),
668 quotearg_colon (data
->file_name
)));
674 set_stat (data
->file_name
, &data
->stat_info
, current_stat_info
,
675 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
677 delayed_set_stat_head
= data
->next
;
682 /* Extract a file from the archive. */
684 extract_archive (void)
686 union block
*data_block
;
695 int interdir_made
= 0;
700 set_next_block_after (current_header
);
701 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
703 if (interactive_option
&& !confirm ("extract", current_file_name
))
709 /* Print the block from current_header and current_stat. */
714 file_name
= safer_name_suffix (current_file_name
, 0);
716 apply_nonancestor_delayed_set_stat (file_name
, 0);
718 /* Take a safety backup of a previously existing file. */
720 if (backup_option
&& !to_stdout_option
)
721 if (!maybe_backup_file (file_name
, 0))
724 ERROR ((0, e
, _("%s: Was unable to backup this file"),
725 quotearg_colon (file_name
)));
730 /* Extract the archive entry according to its type. */
732 typeflag
= current_header
->header
.typeflag
;
736 file_size
= OFF_FROM_HEADER (current_header
->oldgnu_header
.realsize
);
737 if (! fill_in_sparse_array ())
745 /* Appears to be a file. But BSD tar uses the convention that a slash
746 suffix means a directory. */
748 if (current_trailing_slash
)
751 /* FIXME: deal with protection issues. */
754 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
755 | (old_files_option
== OVERWRITE_OLD_FILES
758 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
760 if (to_stdout_option
)
766 if (! prepare_to_extract (file_name
, 0))
775 /* Contiguous files (on the Masscomp) have to specify the size in
776 the open call that creates them. */
778 if (typeflag
== CONTTYPE
)
779 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat
.st_size
);
781 fd
= open (file_name
, openflag
, mode
);
783 #else /* not O_CTG */
784 if (typeflag
== CONTTYPE
)
786 static int conttype_diagnosed
;
788 if (!conttype_diagnosed
)
790 conttype_diagnosed
= 1;
791 WARN ((0, 0, _("Extracting contiguous files as regular files")));
794 fd
= open (file_name
, openflag
, mode
);
796 #endif /* not O_CTG */
800 if (maybe_recoverable (file_name
, &interdir_made
))
803 open_error (file_name
);
811 if (typeflag
== GNUTYPE_SPARSE
)
814 size_t name_length_bis
;
816 /* Kludge alert. NAME is assigned to header.name because
817 during the extraction, the space that contains the header
818 will get scribbled on, and the name will get munged, so any
819 error messages that happen to contain the filename will look
820 REAL interesting unless we do this. */
822 name_length_bis
= strlen (file_name
) + 1;
823 name
= xmalloc (name_length_bis
);
824 memcpy (name
, file_name
, name_length_bis
);
825 size
= extract_sparse_file (fd
, name
,
826 current_stat
.st_size
, file_size
);
830 for (size
= current_stat
.st_size
; size
> 0; )
832 if (multi_volume_option
)
834 assign_string (&save_name
, current_file_name
);
835 save_totsize
= current_stat
.st_size
;
836 save_sizeleft
= size
;
839 /* Locate data, determine max length writeable, write it,
840 block that we have used the data, then check if the write
843 data_block
= find_next_block ();
846 ERROR ((0, 0, _("Unexpected EOF in archive")));
847 break; /* FIXME: What happens, then? */
850 written
= available_space_after (data_block
);
855 count
= full_write (fd
, data_block
->buffer
, written
);
858 set_next_block_after ((union block
*)
859 (data_block
->buffer
+ written
- 1));
860 if (count
!= written
)
862 write_error_details (file_name
, count
, written
);
869 if (multi_volume_option
)
870 assign_string (&save_name
, 0);
872 /* If writing to stdout, don't try to do anything to the filename;
873 it doesn't exist, or we don't want to touch it anyway. */
875 if (to_stdout_option
)
881 close_error (file_name
);
886 set_stat (file_name
, ¤t_stat
, 0, 0,
887 (old_files_option
== OVERWRITE_OLD_FILES
889 : ARCHIVED_PERMSTATUS
),
895 if (! prepare_to_extract (file_name
, 0))
898 if (absolute_names_option
899 || ! (ISSLASH (current_link_name
900 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
901 || contains_dot_dot (current_link_name
)))
903 while (status
= symlink (current_link_name
, file_name
),
905 if (!maybe_recoverable (file_name
, &interdir_made
))
909 set_stat (file_name
, ¤t_stat
, 0, 0, 0, SYMTYPE
);
911 symlink_error (current_link_name
, file_name
);
915 /* This symbolic link is potentially dangerous. Don't
916 create it now; instead, create a placeholder file, which
917 will be replaced after other extraction is done. */
920 while (fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
922 if (! maybe_recoverable (file_name
, &interdir_made
))
927 open_error (file_name
);
928 else if (fstat (fd
, &st
) != 0)
930 stat_error (file_name
);
933 else if (close (fd
) != 0)
934 close_error (file_name
);
937 struct delayed_set_stat
*h
;
938 struct delayed_symlink
*p
=
939 xmalloc (offsetof (struct delayed_symlink
, target
)
940 + strlen (current_link_name
) + 1);
941 p
->next
= delayed_symlink_head
;
942 delayed_symlink_head
= p
;
945 p
->mtime
= st
.st_mtime
;
946 p
->uid
= current_stat
.st_uid
;
947 p
->gid
= current_stat
.st_gid
;
948 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
949 + strlen (file_name
) + 1);
950 p
->sources
->next
= 0;
951 strcpy (p
->sources
->string
, file_name
);
952 strcpy (p
->target
, current_link_name
);
954 h
= delayed_set_stat_head
;
955 if (h
&& ! h
->after_symlinks
956 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
957 && ISSLASH (file_name
[h
->file_name_len
])
958 && (base_name (file_name
)
959 == file_name
+ h
->file_name_len
+ 1))
963 h
->after_symlinks
= 1;
965 if (stat (h
->file_name
, &st
) != 0)
966 stat_error (h
->file_name
);
969 h
->stat_info
.st_dev
= st
.st_dev
;
970 h
->stat_info
.st_ino
= st
.st_ino
;
973 while ((h
= h
->next
) && ! h
->after_symlinks
);
980 if (status
!= 0 && backup_option
)
986 static int warned_once
;
992 _("Attempting extraction of symbolic links as hard links")));
1000 if (! prepare_to_extract (file_name
, 0))
1005 char const *link_name
= safer_name_suffix (current_link_name
, 1);
1006 struct stat st1
, st2
;
1009 /* MSDOS does not implement links. However, djgpp's link() actually
1011 status
= link (link_name
, file_name
);
1015 struct delayed_symlink
*ds
= delayed_symlink_head
;
1016 if (ds
&& stat (link_name
, &st1
) == 0)
1017 for (; ds
; ds
= ds
->next
)
1018 if (ds
->dev
== st1
.st_dev
1019 && ds
->ino
== st1
.st_ino
1020 && ds
->mtime
== st1
.st_mtime
)
1022 struct string_list
*p
=
1023 xmalloc (offsetof (struct string_list
, string
)
1024 + strlen (file_name
) + 1);
1025 strcpy (p
->string
, file_name
);
1026 p
->next
= ds
->sources
;
1032 if (maybe_recoverable (file_name
, &interdir_made
))
1035 if (incremental_option
&& errno
== EEXIST
)
1038 if (stat (link_name
, &st1
) == 0
1039 && stat (file_name
, &st2
) == 0
1040 && st1
.st_dev
== st2
.st_dev
1041 && st1
.st_ino
== st2
.st_ino
)
1044 link_error (link_name
, file_name
);
1046 undo_last_backup ();
1052 current_stat
.st_mode
|= S_IFCHR
;
1058 current_stat
.st_mode
|= S_IFBLK
;
1061 #if S_IFCHR || S_IFBLK
1063 if (! prepare_to_extract (file_name
, 0))
1066 status
= mknod (file_name
, current_stat
.st_mode
,
1067 current_stat
.st_rdev
);
1070 if (maybe_recoverable (file_name
, &interdir_made
))
1072 mknod_error (file_name
);
1074 undo_last_backup ();
1077 set_stat (file_name
, ¤t_stat
, 0, 0,
1078 ARCHIVED_PERMSTATUS
, typeflag
);
1082 #if HAVE_MKFIFO || defined mkfifo
1084 if (! prepare_to_extract (file_name
, 0))
1087 while (status
= mkfifo (file_name
, current_stat
.st_mode
),
1089 if (!maybe_recoverable (file_name
, &interdir_made
))
1093 set_stat (file_name
, ¤t_stat
, 0, 0,
1094 ARCHIVED_PERMSTATUS
, typeflag
);
1097 mkfifo_error (file_name
);
1099 undo_last_backup ();
1105 case GNUTYPE_DUMPDIR
:
1107 if (incremental_option
)
1109 /* Read the entry and delete files that aren't listed in the
1112 gnu_restore (file_name
);
1114 else if (typeflag
== GNUTYPE_DUMPDIR
)
1117 mode
= ((current_stat
.st_mode
1118 | (we_are_root
? 0 : MODE_WXUSR
))
1121 status
= prepare_to_extract (file_name
, 1);
1125 goto directory_exists
;
1128 status
= mkdir (file_name
, mode
);
1134 || old_files_option
== DEFAULT_OLD_FILES
1135 || old_files_option
== OVERWRITE_OLD_FILES
))
1138 if (stat (file_name
, &st
) == 0)
1142 repair_delayed_set_stat (file_name
, &st
);
1145 if (S_ISDIR (st
.st_mode
))
1147 mode
= st
.st_mode
& ~ current_umask
;
1148 goto directory_exists
;
1154 if (maybe_recoverable (file_name
, &interdir_made
))
1157 if (errno
!= EEXIST
)
1159 mkdir_error (file_name
);
1161 undo_last_backup ();
1168 || old_files_option
== DEFAULT_OLD_FILES
1169 || old_files_option
== OVERWRITE_OLD_FILES
)
1170 delay_set_stat (file_name
, ¤t_stat
,
1171 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1173 ? ARCHIVED_PERMSTATUS
1174 : UNKNOWN_PERMSTATUS
));
1177 case GNUTYPE_VOLHDR
:
1179 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1186 case GNUTYPE_MULTIVOL
:
1188 _("%s: Cannot extract -- file is continued from another volume"),
1189 quotearg_colon (current_file_name
)));
1192 undo_last_backup ();
1195 case GNUTYPE_LONGNAME
:
1196 case GNUTYPE_LONGLINK
:
1197 ERROR ((0, 0, _("Visible long name error")));
1200 undo_last_backup ();
1205 _("%s: Unknown file type '%c', extracted as normal file"),
1206 quotearg_colon (file_name
), typeflag
));
1211 /* Extract the symbolic links whose final extraction were delayed. */
1213 apply_delayed_symlinks (void)
1215 struct delayed_symlink
*ds
;
1217 for (ds
= delayed_symlink_head
; ds
; )
1219 struct string_list
*sources
= ds
->sources
;
1220 char const *valid_source
= 0;
1222 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1224 char const *source
= sources
->string
;
1227 /* Make sure the placeholder file is still there. If not,
1228 don't create a symlink, as the placeholder was probably
1229 removed by a later extraction. */
1230 if (lstat (source
, &st
) == 0
1231 && st
.st_dev
== ds
->dev
1232 && st
.st_ino
== ds
->ino
1233 && st
.st_mtime
== ds
->mtime
)
1235 /* Unlink the placeholder, then create a hard link if possible,
1236 a symbolic link otherwise. */
1237 if (unlink (source
) != 0)
1238 unlink_error (source
);
1239 else if (valid_source
&& link (valid_source
, source
) == 0)
1241 else if (symlink (ds
->target
, source
) != 0)
1242 symlink_error (ds
->target
, source
);
1245 valid_source
= source
;
1246 st
.st_uid
= ds
->uid
;
1247 st
.st_gid
= ds
->gid
;
1248 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1253 for (sources
= ds
->sources
; sources
; )
1255 struct string_list
*next
= sources
->next
;
1261 struct delayed_symlink
*next
= ds
->next
;
1267 delayed_symlink_head
= 0;
1270 /* Finish the extraction of an archive. */
1272 extract_finish (void)
1274 /* First, fix the status of ordinary directories that need fixing. */
1275 apply_nonancestor_delayed_set_stat ("", 0);
1277 /* Then, apply delayed symlinks, so that they don't affect delayed
1278 directory status-setting for ordinary directories. */
1279 apply_delayed_symlinks ();
1281 /* Finally, fix the status of directories that are ancestors
1282 of delayed symlinks. */
1283 apply_nonancestor_delayed_set_stat ("", 1);
1290 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));