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
;
65 struct timespec atime
;
66 struct timespec mtime
;
68 mode_t invert_permissions
;
69 enum permstatus permstatus
;
74 static struct delayed_set_stat
*delayed_set_stat_head
;
76 /* List of links whose creation we have delayed. */
79 /* The next delayed link in the list. */
80 struct delayed_link
*next
;
82 /* The device, inode number and last-modified time of the placeholder. */
85 struct timespec mtime
;
87 /* True if the link is symbolic. */
90 /* The desired owner and group of the link, if it is a symlink. */
94 /* A list of sources for this link. 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_link
*delayed_link_head
;
106 struct string_list
*next
;
110 /* Set up to extract files. */
114 we_are_root
= geteuid () == 0;
115 same_permissions_option
+= we_are_root
;
116 same_owner_option
+= we_are_root
;
118 /* Save 'root device' to avoid purging mount points.
119 FIXME: Should the same be done after handling -C option ? */
120 if (one_file_system_option
)
123 char *dir
= xgetcwd ();
125 if (deref_stat (true, dir
, &st
))
128 root_device
= st
.st_dev
;
131 /* Option -p clears the kernel umask, so it does not affect proper
132 restoration of file permissions. New intermediate directories will
133 comply with umask at start of program. */
135 newdir_umask
= umask (0);
136 if (0 < same_permissions_option
)
140 umask (newdir_umask
); /* restore the kernel umask */
141 current_umask
= newdir_umask
;
145 /* If restoring permissions, restore the mode for FILE_NAME from
146 information given in *STAT_INFO (where *CUR_INFO gives
147 the current status if CUR_INFO is nonzero); otherwise invert the
148 INVERT_PERMISSIONS bits from the file's current permissions.
149 PERMSTATUS specifies the status of the file's permissions.
150 TYPEFLAG specifies the type of the file. */
152 set_mode (char const *file_name
,
153 struct stat
const *stat_info
,
154 struct stat
const *cur_info
,
155 mode_t invert_permissions
, enum permstatus permstatus
,
160 if (0 < same_permissions_option
161 && permstatus
!= INTERDIR_PERMSTATUS
)
163 mode
= stat_info
->st_mode
;
165 /* If we created the file and it has a usual mode, then its mode
166 is normally set correctly already. But on many hosts, some
167 directories inherit the setgid bits from their parents, so we
168 we must set directories' modes explicitly. */
169 if (permstatus
== ARCHIVED_PERMSTATUS
170 && ! (mode
& ~ MODE_RWX
)
171 && typeflag
!= DIRTYPE
172 && typeflag
!= GNUTYPE_DUMPDIR
)
175 else if (! invert_permissions
)
179 /* We must inspect a directory's current permissions, since the
180 directory may have inherited its setgid bit from its parent.
182 INVERT_PERMISSIONS happens to be nonzero only for directories
183 that we created, so there's no point optimizing this code for
188 if (stat (file_name
, &st
) != 0)
190 stat_error (file_name
);
195 mode
= cur_info
->st_mode
^ invert_permissions
;
198 if (chmod (file_name
, mode
) != 0)
199 chmod_error_details (file_name
, mode
);
202 /* Check time after successfully setting FILE_NAME's time stamp to T. */
204 check_time (char const *file_name
, struct timespec t
)
207 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
208 file_name
, tartime (t
, true)));
209 else if (timespec_cmp (start_time
, t
) < 0)
213 if (timespec_cmp (now
, t
) < 0)
215 char buf
[TIMESPEC_STRSIZE_BOUND
];
216 struct timespec diff
;
217 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
218 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
219 if (diff
.tv_nsec
< 0)
221 diff
.tv_nsec
+= BILLION
;
224 WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
225 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
230 /* Restore stat attributes (owner, group, mode and times) for
231 FILE_NAME, using information given in *ST.
232 If CUR_INFO is nonzero, *CUR_INFO is the
233 file's currernt status.
234 If not restoring permissions, invert the
235 INVERT_PERMISSIONS bits from the file's current permissions.
236 PERMSTATUS specifies the status of the file's permissions.
237 TYPEFLAG specifies the type of the file. */
239 /* FIXME: About proper restoration of symbolic link attributes, we still do
240 not have it right. Pretesters' reports tell us we need further study and
241 probably more configuration. For now, just use lchown if it exists, and
242 punt for the rest. Sigh! */
245 set_stat (char const *file_name
,
246 struct tar_stat_info
const *st
,
247 struct stat
const *cur_info
,
248 mode_t invert_permissions
, enum permstatus permstatus
,
251 if (typeflag
!= SYMTYPE
)
253 /* We do the utime before the chmod because some versions of utime are
254 broken and trash the modes of the file. */
256 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
258 /* We set the accessed time to `now', which is really the time we
259 started extracting files, unless incremental_option is used, in
260 which case .st_atime is used. */
262 /* FIXME: incremental_option should set ctime too, but how? */
264 struct timespec ts
[2];
265 ts
[0] = incremental_option
? st
->atime
: start_time
;
268 if (utimens (file_name
, ts
) != 0)
269 utime_error (file_name
);
272 check_time (file_name
, ts
[0]);
273 check_time (file_name
, ts
[1]);
277 /* Some systems allow non-root users to give files away. Once this
278 done, it is not possible anymore to change file permissions, so we
279 have to set permissions prior to possibly giving files away. */
281 set_mode (file_name
, &st
->stat
, cur_info
,
282 invert_permissions
, permstatus
, typeflag
);
285 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
287 /* When lchown exists, it should be used to change the attributes of
288 the symbolic link itself. In this case, a mere chown would change
289 the attributes of the file the symbolic link is pointing to, and
290 should be avoided. */
292 if (typeflag
== SYMTYPE
)
295 if (lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
) < 0)
296 chown_error_details (file_name
,
297 st
->stat
.st_uid
, st
->stat
.st_gid
);
302 if (chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
) < 0)
303 chown_error_details (file_name
,
304 st
->stat
.st_uid
, st
->stat
.st_gid
);
306 /* On a few systems, and in particular, those allowing to give files
307 away, changing the owner or group destroys the suid or sgid bits.
308 So let's attempt setting these bits once more. */
309 if (st
->stat
.st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
310 set_mode (file_name
, &st
->stat
, 0,
311 invert_permissions
, permstatus
, typeflag
);
316 /* Remember to restore stat attributes (owner, group, mode and times)
317 for the directory FILE_NAME, using information given in *ST,
318 once we stop extracting files into that directory.
319 If not restoring permissions, remember to invert the
320 INVERT_PERMISSIONS bits from the file's current permissions.
321 PERMSTATUS specifies the status of the file's permissions. */
323 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
324 mode_t invert_permissions
, enum permstatus permstatus
)
326 size_t file_name_len
= strlen (file_name
);
327 struct delayed_set_stat
*data
=
328 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
329 + file_name_len
+ 1);
330 data
->next
= delayed_set_stat_head
;
331 data
->dev
= st
->stat
.st_dev
;
332 data
->ino
= st
->stat
.st_ino
;
333 data
->mode
= st
->stat
.st_mode
;
334 data
->uid
= st
->stat
.st_uid
;
335 data
->gid
= st
->stat
.st_gid
;
336 data
->atime
= st
->atime
;
337 data
->mtime
= st
->mtime
;
338 data
->file_name_len
= file_name_len
;
339 data
->invert_permissions
= invert_permissions
;
340 data
->permstatus
= permstatus
;
341 data
->after_links
= 0;
342 strcpy (data
->file_name
, file_name
);
343 delayed_set_stat_head
= data
;
346 /* Update the delayed_set_stat info for an intermediate directory
347 created within the file name of DIR. The intermediate directory turned
348 out to be the same as this directory, e.g. due to ".." or symbolic
349 links. *DIR_STAT_INFO is the status of the directory. */
351 repair_delayed_set_stat (char const *dir
,
352 struct stat
const *dir_stat_info
)
354 struct delayed_set_stat
*data
;
355 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
358 if (stat (data
->file_name
, &st
) != 0)
360 stat_error (data
->file_name
);
364 if (st
.st_dev
== dir_stat_info
->st_dev
365 && st
.st_ino
== dir_stat_info
->st_ino
)
367 data
->dev
= current_stat_info
.stat
.st_dev
;
368 data
->ino
= current_stat_info
.stat
.st_ino
;
369 data
->mode
= current_stat_info
.stat
.st_mode
;
370 data
->uid
= current_stat_info
.stat
.st_uid
;
371 data
->gid
= current_stat_info
.stat
.st_gid
;
372 data
->atime
= current_stat_info
.atime
;
373 data
->mtime
= current_stat_info
.mtime
;
374 data
->invert_permissions
=
375 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
376 data
->permstatus
= ARCHIVED_PERMSTATUS
;
381 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
382 quotearg_colon (dir
)));
385 /* After a file/link/directory creation has failed, see if
386 it's because some required directory was not present, and if so,
387 create all required directories. Return non-zero if a directory
390 make_directories (char *file_name
)
392 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
393 char *cursor
; /* points into the file name */
394 int did_something
= 0; /* did we do anything yet? */
396 int invert_permissions
;
400 for (cursor
= cursor0
; *cursor
; cursor
++)
402 if (! ISSLASH (*cursor
))
405 /* Avoid mkdir of empty string, if leading or double '/'. */
407 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
410 /* Avoid mkdir where last part of file name is "." or "..". */
412 if (cursor
[-1] == '.'
413 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
414 || (cursor
[-2] == '.'
415 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
418 *cursor
= '\0'; /* truncate the name there */
419 mode
= MODE_RWX
& ~ newdir_umask
;
420 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
421 status
= mkdir (file_name
, mode
^ invert_permissions
);
425 /* Create a struct delayed_set_stat even if
426 invert_permissions is zero, because
427 repair_delayed_set_stat may need to update the struct. */
428 delay_set_stat (file_name
,
429 ¤t_stat_info
/* ignored */,
430 invert_permissions
, INTERDIR_PERMSTATUS
);
432 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
442 continue; /* Directory already exists. */
443 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
444 this. Reported by Warren Hyde
445 <Warren.Hyde@motorola.com> */
446 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
447 && access (file_name
, W_OK
) == 0)
450 /* Some other error in the mkdir. We return to the caller. */
454 return did_something
; /* tell them to retry if we made one */
458 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
462 if (stat (file_name
, &st
))
464 stat_warn (file_name
);
465 /* Be on the safe side: if the file does exist assume it is newer */
466 return errno
!= ENOENT
;
468 if (!S_ISDIR (st
.st_mode
)
469 && timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
476 /* Attempt repairing what went wrong with the extraction. Delete an
477 already existing file or create missing intermediate directories.
478 Return nonzero if we somewhat increased our chances at a successful
479 extraction. errno is properly restored on zero return. */
481 maybe_recoverable (char *file_name
, int *interdir_made
)
491 /* Remove an old file, if the options allow this. */
493 switch (old_files_option
)
498 case KEEP_NEWER_FILES
:
499 if (file_newer_p (file_name
, ¤t_stat_info
))
506 case DEFAULT_OLD_FILES
:
507 case NO_OVERWRITE_DIR_OLD_FILES
:
508 case OVERWRITE_OLD_FILES
:
510 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
515 case UNLINK_FIRST_OLD_FILES
:
520 /* Attempt creating missing intermediate directories. */
521 if (! make_directories (file_name
))
530 /* Just say we can't do anything about it... */
536 /* Fix the statuses of all directories whose statuses need fixing, and
537 which are not ancestors of FILE_NAME. If AFTER_LINKS is
538 nonzero, do this for all such directories; otherwise, stop at the
539 first directory that is marked to be fixed up only after delayed
540 links are applied. */
542 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
544 size_t file_name_len
= strlen (file_name
);
545 bool check_for_renamed_directories
= 0;
547 while (delayed_set_stat_head
)
549 struct delayed_set_stat
*data
= delayed_set_stat_head
;
550 bool skip_this_one
= 0;
552 struct stat
const *cur_info
= 0;
554 check_for_renamed_directories
|= data
->after_links
;
556 if (after_links
< data
->after_links
557 || (data
->file_name_len
< file_name_len
558 && file_name
[data
->file_name_len
]
559 && (ISSLASH (file_name
[data
->file_name_len
])
560 || ISSLASH (file_name
[data
->file_name_len
- 1]))
561 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
564 if (check_for_renamed_directories
)
567 if (stat (data
->file_name
, &st
) != 0)
569 stat_error (data
->file_name
);
572 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
575 _("%s: Directory renamed before its status could be extracted"),
576 quotearg_colon (data
->file_name
)));
583 struct tar_stat_info st
;
584 st
.stat
.st_mode
= data
->mode
;
585 st
.stat
.st_uid
= data
->uid
;
586 st
.stat
.st_gid
= data
->gid
;
587 st
.atime
= data
->atime
;
588 st
.mtime
= data
->mtime
;
589 set_stat (data
->file_name
, &st
, cur_info
,
590 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
593 delayed_set_stat_head
= data
->next
;
600 /* Extractor functions for various member types */
603 extract_dir (char *file_name
, int typeflag
)
607 int interdir_made
= 0;
609 if (incremental_option
)
610 /* Read the entry and delete files that aren't listed in the archive. */
611 purge_directory (file_name
);
612 else if (typeflag
== GNUTYPE_DUMPDIR
)
615 mode
= (current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
)) & MODE_RWX
;
617 while ((status
= mkdir (file_name
, mode
)))
621 || old_files_option
== DEFAULT_OLD_FILES
622 || old_files_option
== OVERWRITE_OLD_FILES
))
625 if (stat (file_name
, &st
) == 0)
629 repair_delayed_set_stat (file_name
, &st
);
632 if (S_ISDIR (st
.st_mode
))
634 mode
= st
.st_mode
& ~ current_umask
;
641 if (maybe_recoverable (file_name
, &interdir_made
))
646 mkdir_error (file_name
);
653 || old_files_option
== DEFAULT_OLD_FILES
654 || old_files_option
== OVERWRITE_OLD_FILES
)
655 delay_set_stat (file_name
, ¤t_stat_info
,
656 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
658 ? ARCHIVED_PERMSTATUS
659 : UNKNOWN_PERMSTATUS
));
666 open_output_file (char *file_name
, int typeflag
)
669 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
670 | (old_files_option
== OVERWRITE_OLD_FILES
673 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
676 /* Contiguous files (on the Masscomp) have to specify the size in
677 the open call that creates them. */
679 if (typeflag
== CONTTYPE
)
680 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
682 fd
= open (file_name
, openflag
, mode
);
684 #else /* not O_CTG */
685 if (typeflag
== CONTTYPE
)
687 static int conttype_diagnosed
;
689 if (!conttype_diagnosed
)
691 conttype_diagnosed
= 1;
692 WARN ((0, 0, _("Extracting contiguous files as regular files")));
695 fd
= open (file_name
, openflag
, mode
);
697 #endif /* not O_CTG */
703 extract_file (char *file_name
, int typeflag
)
707 union block
*data_block
;
711 int interdir_made
= 0;
713 /* FIXME: deal with protection issues. */
715 if (to_stdout_option
)
717 else if (to_command_option
)
719 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
729 fd
= open_output_file (file_name
, typeflag
);
730 while (fd
< 0 && maybe_recoverable (file_name
, &interdir_made
));
734 open_error (file_name
);
739 if (current_stat_info
.is_sparse
)
740 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
742 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
744 if (multi_volume_option
)
746 assign_string (&save_name
, current_stat_info
.orig_file_name
);
747 save_totsize
= current_stat_info
.stat
.st_size
;
748 save_sizeleft
= size
;
751 /* Locate data, determine max length writeable, write it,
752 block that we have used the data, then check if the write
755 data_block
= find_next_block ();
758 ERROR ((0, 0, _("Unexpected EOF in archive")));
759 break; /* FIXME: What happens, then? */
762 written
= available_space_after (data_block
);
767 count
= full_write (fd
, data_block
->buffer
, written
);
770 set_next_block_after ((union block
*)
771 (data_block
->buffer
+ written
- 1));
772 if (count
!= written
)
774 if (!to_command_option
)
775 write_error_details (file_name
, count
, written
);
776 /* FIXME: shouldn't we restore from backup? */
783 if (multi_volume_option
)
784 assign_string (&save_name
, 0);
786 /* If writing to stdout, don't try to do anything to the filename;
787 it doesn't exist, or we don't want to touch it anyway. */
789 if (to_stdout_option
)
794 close_error (file_name
);
796 if (to_command_option
)
799 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
800 (old_files_option
== OVERWRITE_OLD_FILES
?
801 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
807 /* Create a placeholder file with name FILE_NAME, which will be
808 replaced after other extraction is done by a symbolic link if
809 IS_SYMLINK is true, and by a hard link otherwise. Set
810 *INTERDIR_MADE if an intermediate directory is made in the
814 create_placeholder_file (char *file_name
, bool is_symlink
, int *interdir_made
)
819 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
820 if (! maybe_recoverable (file_name
, interdir_made
))
824 open_error (file_name
);
825 else if (fstat (fd
, &st
) != 0)
827 stat_error (file_name
);
830 else if (close (fd
) != 0)
831 close_error (file_name
);
834 struct delayed_set_stat
*h
;
835 struct delayed_link
*p
=
836 xmalloc (offsetof (struct delayed_link
, target
)
837 + strlen (current_stat_info
.link_name
)
839 p
->next
= delayed_link_head
;
840 delayed_link_head
= p
;
843 p
->mtime
= get_stat_mtime (&st
);
844 p
->is_symlink
= is_symlink
;
847 p
->uid
= current_stat_info
.stat
.st_uid
;
848 p
->gid
= current_stat_info
.stat
.st_gid
;
850 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
851 + strlen (file_name
) + 1);
852 p
->sources
->next
= 0;
853 strcpy (p
->sources
->string
, file_name
);
854 strcpy (p
->target
, current_stat_info
.link_name
);
856 h
= delayed_set_stat_head
;
857 if (h
&& ! h
->after_links
858 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
859 && ISSLASH (file_name
[h
->file_name_len
])
860 && (base_name (file_name
) == file_name
+ h
->file_name_len
+ 1))
866 if (stat (h
->file_name
, &st
) != 0)
867 stat_error (h
->file_name
);
874 while ((h
= h
->next
) && ! h
->after_links
);
884 extract_link (char *file_name
, int typeflag
)
886 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
,
887 true, absolute_names_option
);
888 int interdir_made
= 0;
890 if (! absolute_names_option
&& contains_dot_dot (link_name
))
891 return create_placeholder_file (file_name
, false, &interdir_made
);
895 struct stat st1
, st2
;
897 int status
= link (link_name
, file_name
);
902 struct delayed_link
*ds
= delayed_link_head
;
903 if (ds
&& lstat (link_name
, &st1
) == 0)
904 for (; ds
; ds
= ds
->next
)
905 if (ds
->dev
== st1
.st_dev
906 && ds
->ino
== st1
.st_ino
907 && timespec_cmp (ds
->mtime
, get_stat_mtime (&st1
)) == 0)
909 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
910 + strlen (file_name
) + 1);
911 strcpy (p
->string
, file_name
);
912 p
->next
= ds
->sources
;
918 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
919 || (lstat (link_name
, &st1
) == 0
920 && lstat (file_name
, &st2
) == 0
921 && st1
.st_dev
== st2
.st_dev
922 && st1
.st_ino
== st2
.st_ino
))
927 while (maybe_recoverable (file_name
, &interdir_made
));
929 if (!(incremental_option
&& errno
== EEXIST
))
931 link_error (link_name
, file_name
);
938 extract_symlink (char *file_name
, int typeflag
)
942 int interdir_made
= 0;
944 if (! absolute_names_option
945 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
946 || contains_dot_dot (current_stat_info
.link_name
)))
947 return create_placeholder_file (file_name
, true, &interdir_made
);
949 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
950 if (!maybe_recoverable (file_name
, &interdir_made
))
954 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
956 symlink_error (current_stat_info
.link_name
, file_name
);
960 static int warned_once
;
965 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
967 return extract_link (file_name
, typeflag
);
971 #if S_IFCHR || S_IFBLK
973 extract_node (char *file_name
, int typeflag
)
976 int interdir_made
= 0;
979 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
980 current_stat_info
.stat
.st_rdev
);
981 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
984 mknod_error (file_name
);
986 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
987 ARCHIVED_PERMSTATUS
, typeflag
);
992 #if HAVE_MKFIFO || defined mkfifo
994 extract_fifo (char *file_name
, int typeflag
)
997 int interdir_made
= 0;
999 while ((status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
)))
1000 if (!maybe_recoverable (file_name
, &interdir_made
))
1004 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
1005 ARCHIVED_PERMSTATUS
, typeflag
);
1007 mkfifo_error (file_name
);
1013 extract_mangle_wrapper (char *file_name
, int typeflag
)
1021 extract_failure (char *file_name
, int typeflag
)
1026 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1030 /* Prepare to extract a file. Find extractor function.
1031 Return zero if extraction should not proceed. */
1034 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1038 if (EXTRACT_OVER_PIPE
)
1041 /* Select the extractor */
1044 case GNUTYPE_SPARSE
:
1045 *fun
= extract_file
;
1052 /* Appears to be a file. But BSD tar uses the convention that a slash
1053 suffix means a directory. */
1054 if (current_stat_info
.had_trailing_slash
)
1058 *fun
= extract_file
;
1064 *fun
= extract_symlink
;
1068 *fun
= extract_link
;
1073 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1074 *fun
= extract_node
;
1080 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1081 *fun
= extract_node
;
1085 #if HAVE_MKFIFO || defined mkfifo
1087 *fun
= extract_fifo
;
1092 case GNUTYPE_DUMPDIR
:
1096 case GNUTYPE_VOLHDR
:
1098 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1103 *fun
= extract_mangle_wrapper
;
1106 case GNUTYPE_MULTIVOL
:
1108 _("%s: Cannot extract -- file is continued from another volume"),
1109 quotearg_colon (current_stat_info
.file_name
)));
1110 *fun
= extract_failure
;
1113 case GNUTYPE_LONGNAME
:
1114 case GNUTYPE_LONGLINK
:
1115 ERROR ((0, 0, _("Unexpected long name header")));
1116 *fun
= extract_failure
;
1121 _("%s: Unknown file type `%c', extracted as normal file"),
1122 quotearg_colon (file_name
), typeflag
));
1123 *fun
= extract_file
;
1126 /* Determine whether the extraction should proceed */
1130 switch (old_files_option
)
1132 case UNLINK_FIRST_OLD_FILES
:
1133 if (!remove_any_file (file_name
,
1134 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1135 : ORDINARY_REMOVE_OPTION
)
1136 && errno
&& errno
!= ENOENT
)
1138 unlink_error (file_name
);
1143 case KEEP_NEWER_FILES
:
1144 if (file_newer_p (file_name
, ¤t_stat_info
))
1146 WARN ((0, 0, _("Current %s is newer or same age"),
1147 quote (file_name
)));
1159 /* Extract a file from the archive. */
1161 extract_archive (void)
1165 tar_extractor_t fun
;
1167 set_next_block_after (current_header
);
1168 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1170 if (interactive_option
&& !confirm ("extract", current_stat_info
.file_name
))
1176 /* Print the block from current_header and current_stat. */
1179 print_header (¤t_stat_info
, -1);
1181 file_name
= safer_name_suffix (current_stat_info
.file_name
,
1182 false, absolute_names_option
);
1183 if (strip_name_components
)
1185 size_t prefix_len
= stripped_prefix_len (file_name
, strip_name_components
);
1186 if (prefix_len
== (size_t) -1)
1191 file_name
+= prefix_len
;
1194 apply_nonancestor_delayed_set_stat (file_name
, 0);
1196 /* Take a safety backup of a previously existing file. */
1199 if (!maybe_backup_file (file_name
, 0))
1202 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1203 quotearg_colon (file_name
)));
1208 /* Extract the archive entry according to its type. */
1211 typeflag
= sparse_member_p (¤t_stat_info
) ?
1212 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1214 if (prepare_to_extract (file_name
, typeflag
, &fun
))
1216 if (fun
&& (*fun
) (file_name
, typeflag
) && backup_option
)
1217 undo_last_backup ();
1224 /* Extract the symbolic links whose final extraction were delayed. */
1226 apply_delayed_links (void)
1228 struct delayed_link
*ds
;
1230 for (ds
= delayed_link_head
; ds
; )
1232 struct string_list
*sources
= ds
->sources
;
1233 char const *valid_source
= 0;
1235 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1237 char const *source
= sources
->string
;
1240 /* Make sure the placeholder file is still there. If not,
1241 don't create a link, as the placeholder was probably
1242 removed by a later extraction. */
1243 if (lstat (source
, &st
) == 0
1244 && st
.st_dev
== ds
->dev
1245 && st
.st_ino
== ds
->ino
1246 && timespec_cmp (get_stat_mtime (&st
), ds
->mtime
) == 0)
1248 /* Unlink the placeholder, then create a hard link if possible,
1249 a symbolic link otherwise. */
1250 if (unlink (source
) != 0)
1251 unlink_error (source
);
1252 else if (valid_source
&& link (valid_source
, source
) == 0)
1254 else if (!ds
->is_symlink
)
1256 if (link (ds
->target
, source
) != 0)
1257 link_error (ds
->target
, source
);
1259 else if (symlink (ds
->target
, source
) != 0)
1260 symlink_error (ds
->target
, source
);
1263 struct tar_stat_info st1
;
1264 st1
.stat
.st_uid
= ds
->uid
;
1265 st1
.stat
.st_gid
= ds
->gid
;
1266 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1267 valid_source
= source
;
1272 for (sources
= ds
->sources
; sources
; )
1274 struct string_list
*next
= sources
->next
;
1280 struct delayed_link
*next
= ds
->next
;
1286 delayed_link_head
= 0;
1289 /* Finish the extraction of an archive. */
1291 extract_finish (void)
1293 /* First, fix the status of ordinary directories that need fixing. */
1294 apply_nonancestor_delayed_set_stat ("", 0);
1296 /* Then, apply delayed links, so that they don't affect delayed
1297 directory status-setting for ordinary directories. */
1298 apply_delayed_links ();
1300 /* Finally, fix the status of directories that are ancestors
1301 of delayed links. */
1302 apply_nonancestor_delayed_set_stat ("", 1);
1309 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1316 error (0, 0, "%s", _("memory exhausted"));