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. */
22 /* $FreeBSD: src/contrib/tar/src/extract.c,v 1.4.2.3 2002/10/19 09:37:29 sobomax Exp $ */
39 int we_are_root; /* true if our effective uid == 0 */
40 static mode_t newdir_umask; /* umask when creating new directories */
41 static mode_t current_umask; /* current umask (which is set to 0 if -p) */
43 /* Status of the permissions of a file that we are extracting. */
46 /* This file may have existed already; its permissions are unknown. */
49 /* This file was created using the permissions from the archive. */
52 /* This is an intermediate directory; the archive did not specify
57 /* List of directories whose statuses we need to extract after we've
58 finished extracting their subsidiary files. If you consider each
59 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
60 represents an element where AFTER_SYMLINKS is nonzero and [^D]
61 represents an element where AFTER_SYMLINKS is zero, then the head
62 of the subsequence has the longest name, and each non-head element
63 in the prefix is an ancestor (in the directory hierarchy) of the
66 struct delayed_set_stat
68 struct delayed_set_stat *next;
69 struct stat stat_info;
71 mode_t invert_permissions;
72 enum permstatus permstatus;
77 static struct delayed_set_stat *delayed_set_stat_head;
79 /* List of symbolic links whose creation we have delayed. */
80 struct delayed_symlink
82 /* The next delayed symbolic link in the list. */
83 struct delayed_symlink *next;
85 /* The device, inode number and last-modified time of the placeholder. */
90 /* The desired owner and group of the symbolic link. */
94 /* A list of sources for this symlink. The sources are all to be
95 hard-linked together. */
96 struct string_list *sources;
98 /* The desired target of the desired link. */
102 static struct delayed_symlink *delayed_symlink_head;
106 struct string_list *next;
110 /* Set up to extract files. */
114 we_are_root = geteuid () == 0;
116 same_permissions_option += we_are_root;
118 same_owner_option += we_are_root;
119 xalloc_fail_func = extract_finish;
121 /* Option -p clears the kernel umask, so it does not affect proper
122 restoration of file permissions. New intermediate directories will
123 comply with umask at start of program. */
125 newdir_umask = umask (0);
126 if (0 < same_permissions_option)
130 umask (newdir_umask); /* restore the kernel umask */
131 current_umask = newdir_umask;
133 same_permissions_option++;
138 /* If restoring permissions, restore the mode for FILE_NAME from
139 information given in *STAT_INFO (where *CURRENT_STAT_INFO gives
140 the current status if CURRENT_STAT_INFO is nonzero); otherwise invert the
141 INVERT_PERMISSIONS bits from the file's current permissions.
142 PERMSTATUS specifies the status of the file's permissions.
143 TYPEFLAG specifies the type of the file. */
145 set_mode (char const *file_name, struct stat const *stat_info,
146 struct stat const *current_stat_info,
147 mode_t invert_permissions, enum permstatus permstatus,
152 if (0 < same_permissions_option
153 && permstatus != INTERDIR_PERMSTATUS)
155 mode = stat_info->st_mode;
157 /* If we created the file and it has a usual mode, then its mode
158 is normally set correctly already. But on many hosts, some
159 directories inherit the setgid bits from their parents, so we
160 we must set directories' modes explicitly. */
161 if (permstatus == ARCHIVED_PERMSTATUS
162 && ! (mode & ~ MODE_RWX)
163 && typeflag != DIRTYPE
164 && typeflag != GNUTYPE_DUMPDIR)
167 else if (! invert_permissions)
171 /* We must inspect a directory's current permissions, since the
172 directory may have inherited its setgid bit from its parent.
174 INVERT_PERMISSIONS happens to be nonzero only for directories
175 that we created, so there's no point optimizing this code for
178 if (! current_stat_info)
180 if (stat (file_name, &st) != 0)
182 stat_error (file_name);
185 current_stat_info = &st;
187 mode = current_stat_info->st_mode ^ invert_permissions;
190 if (chmod (file_name, mode & ~ current_umask) != 0)
191 chmod_error_details (file_name, mode);
194 /* Check time after successfully setting FILE_NAME's time stamp to T. */
196 check_time (char const *file_name, time_t t)
199 if (start_time < t && (now = time (0)) < t)
200 WARN ((0, 0, _("%s: time stamp %s is %lu s in the future"),
201 file_name, tartime (t), (unsigned long) (t - now)));
204 /* Restore stat attributes (owner, group, mode and times) for
205 FILE_NAME, using information given in *STAT_INFO.
206 If CURRENT_STAT_INFO is nonzero, *CURRENT_STAT_INFO is the
207 file's currernt status.
208 If not restoring permissions, invert the
209 INVERT_PERMISSIONS bits from the file's current permissions.
210 PERMSTATUS specifies the status of the file's permissions.
211 TYPEFLAG specifies the type of the file. */
213 /* FIXME: About proper restoration of symbolic link attributes, we still do
214 not have it right. Pretesters' reports tell us we need further study and
215 probably more configuration. For now, just use lchown if it exists, and
216 punt for the rest. Sigh! */
219 set_stat (char const *file_name, struct stat const *stat_info,
220 struct stat const *current_stat_info,
221 mode_t invert_permissions, enum permstatus permstatus,
224 struct utimbuf utimbuf;
226 if (typeflag != SYMTYPE)
228 /* We do the utime before the chmod because some versions of utime are
229 broken and trash the modes of the file. */
231 if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
233 /* We set the accessed time to `now', which is really the time we
234 started extracting files, unless incremental_option is used, in
235 which case .st_atime is used. */
237 /* FIXME: incremental_option should set ctime too, but how? */
239 if (incremental_option)
240 utimbuf.actime = stat_info->st_atime;
242 utimbuf.actime = start_time;
244 utimbuf.modtime = stat_info->st_mtime;
246 if (utime (file_name, &utimbuf) < 0)
247 utime_error (file_name);
250 check_time (file_name, stat_info->st_atime);
251 check_time (file_name, stat_info->st_mtime);
255 /* Some systems allow non-root users to give files away. Once this
256 done, it is not possible anymore to change file permissions, so we
257 have to set permissions prior to possibly giving files away. */
259 set_mode (file_name, stat_info, current_stat_info,
260 invert_permissions, permstatus, typeflag);
263 if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
265 /* When lchown exists, it should be used to change the attributes of
266 the symbolic link itself. In this case, a mere chown would change
267 the attributes of the file the symbolic link is pointing to, and
268 should be avoided. */
270 if (typeflag == SYMTYPE)
273 if (lchown (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);
280 if (chown (file_name, stat_info->st_uid, stat_info->st_gid) < 0)
281 chown_error_details (file_name,
282 stat_info->st_uid, stat_info->st_gid);
284 /* On a few systems, and in particular, those allowing to give files
285 away, changing the owner or group destroys the suid or sgid bits.
286 So let's attempt setting these bits once more. */
287 if (stat_info->st_mode & (S_ISUID | S_ISGID | S_ISVTX))
288 set_mode (file_name, stat_info, 0,
289 invert_permissions, permstatus, typeflag);
294 /* Remember to restore stat attributes (owner, group, mode and times)
295 for the directory FILE_NAME, using information given in *STAT_INFO,
296 once we stop extracting files into that directory.
297 If not restoring permissions, remember to invert the
298 INVERT_PERMISSIONS bits from the file's current permissions.
299 PERMSTATUS specifies the status of the file's permissions. */
301 delay_set_stat (char const *file_name, struct stat const *stat_info,
302 mode_t invert_permissions, enum permstatus permstatus)
304 size_t file_name_len = strlen (file_name);
305 struct delayed_set_stat *data =
306 xmalloc (offsetof (struct delayed_set_stat, file_name)
307 + file_name_len + 1);
308 data->file_name_len = file_name_len;
309 strcpy (data->file_name, file_name);
310 data->invert_permissions = invert_permissions;
311 data->permstatus = permstatus;
312 data->after_symlinks = 0;
313 data->stat_info = *stat_info;
314 data->next = delayed_set_stat_head;
315 delayed_set_stat_head = data;
318 /* Update the delayed_set_stat info for an intermediate directory
319 created on the path to DIR_NAME. The intermediate directory turned
320 out to be the same as this directory, e.g. due to ".." or symbolic
321 links. *DIR_STAT_INFO is the status of the directory. */
323 repair_delayed_set_stat (char const *dir_name,
324 struct stat const *dir_stat_info)
326 struct delayed_set_stat *data;
327 for (data = delayed_set_stat_head; data; data = data->next)
330 if (stat (data->file_name, &st) != 0)
332 stat_error (data->file_name);
336 if (st.st_dev == dir_stat_info->st_dev
337 && st.st_ino == dir_stat_info->st_ino)
339 data->stat_info = current_stat;
340 data->invert_permissions = (MODE_RWX
341 & (current_stat.st_mode ^ st.st_mode));
342 data->permstatus = ARCHIVED_PERMSTATUS;
347 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
348 quotearg_colon (dir_name)));
351 /* After a file/link/symlink/directory creation has failed, see if
352 it's because some required directory was not present, and if so,
353 create all required directories. Return non-zero if a directory
356 make_directories (char *file_name)
358 char *cursor0 = file_name + FILESYSTEM_PREFIX_LEN (file_name);
359 char *cursor; /* points into path */
360 int did_something = 0; /* did we do anything yet? */
362 int invert_permissions;
366 for (cursor = cursor0; *cursor; cursor++)
368 if (! ISSLASH (*cursor))
371 /* Avoid mkdir of empty string, if leading or double '/'. */
373 if (cursor == cursor0 || ISSLASH (cursor[-1]))
376 /* Avoid mkdir where last part of path is "." or "..". */
378 if (cursor[-1] == '.'
379 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
380 || (cursor[-2] == '.'
381 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
384 *cursor = '\0'; /* truncate the path there */
385 mode = MODE_RWX & ~ newdir_umask;
386 invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
387 status = mkdir (file_name, mode ^ invert_permissions);
391 /* Create a struct delayed_set_stat even if
392 invert_permissions is zero, because
393 repair_delayed_set_stat may need to update the struct. */
394 delay_set_stat (file_name,
395 ¤t_stat /* ignored */,
396 invert_permissions, INTERDIR_PERMSTATUS);
398 print_for_mkdir (file_name, cursor - file_name, mode);
409 /* Turbo C mkdir gives a funny errno. */
413 /* Directory already exists. */
416 /* Some other error in the mkdir. We return to the caller. */
420 return did_something; /* tell them to retry if we made one */
423 /* Prepare to extract a file.
424 Return zero if extraction should not proceed. */
427 prepare_to_extract (char const *file_name)
429 if (to_stdout_option)
432 if (old_files_option == UNLINK_FIRST_OLD_FILES
433 && !remove_any_file (file_name, recursive_unlink_option)
434 && errno && errno != ENOENT)
436 unlink_error (file_name);
443 /* Attempt repairing what went wrong with the extraction. Delete an
444 already existing file or create missing intermediate directories.
445 Return nonzero if we somewhat increased our chances at a successful
446 extraction. errno is properly restored on zero return. */
448 maybe_recoverable (char *file_name, int *interdir_made)
456 /* Remove an old file, if the options allow this. */
458 switch (old_files_option)
463 case DEFAULT_OLD_FILES:
464 case OVERWRITE_OLD_DIRS:
465 case OVERWRITE_OLD_FILES:
467 int r = remove_any_file (file_name, 0);
474 /* Attempt creating missing intermediate directories. */
475 if (! make_directories (file_name))
484 /* Just say we can't do anything about it... */
491 extract_sparse_file (int fd, off_t *sizeleft, off_t totalsize, char *name)
495 /* assuming sizeleft is initially totalsize */
497 while (*sizeleft > 0)
501 union block *data_block = find_next_block ();
504 ERROR ((0, 0, _("Unexpected EOF in archive")));
507 if (lseek (fd, sparsearray[sparse_ind].offset, SEEK_SET) < 0)
509 seek_error_details (name, sparsearray[sparse_ind].offset);
512 written = sparsearray[sparse_ind++].numbytes;
513 while (written > BLOCKSIZE)
515 count = full_write (fd, data_block->buffer, BLOCKSIZE);
518 if (count != BLOCKSIZE)
520 write_error_details (name, count, BLOCKSIZE);
523 set_next_block_after (data_block);
524 data_block = find_next_block ();
527 ERROR ((0, 0, _("Unexpected EOF in archive")));
532 count = full_write (fd, data_block->buffer, written);
535 if (count != written)
537 write_error_details (name, count, written);
541 set_next_block_after (data_block);
545 /* Fix the statuses of all directories whose statuses need fixing, and
546 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
547 nonzero, do this for all such directories; otherwise, stop at the
548 first directory that is marked to be fixed up only after delayed
549 symlinks are applied. */
551 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_symlinks)
553 size_t file_name_len = strlen (file_name);
554 bool check_for_renamed_directories = 0;
556 while (delayed_set_stat_head)
558 struct delayed_set_stat *data = delayed_set_stat_head;
559 bool skip_this_one = 0;
561 struct stat const *current_stat_info = 0;
563 check_for_renamed_directories |= data->after_symlinks;
565 if (after_symlinks < data->after_symlinks
566 || (data->file_name_len < file_name_len
567 && file_name[data->file_name_len]
568 && (ISSLASH (file_name[data->file_name_len])
569 || ISSLASH (file_name[data->file_name_len - 1]))
570 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
573 if (check_for_renamed_directories)
575 current_stat_info = &st;
576 if (stat (data->file_name, &st) != 0)
578 stat_error (data->file_name);
581 else if (! (st.st_dev == data->stat_info.st_dev
582 && (st.st_ino == data->stat_info.st_ino)))
585 _("%s: Directory renamed before its status could be extracted"),
586 quotearg_colon (data->file_name)));
592 set_stat (data->file_name, &data->stat_info, current_stat_info,
593 data->invert_permissions, data->permstatus, DIRTYPE);
595 delayed_set_stat_head = data->next;
600 /* Extract a file from the archive. */
602 extract_archive (void)
604 union block *data_block;
615 int interdir_made = 0;
619 #define CURRENT_FILE_NAME (skipcrud + current_file_name)
621 set_next_block_after (current_header);
622 decode_header (current_header, ¤t_stat, ¤t_format, 1);
624 if (interactive_option && !confirm ("extract", current_file_name))
630 /* Print the block from current_header and current_stat. */
635 /* Check for fully specified file names and other atrocities. */
638 if (! absolute_names_option)
640 if (contains_dot_dot (CURRENT_FILE_NAME))
642 ERROR ((0, 0, _("%s: Member name contains `..'"),
643 quotearg_colon (CURRENT_FILE_NAME)));
648 skipcrud = FILESYSTEM_PREFIX_LEN (current_file_name);
649 while (ISSLASH (CURRENT_FILE_NAME[0]))
654 static int warned_once;
659 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
660 (int) skipcrud, current_file_name));
665 apply_nonancestor_delayed_set_stat (CURRENT_FILE_NAME, 0);
667 /* Take a safety backup of a previously existing file. */
669 if (backup_option && !to_stdout_option)
670 if (!maybe_backup_file (CURRENT_FILE_NAME, 0))
673 ERROR ((0, e, _("%s: Was unable to backup this file"),
674 quotearg_colon (CURRENT_FILE_NAME)));
679 /* Extract the archive entry according to its type. */
681 typeflag = current_header->header.typeflag;
684 /* JK - What we want to do if the file is sparse is loop through
685 the array of sparse structures in the header and read in and
686 translate the character strings representing 1) the offset at
687 which to write and 2) how many bytes to write into numbers,
688 which we store into the scratch array, "sparsearray". This
689 array makes our life easier the same way it did in creating the
690 tar file that had to deal with a sparse file.
692 After we read in the first five (at most) sparse structures, we
693 check to see if the file has an extended header, i.e., if more
694 sparse structures are needed to describe the contents of the new
695 file. If so, we read in the extended headers and continue to
696 store their contents into the sparsearray. */
701 xmalloc (sp_array_size * sizeof (struct sp_array));
703 for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
705 struct sparse const *s = ¤t_header->oldgnu_header.sp[counter];
706 sparsearray[counter].offset = OFF_FROM_HEADER (s->offset);
707 sparsearray[counter].numbytes = SIZE_FROM_HEADER (s->numbytes);
708 if (!sparsearray[counter].numbytes)
712 if (current_header->oldgnu_header.isextended)
714 /* Read in the list of extended headers and translate them
715 into the sparsearray as before. Note that this
716 invalidates current_header. */
718 /* static */ int ind = SPARSES_IN_OLDGNU_HEADER;
722 exhdr = find_next_block ();
725 ERROR ((0, 0, _("Unexpected EOF in archive")));
728 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
730 struct sparse const *s = &exhdr->sparse_header.sp[counter];
731 if (counter + ind > sp_array_size - 1)
733 /* Realloc the scratch area since we've run out of
738 xrealloc (sparsearray,
739 sp_array_size * sizeof (struct sp_array));
741 if (s->numbytes[0] == 0)
743 sparsearray[counter + ind].offset =
744 OFF_FROM_HEADER (s->offset);
745 sparsearray[counter + ind].numbytes =
746 SIZE_FROM_HEADER (s->numbytes);
748 if (!exhdr->sparse_header.isextended)
752 ind += SPARSES_IN_SPARSE_HEADER;
753 set_next_block_after (exhdr);
756 set_next_block_after (exhdr);
764 /* Appears to be a file. But BSD tar uses the convention that a slash
765 suffix means a directory. */
767 name_length = strlen (CURRENT_FILE_NAME);
768 if (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME) < name_length
769 && CURRENT_FILE_NAME[name_length - 1] == '/')
772 /* FIXME: deal with protection issues. */
775 openflag = (O_WRONLY | O_BINARY | O_CREAT
776 | (old_files_option == OVERWRITE_OLD_FILES
779 mode = current_stat.st_mode & MODE_RWX & ~ current_umask;
781 if (to_stdout_option)
787 if (! prepare_to_extract (CURRENT_FILE_NAME))
796 /* Contiguous files (on the Masscomp) have to specify the size in
797 the open call that creates them. */
799 if (typeflag == CONTTYPE)
800 fd = open (CURRENT_FILE_NAME, openflag | O_CTG,
801 mode, current_stat.st_size);
803 fd = open (CURRENT_FILE_NAME, openflag, mode);
805 #else /* not O_CTG */
806 if (typeflag == CONTTYPE)
808 static int conttype_diagnosed;
810 if (!conttype_diagnosed)
812 conttype_diagnosed = 1;
813 WARN ((0, 0, _("Extracting contiguous files as regular files")));
816 fd = open (CURRENT_FILE_NAME, openflag, mode);
818 #endif /* not O_CTG */
822 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
825 open_error (CURRENT_FILE_NAME);
833 if (typeflag == GNUTYPE_SPARSE)
836 size_t name_length_bis;
838 /* Kludge alert. NAME is assigned to header.name because
839 during the extraction, the space that contains the header
840 will get scribbled on, and the name will get munged, so any
841 error messages that happen to contain the filename will look
842 REAL interesting unless we do this. */
844 name_length_bis = strlen (CURRENT_FILE_NAME) + 1;
845 name = xmalloc (name_length_bis);
846 memcpy (name, CURRENT_FILE_NAME, name_length_bis);
847 size = current_stat.st_size;
848 extract_sparse_file (fd, &size, current_stat.st_size, name);
852 for (size = current_stat.st_size; size > 0; )
854 if (multi_volume_option)
856 assign_string (&save_name, current_file_name);
857 save_totsize = current_stat.st_size;
858 save_sizeleft = size;
861 /* Locate data, determine max length writeable, write it,
862 block that we have used the data, then check if the write
865 data_block = find_next_block ();
868 ERROR ((0, 0, _("Unexpected EOF in archive")));
869 break; /* FIXME: What happens, then? */
872 written = available_space_after (data_block);
877 count = full_write (fd, data_block->buffer, written);
880 set_next_block_after ((union block *)
881 (data_block->buffer + written - 1));
882 if (count != written)
884 write_error_details (CURRENT_FILE_NAME, count, written);
891 if (multi_volume_option)
892 assign_string (&save_name, 0);
894 /* If writing to stdout, don't try to do anything to the filename;
895 it doesn't exist, or we don't want to touch it anyway. */
897 if (to_stdout_option)
903 close_error (CURRENT_FILE_NAME);
908 set_stat (CURRENT_FILE_NAME, ¤t_stat, 0, 0,
909 (old_files_option == OVERWRITE_OLD_FILES
911 : ARCHIVED_PERMSTATUS),
917 if (! prepare_to_extract (CURRENT_FILE_NAME))
920 if (absolute_names_option
921 || ! (ISSLASH (current_link_name
922 [FILESYSTEM_PREFIX_LEN (current_link_name)])
923 || contains_dot_dot (current_link_name)))
925 while (status = symlink (current_link_name, CURRENT_FILE_NAME),
927 if (!maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
931 set_stat (CURRENT_FILE_NAME, ¤t_stat, 0, 0, 0, SYMTYPE);
933 symlink_error (current_link_name, CURRENT_FILE_NAME);
937 /* This symbolic link is potentially dangerous. Don't
938 create it now; instead, create a placeholder file, which
939 will be replaced after other extraction is done. */
942 while (fd = open (CURRENT_FILE_NAME, O_WRONLY | O_CREAT | O_EXCL, 0),
944 if (! maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
949 open_error (CURRENT_FILE_NAME);
950 else if (fstat (fd, &st) != 0)
952 stat_error (CURRENT_FILE_NAME);
955 else if (close (fd) != 0)
956 close_error (CURRENT_FILE_NAME);
959 struct delayed_set_stat *h;
960 struct delayed_symlink *p =
961 xmalloc (offsetof (struct delayed_symlink, target)
962 + strlen (current_link_name) + 1);
963 p->next = delayed_symlink_head;
964 delayed_symlink_head = p;
967 p->mtime = st.st_mtime;
968 p->uid = current_stat.st_uid;
969 p->gid = current_stat.st_gid;
970 p->sources = xmalloc (offsetof (struct string_list, string)
971 + strlen (CURRENT_FILE_NAME) + 1);
972 p->sources->next = 0;
973 strcpy (p->sources->string, CURRENT_FILE_NAME);
974 strcpy (p->target, current_link_name);
976 h = delayed_set_stat_head;
977 if (h && ! h->after_symlinks
978 && strncmp (CURRENT_FILE_NAME, h->file_name, h->file_name_len) == 0
979 && ISSLASH (CURRENT_FILE_NAME[h->file_name_len])
980 && (base_name (CURRENT_FILE_NAME)
981 == CURRENT_FILE_NAME + h->file_name_len + 1))
985 h->after_symlinks = 1;
987 if (stat (h->file_name, &st) != 0)
988 stat_error (h->file_name);
991 h->stat_info.st_dev = st.st_dev;
992 h->stat_info.st_ino = st.st_ino;
995 while ((h = h->next) && ! h->after_symlinks);
1002 if (status != 0 && backup_option)
1003 undo_last_backup ();
1008 static int warned_once;
1014 _("Attempting extraction of symbolic links as hard links")));
1022 if (! prepare_to_extract (CURRENT_FILE_NAME))
1027 struct stat st1, st2;
1029 size_t skiplinkcrud;
1031 if (absolute_names_option)
1034 skiplinkcrud = FILESYSTEM_PREFIX_LEN (current_link_name);
1035 while (ISSLASH (current_link_name[skiplinkcrud]))
1039 /* MSDOS does not implement links. However, djgpp's link() actually
1041 status = link (current_link_name + skiplinkcrud, CURRENT_FILE_NAME);
1045 struct delayed_symlink *ds = delayed_symlink_head;
1046 if (ds && stat (current_link_name, &st1) == 0)
1047 for (; ds; ds = ds->next)
1048 if (ds->dev == st1.st_dev
1049 && ds->ino == st1.st_ino
1050 && ds->mtime == st1.st_mtime)
1052 struct string_list *p =
1053 xmalloc (offsetof (struct string_list, string)
1054 + strlen (CURRENT_FILE_NAME) + 1);
1055 strcpy (p->string, CURRENT_FILE_NAME);
1056 p->next = ds->sources;
1062 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
1065 if (incremental_option && errno == EEXIST)
1068 if (stat (current_link_name, &st1) == 0
1069 && stat (CURRENT_FILE_NAME, &st2) == 0
1070 && st1.st_dev == st2.st_dev
1071 && st1.st_ino == st2.st_ino)
1074 link_error (current_link_name, CURRENT_FILE_NAME);
1076 undo_last_backup ();
1082 current_stat.st_mode |= S_IFCHR;
1088 current_stat.st_mode |= S_IFBLK;
1091 #if S_IFCHR || S_IFBLK
1093 if (! prepare_to_extract (CURRENT_FILE_NAME))
1096 status = mknod (CURRENT_FILE_NAME, current_stat.st_mode,
1097 current_stat.st_rdev);
1100 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
1102 mknod_error (CURRENT_FILE_NAME);
1104 undo_last_backup ();
1107 set_stat (CURRENT_FILE_NAME, ¤t_stat, 0, 0,
1108 ARCHIVED_PERMSTATUS, typeflag);
1112 #if HAVE_MKFIFO || defined mkfifo
1114 if (! prepare_to_extract (CURRENT_FILE_NAME))
1117 while (status = mkfifo (CURRENT_FILE_NAME, current_stat.st_mode),
1119 if (!maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
1123 set_stat (CURRENT_FILE_NAME, ¤t_stat, 0, 0,
1124 ARCHIVED_PERMSTATUS, typeflag);
1127 mkfifo_error (CURRENT_FILE_NAME);
1129 undo_last_backup ();
1135 case GNUTYPE_DUMPDIR:
1136 name_length = strlen (CURRENT_FILE_NAME);
1139 /* Remove any redundant trailing "/"s. */
1140 while (FILESYSTEM_PREFIX_LEN (CURRENT_FILE_NAME) < name_length
1141 && CURRENT_FILE_NAME[name_length - 1] == '/')
1143 CURRENT_FILE_NAME[name_length] = '\0';
1145 if (incremental_option)
1147 /* Read the entry and delete files that aren't listed in the
1150 gnu_restore (skipcrud);
1152 else if (typeflag == GNUTYPE_DUMPDIR)
1157 if (stat (CURRENT_FILE_NAME, &st) != 0 || !S_ISDIR (st.st_mode))
1158 if (! prepare_to_extract (CURRENT_FILE_NAME))
1162 mode = ((current_stat.st_mode
1163 | (we_are_root ? 0 : MODE_WXUSR))
1167 status = mkdir (CURRENT_FILE_NAME, mode);
1173 || old_files_option == OVERWRITE_OLD_DIRS
1174 || old_files_option == OVERWRITE_OLD_FILES))
1177 if (stat (CURRENT_FILE_NAME, &st) == 0)
1181 repair_delayed_set_stat (CURRENT_FILE_NAME, &st);
1184 if (S_ISDIR (st.st_mode))
1186 mode = st.st_mode & ~ current_umask;
1187 goto directory_exists;
1193 if (maybe_recoverable (CURRENT_FILE_NAME, &interdir_made))
1196 if (errno != EEXIST)
1198 mkdir_error (CURRENT_FILE_NAME);
1200 undo_last_backup ();
1207 || old_files_option == OVERWRITE_OLD_DIRS
1208 || old_files_option == OVERWRITE_OLD_FILES)
1209 delay_set_stat (CURRENT_FILE_NAME, ¤t_stat,
1210 MODE_RWX & (mode ^ current_stat.st_mode),
1212 ? ARCHIVED_PERMSTATUS
1213 : UNKNOWN_PERMSTATUS));
1216 case GNUTYPE_VOLHDR:
1218 fprintf (stdlis, _("Reading %s\n"), quote (current_file_name));
1225 case GNUTYPE_MULTIVOL:
1227 _("%s: Cannot extract -- file is continued from another volume"),
1228 quotearg_colon (current_file_name)));
1231 undo_last_backup ();
1234 case GNUTYPE_LONGNAME:
1235 case GNUTYPE_LONGLINK:
1236 ERROR ((0, 0, _("Visible long name error")));
1239 undo_last_backup ();
1244 _("%s: Unknown file type '%c', extracted as normal file"),
1245 quotearg_colon (CURRENT_FILE_NAME), typeflag));
1249 #undef CURRENT_FILE_NAME
1252 /* Extract the symbolic links whose final extraction were delayed. */
1254 apply_delayed_symlinks (void)
1256 struct delayed_symlink *ds;
1258 for (ds = delayed_symlink_head; ds; )
1260 struct string_list *sources = ds->sources;
1261 char const *valid_source = 0;
1263 for (sources = ds->sources; sources; sources = sources->next)
1265 char const *source = sources->string;
1268 /* Make sure the placeholder file is still there. If not,
1269 don't create a symlink, as the placeholder was probably
1270 removed by a later extraction. */
1271 if (lstat (source, &st) == 0
1272 && st.st_dev == ds->dev
1273 && st.st_ino == ds->ino
1274 && st.st_mtime == ds->mtime)
1276 /* Unlink the placeholder, then create a hard link if possible,
1277 a symbolic link otherwise. */
1278 if (unlink (source) != 0)
1279 unlink_error (source);
1280 else if (valid_source && link (valid_source, source) == 0)
1282 else if (symlink (ds->target, source) != 0)
1283 symlink_error (ds->target, source);
1286 valid_source = source;
1287 st.st_uid = ds->uid;
1288 st.st_gid = ds->gid;
1289 set_stat (source, &st, 0, 0, 0, SYMTYPE);
1294 for (sources = ds->sources; sources; )
1296 struct string_list *next = sources->next;
1302 struct delayed_symlink *next = ds->next;
1308 delayed_symlink_head = 0;
1311 /* Finish the extraction of an archive. */
1313 extract_finish (void)
1315 /* First, fix the status of ordinary directories that need fixing. */
1316 apply_nonancestor_delayed_set_stat ("", 0);
1318 /* Then, apply delayed symlinks, so that they don't affect delayed
1319 directory status-setting for ordinary directories. */
1320 apply_delayed_symlinks ();
1322 /* Finally, fix the status of directories that are ancestors
1323 of delayed symlinks. */
1324 apply_nonancestor_delayed_set_stat ("", 1);
1331 error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));