1 /* Extract files from a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2010 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-11-19.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 static bool we_are_root
; /* true if our effective uid == 0 */
31 static mode_t newdir_umask
; /* umask when creating new directories */
32 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
34 /* Status of the permissions of a file that we are extracting. */
37 /* This file may have existed already; its permissions are unknown. */
40 /* This file was created using the permissions from the archive,
41 except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
44 /* This is an intermediate directory; the archive did not specify
49 /* List of directories whose statuses we need to extract after we've
50 finished extracting their subsidiary files. If you consider each
51 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
52 represents an element where AFTER_LINKS is nonzero and [^D]
53 represents an element where AFTER_LINKS is zero, then the head
54 of the subsequence has the longest name, and each non-head element
55 in the prefix is an ancestor (in the directory hierarchy) of the
58 struct delayed_set_stat
60 struct delayed_set_stat
*next
;
66 struct timespec atime
;
67 struct timespec mtime
;
69 mode_t invert_permissions
;
70 enum permstatus permstatus
;
75 static struct delayed_set_stat
*delayed_set_stat_head
;
77 /* List of links whose creation we have delayed. */
80 /* The next delayed link in the list. */
81 struct delayed_link
*next
;
83 /* The device, inode number and 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 /* A list of sources for this link. The sources are all to be
98 hard-linked together. */
99 struct string_list
*sources
;
101 /* The desired target of the desired link. */
105 static struct delayed_link
*delayed_link_head
;
109 struct string_list
*next
;
113 /* Set up to extract files. */
117 we_are_root
= geteuid () == 0;
118 same_permissions_option
+= we_are_root
;
119 same_owner_option
+= we_are_root
;
121 /* Option -p clears the kernel umask, so it does not affect proper
122 restoration of file permissions. New intermediate directories will
123 comply with umask at start of program. */
125 newdir_umask
= umask (0);
126 if (0 < same_permissions_option
)
130 umask (newdir_umask
); /* restore the kernel umask */
131 current_umask
= newdir_umask
;
135 /* If restoring permissions, restore the mode for FILE_NAME from
136 information given in *STAT_INFO (where *CUR_INFO gives
137 the current status if CUR_INFO is nonzero); otherwise invert the
138 INVERT_PERMISSIONS bits from the file's current permissions.
139 PERMSTATUS specifies the status of the file's permissions.
140 TYPEFLAG specifies the type of the file. */
142 set_mode (char const *file_name
,
143 struct stat
const *stat_info
,
144 struct stat
const *cur_info
,
145 mode_t invert_permissions
, enum permstatus permstatus
,
151 if (0 < same_permissions_option
152 && permstatus
!= INTERDIR_PERMSTATUS
)
154 mode
= stat_info
->st_mode
;
156 /* If we created the file and it has a mode that we set already
157 with O_CREAT, then its mode is often set correctly already.
158 But if we are changing ownership, the mode's group and and
159 other permission bits were omitted originally, so it's less
160 likely that the mode is OK now. Also, on many hosts, some
161 directories inherit the setgid bits from their parents, so we
162 we must set directories' modes explicitly. */
163 if ((permstatus
== ARCHIVED_PERMSTATUS
164 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
165 && typeflag
!= DIRTYPE
166 && typeflag
!= GNUTYPE_DUMPDIR
)
169 else if (! invert_permissions
)
173 /* We must inspect a directory's current permissions, since the
174 directory may have inherited its setgid bit from its parent.
176 INVERT_PERMISSIONS happens to be nonzero only for directories
177 that we created, so there's no point optimizing this code for
182 if (stat (file_name
, &st
) != 0)
184 stat_error (file_name
);
189 mode
= cur_info
->st_mode
^ invert_permissions
;
192 chmod_errno
= chmod (file_name
, mode
) == 0 ? 0 : errno
;
193 if (chmod_errno
== EPERM
&& (mode
& S_ISUID
) != 0)
195 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
196 setuid-root files would otherwise be a backdoor. See
197 http://opensolaris.org/jive/thread.jspa?threadID=95826
199 if (priv_set_restore_linkdir () == 0)
201 chmod_errno
= chmod (file_name
, mode
) == 0 ? 0 : errno
;
202 priv_set_remove_linkdir ();
208 chmod_error_details (file_name
, mode
);
212 /* Check time after successfully setting FILE_NAME's time stamp to T. */
214 check_time (char const *file_name
, struct timespec t
)
217 WARNOPT (WARN_TIMESTAMP
,
218 (0, 0, _("%s: implausibly old time stamp %s"),
219 file_name
, tartime (t
, true)));
220 else if (timespec_cmp (volume_start_time
, t
) < 0)
224 if (timespec_cmp (now
, t
) < 0)
226 char buf
[TIMESPEC_STRSIZE_BOUND
];
227 struct timespec diff
;
228 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
229 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
230 if (diff
.tv_nsec
< 0)
232 diff
.tv_nsec
+= BILLION
;
235 WARNOPT (WARN_TIMESTAMP
,
236 (0, 0, _("%s: time stamp %s is %s s in the future"),
237 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
242 /* Restore stat attributes (owner, group, mode and times) for
243 FILE_NAME, using information given in *ST.
244 If CUR_INFO is nonzero, *CUR_INFO is the
245 file's current status.
246 If not restoring permissions, invert the
247 INVERT_PERMISSIONS bits from the file's current permissions.
248 PERMSTATUS specifies the status of the file's permissions.
249 TYPEFLAG specifies the type of the file. */
251 /* FIXME: About proper restoration of symbolic link attributes, we still do
252 not have it right. Pretesters' reports tell us we need further study and
253 probably more configuration. For now, just use lchown if it exists, and
254 punt for the rest. Sigh! */
257 set_stat (char const *file_name
,
258 struct tar_stat_info
const *st
,
259 struct stat
const *cur_info
,
260 mode_t invert_permissions
, enum permstatus permstatus
,
263 if (typeflag
!= SYMTYPE
)
265 /* We do the utime before the chmod because some versions of utime are
266 broken and trash the modes of the file. */
268 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
270 /* We set the accessed time to `now', which is really the time we
271 started extracting files, unless incremental_option is used, in
272 which case .st_atime is used. */
274 /* FIXME: incremental_option should set ctime too, but how? */
276 struct timespec ts
[2];
277 if (incremental_option
)
283 if (utimens (file_name
, ts
) != 0)
284 utime_error (file_name
);
287 check_time (file_name
, ts
[0]);
288 check_time (file_name
, ts
[1]);
292 /* Some systems allow non-root users to give files away. Once this
293 done, it is not possible anymore to change file permissions.
294 However, setting file permissions now would be incorrect, since
295 they would apply to the wrong user, and there would be a race
296 condition. So, don't use systems that allow non-root users to
300 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
302 /* When lchown exists, it should be used to change the attributes of
303 the symbolic link itself. In this case, a mere chown would change
304 the attributes of the file the symbolic link is pointing to, and
305 should be avoided. */
306 int chown_result
= 1;
308 if (typeflag
== SYMTYPE
)
311 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
316 chown_result
= chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
319 if (chown_result
== 0)
321 /* Changing the owner can flip st_mode bits in some cases, so
322 ignore cur_info if it might be obsolete now. */
324 && cur_info
->st_mode
& S_IXUGO
325 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
328 else if (chown_result
< 0)
329 chown_error_details (file_name
,
330 st
->stat
.st_uid
, st
->stat
.st_gid
);
333 if (typeflag
!= SYMTYPE
)
334 set_mode (file_name
, &st
->stat
, cur_info
,
335 invert_permissions
, permstatus
, typeflag
);
338 /* Remember to restore stat attributes (owner, group, mode and times)
339 for the directory FILE_NAME, using information given in *ST,
340 once we stop extracting files into that directory.
341 If not restoring permissions, remember to invert the
342 INVERT_PERMISSIONS bits from the file's current permissions.
343 PERMSTATUS specifies the status of the file's permissions.
345 NOTICE: this works only if the archive has usual member order, i.e.
346 directory, then the files in that directory. Incremental archive have
347 somewhat reversed order: first go subdirectories, then all other
348 members. To help cope with this case the variable
349 delay_directory_restore_option is set by prepare_to_extract.
351 If an archive was explicitely created so that its member order is
352 reversed, some directory timestamps can be restored incorrectly,
354 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
357 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
358 mode_t invert_permissions
, enum permstatus permstatus
)
360 size_t file_name_len
= strlen (file_name
);
361 struct delayed_set_stat
*data
=
362 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
363 + file_name_len
+ 1);
364 data
->next
= delayed_set_stat_head
;
365 data
->dev
= st
->stat
.st_dev
;
366 data
->ino
= st
->stat
.st_ino
;
367 data
->mode
= st
->stat
.st_mode
;
368 data
->uid
= st
->stat
.st_uid
;
369 data
->gid
= st
->stat
.st_gid
;
370 data
->atime
= st
->atime
;
371 data
->mtime
= st
->mtime
;
372 data
->file_name_len
= file_name_len
;
373 data
->invert_permissions
= invert_permissions
;
374 data
->permstatus
= permstatus
;
375 data
->after_links
= 0;
376 strcpy (data
->file_name
, file_name
);
377 delayed_set_stat_head
= data
;
380 /* Update the delayed_set_stat info for an intermediate directory
381 created within the file name of DIR. The intermediate directory turned
382 out to be the same as this directory, e.g. due to ".." or symbolic
383 links. *DIR_STAT_INFO is the status of the directory. */
385 repair_delayed_set_stat (char const *dir
,
386 struct stat
const *dir_stat_info
)
388 struct delayed_set_stat
*data
;
389 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
392 if (stat (data
->file_name
, &st
) != 0)
394 stat_error (data
->file_name
);
398 if (st
.st_dev
== dir_stat_info
->st_dev
399 && st
.st_ino
== dir_stat_info
->st_ino
)
401 data
->dev
= current_stat_info
.stat
.st_dev
;
402 data
->ino
= current_stat_info
.stat
.st_ino
;
403 data
->mode
= current_stat_info
.stat
.st_mode
;
404 data
->uid
= current_stat_info
.stat
.st_uid
;
405 data
->gid
= current_stat_info
.stat
.st_gid
;
406 data
->atime
= current_stat_info
.atime
;
407 data
->mtime
= current_stat_info
.mtime
;
408 data
->invert_permissions
=
409 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
410 & MODE_RWX
& ~ current_umask
);
411 data
->permstatus
= ARCHIVED_PERMSTATUS
;
416 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
417 quotearg_colon (dir
)));
420 /* After a file/link/directory creation has failed, see if
421 it's because some required directory was not present, and if so,
422 create all required directories. Return non-zero if a directory
425 make_directories (char *file_name
)
427 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
428 char *cursor
; /* points into the file name */
429 int did_something
= 0; /* did we do anything yet? */
431 int invert_permissions
;
434 for (cursor
= cursor0
; *cursor
; cursor
++)
436 if (! ISSLASH (*cursor
))
439 /* Avoid mkdir of empty string, if leading or double '/'. */
441 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
444 /* Avoid mkdir where last part of file name is "." or "..". */
446 if (cursor
[-1] == '.'
447 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
448 || (cursor
[-2] == '.'
449 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
452 *cursor
= '\0'; /* truncate the name there */
453 mode
= MODE_RWX
& ~ newdir_umask
;
454 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
455 status
= mkdir (file_name
, mode
^ invert_permissions
);
459 /* Create a struct delayed_set_stat even if
460 invert_permissions is zero, because
461 repair_delayed_set_stat may need to update the struct. */
462 delay_set_stat (file_name
,
464 invert_permissions
, INTERDIR_PERMSTATUS
);
466 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
476 continue; /* Directory already exists. */
477 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
478 this. Reported by Warren Hyde
479 <Warren.Hyde@motorola.com> */
480 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
481 && access (file_name
, W_OK
) == 0)
484 /* Some other error in the mkdir. We return to the caller. */
488 return did_something
; /* tell them to retry if we made one */
492 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
496 if (stat (file_name
, &st
))
500 stat_warn (file_name
);
501 /* Be on the safe side: if the file does exist assume it is newer */
506 if (!S_ISDIR (st
.st_mode
)
507 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
516 #define RECOVER_SKIP 2
518 /* Attempt repairing what went wrong with the extraction. Delete an
519 already existing file or create missing intermediate directories.
520 Return RECOVER_OK if we somewhat increased our chances at a successful
521 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
522 caller should skip extraction of that member. The value of errno is
523 properly restored on returning RECOVER_NO. */
526 maybe_recoverable (char *file_name
, bool *interdir_made
)
536 /* Remove an old file, if the options allow this. */
538 switch (old_files_option
)
543 case KEEP_NEWER_FILES
:
544 if (file_newer_p (file_name
, ¤t_stat_info
))
551 case DEFAULT_OLD_FILES
:
552 case NO_OVERWRITE_DIR_OLD_FILES
:
553 case OVERWRITE_OLD_FILES
:
555 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
557 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
560 case UNLINK_FIRST_OLD_FILES
:
565 /* Attempt creating missing intermediate directories. */
566 if (! make_directories (file_name
))
571 *interdir_made
= true;
575 /* Just say we can't do anything about it... */
581 /* Fix the statuses of all directories whose statuses need fixing, and
582 which are not ancestors of FILE_NAME. If AFTER_LINKS is
583 nonzero, do this for all such directories; otherwise, stop at the
584 first directory that is marked to be fixed up only after delayed
585 links are applied. */
587 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
589 size_t file_name_len
= strlen (file_name
);
590 bool check_for_renamed_directories
= 0;
592 while (delayed_set_stat_head
)
594 struct delayed_set_stat
*data
= delayed_set_stat_head
;
595 bool skip_this_one
= 0;
597 struct stat
const *cur_info
= 0;
599 check_for_renamed_directories
|= data
->after_links
;
601 if (after_links
< data
->after_links
602 || (data
->file_name_len
< file_name_len
603 && file_name
[data
->file_name_len
]
604 && (ISSLASH (file_name
[data
->file_name_len
])
605 || ISSLASH (file_name
[data
->file_name_len
- 1]))
606 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
609 if (check_for_renamed_directories
)
612 if (stat (data
->file_name
, &st
) != 0)
614 stat_error (data
->file_name
);
617 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
620 _("%s: Directory renamed before its status could be extracted"),
621 quotearg_colon (data
->file_name
)));
628 struct tar_stat_info sb
;
629 sb
.stat
.st_mode
= data
->mode
;
630 sb
.stat
.st_uid
= data
->uid
;
631 sb
.stat
.st_gid
= data
->gid
;
632 sb
.atime
= data
->atime
;
633 sb
.mtime
= data
->mtime
;
634 set_stat (data
->file_name
, &sb
, cur_info
,
635 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
638 delayed_set_stat_head
= data
->next
;
645 /* Extractor functions for various member types */
648 extract_dir (char *file_name
, int typeflag
)
652 bool interdir_made
= false;
654 /* Save 'root device' to avoid purging mount points. */
655 if (one_file_system_option
&& root_device
== 0)
659 if (stat (".", &st
) != 0)
662 root_device
= st
.st_dev
;
665 if (incremental_option
)
666 /* Read the entry and delete files that aren't listed in the archive. */
667 purge_directory (file_name
);
668 else if (typeflag
== GNUTYPE_DUMPDIR
)
671 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
672 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
675 while ((status
= mkdir (file_name
, mode
)))
679 || old_files_option
== DEFAULT_OLD_FILES
680 || old_files_option
== OVERWRITE_OLD_FILES
))
683 if (stat (file_name
, &st
) == 0)
687 repair_delayed_set_stat (file_name
, &st
);
690 if (S_ISDIR (st
.st_mode
))
700 switch (maybe_recoverable (file_name
, &interdir_made
))
711 mkdir_error (file_name
);
720 || old_files_option
== DEFAULT_OLD_FILES
721 || old_files_option
== OVERWRITE_OLD_FILES
)
724 delay_set_stat (file_name
, ¤t_stat_info
,
725 ((mode
^ current_stat_info
.stat
.st_mode
)
726 & MODE_RWX
& ~ current_umask
),
727 ARCHIVED_PERMSTATUS
);
728 else /* For an already existing directory, invert_perms must be 0 */
729 delay_set_stat (file_name
, ¤t_stat_info
,
738 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
741 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
742 | (old_files_option
== OVERWRITE_OLD_FILES
747 /* Contiguous files (on the Masscomp) have to specify the size in
748 the open call that creates them. */
750 if (typeflag
== CONTTYPE
)
751 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
753 fd
= open (file_name
, openflag
, mode
);
755 #else /* not O_CTG */
756 if (typeflag
== CONTTYPE
)
758 static int conttype_diagnosed
;
760 if (!conttype_diagnosed
)
762 conttype_diagnosed
= 1;
763 WARNOPT (WARN_CONTIGUOUS_CAST
,
764 (0, 0, _("Extracting contiguous files as regular files")));
767 fd
= open (file_name
, openflag
, mode
);
769 #endif /* not O_CTG */
775 extract_file (char *file_name
, int typeflag
)
779 union block
*data_block
;
783 bool interdir_made
= false;
784 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
785 mode_t invert_permissions
=
786 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
788 /* FIXME: deal with protection issues. */
790 if (to_stdout_option
)
792 else if (to_command_option
)
794 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
803 int recover
= RECOVER_NO
;
805 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
807 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
813 if (recover
== RECOVER_SKIP
)
815 open_error (file_name
);
820 mv_begin_read (¤t_stat_info
);
821 if (current_stat_info
.is_sparse
)
822 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
824 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
828 /* Locate data, determine max length writeable, write it,
829 block that we have used the data, then check if the write
832 data_block
= find_next_block ();
835 ERROR ((0, 0, _("Unexpected EOF in archive")));
836 break; /* FIXME: What happens, then? */
839 written
= available_space_after (data_block
);
844 count
= full_write (fd
, data_block
->buffer
, written
);
847 set_next_block_after ((union block
*)
848 (data_block
->buffer
+ written
- 1));
849 if (count
!= written
)
851 if (!to_command_option
)
852 write_error_details (file_name
, count
, written
);
853 /* FIXME: shouldn't we restore from backup? */
862 /* If writing to stdout, don't try to do anything to the filename;
863 it doesn't exist, or we don't want to touch it anyway. */
865 if (to_stdout_option
)
870 close_error (file_name
);
872 if (to_command_option
)
875 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
876 (old_files_option
== OVERWRITE_OLD_FILES
?
877 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
883 /* Create a placeholder file with name FILE_NAME, which will be
884 replaced after other extraction is done by a symbolic link if
885 IS_SYMLINK is true, and by a hard link otherwise. Set
886 *INTERDIR_MADE if an intermediate directory is made in the
890 create_placeholder_file (char *file_name
, bool is_symlink
, bool *interdir_made
)
895 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
897 switch (maybe_recoverable (file_name
, interdir_made
))
906 open_error (file_name
);
911 if (fstat (fd
, &st
) != 0)
913 stat_error (file_name
);
916 else if (close (fd
) != 0)
917 close_error (file_name
);
920 struct delayed_set_stat
*h
;
921 struct delayed_link
*p
=
922 xmalloc (offsetof (struct delayed_link
, target
)
923 + strlen (current_stat_info
.link_name
)
925 p
->next
= delayed_link_head
;
926 delayed_link_head
= p
;
929 p
->ctime
= get_stat_ctime (&st
);
930 p
->is_symlink
= is_symlink
;
933 p
->uid
= current_stat_info
.stat
.st_uid
;
934 p
->gid
= current_stat_info
.stat
.st_gid
;
936 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
937 + strlen (file_name
) + 1);
938 p
->sources
->next
= 0;
939 strcpy (p
->sources
->string
, file_name
);
940 strcpy (p
->target
, current_stat_info
.link_name
);
942 h
= delayed_set_stat_head
;
943 if (h
&& ! h
->after_links
944 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
945 && ISSLASH (file_name
[h
->file_name_len
])
946 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
952 if (stat (h
->file_name
, &st
) != 0)
953 stat_error (h
->file_name
);
960 while ((h
= h
->next
) && ! h
->after_links
);
970 extract_link (char *file_name
, int typeflag
)
972 bool interdir_made
= false;
973 char const *link_name
;
976 link_name
= current_stat_info
.link_name
;
978 if (! absolute_names_option
&& contains_dot_dot (link_name
))
979 return create_placeholder_file (file_name
, false, &interdir_made
);
983 struct stat st1
, st2
;
985 int status
= link (link_name
, file_name
);
990 struct delayed_link
*ds
= delayed_link_head
;
991 if (ds
&& lstat (link_name
, &st1
) == 0)
992 for (; ds
; ds
= ds
->next
)
993 if (ds
->dev
== st1
.st_dev
994 && ds
->ino
== st1
.st_ino
995 && timespec_cmp (ds
->ctime
, get_stat_ctime (&st1
)) == 0)
997 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
998 + strlen (file_name
) + 1);
999 strcpy (p
->string
, file_name
);
1000 p
->next
= ds
->sources
;
1006 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
1007 || (lstat (link_name
, &st1
) == 0
1008 && lstat (file_name
, &st2
) == 0
1009 && st1
.st_dev
== st2
.st_dev
1010 && st1
.st_ino
== st2
.st_ino
))
1015 while ((rc
= maybe_recoverable (file_name
, &interdir_made
)) == RECOVER_OK
);
1017 if (rc
== RECOVER_SKIP
)
1019 if (!(incremental_option
&& errno
== EEXIST
))
1021 link_error (link_name
, file_name
);
1028 extract_symlink (char *file_name
, int typeflag
)
1031 bool interdir_made
= false;
1033 if (! absolute_names_option
1034 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1035 || contains_dot_dot (current_stat_info
.link_name
)))
1036 return create_placeholder_file (file_name
, true, &interdir_made
);
1038 while (symlink (current_stat_info
.link_name
, file_name
))
1039 switch (maybe_recoverable (file_name
, &interdir_made
))
1048 symlink_error (current_stat_info
.link_name
, file_name
);
1052 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
1056 static int warned_once
;
1061 WARNOPT (WARN_SYMBOLIC_CAST
,
1063 _("Attempting extraction of symbolic links as hard links")));
1065 return extract_link (file_name
, typeflag
);
1069 #if S_IFCHR || S_IFBLK
1071 extract_node (char *file_name
, int typeflag
)
1073 bool interdir_made
= false;
1074 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1075 mode_t invert_permissions
=
1076 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1078 while (mknod (file_name
, mode
^ invert_permissions
,
1079 current_stat_info
.stat
.st_rdev
))
1080 switch (maybe_recoverable (file_name
, &interdir_made
))
1089 mknod_error (file_name
);
1093 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1094 ARCHIVED_PERMSTATUS
, typeflag
);
1099 #if HAVE_MKFIFO || defined mkfifo
1101 extract_fifo (char *file_name
, int typeflag
)
1104 bool interdir_made
= false;
1105 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1106 mode_t invert_permissions
=
1107 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1109 while ((status
= mkfifo (file_name
, mode
)) != 0)
1110 switch (maybe_recoverable (file_name
, &interdir_made
))
1119 mkfifo_error (file_name
);
1123 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1124 ARCHIVED_PERMSTATUS
, typeflag
);
1130 extract_volhdr (char *file_name
, int typeflag
)
1137 extract_failure (char *file_name
, int typeflag
)
1142 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1146 /* Prepare to extract a file. Find extractor function.
1147 Return zero if extraction should not proceed. */
1150 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1154 if (EXTRACT_OVER_PIPE
)
1157 /* Select the extractor */
1160 case GNUTYPE_SPARSE
:
1161 *fun
= extract_file
;
1168 /* Appears to be a file. But BSD tar uses the convention that a slash
1169 suffix means a directory. */
1170 if (current_stat_info
.had_trailing_slash
)
1174 *fun
= extract_file
;
1180 *fun
= extract_symlink
;
1184 *fun
= extract_link
;
1189 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1190 *fun
= extract_node
;
1196 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1197 *fun
= extract_node
;
1201 #if HAVE_MKFIFO || defined mkfifo
1203 *fun
= extract_fifo
;
1208 case GNUTYPE_DUMPDIR
:
1210 if (current_stat_info
.is_dumpdir
)
1211 delay_directory_restore_option
= true;
1214 case GNUTYPE_VOLHDR
:
1215 *fun
= extract_volhdr
;
1218 case GNUTYPE_MULTIVOL
:
1220 _("%s: Cannot extract -- file is continued from another volume"),
1221 quotearg_colon (current_stat_info
.file_name
)));
1222 *fun
= extract_failure
;
1225 case GNUTYPE_LONGNAME
:
1226 case GNUTYPE_LONGLINK
:
1227 ERROR ((0, 0, _("Unexpected long name header")));
1228 *fun
= extract_failure
;
1232 WARNOPT (WARN_UNKNOWN_CAST
,
1234 _("%s: Unknown file type `%c', extracted as normal file"),
1235 quotearg_colon (file_name
), typeflag
));
1236 *fun
= extract_file
;
1239 /* Determine whether the extraction should proceed */
1243 switch (old_files_option
)
1245 case UNLINK_FIRST_OLD_FILES
:
1246 if (!remove_any_file (file_name
,
1247 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1248 : ORDINARY_REMOVE_OPTION
)
1249 && errno
&& errno
!= ENOENT
)
1251 unlink_error (file_name
);
1256 case KEEP_NEWER_FILES
:
1257 if (file_newer_p (file_name
, ¤t_stat_info
))
1259 WARNOPT (WARN_IGNORE_NEWER
,
1260 (0, 0, _("Current %s is newer or same age"),
1261 quote (file_name
)));
1273 /* Extract a file from the archive. */
1275 extract_archive (void)
1278 tar_extractor_t fun
;
1280 fatal_exit_hook
= extract_finish
;
1282 set_next_block_after (current_header
);
1284 if (!current_stat_info
.file_name
[0]
1285 || (interactive_option
1286 && !confirm ("extract", current_stat_info
.file_name
)))
1292 /* Print the block from current_header and current_stat. */
1294 print_header (¤t_stat_info
, current_header
, -1);
1296 /* Restore stats for all non-ancestor directories, unless
1297 it is an incremental archive.
1298 (see NOTICE in the comment to delay_set_stat above) */
1299 if (!delay_directory_restore_option
)
1300 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1302 /* Take a safety backup of a previously existing file. */
1305 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1308 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1309 quotearg_colon (current_stat_info
.file_name
)));
1314 /* Extract the archive entry according to its type. */
1316 typeflag
= sparse_member_p (¤t_stat_info
) ?
1317 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1319 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1321 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1323 undo_last_backup ();
1330 /* Extract the symbolic links whose final extraction were delayed. */
1332 apply_delayed_links (void)
1334 struct delayed_link
*ds
;
1336 for (ds
= delayed_link_head
; ds
; )
1338 struct string_list
*sources
= ds
->sources
;
1339 char const *valid_source
= 0;
1341 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1343 char const *source
= sources
->string
;
1346 /* Make sure the placeholder file is still there. If not,
1347 don't create a link, as the placeholder was probably
1348 removed by a later extraction. */
1349 if (lstat (source
, &st
) == 0
1350 && st
.st_dev
== ds
->dev
1351 && st
.st_ino
== ds
->ino
1352 && timespec_cmp (get_stat_ctime (&st
), ds
->ctime
) == 0)
1354 /* Unlink the placeholder, then create a hard link if possible,
1355 a symbolic link otherwise. */
1356 if (unlink (source
) != 0)
1357 unlink_error (source
);
1358 else if (valid_source
&& link (valid_source
, source
) == 0)
1360 else if (!ds
->is_symlink
)
1362 if (link (ds
->target
, source
) != 0)
1363 link_error (ds
->target
, source
);
1365 else if (symlink (ds
->target
, source
) != 0)
1366 symlink_error (ds
->target
, source
);
1369 struct tar_stat_info st1
;
1370 st1
.stat
.st_uid
= ds
->uid
;
1371 st1
.stat
.st_gid
= ds
->gid
;
1372 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1373 valid_source
= source
;
1378 for (sources
= ds
->sources
; sources
; )
1380 struct string_list
*next
= sources
->next
;
1386 struct delayed_link
*next
= ds
->next
;
1392 delayed_link_head
= 0;
1395 /* Finish the extraction of an archive. */
1397 extract_finish (void)
1399 /* First, fix the status of ordinary directories that need fixing. */
1400 apply_nonancestor_delayed_set_stat ("", 0);
1402 /* Then, apply delayed links, so that they don't affect delayed
1403 directory status-setting for ordinary directories. */
1404 apply_delayed_links ();
1406 /* Finally, fix the status of directories that are ancestors
1407 of delayed links. */
1408 apply_nonancestor_delayed_set_stat ("", 1);
1412 rename_directory (char *src
, char *dst
)
1414 if (rename (src
, dst
))
1421 if (make_directories (dst
))
1423 if (rename (src
, dst
) == 0)
1430 /* FIXME: Fall back to recursive copying */
1436 ERROR ((0, e
, _("Cannot rename %s to %s"),