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. 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
)
192 if (start_time
< t
&& (now
= time (0)) < t
)
193 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
194 file_name
, tartime (t
), (unsigned long) (t
- now
)));
197 /* Restore stat attributes (owner, group, mode and times) for
198 FILE_NAME, using information given in *STAT_INFO.
199 If CURRENT_STAT_INFO is nonzero, *CURRENT_STAT_INFO is the
200 file's currernt status.
201 If not restoring permissions, invert the
202 INVERT_PERMISSIONS bits from the file's current permissions.
203 PERMSTATUS specifies the status of the file's permissions.
204 TYPEFLAG specifies the type of the file. */
206 /* FIXME: About proper restoration of symbolic link attributes, we still do
207 not have it right. Pretesters' reports tell us we need further study and
208 probably more configuration. For now, just use lchown if it exists, and
209 punt for the rest. Sigh! */
212 set_stat (char const *file_name
, struct stat
const *stat_info
,
213 struct stat
const *current_stat_info
,
214 mode_t invert_permissions
, enum permstatus permstatus
,
217 struct utimbuf utimbuf
;
219 if (typeflag
!= SYMTYPE
)
221 /* We do the utime before the chmod because some versions of utime are
222 broken and trash the modes of the file. */
224 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
226 /* We set the accessed time to `now', which is really the time we
227 started extracting files, unless incremental_option is used, in
228 which case .st_atime is used. */
230 /* FIXME: incremental_option should set ctime too, but how? */
232 if (incremental_option
)
233 utimbuf
.actime
= stat_info
->st_atime
;
235 utimbuf
.actime
= start_time
;
237 utimbuf
.modtime
= stat_info
->st_mtime
;
239 if (utime (file_name
, &utimbuf
) < 0)
240 utime_error (file_name
);
243 check_time (file_name
, stat_info
->st_atime
);
244 check_time (file_name
, stat_info
->st_mtime
);
248 /* Some systems allow non-root users to give files away. Once this
249 done, it is not possible anymore to change file permissions, so we
250 have to set permissions prior to possibly giving files away. */
252 set_mode (file_name
, stat_info
, current_stat_info
,
253 invert_permissions
, permstatus
, typeflag
);
256 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
258 /* When lchown exists, it should be used to change the attributes of
259 the symbolic link itself. In this case, a mere chown would change
260 the attributes of the file the symbolic link is pointing to, and
261 should be avoided. */
263 if (typeflag
== SYMTYPE
)
266 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
267 chown_error_details (file_name
,
268 stat_info
->st_uid
, stat_info
->st_gid
);
273 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
274 chown_error_details (file_name
,
275 stat_info
->st_uid
, stat_info
->st_gid
);
277 /* On a few systems, and in particular, those allowing to give files
278 away, changing the owner or group destroys the suid or sgid bits.
279 So let's attempt setting these bits once more. */
280 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
281 set_mode (file_name
, stat_info
, 0,
282 invert_permissions
, permstatus
, typeflag
);
287 /* Remember to restore stat attributes (owner, group, mode and times)
288 for the directory FILE_NAME, using information given in *STAT_INFO,
289 once we stop extracting files into that directory.
290 If not restoring permissions, remember to invert the
291 INVERT_PERMISSIONS bits from the file's current permissions.
292 PERMSTATUS specifies the status of the file's permissions. */
294 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
295 mode_t invert_permissions
, enum permstatus permstatus
)
297 size_t file_name_len
= strlen (file_name
);
298 struct delayed_set_stat
*data
=
299 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
300 + file_name_len
+ 1);
301 data
->file_name_len
= file_name_len
;
302 strcpy (data
->file_name
, file_name
);
303 data
->invert_permissions
= invert_permissions
;
304 data
->permstatus
= permstatus
;
305 data
->after_symlinks
= 0;
306 data
->stat_info
= *stat_info
;
307 data
->next
= delayed_set_stat_head
;
308 delayed_set_stat_head
= data
;
311 /* Update the delayed_set_stat info for an intermediate directory
312 created on the path to DIR_NAME. The intermediate directory turned
313 out to be the same as this directory, e.g. due to ".." or symbolic
314 links. *DIR_STAT_INFO is the status of the directory. */
316 repair_delayed_set_stat (char const *dir_name
,
317 struct stat
const *dir_stat_info
)
319 struct delayed_set_stat
*data
;
320 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
323 if (stat (data
->file_name
, &st
) != 0)
325 stat_error (data
->file_name
);
329 if (st
.st_dev
== dir_stat_info
->st_dev
330 && st
.st_ino
== dir_stat_info
->st_ino
)
332 data
->stat_info
= current_stat
;
333 data
->invert_permissions
= (MODE_RWX
334 & (current_stat
.st_mode
^ st
.st_mode
));
335 data
->permstatus
= ARCHIVED_PERMSTATUS
;
340 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
341 quotearg_colon (dir_name
)));
344 /* After a file/link/symlink/directory creation has failed, see if
345 it's because some required directory was not present, and if so,
346 create all required directories. Return non-zero if a directory
349 make_directories (char *file_name
)
351 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
352 char *cursor
; /* points into path */
353 int did_something
= 0; /* did we do anything yet? */
355 int invert_permissions
;
359 for (cursor
= cursor0
; *cursor
; cursor
++)
361 if (! ISSLASH (*cursor
))
364 /* Avoid mkdir of empty string, if leading or double '/'. */
366 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
369 /* Avoid mkdir where last part of path is "." or "..". */
371 if (cursor
[-1] == '.'
372 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
373 || (cursor
[-2] == '.'
374 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
377 *cursor
= '\0'; /* truncate the path there */
378 mode
= MODE_RWX
& ~ newdir_umask
;
379 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
380 status
= mkdir (file_name
, mode
^ invert_permissions
);
384 /* Create a struct delayed_set_stat even if
385 invert_permissions is zero, because
386 repair_delayed_set_stat may need to update the struct. */
387 delay_set_stat (file_name
,
388 ¤t_stat
/* ignored */,
389 invert_permissions
, INTERDIR_PERMSTATUS
);
391 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
402 /* Turbo C mkdir gives a funny errno. */
406 /* Directory already exists. */
409 /* Some other error in the mkdir. We return to the caller. */
413 return did_something
; /* tell them to retry if we made one */
416 /* Prepare to extract a file.
417 Return zero if extraction should not proceed. */
420 prepare_to_extract (char const *file_name
)
422 if (to_stdout_option
)
425 if (old_files_option
== UNLINK_FIRST_OLD_FILES
426 && !remove_any_file (file_name
, recursive_unlink_option
)
427 && errno
&& errno
!= ENOENT
)
429 unlink_error (file_name
);
436 /* Attempt repairing what went wrong with the extraction. Delete an
437 already existing file or create missing intermediate directories.
438 Return nonzero if we somewhat increased our chances at a successful
439 extraction. errno is properly restored on zero return. */
441 maybe_recoverable (char *file_name
, int *interdir_made
)
449 /* Remove an old file, if the options allow this. */
451 switch (old_files_option
)
456 case DEFAULT_OLD_FILES
:
457 case OVERWRITE_OLD_FILES
:
459 int r
= remove_any_file (file_name
, 0);
466 /* Attempt creating missing intermediate directories. */
467 if (! make_directories (file_name
))
476 /* Just say we can't do anything about it... */
483 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
487 /* assuming sizeleft is initially totalsize */
489 while (*sizeleft
> 0)
493 union block
*data_block
= find_next_block ();
496 ERROR ((0, 0, _("Unexpected EOF in archive")));
499 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
501 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
504 written
= sparsearray
[sparse_ind
++].numbytes
;
505 while (written
> BLOCKSIZE
)
507 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
510 if (count
!= BLOCKSIZE
)
512 write_error_details (name
, count
, BLOCKSIZE
);
515 set_next_block_after (data_block
);
516 data_block
= find_next_block ();
519 ERROR ((0, 0, _("Unexpected EOF in archive")));
524 count
= full_write (fd
, data_block
->buffer
, written
);
527 if (count
!= written
)
529 write_error_details (name
, count
, written
);
533 set_next_block_after (data_block
);
537 /* Fix the statuses of all directories whose statuses need fixing, and
538 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
539 nonzero, do this for all such directories; otherwise, stop at the
540 first directory that is marked to be fixed up only after delayed
541 symlinks are applied. */
543 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
545 size_t file_name_len
= strlen (file_name
);
546 bool check_for_renamed_directories
= 0;
548 while (delayed_set_stat_head
)
550 struct delayed_set_stat
*data
= delayed_set_stat_head
;
551 bool skip_this_one
= 0;
552 check_for_renamed_directories
|= data
->after_symlinks
;
554 struct stat
const *current_stat_info
= 0;
556 if (after_symlinks
< data
->after_symlinks
557 || (data
->file_name_len
< file_name_len
558 && file_name
[data
->file_name_len
]
559 && (ISSLASH (file_name
[data
->file_name_len
])
560 || ISSLASH (file_name
[data
->file_name_len
- 1]))
561 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
564 if (check_for_renamed_directories
)
566 current_stat_info
= &st
;
567 if (stat (data
->file_name
, &st
) != 0)
569 stat_error (data
->file_name
);
572 else if (! (st
.st_dev
== data
->stat_info
.st_dev
573 && (st
.st_ino
== data
->stat_info
.st_ino
)))
576 _("%s: Directory renamed before its status could be extracted"),
577 quotearg_colon (data
->file_name
)));
583 set_stat (data
->file_name
, &data
->stat_info
, current_stat_info
,
584 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
586 delayed_set_stat_head
= data
->next
;
591 /* Extract a file from the archive. */
593 extract_archive (void)
595 union block
*data_block
;
606 int interdir_made
= 0;
610 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
612 set_next_block_after (current_header
);
613 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
615 if (interactive_option
&& !confirm ("extract", current_file_name
))
621 /* Print the block from current_header and current_stat. */
626 /* Check for fully specified file names and other atrocities. */
629 if (! absolute_names_option
)
631 if (contains_dot_dot (CURRENT_FILE_NAME
))
633 ERROR ((0, 0, _("%s: Member name contains `..'"),
634 quotearg_colon (CURRENT_FILE_NAME
)));
639 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
640 while (ISSLASH (CURRENT_FILE_NAME
[0]))
645 static int warned_once
;
650 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
651 (int) skipcrud
, current_file_name
));
656 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
, 0);
658 /* Take a safety backup of a previously existing file. */
660 if (backup_option
&& !to_stdout_option
)
661 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
664 ERROR ((0, e
, _("%s: Was unable to backup this file"),
665 quotearg_colon (CURRENT_FILE_NAME
)));
670 /* Extract the archive entry according to its type. */
672 typeflag
= current_header
->header
.typeflag
;
675 /* JK - What we want to do if the file is sparse is loop through
676 the array of sparse structures in the header and read in and
677 translate the character strings representing 1) the offset at
678 which to write and 2) how many bytes to write into numbers,
679 which we store into the scratch array, "sparsearray". This
680 array makes our life easier the same way it did in creating the
681 tar file that had to deal with a sparse file.
683 After we read in the first five (at most) sparse structures, we
684 check to see if the file has an extended header, i.e., if more
685 sparse structures are needed to describe the contents of the new
686 file. If so, we read in the extended headers and continue to
687 store their contents into the sparsearray. */
692 xmalloc (sp_array_size
* sizeof (struct sp_array
));
694 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
696 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
697 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
698 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
699 if (!sparsearray
[counter
].numbytes
)
703 if (current_header
->oldgnu_header
.isextended
)
705 /* Read in the list of extended headers and translate them
706 into the sparsearray as before. Note that this
707 invalidates current_header. */
709 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
713 exhdr
= find_next_block ();
716 ERROR ((0, 0, _("Unexpected EOF in archive")));
719 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
721 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
722 if (counter
+ ind
> sp_array_size
- 1)
724 /* Realloc the scratch area since we've run out of
729 xrealloc (sparsearray
,
730 sp_array_size
* sizeof (struct sp_array
));
732 if (s
->numbytes
[0] == 0)
734 sparsearray
[counter
+ ind
].offset
=
735 OFF_FROM_HEADER (s
->offset
);
736 sparsearray
[counter
+ ind
].numbytes
=
737 SIZE_FROM_HEADER (s
->numbytes
);
739 if (!exhdr
->sparse_header
.isextended
)
743 ind
+= SPARSES_IN_SPARSE_HEADER
;
744 set_next_block_after (exhdr
);
747 set_next_block_after (exhdr
);
755 /* Appears to be a file. But BSD tar uses the convention that a slash
756 suffix means a directory. */
758 name_length
= strlen (CURRENT_FILE_NAME
);
759 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
760 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
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
.st_mode
& MODE_RWX
& ~ current_umask
;
772 if (to_stdout_option
)
778 if (! prepare_to_extract (CURRENT_FILE_NAME
))
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 (CURRENT_FILE_NAME
, openflag
| O_CTG
,
792 mode
, current_stat
.st_size
);
794 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
796 #else /* not O_CTG */
797 if (typeflag
== CONTTYPE
)
799 static int conttype_diagnosed
;
801 if (!conttype_diagnosed
)
803 conttype_diagnosed
= 1;
804 WARN ((0, 0, _("Extracting contiguous files as regular files")));
807 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
809 #endif /* not O_CTG */
813 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
816 open_error (CURRENT_FILE_NAME
);
824 if (typeflag
== GNUTYPE_SPARSE
)
827 size_t name_length_bis
;
829 /* Kludge alert. NAME is assigned to header.name because
830 during the extraction, the space that contains the header
831 will get scribbled on, and the name will get munged, so any
832 error messages that happen to contain the filename will look
833 REAL interesting unless we do this. */
835 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
836 name
= xmalloc (name_length_bis
);
837 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
838 size
= current_stat
.st_size
;
839 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
843 for (size
= current_stat
.st_size
; size
> 0; )
845 if (multi_volume_option
)
847 assign_string (&save_name
, current_file_name
);
848 save_totsize
= current_stat
.st_size
;
849 save_sizeleft
= size
;
852 /* Locate data, determine max length writeable, write it,
853 block that we have used the data, then check if the write
856 data_block
= find_next_block ();
859 ERROR ((0, 0, _("Unexpected EOF in archive")));
860 break; /* FIXME: What happens, then? */
863 written
= available_space_after (data_block
);
868 count
= full_write (fd
, data_block
->buffer
, written
);
871 set_next_block_after ((union block
*)
872 (data_block
->buffer
+ written
- 1));
873 if (count
!= written
)
875 write_error_details (CURRENT_FILE_NAME
, count
, written
);
882 if (multi_volume_option
)
883 assign_string (&save_name
, 0);
885 /* If writing to stdout, don't try to do anything to the filename;
886 it doesn't exist, or we don't want to touch it anyway. */
888 if (to_stdout_option
)
894 close_error (CURRENT_FILE_NAME
);
899 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
900 (old_files_option
== OVERWRITE_OLD_FILES
902 : ARCHIVED_PERMSTATUS
),
908 if (! prepare_to_extract (CURRENT_FILE_NAME
))
911 if (absolute_names_option
912 || ! (ISSLASH (current_link_name
913 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
914 || contains_dot_dot (current_link_name
)))
916 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
918 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
922 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, 0, SYMTYPE
);
924 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
928 /* This symbolic link is potentially dangerous. Don't
929 create it now; instead, create a placeholder file, which
930 will be replaced after other extraction is done. */
933 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
935 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
940 open_error (CURRENT_FILE_NAME
);
941 else if (fstat (fd
, &st
) != 0)
943 stat_error (CURRENT_FILE_NAME
);
946 else if (close (fd
) != 0)
947 close_error (CURRENT_FILE_NAME
);
950 struct delayed_set_stat
*h
;
951 struct delayed_symlink
*p
=
952 xmalloc (offsetof (struct delayed_symlink
, target
)
953 + strlen (current_link_name
) + 1);
954 p
->next
= delayed_symlink_head
;
955 delayed_symlink_head
= p
;
958 p
->mtime
= st
.st_mtime
;
959 p
->uid
= current_stat
.st_uid
;
960 p
->gid
= current_stat
.st_gid
;
961 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
962 + strlen (CURRENT_FILE_NAME
) + 1);
963 p
->sources
->next
= 0;
964 strcpy (p
->sources
->string
, CURRENT_FILE_NAME
);
965 strcpy (p
->target
, current_link_name
);
967 h
= delayed_set_stat_head
;
968 if (h
&& ! h
->after_symlinks
969 && strncmp (CURRENT_FILE_NAME
, h
->file_name
, h
->file_name_len
) == 0
970 && ISSLASH (CURRENT_FILE_NAME
[h
->file_name_len
])
971 && (base_name (CURRENT_FILE_NAME
)
972 == CURRENT_FILE_NAME
+ h
->file_name_len
+ 1))
974 h
->after_symlinks
= 1;
976 if (stat (h
->file_name
, &st
) != 0)
977 stat_error (h
->file_name
);
980 h
->stat_info
.st_dev
= st
.st_dev
;
981 h
->stat_info
.st_ino
= st
.st_ino
;
989 if (status
!= 0 && backup_option
)
995 static int warned_once
;
1001 _("Attempting extraction of symbolic links as hard links")));
1009 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1014 struct stat st1
, st2
;
1017 /* MSDOS does not implement links. However, djgpp's link() actually
1019 status
= link (current_link_name
, CURRENT_FILE_NAME
);
1023 struct delayed_symlink
*ds
= delayed_symlink_head
;
1024 if (ds
&& stat (current_link_name
, &st1
) == 0)
1025 for (; ds
; ds
= ds
->next
)
1026 if (ds
->dev
== st1
.st_dev
1027 && ds
->ino
== st1
.st_ino
1028 && ds
->mtime
== st1
.st_mtime
)
1030 struct string_list
*p
=
1031 xmalloc (offsetof (struct string_list
, string
)
1032 + strlen (CURRENT_FILE_NAME
) + 1);
1033 strcpy (p
->string
, CURRENT_FILE_NAME
);
1034 p
->next
= ds
->sources
;
1040 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1043 if (incremental_option
&& errno
== EEXIST
)
1046 if (stat (current_link_name
, &st1
) == 0
1047 && stat (CURRENT_FILE_NAME
, &st2
) == 0
1048 && st1
.st_dev
== st2
.st_dev
1049 && st1
.st_ino
== st2
.st_ino
)
1052 link_error (current_link_name
, CURRENT_FILE_NAME
);
1054 undo_last_backup ();
1060 current_stat
.st_mode
|= S_IFCHR
;
1066 current_stat
.st_mode
|= S_IFBLK
;
1069 #if S_IFCHR || S_IFBLK
1071 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1074 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
1075 current_stat
.st_rdev
);
1078 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1080 mknod_error (CURRENT_FILE_NAME
);
1082 undo_last_backup ();
1085 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1086 ARCHIVED_PERMSTATUS
, typeflag
);
1090 #if HAVE_MKFIFO || defined mkfifo
1092 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1095 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1097 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1101 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1102 ARCHIVED_PERMSTATUS
, typeflag
);
1105 mkfifo_error (CURRENT_FILE_NAME
);
1107 undo_last_backup ();
1113 case GNUTYPE_DUMPDIR
:
1114 name_length
= strlen (CURRENT_FILE_NAME
);
1117 /* Remove any redundant trailing "/"s. */
1118 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
1119 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
1121 CURRENT_FILE_NAME
[name_length
] = '\0';
1123 if (incremental_option
)
1125 /* Read the entry and delete files that aren't listed in the
1128 gnu_restore (skipcrud
);
1130 else if (typeflag
== GNUTYPE_DUMPDIR
)
1133 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1136 mode
= ((current_stat
.st_mode
1137 | (we_are_root
? 0 : MODE_WXUSR
))
1141 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1146 && (interdir_made
|| old_files_option
== OVERWRITE_OLD_FILES
))
1149 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1153 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1156 if (S_ISDIR (st
.st_mode
))
1158 mode
= st
.st_mode
& ~ current_umask
;
1159 goto directory_exists
;
1165 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1168 if (errno
!= EEXIST
)
1170 mkdir_error (CURRENT_FILE_NAME
);
1172 undo_last_backup ();
1179 || old_files_option
== OVERWRITE_OLD_FILES
)
1180 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1181 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1183 ? ARCHIVED_PERMSTATUS
1184 : UNKNOWN_PERMSTATUS
));
1187 case GNUTYPE_VOLHDR
:
1189 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1196 case GNUTYPE_MULTIVOL
:
1198 _("%s: Cannot extract -- file is continued from another volume"),
1199 quotearg_colon (current_file_name
)));
1202 undo_last_backup ();
1205 case GNUTYPE_LONGNAME
:
1206 case GNUTYPE_LONGLINK
:
1207 ERROR ((0, 0, _("Visible long name error")));
1210 undo_last_backup ();
1215 _("%s: Unknown file type '%c', extracted as normal file"),
1216 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1220 #undef CURRENT_FILE_NAME
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"));