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. */
29 static bool we_are_root
; /* true if our effective uid == 0 */
30 static mode_t newdir_umask
; /* umask when creating new directories */
31 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
33 /* Status of the permissions of a file that we are extracting. */
36 /* This file may have existed already; its permissions are unknown. */
39 /* This file was created using the permissions from the archive,
40 except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
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
;
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 ctime of the placeholder. Use
84 ctime, not mtime, to make false matches less likely if some
85 other process removes the placeholder. */
88 struct timespec ctime
;
90 /* True if the link is symbolic. */
93 /* The desired owner and group of the link, if it is a symlink. */
97 /* The directory that the sources and target are relative to. */
100 /* A list of sources for this link. The sources are all to be
101 hard-linked together. */
102 struct string_list
*sources
;
104 /* The desired target of the desired link. */
108 static struct delayed_link
*delayed_link_head
;
112 struct string_list
*next
;
116 /* Set up to extract files. */
120 we_are_root
= geteuid () == 0;
121 same_permissions_option
+= we_are_root
;
122 same_owner_option
+= we_are_root
;
124 /* Option -p clears the kernel umask, so it does not affect proper
125 restoration of file permissions. New intermediate directories will
126 comply with umask at start of program. */
128 newdir_umask
= umask (0);
129 if (0 < same_permissions_option
)
133 umask (newdir_umask
); /* restore the kernel umask */
134 current_umask
= newdir_umask
;
138 /* Use fchmod if possible, chmod otherwise. */
140 fdchmod (char const *file
, int fd
, mode_t mode
)
144 return fchmod (fd
, mode
);
146 return chmod (file
, mode
);
149 /* Use fchown if possible, chown otherwise. */
151 fdchown (char const *file
, int fd
, uid_t uid
, gid_t gid
)
155 return fchown (fd
, uid
, gid
);
157 return chown (file
, uid
, gid
);
160 /* Use fstat if possible, stat otherwise. */
162 fdstat (char const *file
, int fd
, struct stat
*st
)
165 return fstat (fd
, st
);
166 return stat (file
, st
);
169 /* If restoring permissions, restore the mode for FILE_NAME from
170 information given in *STAT_INFO (where FD is a file descriptor
171 for the file if FD is nonnegative, and where *CUR_INFO gives
172 the current status if CUR_INFO is nonzero); otherwise invert the
173 INVERT_PERMISSIONS bits from the file's current permissions.
174 PERMSTATUS specifies the status of the file's permissions.
175 TYPEFLAG specifies the type of the file. */
177 set_mode (char const *file_name
,
178 struct stat
const *stat_info
,
179 int fd
, struct stat
const *cur_info
,
180 mode_t invert_permissions
, enum permstatus permstatus
,
186 if (0 < same_permissions_option
187 && permstatus
!= INTERDIR_PERMSTATUS
)
189 mode
= stat_info
->st_mode
;
191 /* If we created the file and it has a mode that we set already
192 with O_CREAT, then its mode is often set correctly already.
193 But if we are changing ownership, the mode's group and and
194 other permission bits were omitted originally, so it's less
195 likely that the mode is OK now. Also, on many hosts, some
196 directories inherit the setgid bits from their parents, so we
197 we must set directories' modes explicitly. */
198 if ((permstatus
== ARCHIVED_PERMSTATUS
199 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
200 && typeflag
!= DIRTYPE
201 && typeflag
!= GNUTYPE_DUMPDIR
)
204 else if (! invert_permissions
)
208 /* We must inspect a directory's current permissions, since the
209 directory may have inherited its setgid bit from its parent.
211 INVERT_PERMISSIONS happens to be nonzero only for directories
212 that we created, so there's no point optimizing this code for
217 if (fdstat (file_name
, fd
, &st
) != 0)
219 stat_error (file_name
);
224 mode
= cur_info
->st_mode
^ invert_permissions
;
227 chmod_errno
= fdchmod (file_name
, fd
, mode
) == 0 ? 0 : errno
;
228 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
) != 0)
230 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
231 setuid-root files would otherwise be a backdoor. See
232 http://opensolaris.org/jive/thread.jspa?threadID=95826
234 if (priv_set_restore_linkdir () == 0)
236 chmod_errno
= fdchmod (file_name
, fd
, mode
) == 0 ? 0 : errno
;
237 priv_set_remove_linkdir ();
243 chmod_error_details (file_name
, mode
);
247 /* Check time after successfully setting FILE_NAME's time stamp to T. */
249 check_time (char const *file_name
, struct timespec t
)
252 WARNOPT (WARN_TIMESTAMP
,
253 (0, 0, _("%s: implausibly old time stamp %s"),
254 file_name
, tartime (t
, true)));
255 else if (timespec_cmp (volume_start_time
, t
) < 0)
259 if (timespec_cmp (now
, t
) < 0)
261 char buf
[TIMESPEC_STRSIZE_BOUND
];
262 struct timespec diff
;
263 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
264 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
265 if (diff
.tv_nsec
< 0)
267 diff
.tv_nsec
+= BILLION
;
270 WARNOPT (WARN_TIMESTAMP
,
271 (0, 0, _("%s: time stamp %s is %s s in the future"),
272 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
277 /* Restore stat attributes (owner, group, mode and times) for
278 FILE_NAME, using information given in *ST.
279 If FD is nonnegative, it is a file descriptor for the file.
280 If CUR_INFO is nonzero, *CUR_INFO is the
281 file's current status.
282 If not restoring permissions, invert the
283 INVERT_PERMISSIONS bits from the file's current permissions.
284 PERMSTATUS specifies the status of the file's permissions.
285 TYPEFLAG specifies the type of the file. */
287 /* FIXME: About proper restoration of symbolic link attributes, we still do
288 not have it right. Pretesters' reports tell us we need further study and
289 probably more configuration. For now, just use lchown if it exists, and
290 punt for the rest. Sigh! */
293 set_stat (char const *file_name
,
294 struct tar_stat_info
const *st
,
295 int fd
, struct stat
const *cur_info
,
296 mode_t invert_permissions
, enum permstatus permstatus
,
299 if (typeflag
!= SYMTYPE
)
301 /* We do the utime before the chmod because some versions of utime are
302 broken and trash the modes of the file. */
304 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
306 struct timespec ts
[2];
307 if (incremental_option
)
310 ts
[0].tv_nsec
= UTIME_OMIT
;
313 if (fd_utimensat (fd
, AT_FDCWD
, file_name
, ts
, 0) != 0)
314 utime_error (file_name
);
317 if (incremental_option
)
318 check_time (file_name
, ts
[0]);
319 check_time (file_name
, ts
[1]);
323 /* Some systems allow non-root users to give files away. Once this
324 done, it is not possible anymore to change file permissions.
325 However, setting file permissions now would be incorrect, since
326 they would apply to the wrong user, and there would be a race
327 condition. So, don't use systems that allow non-root users to
331 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
333 /* When lchown exists, it should be used to change the attributes of
334 the symbolic link itself. In this case, a mere chown would change
335 the attributes of the file the symbolic link is pointing to, and
336 should be avoided. */
337 int chown_result
= 1;
339 if (typeflag
== SYMTYPE
)
342 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
347 chown_result
= fdchown (file_name
, fd
,
348 st
->stat
.st_uid
, st
->stat
.st_gid
);
351 if (chown_result
== 0)
353 /* Changing the owner can flip st_mode bits in some cases, so
354 ignore cur_info if it might be obsolete now. */
356 && cur_info
->st_mode
& S_IXUGO
357 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
360 else if (chown_result
< 0)
361 chown_error_details (file_name
,
362 st
->stat
.st_uid
, st
->stat
.st_gid
);
365 if (typeflag
!= SYMTYPE
)
366 set_mode (file_name
, &st
->stat
, fd
, cur_info
,
367 invert_permissions
, permstatus
, typeflag
);
370 /* Remember to restore stat attributes (owner, group, mode and times)
371 for the directory FILE_NAME, using information given in *ST,
372 once we stop extracting files into that directory.
373 If not restoring permissions, remember to invert the
374 INVERT_PERMISSIONS bits from the file's current permissions.
375 PERMSTATUS specifies the status of the file's permissions.
377 NOTICE: this works only if the archive has usual member order, i.e.
378 directory, then the files in that directory. Incremental archive have
379 somewhat reversed order: first go subdirectories, then all other
380 members. To help cope with this case the variable
381 delay_directory_restore_option is set by prepare_to_extract.
383 If an archive was explicitely created so that its member order is
384 reversed, some directory timestamps can be restored incorrectly,
386 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
389 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
390 mode_t invert_permissions
, enum permstatus permstatus
)
392 size_t file_name_len
= strlen (file_name
);
393 struct delayed_set_stat
*data
=
394 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
395 + file_name_len
+ 1);
396 data
->next
= delayed_set_stat_head
;
397 data
->dev
= st
->stat
.st_dev
;
398 data
->ino
= st
->stat
.st_ino
;
399 data
->mode
= st
->stat
.st_mode
;
400 data
->uid
= st
->stat
.st_uid
;
401 data
->gid
= st
->stat
.st_gid
;
402 data
->atime
= st
->atime
;
403 data
->mtime
= st
->mtime
;
404 data
->file_name_len
= file_name_len
;
405 data
->invert_permissions
= invert_permissions
;
406 data
->permstatus
= permstatus
;
407 data
->after_links
= 0;
408 data
->change_dir
= chdir_current
;
409 strcpy (data
->file_name
, file_name
);
410 delayed_set_stat_head
= data
;
413 /* Update the delayed_set_stat info for an intermediate directory
414 created within the file name of DIR. The intermediate directory turned
415 out to be the same as this directory, e.g. due to ".." or symbolic
416 links. *DIR_STAT_INFO is the status of the directory. */
418 repair_delayed_set_stat (char const *dir
,
419 struct stat
const *dir_stat_info
)
421 struct delayed_set_stat
*data
;
422 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
425 if (stat (data
->file_name
, &st
) != 0)
427 stat_error (data
->file_name
);
431 if (st
.st_dev
== dir_stat_info
->st_dev
432 && st
.st_ino
== dir_stat_info
->st_ino
)
434 data
->dev
= current_stat_info
.stat
.st_dev
;
435 data
->ino
= current_stat_info
.stat
.st_ino
;
436 data
->mode
= current_stat_info
.stat
.st_mode
;
437 data
->uid
= current_stat_info
.stat
.st_uid
;
438 data
->gid
= current_stat_info
.stat
.st_gid
;
439 data
->atime
= current_stat_info
.atime
;
440 data
->mtime
= current_stat_info
.mtime
;
441 data
->invert_permissions
=
442 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
443 & MODE_RWX
& ~ current_umask
);
444 data
->permstatus
= ARCHIVED_PERMSTATUS
;
449 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
450 quotearg_colon (dir
)));
453 /* After a file/link/directory creation has failed, see if
454 it's because some required directory was not present, and if so,
455 create all required directories. Return non-zero if a directory
458 make_directories (char *file_name
)
460 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
461 char *cursor
; /* points into the file name */
462 int did_something
= 0; /* did we do anything yet? */
464 int invert_permissions
;
467 for (cursor
= cursor0
; *cursor
; cursor
++)
469 if (! ISSLASH (*cursor
))
472 /* Avoid mkdir of empty string, if leading or double '/'. */
474 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
477 /* Avoid mkdir where last part of file name is "." or "..". */
479 if (cursor
[-1] == '.'
480 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
481 || (cursor
[-2] == '.'
482 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
485 *cursor
= '\0'; /* truncate the name there */
486 mode
= MODE_RWX
& ~ newdir_umask
;
487 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
488 status
= mkdir (file_name
, mode
^ invert_permissions
);
492 /* Create a struct delayed_set_stat even if
493 invert_permissions is zero, because
494 repair_delayed_set_stat may need to update the struct. */
495 delay_set_stat (file_name
,
497 invert_permissions
, INTERDIR_PERMSTATUS
);
499 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
509 continue; /* Directory already exists. */
510 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
511 this. Reported by Warren Hyde
512 <Warren.Hyde@motorola.com> */
513 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
514 && access (file_name
, W_OK
) == 0)
517 /* Some other error in the mkdir. We return to the caller. */
521 return did_something
; /* tell them to retry if we made one */
525 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
529 if (stat (file_name
, &st
))
533 stat_warn (file_name
);
534 /* Be on the safe side: if the file does exist assume it is newer */
539 if (!S_ISDIR (st
.st_mode
)
540 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
549 #define RECOVER_SKIP 2
551 /* Attempt repairing what went wrong with the extraction. Delete an
552 already existing file or create missing intermediate directories.
553 Return RECOVER_OK if we somewhat increased our chances at a successful
554 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
555 caller should skip extraction of that member. The value of errno is
556 properly restored on returning RECOVER_NO. */
559 maybe_recoverable (char *file_name
, bool *interdir_made
)
569 /* Remove an old file, if the options allow this. */
571 switch (old_files_option
)
576 case KEEP_NEWER_FILES
:
577 if (file_newer_p (file_name
, ¤t_stat_info
))
584 case DEFAULT_OLD_FILES
:
585 case NO_OVERWRITE_DIR_OLD_FILES
:
586 case OVERWRITE_OLD_FILES
:
588 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
590 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
593 case UNLINK_FIRST_OLD_FILES
:
598 /* Attempt creating missing intermediate directories. */
599 if (! make_directories (file_name
))
604 *interdir_made
= true;
608 /* Just say we can't do anything about it... */
614 /* Fix the statuses of all directories whose statuses need fixing, and
615 which are not ancestors of FILE_NAME. If AFTER_LINKS is
616 nonzero, do this for all such directories; otherwise, stop at the
617 first directory that is marked to be fixed up only after delayed
618 links are applied. */
620 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
622 size_t file_name_len
= strlen (file_name
);
623 bool check_for_renamed_directories
= 0;
625 while (delayed_set_stat_head
)
627 struct delayed_set_stat
*data
= delayed_set_stat_head
;
628 bool skip_this_one
= 0;
630 struct stat
const *cur_info
= 0;
632 check_for_renamed_directories
|= data
->after_links
;
634 if (after_links
< data
->after_links
635 || (data
->file_name_len
< file_name_len
636 && file_name
[data
->file_name_len
]
637 && (ISSLASH (file_name
[data
->file_name_len
])
638 || ISSLASH (file_name
[data
->file_name_len
- 1]))
639 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
642 chdir_do (data
->change_dir
);
644 if (check_for_renamed_directories
)
647 if (stat (data
->file_name
, &st
) != 0)
649 stat_error (data
->file_name
);
652 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
655 _("%s: Directory renamed before its status could be extracted"),
656 quotearg_colon (data
->file_name
)));
663 struct tar_stat_info sb
;
664 sb
.stat
.st_mode
= data
->mode
;
665 sb
.stat
.st_uid
= data
->uid
;
666 sb
.stat
.st_gid
= data
->gid
;
667 sb
.atime
= data
->atime
;
668 sb
.mtime
= data
->mtime
;
669 set_stat (data
->file_name
, &sb
, -1, cur_info
,
670 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
673 delayed_set_stat_head
= data
->next
;
680 /* Extractor functions for various member types */
683 extract_dir (char *file_name
, int typeflag
)
687 bool interdir_made
= false;
689 /* Save 'root device' to avoid purging mount points. */
690 if (one_file_system_option
&& root_device
== 0)
694 if (stat (".", &st
) != 0)
697 root_device
= st
.st_dev
;
700 if (incremental_option
)
701 /* Read the entry and delete files that aren't listed in the archive. */
702 purge_directory (file_name
);
703 else if (typeflag
== GNUTYPE_DUMPDIR
)
706 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
707 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
710 while ((status
= mkdir (file_name
, mode
)))
714 || old_files_option
== DEFAULT_OLD_FILES
715 || old_files_option
== OVERWRITE_OLD_FILES
))
718 if (stat (file_name
, &st
) == 0)
722 repair_delayed_set_stat (file_name
, &st
);
725 if (S_ISDIR (st
.st_mode
))
735 switch (maybe_recoverable (file_name
, &interdir_made
))
746 mkdir_error (file_name
);
755 || old_files_option
== DEFAULT_OLD_FILES
756 || old_files_option
== OVERWRITE_OLD_FILES
)
759 delay_set_stat (file_name
, ¤t_stat_info
,
760 ((mode
^ current_stat_info
.stat
.st_mode
)
761 & MODE_RWX
& ~ current_umask
),
762 ARCHIVED_PERMSTATUS
);
763 else /* For an already existing directory, invert_perms must be 0 */
764 delay_set_stat (file_name
, ¤t_stat_info
,
773 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
776 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
777 | (old_files_option
== OVERWRITE_OLD_FILES
782 /* Contiguous files (on the Masscomp) have to specify the size in
783 the open call that creates them. */
785 if (typeflag
== CONTTYPE
)
786 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
788 fd
= open (file_name
, openflag
, mode
);
790 #else /* not O_CTG */
791 if (typeflag
== CONTTYPE
)
793 static int conttype_diagnosed
;
795 if (!conttype_diagnosed
)
797 conttype_diagnosed
= 1;
798 WARNOPT (WARN_CONTIGUOUS_CAST
,
799 (0, 0, _("Extracting contiguous files as regular files")));
802 fd
= open (file_name
, openflag
, mode
);
804 #endif /* not O_CTG */
810 extract_file (char *file_name
, int typeflag
)
814 union block
*data_block
;
818 bool interdir_made
= false;
819 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
820 mode_t invert_permissions
=
821 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
823 /* FIXME: deal with protection issues. */
825 if (to_stdout_option
)
827 else if (to_command_option
)
829 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
838 int recover
= RECOVER_NO
;
840 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
842 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
848 if (recover
== RECOVER_SKIP
)
850 open_error (file_name
);
855 mv_begin_read (¤t_stat_info
);
856 if (current_stat_info
.is_sparse
)
857 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
859 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
863 /* Locate data, determine max length writeable, write it,
864 block that we have used the data, then check if the write
867 data_block
= find_next_block ();
870 ERROR ((0, 0, _("Unexpected EOF in archive")));
871 break; /* FIXME: What happens, then? */
874 written
= available_space_after (data_block
);
879 count
= full_write (fd
, data_block
->buffer
, written
);
882 set_next_block_after ((union block
*)
883 (data_block
->buffer
+ written
- 1));
884 if (count
!= written
)
886 if (!to_command_option
)
887 write_error_details (file_name
, count
, written
);
888 /* FIXME: shouldn't we restore from backup? */
897 /* If writing to stdout, don't try to do anything to the filename;
898 it doesn't exist, or we don't want to touch it anyway. */
900 if (to_stdout_option
)
903 if (! to_command_option
)
904 set_stat (file_name
, ¤t_stat_info
, fd
, NULL
, invert_permissions
,
905 (old_files_option
== OVERWRITE_OLD_FILES
906 ? UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
911 close_error (file_name
);
913 if (to_command_option
)
919 /* Create a placeholder file with name FILE_NAME, which will be
920 replaced after other extraction is done by a symbolic link if
921 IS_SYMLINK is true, and by a hard link otherwise. Set
922 *INTERDIR_MADE if an intermediate directory is made in the
926 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
931 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
933 switch (maybe_recoverable (file_name
, interdir_made
))
942 open_error (file_name
);
947 if (fstat (fd
, &st
) != 0)
949 stat_error (file_name
);
952 else if (close (fd
) != 0)
953 close_error (file_name
);
956 struct delayed_set_stat
*h
;
957 struct delayed_link
*p
=
958 xmalloc (offsetof (struct delayed_link
, target
)
959 + strlen (current_stat_info
.link_name
)
961 p
->next
= delayed_link_head
;
962 delayed_link_head
= p
;
965 p
->ctime
= get_stat_ctime (&st
);
966 p
->is_symlink
= is_symlink
;
969 p
->uid
= current_stat_info
.stat
.st_uid
;
970 p
->gid
= current_stat_info
.stat
.st_gid
;
972 p
->change_dir
= chdir_current
;
973 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
974 + strlen (file_name
) + 1);
975 p
->sources
->next
= 0;
976 strcpy (p
->sources
->string
, file_name
);
977 strcpy (p
->target
, current_stat_info
.link_name
);
979 h
= delayed_set_stat_head
;
980 if (h
&& ! h
->after_links
981 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
982 && ISSLASH (file_name
[h
->file_name_len
])
983 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
989 if (stat (h
->file_name
, &st
) != 0)
990 stat_error (h
->file_name
);
997 while ((h
= h
->next
) && ! h
->after_links
);
1007 extract_link (char *file_name
, int typeflag
)
1009 bool interdir_made
= false;
1010 char const *link_name
;
1013 link_name
= current_stat_info
.link_name
;
1015 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1016 return create_placeholder_file (file_name
, false, &interdir_made
);
1020 struct stat st1
, st2
;
1022 int status
= link (link_name
, file_name
);
1027 struct delayed_link
*ds
= delayed_link_head
;
1028 if (ds
&& lstat (link_name
, &st1
) == 0)
1029 for (; ds
; ds
= ds
->next
)
1030 if (ds
->change_dir
== chdir_current
1031 && ds
->dev
== st1
.st_dev
1032 && ds
->ino
== st1
.st_ino
1033 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
1035 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1036 + strlen (file_name
) + 1);
1037 strcpy (p
->string
, file_name
);
1038 p
->next
= ds
->sources
;
1044 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1045 || (lstat (link_name
, &st1
) == 0
1046 && lstat (file_name
, &st2
) == 0
1047 && st1
.st_dev
== st2
.st_dev
1048 && st1
.st_ino
== st2
.st_ino
))
1053 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1055 if (rc
== RECOVER_SKIP
)
1057 if (!(incremental_option
&& errno
== EEXIST
))
1059 link_error (link_name
, file_name
);
1066 extract_symlink (char *file_name
, int typeflag
)
1069 bool interdir_made
= false;
1071 if (! absolute_names_option
1072 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1073 || contains_dot_dot (current_stat_info
.link_name
)))
1074 return create_placeholder_file (file_name
, true, &interdir_made
);
1076 while (symlink (current_stat_info
.link_name
, file_name
))
1077 switch (maybe_recoverable (file_name
, &interdir_made
))
1086 symlink_error (current_stat_info
.link_name
, file_name
);
1090 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, 0, 0, SYMTYPE
);
1094 static int warned_once
;
1099 WARNOPT (WARN_SYMBOLIC_CAST
,
1101 _("Attempting extraction of symbolic links as hard links")));
1103 return extract_link (file_name
, typeflag
);
1107 #if S_IFCHR || S_IFBLK
1109 extract_node (char *file_name
, int typeflag
)
1111 bool interdir_made
= false;
1112 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1113 mode_t invert_permissions
=
1114 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1116 while (mknod (file_name
, mode
^ invert_permissions
,
1117 current_stat_info
.stat
.st_rdev
))
1118 switch (maybe_recoverable (file_name
, &interdir_made
))
1127 mknod_error (file_name
);
1131 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, invert_permissions
,
1132 ARCHIVED_PERMSTATUS
, typeflag
);
1137 #if HAVE_MKFIFO || defined mkfifo
1139 extract_fifo (char *file_name
, int typeflag
)
1142 bool interdir_made
= false;
1143 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1144 mode_t invert_permissions
=
1145 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1147 while ((status
= mkfifo (file_name
, mode
)) != 0)
1148 switch (maybe_recoverable (file_name
, &interdir_made
))
1157 mkfifo_error (file_name
);
1161 set_stat (file_name
, ¤t_stat_info
, -1, NULL
, invert_permissions
,
1162 ARCHIVED_PERMSTATUS
, typeflag
);
1168 extract_volhdr (char *file_name
, int typeflag
)
1175 extract_failure (char *file_name
, int typeflag
)
1180 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1184 /* Prepare to extract a file. Find extractor function.
1185 Return zero if extraction should not proceed. */
1188 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1192 if (EXTRACT_OVER_PIPE
)
1195 /* Select the extractor */
1198 case GNUTYPE_SPARSE
:
1199 *fun
= extract_file
;
1206 /* Appears to be a file. But BSD tar uses the convention that a slash
1207 suffix means a directory. */
1208 if (current_stat_info
.had_trailing_slash
)
1212 *fun
= extract_file
;
1218 *fun
= extract_symlink
;
1222 *fun
= extract_link
;
1227 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1228 *fun
= extract_node
;
1234 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1235 *fun
= extract_node
;
1239 #if HAVE_MKFIFO || defined mkfifo
1241 *fun
= extract_fifo
;
1246 case GNUTYPE_DUMPDIR
:
1248 if (current_stat_info
.is_dumpdir
)
1249 delay_directory_restore_option
= true;
1252 case GNUTYPE_VOLHDR
:
1253 *fun
= extract_volhdr
;
1256 case GNUTYPE_MULTIVOL
:
1258 _("%s: Cannot extract -- file is continued from another volume"),
1259 quotearg_colon (current_stat_info
.file_name
)));
1260 *fun
= extract_failure
;
1263 case GNUTYPE_LONGNAME
:
1264 case GNUTYPE_LONGLINK
:
1265 ERROR ((0, 0, _("Unexpected long name header")));
1266 *fun
= extract_failure
;
1270 WARNOPT (WARN_UNKNOWN_CAST
,
1272 _("%s: Unknown file type `%c', extracted as normal file"),
1273 quotearg_colon (file_name
), typeflag
));
1274 *fun
= extract_file
;
1277 /* Determine whether the extraction should proceed */
1281 switch (old_files_option
)
1283 case UNLINK_FIRST_OLD_FILES
:
1284 if (!remove_any_file (file_name
,
1285 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1286 : ORDINARY_REMOVE_OPTION
)
1287 && errno
&& errno
!= ENOENT
)
1289 unlink_error (file_name
);
1294 case KEEP_NEWER_FILES
:
1295 if (file_newer_p (file_name
, ¤t_stat_info
))
1297 WARNOPT (WARN_IGNORE_NEWER
,
1298 (0, 0, _("Current %s is newer or same age"),
1299 quote (file_name
)));
1311 /* Extract a file from the archive. */
1313 extract_archive (void)
1316 tar_extractor_t fun
;
1318 fatal_exit_hook
= extract_finish
;
1320 set_next_block_after (current_header
);
1322 if (!current_stat_info
.file_name
[0]
1323 || (interactive_option
1324 && !confirm ("extract", current_stat_info
.file_name
)))
1330 /* Print the block from current_header and current_stat. */
1332 print_header (¤t_stat_info
, current_header
, -1);
1334 /* Restore stats for all non-ancestor directories, unless
1335 it is an incremental archive.
1336 (see NOTICE in the comment to delay_set_stat above) */
1337 if (!delay_directory_restore_option
)
1339 int dir
= chdir_current
;
1340 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1344 /* Take a safety backup of a previously existing file. */
1347 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1350 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1351 quotearg_colon (current_stat_info
.file_name
)));
1356 /* Extract the archive entry according to its type. */
1358 typeflag
= sparse_member_p (¤t_stat_info
) ?
1359 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1361 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1363 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1365 undo_last_backup ();
1372 /* Extract the links whose final extraction were delayed. */
1374 apply_delayed_links (void)
1376 struct delayed_link
*ds
;
1378 for (ds
= delayed_link_head
; ds
; )
1380 struct string_list
*sources
= ds
->sources
;
1381 char const *valid_source
= 0;
1383 chdir_do (ds
->change_dir
);
1385 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1387 char const *source
= sources
->string
;
1390 /* Make sure the placeholder file is still there. If not,
1391 don't create a link, as the placeholder was probably
1392 removed by a later extraction. */
1393 if (lstat (source
, &st
) == 0
1394 && st
.st_dev
== ds
->dev
1395 && st
.st_ino
== ds
->ino
1396 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1398 /* Unlink the placeholder, then create a hard link if possible,
1399 a symbolic link otherwise. */
1400 if (unlink (source
) != 0)
1401 unlink_error (source
);
1402 else if (valid_source
&& link (valid_source
, source
) == 0)
1404 else if (!ds
->is_symlink
)
1406 if (link (ds
->target
, source
) != 0)
1407 link_error (ds
->target
, source
);
1409 else if (symlink (ds
->target
, source
) != 0)
1410 symlink_error (ds
->target
, source
);
1413 struct tar_stat_info st1
;
1414 st1
.stat
.st_uid
= ds
->uid
;
1415 st1
.stat
.st_gid
= ds
->gid
;
1416 set_stat (source
, &st1
, -1, NULL
, 0, 0, SYMTYPE
);
1417 valid_source
= source
;
1422 for (sources
= ds
->sources
; sources
; )
1424 struct string_list
*next
= sources
->next
;
1430 struct delayed_link
*next
= ds
->next
;
1436 delayed_link_head
= 0;
1439 /* Finish the extraction of an archive. */
1441 extract_finish (void)
1443 /* First, fix the status of ordinary directories that need fixing. */
1444 apply_nonancestor_delayed_set_stat ("", 0);
1446 /* Then, apply delayed links, so that they don't affect delayed
1447 directory status-setting for ordinary directories. */
1448 apply_delayed_links ();
1450 /* Finally, fix the status of directories that are ancestors
1451 of delayed links. */
1452 apply_nonancestor_delayed_set_stat ("", 1);
1456 rename_directory (char *src
, char *dst
)
1458 if (rename (src
, dst
))
1465 if (make_directories (dst
))
1467 if (rename (src
, dst
) == 0)
1474 /* FIXME: Fall back to recursive copying */
1480 ERROR ((0, e
, _("Cannot rename %s to %s"),