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_DIRS
:
458 case OVERWRITE_OLD_FILES
:
460 int r
= remove_any_file (file_name
, 0);
467 /* Attempt creating missing intermediate directories. */
468 if (! make_directories (file_name
))
477 /* Just say we can't do anything about it... */
484 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
488 /* assuming sizeleft is initially totalsize */
490 while (*sizeleft
> 0)
494 union block
*data_block
= find_next_block ();
497 ERROR ((0, 0, _("Unexpected EOF in archive")));
500 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
502 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
505 written
= sparsearray
[sparse_ind
++].numbytes
;
506 while (written
> BLOCKSIZE
)
508 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
511 if (count
!= BLOCKSIZE
)
513 write_error_details (name
, count
, BLOCKSIZE
);
516 set_next_block_after (data_block
);
517 data_block
= find_next_block ();
520 ERROR ((0, 0, _("Unexpected EOF in archive")));
525 count
= full_write (fd
, data_block
->buffer
, written
);
528 if (count
!= written
)
530 write_error_details (name
, count
, written
);
534 set_next_block_after (data_block
);
538 /* Fix the statuses of all directories whose statuses need fixing, and
539 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
540 nonzero, do this for all such directories; otherwise, stop at the
541 first directory that is marked to be fixed up only after delayed
542 symlinks are applied. */
544 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
546 size_t file_name_len
= strlen (file_name
);
547 bool check_for_renamed_directories
= 0;
549 while (delayed_set_stat_head
)
551 struct delayed_set_stat
*data
= delayed_set_stat_head
;
552 bool skip_this_one
= 0;
554 struct stat
const *current_stat_info
= 0;
556 check_for_renamed_directories
|= data
->after_symlinks
;
558 if (after_symlinks
< data
->after_symlinks
559 || (data
->file_name_len
< file_name_len
560 && file_name
[data
->file_name_len
]
561 && (ISSLASH (file_name
[data
->file_name_len
])
562 || ISSLASH (file_name
[data
->file_name_len
- 1]))
563 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
566 if (check_for_renamed_directories
)
568 current_stat_info
= &st
;
569 if (stat (data
->file_name
, &st
) != 0)
571 stat_error (data
->file_name
);
574 else if (! (st
.st_dev
== data
->stat_info
.st_dev
575 && (st
.st_ino
== data
->stat_info
.st_ino
)))
578 _("%s: Directory renamed before its status could be extracted"),
579 quotearg_colon (data
->file_name
)));
585 set_stat (data
->file_name
, &data
->stat_info
, current_stat_info
,
586 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
588 delayed_set_stat_head
= data
->next
;
593 /* Extract a file from the archive. */
595 extract_archive (void)
597 union block
*data_block
;
608 int interdir_made
= 0;
612 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
614 set_next_block_after (current_header
);
615 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
617 if (interactive_option
&& !confirm ("extract", current_file_name
))
623 /* Print the block from current_header and current_stat. */
628 /* Check for fully specified file names and other atrocities. */
631 if (! absolute_names_option
)
633 if (contains_dot_dot (CURRENT_FILE_NAME
))
635 ERROR ((0, 0, _("%s: Member name contains `..'"),
636 quotearg_colon (CURRENT_FILE_NAME
)));
641 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
642 while (ISSLASH (CURRENT_FILE_NAME
[0]))
647 static int warned_once
;
652 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
653 (int) skipcrud
, current_file_name
));
658 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
, 0);
660 /* Take a safety backup of a previously existing file. */
662 if (backup_option
&& !to_stdout_option
)
663 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
666 ERROR ((0, e
, _("%s: Was unable to backup this file"),
667 quotearg_colon (CURRENT_FILE_NAME
)));
672 /* Extract the archive entry according to its type. */
674 typeflag
= current_header
->header
.typeflag
;
677 /* JK - What we want to do if the file is sparse is loop through
678 the array of sparse structures in the header and read in and
679 translate the character strings representing 1) the offset at
680 which to write and 2) how many bytes to write into numbers,
681 which we store into the scratch array, "sparsearray". This
682 array makes our life easier the same way it did in creating the
683 tar file that had to deal with a sparse file.
685 After we read in the first five (at most) sparse structures, we
686 check to see if the file has an extended header, i.e., if more
687 sparse structures are needed to describe the contents of the new
688 file. If so, we read in the extended headers and continue to
689 store their contents into the sparsearray. */
694 xmalloc (sp_array_size
* sizeof (struct sp_array
));
696 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
698 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
699 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
700 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
701 if (!sparsearray
[counter
].numbytes
)
705 if (current_header
->oldgnu_header
.isextended
)
707 /* Read in the list of extended headers and translate them
708 into the sparsearray as before. Note that this
709 invalidates current_header. */
711 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
715 exhdr
= find_next_block ();
718 ERROR ((0, 0, _("Unexpected EOF in archive")));
721 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
723 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
724 if (counter
+ ind
> sp_array_size
- 1)
726 /* Realloc the scratch area since we've run out of
731 xrealloc (sparsearray
,
732 sp_array_size
* sizeof (struct sp_array
));
734 if (s
->numbytes
[0] == 0)
736 sparsearray
[counter
+ ind
].offset
=
737 OFF_FROM_HEADER (s
->offset
);
738 sparsearray
[counter
+ ind
].numbytes
=
739 SIZE_FROM_HEADER (s
->numbytes
);
741 if (!exhdr
->sparse_header
.isextended
)
745 ind
+= SPARSES_IN_SPARSE_HEADER
;
746 set_next_block_after (exhdr
);
749 set_next_block_after (exhdr
);
757 /* Appears to be a file. But BSD tar uses the convention that a slash
758 suffix means a directory. */
760 name_length
= strlen (CURRENT_FILE_NAME
);
761 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
762 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
765 /* FIXME: deal with protection issues. */
768 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
769 | (old_files_option
== OVERWRITE_OLD_FILES
772 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
774 if (to_stdout_option
)
780 if (! prepare_to_extract (CURRENT_FILE_NAME
))
789 /* Contiguous files (on the Masscomp) have to specify the size in
790 the open call that creates them. */
792 if (typeflag
== CONTTYPE
)
793 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
794 mode
, current_stat
.st_size
);
796 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
798 #else /* not O_CTG */
799 if (typeflag
== CONTTYPE
)
801 static int conttype_diagnosed
;
803 if (!conttype_diagnosed
)
805 conttype_diagnosed
= 1;
806 WARN ((0, 0, _("Extracting contiguous files as regular files")));
809 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
811 #endif /* not O_CTG */
815 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
818 open_error (CURRENT_FILE_NAME
);
826 if (typeflag
== GNUTYPE_SPARSE
)
829 size_t name_length_bis
;
831 /* Kludge alert. NAME is assigned to header.name because
832 during the extraction, the space that contains the header
833 will get scribbled on, and the name will get munged, so any
834 error messages that happen to contain the filename will look
835 REAL interesting unless we do this. */
837 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
838 name
= xmalloc (name_length_bis
);
839 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
840 size
= current_stat
.st_size
;
841 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
845 for (size
= current_stat
.st_size
; size
> 0; )
847 if (multi_volume_option
)
849 assign_string (&save_name
, current_file_name
);
850 save_totsize
= current_stat
.st_size
;
851 save_sizeleft
= size
;
854 /* Locate data, determine max length writeable, write it,
855 block that we have used the data, then check if the write
858 data_block
= find_next_block ();
861 ERROR ((0, 0, _("Unexpected EOF in archive")));
862 break; /* FIXME: What happens, then? */
865 written
= available_space_after (data_block
);
870 count
= full_write (fd
, data_block
->buffer
, written
);
873 set_next_block_after ((union block
*)
874 (data_block
->buffer
+ written
- 1));
875 if (count
!= written
)
877 write_error_details (CURRENT_FILE_NAME
, count
, written
);
884 if (multi_volume_option
)
885 assign_string (&save_name
, 0);
887 /* If writing to stdout, don't try to do anything to the filename;
888 it doesn't exist, or we don't want to touch it anyway. */
890 if (to_stdout_option
)
896 close_error (CURRENT_FILE_NAME
);
901 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
902 (old_files_option
== OVERWRITE_OLD_FILES
904 : ARCHIVED_PERMSTATUS
),
910 if (! prepare_to_extract (CURRENT_FILE_NAME
))
913 if (absolute_names_option
914 || ! (ISSLASH (current_link_name
915 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
916 || contains_dot_dot (current_link_name
)))
918 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
920 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
924 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, 0, SYMTYPE
);
926 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
930 /* This symbolic link is potentially dangerous. Don't
931 create it now; instead, create a placeholder file, which
932 will be replaced after other extraction is done. */
935 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
937 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
942 open_error (CURRENT_FILE_NAME
);
943 else if (fstat (fd
, &st
) != 0)
945 stat_error (CURRENT_FILE_NAME
);
948 else if (close (fd
) != 0)
949 close_error (CURRENT_FILE_NAME
);
952 struct delayed_set_stat
*h
;
953 struct delayed_symlink
*p
=
954 xmalloc (offsetof (struct delayed_symlink
, target
)
955 + strlen (current_link_name
) + 1);
956 p
->next
= delayed_symlink_head
;
957 delayed_symlink_head
= p
;
960 p
->mtime
= st
.st_mtime
;
961 p
->uid
= current_stat
.st_uid
;
962 p
->gid
= current_stat
.st_gid
;
963 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
964 + strlen (CURRENT_FILE_NAME
) + 1);
965 p
->sources
->next
= 0;
966 strcpy (p
->sources
->string
, CURRENT_FILE_NAME
);
967 strcpy (p
->target
, current_link_name
);
969 h
= delayed_set_stat_head
;
970 if (h
&& ! h
->after_symlinks
971 && strncmp (CURRENT_FILE_NAME
, h
->file_name
, h
->file_name_len
) == 0
972 && ISSLASH (CURRENT_FILE_NAME
[h
->file_name_len
])
973 && (base_name (CURRENT_FILE_NAME
)
974 == CURRENT_FILE_NAME
+ h
->file_name_len
+ 1))
978 h
->after_symlinks
= 1;
980 if (stat (h
->file_name
, &st
) != 0)
981 stat_error (h
->file_name
);
984 h
->stat_info
.st_dev
= st
.st_dev
;
985 h
->stat_info
.st_ino
= st
.st_ino
;
988 while ((h
= h
->next
) && ! h
->after_symlinks
);
995 if (status
!= 0 && backup_option
)
1001 static int warned_once
;
1007 _("Attempting extraction of symbolic links as hard links")));
1015 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1020 struct stat st1
, st2
;
1023 /* MSDOS does not implement links. However, djgpp's link() actually
1025 status
= link (current_link_name
, CURRENT_FILE_NAME
);
1029 struct delayed_symlink
*ds
= delayed_symlink_head
;
1030 if (ds
&& stat (current_link_name
, &st1
) == 0)
1031 for (; ds
; ds
= ds
->next
)
1032 if (ds
->dev
== st1
.st_dev
1033 && ds
->ino
== st1
.st_ino
1034 && ds
->mtime
== st1
.st_mtime
)
1036 struct string_list
*p
=
1037 xmalloc (offsetof (struct string_list
, string
)
1038 + strlen (CURRENT_FILE_NAME
) + 1);
1039 strcpy (p
->string
, CURRENT_FILE_NAME
);
1040 p
->next
= ds
->sources
;
1046 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1049 if (incremental_option
&& errno
== EEXIST
)
1052 if (stat (current_link_name
, &st1
) == 0
1053 && stat (CURRENT_FILE_NAME
, &st2
) == 0
1054 && st1
.st_dev
== st2
.st_dev
1055 && st1
.st_ino
== st2
.st_ino
)
1058 link_error (current_link_name
, CURRENT_FILE_NAME
);
1060 undo_last_backup ();
1066 current_stat
.st_mode
|= S_IFCHR
;
1072 current_stat
.st_mode
|= S_IFBLK
;
1075 #if S_IFCHR || S_IFBLK
1077 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1080 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
1081 current_stat
.st_rdev
);
1084 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1086 mknod_error (CURRENT_FILE_NAME
);
1088 undo_last_backup ();
1091 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1092 ARCHIVED_PERMSTATUS
, typeflag
);
1096 #if HAVE_MKFIFO || defined mkfifo
1098 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1101 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1103 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1107 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0,
1108 ARCHIVED_PERMSTATUS
, typeflag
);
1111 mkfifo_error (CURRENT_FILE_NAME
);
1113 undo_last_backup ();
1119 case GNUTYPE_DUMPDIR
:
1120 name_length
= strlen (CURRENT_FILE_NAME
);
1123 /* Remove any redundant trailing "/"s. */
1124 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
1125 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
1127 CURRENT_FILE_NAME
[name_length
] = '\0';
1129 if (incremental_option
)
1131 /* Read the entry and delete files that aren't listed in the
1134 gnu_restore (skipcrud
);
1136 else if (typeflag
== GNUTYPE_DUMPDIR
)
1139 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1142 mode
= ((current_stat
.st_mode
1143 | (we_are_root
? 0 : MODE_WXUSR
))
1147 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1153 || old_files_option
== OVERWRITE_OLD_DIRS
1154 || old_files_option
== OVERWRITE_OLD_FILES
))
1157 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1161 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1164 if (S_ISDIR (st
.st_mode
))
1166 mode
= st
.st_mode
& ~ current_umask
;
1167 goto directory_exists
;
1173 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1176 if (errno
!= EEXIST
)
1178 mkdir_error (CURRENT_FILE_NAME
);
1180 undo_last_backup ();
1187 || old_files_option
== OVERWRITE_OLD_DIRS
1188 || old_files_option
== OVERWRITE_OLD_FILES
)
1189 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1190 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1192 ? ARCHIVED_PERMSTATUS
1193 : UNKNOWN_PERMSTATUS
));
1196 case GNUTYPE_VOLHDR
:
1198 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1205 case GNUTYPE_MULTIVOL
:
1207 _("%s: Cannot extract -- file is continued from another volume"),
1208 quotearg_colon (current_file_name
)));
1211 undo_last_backup ();
1214 case GNUTYPE_LONGNAME
:
1215 case GNUTYPE_LONGLINK
:
1216 ERROR ((0, 0, _("Visible long name error")));
1219 undo_last_backup ();
1224 _("%s: Unknown file type '%c', extracted as normal file"),
1225 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1229 #undef CURRENT_FILE_NAME
1232 /* Extract the symbolic links whose final extraction were delayed. */
1234 apply_delayed_symlinks (void)
1236 struct delayed_symlink
*ds
;
1238 for (ds
= delayed_symlink_head
; ds
; )
1240 struct string_list
*sources
= ds
->sources
;
1241 char const *valid_source
= 0;
1243 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1245 char const *source
= sources
->string
;
1248 /* Make sure the placeholder file is still there. If not,
1249 don't create a symlink, as the placeholder was probably
1250 removed by a later extraction. */
1251 if (lstat (source
, &st
) == 0
1252 && st
.st_dev
== ds
->dev
1253 && st
.st_ino
== ds
->ino
1254 && st
.st_mtime
== ds
->mtime
)
1256 /* Unlink the placeholder, then create a hard link if possible,
1257 a symbolic link otherwise. */
1258 if (unlink (source
) != 0)
1259 unlink_error (source
);
1260 else if (valid_source
&& link (valid_source
, source
) == 0)
1262 else if (symlink (ds
->target
, source
) != 0)
1263 symlink_error (ds
->target
, source
);
1266 valid_source
= source
;
1267 st
.st_uid
= ds
->uid
;
1268 st
.st_gid
= ds
->gid
;
1269 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1274 for (sources
= ds
->sources
; sources
; )
1276 struct string_list
*next
= sources
->next
;
1282 struct delayed_symlink
*next
= ds
->next
;
1288 delayed_symlink_head
= 0;
1291 /* Finish the extraction of an archive. */
1293 extract_finish (void)
1295 /* First, fix the status of ordinary directories that need fixing. */
1296 apply_nonancestor_delayed_set_stat ("", 0);
1298 /* Then, apply delayed symlinks, so that they don't affect delayed
1299 directory status-setting for ordinary directories. */
1300 apply_delayed_symlinks ();
1302 /* Finally, fix the status of directories that are ancestors
1303 of delayed symlinks. */
1304 apply_nonancestor_delayed_set_stat ("", 1);
1311 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));