1 /* Extract files from a tar archive.
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001 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 int 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. The head of the list
57 has the longest name; each non-head element in the list is an
58 ancestor (in the directory hierarchy) of the preceding element. */
59 struct delayed_set_stat
61 struct delayed_set_stat
*next
;
62 struct stat stat_info
;
64 mode_t invert_permissions
;
65 enum permstatus permstatus
;
69 static struct delayed_set_stat
*delayed_set_stat_head
;
71 /* List of symbolic links whose creation we have delayed. */
72 struct delayed_symlink
74 /* The next delayed symbolic link in the list. */
75 struct delayed_symlink
*next
;
77 /* The device, inode number and last-modified time of the placeholder. */
82 /* The desired owner and group of the symbolic link. */
86 /* A list of sources for this symlink. The sources are all to be
87 hard-linked together. */
88 struct string_list
*sources
;
90 /* The desired target of the desired link. */
94 static struct delayed_symlink
*delayed_symlink_head
;
98 struct string_list
*next
;
102 /* Set up to extract files. */
106 we_are_root
= geteuid () == 0;
107 same_permissions_option
+= we_are_root
;
108 same_owner_option
+= we_are_root
;
109 xalloc_fail_func
= extract_finish
;
111 /* Option -p clears the kernel umask, so it does not affect proper
112 restoration of file permissions. New intermediate directories will
113 comply with umask at start of program. */
115 newdir_umask
= umask (0);
116 if (0 < same_permissions_option
)
120 umask (newdir_umask
); /* restore the kernel umask */
121 current_umask
= newdir_umask
;
125 /* If restoring permissions, restore the mode for FILE_NAME from
126 information given in *STAT_INFO; otherwise invert the
127 INVERT_PERMISSIONS bits from the file's current permissions.
128 PERMSTATUS specifies the status of the file's permissions.
129 TYPEFLAG specifies the type of the file. */
131 set_mode (char const *file_name
, struct stat
const *stat_info
,
132 mode_t invert_permissions
, enum permstatus permstatus
,
137 if (0 < same_permissions_option
138 && permstatus
!= INTERDIR_PERMSTATUS
)
140 mode
= stat_info
->st_mode
;
142 /* If we created the file and it has a usual mode, then its mode
143 is normally set correctly already. But on many hosts, some
144 directories inherit the setgid bits from their parents, so we
145 we must set directories' modes explicitly. */
146 if (permstatus
== ARCHIVED_PERMSTATUS
147 && ! (mode
& ~ MODE_RWX
)
148 && typeflag
!= DIRTYPE
149 && typeflag
!= GNUTYPE_DUMPDIR
)
152 else if (! invert_permissions
)
156 /* We must inspect a directory's current permissions, since the
157 directory may have inherited its setgid bit from its parent.
159 INVERT_PERMISSIONS happens to be nonzero only for directories
160 that we created, so there's no point optimizing this code for
163 if (stat (file_name
, &st
) != 0)
165 stat_error (file_name
);
168 mode
= st
.st_mode
^ invert_permissions
;
171 if (chmod (file_name
, mode
) != 0)
172 chmod_error_details (file_name
, mode
);
175 /* Check time after successfully setting FILE_NAME's time stamp to T. */
177 check_time (char const *file_name
, time_t t
)
180 if (start_time
< t
&& (now
= time (0)) < t
)
181 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
182 file_name
, tartime (t
), (unsigned long) (t
- now
)));
185 /* Restore stat attributes (owner, group, mode and times) for
186 FILE_NAME, using information given in *STAT_INFO.
187 If not restoring permissions, invert the
188 INVERT_PERMISSIONS bits from the file's current permissions.
189 PERMSTATUS specifies the status of the file's permissions.
190 TYPEFLAG specifies the type of the file. */
192 /* FIXME: About proper restoration of symbolic link attributes, we still do
193 not have it right. Pretesters' reports tell us we need further study and
194 probably more configuration. For now, just use lchown if it exists, and
195 punt for the rest. Sigh! */
198 set_stat (char const *file_name
, struct stat
const *stat_info
,
199 mode_t invert_permissions
, enum permstatus permstatus
,
202 struct utimbuf utimbuf
;
204 if (typeflag
!= SYMTYPE
)
206 /* We do the utime before the chmod because some versions of utime are
207 broken and trash the modes of the file. */
209 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
211 /* We set the accessed time to `now', which is really the time we
212 started extracting files, unless incremental_option is used, in
213 which case .st_atime is used. */
215 /* FIXME: incremental_option should set ctime too, but how? */
217 if (incremental_option
)
218 utimbuf
.actime
= stat_info
->st_atime
;
220 utimbuf
.actime
= start_time
;
222 utimbuf
.modtime
= stat_info
->st_mtime
;
224 if (utime (file_name
, &utimbuf
) < 0)
225 utime_error (file_name
);
228 check_time (file_name
, stat_info
->st_atime
);
229 check_time (file_name
, stat_info
->st_mtime
);
233 /* Some systems allow non-root users to give files away. Once this
234 done, it is not possible anymore to change file permissions, so we
235 have to set permissions prior to possibly giving files away. */
237 set_mode (file_name
, stat_info
,
238 invert_permissions
, permstatus
, typeflag
);
241 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
243 /* When lchown exists, it should be used to change the attributes of
244 the symbolic link itself. In this case, a mere chown would change
245 the attributes of the file the symbolic link is pointing to, and
246 should be avoided. */
248 if (typeflag
== SYMTYPE
)
251 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
252 chown_error_details (file_name
,
253 stat_info
->st_uid
, stat_info
->st_gid
);
258 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
259 chown_error_details (file_name
,
260 stat_info
->st_uid
, stat_info
->st_gid
);
262 /* On a few systems, and in particular, those allowing to give files
263 away, changing the owner or group destroys the suid or sgid bits.
264 So let's attempt setting these bits once more. */
265 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
266 set_mode (file_name
, stat_info
,
267 invert_permissions
, permstatus
, typeflag
);
272 /* Remember to restore stat attributes (owner, group, mode and times)
273 for the directory FILE_NAME, using information given in *STAT_INFO,
274 once we stop extracting files into that directory.
275 If not restoring permissions, remember to invert the
276 INVERT_PERMISSIONS bits from the file's current permissions.
277 PERMSTATUS specifies the status of the file's permissions. */
279 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
280 mode_t invert_permissions
, enum permstatus permstatus
)
282 size_t file_name_len
= strlen (file_name
);
283 struct delayed_set_stat
*data
=
284 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
285 + file_name_len
+ 1);
286 data
->file_name_len
= file_name_len
;
287 strcpy (data
->file_name
, file_name
);
288 data
->invert_permissions
= invert_permissions
;
289 data
->permstatus
= permstatus
;
290 data
->stat_info
= *stat_info
;
291 data
->next
= delayed_set_stat_head
;
292 delayed_set_stat_head
= data
;
295 /* Update the delayed_set_stat info for an intermediate directory
296 created on the path to DIR_NAME. The intermediate directory turned
297 out to be the same as this directory, e.g. due to ".." or symbolic
298 links. *DIR_STAT_INFO is the status of the directory. */
300 repair_delayed_set_stat (char const *dir_name
,
301 struct stat
const *dir_stat_info
)
303 struct delayed_set_stat
*data
;
304 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
307 if (stat (data
->file_name
, &st
) != 0)
309 stat_error (data
->file_name
);
313 if (st
.st_dev
== dir_stat_info
->st_dev
314 && st
.st_ino
== dir_stat_info
->st_ino
)
316 data
->stat_info
= current_stat
;
317 data
->invert_permissions
= (MODE_RWX
318 & (current_stat
.st_mode
^ st
.st_mode
));
319 data
->permstatus
= ARCHIVED_PERMSTATUS
;
324 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
325 quotearg_colon (dir_name
)));
328 /* After a file/link/symlink/directory creation has failed, see if
329 it's because some required directory was not present, and if so,
330 create all required directories. Return non-zero if a directory
333 make_directories (char *file_name
)
335 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
336 char *cursor
; /* points into path */
337 int did_something
= 0; /* did we do anything yet? */
339 int invert_permissions
;
343 for (cursor
= cursor0
; *cursor
; cursor
++)
345 if (! ISSLASH (*cursor
))
348 /* Avoid mkdir of empty string, if leading or double '/'. */
350 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
353 /* Avoid mkdir where last part of path is "." or "..". */
355 if (cursor
[-1] == '.'
356 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
357 || (cursor
[-2] == '.'
358 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
361 *cursor
= '\0'; /* truncate the path there */
362 mode
= MODE_RWX
& ~ newdir_umask
;
363 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
364 status
= mkdir (file_name
, mode
^ invert_permissions
);
368 /* Create a struct delayed_set_stat even if
369 invert_permissions is zero, because
370 repair_delayed_set_stat may need to update the struct. */
371 delay_set_stat (file_name
,
372 ¤t_stat
/* ignored */,
373 invert_permissions
, INTERDIR_PERMSTATUS
);
375 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
386 /* Turbo C mkdir gives a funny errno. */
390 /* Directory already exists. */
393 /* Some other error in the mkdir. We return to the caller. */
397 return did_something
; /* tell them to retry if we made one */
400 /* Prepare to extract a file.
401 Return zero if extraction should not proceed. */
404 prepare_to_extract (char const *file_name
)
406 if (to_stdout_option
)
409 if (old_files_option
== UNLINK_FIRST_OLD_FILES
410 && !remove_any_file (file_name
, recursive_unlink_option
)
411 && errno
&& errno
!= ENOENT
)
413 unlink_error (file_name
);
420 /* Attempt repairing what went wrong with the extraction. Delete an
421 already existing file or create missing intermediate directories.
422 Return nonzero if we somewhat increased our chances at a successful
423 extraction. errno is properly restored on zero return. */
425 maybe_recoverable (char *file_name
, int *interdir_made
)
433 /* Remove an old file, if the options allow this. */
435 switch (old_files_option
)
440 case DEFAULT_OLD_FILES
:
441 case OVERWRITE_OLD_FILES
:
443 int r
= remove_any_file (file_name
, 0);
450 /* Attempt creating missing intermediate directories. */
451 if (! make_directories (file_name
))
460 /* Just say we can't do anything about it... */
467 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
471 /* assuming sizeleft is initially totalsize */
473 while (*sizeleft
> 0)
477 union block
*data_block
= find_next_block ();
480 ERROR ((0, 0, _("Unexpected EOF in archive")));
483 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
485 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
488 written
= sparsearray
[sparse_ind
++].numbytes
;
489 while (written
> BLOCKSIZE
)
491 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
494 if (count
!= BLOCKSIZE
)
496 write_error_details (name
, count
, BLOCKSIZE
);
499 set_next_block_after (data_block
);
500 data_block
= find_next_block ();
503 ERROR ((0, 0, _("Unexpected EOF in archive")));
508 count
= full_write (fd
, data_block
->buffer
, written
);
511 if (count
!= written
)
513 write_error_details (name
, count
, written
);
517 set_next_block_after (data_block
);
521 /* Fix the statuses of all directories whose statuses need fixing, and
522 which are not ancestors of FILE_NAME. */
524 apply_nonancestor_delayed_set_stat (char const *file_name
)
526 size_t file_name_len
= strlen (file_name
);
528 while (delayed_set_stat_head
)
530 struct delayed_set_stat
*data
= delayed_set_stat_head
;
531 if (data
->file_name_len
< file_name_len
532 && file_name
[data
->file_name_len
]
533 && (ISSLASH (file_name
[data
->file_name_len
])
534 || ISSLASH (file_name
[data
->file_name_len
- 1]))
535 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0)
537 delayed_set_stat_head
= data
->next
;
538 set_stat (data
->file_name
, &data
->stat_info
,
539 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
544 /* Extract a file from the archive. */
546 extract_archive (void)
548 union block
*data_block
;
559 int interdir_made
= 0;
563 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
565 set_next_block_after (current_header
);
566 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
568 if (interactive_option
&& !confirm ("extract", current_file_name
))
574 /* Print the block from current_header and current_stat. */
579 /* Check for fully specified file names and other atrocities. */
582 if (! absolute_names_option
)
584 if (contains_dot_dot (CURRENT_FILE_NAME
))
586 ERROR ((0, 0, _("%s: Member name contains `..'"),
587 quotearg_colon (CURRENT_FILE_NAME
)));
592 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
593 while (ISSLASH (CURRENT_FILE_NAME
[0]))
598 static int warned_once
;
603 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
604 (int) skipcrud
, current_file_name
));
609 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
);
611 /* Take a safety backup of a previously existing file. */
613 if (backup_option
&& !to_stdout_option
)
614 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
617 ERROR ((0, e
, _("%s: Was unable to backup this file"),
618 quotearg_colon (CURRENT_FILE_NAME
)));
623 /* Extract the archive entry according to its type. */
625 typeflag
= current_header
->header
.typeflag
;
628 /* JK - What we want to do if the file is sparse is loop through
629 the array of sparse structures in the header and read in and
630 translate the character strings representing 1) the offset at
631 which to write and 2) how many bytes to write into numbers,
632 which we store into the scratch array, "sparsearray". This
633 array makes our life easier the same way it did in creating the
634 tar file that had to deal with a sparse file.
636 After we read in the first five (at most) sparse structures, we
637 check to see if the file has an extended header, i.e., if more
638 sparse structures are needed to describe the contents of the new
639 file. If so, we read in the extended headers and continue to
640 store their contents into the sparsearray. */
645 xmalloc (sp_array_size
* sizeof (struct sp_array
));
647 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
649 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
650 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
651 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
652 if (!sparsearray
[counter
].numbytes
)
656 if (current_header
->oldgnu_header
.isextended
)
658 /* Read in the list of extended headers and translate them
659 into the sparsearray as before. Note that this
660 invalidates current_header. */
662 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
666 exhdr
= find_next_block ();
669 ERROR ((0, 0, _("Unexpected EOF in archive")));
672 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
674 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
675 if (counter
+ ind
> sp_array_size
- 1)
677 /* Realloc the scratch area since we've run out of
682 xrealloc (sparsearray
,
683 sp_array_size
* sizeof (struct sp_array
));
685 if (s
->numbytes
[0] == 0)
687 sparsearray
[counter
+ ind
].offset
=
688 OFF_FROM_HEADER (s
->offset
);
689 sparsearray
[counter
+ ind
].numbytes
=
690 SIZE_FROM_HEADER (s
->numbytes
);
692 if (!exhdr
->sparse_header
.isextended
)
696 ind
+= SPARSES_IN_SPARSE_HEADER
;
697 set_next_block_after (exhdr
);
700 set_next_block_after (exhdr
);
708 /* Appears to be a file. But BSD tar uses the convention that a slash
709 suffix means a directory. */
711 name_length
= strlen (CURRENT_FILE_NAME
);
712 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
713 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
716 /* FIXME: deal with protection issues. */
719 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
720 | (old_files_option
== OVERWRITE_OLD_FILES
723 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
725 if (to_stdout_option
)
731 if (! prepare_to_extract (CURRENT_FILE_NAME
))
740 /* Contiguous files (on the Masscomp) have to specify the size in
741 the open call that creates them. */
743 if (typeflag
== CONTTYPE
)
744 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
745 mode
, current_stat
.st_size
);
747 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
749 #else /* not O_CTG */
750 if (typeflag
== CONTTYPE
)
752 static int conttype_diagnosed
;
754 if (!conttype_diagnosed
)
756 conttype_diagnosed
= 1;
757 WARN ((0, 0, _("Extracting contiguous files as regular files")));
760 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
762 #endif /* not O_CTG */
766 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
769 open_error (CURRENT_FILE_NAME
);
777 if (typeflag
== GNUTYPE_SPARSE
)
780 size_t name_length_bis
;
782 /* Kludge alert. NAME is assigned to header.name because
783 during the extraction, the space that contains the header
784 will get scribbled on, and the name will get munged, so any
785 error messages that happen to contain the filename will look
786 REAL interesting unless we do this. */
788 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
789 name
= xmalloc (name_length_bis
);
790 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
791 size
= current_stat
.st_size
;
792 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
796 for (size
= current_stat
.st_size
; size
> 0; )
798 if (multi_volume_option
)
800 assign_string (&save_name
, current_file_name
);
801 save_totsize
= current_stat
.st_size
;
802 save_sizeleft
= size
;
805 /* Locate data, determine max length writeable, write it,
806 block that we have used the data, then check if the write
809 data_block
= find_next_block ();
812 ERROR ((0, 0, _("Unexpected EOF in archive")));
813 break; /* FIXME: What happens, then? */
816 written
= available_space_after (data_block
);
821 count
= full_write (fd
, data_block
->buffer
, written
);
824 set_next_block_after ((union block
*)
825 (data_block
->buffer
+ written
- 1));
826 if (count
!= written
)
828 write_error_details (CURRENT_FILE_NAME
, count
, written
);
835 if (multi_volume_option
)
836 assign_string (&save_name
, 0);
838 /* If writing to stdout, don't try to do anything to the filename;
839 it doesn't exist, or we don't want to touch it anyway. */
841 if (to_stdout_option
)
847 close_error (CURRENT_FILE_NAME
);
852 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
853 (old_files_option
== OVERWRITE_OLD_FILES
855 : ARCHIVED_PERMSTATUS
),
861 if (! prepare_to_extract (CURRENT_FILE_NAME
))
864 if (absolute_names_option
865 || ! (ISSLASH (current_link_name
866 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
867 || contains_dot_dot (current_link_name
)))
869 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
871 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
875 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, SYMTYPE
);
877 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
881 /* This symbolic link is potentially dangerous. Don't
882 create it now; instead, create a placeholder file, which
883 will be replaced after other extraction is done. */
886 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
888 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
893 open_error (CURRENT_FILE_NAME
);
894 else if (fstat (fd
, &st
) != 0)
896 stat_error (CURRENT_FILE_NAME
);
899 else if (close (fd
) != 0)
900 close_error (CURRENT_FILE_NAME
);
903 struct delayed_symlink
*p
=
904 xmalloc (offsetof (struct delayed_symlink
, target
)
905 + strlen (current_link_name
) + 1);
906 p
->next
= delayed_symlink_head
;
907 delayed_symlink_head
= p
;
910 p
->mtime
= st
.st_mtime
;
911 p
->uid
= current_stat
.st_uid
;
912 p
->gid
= current_stat
.st_gid
;
913 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
914 + strlen (CURRENT_FILE_NAME
) + 1);
915 p
->sources
->next
= 0;
916 strcpy (p
->sources
->string
, CURRENT_FILE_NAME
);
917 strcpy (p
->target
, current_link_name
);
922 if (status
!= 0 && backup_option
)
928 static int warned_once
;
934 _("Attempting extraction of symbolic links as hard links")));
942 if (! prepare_to_extract (CURRENT_FILE_NAME
))
947 struct stat st1
, st2
;
950 /* MSDOS does not implement links. However, djgpp's link() actually
952 status
= link (current_link_name
, CURRENT_FILE_NAME
);
956 struct delayed_symlink
*ds
= delayed_symlink_head
;
957 if (ds
&& stat (current_link_name
, &st1
) == 0)
958 for (; ds
; ds
= ds
->next
)
959 if (ds
->dev
== st1
.st_dev
960 && ds
->ino
== st1
.st_ino
961 && ds
->mtime
== st1
.st_mtime
)
963 struct string_list
*p
=
964 xmalloc (offsetof (struct string_list
, string
)
965 + strlen (CURRENT_FILE_NAME
) + 1);
966 strcpy (p
->string
, CURRENT_FILE_NAME
);
967 p
->next
= ds
->sources
;
973 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
976 if (incremental_option
&& errno
== EEXIST
)
979 if (stat (current_link_name
, &st1
) == 0
980 && stat (CURRENT_FILE_NAME
, &st2
) == 0
981 && st1
.st_dev
== st2
.st_dev
982 && st1
.st_ino
== st2
.st_ino
)
985 link_error (current_link_name
, CURRENT_FILE_NAME
);
993 current_stat
.st_mode
|= S_IFCHR
;
999 current_stat
.st_mode
|= S_IFBLK
;
1002 #if S_IFCHR || S_IFBLK
1004 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1007 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
1008 current_stat
.st_rdev
);
1011 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1013 mknod_error (CURRENT_FILE_NAME
);
1015 undo_last_backup ();
1018 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
1019 ARCHIVED_PERMSTATUS
, typeflag
);
1023 #if HAVE_MKFIFO || defined mkfifo
1025 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1028 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1030 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1034 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
1035 ARCHIVED_PERMSTATUS
, typeflag
);
1038 mkfifo_error (CURRENT_FILE_NAME
);
1040 undo_last_backup ();
1046 case GNUTYPE_DUMPDIR
:
1047 name_length
= strlen (CURRENT_FILE_NAME
);
1050 /* Remove any redundant trailing "/"s. */
1051 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
1052 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
1054 CURRENT_FILE_NAME
[name_length
] = '\0';
1056 if (incremental_option
)
1058 /* Read the entry and delete files that aren't listed in the
1061 gnu_restore (skipcrud
);
1063 else if (typeflag
== GNUTYPE_DUMPDIR
)
1066 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1069 mode
= ((current_stat
.st_mode
1070 | (we_are_root
? 0 : MODE_WXUSR
))
1074 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1079 && (interdir_made
|| old_files_option
== OVERWRITE_OLD_FILES
))
1082 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1086 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1089 if (S_ISDIR (st
.st_mode
))
1091 mode
= st
.st_mode
& ~ current_umask
;
1092 goto directory_exists
;
1098 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1101 if (errno
!= EEXIST
)
1103 mkdir_error (CURRENT_FILE_NAME
);
1105 undo_last_backup ();
1112 || old_files_option
== OVERWRITE_OLD_FILES
)
1113 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1114 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1116 ? ARCHIVED_PERMSTATUS
1117 : UNKNOWN_PERMSTATUS
));
1120 case GNUTYPE_VOLHDR
:
1122 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1129 case GNUTYPE_MULTIVOL
:
1131 _("%s: Cannot extract -- file is continued from another volume"),
1132 quotearg_colon (current_file_name
)));
1135 undo_last_backup ();
1138 case GNUTYPE_LONGNAME
:
1139 case GNUTYPE_LONGLINK
:
1140 ERROR ((0, 0, _("Visible long name error")));
1143 undo_last_backup ();
1148 _("%s: Unknown file type '%c', extracted as normal file"),
1149 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1153 #undef CURRENT_FILE_NAME
1156 /* Extract the symbolic links whose final extraction were delayed. */
1158 apply_delayed_symlinks (void)
1160 struct delayed_symlink
*ds
;
1162 for (ds
= delayed_symlink_head
; ds
; )
1164 struct string_list
*sources
= ds
->sources
;
1165 char const *valid_source
= 0;
1167 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1169 char const *source
= sources
->string
;
1172 /* Make sure the placeholder file is still there. If not,
1173 don't create a symlink, as the placeholder was probably
1174 removed by a later extraction. */
1175 if (lstat (source
, &st
) == 0
1176 && st
.st_dev
== ds
->dev
1177 && st
.st_ino
== ds
->ino
1178 && st
.st_mtime
== ds
->mtime
)
1180 /* Unlink the placeholder, then create a hard link if possible,
1181 a symbolic link otherwise. */
1182 if (unlink (source
) != 0)
1183 unlink_error (source
);
1184 else if (valid_source
&& link (valid_source
, source
) == 0)
1186 else if (symlink (ds
->target
, source
) != 0)
1187 symlink_error (ds
->target
, source
);
1190 valid_source
= source
;
1191 st
.st_uid
= ds
->uid
;
1192 st
.st_gid
= ds
->gid
;
1193 set_stat (source
, &st
, 0, 0, SYMTYPE
);
1198 for (sources
= ds
->sources
; sources
; )
1200 struct string_list
*next
= sources
->next
;
1206 struct delayed_symlink
*next
= ds
->next
;
1212 delayed_symlink_head
= 0;
1215 /* Finish the extraction of an archive. */
1217 extract_finish (void)
1219 /* Apply delayed symlinks last, so that they don't affect
1220 delayed directory status-setting. */
1221 apply_nonancestor_delayed_set_stat ("");
1222 apply_delayed_symlinks ();
1229 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));