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 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 2, 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. */
43 /* This is an intermediate directory; the archive did not specify
48 /* List of directories whose statuses we need to extract after we've
49 finished extracting their subsidiary files. If you consider each
50 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
51 represents an element where AFTER_LINKS is nonzero and [^D]
52 represents an element where AFTER_LINKS is zero, then the head
53 of the subsequence has the longest name, and each non-head element
54 in the prefix is an ancestor (in the directory hierarchy) of the
57 struct delayed_set_stat
59 struct delayed_set_stat
*next
;
65 struct timespec atime
;
66 struct timespec mtime
;
68 mode_t invert_permissions
;
69 enum permstatus permstatus
;
74 static struct delayed_set_stat
*delayed_set_stat_head
;
76 /* List of links whose creation we have delayed. */
79 /* The next delayed link in the list. */
80 struct delayed_link
*next
;
82 /* The device, inode number and last-modified time of the placeholder. */
85 struct timespec mtime
;
87 /* True if the link is symbolic. */
90 /* The desired owner and group of the link, if it is a symlink. */
94 /* A list of sources for this link. The sources are all to be
95 hard-linked together. */
96 struct string_list
*sources
;
98 /* The desired target of the desired link. */
102 static struct delayed_link
*delayed_link_head
;
106 struct string_list
*next
;
110 /* Set up to extract files. */
114 we_are_root
= geteuid () == 0;
115 same_permissions_option
+= we_are_root
;
116 same_owner_option
+= we_are_root
;
118 /* Option -p clears the kernel umask, so it does not affect proper
119 restoration of file permissions. New intermediate directories will
120 comply with umask at start of program. */
122 newdir_umask
= umask (0);
123 if (0 < same_permissions_option
)
127 umask (newdir_umask
); /* restore the kernel umask */
128 current_umask
= newdir_umask
;
132 /* If restoring permissions, restore the mode for FILE_NAME from
133 information given in *STAT_INFO (where *CUR_INFO gives
134 the current status if CUR_INFO is nonzero); otherwise invert the
135 INVERT_PERMISSIONS bits from the file's current permissions.
136 PERMSTATUS specifies the status of the file's permissions.
137 TYPEFLAG specifies the type of the file. */
139 set_mode (char const *file_name
,
140 struct stat
const *stat_info
,
141 struct stat
const *cur_info
,
142 mode_t invert_permissions
, enum permstatus permstatus
,
147 if (0 < same_permissions_option
148 && permstatus
!= INTERDIR_PERMSTATUS
)
150 mode
= stat_info
->st_mode
;
152 /* If we created the file and it has a usual mode, then its mode
153 is normally set correctly already. But on many hosts, some
154 directories inherit the setgid bits from their parents, so we
155 we must set directories' modes explicitly. */
156 if (permstatus
== ARCHIVED_PERMSTATUS
157 && ! (mode
& ~ MODE_RWX
)
158 && typeflag
!= DIRTYPE
159 && typeflag
!= GNUTYPE_DUMPDIR
)
162 else if (! invert_permissions
)
166 /* We must inspect a directory's current permissions, since the
167 directory may have inherited its setgid bit from its parent.
169 INVERT_PERMISSIONS happens to be nonzero only for directories
170 that we created, so there's no point optimizing this code for
175 if (stat (file_name
, &st
) != 0)
177 stat_error (file_name
);
182 mode
= cur_info
->st_mode
^ invert_permissions
;
185 if (chmod (file_name
, mode
) != 0)
186 chmod_error_details (file_name
, mode
);
189 /* Check time after successfully setting FILE_NAME's time stamp to T. */
191 check_time (char const *file_name
, struct timespec t
)
194 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
195 file_name
, tartime (t
, true)));
196 else if (timespec_cmp (volume_start_time
, t
) < 0)
200 if (timespec_cmp (now
, t
) < 0)
202 char buf
[TIMESPEC_STRSIZE_BOUND
];
203 struct timespec diff
;
204 diff
.tv_sec
= t
.tv_sec
- now
.tv_sec
;
205 diff
.tv_nsec
= t
.tv_nsec
- now
.tv_nsec
;
206 if (diff
.tv_nsec
< 0)
208 diff
.tv_nsec
+= BILLION
;
211 WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
212 file_name
, tartime (t
, true), code_timespec (diff
, buf
)));
217 /* Restore stat attributes (owner, group, mode and times) for
218 FILE_NAME, using information given in *ST.
219 If CUR_INFO is nonzero, *CUR_INFO is the
220 file's currernt status.
221 If not restoring permissions, invert the
222 INVERT_PERMISSIONS bits from the file's current permissions.
223 PERMSTATUS specifies the status of the file's permissions.
224 TYPEFLAG specifies the type of the file. */
226 /* FIXME: About proper restoration of symbolic link attributes, we still do
227 not have it right. Pretesters' reports tell us we need further study and
228 probably more configuration. For now, just use lchown if it exists, and
229 punt for the rest. Sigh! */
232 set_stat (char const *file_name
,
233 struct tar_stat_info
const *st
,
234 struct stat
const *cur_info
,
235 mode_t invert_permissions
, enum permstatus permstatus
,
238 if (typeflag
!= SYMTYPE
)
240 /* We do the utime before the chmod because some versions of utime are
241 broken and trash the modes of the file. */
243 if (! touch_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
245 /* We set the accessed time to `now', which is really the time we
246 started extracting files, unless incremental_option is used, in
247 which case .st_atime is used. */
249 /* FIXME: incremental_option should set ctime too, but how? */
251 struct timespec ts
[2];
252 if (incremental_option
)
258 if (utimens (file_name
, ts
) != 0)
259 utime_error (file_name
);
262 check_time (file_name
, ts
[0]);
263 check_time (file_name
, ts
[1]);
267 /* Some systems allow non-root users to give files away. Once this
268 done, it is not possible anymore to change file permissions, so we
269 have to set permissions prior to possibly giving files away. */
271 set_mode (file_name
, &st
->stat
, cur_info
,
272 invert_permissions
, permstatus
, typeflag
);
275 if (0 < same_owner_option
&& permstatus
!= INTERDIR_PERMSTATUS
)
277 /* When lchown exists, it should be used to change the attributes of
278 the symbolic link itself. In this case, a mere chown would change
279 the attributes of the file the symbolic link is pointing to, and
280 should be avoided. */
282 if (typeflag
== SYMTYPE
)
285 if (lchown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
) < 0)
286 chown_error_details (file_name
,
287 st
->stat
.st_uid
, st
->stat
.st_gid
);
292 if (chown (file_name
, st
->stat
.st_uid
, st
->stat
.st_gid
) < 0)
293 chown_error_details (file_name
,
294 st
->stat
.st_uid
, st
->stat
.st_gid
);
296 /* On a few systems, and in particular, those allowing to give files
297 away, changing the owner or group destroys the suid or sgid bits.
298 So let's attempt setting these bits once more. */
299 if (st
->stat
.st_mode
& (S_ISUID
| S_ISGID
| S_ISVTX
))
300 set_mode (file_name
, &st
->stat
, 0,
301 invert_permissions
, permstatus
, typeflag
);
306 /* Remember to restore stat attributes (owner, group, mode and times)
307 for the directory FILE_NAME, using information given in *ST,
308 once we stop extracting files into that directory.
309 If not restoring permissions, remember to invert the
310 INVERT_PERMISSIONS bits from the file's current permissions.
311 PERMSTATUS specifies the status of the file's permissions.
313 NOTICE: this works only if the archive has usual member order, i.e.
314 directory, then the files in that directory. Incremental archive have
315 somewhat reversed order: first go subdirectories, then all other
316 members. To help cope with this case the variable
317 delay_directory_restore_option is set by prepare_to_extract.
319 If an archive was explicitely created so that its member order is
320 reversed, some directory timestamps can be restored incorrectly,
322 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
325 delay_set_stat (char const *file_name
, struct tar_stat_info
const *st
,
326 mode_t invert_permissions
, enum permstatus permstatus
)
328 size_t file_name_len
= strlen (file_name
);
329 struct delayed_set_stat
*data
=
330 xmalloc (offsetof (struct delayed_set_stat
, file_name
)
331 + file_name_len
+ 1);
332 data
->next
= delayed_set_stat_head
;
333 data
->dev
= st
->stat
.st_dev
;
334 data
->ino
= st
->stat
.st_ino
;
335 data
->mode
= st
->stat
.st_mode
;
336 data
->uid
= st
->stat
.st_uid
;
337 data
->gid
= st
->stat
.st_gid
;
338 data
->atime
= st
->atime
;
339 data
->mtime
= st
->mtime
;
340 data
->file_name_len
= file_name_len
;
341 data
->invert_permissions
= invert_permissions
;
342 data
->permstatus
= permstatus
;
343 data
->after_links
= 0;
344 strcpy (data
->file_name
, file_name
);
345 delayed_set_stat_head
= data
;
348 /* Update the delayed_set_stat info for an intermediate directory
349 created within the file name of DIR. The intermediate directory turned
350 out to be the same as this directory, e.g. due to ".." or symbolic
351 links. *DIR_STAT_INFO is the status of the directory. */
353 repair_delayed_set_stat (char const *dir
,
354 struct stat
const *dir_stat_info
)
356 struct delayed_set_stat
*data
;
357 for (data
= delayed_set_stat_head
; data
; data
= data
->next
)
360 if (stat (data
->file_name
, &st
) != 0)
362 stat_error (data
->file_name
);
366 if (st
.st_dev
== dir_stat_info
->st_dev
367 && st
.st_ino
== dir_stat_info
->st_ino
)
369 data
->dev
= current_stat_info
.stat
.st_dev
;
370 data
->ino
= current_stat_info
.stat
.st_ino
;
371 data
->mode
= current_stat_info
.stat
.st_mode
;
372 data
->uid
= current_stat_info
.stat
.st_uid
;
373 data
->gid
= current_stat_info
.stat
.st_gid
;
374 data
->atime
= current_stat_info
.atime
;
375 data
->mtime
= current_stat_info
.mtime
;
376 data
->invert_permissions
=
377 (MODE_RWX
& (current_stat_info
.stat
.st_mode
^ st
.st_mode
));
378 data
->permstatus
= ARCHIVED_PERMSTATUS
;
383 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
384 quotearg_colon (dir
)));
387 /* After a file/link/directory creation has failed, see if
388 it's because some required directory was not present, and if so,
389 create all required directories. Return non-zero if a directory
392 make_directories (char *file_name
)
394 char *cursor0
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
395 char *cursor
; /* points into the file name */
396 int did_something
= 0; /* did we do anything yet? */
398 int invert_permissions
;
401 for (cursor
= cursor0
; *cursor
; cursor
++)
403 if (! ISSLASH (*cursor
))
406 /* Avoid mkdir of empty string, if leading or double '/'. */
408 if (cursor
== cursor0
|| ISSLASH (cursor
[-1]))
411 /* Avoid mkdir where last part of file name is "." or "..". */
413 if (cursor
[-1] == '.'
414 && (cursor
== cursor0
+ 1 || ISSLASH (cursor
[-2])
415 || (cursor
[-2] == '.'
416 && (cursor
== cursor0
+ 2 || ISSLASH (cursor
[-3])))))
419 *cursor
= '\0'; /* truncate the name there */
420 mode
= MODE_RWX
& ~ newdir_umask
;
421 invert_permissions
= we_are_root
? 0 : MODE_WXUSR
& ~ mode
;
422 status
= mkdir (file_name
, mode
^ invert_permissions
);
426 /* Create a struct delayed_set_stat even if
427 invert_permissions is zero, because
428 repair_delayed_set_stat may need to update the struct. */
429 delay_set_stat (file_name
,
431 invert_permissions
, INTERDIR_PERMSTATUS
);
433 print_for_mkdir (file_name
, cursor
- file_name
, mode
);
443 continue; /* Directory already exists. */
444 else if ((errno
== ENOSYS
/* Automounted dirs on Solaris return
445 this. Reported by Warren Hyde
446 <Warren.Hyde@motorola.com> */
447 || ERRNO_IS_EACCES
) /* Turbo C mkdir gives a funny errno. */
448 && access (file_name
, W_OK
) == 0)
451 /* Some other error in the mkdir. We return to the caller. */
455 return did_something
; /* tell them to retry if we made one */
459 file_newer_p (const char *file_name
, struct tar_stat_info
*tar_stat
)
463 if (stat (file_name
, &st
))
465 stat_warn (file_name
);
466 /* Be on the safe side: if the file does exist assume it is newer */
467 return errno
!= ENOENT
;
469 if (!S_ISDIR (st
.st_mode
)
470 && tar_timespec_cmp (tar_stat
->mtime
, get_stat_mtime (&st
)) <= 0)
477 /* Attempt repairing what went wrong with the extraction. Delete an
478 already existing file or create missing intermediate directories.
479 Return nonzero if we somewhat increased our chances at a successful
480 extraction. errno is properly restored on zero return. */
482 maybe_recoverable (char *file_name
, int *interdir_made
)
492 /* Remove an old file, if the options allow this. */
494 switch (old_files_option
)
499 case KEEP_NEWER_FILES
:
500 if (file_newer_p (file_name
, ¤t_stat_info
))
507 case DEFAULT_OLD_FILES
:
508 case NO_OVERWRITE_DIR_OLD_FILES
:
509 case OVERWRITE_OLD_FILES
:
511 int r
= remove_any_file (file_name
, ORDINARY_REMOVE_OPTION
);
516 case UNLINK_FIRST_OLD_FILES
:
521 /* Attempt creating missing intermediate directories. */
522 if (! make_directories (file_name
))
531 /* Just say we can't do anything about it... */
537 /* Fix the statuses of all directories whose statuses need fixing, and
538 which are not ancestors of FILE_NAME. If AFTER_LINKS is
539 nonzero, do this for all such directories; otherwise, stop at the
540 first directory that is marked to be fixed up only after delayed
541 links are applied. */
543 apply_nonancestor_delayed_set_stat (char const *file_name
, bool after_links
)
545 size_t file_name_len
= strlen (file_name
);
546 bool check_for_renamed_directories
= 0;
548 while (delayed_set_stat_head
)
550 struct delayed_set_stat
*data
= delayed_set_stat_head
;
551 bool skip_this_one
= 0;
553 struct stat
const *cur_info
= 0;
555 check_for_renamed_directories
|= data
->after_links
;
557 if (after_links
< data
->after_links
558 || (data
->file_name_len
< file_name_len
559 && file_name
[data
->file_name_len
]
560 && (ISSLASH (file_name
[data
->file_name_len
])
561 || ISSLASH (file_name
[data
->file_name_len
- 1]))
562 && memcmp (file_name
, data
->file_name
, data
->file_name_len
) == 0))
565 if (check_for_renamed_directories
)
568 if (stat (data
->file_name
, &st
) != 0)
570 stat_error (data
->file_name
);
573 else if (! (st
.st_dev
== data
->dev
&& st
.st_ino
== data
->ino
))
576 _("%s: Directory renamed before its status could be extracted"),
577 quotearg_colon (data
->file_name
)));
584 struct tar_stat_info st
;
585 st
.stat
.st_mode
= data
->mode
;
586 st
.stat
.st_uid
= data
->uid
;
587 st
.stat
.st_gid
= data
->gid
;
588 st
.atime
= data
->atime
;
589 st
.mtime
= data
->mtime
;
590 set_stat (data
->file_name
, &st
, cur_info
,
591 data
->invert_permissions
, data
->permstatus
, DIRTYPE
);
594 delayed_set_stat_head
= data
->next
;
601 /* Extractor functions for various member types */
604 extract_dir (char *file_name
, int typeflag
)
608 int interdir_made
= 0;
610 /* Save 'root device' to avoid purging mount points. */
611 if (one_file_system_option
&& root_device
== 0)
614 char *dir
= xgetcwd ();
616 if (deref_stat (true, dir
, &st
))
619 root_device
= st
.st_dev
;
623 if (incremental_option
)
624 /* Read the entry and delete files that aren't listed in the archive. */
625 purge_directory (file_name
);
626 else if (typeflag
== GNUTYPE_DUMPDIR
)
629 mode
= (current_stat_info
.stat
.st_mode
|
630 (we_are_root
? 0 : MODE_WXUSR
)) & MODE_RWX
;
632 while ((status
= mkdir (file_name
, mode
)))
636 || old_files_option
== DEFAULT_OLD_FILES
637 || old_files_option
== OVERWRITE_OLD_FILES
))
640 if (stat (file_name
, &st
) == 0)
644 repair_delayed_set_stat (file_name
, &st
);
647 if (S_ISDIR (st
.st_mode
))
656 if (maybe_recoverable (file_name
, &interdir_made
))
661 mkdir_error (file_name
);
668 || old_files_option
== DEFAULT_OLD_FILES
669 || old_files_option
== OVERWRITE_OLD_FILES
)
672 delay_set_stat (file_name
, ¤t_stat_info
,
673 MODE_RWX
& (mode
^ current_stat_info
.stat
.st_mode
),
674 ARCHIVED_PERMSTATUS
);
675 else /* For an already existing directory, invert_perms must be 0 */
676 delay_set_stat (file_name
, ¤t_stat_info
,
685 open_output_file (char *file_name
, int typeflag
)
688 int openflag
= (O_WRONLY
| O_BINARY
| O_CREAT
689 | (old_files_option
== OVERWRITE_OLD_FILES
692 mode_t mode
= current_stat_info
.stat
.st_mode
& MODE_RWX
& ~ current_umask
;
695 /* Contiguous files (on the Masscomp) have to specify the size in
696 the open call that creates them. */
698 if (typeflag
== CONTTYPE
)
699 fd
= open (file_name
, openflag
| O_CTG
, mode
, current_stat_info
.stat
.st_size
);
701 fd
= open (file_name
, openflag
, mode
);
703 #else /* not O_CTG */
704 if (typeflag
== CONTTYPE
)
706 static int conttype_diagnosed
;
708 if (!conttype_diagnosed
)
710 conttype_diagnosed
= 1;
711 WARN ((0, 0, _("Extracting contiguous files as regular files")));
714 fd
= open (file_name
, openflag
, mode
);
716 #endif /* not O_CTG */
722 extract_file (char *file_name
, int typeflag
)
726 union block
*data_block
;
730 int interdir_made
= 0;
732 /* FIXME: deal with protection issues. */
734 if (to_stdout_option
)
736 else if (to_command_option
)
738 fd
= sys_exec_command (file_name
, 'f', ¤t_stat_info
);
748 fd
= open_output_file (file_name
, typeflag
);
749 while (fd
< 0 && maybe_recoverable (file_name
, &interdir_made
));
753 open_error (file_name
);
758 mv_begin (¤t_stat_info
);
759 if (current_stat_info
.is_sparse
)
760 sparse_extract_file (fd
, ¤t_stat_info
, &size
);
762 for (size
= current_stat_info
.stat
.st_size
; size
> 0; )
766 /* Locate data, determine max length writeable, write it,
767 block that we have used the data, then check if the write
770 data_block
= find_next_block ();
773 ERROR ((0, 0, _("Unexpected EOF in archive")));
774 break; /* FIXME: What happens, then? */
777 written
= available_space_after (data_block
);
782 count
= full_write (fd
, data_block
->buffer
, written
);
785 set_next_block_after ((union block
*)
786 (data_block
->buffer
+ written
- 1));
787 if (count
!= written
)
789 if (!to_command_option
)
790 write_error_details (file_name
, count
, written
);
791 /* FIXME: shouldn't we restore from backup? */
800 /* If writing to stdout, don't try to do anything to the filename;
801 it doesn't exist, or we don't want to touch it anyway. */
803 if (to_stdout_option
)
808 close_error (file_name
);
810 if (to_command_option
)
813 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
814 (old_files_option
== OVERWRITE_OLD_FILES
?
815 UNKNOWN_PERMSTATUS
: ARCHIVED_PERMSTATUS
),
821 /* Create a placeholder file with name FILE_NAME, which will be
822 replaced after other extraction is done by a symbolic link if
823 IS_SYMLINK is true, and by a hard link otherwise. Set
824 *INTERDIR_MADE if an intermediate directory is made in the
828 create_placeholder_file (char *file_name
, bool is_symlink
, int *interdir_made
)
833 while ((fd
= open (file_name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0)) < 0)
834 if (! maybe_recoverable (file_name
, interdir_made
))
838 open_error (file_name
);
839 else if (fstat (fd
, &st
) != 0)
841 stat_error (file_name
);
844 else if (close (fd
) != 0)
845 close_error (file_name
);
848 struct delayed_set_stat
*h
;
849 struct delayed_link
*p
=
850 xmalloc (offsetof (struct delayed_link
, target
)
851 + strlen (current_stat_info
.link_name
)
853 p
->next
= delayed_link_head
;
854 delayed_link_head
= p
;
857 p
->mtime
= get_stat_mtime (&st
);
858 p
->is_symlink
= is_symlink
;
861 p
->uid
= current_stat_info
.stat
.st_uid
;
862 p
->gid
= current_stat_info
.stat
.st_gid
;
864 p
->sources
= xmalloc (offsetof (struct string_list
, string
)
865 + strlen (file_name
) + 1);
866 p
->sources
->next
= 0;
867 strcpy (p
->sources
->string
, file_name
);
868 strcpy (p
->target
, current_stat_info
.link_name
);
870 h
= delayed_set_stat_head
;
871 if (h
&& ! h
->after_links
872 && strncmp (file_name
, h
->file_name
, h
->file_name_len
) == 0
873 && ISSLASH (file_name
[h
->file_name_len
])
874 && (last_component (file_name
) == file_name
+ h
->file_name_len
+ 1))
880 if (stat (h
->file_name
, &st
) != 0)
881 stat_error (h
->file_name
);
888 while ((h
= h
->next
) && ! h
->after_links
);
898 extract_link (char *file_name
, int typeflag
)
900 char const *link_name
= safer_name_suffix (current_stat_info
.link_name
,
901 true, absolute_names_option
);
902 int interdir_made
= 0;
904 if (! absolute_names_option
&& contains_dot_dot (link_name
))
905 return create_placeholder_file (file_name
, false, &interdir_made
);
909 struct stat st1
, st2
;
911 int status
= link (link_name
, file_name
);
916 struct delayed_link
*ds
= delayed_link_head
;
917 if (ds
&& lstat (link_name
, &st1
) == 0)
918 for (; ds
; ds
= ds
->next
)
919 if (ds
->dev
== st1
.st_dev
920 && ds
->ino
== st1
.st_ino
921 && timespec_cmp (ds
->mtime
, get_stat_mtime (&st1
)) == 0)
923 struct string_list
*p
= xmalloc (offsetof (struct string_list
, string
)
924 + strlen (file_name
) + 1);
925 strcpy (p
->string
, file_name
);
926 p
->next
= ds
->sources
;
932 else if ((e
== EEXIST
&& strcmp (link_name
, file_name
) == 0)
933 || (lstat (link_name
, &st1
) == 0
934 && lstat (file_name
, &st2
) == 0
935 && st1
.st_dev
== st2
.st_dev
936 && st1
.st_ino
== st2
.st_ino
))
941 while (maybe_recoverable (file_name
, &interdir_made
));
943 if (!(incremental_option
&& errno
== EEXIST
))
945 link_error (link_name
, file_name
);
952 extract_symlink (char *file_name
, int typeflag
)
956 int interdir_made
= 0;
958 if (! absolute_names_option
959 && (IS_ABSOLUTE_FILE_NAME (current_stat_info
.link_name
)
960 || contains_dot_dot (current_stat_info
.link_name
)))
961 return create_placeholder_file (file_name
, true, &interdir_made
);
963 while ((status
= symlink (current_stat_info
.link_name
, file_name
)))
964 if (!maybe_recoverable (file_name
, &interdir_made
))
968 set_stat (file_name
, ¤t_stat_info
, NULL
, 0, 0, SYMTYPE
);
970 symlink_error (current_stat_info
.link_name
, file_name
);
974 static int warned_once
;
979 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
981 return extract_link (file_name
, typeflag
);
985 #if S_IFCHR || S_IFBLK
987 extract_node (char *file_name
, int typeflag
)
990 int interdir_made
= 0;
993 status
= mknod (file_name
, current_stat_info
.stat
.st_mode
,
994 current_stat_info
.stat
.st_rdev
);
995 while (status
&& maybe_recoverable (file_name
, &interdir_made
));
998 mknod_error (file_name
);
1000 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
1001 ARCHIVED_PERMSTATUS
, typeflag
);
1006 #if HAVE_MKFIFO || defined mkfifo
1008 extract_fifo (char *file_name
, int typeflag
)
1011 int interdir_made
= 0;
1013 while ((status
= mkfifo (file_name
, current_stat_info
.stat
.st_mode
)))
1014 if (!maybe_recoverable (file_name
, &interdir_made
))
1018 set_stat (file_name
, ¤t_stat_info
, NULL
, 0,
1019 ARCHIVED_PERMSTATUS
, typeflag
);
1021 mkfifo_error (file_name
);
1027 extract_volhdr (char *file_name
, int typeflag
)
1030 fprintf (stdlis
, _("Reading %s\n"), quote (current_stat_info
.file_name
));
1036 extract_failure (char *file_name
, int typeflag
)
1041 typedef int (*tar_extractor_t
) (char *file_name
, int typeflag
);
1045 /* Prepare to extract a file. Find extractor function.
1046 Return zero if extraction should not proceed. */
1049 prepare_to_extract (char const *file_name
, int typeflag
, tar_extractor_t
*fun
)
1053 if (EXTRACT_OVER_PIPE
)
1056 /* Select the extractor */
1059 case GNUTYPE_SPARSE
:
1060 *fun
= extract_file
;
1067 /* Appears to be a file. But BSD tar uses the convention that a slash
1068 suffix means a directory. */
1069 if (current_stat_info
.had_trailing_slash
)
1073 *fun
= extract_file
;
1079 *fun
= extract_symlink
;
1083 *fun
= extract_link
;
1088 current_stat_info
.stat
.st_mode
|= S_IFCHR
;
1089 *fun
= extract_node
;
1095 current_stat_info
.stat
.st_mode
|= S_IFBLK
;
1096 *fun
= extract_node
;
1100 #if HAVE_MKFIFO || defined mkfifo
1102 *fun
= extract_fifo
;
1107 case GNUTYPE_DUMPDIR
:
1109 if (current_stat_info
.is_dumpdir
)
1110 delay_directory_restore_option
= true;
1113 case GNUTYPE_VOLHDR
:
1114 *fun
= extract_volhdr
;
1117 case GNUTYPE_MULTIVOL
:
1119 _("%s: Cannot extract -- file is continued from another volume"),
1120 quotearg_colon (current_stat_info
.file_name
)));
1121 *fun
= extract_failure
;
1124 case GNUTYPE_LONGNAME
:
1125 case GNUTYPE_LONGLINK
:
1126 ERROR ((0, 0, _("Unexpected long name header")));
1127 *fun
= extract_failure
;
1132 _("%s: Unknown file type `%c', extracted as normal file"),
1133 quotearg_colon (file_name
), typeflag
));
1134 *fun
= extract_file
;
1137 /* Determine whether the extraction should proceed */
1141 switch (old_files_option
)
1143 case UNLINK_FIRST_OLD_FILES
:
1144 if (!remove_any_file (file_name
,
1145 recursive_unlink_option
? RECURSIVE_REMOVE_OPTION
1146 : ORDINARY_REMOVE_OPTION
)
1147 && errno
&& errno
!= ENOENT
)
1149 unlink_error (file_name
);
1154 case KEEP_NEWER_FILES
:
1155 if (file_newer_p (file_name
, ¤t_stat_info
))
1157 WARN ((0, 0, _("Current %s is newer or same age"),
1158 quote (file_name
)));
1170 /* Extract a file from the archive. */
1172 extract_archive (void)
1175 tar_extractor_t fun
;
1177 set_next_block_after (current_header
);
1178 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 1);
1179 if (!current_stat_info
.file_name
[0]
1180 || (interactive_option
1181 && !confirm ("extract", current_stat_info
.file_name
)))
1187 /* Print the block from current_header and current_stat. */
1189 print_header (¤t_stat_info
, -1);
1191 /* Restore stats for all non-ancestor directories, unless
1192 it is an incremental archive.
1193 (see NOTICE in the comment to delay_set_stat above) */
1194 if (!delay_directory_restore_option
)
1195 apply_nonancestor_delayed_set_stat (current_stat_info
.file_name
, 0);
1197 /* Take a safety backup of a previously existing file. */
1200 if (!maybe_backup_file (current_stat_info
.file_name
, 0))
1203 ERROR ((0, e
, _("%s: Was unable to backup this file"),
1204 quotearg_colon (current_stat_info
.file_name
)));
1209 /* Extract the archive entry according to its type. */
1211 typeflag
= sparse_member_p (¤t_stat_info
) ?
1212 GNUTYPE_SPARSE
: current_header
->header
.typeflag
;
1214 if (prepare_to_extract (current_stat_info
.file_name
, typeflag
, &fun
))
1216 if (fun
&& (*fun
) (current_stat_info
.file_name
, typeflag
)
1218 undo_last_backup ();
1225 /* Extract the symbolic links whose final extraction were delayed. */
1227 apply_delayed_links (void)
1229 struct delayed_link
*ds
;
1231 for (ds
= delayed_link_head
; ds
; )
1233 struct string_list
*sources
= ds
->sources
;
1234 char const *valid_source
= 0;
1236 for (sources
= ds
->sources
; sources
; sources
= sources
->next
)
1238 char const *source
= sources
->string
;
1241 /* Make sure the placeholder file is still there. If not,
1242 don't create a link, as the placeholder was probably
1243 removed by a later extraction. */
1244 if (lstat (source
, &st
) == 0
1245 && st
.st_dev
== ds
->dev
1246 && st
.st_ino
== ds
->ino
1247 && timespec_cmp (get_stat_mtime (&st
), ds
->mtime
) == 0)
1249 /* Unlink the placeholder, then create a hard link if possible,
1250 a symbolic link otherwise. */
1251 if (unlink (source
) != 0)
1252 unlink_error (source
);
1253 else if (valid_source
&& link (valid_source
, source
) == 0)
1255 else if (!ds
->is_symlink
)
1257 if (link (ds
->target
, source
) != 0)
1258 link_error (ds
->target
, source
);
1260 else if (symlink (ds
->target
, source
) != 0)
1261 symlink_error (ds
->target
, source
);
1264 struct tar_stat_info st1
;
1265 st1
.stat
.st_uid
= ds
->uid
;
1266 st1
.stat
.st_gid
= ds
->gid
;
1267 set_stat (source
, &st1
, NULL
, 0, 0, SYMTYPE
);
1268 valid_source
= source
;
1273 for (sources
= ds
->sources
; sources
; )
1275 struct string_list
*next
= sources
->next
;
1281 struct delayed_link
*next
= ds
->next
;
1287 delayed_link_head
= 0;
1290 /* Finish the extraction of an archive. */
1292 extract_finish (void)
1294 /* First, fix the status of ordinary directories that need fixing. */
1295 apply_nonancestor_delayed_set_stat ("", 0);
1297 /* Then, apply delayed links, so that they don't affect delayed
1298 directory status-setting for ordinary directories. */
1299 apply_delayed_links ();
1301 /* Finally, fix the status of directories that are ancestors
1302 of delayed links. */
1303 apply_nonancestor_delayed_set_stat ("", 1);
1307 rename_directory (char *src
, char *dst
)
1309 if (rename (src
, dst
))
1316 if (make_directories (dst
))
1318 if (rename (src
, dst
) == 0)
1325 /* FIXME: Fall back to recursive copying */
1331 ERROR ((0, e
, _("Cannot rename %s to %s"),
1343 error (TAREXIT_FAILURE
, 0, _("Error is not recoverable: exiting now"));
1350 error (0, 0, "%s", _("memory exhausted"));