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 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
35 #if ! HAVE_FCHMOD && ! defined fchmod
36 # define fchmod(fd, mode) (errno = ENOSYS, -1)
38 #if ! HAVE_FCHOWN && ! defined fchown
39 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
42 /* Return true if an error number ERR means the system call is
43 supported in this case. */
47 return ! (err
== ENOSYS
49 || (EOPNOTSUPP
!= ENOTSUP
&& err
== EOPNOTSUPP
));
52 /* List of directories whose statuses we need to extract after we've
53 finished extracting their subsidiary files. If you consider each
54 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
55 represents an element where AFTER_LINKS is nonzero and [^D]
56 represents an element where AFTER_LINKS is zero, then the head
57 of the subsequence has the longest name, and each non-head element
58 in the prefix is an ancestor (in the directory hierarchy) of the
61 struct delayed_set_stat
63 /* Next directory in list. */
64 struct delayed_set_stat
*next
;
66 /* Metadata for this directory. */
69 mode_t mode
; /* The desired mode is MODE & ~ current_umask. */
72 struct timespec atime
;
73 struct timespec mtime
;
75 /* An estimate of the directory's current mode, along with a mask
76 specifying which bits of this estimate are known to be correct.
77 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
80 mode_t current_mode_mask
;
82 /* This directory is an intermediate directory that was created
83 as an ancestor of some other directory; it was not mentioned
84 in the archive, so do not set its uid, gid, atime, or mtime,
85 and don't alter its mode outside of MODE_RWX. */
88 /* Whether symbolic links should be followed when accessing the
92 /* Do not set the status of this directory until after delayed
96 /* Directory that the name is relative to. */
99 /* Length and contents of name. */
100 size_t file_name_len
;
104 static struct delayed_set_stat
*delayed_set_stat_head
;
106 /* List of links whose creation we have delayed. */
109 /* The next delayed link in the list. */
110 struct delayed_link
*next
;
112 /* The device, inode number and ctime of the placeholder. Use
113 ctime, not mtime, to make false matches less likely if some
114 other process removes the placeholder. */
117 struct timespec ctime
;
119 /* True if the link is symbolic. */
122 /* The desired metadata, valid only the link is symbolic. */
126 struct timespec atime
;
127 struct timespec mtime
;
129 /* The directory that the sources and target are relative to. */
132 /* A list of sources for this link. The sources are all to be
133 hard-linked together. */
134 struct string_list
*sources
;
136 /* The desired target of the desired link. */
140 static struct delayed_link
*delayed_link_head
;
144 struct string_list
*next
;
148 /* Set up to extract files. */
152 we_are_root
= geteuid () == 0;
153 same_permissions_option
+= we_are_root
;
154 same_owner_option
+= we_are_root
;
156 /* Option -p clears the kernel umask, so it does not affect proper
157 restoration of file permissions. New intermediate directories will
158 comply with umask at start of program. */
160 newdir_umask
= umask (0);
161 if (0 < same_permissions_option
)
165 umask (newdir_umask
); /* restore the kernel umask */
166 current_umask
= newdir_umask
;
170 /* Use fchmod if possible, fchmodat otherwise. */
172 fd_chmod (int fd
, char const *file
, mode_t mode
, int atflag
)
176 int result
= fchmod (fd
, mode
);
177 if (result
== 0 || implemented (errno
))
180 return fchmodat (AT_FDCWD
, file
, mode
, atflag
);
183 /* Use fchown if possible, fchownat otherwise. */
185 fd_chown (int fd
, char const *file
, uid_t uid
, gid_t gid
, int atflag
)
189 int result
= fchown (fd
, uid
, gid
);
190 if (result
== 0 || implemented (errno
))
193 return fchownat (AT_FDCWD
, file
, uid
, gid
, atflag
);
196 /* Use fstat if possible, fstatat otherwise. */
198 fd_stat (int fd
, char const *file
, struct stat
*st
, int atflag
)
202 : fstatat (AT_FDCWD
, file
, st
, atflag
));
205 /* Set the mode for FILE_NAME to MODE.
206 MODE_MASK specifies the bits of MODE that we care about;
207 thus if MODE_MASK is zero, do nothing.
208 If FD is nonnegative, it is a file descriptor for the file.
209 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
210 the file's current mode, using the style of struct delayed_set_stat.
211 TYPEFLAG specifies the type of the file.
212 ATFLAG specifies the flag to use when statting the file. */
214 set_mode (char const *file_name
,
215 mode_t mode
, mode_t mode_mask
, int fd
,
216 mode_t current_mode
, mode_t current_mode_mask
,
217 char typeflag
, int atflag
)
219 if (((current_mode
^ mode
) | ~ current_mode_mask
) & mode_mask
)
221 if (MODE_ALL
& ~ mode_mask
& ~ current_mode_mask
)
224 if (fd_stat (fd
, file_name
, &st
, atflag
) != 0)
226 stat_error (file_name
);
229 current_mode
= st
.st_mode
;
232 current_mode
&= MODE_ALL
;
233 mode
= (current_mode
& ~ mode_mask
) | (mode
& mode_mask
);
235 if (current_mode
!= mode
)
238 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
240 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
241 setuid-root files would otherwise be a backdoor. See
242 http://opensolaris.org/jive/thread.jspa?threadID=95826
244 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
)
245 && priv_set_restore_linkdir () == 0)
248 fd_chmod (fd
, file_name
, mode
, atflag
) == 0 ? 0 : errno
;
249 priv_set_remove_linkdir ();
252 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
253 returns ENOTSUP even when operating on non-symlinks, try
254 again with the flag disabled if it does not appear to be
255 supported and if the file is not a symlink. This
256 introduces a race, alas. */
257 if (atflag
&& typeflag
!= SYMTYPE
&& ! implemented (chmod_errno
))
258 chmod_errno
= fd_chmod (fd
, file_name
, mode
, 0) == 0 ? 0 : errno
;
261 && (typeflag
!= SYMTYPE
|| implemented (chmod_errno
)))
264 chmod_error_details (file_name
, mode
);
270 /* Check time after successfully setting FILE_NAME's time stamp to T. */
272 check_time (char const *file_name
, struct timespec t
)
275 WARNOPT (WARN_TIMESTAMP
,
276 (0, 0, _("%s: implausibly old time stamp %s"),
277 file_name
, tartime (t
, true)));
278 else if (timespec_cmp (volume_start_time
, t
) < 0)
282 if (timespec_cmp (now
, t
) < 0)
284 char buf
[TIMESPEC_STRSIZE_BOUND
];
285 struct timespec diff
;
286 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
287 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
288 if (diff
.tv_nsec
< 0)
290 diff
.tv_nsec
+= BILLION
;
293 WARNOPT (WARN_TIMESTAMP
,
294 (0, 0, _("%s: time stamp %s is %s s in the future"),
295 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
300 /* Restore stat attributes (owner, group, mode and times) for
301 FILE_NAME, using information given in *ST.
302 If FD is nonnegative, it is a file descriptor for the file.
303 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
304 the file's current mode, using the style of struct delayed_set_stat.
305 TYPEFLAG specifies the type of the file.
306 If INTERDIR, this is an intermediate directory.
307 ATFLAG specifies the flag to use when statting the file. */
310 set_stat (char const *file_name
,
311 struct tar_stat_info
const *st
,
312 int fd
, mode_t current_mode
, mode_t current_mode_mask
,
313 char typeflag
, bool interdir
, int atflag
)
315 /* Do the utime before the chmod because some versions of utime are
316 broken and trash the modes of the file. */
318 if (! touch_option
&& ! interdir
)
320 struct timespec ts
[2];
321 if (incremental_option
)
324 ts
[0].tv_nsec
= UTIME_OMIT
;
327 if (fd_utimensat (fd
, AT_FDCWD
, file_name
, ts
, atflag
) == 0)
329 if (incremental_option
)
330 check_time (file_name
, ts
[0]);
331 check_time (file_name
, ts
[1]);
333 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
334 utime_error (file_name
);
337 if (0 < same_owner_option
&& ! interdir
)
339 /* Some systems allow non-root users to give files away. Once this
340 done, it is not possible anymore to change file permissions.
341 However, setting file permissions now would be incorrect, since
342 they would apply to the wrong user, and there would be a race
343 condition. So, don't use systems that allow non-root users to
345 uid_t uid
= st
->stat
.st_uid
;
346 gid_t gid
= st
->stat
.st_gid
;
348 if (fd_chown (fd
, file_name
, uid
, gid
, atflag
) == 0)
350 /* Changing the owner can clear st_mode bits in some cases. */
351 if ((current_mode
| ~ current_mode_mask
) & S_IXUGO
)
352 current_mode_mask
&= ~ (current_mode
& (S_ISUID
| S_ISGID
));
354 else if (typeflag
!= SYMTYPE
|| implemented (errno
))
355 chown_error_details (file_name
, uid
, gid
);
359 st
->stat
.st_mode
& ~ current_umask
,
360 0 < same_permissions_option
&& ! interdir
? MODE_ALL
: MODE_RWX
,
361 fd
, current_mode
, current_mode_mask
, typeflag
, atflag
);
364 /* Remember to restore stat attributes (owner, group, mode and times)
365 for the directory FILE_NAME, using information given in *ST,
366 once we stop extracting files into that directory.
368 If ST is null, merely create a placeholder node for an intermediate
369 directory that was created by make_directories.
371 NOTICE: this works only if the archive has usual member order, i.e.
372 directory, then the files in that directory. Incremental archive have
373 somewhat reversed order: first go subdirectories, then all other
374 members. To help cope with this case the variable
375 delay_directory_restore_option is set by prepare_to_extract.
377 If an archive was explicitely created so that its member order is
378 reversed, some directory timestamps can be restored incorrectly,
380 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
383 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
384 mode_t current_mode
, mode_t current_mode_mask
,
385 mode_t mode
, int atflag
)
387 size_t file_name_len
= strlen (file_name
);
388 struct delayed_set_stat
*data
=
389 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
390 + file_name_len
+ 1);
391 data
->next
= delayed_set_stat_head
;
395 data
->dev
= st
->stat
.st_dev
;
396 data
->ino
= st
->stat
.st_ino
;
397 data
->uid
= st
->stat
.st_uid
;
398 data
->gid
= st
->stat
.st_gid
;
399 data
->atime
= st
->atime
;
400 data
->mtime
= st
->mtime
;
402 data
->file_name_len
= file_name_len
;
403 data
->current_mode
= current_mode
;
404 data
->current_mode_mask
= current_mode_mask
;
405 data
->interdir
= ! st
;
406 data
->atflag
= atflag
;
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 (fstatat (AT_FDCWD
, data
->file_name
, &st
, data
->atflag
) != 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
->current_mode
= st
.st_mode
;
442 data
->current_mode_mask
= ALL_MODE_BITS
;
443 data
->interdir
= false;
448 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
449 quotearg_colon (dir
)));
452 /* After a file/link/directory creation has failed, see if
453 it's because some required directory was not present, and if so,
454 create all required directories. Return non-zero if a directory
457 make_directories (char *file_name
)
459 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
460 char *cursor
; /* points into the file name */
461 int did_something
= 0; /* did we do anything yet? */
464 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 desired_mode
= MODE_RWX
& ~ newdir_umask
;
487 mode
= desired_mode
| (we_are_root
? 0 : MODE_WXUSR
);
488 status
= mkdir (file_name
, mode
);
492 /* Create a struct delayed_set_stat even if
493 mode == desired_mode, because
494 repair_delayed_set_stat may need to update the struct. */
495 delay_set_stat (file_name
,
496 0, mode
& ~ current_umask
, MODE_RWX
,
497 desired_mode
, AT_SYMLINK_NOFOLLOW
);
499 print_for_mkdir (file_name
, cursor
- file_name
, desired_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 mode_t current_mode
= data
->current_mode
;
631 mode_t current_mode_mask
= data
->current_mode_mask
;
633 check_for_renamed_directories
|= data
->after_links
;
635 if (after_links
< data
->after_links
636 || (data
->file_name_len
< file_name_len
637 && file_name
[data
->file_name_len
]
638 && (ISSLASH (file_name
[data
->file_name_len
])
639 || ISSLASH (file_name
[data
->file_name_len
- 1]))
640 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
643 chdir_do (data
->change_dir
);
645 if (check_for_renamed_directories
)
647 if (fstatat (AT_FDCWD
, data
->file_name
, &st
, data
->atflag
) != 0)
649 stat_error (data
->file_name
);
654 current_mode
= st
.st_mode
;
655 current_mode_mask
= ALL_MODE_BITS
;
656 if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
659 _("%s: Directory renamed before its status could be extracted"),
660 quotearg_colon (data
->file_name
)));
668 struct tar_stat_info sb
;
669 sb
.stat
.st_mode
= data
->mode
;
670 sb
.stat
.st_uid
= data
->uid
;
671 sb
.stat
.st_gid
= data
->gid
;
672 sb
.atime
= data
->atime
;
673 sb
.mtime
= data
->mtime
;
674 set_stat (data
->file_name
, &sb
,
675 -1, current_mode
, current_mode_mask
,
676 DIRTYPE
, data
->interdir
, data
->atflag
);
679 delayed_set_stat_head
= data
->next
;
686 /* Extractor functions for various member types */
689 extract_dir (char *file_name
, int typeflag
)
693 mode_t current_mode
= 0;
694 mode_t current_mode_mask
= 0;
696 bool interdir_made
= false;
698 /* Save 'root device' to avoid purging mount points. */
699 if (one_file_system_option
&& root_device
== 0)
703 if (stat (".", &st
) != 0)
706 root_device
= st
.st_dev
;
709 if (incremental_option
)
710 /* Read the entry and delete files that aren't listed in the archive. */
711 purge_directory (file_name
);
712 else if (typeflag
== GNUTYPE_DUMPDIR
)
715 /* If ownership or permissions will be restored later, create the
716 directory with restrictive permissions at first, so that in the
717 meantime processes owned by other users do not inadvertently
718 create files under this directory that inherit the wrong owner,
719 group, or permissions from the directory. If not root, though,
720 make the directory writeable and searchable at first, so that
721 files can be created under it. */
722 mode
= ((current_stat_info
.stat
.st_mode
723 & (0 < same_owner_option
|| 0 < same_permissions_option
726 | (we_are_root
? 0 : MODE_WXUSR
));
730 status
= mkdir (file_name
, mode
);
733 current_mode
= mode
& ~ current_umask
;
734 current_mode_mask
= MODE_RWX
;
735 atflag
= AT_SYMLINK_NOFOLLOW
;
741 || old_files_option
== DEFAULT_OLD_FILES
742 || old_files_option
== OVERWRITE_OLD_FILES
))
745 if (stat (file_name
, &st
) == 0)
747 current_mode
= st
.st_mode
;
748 current_mode_mask
= ALL_MODE_BITS
;
750 if (S_ISDIR (current_mode
))
754 repair_delayed_set_stat (file_name
, &st
);
763 switch (maybe_recoverable (file_name
, &interdir_made
))
774 mkdir_error (file_name
);
783 || old_files_option
== DEFAULT_OLD_FILES
784 || old_files_option
== OVERWRITE_OLD_FILES
)
785 delay_set_stat (file_name
, ¤t_stat_info
,
786 current_mode
, current_mode_mask
,
787 current_stat_info
.stat
.st_mode
, atflag
);
794 open_output_file (char const *file_name
, int typeflag
, mode_t mode
,
795 mode_t
*current_mode
, mode_t
*current_mode_mask
)
798 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
799 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
800 | (overwriting_old_files
? O_TRUNC
: O_EXCL
));
803 /* Contiguous files (on the Masscomp) have to specify the size in
804 the open call that creates them. */
806 if (typeflag
== CONTTYPE
)
807 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
809 fd
= open (file_name
, openflag
, mode
);
811 #else /* not O_CTG */
812 if (typeflag
== CONTTYPE
)
814 static int conttype_diagnosed
;
816 if (!conttype_diagnosed
)
818 conttype_diagnosed
= 1;
819 WARNOPT (WARN_CONTIGUOUS_CAST
,
820 (0, 0, _("Extracting contiguous files as regular files")));
823 fd
= open (file_name
, openflag
, mode
);
825 #endif /* not O_CTG */
829 if (overwriting_old_files
)
832 if (fstat (fd
, &st
) != 0)
839 if (! S_ISREG (st
.st_mode
))
845 *current_mode
= st
.st_mode
;
846 *current_mode_mask
= ALL_MODE_BITS
;
850 *current_mode
= mode
& ~ current_umask
;
851 *current_mode_mask
= MODE_RWX
;
859 extract_file (char *file_name
, int typeflag
)
863 union block
*data_block
;
867 bool interdir_made
= false;
868 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
869 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
870 mode_t current_mode
= 0;
871 mode_t current_mode_mask
= 0;
873 if (to_stdout_option
)
875 else if (to_command_option
)
877 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
886 int recover
= RECOVER_NO
;
888 fd
= open_output_file (file_name
, typeflag
, mode
,
889 ¤t_mode
, ¤t_mode_mask
);
891 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
897 if (recover
== RECOVER_SKIP
)
899 open_error (file_name
);
904 mv_begin_read (¤t_stat_info
);
905 if (current_stat_info
.is_sparse
)
906 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
908 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
912 /* Locate data, determine max length writeable, write it,
913 block that we have used the data, then check if the write
916 data_block
= find_next_block ();
919 ERROR ((0, 0, _("Unexpected EOF in archive")));
920 break; /* FIXME: What happens, then? */
923 written
= available_space_after (data_block
);
928 count
= full_write (fd
, data_block
->buffer
, written
);
931 set_next_block_after ((union block
*)
932 (data_block
->buffer
+ written
- 1));
933 if (count
!= written
)
935 if (!to_command_option
)
936 write_error_details (file_name
, count
, written
);
937 /* FIXME: shouldn't we restore from backup? */
946 /* If writing to stdout, don't try to do anything to the filename;
947 it doesn't exist, or we don't want to touch it anyway. */
949 if (to_stdout_option
)
952 if (! to_command_option
)
953 set_stat (file_name
, ¤t_stat_info
, fd
,
954 current_mode
, current_mode_mask
, typeflag
, false,
955 (old_files_option
== OVERWRITE_OLD_FILES
956 ? 0 : AT_SYMLINK_NOFOLLOW
));
960 close_error (file_name
);
962 if (to_command_option
)
968 /* Create a placeholder file with name FILE_NAME, which will be
969 replaced after other extraction is done by a symbolic link if
970 IS_SYMLINK is true, and by a hard link otherwise. Set
971 *INTERDIR_MADE if an intermediate directory is made in the
975 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
980 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
982 switch (maybe_recoverable (file_name
, interdir_made
))
991 open_error (file_name
);
996 if (fstat (fd
, &st
) != 0)
998 stat_error (file_name
);
1001 else if (close (fd
) != 0)
1002 close_error (file_name
);
1005 struct delayed_set_stat
*h
;
1006 struct delayed_link
*p
=
1007 xmalloc (offsetof (struct delayed_link
, target
)
1008 + strlen (current_stat_info
.link_name
)
1010 p
->next
= delayed_link_head
;
1011 delayed_link_head
= p
;
1014 p
->ctime
= get_stat_ctime (&st
);
1015 p
->is_symlink
= is_symlink
;
1018 p
->mode
= current_stat_info
.stat
.st_mode
;
1019 p
->uid
= current_stat_info
.stat
.st_uid
;
1020 p
->gid
= current_stat_info
.stat
.st_gid
;
1021 p
->atime
= current_stat_info
.atime
;
1022 p
->mtime
= current_stat_info
.mtime
;
1024 p
->change_dir
= chdir_current
;
1025 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
1026 + strlen (file_name
) + 1);
1027 p
->sources
->next
= 0;
1028 strcpy (p
->sources
->string
, file_name
);
1029 strcpy (p
->target
, current_stat_info
.link_name
);
1031 h
= delayed_set_stat_head
;
1032 if (h
&& ! h
->after_links
1033 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
1034 && ISSLASH (file_name
[h
->file_name_len
])
1035 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
1041 if (stat (h
->file_name
, &st
) != 0)
1042 stat_error (h
->file_name
);
1049 while ((h
= h
->next
) && ! h
->after_links
);
1059 extract_link (char *file_name
, int typeflag
)
1061 bool interdir_made
= false;
1062 char const *link_name
;
1065 link_name
= current_stat_info
.link_name
;
1067 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1068 return create_placeholder_file (file_name
, false, &interdir_made
);
1072 struct stat st1
, st2
;
1074 int status
= link (link_name
, file_name
);
1079 struct delayed_link
*ds
= delayed_link_head
;
1080 if (ds
&& lstat (link_name
, &st1
) == 0)
1081 for (; ds
; ds
= ds
->next
)
1082 if (ds
->change_dir
== chdir_current
1083 && ds
->dev
== st1
.st_dev
1084 && ds
->ino
== st1
.st_ino
1085 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
1087 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1088 + strlen (file_name
) + 1);
1089 strcpy (p
->string
, file_name
);
1090 p
->next
= ds
->sources
;
1096 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1097 || (lstat (link_name
, &st1
) == 0
1098 && lstat (file_name
, &st2
) == 0
1099 && st1
.st_dev
== st2
.st_dev
1100 && st1
.st_ino
== st2
.st_ino
))
1105 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1107 if (rc
== RECOVER_SKIP
)
1109 if (!(incremental_option
&& errno
== EEXIST
))
1111 link_error (link_name
, file_name
);
1118 extract_symlink (char *file_name
, int typeflag
)
1121 bool interdir_made
= false;
1123 if (! absolute_names_option
1124 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1125 || contains_dot_dot (current_stat_info
.link_name
)))
1126 return create_placeholder_file (file_name
, true, &interdir_made
);
1128 while (symlink (current_stat_info
.link_name
, file_name
))
1129 switch (maybe_recoverable (file_name
, &interdir_made
))
1138 symlink_error (current_stat_info
.link_name
, file_name
);
1142 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1143 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1147 static int warned_once
;
1152 WARNOPT (WARN_SYMBOLIC_CAST
,
1154 _("Attempting extraction of symbolic links as hard links")));
1156 return extract_link (file_name
, typeflag
);
1160 #if S_IFCHR || S_IFBLK
1162 extract_node (char *file_name
, int typeflag
)
1164 bool interdir_made
= false;
1165 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1166 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1168 while (mknod (file_name
, mode
, current_stat_info
.stat
.st_rdev
) != 0)
1169 switch (maybe_recoverable (file_name
, &interdir_made
))
1178 mknod_error (file_name
);
1182 set_stat (file_name
, ¤t_stat_info
, -1,
1183 mode
& ~ current_umask
, MODE_RWX
,
1184 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1189 #if HAVE_MKFIFO || defined mkfifo
1191 extract_fifo (char *file_name
, int typeflag
)
1193 bool interdir_made
= false;
1194 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1195 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1197 while (mkfifo (file_name
, mode
) != 0)
1198 switch (maybe_recoverable (file_name
, &interdir_made
))
1207 mkfifo_error (file_name
);
1211 set_stat (file_name
, ¤t_stat_info
, -1,
1212 mode
& ~ current_umask
, MODE_RWX
,
1213 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1219 extract_volhdr (char *file_name
, int typeflag
)
1226 extract_failure (char *file_name
, int typeflag
)
1231 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1235 /* Prepare to extract a file. Find extractor function.
1236 Return zero if extraction should not proceed. */
1239 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1243 if (EXTRACT_OVER_PIPE
)
1246 /* Select the extractor */
1249 case GNUTYPE_SPARSE
:
1250 *fun
= extract_file
;
1257 /* Appears to be a file. But BSD tar uses the convention that a slash
1258 suffix means a directory. */
1259 if (current_stat_info
.had_trailing_slash
)
1263 *fun
= extract_file
;
1269 *fun
= extract_symlink
;
1273 *fun
= extract_link
;
1278 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1279 *fun
= extract_node
;
1285 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1286 *fun
= extract_node
;
1290 #if HAVE_MKFIFO || defined mkfifo
1292 *fun
= extract_fifo
;
1297 case GNUTYPE_DUMPDIR
:
1299 if (current_stat_info
.is_dumpdir
)
1300 delay_directory_restore_option
= true;
1303 case GNUTYPE_VOLHDR
:
1304 *fun
= extract_volhdr
;
1307 case GNUTYPE_MULTIVOL
:
1309 _("%s: Cannot extract -- file is continued from another volume"),
1310 quotearg_colon (current_stat_info
.file_name
)));
1311 *fun
= extract_failure
;
1314 case GNUTYPE_LONGNAME
:
1315 case GNUTYPE_LONGLINK
:
1316 ERROR ((0, 0, _("Unexpected long name header")));
1317 *fun
= extract_failure
;
1321 WARNOPT (WARN_UNKNOWN_CAST
,
1323 _("%s: Unknown file type `%c', extracted as normal file"),
1324 quotearg_colon (file_name
), typeflag
));
1325 *fun
= extract_file
;
1328 /* Determine whether the extraction should proceed */
1332 switch (old_files_option
)
1334 case UNLINK_FIRST_OLD_FILES
:
1335 if (!remove_any_file (file_name
,
1336 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1337 : ORDINARY_REMOVE_OPTION
)
1338 && errno
&& errno
!= ENOENT
)
1340 unlink_error (file_name
);
1345 case KEEP_NEWER_FILES
:
1346 if (file_newer_p (file_name
, ¤t_stat_info
))
1348 WARNOPT (WARN_IGNORE_NEWER
,
1349 (0, 0, _("Current %s is newer or same age"),
1350 quote (file_name
)));
1362 /* Extract a file from the archive. */
1364 extract_archive (void)
1367 tar_extractor_t fun
;
1369 fatal_exit_hook
= extract_finish
;
1371 set_next_block_after (current_header
);
1373 if (!current_stat_info
.file_name
[0]
1374 || (interactive_option
1375 && !confirm ("extract", current_stat_info
.file_name
)))
1381 /* Print the block from current_header and current_stat. */
1383 print_header (¤t_stat_info
, current_header
, -1);
1385 /* Restore stats for all non-ancestor directories, unless
1386 it is an incremental archive.
1387 (see NOTICE in the comment to delay_set_stat above) */
1388 if (!delay_directory_restore_option
)
1390 int dir
= chdir_current
;
1391 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1395 /* Take a safety backup of a previously existing file. */
1398 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1401 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1402 quotearg_colon (current_stat_info
.file_name
)));
1407 /* Extract the archive entry according to its type. */
1409 typeflag
= sparse_member_p (¤t_stat_info
) ?
1410 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1412 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1414 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1416 undo_last_backup ();
1423 /* Extract the links whose final extraction were delayed. */
1425 apply_delayed_links (void)
1427 struct delayed_link
*ds
;
1429 for (ds
= delayed_link_head
; ds
; )
1431 struct string_list
*sources
= ds
->sources
;
1432 char const *valid_source
= 0;
1434 chdir_do (ds
->change_dir
);
1436 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1438 char const *source
= sources
->string
;
1441 /* Make sure the placeholder file is still there. If not,
1442 don't create a link, as the placeholder was probably
1443 removed by a later extraction. */
1444 if (lstat (source
, &st
) == 0
1445 && st
.st_dev
== ds
->dev
1446 && st
.st_ino
== ds
->ino
1447 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1449 /* Unlink the placeholder, then create a hard link if possible,
1450 a symbolic link otherwise. */
1451 if (unlink (source
) != 0)
1452 unlink_error (source
);
1453 else if (valid_source
&& link (valid_source
, source
) == 0)
1455 else if (!ds
->is_symlink
)
1457 if (link (ds
->target
, source
) != 0)
1458 link_error (ds
->target
, source
);
1460 else if (symlink (ds
->target
, source
) != 0)
1461 symlink_error (ds
->target
, source
);
1464 struct tar_stat_info st1
;
1465 st1
.stat
.st_mode
= ds
->mode
;
1466 st1
.stat
.st_uid
= ds
->uid
;
1467 st1
.stat
.st_gid
= ds
->gid
;
1468 st1
.atime
= ds
->atime
;
1469 st1
.mtime
= ds
->mtime
;
1470 set_stat (source
, &st1
, -1, 0, 0, SYMTYPE
,
1471 false, AT_SYMLINK_NOFOLLOW
);
1472 valid_source
= source
;
1477 for (sources
= ds
->sources
; sources
; )
1479 struct string_list
*next
= sources
->next
;
1485 struct delayed_link
*next
= ds
->next
;
1491 delayed_link_head
= 0;
1494 /* Finish the extraction of an archive. */
1496 extract_finish (void)
1498 /* First, fix the status of ordinary directories that need fixing. */
1499 apply_nonancestor_delayed_set_stat ("", 0);
1501 /* Then, apply delayed links, so that they don't affect delayed
1502 directory status-setting for ordinary directories. */
1503 apply_delayed_links ();
1505 /* Finally, fix the status of directories that are ancestors
1506 of delayed links. */
1507 apply_nonancestor_delayed_set_stat ("", 1);
1511 rename_directory (char *src
, char *dst
)
1513 if (rename (src
, dst
))
1520 if (make_directories (dst
))
1522 if (rename (src
, dst
) == 0)
1529 /* FIXME: Fall back to recursive copying */
1535 ERROR ((0, e
, _("Cannot rename %s to %s"),