1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005 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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 static bool we_are_root
; /* true if our effective uid == 0 */
31 static mode_t newdir_umask
; /* umask when creating new directories */
32 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
34 /* Status of the permissions of a file that we are extracting. */
37 /* This file may have existed already; its permissions are unknown. */
40 /* This file was created using the permissions from the archive. */
43 /* This is an intermediate directory; the archive did not specify
48 /* List of directories whose statuses we need to extract after we've
49 finished extracting their subsidiary files. If you consider each
50 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
51 represents an element where AFTER_SYMLINKS is nonzero and [^D]
52 represents an element where AFTER_SYMLINKS is zero, then the head
53 of the subsequence has the longest name, and each non-head element
54 in the prefix is an ancestor (in the directory hierarchy) of the
57 struct delayed_set_stat
59 struct delayed_set_stat
*next
;
60 struct stat stat_info
;
62 mode_t invert_permissions
;
63 enum permstatus permstatus
;
68 static struct delayed_set_stat
*delayed_set_stat_head
;
70 /* List of symbolic links whose creation we have delayed. */
71 struct delayed_symlink
73 /* The next delayed symbolic link in the list. */
74 struct delayed_symlink
*next
;
76 /* The device, inode number and last-modified time of the placeholder. */
81 /* The desired owner and group of the symbolic link. */
85 /* A list of sources for this symlink. The sources are all to be
86 hard-linked together. */
87 struct string_list
*sources
;
89 /* The desired target of the desired link. */
93 static struct delayed_symlink
*delayed_symlink_head
;
97 struct string_list
*next
;
101 /* Set up to extract files. */
105 we_are_root
= geteuid () == 0;
106 same_permissions_option
+= we_are_root
;
107 same_owner_option
+= we_are_root
;
109 /* Save 'root device' to avoid purging mount points.
110 FIXME: Should the same be done after handling -C option ? */
111 if (one_file_system_option
)
114 char *dir
= xgetcwd ();
116 if (deref_stat (true, dir
, &st
))
119 root_device
= st
.st_dev
;
122 /* Option -p clears the kernel umask, so it does not affect proper
123 restoration of file permissions. New intermediate directories will
124 comply with umask at start of program. */
126 newdir_umask
= umask (0);
127 if (0 < same_permissions_option
)
131 umask (newdir_umask
); /* restore the kernel umask */
132 current_umask
= newdir_umask
;
136 /* If restoring permissions, restore the mode for FILE_NAME from
137 information given in *STAT_INFO (where *CUR_INFO gives
138 the current status if CUR_INFO is nonzero); otherwise invert the
139 INVERT_PERMISSIONS bits from the file's current permissions.
140 PERMSTATUS specifies the status of the file's permissions.
141 TYPEFLAG specifies the type of the file. */
143 set_mode (char const *file_name
,
144 struct stat
const *stat_info
,
145 struct stat
const *cur_info
,
146 mode_t invert_permissions
, enum permstatus permstatus
,
151 if (0 < same_permissions_option
152 && permstatus
!= INTERDIR_PERMSTATUS
)
154 mode
= stat_info
->st_mode
;
156 /* If we created the file and it has a usual mode, then its mode
157 is normally set correctly already. But on many hosts, some
158 directories inherit the setgid bits from their parents, so we
159 we must set directories' modes explicitly. */
160 if (permstatus
== ARCHIVED_PERMSTATUS
161 && ! (mode
& ~ MODE_RWX
)
162 && typeflag
!= DIRTYPE
163 && typeflag
!= GNUTYPE_DUMPDIR
)
166 else if (! invert_permissions
)
170 /* We must inspect a directory's current permissions, since the
171 directory may have inherited its setgid bit from its parent.
173 INVERT_PERMISSIONS happens to be nonzero only for directories
174 that we created, so there's no point optimizing this code for
179 if (stat (file_name
, &st
) != 0)
181 stat_error (file_name
);
186 mode
= cur_info
->st_mode
^ invert_permissions
;
189 if (chmod (file_name
, mode
) != 0)
190 chmod_error_details (file_name
, mode
);
193 /* Check time after successfully setting FILE_NAME's time stamp to T. */
195 check_time (char const *file_name
, struct timespec t
)
198 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
199 file_name
, tartime (t
, true)));
200 else if (timespec_lt (start_time
, t
))
204 if (timespec_lt (now
, t
))
206 unsigned long int ds
= t
.tv_sec
- now
.tv_sec
;
207 int dns
= t
.tv_nsec
- now
.tv_nsec
;
208 char dnsbuf
[sizeof ".FFFFFFFFF"];
214 code_ns_fraction (dns
, dnsbuf
);
215 WARN ((0, 0, _("%s: time stamp %s is %lu%s s in the future"),
216 file_name
, tartime (t
, true), ds
, dnsbuf
));
221 /* Restore stat attributes (owner, group, mode and times) for
222 FILE_NAME, using information given in *STAT_INFO.
223 If CUR_INFO is nonzero, *CUR_INFO is the
224 file's currernt status.
225 If not restoring permissions, invert the
226 INVERT_PERMISSIONS bits from the file's current permissions.
227 PERMSTATUS specifies the status of the file's permissions.
228 TYPEFLAG specifies the type of the file. */
230 /* FIXME: About proper restoration of symbolic link attributes, we still do
231 not have it right. Pretesters' reports tell us we need further study and
232 probably more configuration. For now, just use lchown if it exists, and
233 punt for the rest. Sigh! */
236 set_stat (char const *file_name
,
237 struct stat
const *stat_info
,
238 struct stat
const *cur_info
,
239 mode_t invert_permissions
, enum permstatus permstatus
,
242 if (typeflag
!= SYMTYPE
)
244 /* We do the utime before the chmod because some versions of utime are
245 broken and trash the modes of the file. */
247 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
249 /* We set the accessed time to `now', which is really the time we
250 started extracting files, unless incremental_option is used, in
251 which case .st_atime is used. */
253 /* FIXME: incremental_option should set ctime too, but how? */
255 struct timespec ts
[2];
256 ts
[0] = incremental_option
? get_stat_atime (stat_info
) : start_time
;
257 ts
[1] = get_stat_mtime (stat_info
);
259 if (utimens (file_name
, ts
) != 0)
260 utime_error (file_name
);
263 check_time (file_name
, ts
[0]);
264 check_time (file_name
, ts
[1]);
268 /* Some systems allow non-root users to give files away. Once this
269 done, it is not possible anymore to change file permissions, so we
270 have to set permissions prior to possibly giving files away. */
272 set_mode (file_name
, stat_info
, cur_info
,
273 invert_permissions
, permstatus
, typeflag
);
276 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
278 /* When lchown exists, it should be used to change the attributes of
279 the symbolic link itself. In this case, a mere chown would change
280 the attributes of the file the symbolic link is pointing to, and
281 should be avoided. */
283 if (typeflag
== SYMTYPE
)
286 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
287 chown_error_details (file_name
,
288 stat_info
->st_uid
, stat_info
->st_gid
);
293 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
294 chown_error_details (file_name
,
295 stat_info
->st_uid
, stat_info
->st_gid
);
297 /* On a few systems, and in particular, those allowing to give files
298 away, changing the owner or group destroys the suid or sgid bits.
299 So let's attempt setting these bits once more. */
300 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
301 set_mode (file_name
, stat_info
, 0,
302 invert_permissions
, permstatus
, typeflag
);
307 /* Remember to restore stat attributes (owner, group, mode and times)
308 for the directory FILE_NAME, using information given in *STAT_INFO,
309 once we stop extracting files into that directory.
310 If not restoring permissions, remember to invert the
311 INVERT_PERMISSIONS bits from the file's current permissions.
312 PERMSTATUS specifies the status of the file's permissions. */
314 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
315 mode_t invert_permissions
, enum permstatus permstatus
)
317 size_t file_name_len
= strlen (file_name
);
318 struct delayed_set_stat
*data
=
319 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
320 + file_name_len
+ 1);
321 data
->file_name_len
= file_name_len
;
322 strcpy (data
->file_name
, file_name
);
323 data
->invert_permissions
= invert_permissions
;
324 data
->permstatus
= permstatus
;
325 data
->after_symlinks
= 0;
326 data
->stat_info
= *stat_info
;
327 data
->next
= delayed_set_stat_head
;
328 delayed_set_stat_head
= data
;
331 /* Update the delayed_set_stat info for an intermediate directory
332 created within the file name of DIR. The intermediate directory turned
333 out to be the same as this directory, e.g. due to ".." or symbolic
334 links. *DIR_STAT_INFO is the status of the directory. */
336 repair_delayed_set_stat (char const *dir
,
337 struct stat
const *dir_stat_info
)
339 struct delayed_set_stat
*data
;
340 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
343 if (stat (data
->file_name
, &st
) != 0)
345 stat_error (data
->file_name
);
349 if (st
.st_dev
== dir_stat_info
->st_dev
350 && st
.st_ino
== dir_stat_info
->st_ino
)
352 data
->stat_info
= current_stat_info
.stat
;
353 data
->invert_permissions
=
354 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
355 data
->permstatus
= ARCHIVED_PERMSTATUS
;
360 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
361 quotearg_colon (dir
)));
364 /* After a file/link/symlink/directory creation has failed, see if
365 it's because some required directory was not present, and if so,
366 create all required directories. Return non-zero if a directory
369 make_directories (char *file_name
)
371 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
372 char *cursor
; /* points into the file name */
373 int did_something
= 0; /* did we do anything yet? */
375 int invert_permissions
;
379 for (cursor
= cursor0
; *cursor
; cursor
++)
381 if (! ISSLASH (*cursor
))
384 /* Avoid mkdir of empty string, if leading or double '/'. */
386 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
389 /* Avoid mkdir where last part of file name is "." or "..". */
391 if (cursor
[-1] == '.'
392 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
393 || (cursor
[-2] == '.'
394 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
397 *cursor
= '\0'; /* truncate the name there */
398 mode
= MODE_RWX
& ~ newdir_umask
;
399 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
400 status
= mkdir (file_name
, mode
^ invert_permissions
);
404 /* Create a struct delayed_set_stat even if
405 invert_permissions is zero, because
406 repair_delayed_set_stat may need to update the struct. */
407 delay_set_stat (file_name
,
408 ¤t_stat_info
.stat
/* ignored */,
409 invert_permissions
, INTERDIR_PERMSTATUS
);
411 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
421 continue; /* Directory already exists. */
422 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
423 this. Reported by Warren Hyde
424 <Warren.Hyde@motorola.com> */
425 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
426 && access (file_name
, W_OK
) == 0)
429 /* Some other error in the mkdir. We return to the caller. */
433 return did_something
; /* tell them to retry if we made one */
437 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
441 if (stat (file_name
, &st
))
443 stat_warn (file_name
);
444 /* Be on the safe side: if the file does exist assume it is newer */
445 return errno
!= ENOENT
;
447 if (!S_ISDIR (st
.st_mode
)
448 && st
.st_mtime
>= tar_stat
->stat
.st_mtime
)
455 /* Attempt repairing what went wrong with the extraction. Delete an
456 already existing file or create missing intermediate directories.
457 Return nonzero if we somewhat increased our chances at a successful
458 extraction. errno is properly restored on zero return. */
460 maybe_recoverable (char *file_name
, int *interdir_made
)
470 /* Remove an old file, if the options allow this. */
472 switch (old_files_option
)
477 case KEEP_NEWER_FILES
:
478 if (file_newer_p (file_name
, ¤t_stat_info
))
485 case DEFAULT_OLD_FILES
:
486 case NO_OVERWRITE_DIR_OLD_FILES
:
487 case OVERWRITE_OLD_FILES
:
489 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
494 case UNLINK_FIRST_OLD_FILES
:
499 /* Attempt creating missing intermediate directories. */
500 if (! make_directories (file_name
))
509 /* Just say we can't do anything about it... */
515 /* Fix the statuses of all directories whose statuses need fixing, and
516 which are not ancestors of FILE_NAME. If AFTER_SYMLINKS is
517 nonzero, do this for all such directories; otherwise, stop at the
518 first directory that is marked to be fixed up only after delayed
519 symlinks are applied. */
521 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_symlinks
)
523 size_t file_name_len
= strlen (file_name
);
524 bool check_for_renamed_directories
= 0;
526 while (delayed_set_stat_head
)
528 struct delayed_set_stat
*data
= delayed_set_stat_head
;
529 bool skip_this_one
= 0;
531 struct stat
const *cur_info
= 0;
533 check_for_renamed_directories
|= data
->after_symlinks
;
535 if (after_symlinks
< data
->after_symlinks
536 || (data
->file_name_len
< file_name_len
537 && file_name
[data
->file_name_len
]
538 && (ISSLASH (file_name
[data
->file_name_len
])
539 || ISSLASH (file_name
[data
->file_name_len
- 1]))
540 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
543 if (check_for_renamed_directories
)
546 if (stat (data
->file_name
, &st
) != 0)
548 stat_error (data
->file_name
);
551 else if (! (st
.st_dev
== data
->stat_info
.st_dev
552 && (st
.st_ino
== data
->stat_info
.st_ino
)))
555 _("%s: Directory renamed before its status could be extracted"),
556 quotearg_colon (data
->file_name
)));
562 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
563 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
565 delayed_set_stat_head
= data
->next
;
572 /* Extractor functions for various member types */
575 extract_dir (char *file_name
, int typeflag
)
579 int interdir_made
= 0;
581 if (incremental_option
)
582 /* Read the entry and delete files that aren't listed in the archive. */
583 purge_directory (file_name
);
584 else if (typeflag
== GNUTYPE_DUMPDIR
)
587 mode
= (current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
)) & MODE_RWX
;
589 while ((status
= mkdir (file_name
, mode
)))
593 || old_files_option
== DEFAULT_OLD_FILES
594 || old_files_option
== OVERWRITE_OLD_FILES
))
597 if (stat (file_name
, &st
) == 0)
601 repair_delayed_set_stat (file_name
, &st
);
604 if (S_ISDIR (st
.st_mode
))
606 mode
= st
.st_mode
& ~ current_umask
;
613 if (maybe_recoverable (file_name
, &interdir_made
))
618 mkdir_error (file_name
);
625 || old_files_option
== DEFAULT_OLD_FILES
626 || old_files_option
== OVERWRITE_OLD_FILES
)
627 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
628 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
630 ? ARCHIVED_PERMSTATUS
631 : UNKNOWN_PERMSTATUS
));
638 open_output_file (char *file_name
, int typeflag
)
641 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
642 | (old_files_option
== OVERWRITE_OLD_FILES
645 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
648 /* Contiguous files (on the Masscomp) have to specify the size in
649 the open call that creates them. */
651 if (typeflag
== CONTTYPE
)
652 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
654 fd
= open (file_name
, openflag
, mode
);
656 #else /* not O_CTG */
657 if (typeflag
== CONTTYPE
)
659 static int conttype_diagnosed
;
661 if (!conttype_diagnosed
)
663 conttype_diagnosed
= 1;
664 WARN ((0, 0, _("Extracting contiguous files as regular files")));
667 fd
= open (file_name
, openflag
, mode
);
669 #endif /* not O_CTG */
675 extract_file (char *file_name
, int typeflag
)
679 union block
*data_block
;
683 int interdir_made
= 0;
685 /* FIXME: deal with protection issues. */
687 if (to_stdout_option
)
689 else if (to_command_option
)
691 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
701 fd
= open_output_file (file_name
, typeflag
);
702 while (fd
< 0 && maybe_recoverable (file_name
, &interdir_made
));
706 open_error (file_name
);
711 if (current_stat_info
.is_sparse
)
712 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
714 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
716 if (multi_volume_option
)
718 assign_string (&save_name
, current_stat_info
.orig_file_name
);
719 save_totsize
= current_stat_info
.stat
.st_size
;
720 save_sizeleft
= size
;
723 /* Locate data, determine max length writeable, write it,
724 block that we have used the data, then check if the write
727 data_block
= find_next_block ();
730 ERROR ((0, 0, _("Unexpected EOF in archive")));
731 break; /* FIXME: What happens, then? */
734 written
= available_space_after (data_block
);
739 count
= full_write (fd
, data_block
->buffer
, written
);
742 set_next_block_after ((union block
*)
743 (data_block
->buffer
+ written
- 1));
744 if (count
!= written
)
746 if (!to_command_option
)
747 write_error_details (file_name
, count
, written
);
748 /* FIXME: shouldn't we restore from backup? */
755 if (multi_volume_option
)
756 assign_string (&save_name
, 0);
758 /* If writing to stdout, don't try to do anything to the filename;
759 it doesn't exist, or we don't want to touch it anyway. */
761 if (to_stdout_option
)
766 close_error (file_name
);
768 if (to_command_option
)
771 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
772 (old_files_option
== OVERWRITE_OLD_FILES
?
773 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
780 extract_link (char *file_name
, int typeflag
)
782 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
,
783 true, absolute_names_option
);
784 int interdir_made
= 0;
788 struct stat st1
, st2
;
790 int status
= link (link_name
, file_name
);
795 struct delayed_symlink
*ds
= delayed_symlink_head
;
796 if (ds
&& lstat (link_name
, &st1
) == 0)
797 for (; ds
; ds
= ds
->next
)
798 if (ds
->dev
== st1
.st_dev
799 && ds
->ino
== st1
.st_ino
800 && ds
->mtime
== st1
.st_mtime
)
802 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
803 + strlen (file_name
) + 1);
804 strcpy (p
->string
, file_name
);
805 p
->next
= ds
->sources
;
811 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
812 || (lstat (link_name
, &st1
) == 0
813 && lstat (file_name
, &st2
) == 0
814 && st1
.st_dev
== st2
.st_dev
815 && st1
.st_ino
== st2
.st_ino
))
820 while (maybe_recoverable (file_name
, &interdir_made
));
822 if (!(incremental_option
&& errno
== EEXIST
))
824 link_error (link_name
, file_name
);
831 extract_symlink (char *file_name
, int typeflag
)
835 int interdir_made
= 0;
837 if (absolute_names_option
838 || ! (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
839 || contains_dot_dot (current_stat_info
.link_name
)))
841 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
842 if (!maybe_recoverable (file_name
, &interdir_made
))
846 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
848 symlink_error (current_stat_info
.link_name
, file_name
);
852 /* This symbolic link is potentially dangerous. Don't
853 create it now; instead, create a placeholder file, which
854 will be replaced after other extraction is done. */
857 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
858 if (! maybe_recoverable (file_name
, &interdir_made
))
863 open_error (file_name
);
864 else if (fstat (fd
, &st
) != 0)
866 stat_error (file_name
);
869 else if (close (fd
) != 0)
870 close_error (file_name
);
873 struct delayed_set_stat
*h
;
874 struct delayed_symlink
*p
= xmalloc (offsetof (struct delayed_symlink
, target
)
875 + strlen (current_stat_info
.link_name
)
877 p
->next
= delayed_symlink_head
;
878 delayed_symlink_head
= p
;
881 p
->mtime
= st
.st_mtime
;
882 p
->uid
= current_stat_info
.stat
.st_uid
;
883 p
->gid
= current_stat_info
.stat
.st_gid
;
884 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
885 + strlen (file_name
) + 1);
886 p
->sources
->next
= 0;
887 strcpy (p
->sources
->string
, file_name
);
888 strcpy (p
->target
, current_stat_info
.link_name
);
890 h
= delayed_set_stat_head
;
891 if (h
&& ! h
->after_symlinks
892 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
893 && ISSLASH (file_name
[h
->file_name_len
])
894 && (base_name (file_name
) == file_name
+ h
->file_name_len
+ 1))
898 h
->after_symlinks
= 1;
900 if (stat (h
->file_name
, &st
) != 0)
901 stat_error (h
->file_name
);
904 h
->stat_info
.st_dev
= st
.st_dev
;
905 h
->stat_info
.st_ino
= st
.st_ino
;
908 while ((h
= h
->next
) && ! h
->after_symlinks
);
918 static int warned_once
;
923 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
925 return extract_link (file_name
, typeflag
);
929 #if S_IFCHR || S_IFBLK
931 extract_node (char *file_name
, int typeflag
)
934 int interdir_made
= 0;
937 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
938 current_stat_info
.stat
.st_rdev
);
939 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
942 mknod_error (file_name
);
944 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, ARCHIVED_PERMSTATUS
, typeflag
);
949 #if HAVE_MKFIFO || defined mkfifo
951 extract_fifo (char *file_name
, int typeflag
)
954 int interdir_made
= 0;
956 while ((status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
)))
957 if (!maybe_recoverable (file_name
, &interdir_made
))
961 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
962 ARCHIVED_PERMSTATUS
, typeflag
);
964 mkfifo_error (file_name
);
970 extract_mangle_wrapper (char *file_name
, int typeflag
)
978 extract_failure (char *file_name
, int typeflag
)
983 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
987 /* Prepare to extract a file. Find extractor function.
988 Return zero if extraction should not proceed. */
991 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
995 if (EXTRACT_OVER_PIPE
)
998 /* Select the extractor */
1001 case GNUTYPE_SPARSE
:
1002 *fun
= extract_file
;
1009 /* Appears to be a file. But BSD tar uses the convention that a slash
1010 suffix means a directory. */
1011 if (current_stat_info
.had_trailing_slash
)
1015 *fun
= extract_file
;
1021 *fun
= extract_symlink
;
1025 *fun
= extract_link
;
1030 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1031 *fun
= extract_node
;
1037 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1038 *fun
= extract_node
;
1042 #if HAVE_MKFIFO || defined mkfifo
1044 *fun
= extract_fifo
;
1049 case GNUTYPE_DUMPDIR
:
1053 case GNUTYPE_VOLHDR
:
1055 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1060 *fun
= extract_mangle_wrapper
;
1063 case GNUTYPE_MULTIVOL
:
1065 _("%s: Cannot extract -- file is continued from another volume"),
1066 quotearg_colon (current_stat_info
.file_name
)));
1067 *fun
= extract_failure
;
1070 case GNUTYPE_LONGNAME
:
1071 case GNUTYPE_LONGLINK
:
1072 ERROR ((0, 0, _("Unexpected long name header")));
1073 *fun
= extract_failure
;
1078 _("%s: Unknown file type `%c', extracted as normal file"),
1079 quotearg_colon (file_name
), typeflag
));
1080 *fun
= extract_file
;
1083 /* Determine whether the extraction should proceed */
1087 switch (old_files_option
)
1089 case UNLINK_FIRST_OLD_FILES
:
1090 if (!remove_any_file (file_name
,
1091 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1092 : ORDINARY_REMOVE_OPTION
)
1093 && errno
&& errno
!= ENOENT
)
1095 unlink_error (file_name
);
1100 case KEEP_NEWER_FILES
:
1101 if (file_newer_p (file_name
, ¤t_stat_info
))
1103 WARN ((0, 0, _("Current %s is newer or same age"),
1104 quote (file_name
)));
1116 /* Extract a file from the archive. */
1118 extract_archive (void)
1122 tar_extractor_t fun
;
1124 set_next_block_after (current_header
);
1125 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1127 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
1133 /* Print the block from current_header and current_stat. */
1136 print_header (¤t_stat_info
, -1);
1138 file_name
= safer_name_suffix (current_stat_info
.file_name
,
1139 false, absolute_names_option
);
1140 if (strip_name_components
)
1142 size_t prefix_len
= stripped_prefix_len (file_name
, strip_name_components
);
1143 if (prefix_len
== (size_t) -1)
1148 file_name
+= prefix_len
;
1151 apply_nonancestor_delayed_set_stat (file_name
, 0);
1153 /* Take a safety backup of a previously existing file. */
1156 if (!maybe_backup_file (file_name
, 0))
1159 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1160 quotearg_colon (file_name
)));
1165 /* Extract the archive entry according to its type. */
1168 typeflag
= sparse_member_p (¤t_stat_info
) ?
1169 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1171 if (prepare_to_extract (file_name
, typeflag
, &fun
))
1173 if (fun
&& (*fun
) (file_name
, typeflag
) && backup_option
)
1174 undo_last_backup ();
1181 /* Extract the symbolic links whose final extraction were delayed. */
1183 apply_delayed_symlinks (void)
1185 struct delayed_symlink
*ds
;
1187 for (ds
= delayed_symlink_head
; ds
; )
1189 struct string_list
*sources
= ds
->sources
;
1190 char const *valid_source
= 0;
1192 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1194 char const *source
= sources
->string
;
1197 /* Make sure the placeholder file is still there. If not,
1198 don't create a symlink, as the placeholder was probably
1199 removed by a later extraction. */
1200 if (lstat (source
, &st
) == 0
1201 && st
.st_dev
== ds
->dev
1202 && st
.st_ino
== ds
->ino
1203 && st
.st_mtime
== ds
->mtime
)
1205 /* Unlink the placeholder, then create a hard link if possible,
1206 a symbolic link otherwise. */
1207 if (unlink (source
) != 0)
1208 unlink_error (source
);
1209 else if (valid_source
&& link (valid_source
, source
) == 0)
1211 else if (symlink (ds
->target
, source
) != 0)
1212 symlink_error (ds
->target
, source
);
1215 valid_source
= source
;
1216 st
.st_uid
= ds
->uid
;
1217 st
.st_gid
= ds
->gid
;
1218 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1223 for (sources
= ds
->sources
; sources
; )
1225 struct string_list
*next
= sources
->next
;
1231 struct delayed_symlink
*next
= ds
->next
;
1237 delayed_symlink_head
= 0;
1240 /* Finish the extraction of an archive. */
1242 extract_finish (void)
1244 /* First, fix the status of ordinary directories that need fixing. */
1245 apply_nonancestor_delayed_set_stat ("", 0);
1247 /* Then, apply delayed symlinks, so that they don't affect delayed
1248 directory status-setting for ordinary directories. */
1249 apply_delayed_symlinks ();
1251 /* Finally, fix the status of directories that are ancestors
1252 of delayed symlinks. */
1253 apply_nonancestor_delayed_set_stat ("", 1);
1260 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1267 error (0, 0, "%s", _("memory exhausted"));