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_LINKS is nonzero and [^D]
52 represents an element where AFTER_LINKS 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 links whose creation we have delayed. */
73 /* The next delayed link in the list. */
74 struct delayed_link
*next
;
76 /* The device, inode number and last-modified time of the placeholder. */
81 /* True if the link is symbolic. */
84 /* The desired owner and group of the link, if it is a symlink. */
88 /* A list of sources for this link. The sources are all to be
89 hard-linked together. */
90 struct string_list
*sources
;
92 /* The desired target of the desired link. */
96 static struct delayed_link
*delayed_link_head
;
100 struct string_list
*next
;
104 /* Set up to extract files. */
108 we_are_root
= geteuid () == 0;
109 same_permissions_option
+= we_are_root
;
110 same_owner_option
+= we_are_root
;
112 /* Save 'root device' to avoid purging mount points.
113 FIXME: Should the same be done after handling -C option ? */
114 if (one_file_system_option
)
117 char *dir
= xgetcwd ();
119 if (deref_stat (true, dir
, &st
))
122 root_device
= st
.st_dev
;
125 /* Option -p clears the kernel umask, so it does not affect proper
126 restoration of file permissions. New intermediate directories will
127 comply with umask at start of program. */
129 newdir_umask
= umask (0);
130 if (0 < same_permissions_option
)
134 umask (newdir_umask
); /* restore the kernel umask */
135 current_umask
= newdir_umask
;
139 /* If restoring permissions, restore the mode for FILE_NAME from
140 information given in *STAT_INFO (where *CUR_INFO gives
141 the current status if CUR_INFO is nonzero); otherwise invert the
142 INVERT_PERMISSIONS bits from the file's current permissions.
143 PERMSTATUS specifies the status of the file's permissions.
144 TYPEFLAG specifies the type of the file. */
146 set_mode (char const *file_name
,
147 struct stat
const *stat_info
,
148 struct stat
const *cur_info
,
149 mode_t invert_permissions
, enum permstatus permstatus
,
154 if (0 < same_permissions_option
155 && permstatus
!= INTERDIR_PERMSTATUS
)
157 mode
= stat_info
->st_mode
;
159 /* If we created the file and it has a usual mode, then its mode
160 is normally set correctly already. But on many hosts, some
161 directories inherit the setgid bits from their parents, so we
162 we must set directories' modes explicitly. */
163 if (permstatus
== ARCHIVED_PERMSTATUS
164 && ! (mode
& ~ MODE_RWX
)
165 && typeflag
!= DIRTYPE
166 && typeflag
!= GNUTYPE_DUMPDIR
)
169 else if (! invert_permissions
)
173 /* We must inspect a directory's current permissions, since the
174 directory may have inherited its setgid bit from its parent.
176 INVERT_PERMISSIONS happens to be nonzero only for directories
177 that we created, so there's no point optimizing this code for
182 if (stat (file_name
, &st
) != 0)
184 stat_error (file_name
);
189 mode
= cur_info
->st_mode
^ invert_permissions
;
192 if (chmod (file_name
, mode
) != 0)
193 chmod_error_details (file_name
, mode
);
196 /* Check time after successfully setting FILE_NAME's time stamp to T. */
198 check_time (char const *file_name
, struct timespec t
)
201 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
202 file_name
, tartime (t
, true)));
203 else if (timespec_lt (start_time
, t
))
207 if (timespec_lt (now
, t
))
209 unsigned long int ds
= t
.tv_sec
- now
.tv_sec
;
210 int dns
= t
.tv_nsec
- now
.tv_nsec
;
211 char dnsbuf
[sizeof ".FFFFFFFFF"];
217 code_ns_fraction (dns
, dnsbuf
);
218 WARN ((0, 0, _("%s: time stamp %s is %lu%s s in the future"),
219 file_name
, tartime (t
, true), ds
, dnsbuf
));
224 /* Restore stat attributes (owner, group, mode and times) for
225 FILE_NAME, using information given in *STAT_INFO.
226 If CUR_INFO is nonzero, *CUR_INFO is the
227 file's currernt status.
228 If not restoring permissions, invert the
229 INVERT_PERMISSIONS bits from the file's current permissions.
230 PERMSTATUS specifies the status of the file's permissions.
231 TYPEFLAG specifies the type of the file. */
233 /* FIXME: About proper restoration of symbolic link attributes, we still do
234 not have it right. Pretesters' reports tell us we need further study and
235 probably more configuration. For now, just use lchown if it exists, and
236 punt for the rest. Sigh! */
239 set_stat (char const *file_name
,
240 struct stat
const *stat_info
,
241 struct stat
const *cur_info
,
242 mode_t invert_permissions
, enum permstatus permstatus
,
245 if (typeflag
!= SYMTYPE
)
247 /* We do the utime before the chmod because some versions of utime are
248 broken and trash the modes of the file. */
250 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
252 /* We set the accessed time to `now', which is really the time we
253 started extracting files, unless incremental_option is used, in
254 which case .st_atime is used. */
256 /* FIXME: incremental_option should set ctime too, but how? */
258 struct timespec ts
[2];
259 ts
[0] = incremental_option
? get_stat_atime (stat_info
) : start_time
;
260 ts
[1] = get_stat_mtime (stat_info
);
262 if (utimens (file_name
, ts
) != 0)
263 utime_error (file_name
);
266 check_time (file_name
, ts
[0]);
267 check_time (file_name
, ts
[1]);
271 /* Some systems allow non-root users to give files away. Once this
272 done, it is not possible anymore to change file permissions, so we
273 have to set permissions prior to possibly giving files away. */
275 set_mode (file_name
, stat_info
, cur_info
,
276 invert_permissions
, permstatus
, typeflag
);
279 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
281 /* When lchown exists, it should be used to change the attributes of
282 the symbolic link itself. In this case, a mere chown would change
283 the attributes of the file the symbolic link is pointing to, and
284 should be avoided. */
286 if (typeflag
== SYMTYPE
)
289 if (lchown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
290 chown_error_details (file_name
,
291 stat_info
->st_uid
, stat_info
->st_gid
);
296 if (chown (file_name
, stat_info
->st_uid
, stat_info
->st_gid
) < 0)
297 chown_error_details (file_name
,
298 stat_info
->st_uid
, stat_info
->st_gid
);
300 /* On a few systems, and in particular, those allowing to give files
301 away, changing the owner or group destroys the suid or sgid bits.
302 So let's attempt setting these bits once more. */
303 if (stat_info
->st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
304 set_mode (file_name
, stat_info
, 0,
305 invert_permissions
, permstatus
, typeflag
);
310 /* Remember to restore stat attributes (owner, group, mode and times)
311 for the directory FILE_NAME, using information given in *STAT_INFO,
312 once we stop extracting files into that directory.
313 If not restoring permissions, remember to invert the
314 INVERT_PERMISSIONS bits from the file's current permissions.
315 PERMSTATUS specifies the status of the file's permissions. */
317 delay_set_stat (char const *file_name
, struct stat
const *stat_info
,
318 mode_t invert_permissions
, enum permstatus permstatus
)
320 size_t file_name_len
= strlen (file_name
);
321 struct delayed_set_stat
*data
=
322 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
323 + file_name_len
+ 1);
324 data
->file_name_len
= file_name_len
;
325 strcpy (data
->file_name
, file_name
);
326 data
->invert_permissions
= invert_permissions
;
327 data
->permstatus
= permstatus
;
328 data
->after_links
= 0;
329 data
->stat_info
= *stat_info
;
330 data
->next
= delayed_set_stat_head
;
331 delayed_set_stat_head
= data
;
334 /* Update the delayed_set_stat info for an intermediate directory
335 created within the file name of DIR. The intermediate directory turned
336 out to be the same as this directory, e.g. due to ".." or symbolic
337 links. *DIR_STAT_INFO is the status of the directory. */
339 repair_delayed_set_stat (char const *dir
,
340 struct stat
const *dir_stat_info
)
342 struct delayed_set_stat
*data
;
343 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
346 if (stat (data
->file_name
, &st
) != 0)
348 stat_error (data
->file_name
);
352 if (st
.st_dev
== dir_stat_info
->st_dev
353 && st
.st_ino
== dir_stat_info
->st_ino
)
355 data
->stat_info
= current_stat_info
.stat
;
356 data
->invert_permissions
=
357 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
358 data
->permstatus
= ARCHIVED_PERMSTATUS
;
363 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
364 quotearg_colon (dir
)));
367 /* After a file/link/directory creation has failed, see if
368 it's because some required directory was not present, and if so,
369 create all required directories. Return non-zero if a directory
372 make_directories (char *file_name
)
374 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
375 char *cursor
; /* points into the file name */
376 int did_something
= 0; /* did we do anything yet? */
378 int invert_permissions
;
382 for (cursor
= cursor0
; *cursor
; cursor
++)
384 if (! ISSLASH (*cursor
))
387 /* Avoid mkdir of empty string, if leading or double '/'. */
389 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
392 /* Avoid mkdir where last part of file name is "." or "..". */
394 if (cursor
[-1] == '.'
395 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
396 || (cursor
[-2] == '.'
397 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
400 *cursor
= '\0'; /* truncate the name there */
401 mode
= MODE_RWX
& ~ newdir_umask
;
402 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
403 status
= mkdir (file_name
, mode
^ invert_permissions
);
407 /* Create a struct delayed_set_stat even if
408 invert_permissions is zero, because
409 repair_delayed_set_stat may need to update the struct. */
410 delay_set_stat (file_name
,
411 ¤t_stat_info
.stat
/* ignored */,
412 invert_permissions
, INTERDIR_PERMSTATUS
);
414 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
424 continue; /* Directory already exists. */
425 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
426 this. Reported by Warren Hyde
427 <Warren.Hyde@motorola.com> */
428 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
429 && access (file_name
, W_OK
) == 0)
432 /* Some other error in the mkdir. We return to the caller. */
436 return did_something
; /* tell them to retry if we made one */
440 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
444 if (stat (file_name
, &st
))
446 stat_warn (file_name
);
447 /* Be on the safe side: if the file does exist assume it is newer */
448 return errno
!= ENOENT
;
450 if (!S_ISDIR (st
.st_mode
)
451 && st
.st_mtime
>= tar_stat
->stat
.st_mtime
)
458 /* Attempt repairing what went wrong with the extraction. Delete an
459 already existing file or create missing intermediate directories.
460 Return nonzero if we somewhat increased our chances at a successful
461 extraction. errno is properly restored on zero return. */
463 maybe_recoverable (char *file_name
, int *interdir_made
)
473 /* Remove an old file, if the options allow this. */
475 switch (old_files_option
)
480 case KEEP_NEWER_FILES
:
481 if (file_newer_p (file_name
, ¤t_stat_info
))
488 case DEFAULT_OLD_FILES
:
489 case NO_OVERWRITE_DIR_OLD_FILES
:
490 case OVERWRITE_OLD_FILES
:
492 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
497 case UNLINK_FIRST_OLD_FILES
:
502 /* Attempt creating missing intermediate directories. */
503 if (! make_directories (file_name
))
512 /* Just say we can't do anything about it... */
518 /* Fix the statuses of all directories whose statuses need fixing, and
519 which are not ancestors of FILE_NAME. If AFTER_LINKS is
520 nonzero, do this for all such directories; otherwise, stop at the
521 first directory that is marked to be fixed up only after delayed
522 links are applied. */
524 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
526 size_t file_name_len
= strlen (file_name
);
527 bool check_for_renamed_directories
= 0;
529 while (delayed_set_stat_head
)
531 struct delayed_set_stat
*data
= delayed_set_stat_head
;
532 bool skip_this_one
= 0;
534 struct stat
const *cur_info
= 0;
536 check_for_renamed_directories
|= data
->after_links
;
538 if (after_links
< data
->after_links
539 || (data
->file_name_len
< file_name_len
540 && file_name
[data
->file_name_len
]
541 && (ISSLASH (file_name
[data
->file_name_len
])
542 || ISSLASH (file_name
[data
->file_name_len
- 1]))
543 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
546 if (check_for_renamed_directories
)
549 if (stat (data
->file_name
, &st
) != 0)
551 stat_error (data
->file_name
);
554 else if (! (st
.st_dev
== data
->stat_info
.st_dev
555 && (st
.st_ino
== data
->stat_info
.st_ino
)))
558 _("%s: Directory renamed before its status could be extracted"),
559 quotearg_colon (data
->file_name
)));
565 set_stat (data
->file_name
, &data
->stat_info
, cur_info
,
566 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
568 delayed_set_stat_head
= data
->next
;
575 /* Extractor functions for various member types */
578 extract_dir (char *file_name
, int typeflag
)
582 int interdir_made
= 0;
584 if (incremental_option
)
585 /* Read the entry and delete files that aren't listed in the archive. */
586 purge_directory (file_name
);
587 else if (typeflag
== GNUTYPE_DUMPDIR
)
590 mode
= (current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
)) & MODE_RWX
;
592 while ((status
= mkdir (file_name
, mode
)))
596 || old_files_option
== DEFAULT_OLD_FILES
597 || old_files_option
== OVERWRITE_OLD_FILES
))
600 if (stat (file_name
, &st
) == 0)
604 repair_delayed_set_stat (file_name
, &st
);
607 if (S_ISDIR (st
.st_mode
))
609 mode
= st
.st_mode
& ~ current_umask
;
616 if (maybe_recoverable (file_name
, &interdir_made
))
621 mkdir_error (file_name
);
628 || old_files_option
== DEFAULT_OLD_FILES
629 || old_files_option
== OVERWRITE_OLD_FILES
)
630 delay_set_stat (file_name
, ¤t_stat_info
.stat
,
631 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
633 ? ARCHIVED_PERMSTATUS
634 : UNKNOWN_PERMSTATUS
));
641 open_output_file (char *file_name
, int typeflag
)
644 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
645 | (old_files_option
== OVERWRITE_OLD_FILES
648 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
651 /* Contiguous files (on the Masscomp) have to specify the size in
652 the open call that creates them. */
654 if (typeflag
== CONTTYPE
)
655 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
657 fd
= open (file_name
, openflag
, mode
);
659 #else /* not O_CTG */
660 if (typeflag
== CONTTYPE
)
662 static int conttype_diagnosed
;
664 if (!conttype_diagnosed
)
666 conttype_diagnosed
= 1;
667 WARN ((0, 0, _("Extracting contiguous files as regular files")));
670 fd
= open (file_name
, openflag
, mode
);
672 #endif /* not O_CTG */
678 extract_file (char *file_name
, int typeflag
)
682 union block
*data_block
;
686 int interdir_made
= 0;
688 /* FIXME: deal with protection issues. */
690 if (to_stdout_option
)
692 else if (to_command_option
)
694 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
704 fd
= open_output_file (file_name
, typeflag
);
705 while (fd
< 0 && maybe_recoverable (file_name
, &interdir_made
));
709 open_error (file_name
);
714 if (current_stat_info
.is_sparse
)
715 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
717 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
719 if (multi_volume_option
)
721 assign_string (&save_name
, current_stat_info
.orig_file_name
);
722 save_totsize
= current_stat_info
.stat
.st_size
;
723 save_sizeleft
= size
;
726 /* Locate data, determine max length writeable, write it,
727 block that we have used the data, then check if the write
730 data_block
= find_next_block ();
733 ERROR ((0, 0, _("Unexpected EOF in archive")));
734 break; /* FIXME: What happens, then? */
737 written
= available_space_after (data_block
);
742 count
= full_write (fd
, data_block
->buffer
, written
);
745 set_next_block_after ((union block
*)
746 (data_block
->buffer
+ written
- 1));
747 if (count
!= written
)
749 if (!to_command_option
)
750 write_error_details (file_name
, count
, written
);
751 /* FIXME: shouldn't we restore from backup? */
758 if (multi_volume_option
)
759 assign_string (&save_name
, 0);
761 /* If writing to stdout, don't try to do anything to the filename;
762 it doesn't exist, or we don't want to touch it anyway. */
764 if (to_stdout_option
)
769 close_error (file_name
);
771 if (to_command_option
)
774 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0,
775 (old_files_option
== OVERWRITE_OLD_FILES
?
776 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
782 /* Create a placeholder file with name FILE_NAME, which will be
783 replaced after other extraction is done by a symbolic link if
784 IS_SYMLINK is true, and by a hard link otherwise. Set
785 *INTERDIR_MADE if an intermediate directory is made in the
789 create_placeholder_file (char *file_name
, bool is_symlink
, int *interdir_made
)
794 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
795 if (! maybe_recoverable (file_name
, interdir_made
))
799 open_error (file_name
);
800 else if (fstat (fd
, &st
) != 0)
802 stat_error (file_name
);
805 else if (close (fd
) != 0)
806 close_error (file_name
);
809 struct delayed_set_stat
*h
;
810 struct delayed_link
*p
=
811 xmalloc (offsetof (struct delayed_link
, target
)
812 + strlen (current_stat_info
.link_name
)
814 p
->next
= delayed_link_head
;
815 delayed_link_head
= p
;
818 p
->mtime
= st
.st_mtime
;
819 p
->is_symlink
= is_symlink
;
822 p
->uid
= current_stat_info
.stat
.st_uid
;
823 p
->gid
= current_stat_info
.stat
.st_gid
;
825 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
826 + strlen (file_name
) + 1);
827 p
->sources
->next
= 0;
828 strcpy (p
->sources
->string
, file_name
);
829 strcpy (p
->target
, current_stat_info
.link_name
);
831 h
= delayed_set_stat_head
;
832 if (h
&& ! h
->after_links
833 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
834 && ISSLASH (file_name
[h
->file_name_len
])
835 && (base_name (file_name
) == file_name
+ h
->file_name_len
+ 1))
841 if (stat (h
->file_name
, &st
) != 0)
842 stat_error (h
->file_name
);
845 h
->stat_info
.st_dev
= st
.st_dev
;
846 h
->stat_info
.st_ino
= st
.st_ino
;
849 while ((h
= h
->next
) && ! h
->after_links
);
859 extract_link (char *file_name
, int typeflag
)
861 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
,
862 true, absolute_names_option
);
863 int interdir_made
= 0;
865 if (! absolute_names_option
&& contains_dot_dot (link_name
))
866 return create_placeholder_file (file_name
, false, &interdir_made
);
870 struct stat st1
, st2
;
872 int status
= link (link_name
, file_name
);
877 struct delayed_link
*ds
= delayed_link_head
;
878 if (ds
&& lstat (link_name
, &st1
) == 0)
879 for (; ds
; ds
= ds
->next
)
880 if (ds
->dev
== st1
.st_dev
881 && ds
->ino
== st1
.st_ino
882 && ds
->mtime
== st1
.st_mtime
)
884 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
885 + strlen (file_name
) + 1);
886 strcpy (p
->string
, file_name
);
887 p
->next
= ds
->sources
;
893 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
894 || (lstat (link_name
, &st1
) == 0
895 && lstat (file_name
, &st2
) == 0
896 && st1
.st_dev
== st2
.st_dev
897 && st1
.st_ino
== st2
.st_ino
))
902 while (maybe_recoverable (file_name
, &interdir_made
));
904 if (!(incremental_option
&& errno
== EEXIST
))
906 link_error (link_name
, file_name
);
913 extract_symlink (char *file_name
, int typeflag
)
917 int interdir_made
= 0;
919 if (! absolute_names_option
920 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
921 || contains_dot_dot (current_stat_info
.link_name
)))
922 return create_placeholder_file (file_name
, true, &interdir_made
);
924 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
925 if (!maybe_recoverable (file_name
, &interdir_made
))
929 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, 0, SYMTYPE
);
931 symlink_error (current_stat_info
.link_name
, file_name
);
935 static int warned_once
;
940 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
942 return extract_link (file_name
, typeflag
);
946 #if S_IFCHR || S_IFBLK
948 extract_node (char *file_name
, int typeflag
)
951 int interdir_made
= 0;
954 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
955 current_stat_info
.stat
.st_rdev
);
956 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
959 mknod_error (file_name
);
961 set_stat (file_name
, ¤t_stat_info
.stat
, 0, 0, ARCHIVED_PERMSTATUS
, typeflag
);
966 #if HAVE_MKFIFO || defined mkfifo
968 extract_fifo (char *file_name
, int typeflag
)
971 int interdir_made
= 0;
973 while ((status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
)))
974 if (!maybe_recoverable (file_name
, &interdir_made
))
978 set_stat (file_name
, ¤t_stat_info
.stat
, NULL
, 0,
979 ARCHIVED_PERMSTATUS
, typeflag
);
981 mkfifo_error (file_name
);
987 extract_mangle_wrapper (char *file_name
, int typeflag
)
995 extract_failure (char *file_name
, int typeflag
)
1000 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1004 /* Prepare to extract a file. Find extractor function.
1005 Return zero if extraction should not proceed. */
1008 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1012 if (EXTRACT_OVER_PIPE
)
1015 /* Select the extractor */
1018 case GNUTYPE_SPARSE
:
1019 *fun
= extract_file
;
1026 /* Appears to be a file. But BSD tar uses the convention that a slash
1027 suffix means a directory. */
1028 if (current_stat_info
.had_trailing_slash
)
1032 *fun
= extract_file
;
1038 *fun
= extract_symlink
;
1042 *fun
= extract_link
;
1047 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1048 *fun
= extract_node
;
1054 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1055 *fun
= extract_node
;
1059 #if HAVE_MKFIFO || defined mkfifo
1061 *fun
= extract_fifo
;
1066 case GNUTYPE_DUMPDIR
:
1070 case GNUTYPE_VOLHDR
:
1072 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1077 *fun
= extract_mangle_wrapper
;
1080 case GNUTYPE_MULTIVOL
:
1082 _("%s: Cannot extract -- file is continued from another volume"),
1083 quotearg_colon (current_stat_info
.file_name
)));
1084 *fun
= extract_failure
;
1087 case GNUTYPE_LONGNAME
:
1088 case GNUTYPE_LONGLINK
:
1089 ERROR ((0, 0, _("Unexpected long name header")));
1090 *fun
= extract_failure
;
1095 _("%s: Unknown file type `%c', extracted as normal file"),
1096 quotearg_colon (file_name
), typeflag
));
1097 *fun
= extract_file
;
1100 /* Determine whether the extraction should proceed */
1104 switch (old_files_option
)
1106 case UNLINK_FIRST_OLD_FILES
:
1107 if (!remove_any_file (file_name
,
1108 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1109 : ORDINARY_REMOVE_OPTION
)
1110 && errno
&& errno
!= ENOENT
)
1112 unlink_error (file_name
);
1117 case KEEP_NEWER_FILES
:
1118 if (file_newer_p (file_name
, ¤t_stat_info
))
1120 WARN ((0, 0, _("Current %s is newer or same age"),
1121 quote (file_name
)));
1133 /* Extract a file from the archive. */
1135 extract_archive (void)
1139 tar_extractor_t fun
;
1141 set_next_block_after (current_header
);
1142 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1144 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
1150 /* Print the block from current_header and current_stat. */
1153 print_header (¤t_stat_info
, -1);
1155 file_name
= safer_name_suffix (current_stat_info
.file_name
,
1156 false, absolute_names_option
);
1157 if (strip_name_components
)
1159 size_t prefix_len
= stripped_prefix_len (file_name
, strip_name_components
);
1160 if (prefix_len
== (size_t) -1)
1165 file_name
+= prefix_len
;
1168 apply_nonancestor_delayed_set_stat (file_name
, 0);
1170 /* Take a safety backup of a previously existing file. */
1173 if (!maybe_backup_file (file_name
, 0))
1176 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1177 quotearg_colon (file_name
)));
1182 /* Extract the archive entry according to its type. */
1185 typeflag
= sparse_member_p (¤t_stat_info
) ?
1186 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1188 if (prepare_to_extract (file_name
, typeflag
, &fun
))
1190 if (fun
&& (*fun
) (file_name
, typeflag
) && backup_option
)
1191 undo_last_backup ();
1198 /* Extract the symbolic links whose final extraction were delayed. */
1200 apply_delayed_links (void)
1202 struct delayed_link
*ds
;
1204 for (ds
= delayed_link_head
; ds
; )
1206 struct string_list
*sources
= ds
->sources
;
1207 char const *valid_source
= 0;
1209 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1211 char const *source
= sources
->string
;
1214 /* Make sure the placeholder file is still there. If not,
1215 don't create a link, as the placeholder was probably
1216 removed by a later extraction. */
1217 if (lstat (source
, &st
) == 0
1218 && st
.st_dev
== ds
->dev
1219 && st
.st_ino
== ds
->ino
1220 && st
.st_mtime
== ds
->mtime
)
1222 /* Unlink the placeholder, then create a hard link if possible,
1223 a symbolic link otherwise. */
1224 if (unlink (source
) != 0)
1225 unlink_error (source
);
1226 else if (valid_source
&& link (valid_source
, source
) == 0)
1228 else if (!ds
->is_symlink
)
1230 if (link (ds
->target
, source
) != 0)
1231 link_error (ds
->target
, source
);
1233 else if (symlink (ds
->target
, source
) != 0)
1234 symlink_error (ds
->target
, source
);
1237 valid_source
= source
;
1238 st
.st_uid
= ds
->uid
;
1239 st
.st_gid
= ds
->gid
;
1240 set_stat (source
, &st
, 0, 0, 0, SYMTYPE
);
1245 for (sources
= ds
->sources
; sources
; )
1247 struct string_list
*next
= sources
->next
;
1253 struct delayed_link
*next
= ds
->next
;
1259 delayed_link_head
= 0;
1262 /* Finish the extraction of an archive. */
1264 extract_finish (void)
1266 /* First, fix the status of ordinary directories that need fixing. */
1267 apply_nonancestor_delayed_set_stat ("", 0);
1269 /* Then, apply delayed links, so that they don't affect delayed
1270 directory status-setting for ordinary directories. */
1271 apply_delayed_links ();
1273 /* Finally, fix the status of directories that are ancestors
1274 of delayed links. */
1275 apply_nonancestor_delayed_set_stat ("", 1);
1282 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1289 error (0, 0, "%s", _("memory exhausted"));