1 /* Extract files from a tar archive.
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-11-19.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
37 int we_are_root
; /* true if our effective uid == 0 */
38 static mode_t newdir_umask
; /* umask when creating new directories */
39 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
41 /* Status of the permissions of a file that we are extracting. */
44 /* This file may have existed already; its permissions are unknown. */
47 /* This file was created using the permissions from the archive. */
50 /* This is an intermediate directory; the archive did not specify
55 /* List of directories whose statuses we need to extract after we've
56 finished extracting their subsidiary files. The head of the list
57 has the longest name; each non-head element in the list is an
58 ancestor (in the directory hierarchy) of the preceding element. */
59 struct delayed_set_stat
61 struct delayed_set_stat
*next
;
62 struct stat stat_info
;
64 mode_t invert_permissions
;
65 enum permstatus permstatus
;
69 static struct delayed_set_stat
*delayed_set_stat_head
;
71 /* List of symbolic links whose creation we have delayed. */
72 struct delayed_symlink
74 /* The next delayed symbolic link in the list. */
75 struct delayed_symlink
*next
;
77 /* The device, inode number and last-modified time of the placeholder. */
82 /* The desired owner and group of the symbolic link. */
86 /* A list of sources for this symlink. The sources are all to be
87 hard-linked together. */
88 struct string_list
*sources
;
90 /* The desired target of the desired link. */
94 static struct delayed_symlink
*delayed_symlink_head
;
98 struct string_list
*next
;
102 /* Set up to extract files. */
106 we_are_root
= geteuid () == 0;
107 same_permissions_option
+= we_are_root
;
108 same_owner_option
+= we_are_root
;
109 xalloc_fail_func
= extract_finish
;
111 /* Option -p clears the kernel umask, so it does not affect proper
112 restoration of file permissions. New intermediate directories will
113 comply with umask at start of program. */
115 newdir_umask
= umask (0);
116 if (0 < same_permissions_option
)
120 umask (newdir_umask
); /* restore the kernel umask */
121 current_umask
= newdir_umask
;
125 /* If restoring permissions, restore the mode for FILE_NAME from
126 information given in *STAT_INFO; otherwise invert the
127 INVERT_PERMISSIONS bits from the file's current permissions.
128 PERMSTATUS specifies the status of the file's permissions.
129 TYPEFLAG specifies the type of the file. */
131 set_mode (char const *file_name
, struct stat
const *stat_info
,
132 mode_t invert_permissions
, enum permstatus permstatus
,
137 if (0 < same_permissions_option
138 && permstatus
!= INTERDIR_PERMSTATUS
)
140 mode
= stat_info
->st_mode
;
142 /* If we created the file and it has a usual mode, then its mode
143 is normally set correctly already. But on many hosts, some
144 directories inherit the setgid bits from their parents, so we
145 we must set directories' modes explicitly. */
146 if (permstatus
== ARCHIVED_PERMSTATUS
147 && ! (mode
& ~ MODE_RWX
)
148 && typeflag
!= DIRTYPE
149 && typeflag
!= GNUTYPE_DUMPDIR
)
152 else if (! invert_permissions
)
156 /* We must inspect a directory's current permissions, since the
157 directory may have inherited its setgid bit from its parent.
159 INVERT_PERMISSIONS happens to be nonzero only for directories
160 that we created, so there's no point optimizing this code for
163 if (stat (file_name
, &st
) != 0)
165 stat_error (file_name
);
168 mode
= st
.st_mode
^ invert_permissions
;
171 if (chmod (file_name
, mode
) != 0)
172 chmod_error_details (file_name
, mode
);
175 /* Check time after successfully setting FILE_NAME's time stamp to T. */
177 check_time (char const *file_name
, time_t t
)
180 if (start_time
< t
&& (now
= time (0)) < t
)
181 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
182 file_name
, tartime (t
), (unsigned long) (t
- now
)));
185 /* Restore stat attributes (owner, group, mode and times) for
186 FILE_NAME, using information given in *STAT_INFO.
187 If not restoring permissions, invert the
188 INVERT_PERMISSIONS bits from the file's current permissions.
189 PERMSTATUS specifies the status of the file's permissions.
190 TYPEFLAG specifies the type of the file. */
192 /* FIXME: About proper restoration of symbolic link attributes, we still do
193 not have it right. Pretesters' reports tell us we need further study and
194 probably more configuration. For now, just use lchown if it exists, and
195 punt for the rest. Sigh! */
198 set_stat (char const *file_name
, struct stat
const *stat_info
,
199 mode_t invert_permissions
, enum permstatus permstatus
,
202 struct utimbuf utimbuf
;
204 if (typeflag
!= SYMTYPE
)
206 /* We do the utime before the chmod because some versions of utime are
207 broken and trash the modes of the file. */
209 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
211 /* We set the accessed time to `now', which is really the time we
212 started extracting files, unless incremental_option is used, in
213 which case .st_atime is used. */
215 /* FIXME: incremental_option should set ctime too, but how? */
217 if (incremental_option
)
218 utimbuf
.actime
= stat_info
->st_atime
;
220 utimbuf
.actime
= start_time
;
222 utimbuf
.modtime
= stat_info
->st_mtime
;
224 if (utime (file_name
, &utimbuf
) < 0)
225 utime_error (file_name
);
228 check_time (file_name
, stat_info
->st_atime
);
229 check_time (file_name
, stat_info
->st_mtime
);
233 /* Some systems allow non-root users to give files away. Once this
234 done, it is not possible anymore to change file permissions, so we
235 have to set permissions prior to possibly giving files away. */
237 set_mode (file_name
, stat_info
,
238 invert_permissions
, permstatus
, typeflag
);
241 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
243 /* When lchown exists, it should be used to change the attributes of
244 the symbolic link itself. In this case, a mere chown would change
245 the attributes of the file the symbolic link is pointing to, and
246 should be avoided. */
248 if (typeflag
== SYMTYPE
)
251 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
252 chown_error_details (file_name
,
253 stat_info
->st_uid
, stat_info
->st_gid
);
258 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
259 chown_error_details (file_name
,
260 stat_info
->st_uid
, stat_info
->st_gid
);
262 /* On a few systems, and in particular, those allowing to give files
263 away, changing the owner or group destroys the suid or sgid bits.
264 So let's attempt setting these bits once more. */
265 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
266 set_mode (file_name
, stat_info
,
267 invert_permissions
, permstatus
, typeflag
);
272 /* Remember to restore stat attributes (owner, group, mode and times)
273 for the directory FILE_NAME, using information given in *STAT_INFO,
274 once we stop extracting files into that directory.
275 If not restoring permissions, remember to invert the
276 INVERT_PERMISSIONS bits from the file's current permissions.
277 PERMSTATUS specifies the status of the file's permissions. */
279 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
280 mode_t invert_permissions
, enum permstatus permstatus
)
282 size_t file_name_len
= strlen (file_name
);
283 struct delayed_set_stat
*data
=
284 xmalloc (offsetof (struct delayed_set_stat
, file_name
) + file_name_len
);
285 data
->file_name_len
= file_name_len
;
286 strcpy (data
->file_name
, file_name
);
287 data
->invert_permissions
= invert_permissions
;
288 data
->permstatus
= permstatus
;
289 data
->stat_info
= *stat_info
;
290 data
->next
= delayed_set_stat_head
;
291 delayed_set_stat_head
= data
;
294 /* Update the delayed_set_stat info for an intermediate directory
295 created on the path to DIR_NAME. The intermediate directory turned
296 out to be the same as this directory, e.g. due to ".." or symbolic
297 links. *DIR_STAT_INFO is the status of the directory. */
299 repair_delayed_set_stat (char const *dir_name
,
300 struct stat
const *dir_stat_info
)
302 struct delayed_set_stat
*data
;
303 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
306 if (stat (data
->file_name
, &st
) != 0)
308 stat_error (data
->file_name
);
312 if (st
.st_dev
== dir_stat_info
->st_dev
313 && st
.st_ino
== dir_stat_info
->st_ino
)
315 data
->stat_info
= current_stat
;
316 data
->invert_permissions
= (MODE_RWX
317 & (current_stat
.st_mode
^ st
.st_mode
));
318 data
->permstatus
= ARCHIVED_PERMSTATUS
;
323 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
324 quotearg_colon (dir_name
)));
327 /* After a file/link/symlink/directory creation has failed, see if
328 it's because some required directory was not present, and if so,
329 create all required directories. Return non-zero if a directory
332 make_directories (char *file_name
)
334 char *cursor0
= file_name
+ FILESYSTEM_PREFIX_LEN (file_name
);
335 char *cursor
; /* points into path */
336 int did_something
= 0; /* did we do anything yet? */
338 int invert_permissions
;
342 for (cursor
= cursor0
; *cursor
; cursor
++)
344 if (! ISSLASH (*cursor
))
347 /* Avoid mkdir of empty string, if leading or double '/'. */
349 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
352 /* Avoid mkdir where last part of path is "." or "..". */
354 if (cursor
[-1] == '.'
355 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
356 || (cursor
[-2] == '.'
357 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
360 *cursor
= '\0'; /* truncate the path there */
361 mode
= MODE_RWX
& ~ newdir_umask
;
362 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
363 status
= mkdir (file_name
, mode
^ invert_permissions
);
367 /* Create a struct delayed_set_stat even if
368 invert_permissions is zero, because
369 repair_delayed_set_stat may need to update the struct. */
370 delay_set_stat (file_name
,
371 ¤t_stat
/* ignored */,
372 invert_permissions
, INTERDIR_PERMSTATUS
);
374 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
385 /* Turbo C mkdir gives a funny errno. */
389 /* Directory already exists. */
392 /* Some other error in the mkdir. We return to the caller. */
396 return did_something
; /* tell them to retry if we made one */
399 /* Prepare to extract a file.
400 Return zero if extraction should not proceed. */
403 prepare_to_extract (char const *file_name
)
405 if (to_stdout_option
)
408 if (old_files_option
== UNLINK_FIRST_OLD_FILES
409 && !remove_any_file (file_name
, recursive_unlink_option
)
410 && errno
&& errno
!= ENOENT
)
412 unlink_error (file_name
);
419 /* Attempt repairing what went wrong with the extraction. Delete an
420 already existing file or create missing intermediate directories.
421 Return nonzero if we somewhat increased our chances at a successful
422 extraction. errno is properly restored on zero return. */
424 maybe_recoverable (char *file_name
, int *interdir_made
)
432 /* Remove an old file, if the options allow this. */
434 switch (old_files_option
)
439 case DEFAULT_OLD_FILES
:
440 case OVERWRITE_OLD_FILES
:
442 int r
= remove_any_file (file_name
, 0);
449 /* Attempt creating missing intermediate directories. */
450 if (! make_directories (file_name
))
459 /* Just say we can't do anything about it... */
466 extract_sparse_file (int fd
, off_t
*sizeleft
, off_t totalsize
, char *name
)
470 /* assuming sizeleft is initially totalsize */
472 while (*sizeleft
> 0)
476 union block
*data_block
= find_next_block ();
479 ERROR ((0, 0, _("Unexpected EOF in archive")));
482 if (lseek (fd
, sparsearray
[sparse_ind
].offset
, SEEK_SET
) < 0)
484 seek_error_details (name
, sparsearray
[sparse_ind
].offset
);
487 written
= sparsearray
[sparse_ind
++].numbytes
;
488 while (written
> BLOCKSIZE
)
490 count
= full_write (fd
, data_block
->buffer
, BLOCKSIZE
);
493 if (count
!= BLOCKSIZE
)
495 write_error_details (name
, count
, BLOCKSIZE
);
498 set_next_block_after (data_block
);
499 data_block
= find_next_block ();
502 ERROR ((0, 0, _("Unexpected EOF in archive")));
507 count
= full_write (fd
, data_block
->buffer
, written
);
510 if (count
!= written
)
512 write_error_details (name
, count
, written
);
516 set_next_block_after (data_block
);
520 /* Fix the statuses of all directories whose statuses need fixing, and
521 which are not ancestors of FILE_NAME. */
523 apply_nonancestor_delayed_set_stat (char const *file_name
)
525 size_t file_name_len
= strlen (file_name
);
527 while (delayed_set_stat_head
)
529 struct delayed_set_stat
*data
= delayed_set_stat_head
;
530 if (data
->file_name_len
< file_name_len
531 && file_name
[data
->file_name_len
]
532 && (ISSLASH (file_name
[data
->file_name_len
])
533 || ISSLASH (file_name
[data
->file_name_len
- 1]))
534 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0)
536 delayed_set_stat_head
= data
->next
;
537 set_stat (data
->file_name
, &data
->stat_info
,
538 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
543 /* Extract a file from the archive. */
545 extract_archive (void)
547 union block
*data_block
;
558 int interdir_made
= 0;
562 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
564 set_next_block_after (current_header
);
565 decode_header (current_header
, ¤t_stat
, ¤t_format
, 1);
567 if (interactive_option
&& !confirm ("extract", current_file_name
))
573 /* Print the block from current_header and current_stat. */
578 /* Check for fully specified file names and other atrocities. */
581 if (! absolute_names_option
)
583 if (contains_dot_dot (CURRENT_FILE_NAME
))
585 ERROR ((0, 0, _("%s: Member name contains `..'"),
586 quotearg_colon (CURRENT_FILE_NAME
)));
591 skipcrud
= FILESYSTEM_PREFIX_LEN (current_file_name
);
592 while (ISSLASH (CURRENT_FILE_NAME
[0]))
597 static int warned_once
;
602 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
603 (int) skipcrud
, current_file_name
));
608 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME
);
610 /* Take a safety backup of a previously existing file. */
612 if (backup_option
&& !to_stdout_option
)
613 if (!maybe_backup_file (CURRENT_FILE_NAME
, 0))
616 ERROR ((0, e
, _("%s: Was unable to backup this file"),
617 quotearg_colon (CURRENT_FILE_NAME
)));
622 /* Extract the archive entry according to its type. */
624 typeflag
= current_header
->header
.typeflag
;
627 /* JK - What we want to do if the file is sparse is loop through
628 the array of sparse structures in the header and read in and
629 translate the character strings representing 1) the offset at
630 which to write and 2) how many bytes to write into numbers,
631 which we store into the scratch array, "sparsearray". This
632 array makes our life easier the same way it did in creating the
633 tar file that had to deal with a sparse file.
635 After we read in the first five (at most) sparse structures, we
636 check to see if the file has an extended header, i.e., if more
637 sparse structures are needed to describe the contents of the new
638 file. If so, we read in the extended headers and continue to
639 store their contents into the sparsearray. */
644 xmalloc (sp_array_size
* sizeof (struct sp_array
));
646 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
648 struct sparse
const *s
= ¤t_header
->oldgnu_header
.sp
[counter
];
649 sparsearray
[counter
].offset
= OFF_FROM_HEADER (s
->offset
);
650 sparsearray
[counter
].numbytes
= SIZE_FROM_HEADER (s
->numbytes
);
651 if (!sparsearray
[counter
].numbytes
)
655 if (current_header
->oldgnu_header
.isextended
)
657 /* Read in the list of extended headers and translate them
658 into the sparsearray as before. Note that this
659 invalidates current_header. */
661 /* static */ int ind
= SPARSES_IN_OLDGNU_HEADER
;
665 exhdr
= find_next_block ();
668 ERROR ((0, 0, _("Unexpected EOF in archive")));
671 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
673 struct sparse
const *s
= &exhdr
->sparse_header
.sp
[counter
];
674 if (counter
+ ind
> sp_array_size
- 1)
676 /* Realloc the scratch area since we've run out of
681 xrealloc (sparsearray
,
682 sp_array_size
* sizeof (struct sp_array
));
684 if (s
->numbytes
[0] == 0)
686 sparsearray
[counter
+ ind
].offset
=
687 OFF_FROM_HEADER (s
->offset
);
688 sparsearray
[counter
+ ind
].numbytes
=
689 SIZE_FROM_HEADER (s
->numbytes
);
691 if (!exhdr
->sparse_header
.isextended
)
695 ind
+= SPARSES_IN_SPARSE_HEADER
;
696 set_next_block_after (exhdr
);
699 set_next_block_after (exhdr
);
707 /* Appears to be a file. But BSD tar uses the convention that a slash
708 suffix means a directory. */
710 name_length
= strlen (CURRENT_FILE_NAME
);
711 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
712 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
715 /* FIXME: deal with protection issues. */
718 openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
719 | (old_files_option
== OVERWRITE_OLD_FILES
722 mode
= current_stat
.st_mode
& MODE_RWX
& ~ current_umask
;
724 if (to_stdout_option
)
730 if (! prepare_to_extract (CURRENT_FILE_NAME
))
739 /* Contiguous files (on the Masscomp) have to specify the size in
740 the open call that creates them. */
742 if (typeflag
== CONTTYPE
)
743 fd
= open (CURRENT_FILE_NAME
, openflag
| O_CTG
,
744 mode
, current_stat
.st_size
);
746 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
748 #else /* not O_CTG */
749 if (typeflag
== CONTTYPE
)
751 static int conttype_diagnosed
;
753 if (!conttype_diagnosed
)
755 conttype_diagnosed
= 1;
756 WARN ((0, 0, _("Extracting contiguous files as regular files")));
759 fd
= open (CURRENT_FILE_NAME
, openflag
, mode
);
761 #endif /* not O_CTG */
765 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
768 open_error (CURRENT_FILE_NAME
);
776 if (typeflag
== GNUTYPE_SPARSE
)
779 size_t name_length_bis
;
781 /* Kludge alert. NAME is assigned to header.name because
782 during the extraction, the space that contains the header
783 will get scribbled on, and the name will get munged, so any
784 error messages that happen to contain the filename will look
785 REAL interesting unless we do this. */
787 name_length_bis
= strlen (CURRENT_FILE_NAME
) + 1;
788 name
= xmalloc (name_length_bis
);
789 memcpy (name
, CURRENT_FILE_NAME
, name_length_bis
);
790 size
= current_stat
.st_size
;
791 extract_sparse_file (fd
, &size
, current_stat
.st_size
, name
);
795 for (size
= current_stat
.st_size
; size
> 0; )
797 if (multi_volume_option
)
799 assign_string (&save_name
, current_file_name
);
800 save_totsize
= current_stat
.st_size
;
801 save_sizeleft
= size
;
804 /* Locate data, determine max length writeable, write it,
805 block that we have used the data, then check if the write
808 data_block
= find_next_block ();
811 ERROR ((0, 0, _("Unexpected EOF in archive")));
812 break; /* FIXME: What happens, then? */
815 written
= available_space_after (data_block
);
820 count
= full_write (fd
, data_block
->buffer
, written
);
823 set_next_block_after ((union block
*)
824 (data_block
->buffer
+ written
- 1));
825 if (count
!= written
)
827 write_error_details (CURRENT_FILE_NAME
, count
, written
);
834 if (multi_volume_option
)
835 assign_string (&save_name
, 0);
837 /* If writing to stdout, don't try to do anything to the filename;
838 it doesn't exist, or we don't want to touch it anyway. */
840 if (to_stdout_option
)
846 close_error (CURRENT_FILE_NAME
);
851 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
852 (old_files_option
== OVERWRITE_OLD_FILES
854 : ARCHIVED_PERMSTATUS
),
860 if (! prepare_to_extract (CURRENT_FILE_NAME
))
863 if (absolute_names_option
864 || ! (ISSLASH (current_link_name
865 [FILESYSTEM_PREFIX_LEN (current_link_name
)])
866 || contains_dot_dot (current_link_name
)))
868 while (status
= symlink (current_link_name
, CURRENT_FILE_NAME
),
870 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
874 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0, 0, SYMTYPE
);
876 symlink_error (current_link_name
, CURRENT_FILE_NAME
);
880 /* This symbolic link is potentially dangerous. Don't
881 create it now; instead, create a placeholder file, which
882 will be replaced after other extraction is done. */
885 while (fd
= open (CURRENT_FILE_NAME
, O_WRONLY
| O_CREAT
| O_EXCL
, 0),
887 if (! maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
892 open_error (CURRENT_FILE_NAME
);
893 else if (fstat (fd
, &st
) != 0)
895 stat_error (CURRENT_FILE_NAME
);
898 else if (close (fd
) != 0)
899 close_error (CURRENT_FILE_NAME
);
902 size_t filelen
= strlen (CURRENT_FILE_NAME
);
903 size_t linklen
= strlen (current_link_name
);
904 struct delayed_symlink
*p
=
905 xmalloc (offsetof (struct delayed_symlink
, target
)
907 p
->next
= delayed_symlink_head
;
908 delayed_symlink_head
= p
;
911 p
->mtime
= st
.st_mtime
;
912 p
->uid
= current_stat
.st_uid
;
913 p
->gid
= current_stat
.st_gid
;
914 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
916 p
->sources
->next
= 0;
917 memcpy (p
->sources
->string
, CURRENT_FILE_NAME
, filelen
+ 1);
918 memcpy (p
->target
, current_link_name
, linklen
+ 1);
923 if (status
!= 0 && backup_option
)
929 static int warned_once
;
935 _("Attempting extraction of symbolic links as hard links")));
943 if (! prepare_to_extract (CURRENT_FILE_NAME
))
948 struct stat st1
, st2
;
951 /* MSDOS does not implement links. However, djgpp's link() actually
953 status
= link (current_link_name
, CURRENT_FILE_NAME
);
957 struct delayed_symlink
*ds
= delayed_symlink_head
;
958 if (ds
&& stat (current_link_name
, &st1
) == 0)
959 for (; ds
; ds
= ds
->next
)
960 if (ds
->dev
== st1
.st_dev
961 && ds
->ino
== st1
.st_ino
962 && ds
->mtime
== st1
.st_mtime
)
964 struct string_list
*p
=
965 xmalloc (offsetof (struct string_list
, string
)
966 + strlen (CURRENT_FILE_NAME
) + 1);
967 strcpy (p
->string
, CURRENT_FILE_NAME
);
968 p
->next
= ds
->sources
;
974 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
977 if (incremental_option
&& errno
== EEXIST
)
980 if (stat (current_link_name
, &st1
) == 0
981 && stat (CURRENT_FILE_NAME
, &st2
) == 0
982 && st1
.st_dev
== st2
.st_dev
983 && st1
.st_ino
== st2
.st_ino
)
986 link_error (current_link_name
, CURRENT_FILE_NAME
);
994 current_stat
.st_mode
|= S_IFCHR
;
1000 current_stat
.st_mode
|= S_IFBLK
;
1003 #if S_IFCHR || S_IFBLK
1005 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1008 status
= mknod (CURRENT_FILE_NAME
, current_stat
.st_mode
,
1009 current_stat
.st_rdev
);
1012 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1014 mknod_error (CURRENT_FILE_NAME
);
1016 undo_last_backup ();
1019 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
1020 ARCHIVED_PERMSTATUS
, typeflag
);
1024 #if HAVE_MKFIFO || defined mkfifo
1026 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1029 while (status
= mkfifo (CURRENT_FILE_NAME
, current_stat
.st_mode
),
1031 if (!maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1035 set_stat (CURRENT_FILE_NAME
, ¤t_stat
, 0,
1036 ARCHIVED_PERMSTATUS
, typeflag
);
1039 mkfifo_error (CURRENT_FILE_NAME
);
1041 undo_last_backup ();
1047 case GNUTYPE_DUMPDIR
:
1048 name_length
= strlen (CURRENT_FILE_NAME
);
1051 /* Remove any redundant trailing "/"s. */
1052 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME
) < name_length
1053 && CURRENT_FILE_NAME
[name_length
- 1] == '/')
1055 CURRENT_FILE_NAME
[name_length
] = '\0';
1057 if (incremental_option
)
1059 /* Read the entry and delete files that aren't listed in the
1062 gnu_restore (skipcrud
);
1064 else if (typeflag
== GNUTYPE_DUMPDIR
)
1067 if (! prepare_to_extract (CURRENT_FILE_NAME
))
1070 mode
= ((current_stat
.st_mode
1071 | (we_are_root
? 0 : MODE_WXUSR
))
1075 status
= mkdir (CURRENT_FILE_NAME
, mode
);
1080 && (interdir_made
|| old_files_option
== OVERWRITE_OLD_FILES
))
1083 if (stat (CURRENT_FILE_NAME
, &st
) == 0)
1087 repair_delayed_set_stat (CURRENT_FILE_NAME
, &st
);
1090 if (S_ISDIR (st
.st_mode
))
1092 mode
= st
.st_mode
& ~ current_umask
;
1093 goto directory_exists
;
1099 if (maybe_recoverable (CURRENT_FILE_NAME
, &interdir_made
))
1102 if (errno
!= EEXIST
)
1104 mkdir_error (CURRENT_FILE_NAME
);
1106 undo_last_backup ();
1113 || old_files_option
== OVERWRITE_OLD_FILES
)
1114 delay_set_stat (CURRENT_FILE_NAME
, ¤t_stat
,
1115 MODE_RWX
& (mode
^ current_stat
.st_mode
),
1117 ? ARCHIVED_PERMSTATUS
1118 : UNKNOWN_PERMSTATUS
));
1121 case GNUTYPE_VOLHDR
:
1123 fprintf (stdlis
, _("Reading %s\n"), quote (current_file_name
));
1130 case GNUTYPE_MULTIVOL
:
1132 _("%s: Cannot extract -- file is continued from another volume"),
1133 quotearg_colon (current_file_name
)));
1136 undo_last_backup ();
1139 case GNUTYPE_LONGNAME
:
1140 case GNUTYPE_LONGLINK
:
1141 ERROR ((0, 0, _("Visible long name error")));
1144 undo_last_backup ();
1149 _("%s: Unknown file type '%c', extracted as normal file"),
1150 quotearg_colon (CURRENT_FILE_NAME
), typeflag
));
1154 #undef CURRENT_FILE_NAME
1157 /* Extract the symbolic links whose final extraction were delayed. */
1159 apply_delayed_symlinks (void)
1161 struct delayed_symlink
*ds
;
1163 for (ds
= delayed_symlink_head
; ds
; )
1165 struct string_list
*sources
= ds
->sources
;
1166 char const *valid_source
= 0;
1168 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1170 char const *source
= sources
->string
;
1173 /* Make sure the placeholder file is still there. If not,
1174 don't create a symlink, as the placeholder was probably
1175 removed by a later extraction. */
1176 if (lstat (source
, &st
) == 0
1177 && st
.st_dev
== ds
->dev
1178 && st
.st_ino
== ds
->ino
1179 && st
.st_mtime
== ds
->mtime
)
1181 /* Unlink the placeholder, then create a hard link if possible,
1182 a symbolic link otherwise. */
1183 if (unlink (source
) != 0)
1184 unlink_error (source
);
1185 else if (valid_source
&& link (valid_source
, source
) == 0)
1187 else if (symlink (ds
->target
, source
) != 0)
1188 symlink_error (ds
->target
, source
);
1191 valid_source
= source
;
1192 st
.st_uid
= ds
->uid
;
1193 st
.st_gid
= ds
->gid
;
1194 set_stat (source
, &st
, 0, 0, SYMTYPE
);
1199 for (sources
= ds
->sources
; sources
; )
1201 struct string_list
*next
= sources
->next
;
1207 struct delayed_symlink
*next
= ds
->next
;
1213 delayed_symlink_head
= 0;
1216 /* Finish the extraction of an archive. */
1218 extract_finish (void)
1220 /* Apply delayed symlinks last, so that they don't affect
1221 delayed directory status-setting. */
1222 apply_nonancestor_delayed_set_stat ("");
1223 apply_delayed_symlinks ();
1230 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));