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 /* For each entry H in the leading prefix of entries in HEAD that do
365 not have after_links marked, mark H and fill in its dev and ino
366 members. Assume HEAD && ! HEAD->after_links. */
368 mark_after_links (struct delayed_set_stat
*head
)
370 struct delayed_set_stat
*h
= head
;
377 if (stat (h
->file_name
, &st
) != 0)
378 stat_error (h
->file_name
);
385 while ((h
= h
->next
) && ! h
->after_links
);
388 /* Remember to restore stat attributes (owner, group, mode and times)
389 for the directory FILE_NAME, using information given in *ST,
390 once we stop extracting files into that directory.
392 If ST is null, merely create a placeholder node for an intermediate
393 directory that was created by make_directories.
395 NOTICE: this works only if the archive has usual member order, i.e.
396 directory, then the files in that directory. Incremental archive have
397 somewhat reversed order: first go subdirectories, then all other
398 members. To help cope with this case the variable
399 delay_directory_restore_option is set by prepare_to_extract.
401 If an archive was explicitely created so that its member order is
402 reversed, some directory timestamps can be restored incorrectly,
404 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
407 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
408 mode_t current_mode
, mode_t current_mode_mask
,
409 mode_t mode
, int atflag
)
411 size_t file_name_len
= strlen (file_name
);
412 struct delayed_set_stat
*data
=
413 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
414 + file_name_len
+ 1);
415 data
->next
= delayed_set_stat_head
;
419 data
->dev
= st
->stat
.st_dev
;
420 data
->ino
= st
->stat
.st_ino
;
421 data
->uid
= st
->stat
.st_uid
;
422 data
->gid
= st
->stat
.st_gid
;
423 data
->atime
= st
->atime
;
424 data
->mtime
= st
->mtime
;
426 data
->file_name_len
= file_name_len
;
427 data
->current_mode
= current_mode
;
428 data
->current_mode_mask
= current_mode_mask
;
429 data
->interdir
= ! st
;
430 data
->atflag
= atflag
;
431 data
->after_links
= 0;
432 data
->change_dir
= chdir_current
;
433 strcpy (data
->file_name
, file_name
);
434 delayed_set_stat_head
= data
;
435 if (must_be_dot_or_slash (file_name
))
436 mark_after_links (data
);
439 /* Update the delayed_set_stat info for an intermediate directory
440 created within the file name of DIR. The intermediate directory turned
441 out to be the same as this directory, e.g. due to ".." or symbolic
442 links. *DIR_STAT_INFO is the status of the directory. */
444 repair_delayed_set_stat (char const *dir
,
445 struct stat
const *dir_stat_info
)
447 struct delayed_set_stat
*data
;
448 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
451 if (fstatat (AT_FDCWD
, data
->file_name
, &st
, data
->atflag
) != 0)
453 stat_error (data
->file_name
);
457 if (st
.st_dev
== dir_stat_info
->st_dev
458 && st
.st_ino
== dir_stat_info
->st_ino
)
460 data
->dev
= current_stat_info
.stat
.st_dev
;
461 data
->ino
= current_stat_info
.stat
.st_ino
;
462 data
->mode
= current_stat_info
.stat
.st_mode
;
463 data
->uid
= current_stat_info
.stat
.st_uid
;
464 data
->gid
= current_stat_info
.stat
.st_gid
;
465 data
->atime
= current_stat_info
.atime
;
466 data
->mtime
= current_stat_info
.mtime
;
467 data
->current_mode
= st
.st_mode
;
468 data
->current_mode_mask
= ALL_MODE_BITS
;
469 data
->interdir
= false;
474 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
475 quotearg_colon (dir
)));
478 /* After a file/link/directory creation has failed, see if
479 it's because some required directory was not present, and if so,
480 create all required directories. Return non-zero if a directory
483 make_directories (char *file_name
)
485 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
486 char *cursor
; /* points into the file name */
487 int did_something
= 0; /* did we do anything yet? */
490 for (cursor
= cursor0
; *cursor
; cursor
++)
495 if (! ISSLASH (*cursor
))
498 /* Avoid mkdir of empty string, if leading or double '/'. */
500 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
503 /* Avoid mkdir where last part of file name is "." or "..". */
505 if (cursor
[-1] == '.'
506 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
507 || (cursor
[-2] == '.'
508 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
511 *cursor
= '\0'; /* truncate the name there */
512 desired_mode
= MODE_RWX
& ~ newdir_umask
;
513 mode
= desired_mode
| (we_are_root
? 0 : MODE_WXUSR
);
514 status
= mkdir (file_name
, mode
);
518 /* Create a struct delayed_set_stat even if
519 mode == desired_mode, because
520 repair_delayed_set_stat may need to update the struct. */
521 delay_set_stat (file_name
,
522 0, mode
& ~ current_umask
, MODE_RWX
,
523 desired_mode
, AT_SYMLINK_NOFOLLOW
);
525 print_for_mkdir (file_name
, cursor
- file_name
, desired_mode
);
535 continue; /* Directory already exists. */
536 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
537 this. Reported by Warren Hyde
538 <Warren.Hyde@motorola.com> */
539 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
540 && access (file_name
, W_OK
) == 0)
543 /* Some other error in the mkdir. We return to the caller. */
547 return did_something
; /* tell them to retry if we made one */
551 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
555 if (stat (file_name
, &st
))
559 stat_warn (file_name
);
560 /* Be on the safe side: if the file does exist assume it is newer */
565 if (!S_ISDIR (st
.st_mode
)
566 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
575 #define RECOVER_SKIP 2
577 /* Attempt repairing what went wrong with the extraction. Delete an
578 already existing file or create missing intermediate directories.
579 Return RECOVER_OK if we somewhat increased our chances at a successful
580 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
581 caller should skip extraction of that member. The value of errno is
582 properly restored on returning RECOVER_NO. */
585 maybe_recoverable (char *file_name
, bool *interdir_made
)
595 /* Remove an old file, if the options allow this. */
597 switch (old_files_option
)
602 case KEEP_NEWER_FILES
:
603 if (file_newer_p (file_name
, ¤t_stat_info
))
610 case DEFAULT_OLD_FILES
:
611 case NO_OVERWRITE_DIR_OLD_FILES
:
612 case OVERWRITE_OLD_FILES
:
614 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
616 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
619 case UNLINK_FIRST_OLD_FILES
:
624 /* Attempt creating missing intermediate directories. */
625 if (! make_directories (file_name
))
630 *interdir_made
= true;
634 /* Just say we can't do anything about it... */
640 /* Fix the statuses of all directories whose statuses need fixing, and
641 which are not ancestors of FILE_NAME. If AFTER_LINKS is
642 nonzero, do this for all such directories; otherwise, stop at the
643 first directory that is marked to be fixed up only after delayed
644 links are applied. */
646 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
648 size_t file_name_len
= strlen (file_name
);
649 bool check_for_renamed_directories
= 0;
651 while (delayed_set_stat_head
)
653 struct delayed_set_stat
*data
= delayed_set_stat_head
;
654 bool skip_this_one
= 0;
656 mode_t current_mode
= data
->current_mode
;
657 mode_t current_mode_mask
= data
->current_mode_mask
;
659 check_for_renamed_directories
|= data
->after_links
;
661 if (after_links
< data
->after_links
662 || (data
->file_name_len
< file_name_len
663 && file_name
[data
->file_name_len
]
664 && (ISSLASH (file_name
[data
->file_name_len
])
665 || ISSLASH (file_name
[data
->file_name_len
- 1]))
666 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
669 chdir_do (data
->change_dir
);
671 if (check_for_renamed_directories
)
673 if (fstatat (AT_FDCWD
, data
->file_name
, &st
, data
->atflag
) != 0)
675 stat_error (data
->file_name
);
680 current_mode
= st
.st_mode
;
681 current_mode_mask
= ALL_MODE_BITS
;
682 if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
685 _("%s: Directory renamed before its status could be extracted"),
686 quotearg_colon (data
->file_name
)));
694 struct tar_stat_info sb
;
695 sb
.stat
.st_mode
= data
->mode
;
696 sb
.stat
.st_uid
= data
->uid
;
697 sb
.stat
.st_gid
= data
->gid
;
698 sb
.atime
= data
->atime
;
699 sb
.mtime
= data
->mtime
;
700 set_stat (data
->file_name
, &sb
,
701 -1, current_mode
, current_mode_mask
,
702 DIRTYPE
, data
->interdir
, data
->atflag
);
705 delayed_set_stat_head
= data
->next
;
712 /* Extractor functions for various member types */
715 extract_dir (char *file_name
, int typeflag
)
719 mode_t current_mode
= 0;
720 mode_t current_mode_mask
= 0;
722 bool interdir_made
= false;
724 /* Save 'root device' to avoid purging mount points. */
725 if (one_file_system_option
&& root_device
== 0)
729 if (stat (".", &st
) != 0)
732 root_device
= st
.st_dev
;
735 if (incremental_option
)
736 /* Read the entry and delete files that aren't listed in the archive. */
737 purge_directory (file_name
);
738 else if (typeflag
== GNUTYPE_DUMPDIR
)
741 /* If ownership or permissions will be restored later, create the
742 directory with restrictive permissions at first, so that in the
743 meantime processes owned by other users do not inadvertently
744 create files under this directory that inherit the wrong owner,
745 group, or permissions from the directory. If not root, though,
746 make the directory writeable and searchable at first, so that
747 files can be created under it. */
748 mode
= ((current_stat_info
.stat
.st_mode
749 & (0 < same_owner_option
|| 0 < same_permissions_option
752 | (we_are_root
? 0 : MODE_WXUSR
));
756 status
= mkdir (file_name
, mode
);
759 current_mode
= mode
& ~ current_umask
;
760 current_mode_mask
= MODE_RWX
;
761 atflag
= AT_SYMLINK_NOFOLLOW
;
767 || old_files_option
== DEFAULT_OLD_FILES
768 || old_files_option
== OVERWRITE_OLD_FILES
))
771 if (stat (file_name
, &st
) == 0)
773 current_mode
= st
.st_mode
;
774 current_mode_mask
= ALL_MODE_BITS
;
776 if (S_ISDIR (current_mode
))
780 repair_delayed_set_stat (file_name
, &st
);
789 switch (maybe_recoverable (file_name
, &interdir_made
))
800 mkdir_error (file_name
);
809 || old_files_option
== DEFAULT_OLD_FILES
810 || old_files_option
== OVERWRITE_OLD_FILES
)
811 delay_set_stat (file_name
, ¤t_stat_info
,
812 current_mode
, current_mode_mask
,
813 current_stat_info
.stat
.st_mode
, atflag
);
820 open_output_file (char const *file_name
, int typeflag
, mode_t mode
,
821 mode_t
*current_mode
, mode_t
*current_mode_mask
)
824 bool overwriting_old_files
= old_files_option
== OVERWRITE_OLD_FILES
;
825 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
826 | (overwriting_old_files
? O_TRUNC
: O_EXCL
));
829 /* Contiguous files (on the Masscomp) have to specify the size in
830 the open call that creates them. */
832 if (typeflag
== CONTTYPE
)
833 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
835 fd
= open (file_name
, openflag
, mode
);
837 #else /* not O_CTG */
838 if (typeflag
== CONTTYPE
)
840 static int conttype_diagnosed
;
842 if (!conttype_diagnosed
)
844 conttype_diagnosed
= 1;
845 WARNOPT (WARN_CONTIGUOUS_CAST
,
846 (0, 0, _("Extracting contiguous files as regular files")));
849 fd
= open (file_name
, openflag
, mode
);
851 #endif /* not O_CTG */
855 if (overwriting_old_files
)
858 if (fstat (fd
, &st
) != 0)
865 if (! S_ISREG (st
.st_mode
))
871 *current_mode
= st
.st_mode
;
872 *current_mode_mask
= ALL_MODE_BITS
;
876 *current_mode
= mode
& ~ current_umask
;
877 *current_mode_mask
= MODE_RWX
;
885 extract_file (char *file_name
, int typeflag
)
889 union block
*data_block
;
893 bool interdir_made
= false;
894 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
895 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
896 mode_t current_mode
= 0;
897 mode_t current_mode_mask
= 0;
899 if (to_stdout_option
)
901 else if (to_command_option
)
903 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
912 int recover
= RECOVER_NO
;
914 fd
= open_output_file (file_name
, typeflag
, mode
,
915 ¤t_mode
, ¤t_mode_mask
);
917 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
923 if (recover
== RECOVER_SKIP
)
925 open_error (file_name
);
930 mv_begin_read (¤t_stat_info
);
931 if (current_stat_info
.is_sparse
)
932 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
934 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
938 /* Locate data, determine max length writeable, write it,
939 block that we have used the data, then check if the write
942 data_block
= find_next_block ();
945 ERROR ((0, 0, _("Unexpected EOF in archive")));
946 break; /* FIXME: What happens, then? */
949 written
= available_space_after (data_block
);
954 count
= full_write (fd
, data_block
->buffer
, written
);
957 set_next_block_after ((union block
*)
958 (data_block
->buffer
+ written
- 1));
959 if (count
!= written
)
961 if (!to_command_option
)
962 write_error_details (file_name
, count
, written
);
963 /* FIXME: shouldn't we restore from backup? */
972 /* If writing to stdout, don't try to do anything to the filename;
973 it doesn't exist, or we don't want to touch it anyway. */
975 if (to_stdout_option
)
978 if (! to_command_option
)
979 set_stat (file_name
, ¤t_stat_info
, fd
,
980 current_mode
, current_mode_mask
, typeflag
, false,
981 (old_files_option
== OVERWRITE_OLD_FILES
982 ? 0 : AT_SYMLINK_NOFOLLOW
));
986 close_error (file_name
);
988 if (to_command_option
)
994 /* Create a placeholder file with name FILE_NAME, which will be
995 replaced after other extraction is done by a symbolic link if
996 IS_SYMLINK is true, and by a hard link otherwise. Set
997 *INTERDIR_MADE if an intermediate directory is made in the
1001 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
1006 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
1008 switch (maybe_recoverable (file_name
, interdir_made
))
1017 open_error (file_name
);
1022 if (fstat (fd
, &st
) != 0)
1024 stat_error (file_name
);
1027 else if (close (fd
) != 0)
1028 close_error (file_name
);
1031 struct delayed_set_stat
*h
;
1032 struct delayed_link
*p
=
1033 xmalloc (offsetof (struct delayed_link
, target
)
1034 + strlen (current_stat_info
.link_name
)
1036 p
->next
= delayed_link_head
;
1037 delayed_link_head
= p
;
1040 p
->ctime
= get_stat_ctime (&st
);
1041 p
->is_symlink
= is_symlink
;
1044 p
->mode
= current_stat_info
.stat
.st_mode
;
1045 p
->uid
= current_stat_info
.stat
.st_uid
;
1046 p
->gid
= current_stat_info
.stat
.st_gid
;
1047 p
->atime
= current_stat_info
.atime
;
1048 p
->mtime
= current_stat_info
.mtime
;
1050 p
->change_dir
= chdir_current
;
1051 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
1052 + strlen (file_name
) + 1);
1053 p
->sources
->next
= 0;
1054 strcpy (p
->sources
->string
, file_name
);
1055 strcpy (p
->target
, current_stat_info
.link_name
);
1057 h
= delayed_set_stat_head
;
1058 if (h
&& ! h
->after_links
1059 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
1060 && ISSLASH (file_name
[h
->file_name_len
])
1061 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
1062 mark_after_links (h
);
1071 extract_link (char *file_name
, int typeflag
)
1073 bool interdir_made
= false;
1074 char const *link_name
;
1077 link_name
= current_stat_info
.link_name
;
1079 if (! absolute_names_option
&& contains_dot_dot (link_name
))
1080 return create_placeholder_file (file_name
, false, &interdir_made
);
1084 struct stat st1
, st2
;
1086 int status
= link (link_name
, file_name
);
1091 struct delayed_link
*ds
= delayed_link_head
;
1092 if (ds
&& lstat (link_name
, &st1
) == 0)
1093 for (; ds
; ds
= ds
->next
)
1094 if (ds
->change_dir
== chdir_current
1095 && ds
->dev
== st1
.st_dev
1096 && ds
->ino
== st1
.st_ino
1097 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
1099 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
1100 + strlen (file_name
) + 1);
1101 strcpy (p
->string
, file_name
);
1102 p
->next
= ds
->sources
;
1108 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1109 || (lstat (link_name
, &st1
) == 0
1110 && lstat (file_name
, &st2
) == 0
1111 && st1
.st_dev
== st2
.st_dev
1112 && st1
.st_ino
== st2
.st_ino
))
1117 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1119 if (rc
== RECOVER_SKIP
)
1121 if (!(incremental_option
&& errno
== EEXIST
))
1123 link_error (link_name
, file_name
);
1130 extract_symlink (char *file_name
, int typeflag
)
1133 bool interdir_made
= false;
1135 if (! absolute_names_option
1136 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1137 || contains_dot_dot (current_stat_info
.link_name
)))
1138 return create_placeholder_file (file_name
, true, &interdir_made
);
1140 while (symlink (current_stat_info
.link_name
, file_name
))
1141 switch (maybe_recoverable (file_name
, &interdir_made
))
1150 symlink_error (current_stat_info
.link_name
, file_name
);
1154 set_stat (file_name
, ¤t_stat_info
, -1, 0, 0,
1155 SYMTYPE
, false, AT_SYMLINK_NOFOLLOW
);
1159 static int warned_once
;
1164 WARNOPT (WARN_SYMBOLIC_CAST
,
1166 _("Attempting extraction of symbolic links as hard links")));
1168 return extract_link (file_name
, typeflag
);
1172 #if S_IFCHR || S_IFBLK
1174 extract_node (char *file_name
, int typeflag
)
1176 bool interdir_made
= false;
1177 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1178 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1180 while (mknod (file_name
, mode
, current_stat_info
.stat
.st_rdev
) != 0)
1181 switch (maybe_recoverable (file_name
, &interdir_made
))
1190 mknod_error (file_name
);
1194 set_stat (file_name
, ¤t_stat_info
, -1,
1195 mode
& ~ current_umask
, MODE_RWX
,
1196 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1201 #if HAVE_MKFIFO || defined mkfifo
1203 extract_fifo (char *file_name
, int typeflag
)
1205 bool interdir_made
= false;
1206 mode_t mode
= (current_stat_info
.stat
.st_mode
& MODE_RWX
1207 & ~ (0 < same_owner_option
? S_IRWXG
| S_IRWXO
: 0));
1209 while (mkfifo (file_name
, mode
) != 0)
1210 switch (maybe_recoverable (file_name
, &interdir_made
))
1219 mkfifo_error (file_name
);
1223 set_stat (file_name
, ¤t_stat_info
, -1,
1224 mode
& ~ current_umask
, MODE_RWX
,
1225 typeflag
, false, AT_SYMLINK_NOFOLLOW
);
1231 extract_volhdr (char *file_name
, int typeflag
)
1238 extract_failure (char *file_name
, int typeflag
)
1243 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1247 /* Prepare to extract a file. Find extractor function.
1248 Return zero if extraction should not proceed. */
1251 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1255 if (EXTRACT_OVER_PIPE
)
1258 /* Select the extractor */
1261 case GNUTYPE_SPARSE
:
1262 *fun
= extract_file
;
1269 /* Appears to be a file. But BSD tar uses the convention that a slash
1270 suffix means a directory. */
1271 if (current_stat_info
.had_trailing_slash
)
1275 *fun
= extract_file
;
1281 *fun
= extract_symlink
;
1285 *fun
= extract_link
;
1290 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1291 *fun
= extract_node
;
1297 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1298 *fun
= extract_node
;
1302 #if HAVE_MKFIFO || defined mkfifo
1304 *fun
= extract_fifo
;
1309 case GNUTYPE_DUMPDIR
:
1311 if (current_stat_info
.is_dumpdir
)
1312 delay_directory_restore_option
= true;
1315 case GNUTYPE_VOLHDR
:
1316 *fun
= extract_volhdr
;
1319 case GNUTYPE_MULTIVOL
:
1321 _("%s: Cannot extract -- file is continued from another volume"),
1322 quotearg_colon (current_stat_info
.file_name
)));
1323 *fun
= extract_failure
;
1326 case GNUTYPE_LONGNAME
:
1327 case GNUTYPE_LONGLINK
:
1328 ERROR ((0, 0, _("Unexpected long name header")));
1329 *fun
= extract_failure
;
1333 WARNOPT (WARN_UNKNOWN_CAST
,
1335 _("%s: Unknown file type `%c', extracted as normal file"),
1336 quotearg_colon (file_name
), typeflag
));
1337 *fun
= extract_file
;
1340 /* Determine whether the extraction should proceed */
1344 switch (old_files_option
)
1346 case UNLINK_FIRST_OLD_FILES
:
1347 if (!remove_any_file (file_name
,
1348 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1349 : ORDINARY_REMOVE_OPTION
)
1350 && errno
&& errno
!= ENOENT
)
1352 unlink_error (file_name
);
1357 case KEEP_NEWER_FILES
:
1358 if (file_newer_p (file_name
, ¤t_stat_info
))
1360 WARNOPT (WARN_IGNORE_NEWER
,
1361 (0, 0, _("Current %s is newer or same age"),
1362 quote (file_name
)));
1374 /* Extract a file from the archive. */
1376 extract_archive (void)
1379 tar_extractor_t fun
;
1381 fatal_exit_hook
= extract_finish
;
1383 set_next_block_after (current_header
);
1385 if (!current_stat_info
.file_name
[0]
1386 || (interactive_option
1387 && !confirm ("extract", current_stat_info
.file_name
)))
1393 /* Print the block from current_header and current_stat. */
1395 print_header (¤t_stat_info
, current_header
, -1);
1397 /* Restore stats for all non-ancestor directories, unless
1398 it is an incremental archive.
1399 (see NOTICE in the comment to delay_set_stat above) */
1400 if (!delay_directory_restore_option
)
1402 int dir
= chdir_current
;
1403 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1407 /* Take a safety backup of a previously existing file. */
1410 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1413 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1414 quotearg_colon (current_stat_info
.file_name
)));
1419 /* Extract the archive entry according to its type. */
1421 typeflag
= sparse_member_p (¤t_stat_info
) ?
1422 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1424 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1426 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1428 undo_last_backup ();
1435 /* Extract the links whose final extraction were delayed. */
1437 apply_delayed_links (void)
1439 struct delayed_link
*ds
;
1441 for (ds
= delayed_link_head
; ds
; )
1443 struct string_list
*sources
= ds
->sources
;
1444 char const *valid_source
= 0;
1446 chdir_do (ds
->change_dir
);
1448 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1450 char const *source
= sources
->string
;
1453 /* Make sure the placeholder file is still there. If not,
1454 don't create a link, as the placeholder was probably
1455 removed by a later extraction. */
1456 if (lstat (source
, &st
) == 0
1457 && st
.st_dev
== ds
->dev
1458 && st
.st_ino
== ds
->ino
1459 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1461 /* Unlink the placeholder, then create a hard link if possible,
1462 a symbolic link otherwise. */
1463 if (unlink (source
) != 0)
1464 unlink_error (source
);
1465 else if (valid_source
&& link (valid_source
, source
) == 0)
1467 else if (!ds
->is_symlink
)
1469 if (link (ds
->target
, source
) != 0)
1470 link_error (ds
->target
, source
);
1472 else if (symlink (ds
->target
, source
) != 0)
1473 symlink_error (ds
->target
, source
);
1476 struct tar_stat_info st1
;
1477 st1
.stat
.st_mode
= ds
->mode
;
1478 st1
.stat
.st_uid
= ds
->uid
;
1479 st1
.stat
.st_gid
= ds
->gid
;
1480 st1
.atime
= ds
->atime
;
1481 st1
.mtime
= ds
->mtime
;
1482 set_stat (source
, &st1
, -1, 0, 0, SYMTYPE
,
1483 false, AT_SYMLINK_NOFOLLOW
);
1484 valid_source
= source
;
1489 for (sources
= ds
->sources
; sources
; )
1491 struct string_list
*next
= sources
->next
;
1497 struct delayed_link
*next
= ds
->next
;
1503 delayed_link_head
= 0;
1506 /* Finish the extraction of an archive. */
1508 extract_finish (void)
1510 /* First, fix the status of ordinary directories that need fixing. */
1511 apply_nonancestor_delayed_set_stat ("", 0);
1513 /* Then, apply delayed links, so that they don't affect delayed
1514 directory status-setting for ordinary directories. */
1515 apply_delayed_links ();
1517 /* Finally, fix the status of directories that are ancestors
1518 of delayed links. */
1519 apply_nonancestor_delayed_set_stat ("", 1);
1523 rename_directory (char *src
, char *dst
)
1525 if (rename (src
, dst
))
1532 if (make_directories (dst
))
1534 if (rename (src
, dst
) == 0)
1541 /* FIXME: Fall back to recursive copying */
1547 ERROR ((0, e
, _("Cannot rename %s to %s"),