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
;
75 static struct delayed_set_stat
*delayed_set_stat_head
;
77 /* List of links whose creation we have delayed. */
80 /* The next delayed link in the list. */
81 struct delayed_link
*next
;
83 /* The device, inode number and last-modified time of the placeholder. */
86 struct timespec mtime
;
88 /* True if the link is symbolic. */
91 /* The desired owner and group of the link, if it is a symlink. */
95 /* A list of sources for this link. The sources are all to be
96 hard-linked together. */
97 struct string_list
*sources
;
99 /* The desired target of the desired link. */
103 static struct delayed_link
*delayed_link_head
;
107 struct string_list
*next
;
111 /* Set up to extract files. */
115 we_are_root
= geteuid () == 0;
116 same_permissions_option
+= we_are_root
;
117 same_owner_option
+= we_are_root
;
119 /* Option -p clears the kernel umask, so it does not affect proper
120 restoration of file permissions. New intermediate directories will
121 comply with umask at start of program. */
123 newdir_umask
= umask (0);
124 if (0 < same_permissions_option
)
128 umask (newdir_umask
); /* restore the kernel umask */
129 current_umask
= newdir_umask
;
133 /* If restoring permissions, restore the mode for FILE_NAME from
134 information given in *STAT_INFO (where *CUR_INFO gives
135 the current status if CUR_INFO is nonzero); otherwise invert the
136 INVERT_PERMISSIONS bits from the file's current permissions.
137 PERMSTATUS specifies the status of the file's permissions.
138 TYPEFLAG specifies the type of the file. */
140 set_mode (char const *file_name
,
141 struct stat
const *stat_info
,
142 struct stat
const *cur_info
,
143 mode_t invert_permissions
, enum permstatus permstatus
,
149 if (0 < same_permissions_option
150 && permstatus
!= INTERDIR_PERMSTATUS
)
152 mode
= stat_info
->st_mode
;
154 /* If we created the file and it has a mode that we set already
155 with O_CREAT, then its mode is often set correctly already.
156 But if we are changing ownership, the mode's group and and
157 other permission bits were omitted originally, so it's less
158 likely that the mode is OK now. Also, on many hosts, some
159 directories inherit the setgid bits from their parents, so we
160 we must set directories' modes explicitly. */
161 if ((permstatus
== ARCHIVED_PERMSTATUS
162 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
163 && typeflag
!= DIRTYPE
164 && typeflag
!= GNUTYPE_DUMPDIR
)
167 else if (! invert_permissions
)
171 /* We must inspect a directory's current permissions, since the
172 directory may have inherited its setgid bit from its parent.
174 INVERT_PERMISSIONS happens to be nonzero only for directories
175 that we created, so there's no point optimizing this code for
180 if (stat (file_name
, &st
) != 0)
182 stat_error (file_name
);
187 mode
= cur_info
->st_mode
^ invert_permissions
;
190 failed
= chmod (file_name
, mode
) != 0;
191 if (failed
&& errno
== EPERM
)
193 /* On Solaris, chmod may fail if we don't have PRIV_ALL. */
194 if (priv_set_restore_linkdir () == 0)
196 failed
= chmod (file_name
, mode
) != 0;
197 priv_set_remove_linkdir ();
201 chmod_error_details (file_name
, mode
);
204 /* Check time after successfully setting FILE_NAME's time stamp to T. */
206 check_time (char const *file_name
, struct timespec t
)
209 WARNOPT (WARN_TIMESTAMP
,
210 (0, 0, _("%s: implausibly old time stamp %s"),
211 file_name
, tartime (t
, true)));
212 else if (timespec_cmp (volume_start_time
, t
) < 0)
216 if (timespec_cmp (now
, t
) < 0)
218 char buf
[TIMESPEC_STRSIZE_BOUND
];
219 struct timespec diff
;
220 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
221 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
222 if (diff
.tv_nsec
< 0)
224 diff
.tv_nsec
+= BILLION
;
227 WARNOPT (WARN_TIMESTAMP
,
228 (0, 0, _("%s: time stamp %s is %s s in the future"),
229 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
234 /* Restore stat attributes (owner, group, mode and times) for
235 FILE_NAME, using information given in *ST.
236 If CUR_INFO is nonzero, *CUR_INFO is the
237 file's current status.
238 If not restoring permissions, invert the
239 INVERT_PERMISSIONS bits from the file's current permissions.
240 PERMSTATUS specifies the status of the file's permissions.
241 TYPEFLAG specifies the type of the file. */
243 /* FIXME: About proper restoration of symbolic link attributes, we still do
244 not have it right. Pretesters' reports tell us we need further study and
245 probably more configuration. For now, just use lchown if it exists, and
246 punt for the rest. Sigh! */
249 set_stat (char const *file_name
,
250 struct tar_stat_info
const *st
,
251 struct stat
const *cur_info
,
252 mode_t invert_permissions
, enum permstatus permstatus
,
255 if (typeflag
!= SYMTYPE
)
257 /* We do the utime before the chmod because some versions of utime are
258 broken and trash the modes of the file. */
260 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
262 /* We set the accessed time to `now', which is really the time we
263 started extracting files, unless incremental_option is used, in
264 which case .st_atime is used. */
266 /* FIXME: incremental_option should set ctime too, but how? */
268 struct timespec ts
[2];
269 if (incremental_option
)
275 if (utimens (file_name
, ts
) != 0)
276 utime_error (file_name
);
279 check_time (file_name
, ts
[0]);
280 check_time (file_name
, ts
[1]);
284 /* Some systems allow non-root users to give files away. Once this
285 done, it is not possible anymore to change file permissions.
286 However, setting file permissions now would be incorrect, since
287 they would apply to the wrong user, and there would be a race
288 condition. So, don't use systems that allow non-root users to
292 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
294 /* When lchown exists, it should be used to change the attributes of
295 the symbolic link itself. In this case, a mere chown would change
296 the attributes of the file the symbolic link is pointing to, and
297 should be avoided. */
298 int chown_result
= 1;
300 if (typeflag
== SYMTYPE
)
303 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
308 chown_result
= chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
311 if (chown_result
== 0)
313 /* Changing the owner can flip st_mode bits in some cases, so
314 ignore cur_info if it might be obsolete now. */
316 && cur_info
->st_mode
& S_IXUGO
317 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
320 else if (chown_result
< 0)
321 chown_error_details (file_name
,
322 st
->stat
.st_uid
, st
->stat
.st_gid
);
325 if (typeflag
!= SYMTYPE
)
326 set_mode (file_name
, &st
->stat
, cur_info
,
327 invert_permissions
, permstatus
, typeflag
);
330 /* Remember to restore stat attributes (owner, group, mode and times)
331 for the directory FILE_NAME, using information given in *ST,
332 once we stop extracting files into that directory.
333 If not restoring permissions, remember to invert the
334 INVERT_PERMISSIONS bits from the file's current permissions.
335 PERMSTATUS specifies the status of the file's permissions.
337 NOTICE: this works only if the archive has usual member order, i.e.
338 directory, then the files in that directory. Incremental archive have
339 somewhat reversed order: first go subdirectories, then all other
340 members. To help cope with this case the variable
341 delay_directory_restore_option is set by prepare_to_extract.
343 If an archive was explicitely created so that its member order is
344 reversed, some directory timestamps can be restored incorrectly,
346 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
349 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
350 mode_t invert_permissions
, enum permstatus permstatus
)
352 size_t file_name_len
= strlen (file_name
);
353 struct delayed_set_stat
*data
=
354 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
355 + file_name_len
+ 1);
356 data
->next
= delayed_set_stat_head
;
357 data
->dev
= st
->stat
.st_dev
;
358 data
->ino
= st
->stat
.st_ino
;
359 data
->mode
= st
->stat
.st_mode
;
360 data
->uid
= st
->stat
.st_uid
;
361 data
->gid
= st
->stat
.st_gid
;
362 data
->atime
= st
->atime
;
363 data
->mtime
= st
->mtime
;
364 data
->file_name_len
= file_name_len
;
365 data
->invert_permissions
= invert_permissions
;
366 data
->permstatus
= permstatus
;
367 data
->after_links
= 0;
368 strcpy (data
->file_name
, file_name
);
369 delayed_set_stat_head
= data
;
372 /* Update the delayed_set_stat info for an intermediate directory
373 created within the file name of DIR. The intermediate directory turned
374 out to be the same as this directory, e.g. due to ".." or symbolic
375 links. *DIR_STAT_INFO is the status of the directory. */
377 repair_delayed_set_stat (char const *dir
,
378 struct stat
const *dir_stat_info
)
380 struct delayed_set_stat
*data
;
381 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
384 if (stat (data
->file_name
, &st
) != 0)
386 stat_error (data
->file_name
);
390 if (st
.st_dev
== dir_stat_info
->st_dev
391 && st
.st_ino
== dir_stat_info
->st_ino
)
393 data
->dev
= current_stat_info
.stat
.st_dev
;
394 data
->ino
= current_stat_info
.stat
.st_ino
;
395 data
->mode
= current_stat_info
.stat
.st_mode
;
396 data
->uid
= current_stat_info
.stat
.st_uid
;
397 data
->gid
= current_stat_info
.stat
.st_gid
;
398 data
->atime
= current_stat_info
.atime
;
399 data
->mtime
= current_stat_info
.mtime
;
400 data
->invert_permissions
=
401 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
402 & MODE_RWX
& ~ current_umask
);
403 data
->permstatus
= ARCHIVED_PERMSTATUS
;
408 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
409 quotearg_colon (dir
)));
412 /* After a file/link/directory creation has failed, see if
413 it's because some required directory was not present, and if so,
414 create all required directories. Return non-zero if a directory
417 make_directories (char *file_name
)
419 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
420 char *cursor
; /* points into the file name */
421 int did_something
= 0; /* did we do anything yet? */
423 int invert_permissions
;
426 for (cursor
= cursor0
; *cursor
; cursor
++)
428 if (! ISSLASH (*cursor
))
431 /* Avoid mkdir of empty string, if leading or double '/'. */
433 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
436 /* Avoid mkdir where last part of file name is "." or "..". */
438 if (cursor
[-1] == '.'
439 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
440 || (cursor
[-2] == '.'
441 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
444 *cursor
= '\0'; /* truncate the name there */
445 mode
= MODE_RWX
& ~ newdir_umask
;
446 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
447 status
= mkdir (file_name
, mode
^ invert_permissions
);
451 /* Create a struct delayed_set_stat even if
452 invert_permissions is zero, because
453 repair_delayed_set_stat may need to update the struct. */
454 delay_set_stat (file_name
,
456 invert_permissions
, INTERDIR_PERMSTATUS
);
458 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
468 continue; /* Directory already exists. */
469 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
470 this. Reported by Warren Hyde
471 <Warren.Hyde@motorola.com> */
472 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
473 && access (file_name
, W_OK
) == 0)
476 /* Some other error in the mkdir. We return to the caller. */
480 return did_something
; /* tell them to retry if we made one */
484 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
488 if (stat (file_name
, &st
))
492 stat_warn (file_name
);
493 /* Be on the safe side: if the file does exist assume it is newer */
498 if (!S_ISDIR (st
.st_mode
)
499 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
508 #define RECOVER_SKIP 2
510 /* Attempt repairing what went wrong with the extraction. Delete an
511 already existing file or create missing intermediate directories.
512 Return RECOVER_OK if we somewhat increased our chances at a successful
513 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
514 caller should skip extraction of that member. The value of errno is
515 properly restored on returning RECOVER_NO. */
518 maybe_recoverable (char *file_name
, bool *interdir_made
)
528 /* Remove an old file, if the options allow this. */
530 switch (old_files_option
)
535 case KEEP_NEWER_FILES
:
536 if (file_newer_p (file_name
, ¤t_stat_info
))
543 case DEFAULT_OLD_FILES
:
544 case NO_OVERWRITE_DIR_OLD_FILES
:
545 case OVERWRITE_OLD_FILES
:
547 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
549 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
552 case UNLINK_FIRST_OLD_FILES
:
557 /* Attempt creating missing intermediate directories. */
558 if (! make_directories (file_name
))
563 *interdir_made
= true;
567 /* Just say we can't do anything about it... */
573 /* Fix the statuses of all directories whose statuses need fixing, and
574 which are not ancestors of FILE_NAME. If AFTER_LINKS is
575 nonzero, do this for all such directories; otherwise, stop at the
576 first directory that is marked to be fixed up only after delayed
577 links are applied. */
579 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
581 size_t file_name_len
= strlen (file_name
);
582 bool check_for_renamed_directories
= 0;
584 while (delayed_set_stat_head
)
586 struct delayed_set_stat
*data
= delayed_set_stat_head
;
587 bool skip_this_one
= 0;
589 struct stat
const *cur_info
= 0;
591 check_for_renamed_directories
|= data
->after_links
;
593 if (after_links
< data
->after_links
594 || (data
->file_name_len
< file_name_len
595 && file_name
[data
->file_name_len
]
596 && (ISSLASH (file_name
[data
->file_name_len
])
597 || ISSLASH (file_name
[data
->file_name_len
- 1]))
598 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
601 if (check_for_renamed_directories
)
604 if (stat (data
->file_name
, &st
) != 0)
606 stat_error (data
->file_name
);
609 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
612 _("%s: Directory renamed before its status could be extracted"),
613 quotearg_colon (data
->file_name
)));
620 struct tar_stat_info sb
;
621 sb
.stat
.st_mode
= data
->mode
;
622 sb
.stat
.st_uid
= data
->uid
;
623 sb
.stat
.st_gid
= data
->gid
;
624 sb
.atime
= data
->atime
;
625 sb
.mtime
= data
->mtime
;
626 set_stat (data
->file_name
, &sb
, cur_info
,
627 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
630 delayed_set_stat_head
= data
->next
;
637 /* Extractor functions for various member types */
640 extract_dir (char *file_name
, int typeflag
)
644 bool interdir_made
= false;
646 /* Save 'root device' to avoid purging mount points. */
647 if (one_file_system_option
&& root_device
== 0)
651 if (stat (".", &st
) != 0)
654 root_device
= st
.st_dev
;
657 if (incremental_option
)
658 /* Read the entry and delete files that aren't listed in the archive. */
659 purge_directory (file_name
);
660 else if (typeflag
== GNUTYPE_DUMPDIR
)
663 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
664 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
667 while ((status
= mkdir (file_name
, mode
)))
671 || old_files_option
== DEFAULT_OLD_FILES
672 || old_files_option
== OVERWRITE_OLD_FILES
))
675 if (stat (file_name
, &st
) == 0)
679 repair_delayed_set_stat (file_name
, &st
);
682 if (S_ISDIR (st
.st_mode
))
692 switch (maybe_recoverable (file_name
, &interdir_made
))
703 mkdir_error (file_name
);
712 || old_files_option
== DEFAULT_OLD_FILES
713 || old_files_option
== OVERWRITE_OLD_FILES
)
716 delay_set_stat (file_name
, ¤t_stat_info
,
717 ((mode
^ current_stat_info
.stat
.st_mode
)
718 & MODE_RWX
& ~ current_umask
),
719 ARCHIVED_PERMSTATUS
);
720 else /* For an already existing directory, invert_perms must be 0 */
721 delay_set_stat (file_name
, ¤t_stat_info
,
730 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
733 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
734 | (old_files_option
== OVERWRITE_OLD_FILES
739 /* Contiguous files (on the Masscomp) have to specify the size in
740 the open call that creates them. */
742 if (typeflag
== CONTTYPE
)
743 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
745 fd
= open (file_name
, openflag
, mode
);
747 #else /* not O_CTG */
748 if (typeflag
== CONTTYPE
)
750 static int conttype_diagnosed
;
752 if (!conttype_diagnosed
)
754 conttype_diagnosed
= 1;
755 WARNOPT (WARN_CONTIGUOUS_CAST
,
756 (0, 0, _("Extracting contiguous files as regular files")));
759 fd
= open (file_name
, openflag
, mode
);
761 #endif /* not O_CTG */
767 extract_file (char *file_name
, int typeflag
)
771 union block
*data_block
;
775 bool interdir_made
= false;
776 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
777 mode_t invert_permissions
=
778 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
780 /* FIXME: deal with protection issues. */
782 if (to_stdout_option
)
784 else if (to_command_option
)
786 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
795 int recover
= RECOVER_NO
;
797 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
799 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
805 if (recover
== RECOVER_SKIP
)
807 open_error (file_name
);
812 mv_begin_read (¤t_stat_info
);
813 if (current_stat_info
.is_sparse
)
814 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
816 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
820 /* Locate data, determine max length writeable, write it,
821 block that we have used the data, then check if the write
824 data_block
= find_next_block ();
827 ERROR ((0, 0, _("Unexpected EOF in archive")));
828 break; /* FIXME: What happens, then? */
831 written
= available_space_after (data_block
);
836 count
= full_write (fd
, data_block
->buffer
, written
);
839 set_next_block_after ((union block
*)
840 (data_block
->buffer
+ written
- 1));
841 if (count
!= written
)
843 if (!to_command_option
)
844 write_error_details (file_name
, count
, written
);
845 /* FIXME: shouldn't we restore from backup? */
854 /* If writing to stdout, don't try to do anything to the filename;
855 it doesn't exist, or we don't want to touch it anyway. */
857 if (to_stdout_option
)
862 close_error (file_name
);
864 if (to_command_option
)
867 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
868 (old_files_option
== OVERWRITE_OLD_FILES
?
869 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
875 /* Create a placeholder file with name FILE_NAME, which will be
876 replaced after other extraction is done by a symbolic link if
877 IS_SYMLINK is true, and by a hard link otherwise. Set
878 *INTERDIR_MADE if an intermediate directory is made in the
882 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
887 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
889 switch (maybe_recoverable (file_name
, interdir_made
))
898 open_error (file_name
);
903 if (fstat (fd
, &st
) != 0)
905 stat_error (file_name
);
908 else if (close (fd
) != 0)
909 close_error (file_name
);
912 struct delayed_set_stat
*h
;
913 struct delayed_link
*p
=
914 xmalloc (offsetof (struct delayed_link
, target
)
915 + strlen (current_stat_info
.link_name
)
917 p
->next
= delayed_link_head
;
918 delayed_link_head
= p
;
921 p
->mtime
= get_stat_mtime (&st
);
922 p
->is_symlink
= is_symlink
;
925 p
->uid
= current_stat_info
.stat
.st_uid
;
926 p
->gid
= current_stat_info
.stat
.st_gid
;
928 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
929 + strlen (file_name
) + 1);
930 p
->sources
->next
= 0;
931 strcpy (p
->sources
->string
, file_name
);
932 strcpy (p
->target
, current_stat_info
.link_name
);
934 h
= delayed_set_stat_head
;
935 if (h
&& ! h
->after_links
936 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
937 && ISSLASH (file_name
[h
->file_name_len
])
938 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
944 if (stat (h
->file_name
, &st
) != 0)
945 stat_error (h
->file_name
);
952 while ((h
= h
->next
) && ! h
->after_links
);
962 extract_link (char *file_name
, int typeflag
)
964 bool interdir_made
= false;
965 char const *link_name
;
968 link_name
= current_stat_info
.link_name
;
970 if (! absolute_names_option
&& contains_dot_dot (link_name
))
971 return create_placeholder_file (file_name
, false, &interdir_made
);
975 struct stat st1
, st2
;
977 int status
= link (link_name
, file_name
);
982 struct delayed_link
*ds
= delayed_link_head
;
983 if (ds
&& lstat (link_name
, &st1
) == 0)
984 for (; ds
; ds
= ds
->next
)
985 if (ds
->dev
== st1
.st_dev
986 && ds
->ino
== st1
.st_ino
987 && timespec_cmp (ds
->mtime
, get_stat_mtime (&st1
)) == 0)
989 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
990 + strlen (file_name
) + 1);
991 strcpy (p
->string
, file_name
);
992 p
->next
= ds
->sources
;
998 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
999 || (lstat (link_name
, &st1
) == 0
1000 && lstat (file_name
, &st2
) == 0
1001 && st1
.st_dev
== st2
.st_dev
1002 && st1
.st_ino
== st2
.st_ino
))
1007 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1009 if (rc
== RECOVER_SKIP
)
1011 if (!(incremental_option
&& errno
== EEXIST
))
1013 link_error (link_name
, file_name
);
1020 extract_symlink (char *file_name
, int typeflag
)
1023 bool interdir_made
= false;
1025 if (! absolute_names_option
1026 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1027 || contains_dot_dot (current_stat_info
.link_name
)))
1028 return create_placeholder_file (file_name
, true, &interdir_made
);
1030 while (symlink (current_stat_info
.link_name
, file_name
))
1031 switch (maybe_recoverable (file_name
, &interdir_made
))
1040 symlink_error (current_stat_info
.link_name
, file_name
);
1044 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
1048 static int warned_once
;
1053 WARNOPT (WARN_SYMBOLIC_CAST
,
1055 _("Attempting extraction of symbolic links as hard links")));
1057 return extract_link (file_name
, typeflag
);
1061 #if S_IFCHR || S_IFBLK
1063 extract_node (char *file_name
, int typeflag
)
1065 bool interdir_made
= false;
1066 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1067 mode_t invert_permissions
=
1068 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1070 while (mknod (file_name
, mode
^ invert_permissions
,
1071 current_stat_info
.stat
.st_rdev
))
1072 switch (maybe_recoverable (file_name
, &interdir_made
))
1081 mknod_error (file_name
);
1085 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1086 ARCHIVED_PERMSTATUS
, typeflag
);
1091 #if HAVE_MKFIFO || defined mkfifo
1093 extract_fifo (char *file_name
, int typeflag
)
1096 bool interdir_made
= false;
1097 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1098 mode_t invert_permissions
=
1099 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1101 while ((status
= mkfifo (file_name
, mode
)) != 0)
1102 switch (maybe_recoverable (file_name
, &interdir_made
))
1111 mkfifo_error (file_name
);
1115 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1116 ARCHIVED_PERMSTATUS
, typeflag
);
1122 extract_volhdr (char *file_name
, int typeflag
)
1129 extract_failure (char *file_name
, int typeflag
)
1134 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1138 /* Prepare to extract a file. Find extractor function.
1139 Return zero if extraction should not proceed. */
1142 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1146 if (EXTRACT_OVER_PIPE
)
1149 /* Select the extractor */
1152 case GNUTYPE_SPARSE
:
1153 *fun
= extract_file
;
1160 /* Appears to be a file. But BSD tar uses the convention that a slash
1161 suffix means a directory. */
1162 if (current_stat_info
.had_trailing_slash
)
1166 *fun
= extract_file
;
1172 *fun
= extract_symlink
;
1176 *fun
= extract_link
;
1181 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1182 *fun
= extract_node
;
1188 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1189 *fun
= extract_node
;
1193 #if HAVE_MKFIFO || defined mkfifo
1195 *fun
= extract_fifo
;
1200 case GNUTYPE_DUMPDIR
:
1202 if (current_stat_info
.is_dumpdir
)
1203 delay_directory_restore_option
= true;
1206 case GNUTYPE_VOLHDR
:
1207 *fun
= extract_volhdr
;
1210 case GNUTYPE_MULTIVOL
:
1212 _("%s: Cannot extract -- file is continued from another volume"),
1213 quotearg_colon (current_stat_info
.file_name
)));
1214 *fun
= extract_failure
;
1217 case GNUTYPE_LONGNAME
:
1218 case GNUTYPE_LONGLINK
:
1219 ERROR ((0, 0, _("Unexpected long name header")));
1220 *fun
= extract_failure
;
1224 WARNOPT (WARN_UNKNOWN_CAST
,
1226 _("%s: Unknown file type `%c', extracted as normal file"),
1227 quotearg_colon (file_name
), typeflag
));
1228 *fun
= extract_file
;
1231 /* Determine whether the extraction should proceed */
1235 switch (old_files_option
)
1237 case UNLINK_FIRST_OLD_FILES
:
1238 if (!remove_any_file (file_name
,
1239 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1240 : ORDINARY_REMOVE_OPTION
)
1241 && errno
&& errno
!= ENOENT
)
1243 unlink_error (file_name
);
1248 case KEEP_NEWER_FILES
:
1249 if (file_newer_p (file_name
, ¤t_stat_info
))
1251 WARNOPT (WARN_IGNORE_NEWER
,
1252 (0, 0, _("Current %s is newer or same age"),
1253 quote (file_name
)));
1265 /* Extract a file from the archive. */
1267 extract_archive (void)
1270 tar_extractor_t fun
;
1272 fatal_exit_hook
= extract_finish
;
1274 /* Try to disable the ability to unlink a directory. */
1275 priv_set_remove_linkdir ();
1277 set_next_block_after (current_header
);
1279 if (!current_stat_info
.file_name
[0]
1280 || (interactive_option
1281 && !confirm ("extract", current_stat_info
.file_name
)))
1287 /* Print the block from current_header and current_stat. */
1289 print_header (¤t_stat_info
, current_header
, -1);
1291 /* Restore stats for all non-ancestor directories, unless
1292 it is an incremental archive.
1293 (see NOTICE in the comment to delay_set_stat above) */
1294 if (!delay_directory_restore_option
)
1295 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1297 /* Take a safety backup of a previously existing file. */
1300 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1303 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1304 quotearg_colon (current_stat_info
.file_name
)));
1309 /* Extract the archive entry according to its type. */
1311 typeflag
= sparse_member_p (¤t_stat_info
) ?
1312 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1314 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1316 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1318 undo_last_backup ();
1325 /* Extract the symbolic links whose final extraction were delayed. */
1327 apply_delayed_links (void)
1329 struct delayed_link
*ds
;
1331 for (ds
= delayed_link_head
; ds
; )
1333 struct string_list
*sources
= ds
->sources
;
1334 char const *valid_source
= 0;
1336 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1338 char const *source
= sources
->string
;
1341 /* Make sure the placeholder file is still there. If not,
1342 don't create a link, as the placeholder was probably
1343 removed by a later extraction. */
1344 if (lstat (source
, &st
) == 0
1345 && st
.st_dev
== ds
->dev
1346 && st
.st_ino
== ds
->ino
1347 && timespec_cmp (get_stat_mtime (&st
), ds
->mtime
) == 0)
1349 /* Unlink the placeholder, then create a hard link if possible,
1350 a symbolic link otherwise. */
1351 if (unlink (source
) != 0)
1352 unlink_error (source
);
1353 else if (valid_source
&& link (valid_source
, source
) == 0)
1355 else if (!ds
->is_symlink
)
1357 if (link (ds
->target
, source
) != 0)
1358 link_error (ds
->target
, source
);
1360 else if (symlink (ds
->target
, source
) != 0)
1361 symlink_error (ds
->target
, source
);
1364 struct tar_stat_info st1
;
1365 st1
.stat
.st_uid
= ds
->uid
;
1366 st1
.stat
.st_gid
= ds
->gid
;
1367 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1368 valid_source
= source
;
1373 for (sources
= ds
->sources
; sources
; )
1375 struct string_list
*next
= sources
->next
;
1381 struct delayed_link
*next
= ds
->next
;
1387 delayed_link_head
= 0;
1390 /* Finish the extraction of an archive. */
1392 extract_finish (void)
1394 /* First, fix the status of ordinary directories that need fixing. */
1395 apply_nonancestor_delayed_set_stat ("", 0);
1397 /* Then, apply delayed links, so that they don't affect delayed
1398 directory status-setting for ordinary directories. */
1399 apply_delayed_links ();
1401 /* Finally, fix the status of directories that are ancestors
1402 of delayed links. */
1403 apply_nonancestor_delayed_set_stat ("", 1);
1407 rename_directory (char *src
, char *dst
)
1409 if (rename (src
, dst
))
1416 if (make_directories (dst
))
1418 if (rename (src
, dst
) == 0)
1425 /* FIXME: Fall back to recursive copying */
1431 ERROR ((0, e
, _("Cannot rename %s to %s"),