1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2010 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 3, 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,
41 except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
44 /* This is an intermediate directory; the archive did not specify
49 /* List of directories whose statuses we need to extract after we've
50 finished extracting their subsidiary files. If you consider each
51 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
52 represents an element where AFTER_LINKS is nonzero and [^D]
53 represents an element where AFTER_LINKS is zero, then the head
54 of the subsequence has the longest name, and each non-head element
55 in the prefix is an ancestor (in the directory hierarchy) of the
58 struct delayed_set_stat
60 struct delayed_set_stat
*next
;
66 struct timespec atime
;
67 struct timespec mtime
;
69 mode_t invert_permissions
;
70 enum permstatus permstatus
;
76 static struct delayed_set_stat
*delayed_set_stat_head
;
78 /* List of links whose creation we have delayed. */
81 /* The next delayed link in the list. */
82 struct delayed_link
*next
;
84 /* The device, inode number and ctime of the placeholder. Use
85 ctime, not mtime, to make false matches less likely if some
86 other process removes the placeholder. */
89 struct timespec ctime
;
91 /* True if the link is symbolic. */
94 /* The desired owner and group of the link, if it is a symlink. */
98 /* The directory that the sources and target are relative to. */
101 /* A list of sources for this link. The sources are all to be
102 hard-linked together. */
103 struct string_list
*sources
;
105 /* The desired target of the desired link. */
109 static struct delayed_link
*delayed_link_head
;
113 struct string_list
*next
;
117 /* Set up to extract files. */
121 we_are_root
= geteuid () == 0;
122 same_permissions_option
+= we_are_root
;
123 same_owner_option
+= we_are_root
;
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 /* Use fchmod if possible, chmod otherwise. */
141 fdchmod (char const *file
, int fd
, mode_t mode
)
145 return fchmod (fd
, mode
);
147 return chmod (file
, mode
);
150 /* Use fchown if possible, chown otherwise. */
152 fdchown (char const *file
, int fd
, uid_t uid
, gid_t gid
)
156 return fchown (fd
, uid
, gid
);
158 return chown (file
, uid
, gid
);
161 /* Use fstat if possible, stat otherwise. */
163 fdstat (char const *file
, int fd
, struct stat
*st
)
166 return fstat (fd
, st
);
167 return stat (file
, st
);
170 /* If restoring permissions, restore the mode for FILE_NAME from
171 information given in *STAT_INFO (where FD is a file descriptor
172 for the file if FD is nonnegative, and where *CUR_INFO gives
173 the current status if CUR_INFO is nonzero); otherwise invert the
174 INVERT_PERMISSIONS bits from the file's current permissions.
175 PERMSTATUS specifies the status of the file's permissions.
176 TYPEFLAG specifies the type of the file. */
178 set_mode (char const *file_name
,
179 struct stat
const *stat_info
,
180 int fd
, struct stat
const *cur_info
,
181 mode_t invert_permissions
, enum permstatus permstatus
,
187 if (0 < same_permissions_option
188 && permstatus
!= INTERDIR_PERMSTATUS
)
190 mode
= stat_info
->st_mode
;
192 /* If we created the file and it has a mode that we set already
193 with O_CREAT, then its mode is often set correctly already.
194 But if we are changing ownership, the mode's group and and
195 other permission bits were omitted originally, so it's less
196 likely that the mode is OK now. Also, on many hosts, some
197 directories inherit the setgid bits from their parents, so we
198 we must set directories' modes explicitly. */
199 if ((permstatus
== ARCHIVED_PERMSTATUS
200 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
201 && typeflag
!= DIRTYPE
202 && typeflag
!= GNUTYPE_DUMPDIR
)
205 else if (! invert_permissions
)
209 /* We must inspect a directory's current permissions, since the
210 directory may have inherited its setgid bit from its parent.
212 INVERT_PERMISSIONS happens to be nonzero only for directories
213 that we created, so there's no point optimizing this code for
218 if (fdstat (file_name
, fd
, &st
) != 0)
220 stat_error (file_name
);
225 mode
= cur_info
->st_mode
^ invert_permissions
;
228 chmod_errno
= fdchmod (file_name
, fd
, mode
) == 0 ? 0 : errno
;
229 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
) != 0)
231 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
232 setuid-root files would otherwise be a backdoor. See
233 http://opensolaris.org/jive/thread.jspa?threadID=95826
235 if (priv_set_restore_linkdir () == 0)
237 chmod_errno
= fdchmod (file_name
, fd
, mode
) == 0 ? 0 : errno
;
238 priv_set_remove_linkdir ();
244 chmod_error_details (file_name
, mode
);
248 /* Check time after successfully setting FILE_NAME's time stamp to T. */
250 check_time (char const *file_name
, struct timespec t
)
253 WARNOPT (WARN_TIMESTAMP
,
254 (0, 0, _("%s: implausibly old time stamp %s"),
255 file_name
, tartime (t
, true)));
256 else if (timespec_cmp (volume_start_time
, t
) < 0)
260 if (timespec_cmp (now
, t
) < 0)
262 char buf
[TIMESPEC_STRSIZE_BOUND
];
263 struct timespec diff
;
264 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
265 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
266 if (diff
.tv_nsec
< 0)
268 diff
.tv_nsec
+= BILLION
;
271 WARNOPT (WARN_TIMESTAMP
,
272 (0, 0, _("%s: time stamp %s is %s s in the future"),
273 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
278 /* Restore stat attributes (owner, group, mode and times) for
279 FILE_NAME, using information given in *ST.
280 If FD is nonnegative, it is a file descriptor for the file.
281 If CUR_INFO is nonzero, *CUR_INFO is the
282 file's current status.
283 If not restoring permissions, invert the
284 INVERT_PERMISSIONS bits from the file's current permissions.
285 PERMSTATUS specifies the status of the file's permissions.
286 TYPEFLAG specifies the type of the file. */
288 /* FIXME: About proper restoration of symbolic link attributes, we still do
289 not have it right. Pretesters' reports tell us we need further study and
290 probably more configuration. For now, just use lchown if it exists, and
291 punt for the rest. Sigh! */
294 set_stat (char const *file_name
,
295 struct tar_stat_info
const *st
,
296 int fd
, struct stat
const *cur_info
,
297 mode_t invert_permissions
, enum permstatus permstatus
,
300 if (typeflag
!= SYMTYPE
)
302 /* We do the utime before the chmod because some versions of utime are
303 broken and trash the modes of the file. */
305 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
307 /* We set the accessed time to `now', which is really the time we
308 started extracting files, unless incremental_option is used, in
309 which case .st_atime is used. */
311 /* FIXME: incremental_option should set ctime too, but how? */
313 struct timespec ts
[2];
314 if (incremental_option
)
320 if (fdutimens (file_name
, fd
, ts
) != 0)
321 utime_error (file_name
);
324 check_time (file_name
, ts
[0]);
325 check_time (file_name
, ts
[1]);
329 /* Some systems allow non-root users to give files away. Once this
330 done, it is not possible anymore to change file permissions.
331 However, setting file permissions now would be incorrect, since
332 they would apply to the wrong user, and there would be a race
333 condition. So, don't use systems that allow non-root users to
337 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
339 /* When lchown exists, it should be used to change the attributes of
340 the symbolic link itself. In this case, a mere chown would change
341 the attributes of the file the symbolic link is pointing to, and
342 should be avoided. */
343 int chown_result
= 1;
345 if (typeflag
== SYMTYPE
)
348 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
353 chown_result
= fdchown (file_name
, fd
,
354 st
->stat
.st_uid
, st
->stat
.st_gid
);
357 if (chown_result
== 0)
359 /* Changing the owner can flip st_mode bits in some cases, so
360 ignore cur_info if it might be obsolete now. */
362 && cur_info
->st_mode
& S_IXUGO
363 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
366 else if (chown_result
< 0)
367 chown_error_details (file_name
,
368 st
->stat
.st_uid
, st
->stat
.st_gid
);
371 if (typeflag
!= SYMTYPE
)
372 set_mode (file_name
, &st
->stat
, fd
, cur_info
,
373 invert_permissions
, permstatus
, typeflag
);
376 /* Remember to restore stat attributes (owner, group, mode and times)
377 for the directory FILE_NAME, using information given in *ST,
378 once we stop extracting files into that directory.
379 If not restoring permissions, remember to invert the
380 INVERT_PERMISSIONS bits from the file's current permissions.
381 PERMSTATUS specifies the status of the file's permissions.
383 NOTICE: this works only if the archive has usual member order, i.e.
384 directory, then the files in that directory. Incremental archive have
385 somewhat reversed order: first go subdirectories, then all other
386 members. To help cope with this case the variable
387 delay_directory_restore_option is set by prepare_to_extract.
389 If an archive was explicitely created so that its member order is
390 reversed, some directory timestamps can be restored incorrectly,
392 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
395 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
396 mode_t invert_permissions
, enum permstatus permstatus
)
398 size_t file_name_len
= strlen (file_name
);
399 struct delayed_set_stat
*data
=
400 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
401 + file_name_len
+ 1);
402 data
->next
= delayed_set_stat_head
;
403 data
->dev
= st
->stat
.st_dev
;
404 data
->ino
= st
->stat
.st_ino
;
405 data
->mode
= st
->stat
.st_mode
;
406 data
->uid
= st
->stat
.st_uid
;
407 data
->gid
= st
->stat
.st_gid
;
408 data
->atime
= st
->atime
;
409 data
->mtime
= st
->mtime
;
410 data
->file_name_len
= file_name_len
;
411 data
->invert_permissions
= invert_permissions
;
412 data
->permstatus
= permstatus
;
413 data
->after_links
= 0;
414 data
->change_dir
= chdir_current
;
415 strcpy (data
->file_name
, file_name
);
416 delayed_set_stat_head
= data
;
419 /* Update the delayed_set_stat info for an intermediate directory
420 created within the file name of DIR. The intermediate directory turned
421 out to be the same as this directory, e.g. due to ".." or symbolic
422 links. *DIR_STAT_INFO is the status of the directory. */
424 repair_delayed_set_stat (char const *dir
,
425 struct stat
const *dir_stat_info
)
427 struct delayed_set_stat
*data
;
428 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
431 if (stat (data
->file_name
, &st
) != 0)
433 stat_error (data
->file_name
);
437 if (st
.st_dev
== dir_stat_info
->st_dev
438 && st
.st_ino
== dir_stat_info
->st_ino
)
440 data
->dev
= current_stat_info
.stat
.st_dev
;
441 data
->ino
= current_stat_info
.stat
.st_ino
;
442 data
->mode
= current_stat_info
.stat
.st_mode
;
443 data
->uid
= current_stat_info
.stat
.st_uid
;
444 data
->gid
= current_stat_info
.stat
.st_gid
;
445 data
->atime
= current_stat_info
.atime
;
446 data
->mtime
= current_stat_info
.mtime
;
447 data
->invert_permissions
=
448 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
449 & MODE_RWX
& ~ current_umask
);
450 data
->permstatus
= ARCHIVED_PERMSTATUS
;
455 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
456 quotearg_colon (dir
)));
459 /* After a file/link/directory creation has failed, see if
460 it's because some required directory was not present, and if so,
461 create all required directories. Return non-zero if a directory
464 make_directories (char *file_name
)
466 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
467 char *cursor
; /* points into the file name */
468 int did_something
= 0; /* did we do anything yet? */
470 int invert_permissions
;
473 for (cursor
= cursor0
; *cursor
; cursor
++)
475 if (! ISSLASH (*cursor
))
478 /* Avoid mkdir of empty string, if leading or double '/'. */
480 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
483 /* Avoid mkdir where last part of file name is "." or "..". */
485 if (cursor
[-1] == '.'
486 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
487 || (cursor
[-2] == '.'
488 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
491 *cursor
= '\0'; /* truncate the name there */
492 mode
= MODE_RWX
& ~ newdir_umask
;
493 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
494 status
= mkdir (file_name
, mode
^ invert_permissions
);
498 /* Create a struct delayed_set_stat even if
499 invert_permissions is zero, because
500 repair_delayed_set_stat may need to update the struct. */
501 delay_set_stat (file_name
,
503 invert_permissions
, INTERDIR_PERMSTATUS
);
505 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
515 continue; /* Directory already exists. */
516 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
517 this. Reported by Warren Hyde
518 <Warren.Hyde@motorola.com> */
519 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
520 && access (file_name
, W_OK
) == 0)
523 /* Some other error in the mkdir. We return to the caller. */
527 return did_something
; /* tell them to retry if we made one */
531 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
535 if (stat (file_name
, &st
))
539 stat_warn (file_name
);
540 /* Be on the safe side: if the file does exist assume it is newer */
545 if (!S_ISDIR (st
.st_mode
)
546 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
555 #define RECOVER_SKIP 2
557 /* Attempt repairing what went wrong with the extraction. Delete an
558 already existing file or create missing intermediate directories.
559 Return RECOVER_OK if we somewhat increased our chances at a successful
560 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
561 caller should skip extraction of that member. The value of errno is
562 properly restored on returning RECOVER_NO. */
565 maybe_recoverable (char *file_name
, bool *interdir_made
)
575 /* Remove an old file, if the options allow this. */
577 switch (old_files_option
)
582 case KEEP_NEWER_FILES
:
583 if (file_newer_p (file_name
, ¤t_stat_info
))
590 case DEFAULT_OLD_FILES
:
591 case NO_OVERWRITE_DIR_OLD_FILES
:
592 case OVERWRITE_OLD_FILES
:
594 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
596 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
599 case UNLINK_FIRST_OLD_FILES
:
604 /* Attempt creating missing intermediate directories. */
605 if (! make_directories (file_name
))
610 *interdir_made
= true;
614 /* Just say we can't do anything about it... */
620 /* Fix the statuses of all directories whose statuses need fixing, and
621 which are not ancestors of FILE_NAME. If AFTER_LINKS is
622 nonzero, do this for all such directories; otherwise, stop at the
623 first directory that is marked to be fixed up only after delayed
624 links are applied. */
626 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
628 size_t file_name_len
= strlen (file_name
);
629 bool check_for_renamed_directories
= 0;
631 while (delayed_set_stat_head
)
633 struct delayed_set_stat
*data
= delayed_set_stat_head
;
634 bool skip_this_one
= 0;
636 struct stat
const *cur_info
= 0;
638 check_for_renamed_directories
|= data
->after_links
;
640 if (after_links
< data
->after_links
641 || (data
->file_name_len
< file_name_len
642 && file_name
[data
->file_name_len
]
643 && (ISSLASH (file_name
[data
->file_name_len
])
644 || ISSLASH (file_name
[data
->file_name_len
- 1]))
645 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
648 chdir_do (data
->change_dir
);
650 if (check_for_renamed_directories
)
653 if (stat (data
->file_name
, &st
) != 0)
655 stat_error (data
->file_name
);
658 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
661 _("%s: Directory renamed before its status could be extracted"),
662 quotearg_colon (data
->file_name
)));
669 struct tar_stat_info sb
;
670 sb
.stat
.st_mode
= data
->mode
;
671 sb
.stat
.st_uid
= data
->uid
;
672 sb
.stat
.st_gid
= data
->gid
;
673 sb
.atime
= data
->atime
;
674 sb
.mtime
= data
->mtime
;
675 set_stat (data
->file_name
, &sb
, -1, cur_info
,
676 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
679 delayed_set_stat_head
= data
->next
;
686 /* Extractor functions for various member types */
689 extract_dir (char *file_name
, int typeflag
)
693 bool interdir_made
= false;
695 /* Save 'root device' to avoid purging mount points. */
696 if (one_file_system_option
&& root_device
== 0)
700 if (stat (".", &st
) != 0)
703 root_device
= st
.st_dev
;
706 if (incremental_option
)
707 /* Read the entry and delete files that aren't listed in the archive. */
708 purge_directory (file_name
);
709 else if (typeflag
== GNUTYPE_DUMPDIR
)
712 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
713 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
716 while ((status
= mkdir (file_name
, mode
)))
720 || old_files_option
== DEFAULT_OLD_FILES
721 || old_files_option
== OVERWRITE_OLD_FILES
))
724 if (stat (file_name
, &st
) == 0)
728 repair_delayed_set_stat (file_name
, &st
);
731 if (S_ISDIR (st
.st_mode
))
741 switch (maybe_recoverable (file_name
, &interdir_made
))
752 mkdir_error (file_name
);
761 || old_files_option
== DEFAULT_OLD_FILES
762 || old_files_option
== OVERWRITE_OLD_FILES
)
765 delay_set_stat (file_name
, ¤t_stat_info
,
766 ((mode
^ current_stat_info
.stat
.st_mode
)
767 & MODE_RWX
& ~ current_umask
),
768 ARCHIVED_PERMSTATUS
);
769 else /* For an already existing directory, invert_perms must be 0 */
770 delay_set_stat (file_name
, ¤t_stat_info
,
779 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
782 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
783 | (old_files_option
== OVERWRITE_OLD_FILES
788 /* Contiguous files (on the Masscomp) have to specify the size in
789 the open call that creates them. */
791 if (typeflag
== CONTTYPE
)
792 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
794 fd
= open (file_name
, openflag
, mode
);
796 #else /* not O_CTG */
797 if (typeflag
== CONTTYPE
)
799 static int conttype_diagnosed
;
801 if (!conttype_diagnosed
)
803 conttype_diagnosed
= 1;
804 WARNOPT (WARN_CONTIGUOUS_CAST
,
805 (0, 0, _("Extracting contiguous files as regular files")));
808 fd
= open (file_name
, openflag
, mode
);
810 #endif /* not O_CTG */
816 extract_file (char *file_name
, int typeflag
)
820 union block
*data_block
;
824 bool interdir_made
= false;
825 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
826 mode_t invert_permissions
=
827 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
829 /* FIXME: deal with protection issues. */
831 if (to_stdout_option
)
833 else if (to_command_option
)
835 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
844 int recover
= RECOVER_NO
;
846 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
848 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
854 if (recover
== RECOVER_SKIP
)
856 open_error (file_name
);
861 mv_begin_read (¤t_stat_info
);
862 if (current_stat_info
.is_sparse
)
863 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
865 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
869 /* Locate data, determine max length writeable, write it,
870 block that we have used the data, then check if the write
873 data_block
= find_next_block ();
876 ERROR ((0, 0, _("Unexpected EOF in archive")));
877 break; /* FIXME: What happens, then? */
880 written
= available_space_after (data_block
);
885 count
= full_write (fd
, data_block
->buffer
, written
);
888 set_next_block_after ((union block
*)
889 (data_block
->buffer
+ written
- 1));
890 if (count
!= written
)
892 if (!to_command_option
)
893 write_error_details (file_name
, count
, written
);
894 /* FIXME: shouldn't we restore from backup? */
903 /* If writing to stdout, don't try to do anything to the filename;
904 it doesn't exist, or we don't want to touch it anyway. */
906 if (to_stdout_option
)
909 if (! to_command_option
)
910 set_stat (file_name
, ¤t_stat_info
, fd
, NULL
, invert_permissions
,
911 (old_files_option
== OVERWRITE_OLD_FILES
912 ? UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
917 close_error (file_name
);
919 if (to_command_option
)
925 /* Create a placeholder file with name FILE_NAME, which will be
926 replaced after other extraction is done by a symbolic link if
927 IS_SYMLINK is true, and by a hard link otherwise. Set
928 *INTERDIR_MADE if an intermediate directory is made in the
932 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
937 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
939 switch (maybe_recoverable (file_name
, interdir_made
))
948 open_error (file_name
);
953 if (fstat (fd
, &st
) != 0)
955 stat_error (file_name
);
958 else if (close (fd
) != 0)
959 close_error (file_name
);
962 struct delayed_set_stat
*h
;
963 struct delayed_link
*p
=
964 xmalloc (offsetof (struct delayed_link
, target
)
965 + strlen (current_stat_info
.link_name
)
967 p
->next
= delayed_link_head
;
968 delayed_link_head
= p
;
971 p
->ctime
= get_stat_ctime (&st
);
972 p
->is_symlink
= is_symlink
;
975 p
->uid
= current_stat_info
.stat
.st_uid
;
976 p
->gid
= current_stat_info
.stat
.st_gid
;
978 p
->change_dir
= chdir_current
;
979 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
980 + strlen (file_name
) + 1);
981 p
->sources
->next
= 0;
982 strcpy (p
->sources
->string
, file_name
);
983 strcpy (p
->target
, current_stat_info
.link_name
);
985 h
= delayed_set_stat_head
;
986 if (h
&& ! h
->after_links
987 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
988 && ISSLASH (file_name
[h
->file_name_len
])
989 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
995 if (stat (h
->file_name
, &st
) != 0)
996 stat_error (h
->file_name
);
1003 while ((h
= h
->next
) && ! h
->after_links
);
1013 extract_link (char *file_name
, int typeflag
)
1015 bool interdir_made
= false;
1016 char const *link_name
;
1019 link_name
= current_stat_info
.link_name
;
1021 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1022 return create_placeholder_file (file_name
, false, &interdir_made
);
1026 struct stat st1
, st2
;
1028 int status
= link (link_name
, file_name
);
1033 struct delayed_link
*ds
= delayed_link_head
;
1034 if (ds
&& lstat (link_name
, &st1
) == 0)
1035 for (; ds
; ds
= ds
->next
)
1036 if (ds
->change_dir
== chdir_current
1037 && ds
->dev
== st1
.st_dev
1038 && ds
->ino
== st1
.st_ino
1039 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
1041 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1042 + strlen (file_name
) + 1);
1043 strcpy (p
->string
, file_name
);
1044 p
->next
= ds
->sources
;
1050 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1051 || (lstat (link_name
, &st1
) == 0
1052 && lstat (file_name
, &st2
) == 0
1053 && st1
.st_dev
== st2
.st_dev
1054 && st1
.st_ino
== st2
.st_ino
))
1059 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1061 if (rc
== RECOVER_SKIP
)
1063 if (!(incremental_option
&& errno
== EEXIST
))
1065 link_error (link_name
, file_name
);
1072 extract_symlink (char *file_name
, int typeflag
)
1075 bool interdir_made
= false;
1077 if (! absolute_names_option
1078 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1079 || contains_dot_dot (current_stat_info
.link_name
)))
1080 return create_placeholder_file (file_name
, true, &interdir_made
);
1082 while (symlink (current_stat_info
.link_name
, file_name
))
1083 switch (maybe_recoverable (file_name
, &interdir_made
))
1092 symlink_error (current_stat_info
.link_name
, file_name
);
1096 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, 0, 0, SYMTYPE
);
1100 static int warned_once
;
1105 WARNOPT (WARN_SYMBOLIC_CAST
,
1107 _("Attempting extraction of symbolic links as hard links")));
1109 return extract_link (file_name
, typeflag
);
1113 #if S_IFCHR || S_IFBLK
1115 extract_node (char *file_name
, int typeflag
)
1117 bool interdir_made
= false;
1118 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1119 mode_t invert_permissions
=
1120 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1122 while (mknod (file_name
, mode
^ invert_permissions
,
1123 current_stat_info
.stat
.st_rdev
))
1124 switch (maybe_recoverable (file_name
, &interdir_made
))
1133 mknod_error (file_name
);
1137 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, invert_permissions
,
1138 ARCHIVED_PERMSTATUS
, typeflag
);
1143 #if HAVE_MKFIFO || defined mkfifo
1145 extract_fifo (char *file_name
, int typeflag
)
1148 bool interdir_made
= false;
1149 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1150 mode_t invert_permissions
=
1151 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1153 while ((status
= mkfifo (file_name
, mode
)) != 0)
1154 switch (maybe_recoverable (file_name
, &interdir_made
))
1163 mkfifo_error (file_name
);
1167 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, invert_permissions
,
1168 ARCHIVED_PERMSTATUS
, typeflag
);
1174 extract_volhdr (char *file_name
, int typeflag
)
1181 extract_failure (char *file_name
, int typeflag
)
1186 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1190 /* Prepare to extract a file. Find extractor function.
1191 Return zero if extraction should not proceed. */
1194 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1198 if (EXTRACT_OVER_PIPE
)
1201 /* Select the extractor */
1204 case GNUTYPE_SPARSE
:
1205 *fun
= extract_file
;
1212 /* Appears to be a file. But BSD tar uses the convention that a slash
1213 suffix means a directory. */
1214 if (current_stat_info
.had_trailing_slash
)
1218 *fun
= extract_file
;
1224 *fun
= extract_symlink
;
1228 *fun
= extract_link
;
1233 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1234 *fun
= extract_node
;
1240 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1241 *fun
= extract_node
;
1245 #if HAVE_MKFIFO || defined mkfifo
1247 *fun
= extract_fifo
;
1252 case GNUTYPE_DUMPDIR
:
1254 if (current_stat_info
.is_dumpdir
)
1255 delay_directory_restore_option
= true;
1258 case GNUTYPE_VOLHDR
:
1259 *fun
= extract_volhdr
;
1262 case GNUTYPE_MULTIVOL
:
1264 _("%s: Cannot extract -- file is continued from another volume"),
1265 quotearg_colon (current_stat_info
.file_name
)));
1266 *fun
= extract_failure
;
1269 case GNUTYPE_LONGNAME
:
1270 case GNUTYPE_LONGLINK
:
1271 ERROR ((0, 0, _("Unexpected long name header")));
1272 *fun
= extract_failure
;
1276 WARNOPT (WARN_UNKNOWN_CAST
,
1278 _("%s: Unknown file type `%c', extracted as normal file"),
1279 quotearg_colon (file_name
), typeflag
));
1280 *fun
= extract_file
;
1283 /* Determine whether the extraction should proceed */
1287 switch (old_files_option
)
1289 case UNLINK_FIRST_OLD_FILES
:
1290 if (!remove_any_file (file_name
,
1291 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1292 : ORDINARY_REMOVE_OPTION
)
1293 && errno
&& errno
!= ENOENT
)
1295 unlink_error (file_name
);
1300 case KEEP_NEWER_FILES
:
1301 if (file_newer_p (file_name
, ¤t_stat_info
))
1303 WARNOPT (WARN_IGNORE_NEWER
,
1304 (0, 0, _("Current %s is newer or same age"),
1305 quote (file_name
)));
1317 /* Extract a file from the archive. */
1319 extract_archive (void)
1322 tar_extractor_t fun
;
1324 fatal_exit_hook
= extract_finish
;
1326 set_next_block_after (current_header
);
1328 if (!current_stat_info
.file_name
[0]
1329 || (interactive_option
1330 && !confirm ("extract", current_stat_info
.file_name
)))
1336 /* Print the block from current_header and current_stat. */
1338 print_header (¤t_stat_info
, current_header
, -1);
1340 /* Restore stats for all non-ancestor directories, unless
1341 it is an incremental archive.
1342 (see NOTICE in the comment to delay_set_stat above) */
1343 if (!delay_directory_restore_option
)
1345 int dir
= chdir_current
;
1346 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1350 /* Take a safety backup of a previously existing file. */
1353 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1356 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1357 quotearg_colon (current_stat_info
.file_name
)));
1362 /* Extract the archive entry according to its type. */
1364 typeflag
= sparse_member_p (¤t_stat_info
) ?
1365 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1367 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1369 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1371 undo_last_backup ();
1378 /* Extract the links whose final extraction were delayed. */
1380 apply_delayed_links (void)
1382 struct delayed_link
*ds
;
1384 for (ds
= delayed_link_head
; ds
; )
1386 struct string_list
*sources
= ds
->sources
;
1387 char const *valid_source
= 0;
1389 chdir_do (ds
->change_dir
);
1391 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1393 char const *source
= sources
->string
;
1396 /* Make sure the placeholder file is still there. If not,
1397 don't create a link, as the placeholder was probably
1398 removed by a later extraction. */
1399 if (lstat (source
, &st
) == 0
1400 && st
.st_dev
== ds
->dev
1401 && st
.st_ino
== ds
->ino
1402 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1404 /* Unlink the placeholder, then create a hard link if possible,
1405 a symbolic link otherwise. */
1406 if (unlink (source
) != 0)
1407 unlink_error (source
);
1408 else if (valid_source
&& link (valid_source
, source
) == 0)
1410 else if (!ds
->is_symlink
)
1412 if (link (ds
->target
, source
) != 0)
1413 link_error (ds
->target
, source
);
1415 else if (symlink (ds
->target
, source
) != 0)
1416 symlink_error (ds
->target
, source
);
1419 struct tar_stat_info st1
;
1420 st1
.stat
.st_uid
= ds
->uid
;
1421 st1
.stat
.st_gid
= ds
->gid
;
1422 set_stat (source
, &st1
, -1, NULL
, 0, 0, SYMTYPE
);
1423 valid_source
= source
;
1428 for (sources
= ds
->sources
; sources
; )
1430 struct string_list
*next
= sources
->next
;
1436 struct delayed_link
*next
= ds
->next
;
1442 delayed_link_head
= 0;
1445 /* Finish the extraction of an archive. */
1447 extract_finish (void)
1449 /* First, fix the status of ordinary directories that need fixing. */
1450 apply_nonancestor_delayed_set_stat ("", 0);
1452 /* Then, apply delayed links, so that they don't affect delayed
1453 directory status-setting for ordinary directories. */
1454 apply_delayed_links ();
1456 /* Finally, fix the status of directories that are ancestors
1457 of delayed links. */
1458 apply_nonancestor_delayed_set_stat ("", 1);
1462 rename_directory (char *src
, char *dst
)
1464 if (rename (src
, dst
))
1471 if (make_directories (dst
))
1473 if (rename (src
, dst
) == 0)
1480 /* FIXME: Fall back to recursive copying */
1486 ERROR ((0, e
, _("Cannot rename %s to %s"),