1 /* Extract files from a tar archive.
2 Copyright 1988,92,93,94,96,97,98,99,2000,2001 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-11-19.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 int we_are_root
; /* true if our effective uid == 0 */
35 static mode_t newdir_umask
; /* umask when creating new directories */
36 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
38 /* Status of the permissions of a file that we are extracting. */
41 /* This file may have existed already; its permissions are unknown. */
44 /* This file was created using the permissions from the archive. */
47 /* This is an intermediate directory; the archive did not specify
52 /* List of directories whose statuses we need to extract after we've
53 finished extracting their subsidiary files. The head of the list
54 has the longest name; each non-head element in the list is an
55 ancestor (in the directory hierarchy) of the preceding element. */
56 struct delayed_set_stat
58 struct delayed_set_stat
*next
;
59 struct stat stat_info
;
61 mode_t invert_permissions
;
62 enum permstatus permstatus
;
66 static struct delayed_set_stat
*delayed_set_stat_head
;
68 /* List of symbolic links whose creation we have delayed. */
69 struct delayed_symlink
71 /* The next delayed symbolic link in the list. */
72 struct delayed_symlink
*next
;
74 /* The device, inode number and last-modified time of the
75 placeholder symbolic link. */
80 /* The desired owner and group of the symbolic link. */
84 /* The location and desired target of the desired link, as two
85 adjacent character strings, both null-terminated. */
89 static struct delayed_symlink
*delayed_symlink_head
;
91 /* Set up to extract files. */
95 we_are_root
= geteuid () == 0;
96 same_permissions_option
+= we_are_root
;
97 same_owner_option
+= we_are_root
;
98 xalloc_fail_func
= extract_finish
;
100 /* Option -p clears the kernel umask, so it does not affect proper
101 restoration of file permissions. New intermediate directories will
102 comply with umask at start of program. */
104 newdir_umask
= umask (0);
105 if (0 < same_permissions_option
)
109 umask (newdir_umask
); /* restore the kernel umask */
110 current_umask
= newdir_umask
;
114 /* If restoring permissions, restore the mode for FILE_NAME from
115 information given in *STAT_INFO; otherwise invert the
116 INVERT_PERMISSIONS bits from the file's current permissions.
117 PERMSTATUS specifies the status of the file's permissions.
118 TYPEFLAG specifies the type of the file. */
120 set_mode (char const *file_name
, struct stat
const *stat_info
,
121 mode_t invert_permissions
, enum permstatus permstatus
,
126 if (0 < same_permissions_option
127 && permstatus
!= INTERDIR_PERMSTATUS
)
129 mode
= stat_info
->st_mode
;
131 /* If we created the file and it has a usual mode, then its mode
132 is normally set correctly already. But on many hosts, some
133 directories inherit the setgid bits from their parents, so we
134 we must set directories' modes explicitly. */
135 if (permstatus
== ARCHIVED_PERMSTATUS
136 && ! (mode
& ~ MODE_RWX
)
137 && typeflag
!= DIRTYPE
138 && typeflag
!= GNUTYPE_DUMPDIR
)
141 else if (! invert_permissions
)
145 /* We must inspect a directory's current permissions, since the
146 directory may have inherited its setgid bit from its parent.
148 INVERT_PERMISSIONS happens to be nonzero only for directories
149 that we created, so there's no point optimizing this code for
152 if (stat (file_name
, &st
) != 0)
154 stat_error (file_name
);
157 mode
= st
.st_mode
^ invert_permissions
;
160 if (chmod (file_name
, mode
) != 0)
161 chmod_error_details (file_name
, mode
);
164 /* Check time after successfully setting FILE_NAME's time stamp to T. */
166 check_time (char const *file_name
, time_t t
)
169 if (start_time
< t
&& (now
= time (0)) < t
)
170 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
171 file_name
, tartime (t
), (unsigned long) (t
- now
)));
174 /* Restore stat attributes (owner, group, mode and times) for
175 FILE_NAME, using information given in *STAT_INFO.
176 If not restoring permissions, invert the
177 INVERT_PERMISSIONS bits from the file's current permissions.
178 PERMSTATUS specifies the status of the file's permissions.
179 TYPEFLAG specifies the type of the file. */
181 /* FIXME: About proper restoration of symbolic link attributes, we still do
182 not have it right. Pretesters' reports tell us we need further study and
183 probably more configuration. For now, just use lchown if it exists, and
184 punt for the rest. Sigh! */
187 set_stat (char const *file_name
, struct stat
const *stat_info
,
188 mode_t invert_permissions
, enum permstatus permstatus
,
191 struct utimbuf utimbuf
;
193 if (typeflag
!= SYMTYPE
)
195 /* We do the utime before the chmod because some versions of utime are
196 broken and trash the modes of the file. */
198 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
200 /* We set the accessed time to `now', which is really the time we
201 started extracting files, unless incremental_option is used, in
202 which case .st_atime is used. */
204 /* FIXME: incremental_option should set ctime too, but how? */
206 if (incremental_option
)
207 utimbuf
.actime
= stat_info
->st_atime
;
209 utimbuf
.actime
= start_time
;
211 utimbuf
.modtime
= stat_info
->st_mtime
;
213 if (utime (file_name
, &utimbuf
) < 0)
214 utime_error (file_name
);
217 check_time (file_name
, stat_info
->st_atime
);
218 check_time (file_name
, stat_info
->st_mtime
);
222 /* Some systems allow non-root users to give files away. Once this
223 done, it is not possible anymore to change file permissions, so we
224 have to set permissions prior to possibly giving files away. */
226 set_mode (file_name
, stat_info
,
227 invert_permissions
, permstatus
, typeflag
);
230 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
232 /* When lchown exists, it should be used to change the attributes of
233 the symbolic link itself. In this case, a mere chown would change
234 the attributes of the file the symbolic link is pointing to, and
235 should be avoided. */
237 if (typeflag
== SYMTYPE
)
240 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
241 chown_error_details (file_name
,
242 stat_info
->st_uid
, stat_info
->st_gid
);
247 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
248 chown_error_details (file_name
,
249 stat_info
->st_uid
, stat_info
->st_gid
);
251 /* On a few systems, and in particular, those allowing to give files
252 away, changing the owner or group destroys the suid or sgid bits.
253 So let's attempt setting these bits once more. */
254 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
255 set_mode (file_name
, stat_info
,
256 invert_permissions
, permstatus
, typeflag
);
261 /* Remember to restore stat attributes (owner, group, mode and times)
262 for the directory FILE_NAME, using information given in *STAT_INFO,
263 once we stop extracting files into that directory.
264 If not restoring permissions, remember to invert the
265 INVERT_PERMISSIONS bits from the file's current permissions.
266 PERMSTATUS specifies the status of the file's permissions. */
268 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
269 mode_t invert_permissions
, enum permstatus permstatus
)
271 size_t file_name_len
= strlen (file_name
);
272 struct delayed_set_stat
*data
= xmalloc (sizeof *data
+ file_name_len
);
273 data
->file_name_len
= file_name_len
;
274 strcpy (data
->file_name
, file_name
);
275 data
->invert_permissions
= invert_permissions
;
276 data
->permstatus
= permstatus
;
277 data
->stat_info
= *stat_info
;
278 data
->next
= delayed_set_stat_head
;
279 delayed_set_stat_head
= data
;
282 /* Update the delayed_set_stat info for an intermediate directory
283 created on the path to DIR_NAME. The intermediate directory turned
284 out to be the same as this directory, e.g. due trailing slash or
285 ".." or symbolic links. *DIR_STAT_INFO is the status of the
288 repair_delayed_set_stat (char const *dir_name
,
289 struct stat
const *dir_stat_info
)
291 struct delayed_set_stat
*data
;
292 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
295 if (stat (data
->file_name
, &st
) != 0)
297 stat_error (data
->file_name
);
301 if (st
.st_dev
== dir_stat_info
->st_dev
302 && st
.st_ino
== dir_stat_info
->st_ino
)
304 data
->stat_info
= current_stat
;
305 data
->invert_permissions
= (MODE_RWX
306 & (current_stat
.st_mode
^ st
.st_mode
));
307 data
->permstatus
= ARCHIVED_PERMSTATUS
;
312 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
313 quotearg_colon (dir_name
)));
316 /* After a file/link/symlink/directory creation has failed, see if
317 it's because some required directory was not present, and if so,
318 create all required directories. Return non-zero if a directory
321 make_directories (char *file_name
)
323 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
324 char *cursor
; /* points into path */
325 int did_something
= 0; /* did we do anything yet? */
327 int invert_permissions
;
331 for (cursor
= cursor0
; *cursor
; cursor
++)
333 if (! ISSLASH (*cursor
))
336 /* Avoid mkdir of empty string, if leading or double '/'. */
338 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
341 /* Avoid mkdir where last part of path is "." or "..". */
343 if (cursor
[-1] == '.'
344 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
345 || (cursor
[-2] == '.'
346 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
349 *cursor
= '\0'; /* truncate the path there */
350 mode
= MODE_RWX
& ~ newdir_umask
;
351 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
352 status
= mkdir (file_name
, mode
^ invert_permissions
);
356 /* Create a struct delayed_set_stat even if
357 invert_permissions is zero, because
358 repair_delayed_set_stat may need to update the struct. */
359 delay_set_stat (file_name
,
360 ¤t_stat
/* ignored */,
361 invert_permissions
, INTERDIR_PERMSTATUS
);
363 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
374 /* Turbo C mkdir gives a funny errno. */
378 /* Directory already exists. */
381 /* Some other error in the mkdir. We return to the caller. */
385 return did_something
; /* tell them to retry if we made one */
388 /* Prepare to extract a file.
389 Return zero if extraction should not proceed. */
392 prepare_to_extract (char const *file_name
)
394 if (to_stdout_option
)
397 if (old_files_option
== UNLINK_FIRST_OLD_FILES
398 && !remove_any_file (file_name
, recursive_unlink_option
)
399 && errno
&& errno
!= ENOENT
)
401 unlink_error (file_name
);
408 /* Attempt repairing what went wrong with the extraction. Delete an
409 already existing file or create missing intermediate directories.
410 Return nonzero if we somewhat increased our chances at a successful
411 extraction. errno is properly restored on zero return. */
413 maybe_recoverable (char *file_name
, int *interdir_made
)
421 /* Remove an old file, if the options allow this. */
423 switch (old_files_option
)
428 case DEFAULT_OLD_FILES
:
429 case OVERWRITE_OLD_FILES
:
431 int r
= remove_any_file (file_name
, 0);
438 /* Attempt creating missing intermediate directories. */
439 if (! make_directories (file_name
))
448 /* Just say we can't do anything about it... */
455 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
459 /* assuming sizeleft is initially totalsize */
461 while (*sizeleft
> 0)
465 union block
*data_block
= find_next_block ();
468 ERROR ((0, 0, _("Unexpected EOF in archive")));
471 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
473 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
476 written
= sparsearray
[sparse_ind
++].numbytes
;
477 while (written
> BLOCKSIZE
)
479 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
482 if (count
!= BLOCKSIZE
)
484 write_error_details (name
, count
, BLOCKSIZE
);
487 set_next_block_after (data_block
);
488 data_block
= find_next_block ();
491 ERROR ((0, 0, _("Unexpected EOF in archive")));
496 count
= full_write (fd
, data_block
->buffer
, written
);
499 if (count
!= written
)
501 write_error_details (name
, count
, written
);
505 set_next_block_after (data_block
);
509 /* Fix the statuses of all directories whose statuses need fixing, and
510 which are not ancestors of FILE_NAME. */
512 apply_nonancestor_delayed_set_stat (char const *file_name
)
514 size_t file_name_len
= strlen (file_name
);
516 while (delayed_set_stat_head
)
518 struct delayed_set_stat
*data
= delayed_set_stat_head
;
519 if (data
->file_name_len
< file_name_len
520 && file_name
[data
->file_name_len
]
521 && (ISSLASH (file_name
[data
->file_name_len
])
522 || ISSLASH (file_name
[data
->file_name_len
- 1]))
523 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0)
525 delayed_set_stat_head
= data
->next
;
526 set_stat (data
->file_name
, &data
->stat_info
,
527 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
532 /* Extract a file from the archive. */
534 extract_archive (void)
536 union block
*data_block
;
547 int interdir_made
= 0;
551 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
553 set_next_block_after (current_header
);
554 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
556 if (interactive_option
&& !confirm ("extract", current_file_name
))
562 /* Print the block from current_header and current_stat. */
567 /* Check for fully specified file names and other atrocities. */
570 if (! absolute_names_option
)
572 if (contains_dot_dot (CURRENT_FILE_NAME
))
574 ERROR ((0, 0, _("%s: Member name contains `..'"),
575 quotearg_colon (CURRENT_FILE_NAME
)));
580 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
581 while (ISSLASH (CURRENT_FILE_NAME
[0]))
586 static int warned_once
;
591 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
592 (int) skipcrud
, current_file_name
));
597 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
);
599 /* Take a safety backup of a previously existing file. */
601 if (backup_option
&& !to_stdout_option
)
602 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
605 ERROR ((0, e
, _("%s: Was unable to backup this file"),
606 quotearg_colon (CURRENT_FILE_NAME
)));
611 /* Extract the archive entry according to its type. */
613 typeflag
= current_header
->header
.typeflag
;
616 /* JK - What we want to do if the file is sparse is loop through
617 the array of sparse structures in the header and read in and
618 translate the character strings representing 1) the offset at
619 which to write and 2) how many bytes to write into numbers,
620 which we store into the scratch array, "sparsearray". This
621 array makes our life easier the same way it did in creating the
622 tar file that had to deal with a sparse file.
624 After we read in the first five (at most) sparse structures, we
625 check to see if the file has an extended header, i.e., if more
626 sparse structures are needed to describe the contents of the new
627 file. If so, we read in the extended headers and continue to
628 store their contents into the sparsearray. */
633 xmalloc (sp_array_size
* sizeof (struct sp_array
));
635 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
637 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
638 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
639 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
640 if (!sparsearray
[counter
].numbytes
)
644 if (current_header
->oldgnu_header
.isextended
)
646 /* Read in the list of extended headers and translate them
647 into the sparsearray as before. Note that this
648 invalidates current_header. */
650 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
654 exhdr
= find_next_block ();
657 ERROR ((0, 0, _("Unexpected EOF in archive")));
660 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
662 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
663 if (counter
+ ind
> sp_array_size
- 1)
665 /* Realloc the scratch area since we've run out of
670 xrealloc (sparsearray
,
671 sp_array_size
* sizeof (struct sp_array
));
673 if (s
->numbytes
[0] == 0)
675 sparsearray
[counter
+ ind
].offset
=
676 OFF_FROM_HEADER (s
->offset
);
677 sparsearray
[counter
+ ind
].numbytes
=
678 SIZE_FROM_HEADER (s
->numbytes
);
680 if (!exhdr
->sparse_header
.isextended
)
684 ind
+= SPARSES_IN_SPARSE_HEADER
;
685 set_next_block_after (exhdr
);
688 set_next_block_after (exhdr
);
696 /* Appears to be a file. But BSD tar uses the convention that a slash
697 suffix means a directory. */
699 name_length
= strlen (CURRENT_FILE_NAME
);
700 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
701 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
704 /* FIXME: deal with protection issues. */
707 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
708 | (old_files_option
== OVERWRITE_OLD_FILES
711 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
713 if (to_stdout_option
)
719 if (! prepare_to_extract (CURRENT_FILE_NAME
))
728 /* Contiguous files (on the Masscomp) have to specify the size in
729 the open call that creates them. */
731 if (typeflag
== CONTTYPE
)
732 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
733 mode
, current_stat
.st_size
);
735 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
737 #else /* not O_CTG */
738 if (typeflag
== CONTTYPE
)
740 static int conttype_diagnosed
;
742 if (!conttype_diagnosed
)
744 conttype_diagnosed
= 1;
745 WARN ((0, 0, _("Extracting contiguous files as regular files")));
748 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
750 #endif /* not O_CTG */
754 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
757 open_error (CURRENT_FILE_NAME
);
765 if (typeflag
== GNUTYPE_SPARSE
)
768 size_t name_length_bis
;
770 /* Kludge alert. NAME is assigned to header.name because
771 during the extraction, the space that contains the header
772 will get scribbled on, and the name will get munged, so any
773 error messages that happen to contain the filename will look
774 REAL interesting unless we do this. */
776 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
777 name
= xmalloc (name_length_bis
);
778 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
779 size
= current_stat
.st_size
;
780 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
784 for (size
= current_stat
.st_size
; size
> 0; )
786 if (multi_volume_option
)
788 assign_string (&save_name
, current_file_name
);
789 save_totsize
= current_stat
.st_size
;
790 save_sizeleft
= size
;
793 /* Locate data, determine max length writeable, write it,
794 block that we have used the data, then check if the write
797 data_block
= find_next_block ();
800 ERROR ((0, 0, _("Unexpected EOF in archive")));
801 break; /* FIXME: What happens, then? */
804 written
= available_space_after (data_block
);
809 count
= full_write (fd
, data_block
->buffer
, written
);
812 set_next_block_after ((union block
*)
813 (data_block
->buffer
+ written
- 1));
814 if (count
!= written
)
816 write_error_details (CURRENT_FILE_NAME
, count
, written
);
823 if (multi_volume_option
)
824 assign_string (&save_name
, 0);
826 /* If writing to stdout, don't try to do anything to the filename;
827 it doesn't exist, or we don't want to touch it anyway. */
829 if (to_stdout_option
)
835 close_error (CURRENT_FILE_NAME
);
840 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
841 (old_files_option
== OVERWRITE_OLD_FILES
843 : ARCHIVED_PERMSTATUS
),
849 if (! prepare_to_extract (CURRENT_FILE_NAME
))
852 if (absolute_names_option
853 || (ISSLASH (current_link_name
854 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
855 && ! contains_dot_dot (current_link_name
)))
857 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
859 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
863 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, SYMTYPE
);
865 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
869 /* This symbolic link is potentially dangerous. Don't
870 create it now; instead, create a placeholder file, which
871 will be replaced after other extraction is done. */
874 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
876 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
881 open_error (CURRENT_FILE_NAME
);
882 else if (fstat (fd
, &st
) != 0)
884 stat_error (CURRENT_FILE_NAME
);
887 else if (close (fd
) != 0)
888 close_error (CURRENT_FILE_NAME
);
891 size_t filelen
= strlen (CURRENT_FILE_NAME
);
892 size_t linklen
= strlen (current_link_name
);
893 struct delayed_symlink
*p
=
894 xmalloc (sizeof *p
+ filelen
+ linklen
+ 1);
895 p
->next
= delayed_symlink_head
;
896 delayed_symlink_head
= p
;
899 p
->mtime
= st
.st_mtime
;
900 p
->uid
= current_stat
.st_uid
;
901 p
->gid
= current_stat
.st_gid
;
902 memcpy (p
->names
, CURRENT_FILE_NAME
, filelen
+ 1);
903 memcpy (p
->names
+ filelen
+ 1, current_link_name
, linklen
+ 1);
908 if (status
!= 0 && backup_option
)
914 static int warned_once
;
920 _("Attempting extraction of symbolic links as hard links")));
928 if (! prepare_to_extract (CURRENT_FILE_NAME
))
933 struct stat st1
, st2
;
936 /* MSDOS does not implement links. However, djgpp's link() actually
938 status
= link (current_link_name
, CURRENT_FILE_NAME
);
942 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
945 if (incremental_option
&& errno
== EEXIST
)
948 if (stat (current_link_name
, &st1
) == 0
949 && stat (CURRENT_FILE_NAME
, &st2
) == 0
950 && st1
.st_dev
== st2
.st_dev
951 && st1
.st_ino
== st2
.st_ino
)
954 ERROR ((0, e
, _("%s: Cannot link to %s"),
955 quotearg_colon (CURRENT_FILE_NAME
),
956 quote (current_link_name
)));
964 current_stat
.st_mode
|= S_IFCHR
;
970 current_stat
.st_mode
|= S_IFBLK
;
973 #if S_IFCHR || S_IFBLK
975 if (! prepare_to_extract (CURRENT_FILE_NAME
))
978 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
979 current_stat
.st_rdev
);
982 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
984 mknod_error (CURRENT_FILE_NAME
);
989 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
990 ARCHIVED_PERMSTATUS
, typeflag
);
994 #if HAVE_MKFIFO || defined mkfifo
996 if (! prepare_to_extract (CURRENT_FILE_NAME
))
999 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1001 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1005 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
1006 ARCHIVED_PERMSTATUS
, typeflag
);
1009 mkfifo_error (CURRENT_FILE_NAME
);
1011 undo_last_backup ();
1017 case GNUTYPE_DUMPDIR
:
1018 name_length
= strlen (CURRENT_FILE_NAME
);
1022 if (incremental_option
)
1024 /* Read the entry and delete files that aren't listed in the
1027 gnu_restore (skipcrud
);
1029 else if (typeflag
== GNUTYPE_DUMPDIR
)
1032 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1035 mode
= ((current_stat
.st_mode
1036 | (we_are_root
? 0 : MODE_WXUSR
))
1041 /* Do not pass redundant trailing "/" to mkdir, as POSIX does
1042 not allow mkdir to ignore it. */
1043 size_t len
= name_length
;
1045 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < len
1046 && CURRENT_FILE_NAME
[len
- 1] == '/')
1048 CURRENT_FILE_NAME
[len
] = '\0';
1049 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1050 CURRENT_FILE_NAME
[len
] = ch
;
1055 if (errno
== EEXIST
&& interdir_made
)
1058 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1060 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1066 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1069 if (errno
!= EEXIST
)
1071 mkdir_error (CURRENT_FILE_NAME
);
1073 undo_last_backup ();
1079 || old_files_option
== OVERWRITE_OLD_FILES
)
1080 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1081 mode
& ~ current_stat
.st_mode
,
1083 ? ARCHIVED_PERMSTATUS
1084 : UNKNOWN_PERMSTATUS
));
1087 case GNUTYPE_VOLHDR
:
1089 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1096 case GNUTYPE_MULTIVOL
:
1098 _("%s: Cannot extract -- file is continued from another volume"),
1099 quotearg_colon (current_file_name
)));
1102 undo_last_backup ();
1105 case GNUTYPE_LONGNAME
:
1106 case GNUTYPE_LONGLINK
:
1107 ERROR ((0, 0, _("Visible long name error")));
1110 undo_last_backup ();
1115 _("%s: Unknown file type '%c', extracted as normal file"),
1116 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1120 #undef CURRENT_FILE_NAME
1123 /* Extract the symbolic links whose final extraction were delayed. */
1125 apply_delayed_symlinks (void)
1127 struct delayed_symlink
*p
;
1128 struct delayed_symlink
*next
;
1130 for (p
= delayed_symlink_head
; p
; p
= next
)
1132 char const *file
= p
->names
;
1135 /* Before doing anything, make sure the placeholder file is still
1136 there. If the placeholder isn't there, don't worry about it, as
1137 it may have been removed by a later extraction. */
1138 if (lstat (file
, &st
) == 0
1139 && st
.st_dev
== p
->dev
1140 && st
.st_ino
== p
->ino
1141 && st
.st_mtime
== p
->mtime
)
1143 if (unlink (file
) != 0)
1144 unlink_error (file
);
1147 char const *contents
= file
+ strlen (file
) + 1;
1148 if (symlink (contents
, file
) != 0)
1149 symlink_error (contents
, file
);
1154 set_stat (file
, &st
, 0, 0, SYMTYPE
);
1163 delayed_symlink_head
= 0;
1166 /* Finish the extraction of an archive. */
1168 extract_finish (void)
1170 /* Apply delayed symlinks last, so that they don't affect
1171 delayed directory status-setting. */
1172 apply_nonancestor_delayed_set_stat ("");
1173 apply_delayed_symlinks ();
1180 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));