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 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. */
31 static bool we_are_root
; /* true if our effective uid == 0 */
32 static mode_t newdir_umask
; /* umask when creating new directories */
33 static mode_t current_umask
; /* current umask (which is set to 0 if -p) */
35 /* Status of the permissions of a file that we are extracting. */
38 /* This file may have existed already; its permissions are unknown. */
41 /* This file was created using the permissions from the archive,
42 except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option. */
45 /* This is an intermediate directory; the archive did not specify
50 /* List of directories whose statuses we need to extract after we've
51 finished extracting their subsidiary files. If you consider each
52 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
53 represents an element where AFTER_LINKS is nonzero and [^D]
54 represents an element where AFTER_LINKS is zero, then the head
55 of the subsequence has the longest name, and each non-head element
56 in the prefix is an ancestor (in the directory hierarchy) of the
59 struct delayed_set_stat
61 struct delayed_set_stat
*next
;
67 struct timespec atime
;
68 struct timespec mtime
;
70 mode_t invert_permissions
;
71 enum permstatus permstatus
;
76 static struct delayed_set_stat
*delayed_set_stat_head
;
78 /* List of links whose creation we have delayed. */
81 /* The next delayed link in the list. */
82 struct delayed_link
*next
;
84 /* The device, inode number and last-modified time of the placeholder. */
87 struct timespec mtime
;
89 /* True if the link is symbolic. */
92 /* The desired owner and group of the link, if it is a symlink. */
96 /* A list of sources for this link. The sources are all to be
97 hard-linked together. */
98 struct string_list
*sources
;
100 /* The desired target of the desired link. */
104 static struct delayed_link
*delayed_link_head
;
108 struct string_list
*next
;
112 /* Set up to extract files. */
116 we_are_root
= geteuid () == 0;
117 same_permissions_option
+= we_are_root
;
118 same_owner_option
+= we_are_root
;
120 /* Option -p clears the kernel umask, so it does not affect proper
121 restoration of file permissions. New intermediate directories will
122 comply with umask at start of program. */
124 newdir_umask
= umask (0);
125 if (0 < same_permissions_option
)
129 umask (newdir_umask
); /* restore the kernel umask */
130 current_umask
= newdir_umask
;
134 /* If restoring permissions, restore the mode for FILE_NAME from
135 information given in *STAT_INFO (where *CUR_INFO gives
136 the current status if CUR_INFO is nonzero); otherwise invert the
137 INVERT_PERMISSIONS bits from the file's current permissions.
138 PERMSTATUS specifies the status of the file's permissions.
139 TYPEFLAG specifies the type of the file. */
141 set_mode (char const *file_name
,
142 struct stat
const *stat_info
,
143 struct stat
const *cur_info
,
144 mode_t invert_permissions
, enum permstatus permstatus
,
150 if (0 < same_permissions_option
151 && permstatus
!= INTERDIR_PERMSTATUS
)
153 mode
= stat_info
->st_mode
;
155 /* If we created the file and it has a mode that we set already
156 with O_CREAT, then its mode is often set correctly already.
157 But if we are changing ownership, the mode's group and and
158 other permission bits were omitted originally, so it's less
159 likely that the mode is OK now. Also, on many hosts, some
160 directories inherit the setgid bits from their parents, so we
161 we must set directories' modes explicitly. */
162 if ((permstatus
== ARCHIVED_PERMSTATUS
163 && ! (mode
& ~ (0 < same_owner_option
? S_IRWXU
: MODE_RWX
)))
164 && typeflag
!= DIRTYPE
165 && typeflag
!= GNUTYPE_DUMPDIR
)
168 else if (! invert_permissions
)
172 /* We must inspect a directory's current permissions, since the
173 directory may have inherited its setgid bit from its parent.
175 INVERT_PERMISSIONS happens to be nonzero only for directories
176 that we created, so there's no point optimizing this code for
181 if (stat (file_name
, &st
) != 0)
183 stat_error (file_name
);
188 mode
= cur_info
->st_mode
^ invert_permissions
;
191 failed
= chmod (file_name
, mode
) != 0;
192 if (failed
&& errno
== EPERM
)
194 /* On Solaris, chmod may fail if we don't have PRIV_ALL. */
195 if (priv_set_restore_linkdir () == 0)
197 failed
= chmod (file_name
, mode
) != 0;
198 priv_set_remove_linkdir ();
202 chmod_error_details (file_name
, mode
);
205 /* Check time after successfully setting FILE_NAME's time stamp to T. */
207 check_time (char const *file_name
, struct timespec t
)
210 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
211 file_name
, tartime (t
, true)));
212 else if (timespec_cmp (volume_start_time
, t
) < 0)
216 if (timespec_cmp (now
, t
) < 0)
218 char buf
[TIMESPEC_STRSIZE_BOUND
];
219 struct timespec diff
;
220 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
221 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
222 if (diff
.tv_nsec
< 0)
224 diff
.tv_nsec
+= BILLION
;
227 WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
228 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
233 /* Restore stat attributes (owner, group, mode and times) for
234 FILE_NAME, using information given in *ST.
235 If CUR_INFO is nonzero, *CUR_INFO is the
236 file's current status.
237 If not restoring permissions, invert the
238 INVERT_PERMISSIONS bits from the file's current permissions.
239 PERMSTATUS specifies the status of the file's permissions.
240 TYPEFLAG specifies the type of the file. */
242 /* FIXME: About proper restoration of symbolic link attributes, we still do
243 not have it right. Pretesters' reports tell us we need further study and
244 probably more configuration. For now, just use lchown if it exists, and
245 punt for the rest. Sigh! */
248 set_stat (char const *file_name
,
249 struct tar_stat_info
const *st
,
250 struct stat
const *cur_info
,
251 mode_t invert_permissions
, enum permstatus permstatus
,
254 if (typeflag
!= SYMTYPE
)
256 /* We do the utime before the chmod because some versions of utime are
257 broken and trash the modes of the file. */
259 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
261 /* We set the accessed time to `now', which is really the time we
262 started extracting files, unless incremental_option is used, in
263 which case .st_atime is used. */
265 /* FIXME: incremental_option should set ctime too, but how? */
267 struct timespec ts
[2];
268 if (incremental_option
)
274 if (utimens (file_name
, ts
) != 0)
275 utime_error (file_name
);
278 check_time (file_name
, ts
[0]);
279 check_time (file_name
, ts
[1]);
283 /* Some systems allow non-root users to give files away. Once this
284 done, it is not possible anymore to change file permissions.
285 However, setting file permissions now would be incorrect, since
286 they would apply to the wrong user, and there would be a race
287 condition. So, don't use systems that allow non-root users to
291 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
293 /* When lchown exists, it should be used to change the attributes of
294 the symbolic link itself. In this case, a mere chown would change
295 the attributes of the file the symbolic link is pointing to, and
296 should be avoided. */
297 int chown_result
= 1;
299 if (typeflag
== SYMTYPE
)
302 chown_result
= lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
307 chown_result
= chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
);
310 if (chown_result
== 0)
312 /* Changing the owner can flip st_mode bits in some cases, so
313 ignore cur_info if it might be obsolete now. */
315 && cur_info
->st_mode
& S_IXUGO
316 && cur_info
->st_mode
& (S_ISUID
| S_ISGID
))
319 else if (chown_result
< 0)
320 chown_error_details (file_name
,
321 st
->stat
.st_uid
, st
->stat
.st_gid
);
324 if (typeflag
!= SYMTYPE
)
325 set_mode (file_name
, &st
->stat
, cur_info
,
326 invert_permissions
, permstatus
, typeflag
);
329 /* Remember to restore stat attributes (owner, group, mode and times)
330 for the directory FILE_NAME, using information given in *ST,
331 once we stop extracting files into that directory.
332 If not restoring permissions, remember to invert the
333 INVERT_PERMISSIONS bits from the file's current permissions.
334 PERMSTATUS specifies the status of the file's permissions.
336 NOTICE: this works only if the archive has usual member order, i.e.
337 directory, then the files in that directory. Incremental archive have
338 somewhat reversed order: first go subdirectories, then all other
339 members. To help cope with this case the variable
340 delay_directory_restore_option is set by prepare_to_extract.
342 If an archive was explicitely created so that its member order is
343 reversed, some directory timestamps can be restored incorrectly,
345 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
348 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
349 mode_t invert_permissions
, enum permstatus permstatus
)
351 size_t file_name_len
= strlen (file_name
);
352 struct delayed_set_stat
*data
=
353 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
354 + file_name_len
+ 1);
355 data
->next
= delayed_set_stat_head
;
356 data
->dev
= st
->stat
.st_dev
;
357 data
->ino
= st
->stat
.st_ino
;
358 data
->mode
= st
->stat
.st_mode
;
359 data
->uid
= st
->stat
.st_uid
;
360 data
->gid
= st
->stat
.st_gid
;
361 data
->atime
= st
->atime
;
362 data
->mtime
= st
->mtime
;
363 data
->file_name_len
= file_name_len
;
364 data
->invert_permissions
= invert_permissions
;
365 data
->permstatus
= permstatus
;
366 data
->after_links
= 0;
367 strcpy (data
->file_name
, file_name
);
368 delayed_set_stat_head
= data
;
371 /* Update the delayed_set_stat info for an intermediate directory
372 created within the file name of DIR. The intermediate directory turned
373 out to be the same as this directory, e.g. due to ".." or symbolic
374 links. *DIR_STAT_INFO is the status of the directory. */
376 repair_delayed_set_stat (char const *dir
,
377 struct stat
const *dir_stat_info
)
379 struct delayed_set_stat
*data
;
380 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
383 if (stat (data
->file_name
, &st
) != 0)
385 stat_error (data
->file_name
);
389 if (st
.st_dev
== dir_stat_info
->st_dev
390 && st
.st_ino
== dir_stat_info
->st_ino
)
392 data
->dev
= current_stat_info
.stat
.st_dev
;
393 data
->ino
= current_stat_info
.stat
.st_ino
;
394 data
->mode
= current_stat_info
.stat
.st_mode
;
395 data
->uid
= current_stat_info
.stat
.st_uid
;
396 data
->gid
= current_stat_info
.stat
.st_gid
;
397 data
->atime
= current_stat_info
.atime
;
398 data
->mtime
= current_stat_info
.mtime
;
399 data
->invert_permissions
=
400 ((current_stat_info
.stat
.st_mode
^ st
.st_mode
)
401 & MODE_RWX
& ~ current_umask
);
402 data
->permstatus
= ARCHIVED_PERMSTATUS
;
407 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
408 quotearg_colon (dir
)));
411 /* After a file/link/directory creation has failed, see if
412 it's because some required directory was not present, and if so,
413 create all required directories. Return non-zero if a directory
416 make_directories (char *file_name
)
418 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
419 char *cursor
; /* points into the file name */
420 int did_something
= 0; /* did we do anything yet? */
422 int invert_permissions
;
425 for (cursor
= cursor0
; *cursor
; cursor
++)
427 if (! ISSLASH (*cursor
))
430 /* Avoid mkdir of empty string, if leading or double '/'. */
432 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
435 /* Avoid mkdir where last part of file name is "." or "..". */
437 if (cursor
[-1] == '.'
438 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
439 || (cursor
[-2] == '.'
440 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
443 *cursor
= '\0'; /* truncate the name there */
444 mode
= MODE_RWX
& ~ newdir_umask
;
445 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
446 status
= mkdir (file_name
, mode
^ invert_permissions
);
450 /* Create a struct delayed_set_stat even if
451 invert_permissions is zero, because
452 repair_delayed_set_stat may need to update the struct. */
453 delay_set_stat (file_name
,
455 invert_permissions
, INTERDIR_PERMSTATUS
);
457 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
467 continue; /* Directory already exists. */
468 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
469 this. Reported by Warren Hyde
470 <Warren.Hyde@motorola.com> */
471 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
472 && access (file_name
, W_OK
) == 0)
475 /* Some other error in the mkdir. We return to the caller. */
479 return did_something
; /* tell them to retry if we made one */
483 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
487 if (stat (file_name
, &st
))
489 stat_warn (file_name
);
490 /* Be on the safe side: if the file does exist assume it is newer */
491 return errno
!= ENOENT
;
493 if (!S_ISDIR (st
.st_mode
)
494 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
503 #define RECOVER_SKIP 2
505 /* Attempt repairing what went wrong with the extraction. Delete an
506 already existing file or create missing intermediate directories.
507 Return RECOVER_OK if we somewhat increased our chances at a successful
508 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
509 caller should skip extraction of that member. The value of errno is
510 properly restored on returning RECOVER_NO. */
513 maybe_recoverable (char *file_name
, int *interdir_made
)
523 /* Remove an old file, if the options allow this. */
525 switch (old_files_option
)
530 case KEEP_NEWER_FILES
:
531 if (file_newer_p (file_name
, ¤t_stat_info
))
538 case DEFAULT_OLD_FILES
:
539 case NO_OVERWRITE_DIR_OLD_FILES
:
540 case OVERWRITE_OLD_FILES
:
542 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
544 return r
> 0 ? RECOVER_OK
: RECOVER_NO
;
547 case UNLINK_FIRST_OLD_FILES
:
552 /* Attempt creating missing intermediate directories. */
553 if (! make_directories (file_name
))
562 /* Just say we can't do anything about it... */
568 /* Fix the statuses of all directories whose statuses need fixing, and
569 which are not ancestors of FILE_NAME. If AFTER_LINKS is
570 nonzero, do this for all such directories; otherwise, stop at the
571 first directory that is marked to be fixed up only after delayed
572 links are applied. */
574 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
576 size_t file_name_len
= strlen (file_name
);
577 bool check_for_renamed_directories
= 0;
579 while (delayed_set_stat_head
)
581 struct delayed_set_stat
*data
= delayed_set_stat_head
;
582 bool skip_this_one
= 0;
584 struct stat
const *cur_info
= 0;
586 check_for_renamed_directories
|= data
->after_links
;
588 if (after_links
< data
->after_links
589 || (data
->file_name_len
< file_name_len
590 && file_name
[data
->file_name_len
]
591 && (ISSLASH (file_name
[data
->file_name_len
])
592 || ISSLASH (file_name
[data
->file_name_len
- 1]))
593 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
596 if (check_for_renamed_directories
)
599 if (stat (data
->file_name
, &st
) != 0)
601 stat_error (data
->file_name
);
604 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
607 _("%s: Directory renamed before its status could be extracted"),
608 quotearg_colon (data
->file_name
)));
615 struct tar_stat_info sb
;
616 sb
.stat
.st_mode
= data
->mode
;
617 sb
.stat
.st_uid
= data
->uid
;
618 sb
.stat
.st_gid
= data
->gid
;
619 sb
.atime
= data
->atime
;
620 sb
.mtime
= data
->mtime
;
621 set_stat (data
->file_name
, &sb
, cur_info
,
622 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
625 delayed_set_stat_head
= data
->next
;
632 /* Extractor functions for various member types */
635 extract_dir (char *file_name
, int typeflag
)
639 int interdir_made
= 0;
641 /* Save 'root device' to avoid purging mount points. */
642 if (one_file_system_option
&& root_device
== 0)
645 char *dir
= xgetcwd ();
647 if (deref_stat (true, dir
, &st
))
650 root_device
= st
.st_dev
;
654 if (incremental_option
)
655 /* Read the entry and delete files that aren't listed in the archive. */
656 purge_directory (file_name
);
657 else if (typeflag
== GNUTYPE_DUMPDIR
)
660 mode
= current_stat_info
.stat
.st_mode
| (we_are_root
? 0 : MODE_WXUSR
);
661 if (0 < same_owner_option
|| current_stat_info
.stat
.st_mode
& ~ MODE_RWX
)
664 while ((status
= mkdir (file_name
, mode
)))
668 || old_files_option
== DEFAULT_OLD_FILES
669 || old_files_option
== OVERWRITE_OLD_FILES
))
672 if (stat (file_name
, &st
) == 0)
676 repair_delayed_set_stat (file_name
, &st
);
679 if (S_ISDIR (st
.st_mode
))
688 switch (maybe_recoverable (file_name
, &interdir_made
))
699 mkdir_error (file_name
);
708 || old_files_option
== DEFAULT_OLD_FILES
709 || old_files_option
== OVERWRITE_OLD_FILES
)
712 delay_set_stat (file_name
, ¤t_stat_info
,
713 ((mode
^ current_stat_info
.stat
.st_mode
)
714 & MODE_RWX
& ~ current_umask
),
715 ARCHIVED_PERMSTATUS
);
716 else /* For an already existing directory, invert_perms must be 0 */
717 delay_set_stat (file_name
, ¤t_stat_info
,
726 open_output_file (char *file_name
, int typeflag
, mode_t mode
)
729 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
730 | (old_files_option
== OVERWRITE_OLD_FILES
735 /* Contiguous files (on the Masscomp) have to specify the size in
736 the open call that creates them. */
738 if (typeflag
== CONTTYPE
)
739 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
741 fd
= open (file_name
, openflag
, mode
);
743 #else /* not O_CTG */
744 if (typeflag
== CONTTYPE
)
746 static int conttype_diagnosed
;
748 if (!conttype_diagnosed
)
750 conttype_diagnosed
= 1;
751 WARN ((0, 0, _("Extracting contiguous files as regular files")));
754 fd
= open (file_name
, openflag
, mode
);
756 #endif /* not O_CTG */
762 extract_file (char *file_name
, int typeflag
)
766 union block
*data_block
;
770 int interdir_made
= 0;
771 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
772 mode_t invert_permissions
=
773 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
775 /* FIXME: deal with protection issues. */
777 if (to_stdout_option
)
779 else if (to_command_option
)
781 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
790 int recover
= RECOVER_NO
;
792 fd
= open_output_file (file_name
, typeflag
, mode
^ invert_permissions
);
794 && (recover
= maybe_recoverable (file_name
, &interdir_made
))
800 if (recover
== RECOVER_SKIP
)
802 open_error (file_name
);
807 mv_begin (¤t_stat_info
);
808 if (current_stat_info
.is_sparse
)
809 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
811 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
815 /* Locate data, determine max length writeable, write it,
816 block that we have used the data, then check if the write
819 data_block
= find_next_block ();
822 ERROR ((0, 0, _("Unexpected EOF in archive")));
823 break; /* FIXME: What happens, then? */
826 written
= available_space_after (data_block
);
831 count
= full_write (fd
, data_block
->buffer
, written
);
834 set_next_block_after ((union block
*)
835 (data_block
->buffer
+ written
- 1));
836 if (count
!= written
)
838 if (!to_command_option
)
839 write_error_details (file_name
, count
, written
);
840 /* FIXME: shouldn't we restore from backup? */
849 /* If writing to stdout, don't try to do anything to the filename;
850 it doesn't exist, or we don't want to touch it anyway. */
852 if (to_stdout_option
)
857 close_error (file_name
);
859 if (to_command_option
)
862 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
863 (old_files_option
== OVERWRITE_OLD_FILES
?
864 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
870 /* Create a placeholder file with name FILE_NAME, which will be
871 replaced after other extraction is done by a symbolic link if
872 IS_SYMLINK is true, and by a hard link otherwise. Set
873 *INTERDIR_MADE if an intermediate directory is made in the
877 create_placeholder_file (char *file_name
, bool is_symlink
, int *interdir_made
)
882 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
883 if (! maybe_recoverable (file_name
, interdir_made
))
887 open_error (file_name
);
888 else if (fstat (fd
, &st
) != 0)
890 stat_error (file_name
);
893 else if (close (fd
) != 0)
894 close_error (file_name
);
897 struct delayed_set_stat
*h
;
898 struct delayed_link
*p
=
899 xmalloc (offsetof (struct delayed_link
, target
)
900 + strlen (current_stat_info
.link_name
)
902 p
->next
= delayed_link_head
;
903 delayed_link_head
= p
;
906 p
->mtime
= get_stat_mtime (&st
);
907 p
->is_symlink
= is_symlink
;
910 p
->uid
= current_stat_info
.stat
.st_uid
;
911 p
->gid
= current_stat_info
.stat
.st_gid
;
913 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
914 + strlen (file_name
) + 1);
915 p
->sources
->next
= 0;
916 strcpy (p
->sources
->string
, file_name
);
917 strcpy (p
->target
, current_stat_info
.link_name
);
919 h
= delayed_set_stat_head
;
920 if (h
&& ! h
->after_links
921 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
922 && ISSLASH (file_name
[h
->file_name_len
])
923 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
929 if (stat (h
->file_name
, &st
) != 0)
930 stat_error (h
->file_name
);
937 while ((h
= h
->next
) && ! h
->after_links
);
947 extract_link (char *file_name
, int typeflag
)
949 int interdir_made
= 0;
950 char const *link_name
;
952 link_name
= current_stat_info
.link_name
;
954 if (! absolute_names_option
&& contains_dot_dot (link_name
))
955 return create_placeholder_file (file_name
, false, &interdir_made
);
959 struct stat st1
, st2
;
961 int status
= link (link_name
, file_name
);
966 struct delayed_link
*ds
= delayed_link_head
;
967 if (ds
&& lstat (link_name
, &st1
) == 0)
968 for (; ds
; ds
= ds
->next
)
969 if (ds
->dev
== st1
.st_dev
970 && ds
->ino
== st1
.st_ino
971 && timespec_cmp (ds
->mtime
, get_stat_mtime (&st1
)) == 0)
973 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
974 + strlen (file_name
) + 1);
975 strcpy (p
->string
, file_name
);
976 p
->next
= ds
->sources
;
982 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
983 || (lstat (link_name
, &st1
) == 0
984 && lstat (file_name
, &st2
) == 0
985 && st1
.st_dev
== st2
.st_dev
986 && st1
.st_ino
== st2
.st_ino
))
991 while (maybe_recoverable (file_name
, &interdir_made
));
993 if (!(incremental_option
&& errno
== EEXIST
))
995 link_error (link_name
, file_name
);
1002 extract_symlink (char *file_name
, int typeflag
)
1006 int interdir_made
= 0;
1008 if (! absolute_names_option
1009 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
1010 || contains_dot_dot (current_stat_info
.link_name
)))
1011 return create_placeholder_file (file_name
, true, &interdir_made
);
1013 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
1014 if (!maybe_recoverable (file_name
, &interdir_made
))
1018 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
1020 symlink_error (current_stat_info
.link_name
, file_name
);
1024 static int warned_once
;
1029 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
1031 return extract_link (file_name
, typeflag
);
1035 #if S_IFCHR || S_IFBLK
1037 extract_node (char *file_name
, int typeflag
)
1040 int interdir_made
= 0;
1041 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1042 mode_t invert_permissions
=
1043 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1046 status
= mknod (file_name
, mode
^ invert_permissions
,
1047 current_stat_info
.stat
.st_rdev
);
1048 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
1051 mknod_error (file_name
);
1053 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1054 ARCHIVED_PERMSTATUS
, typeflag
);
1059 #if HAVE_MKFIFO || defined mkfifo
1061 extract_fifo (char *file_name
, int typeflag
)
1064 int interdir_made
= 0;
1065 mode_t mode
= current_stat_info
.stat
.st_mode
& ~ current_umask
;
1066 mode_t invert_permissions
=
1067 0 < same_owner_option
? mode
& (S_IRWXG
| S_IRWXO
) : 0;
1069 while ((status
= mkfifo (file_name
, mode
)) != 0)
1070 if (!maybe_recoverable (file_name
, &interdir_made
))
1074 set_stat (file_name
, ¤t_stat_info
, NULL
, invert_permissions
,
1075 ARCHIVED_PERMSTATUS
, typeflag
);
1077 mkfifo_error (file_name
);
1083 extract_volhdr (char *file_name
, int typeflag
)
1086 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1092 extract_failure (char *file_name
, int typeflag
)
1097 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1101 /* Prepare to extract a file. Find extractor function.
1102 Return zero if extraction should not proceed. */
1105 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1109 if (EXTRACT_OVER_PIPE
)
1112 /* Select the extractor */
1115 case GNUTYPE_SPARSE
:
1116 *fun
= extract_file
;
1123 /* Appears to be a file. But BSD tar uses the convention that a slash
1124 suffix means a directory. */
1125 if (current_stat_info
.had_trailing_slash
)
1129 *fun
= extract_file
;
1135 *fun
= extract_symlink
;
1139 *fun
= extract_link
;
1144 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1145 *fun
= extract_node
;
1151 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1152 *fun
= extract_node
;
1156 #if HAVE_MKFIFO || defined mkfifo
1158 *fun
= extract_fifo
;
1163 case GNUTYPE_DUMPDIR
:
1165 if (current_stat_info
.is_dumpdir
)
1166 delay_directory_restore_option
= true;
1169 case GNUTYPE_VOLHDR
:
1170 *fun
= extract_volhdr
;
1173 case GNUTYPE_MULTIVOL
:
1175 _("%s: Cannot extract -- file is continued from another volume"),
1176 quotearg_colon (current_stat_info
.file_name
)));
1177 *fun
= extract_failure
;
1180 case GNUTYPE_LONGNAME
:
1181 case GNUTYPE_LONGLINK
:
1182 ERROR ((0, 0, _("Unexpected long name header")));
1183 *fun
= extract_failure
;
1188 _("%s: Unknown file type `%c', extracted as normal file"),
1189 quotearg_colon (file_name
), typeflag
));
1190 *fun
= extract_file
;
1193 /* Determine whether the extraction should proceed */
1197 switch (old_files_option
)
1199 case UNLINK_FIRST_OLD_FILES
:
1200 if (!remove_any_file (file_name
,
1201 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1202 : ORDINARY_REMOVE_OPTION
)
1203 && errno
&& errno
!= ENOENT
)
1205 unlink_error (file_name
);
1210 case KEEP_NEWER_FILES
:
1211 if (file_newer_p (file_name
, ¤t_stat_info
))
1213 WARN ((0, 0, _("Current %s is newer or same age"),
1214 quote (file_name
)));
1226 /* Extract a file from the archive. */
1228 extract_archive (void)
1231 tar_extractor_t fun
;
1233 /* Try to disable the ability to unlink a directory. */
1234 priv_set_remove_linkdir ();
1236 set_next_block_after (current_header
);
1237 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1238 if (!current_stat_info
.file_name
[0]
1239 || (interactive_option
1240 && !confirm ("extract", current_stat_info
.file_name
)))
1246 /* Print the block from current_header and current_stat. */
1248 print_header (¤t_stat_info
, -1);
1250 /* Restore stats for all non-ancestor directories, unless
1251 it is an incremental archive.
1252 (see NOTICE in the comment to delay_set_stat above) */
1253 if (!delay_directory_restore_option
)
1254 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1256 /* Take a safety backup of a previously existing file. */
1259 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1262 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1263 quotearg_colon (current_stat_info
.file_name
)));
1268 /* Extract the archive entry according to its type. */
1270 typeflag
= sparse_member_p (¤t_stat_info
) ?
1271 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1273 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1275 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1277 undo_last_backup ();
1284 /* Extract the symbolic links whose final extraction were delayed. */
1286 apply_delayed_links (void)
1288 struct delayed_link
*ds
;
1290 for (ds
= delayed_link_head
; ds
; )
1292 struct string_list
*sources
= ds
->sources
;
1293 char const *valid_source
= 0;
1295 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1297 char const *source
= sources
->string
;
1300 /* Make sure the placeholder file is still there. If not,
1301 don't create a link, as the placeholder was probably
1302 removed by a later extraction. */
1303 if (lstat (source
, &st
) == 0
1304 && st
.st_dev
== ds
->dev
1305 && st
.st_ino
== ds
->ino
1306 && timespec_cmp (get_stat_mtime (&st
), ds
->mtime
) == 0)
1308 /* Unlink the placeholder, then create a hard link if possible,
1309 a symbolic link otherwise. */
1310 if (unlink (source
) != 0)
1311 unlink_error (source
);
1312 else if (valid_source
&& link (valid_source
, source
) == 0)
1314 else if (!ds
->is_symlink
)
1316 if (link (ds
->target
, source
) != 0)
1317 link_error (ds
->target
, source
);
1319 else if (symlink (ds
->target
, source
) != 0)
1320 symlink_error (ds
->target
, source
);
1323 struct tar_stat_info st1
;
1324 st1
.stat
.st_uid
= ds
->uid
;
1325 st1
.stat
.st_gid
= ds
->gid
;
1326 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1327 valid_source
= source
;
1332 for (sources
= ds
->sources
; sources
; )
1334 struct string_list
*next
= sources
->next
;
1340 struct delayed_link
*next
= ds
->next
;
1346 delayed_link_head
= 0;
1349 /* Finish the extraction of an archive. */
1351 extract_finish (void)
1353 /* First, fix the status of ordinary directories that need fixing. */
1354 apply_nonancestor_delayed_set_stat ("", 0);
1356 /* Then, apply delayed links, so that they don't affect delayed
1357 directory status-setting for ordinary directories. */
1358 apply_delayed_links ();
1360 /* Finally, fix the status of directories that are ancestors
1361 of delayed links. */
1362 apply_nonancestor_delayed_set_stat ("", 1);
1366 rename_directory (char *src
, char *dst
)
1368 if (rename (src
, dst
))
1375 if (make_directories (dst
))
1377 if (rename (src
, dst
) == 0)
1384 /* FIXME: Fall back to recursive copying */
1390 ERROR ((0, e
, _("Cannot rename %s to %s"),
1402 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1409 error (0, 0, "%s", _("memory exhausted"));