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;
553 struct stat
const *current_stat_info
= 0;
555 check_for_renamed_directories
|= data
->after_symlinks
;
557 if (after_symlinks
< data
->after_symlinks
558 || (data
->file_name_len
< file_name_len
559 && file_name
[data
->file_name_len
]
560 && (ISSLASH (file_name
[data
->file_name_len
])
561 || ISSLASH (file_name
[data
->file_name_len
- 1]))
562 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
565 if (check_for_renamed_directories
)
567 current_stat_info
= &st
;
568 if (stat (data
->file_name
, &st
) != 0)
570 stat_error (data
->file_name
);
573 else if (! (st
.st_dev
== data
->stat_info
.st_dev
574 && (st
.st_ino
== data
->stat_info
.st_ino
)))
577 _("%s: Directory renamed before its status could be extracted"),
578 quotearg_colon (data
->file_name
)));
584 set_stat (data
->file_name
, &data
->stat_info
, current_stat_info
,
585 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
587 delayed_set_stat_head
= data
->next
;
592 /* Extract a file from the archive. */
594 extract_archive (void)
596 union block
*data_block
;
607 int interdir_made
= 0;
611 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
613 set_next_block_after (current_header
);
614 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
616 if (interactive_option
&& !confirm ("extract", current_file_name
))
622 /* Print the block from current_header and current_stat. */
627 /* Check for fully specified file names and other atrocities. */
630 if (! absolute_names_option
)
632 if (contains_dot_dot (CURRENT_FILE_NAME
))
634 ERROR ((0, 0, _("%s: Member name contains `..'"),
635 quotearg_colon (CURRENT_FILE_NAME
)));
640 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
641 while (ISSLASH (CURRENT_FILE_NAME
[0]))
646 static int warned_once
;
651 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
652 (int) skipcrud
, current_file_name
));
657 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
, 0);
659 /* Take a safety backup of a previously existing file. */
661 if (backup_option
&& !to_stdout_option
)
662 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
665 ERROR ((0, e
, _("%s: Was unable to backup this file"),
666 quotearg_colon (CURRENT_FILE_NAME
)));
671 /* Extract the archive entry according to its type. */
673 typeflag
= current_header
->header
.typeflag
;
676 /* JK - What we want to do if the file is sparse is loop through
677 the array of sparse structures in the header and read in and
678 translate the character strings representing 1) the offset at
679 which to write and 2) how many bytes to write into numbers,
680 which we store into the scratch array, "sparsearray". This
681 array makes our life easier the same way it did in creating the
682 tar file that had to deal with a sparse file.
684 After we read in the first five (at most) sparse structures, we
685 check to see if the file has an extended header, i.e., if more
686 sparse structures are needed to describe the contents of the new
687 file. If so, we read in the extended headers and continue to
688 store their contents into the sparsearray. */
693 xmalloc (sp_array_size
* sizeof (struct sp_array
));
695 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
697 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
698 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
699 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
700 if (!sparsearray
[counter
].numbytes
)
704 if (current_header
->oldgnu_header
.isextended
)
706 /* Read in the list of extended headers and translate them
707 into the sparsearray as before. Note that this
708 invalidates current_header. */
710 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
714 exhdr
= find_next_block ();
717 ERROR ((0, 0, _("Unexpected EOF in archive")));
720 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
722 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
723 if (counter
+ ind
> sp_array_size
- 1)
725 /* Realloc the scratch area since we've run out of
730 xrealloc (sparsearray
,
731 sp_array_size
* sizeof (struct sp_array
));
733 if (s
->numbytes
[0] == 0)
735 sparsearray
[counter
+ ind
].offset
=
736 OFF_FROM_HEADER (s
->offset
);
737 sparsearray
[counter
+ ind
].numbytes
=
738 SIZE_FROM_HEADER (s
->numbytes
);
740 if (!exhdr
->sparse_header
.isextended
)
744 ind
+= SPARSES_IN_SPARSE_HEADER
;
745 set_next_block_after (exhdr
);
748 set_next_block_after (exhdr
);
756 /* Appears to be a file. But BSD tar uses the convention that a slash
757 suffix means a directory. */
759 name_length
= strlen (CURRENT_FILE_NAME
);
760 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
761 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
764 /* FIXME: deal with protection issues. */
767 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
768 | (old_files_option
== OVERWRITE_OLD_FILES
771 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
773 if (to_stdout_option
)
779 if (! prepare_to_extract (CURRENT_FILE_NAME
))
788 /* Contiguous files (on the Masscomp) have to specify the size in
789 the open call that creates them. */
791 if (typeflag
== CONTTYPE
)
792 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
793 mode
, current_stat
.st_size
);
795 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
797 #else /* not O_CTG */
798 if (typeflag
== CONTTYPE
)
800 static int conttype_diagnosed
;
802 if (!conttype_diagnosed
)
804 conttype_diagnosed
= 1;
805 WARN ((0, 0, _("Extracting contiguous files as regular files")));
808 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
810 #endif /* not O_CTG */
814 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
817 open_error (CURRENT_FILE_NAME
);
825 if (typeflag
== GNUTYPE_SPARSE
)
828 size_t name_length_bis
;
830 /* Kludge alert. NAME is assigned to header.name because
831 during the extraction, the space that contains the header
832 will get scribbled on, and the name will get munged, so any
833 error messages that happen to contain the filename will look
834 REAL interesting unless we do this. */
836 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
837 name
= xmalloc (name_length_bis
);
838 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
839 size
= current_stat
.st_size
;
840 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
844 for (size
= current_stat
.st_size
; size
> 0; )
846 if (multi_volume_option
)
848 assign_string (&save_name
, current_file_name
);
849 save_totsize
= current_stat
.st_size
;
850 save_sizeleft
= size
;
853 /* Locate data, determine max length writeable, write it,
854 block that we have used the data, then check if the write
857 data_block
= find_next_block ();
860 ERROR ((0, 0, _("Unexpected EOF in archive")));
861 break; /* FIXME: What happens, then? */
864 written
= available_space_after (data_block
);
869 count
= full_write (fd
, data_block
->buffer
, written
);
872 set_next_block_after ((union block
*)
873 (data_block
->buffer
+ written
- 1));
874 if (count
!= written
)
876 write_error_details (CURRENT_FILE_NAME
, count
, written
);
883 if (multi_volume_option
)
884 assign_string (&save_name
, 0);
886 /* If writing to stdout, don't try to do anything to the filename;
887 it doesn't exist, or we don't want to touch it anyway. */
889 if (to_stdout_option
)
895 close_error (CURRENT_FILE_NAME
);
900 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
901 (old_files_option
== OVERWRITE_OLD_FILES
903 : ARCHIVED_PERMSTATUS
),
909 if (! prepare_to_extract (CURRENT_FILE_NAME
))
912 if (absolute_names_option
913 || ! (ISSLASH (current_link_name
914 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
915 || contains_dot_dot (current_link_name
)))
917 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
919 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
923 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, 0, SYMTYPE
);
925 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
929 /* This symbolic link is potentially dangerous. Don't
930 create it now; instead, create a placeholder file, which
931 will be replaced after other extraction is done. */
934 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
936 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
941 open_error (CURRENT_FILE_NAME
);
942 else if (fstat (fd
, &st
) != 0)
944 stat_error (CURRENT_FILE_NAME
);
947 else if (close (fd
) != 0)
948 close_error (CURRENT_FILE_NAME
);
951 struct delayed_set_stat
*h
;
952 struct delayed_symlink
*p
=
953 xmalloc (offsetof (struct delayed_symlink
, target
)
954 + strlen (current_link_name
) + 1);
955 p
->next
= delayed_symlink_head
;
956 delayed_symlink_head
= p
;
959 p
->mtime
= st
.st_mtime
;
960 p
->uid
= current_stat
.st_uid
;
961 p
->gid
= current_stat
.st_gid
;
962 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
963 + strlen (CURRENT_FILE_NAME
) + 1);
964 p
->sources
->next
= 0;
965 strcpy (p
->sources
->string
, CURRENT_FILE_NAME
);
966 strcpy (p
->target
, current_link_name
);
968 h
= delayed_set_stat_head
;
969 if (h
&& ! h
->after_symlinks
970 && strncmp (CURRENT_FILE_NAME
, h
->file_name
, h
->file_name_len
) == 0
971 && ISSLASH (CURRENT_FILE_NAME
[h
->file_name_len
])
972 && (base_name (CURRENT_FILE_NAME
)
973 == CURRENT_FILE_NAME
+ h
->file_name_len
+ 1))
975 h
->after_symlinks
= 1;
977 if (stat (h
->file_name
, &st
) != 0)
978 stat_error (h
->file_name
);
981 h
->stat_info
.st_dev
= st
.st_dev
;
982 h
->stat_info
.st_ino
= st
.st_ino
;
990 if (status
!= 0 && backup_option
)
996 static int warned_once
;
1002 _("Attempting extraction of symbolic links as hard links")));
1010 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1015 struct stat st1
, st2
;
1018 /* MSDOS does not implement links. However, djgpp's link() actually
1020 status
= link (current_link_name
, CURRENT_FILE_NAME
);
1024 struct delayed_symlink
*ds
= delayed_symlink_head
;
1025 if (ds
&& stat (current_link_name
, &st1
) == 0)
1026 for (; ds
; ds
= ds
->next
)
1027 if (ds
->dev
== st1
.st_dev
1028 && ds
->ino
== st1
.st_ino
1029 && ds
->mtime
== st1
.st_mtime
)
1031 struct string_list
*p
=
1032 xmalloc (offsetof (struct string_list
, string
)
1033 + strlen (CURRENT_FILE_NAME
) + 1);
1034 strcpy (p
->string
, CURRENT_FILE_NAME
);
1035 p
->next
= ds
->sources
;
1041 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1044 if (incremental_option
&& errno
== EEXIST
)
1047 if (stat (current_link_name
, &st1
) == 0
1048 && stat (CURRENT_FILE_NAME
, &st2
) == 0
1049 && st1
.st_dev
== st2
.st_dev
1050 && st1
.st_ino
== st2
.st_ino
)
1053 link_error (current_link_name
, CURRENT_FILE_NAME
);
1055 undo_last_backup ();
1061 current_stat
.st_mode
|= S_IFCHR
;
1067 current_stat
.st_mode
|= S_IFBLK
;
1070 #if S_IFCHR || S_IFBLK
1072 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1075 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
1076 current_stat
.st_rdev
);
1079 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1081 mknod_error (CURRENT_FILE_NAME
);
1083 undo_last_backup ();
1086 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1087 ARCHIVED_PERMSTATUS
, typeflag
);
1091 #if HAVE_MKFIFO || defined mkfifo
1093 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1096 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1098 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1102 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1103 ARCHIVED_PERMSTATUS
, typeflag
);
1106 mkfifo_error (CURRENT_FILE_NAME
);
1108 undo_last_backup ();
1114 case GNUTYPE_DUMPDIR
:
1115 name_length
= strlen (CURRENT_FILE_NAME
);
1118 /* Remove any redundant trailing "/"s. */
1119 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
1120 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
1122 CURRENT_FILE_NAME
[name_length
] = '\0';
1124 if (incremental_option
)
1126 /* Read the entry and delete files that aren't listed in the
1129 gnu_restore (skipcrud
);
1131 else if (typeflag
== GNUTYPE_DUMPDIR
)
1134 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1137 mode
= ((current_stat
.st_mode
1138 | (we_are_root
? 0 : MODE_WXUSR
))
1142 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1147 && (interdir_made
|| old_files_option
== OVERWRITE_OLD_FILES
))
1150 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1154 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1157 if (S_ISDIR (st
.st_mode
))
1159 mode
= st
.st_mode
& ~ current_umask
;
1160 goto directory_exists
;
1166 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1169 if (errno
!= EEXIST
)
1171 mkdir_error (CURRENT_FILE_NAME
);
1173 undo_last_backup ();
1180 || old_files_option
== OVERWRITE_OLD_FILES
)
1181 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1182 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1184 ? ARCHIVED_PERMSTATUS
1185 : UNKNOWN_PERMSTATUS
));
1188 case GNUTYPE_VOLHDR
:
1190 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1197 case GNUTYPE_MULTIVOL
:
1199 _("%s: Cannot extract -- file is continued from another volume"),
1200 quotearg_colon (current_file_name
)));
1203 undo_last_backup ();
1206 case GNUTYPE_LONGNAME
:
1207 case GNUTYPE_LONGLINK
:
1208 ERROR ((0, 0, _("Visible long name error")));
1211 undo_last_backup ();
1216 _("%s: Unknown file type '%c', extracted as normal file"),
1217 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1221 #undef CURRENT_FILE_NAME
1224 /* Extract the symbolic links whose final extraction were delayed. */
1226 apply_delayed_symlinks (void)
1228 struct delayed_symlink
*ds
;
1230 for (ds
= delayed_symlink_head
; ds
; )
1232 struct string_list
*sources
= ds
->sources
;
1233 char const *valid_source
= 0;
1235 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1237 char const *source
= sources
->string
;
1240 /* Make sure the placeholder file is still there. If not,
1241 don't create a symlink, as the placeholder was probably
1242 removed by a later extraction. */
1243 if (lstat (source
, &st
) == 0
1244 && st
.st_dev
== ds
->dev
1245 && st
.st_ino
== ds
->ino
1246 && st
.st_mtime
== ds
->mtime
)
1248 /* Unlink the placeholder, then create a hard link if possible,
1249 a symbolic link otherwise. */
1250 if (unlink (source
) != 0)
1251 unlink_error (source
);
1252 else if (valid_source
&& link (valid_source
, source
) == 0)
1254 else if (symlink (ds
->target
, source
) != 0)
1255 symlink_error (ds
->target
, source
);
1258 valid_source
= source
;
1259 st
.st_uid
= ds
->uid
;
1260 st
.st_gid
= ds
->gid
;
1261 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1266 for (sources
= ds
->sources
; sources
; )
1268 struct string_list
*next
= sources
->next
;
1274 struct delayed_symlink
*next
= ds
->next
;
1280 delayed_symlink_head
= 0;
1283 /* Finish the extraction of an archive. */
1285 extract_finish (void)
1287 /* First, fix the status of ordinary directories that need fixing. */
1288 apply_nonancestor_delayed_set_stat ("", 0);
1290 /* Then, apply delayed symlinks, so that they don't affect delayed
1291 directory status-setting for ordinary directories. */
1292 apply_delayed_symlinks ();
1294 /* Finally, fix the status of directories that are ancestors
1295 of delayed symlinks. */
1296 apply_nonancestor_delayed_set_stat ("", 1);
1303 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));