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. */
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 last-modified time of the placeholder. */
86 struct timespec mtime;
88 /* True if the link is symbolic. */
91 /* The desired owner and group of the link, if it is a symlink. */
95 /* A list of sources for this link. The sources are all to be
96 hard-linked together. */
97 struct string_list *sources;
99 /* The desired target of the desired link. */
103 static struct delayed_link *delayed_link_head;
107 struct string_list *next;
111 /* Set up to extract files. */
115 we_are_root = geteuid () == 0;
116 same_permissions_option += we_are_root;
117 same_owner_option += we_are_root;
119 /* Option -p clears the kernel umask, so it does not affect proper
120 restoration of file permissions. New intermediate directories will
121 comply with umask at start of program. */
123 newdir_umask = umask (0);
124 if (0 < same_permissions_option)
128 umask (newdir_umask); /* restore the kernel umask */
129 current_umask = newdir_umask;
133 /* If restoring permissions, restore the mode for FILE_NAME from
134 information given in *STAT_INFO (where *CUR_INFO gives
135 the current status if CUR_INFO is nonzero); otherwise invert the
136 INVERT_PERMISSIONS bits from the file's current permissions.
137 PERMSTATUS specifies the status of the file's permissions.
138 TYPEFLAG specifies the type of the file. */
140 set_mode (char const *file_name,
141 struct stat const *stat_info,
142 struct stat const *cur_info,
143 mode_t invert_permissions, enum permstatus permstatus,
148 if (0 < same_permissions_option
149 && permstatus != INTERDIR_PERMSTATUS)
151 mode = stat_info->st_mode;
153 /* If we created the file and it has a mode that we set already
154 with O_CREAT, then its mode is often set correctly already.
155 But if we are changing ownership, the mode's group and and
156 other permission bits were omitted originally, so it's less
157 likely that the mode is OK now. Also, on many hosts, some
158 directories inherit the setgid bits from their parents, so we
159 we must set directories' modes explicitly. */
160 if ((permstatus == ARCHIVED_PERMSTATUS
161 && ! (mode & ~ (0 < same_owner_option ? S_IRWXU : MODE_RWX)))
162 && typeflag != DIRTYPE
163 && typeflag != GNUTYPE_DUMPDIR)
166 else if (! invert_permissions)
170 /* We must inspect a directory's current permissions, since the
171 directory may have inherited its setgid bit from its parent.
173 INVERT_PERMISSIONS happens to be nonzero only for directories
174 that we created, so there's no point optimizing this code for
179 if (stat (file_name, &st) != 0)
181 stat_error (file_name);
186 mode = cur_info->st_mode ^ invert_permissions;
189 if (chmod (file_name, mode) != 0)
190 chmod_error_details (file_name, mode);
193 /* Check time after successfully setting FILE_NAME's time stamp to T. */
195 check_time (char const *file_name, struct timespec t)
198 WARN ((0, 0, _("%s: implausibly old time stamp %s"),
199 file_name, tartime (t, true)));
200 else if (timespec_cmp (volume_start_time, t) < 0)
204 if (timespec_cmp (now, t) < 0)
206 char buf[TIMESPEC_STRSIZE_BOUND];
207 struct timespec diff;
208 diff.tv_sec = t.tv_sec - now.tv_sec;
209 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
210 if (diff.tv_nsec < 0)
212 diff.tv_nsec += BILLION;
215 WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
216 file_name, tartime (t, true), code_timespec (diff, buf)));
221 /* Restore stat attributes (owner, group, mode and times) for
222 FILE_NAME, using information given in *ST.
223 If CUR_INFO is nonzero, *CUR_INFO is the
224 file's current status.
225 If not restoring permissions, invert the
226 INVERT_PERMISSIONS bits from the file's current permissions.
227 PERMSTATUS specifies the status of the file's permissions.
228 TYPEFLAG specifies the type of the file. */
230 /* FIXME: About proper restoration of symbolic link attributes, we still do
231 not have it right. Pretesters' reports tell us we need further study and
232 probably more configuration. For now, just use lchown if it exists, and
233 punt for the rest. Sigh! */
236 set_stat (char const *file_name,
237 struct tar_stat_info const *st,
238 struct stat const *cur_info,
239 mode_t invert_permissions, enum permstatus permstatus,
242 /* We do the utime before the chmod because some versions of utime are
243 broken and trash the modes of the file. */
245 if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
247 /* We set the accessed time to `now', which is really the time we
248 started extracting files, unless incremental_option is used, in
249 which case .st_atime is used. */
251 /* FIXME: incremental_option should set ctime too, but how? */
253 struct timespec ts[2];
254 if (incremental_option)
260 if (utimens (file_name, ts, typeflag == SYMTYPE) != 0)
261 utime_error (file_name);
264 check_time (file_name, ts[0]);
265 check_time (file_name, ts[1]);
269 /* Some systems allow non-root users to give files away. Once this
270 done, it is not possible anymore to change file permissions.
271 However, setting file permissions now would be incorrect, since
272 they would apply to the wrong user, and there would be a race
273 condition. So, don't use systems that allow non-root users to
276 if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
278 /* When lchown exists, it should be used to change the attributes of
279 the symbolic link itself. In this case, a mere chown would change
280 the attributes of the file the symbolic link is pointing to, and
281 should be avoided. */
282 int chown_result = 1;
284 if (typeflag == SYMTYPE)
287 chown_result = lchown (file_name, st->stat.st_uid, st->stat.st_gid);
292 chown_result = chown (file_name, st->stat.st_uid, st->stat.st_gid);
295 if (chown_result == 0)
297 /* Changing the owner can flip st_mode bits in some cases, so
298 ignore cur_info if it might be obsolete now. */
300 && cur_info->st_mode & S_IXUGO
301 && cur_info->st_mode & (S_ISUID | S_ISGID))
304 else if (chown_result < 0)
305 chown_error_details (file_name,
306 st->stat.st_uid, st->stat.st_gid);
309 if (typeflag != SYMTYPE)
310 set_mode (file_name, &st->stat, cur_info,
311 invert_permissions, permstatus, typeflag);
314 /* Remember to restore stat attributes (owner, group, mode and times)
315 for the directory FILE_NAME, using information given in *ST,
316 once we stop extracting files into that directory.
317 If not restoring permissions, remember to invert the
318 INVERT_PERMISSIONS bits from the file's current permissions.
319 PERMSTATUS specifies the status of the file's permissions.
321 NOTICE: this works only if the archive has usual member order, i.e.
322 directory, then the files in that directory. Incremental archive have
323 somewhat reversed order: first go subdirectories, then all other
324 members. To help cope with this case the variable
325 delay_directory_restore_option is set by prepare_to_extract.
327 If an archive was explicitely created so that its member order is
328 reversed, some directory timestamps can be restored incorrectly,
330 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
333 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
334 mode_t invert_permissions, enum permstatus permstatus)
336 size_t file_name_len = strlen (file_name);
337 struct delayed_set_stat *data =
338 xmalloc (offsetof (struct delayed_set_stat, file_name)
339 + file_name_len + 1);
340 data->next = delayed_set_stat_head;
341 data->dev = st->stat.st_dev;
342 data->ino = st->stat.st_ino;
343 data->mode = st->stat.st_mode;
344 data->uid = st->stat.st_uid;
345 data->gid = st->stat.st_gid;
346 data->atime = st->atime;
347 data->mtime = st->mtime;
348 data->file_name_len = file_name_len;
349 data->invert_permissions = invert_permissions;
350 data->permstatus = permstatus;
351 data->after_links = 0;
352 strcpy (data->file_name, file_name);
353 delayed_set_stat_head = data;
356 /* Update the delayed_set_stat info for an intermediate directory
357 created within the file name of DIR. The intermediate directory turned
358 out to be the same as this directory, e.g. due to ".." or symbolic
359 links. *DIR_STAT_INFO is the status of the directory. */
361 repair_delayed_set_stat (char const *dir,
362 struct stat const *dir_stat_info)
364 struct delayed_set_stat *data;
365 for (data = delayed_set_stat_head; data; data = data->next)
368 if (stat (data->file_name, &st) != 0)
370 stat_error (data->file_name);
374 if (st.st_dev == dir_stat_info->st_dev
375 && st.st_ino == dir_stat_info->st_ino)
377 data->dev = current_stat_info.stat.st_dev;
378 data->ino = current_stat_info.stat.st_ino;
379 data->mode = current_stat_info.stat.st_mode;
380 data->uid = current_stat_info.stat.st_uid;
381 data->gid = current_stat_info.stat.st_gid;
382 data->atime = current_stat_info.atime;
383 data->mtime = current_stat_info.mtime;
384 data->invert_permissions =
385 ((current_stat_info.stat.st_mode ^ st.st_mode)
386 & MODE_RWX & ~ current_umask);
387 data->permstatus = ARCHIVED_PERMSTATUS;
392 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
393 quotearg_colon (dir)));
396 /* After a file/link/directory creation has failed, see if
397 it's because some required directory was not present, and if so,
398 create all required directories. Return non-zero if a directory
401 make_directories (char *file_name)
403 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
404 char *cursor; /* points into the file name */
405 int did_something = 0; /* did we do anything yet? */
407 int invert_permissions;
410 for (cursor = cursor0; *cursor; cursor++)
412 if (! ISSLASH (*cursor))
415 /* Avoid mkdir of empty string, if leading or double '/'. */
417 if (cursor == cursor0 || ISSLASH (cursor[-1]))
420 /* Avoid mkdir where last part of file name is "." or "..". */
422 if (cursor[-1] == '.'
423 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
424 || (cursor[-2] == '.'
425 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
428 *cursor = '\0'; /* truncate the name there */
429 mode = MODE_RWX & ~ newdir_umask;
430 invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
431 status = mkdir (file_name, mode ^ invert_permissions);
435 /* Create a struct delayed_set_stat even if
436 invert_permissions is zero, because
437 repair_delayed_set_stat may need to update the struct. */
438 delay_set_stat (file_name,
440 invert_permissions, INTERDIR_PERMSTATUS);
442 print_for_mkdir (file_name, cursor - file_name, mode);
452 continue; /* Directory already exists. */
453 else if ((errno == ENOSYS /* Automounted dirs on Solaris return
454 this. Reported by Warren Hyde
455 <Warren.Hyde@motorola.com> */
456 || ERRNO_IS_EACCES) /* Turbo C mkdir gives a funny errno. */
457 && access (file_name, W_OK) == 0)
460 /* Some other error in the mkdir. We return to the caller. */
464 return did_something; /* tell them to retry if we made one */
468 file_newer_p (const char *file_name, struct tar_stat_info *tar_stat)
472 if (stat (file_name, &st))
474 stat_warn (file_name);
475 /* Be on the safe side: if the file does exist assume it is newer */
476 return errno != ENOENT;
478 if (!S_ISDIR (st.st_mode)
479 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (&st)) <= 0)
486 /* Attempt repairing what went wrong with the extraction. Delete an
487 already existing file or create missing intermediate directories.
488 Return nonzero if we somewhat increased our chances at a successful
489 extraction. errno is properly restored on zero return. */
491 maybe_recoverable (char *file_name, int *interdir_made)
501 /* Remove an old file, if the options allow this. */
503 switch (old_files_option)
508 case KEEP_NEWER_FILES:
509 if (file_newer_p (file_name, ¤t_stat_info))
516 case DEFAULT_OLD_FILES:
517 case NO_OVERWRITE_DIR_OLD_FILES:
518 case OVERWRITE_OLD_FILES:
520 int r = remove_any_file (file_name, ORDINARY_REMOVE_OPTION);
525 case UNLINK_FIRST_OLD_FILES:
530 /* Attempt creating missing intermediate directories. */
531 if (! make_directories (file_name))
540 /* Just say we can't do anything about it... */
546 /* Fix the statuses of all directories whose statuses need fixing, and
547 which are not ancestors of FILE_NAME. If AFTER_LINKS is
548 nonzero, do this for all such directories; otherwise, stop at the
549 first directory that is marked to be fixed up only after delayed
550 links are applied. */
552 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
554 size_t file_name_len = strlen (file_name);
555 bool check_for_renamed_directories = 0;
557 while (delayed_set_stat_head)
559 struct delayed_set_stat *data = delayed_set_stat_head;
560 bool skip_this_one = 0;
562 struct stat const *cur_info = 0;
564 check_for_renamed_directories |= data->after_links;
566 if (after_links < data->after_links
567 || (data->file_name_len < file_name_len
568 && file_name[data->file_name_len]
569 && (ISSLASH (file_name[data->file_name_len])
570 || ISSLASH (file_name[data->file_name_len - 1]))
571 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
574 if (check_for_renamed_directories)
577 if (stat (data->file_name, &st) != 0)
579 stat_error (data->file_name);
582 else if (! (st.st_dev == data->dev && st.st_ino == data->ino))
585 _("%s: Directory renamed before its status could be extracted"),
586 quotearg_colon (data->file_name)));
593 struct tar_stat_info sb;
594 sb.stat.st_mode = data->mode;
595 sb.stat.st_uid = data->uid;
596 sb.stat.st_gid = data->gid;
597 sb.atime = data->atime;
598 sb.mtime = data->mtime;
599 set_stat (data->file_name, &sb, cur_info,
600 data->invert_permissions, data->permstatus, DIRTYPE);
603 delayed_set_stat_head = data->next;
610 /* Extractor functions for various member types */
613 extract_dir (char *file_name, int typeflag)
617 int interdir_made = 0;
619 /* Save 'root device' to avoid purging mount points. */
620 if (one_file_system_option && root_device == 0)
623 char *dir = xgetcwd ();
625 if (deref_stat (true, dir, &st))
628 root_device = st.st_dev;
632 if (incremental_option)
633 /* Read the entry and delete files that aren't listed in the archive. */
634 purge_directory (file_name);
635 else if (typeflag == GNUTYPE_DUMPDIR)
638 mode = current_stat_info.stat.st_mode | (we_are_root ? 0 : MODE_WXUSR);
639 if (0 < same_owner_option || current_stat_info.stat.st_mode & ~ MODE_RWX)
642 while ((status = mkdir (file_name, mode)))
646 || old_files_option == DEFAULT_OLD_FILES
647 || old_files_option == OVERWRITE_OLD_FILES))
650 if (stat (file_name, &st) == 0)
654 repair_delayed_set_stat (file_name, &st);
657 if (S_ISDIR (st.st_mode))
666 if (maybe_recoverable (file_name, &interdir_made))
671 mkdir_error (file_name);
678 || old_files_option == DEFAULT_OLD_FILES
679 || old_files_option == OVERWRITE_OLD_FILES)
682 delay_set_stat (file_name, ¤t_stat_info,
683 ((mode ^ current_stat_info.stat.st_mode)
684 & MODE_RWX & ~ current_umask),
685 ARCHIVED_PERMSTATUS);
686 else /* For an already existing directory, invert_perms must be 0 */
687 delay_set_stat (file_name, ¤t_stat_info,
696 open_output_file (char *file_name, int typeflag, mode_t mode)
699 int openflag = (O_WRONLY | O_BINARY | O_CREAT
700 | (old_files_option == OVERWRITE_OLD_FILES
705 /* Contiguous files (on the Masscomp) have to specify the size in
706 the open call that creates them. */
708 if (typeflag == CONTTYPE)
709 fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
711 fd = open (file_name, openflag, mode);
713 #else /* not O_CTG */
714 if (typeflag == CONTTYPE)
716 static int conttype_diagnosed;
718 if (!conttype_diagnosed)
720 conttype_diagnosed = 1;
721 WARN ((0, 0, _("Extracting contiguous files as regular files")));
724 fd = open (file_name, openflag, mode);
726 #endif /* not O_CTG */
732 extract_file (char *file_name, int typeflag)
736 union block *data_block;
740 int interdir_made = 0;
741 mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
742 mode_t invert_permissions =
743 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
745 /* FIXME: deal with protection issues. */
747 if (to_stdout_option)
749 else if (to_command_option)
751 fd = sys_exec_command (file_name, 'f', ¤t_stat_info);
761 fd = open_output_file (file_name, typeflag, mode ^ invert_permissions);
762 while (fd < 0 && maybe_recoverable (file_name, &interdir_made));
767 open_error (file_name);
772 mv_begin (¤t_stat_info);
773 if (current_stat_info.is_sparse)
774 sparse_extract_file (fd, ¤t_stat_info, &size);
776 for (size = current_stat_info.stat.st_size; size > 0; )
780 /* Locate data, determine max length writeable, write it,
781 block that we have used the data, then check if the write
784 data_block = find_next_block ();
787 ERROR ((0, 0, _("Unexpected EOF in archive")));
788 break; /* FIXME: What happens, then? */
791 written = available_space_after (data_block);
796 count = full_write (fd, data_block->buffer, written);
799 set_next_block_after ((union block *)
800 (data_block->buffer + written - 1));
801 if (count != written)
803 if (!to_command_option)
804 write_error_details (file_name, count, written);
805 /* FIXME: shouldn't we restore from backup? */
814 /* If writing to stdout, don't try to do anything to the filename;
815 it doesn't exist, or we don't want to touch it anyway. */
817 if (to_stdout_option)
822 close_error (file_name);
824 if (to_command_option)
827 set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
828 (old_files_option == OVERWRITE_OLD_FILES ?
829 UNKNOWN_PERMSTATUS : ARCHIVED_PERMSTATUS),
835 /* Create a placeholder file with name FILE_NAME, which will be
836 replaced after other extraction is done by a symbolic link if
837 IS_SYMLINK is true, and by a hard link otherwise. Set
838 *INTERDIR_MADE if an intermediate directory is made in the
842 create_placeholder_file (char *file_name, bool is_symlink, int *interdir_made)
847 while ((fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
848 if (! maybe_recoverable (file_name, interdir_made))
852 open_error (file_name);
853 else if (fstat (fd, &st) != 0)
855 stat_error (file_name);
858 else if (close (fd) != 0)
859 close_error (file_name);
862 struct delayed_set_stat *h;
863 struct delayed_link *p =
864 xmalloc (offsetof (struct delayed_link, target)
865 + strlen (current_stat_info.link_name)
867 p->next = delayed_link_head;
868 delayed_link_head = p;
871 p->mtime = get_stat_mtime (&st);
872 p->is_symlink = is_symlink;
875 p->uid = current_stat_info.stat.st_uid;
876 p->gid = current_stat_info.stat.st_gid;
878 p->sources = xmalloc (offsetof (struct string_list, string)
879 + strlen (file_name) + 1);
880 p->sources->next = 0;
881 strcpy (p->sources->string, file_name);
882 strcpy (p->target, current_stat_info.link_name);
884 h = delayed_set_stat_head;
885 if (h && ! h->after_links
886 && strncmp (file_name, h->file_name, h->file_name_len) == 0
887 && ISSLASH (file_name[h->file_name_len])
888 && (last_component (file_name) == file_name + h->file_name_len + 1))
894 if (stat (h->file_name, &st) != 0)
895 stat_error (h->file_name);
902 while ((h = h->next) && ! h->after_links);
912 extract_link (char *file_name, int typeflag)
914 int interdir_made = 0;
915 char const *link_name;
917 link_name = current_stat_info.link_name;
919 if (! absolute_names_option && contains_dot_dot (link_name))
920 return create_placeholder_file (file_name, false, &interdir_made);
924 struct stat st1, st2;
926 int status = link (link_name, file_name);
931 struct delayed_link *ds = delayed_link_head;
932 if (ds && lstat (link_name, &st1) == 0)
933 for (; ds; ds = ds->next)
934 if (ds->dev == st1.st_dev
935 && ds->ino == st1.st_ino
936 && timespec_cmp (ds->mtime, get_stat_mtime (&st1)) == 0)
938 struct string_list *p = xmalloc (offsetof (struct string_list, string)
939 + strlen (file_name) + 1);
940 strcpy (p->string, file_name);
941 p->next = ds->sources;
947 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
948 || (lstat (link_name, &st1) == 0
949 && lstat (file_name, &st2) == 0
950 && st1.st_dev == st2.st_dev
951 && st1.st_ino == st2.st_ino))
956 while (maybe_recoverable (file_name, &interdir_made));
958 if (!(incremental_option && errno == EEXIST))
960 link_error (link_name, file_name);
967 extract_symlink (char *file_name, int typeflag)
971 int interdir_made = 0;
973 if (! absolute_names_option
974 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
975 || contains_dot_dot (current_stat_info.link_name)))
976 return create_placeholder_file (file_name, true, &interdir_made);
978 while ((status = symlink (current_stat_info.link_name, file_name)))
979 if (!maybe_recoverable (file_name, &interdir_made))
983 set_stat (file_name, ¤t_stat_info, NULL, 0, 0, SYMTYPE);
985 symlink_error (current_stat_info.link_name, file_name);
989 static int warned_once;
994 WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
996 return extract_link (file_name, typeflag);
1000 #if S_IFCHR || S_IFBLK
1002 extract_node (char *file_name, int typeflag)
1005 int interdir_made = 0;
1006 mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
1007 mode_t invert_permissions =
1008 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
1011 status = mknod (file_name, mode ^ invert_permissions,
1012 current_stat_info.stat.st_rdev);
1013 while (status && maybe_recoverable (file_name, &interdir_made));
1016 mknod_error (file_name);
1018 set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
1019 ARCHIVED_PERMSTATUS, typeflag);
1024 #if HAVE_MKFIFO || defined mkfifo
1026 extract_fifo (char *file_name, int typeflag)
1029 int interdir_made = 0;
1030 mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
1031 mode_t invert_permissions =
1032 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
1034 while ((status = mkfifo (file_name, mode)) != 0)
1035 if (!maybe_recoverable (file_name, &interdir_made))
1039 set_stat (file_name, ¤t_stat_info, NULL, invert_permissions,
1040 ARCHIVED_PERMSTATUS, typeflag);
1042 mkfifo_error (file_name);
1048 extract_volhdr (char *file_name, int typeflag)
1051 fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
1057 extract_failure (char *file_name, int typeflag)
1062 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1066 /* Prepare to extract a file. Find extractor function.
1067 Return zero if extraction should not proceed. */
1070 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1074 if (EXTRACT_OVER_PIPE)
1077 /* Select the extractor */
1080 case GNUTYPE_SPARSE:
1081 *fun = extract_file;
1088 /* Appears to be a file. But BSD tar uses the convention that a slash
1089 suffix means a directory. */
1090 if (current_stat_info.had_trailing_slash)
1094 *fun = extract_file;
1100 *fun = extract_symlink;
1104 *fun = extract_link;
1109 current_stat_info.stat.st_mode |= S_IFCHR;
1110 *fun = extract_node;
1116 current_stat_info.stat.st_mode |= S_IFBLK;
1117 *fun = extract_node;
1121 #if HAVE_MKFIFO || defined mkfifo
1123 *fun = extract_fifo;
1128 case GNUTYPE_DUMPDIR:
1130 if (current_stat_info.is_dumpdir)
1131 delay_directory_restore_option = true;
1134 case GNUTYPE_VOLHDR:
1135 *fun = extract_volhdr;
1138 case GNUTYPE_MULTIVOL:
1140 _("%s: Cannot extract -- file is continued from another volume"),
1141 quotearg_colon (current_stat_info.file_name)));
1142 *fun = extract_failure;
1145 case GNUTYPE_LONGNAME:
1146 case GNUTYPE_LONGLINK:
1147 ERROR ((0, 0, _("Unexpected long name header")));
1148 *fun = extract_failure;
1153 _("%s: Unknown file type `%c', extracted as normal file"),
1154 quotearg_colon (file_name), typeflag));
1155 *fun = extract_file;
1158 /* Determine whether the extraction should proceed */
1162 switch (old_files_option)
1164 case UNLINK_FIRST_OLD_FILES:
1165 if (!remove_any_file (file_name,
1166 recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1167 : ORDINARY_REMOVE_OPTION)
1168 && errno && errno != ENOENT)
1170 unlink_error (file_name);
1175 case KEEP_NEWER_FILES:
1176 if (file_newer_p (file_name, ¤t_stat_info))
1178 WARN ((0, 0, _("Current %s is newer or same age"),
1179 quote (file_name)));
1191 /* Extract a file from the archive. */
1193 extract_archive (void)
1196 tar_extractor_t fun;
1198 set_next_block_after (current_header);
1199 decode_header (current_header, ¤t_stat_info, ¤t_format, 1);
1200 if (!current_stat_info.file_name[0]
1201 || (interactive_option
1202 && !confirm ("extract", current_stat_info.file_name)))
1208 /* Print the block from current_header and current_stat. */
1210 print_header (¤t_stat_info, -1);
1212 /* Restore stats for all non-ancestor directories, unless
1213 it is an incremental archive.
1214 (see NOTICE in the comment to delay_set_stat above) */
1215 if (!delay_directory_restore_option)
1216 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1218 /* Take a safety backup of a previously existing file. */
1221 if (!maybe_backup_file (current_stat_info.file_name, 0))
1224 ERROR ((0, e, _("%s: Was unable to backup this file"),
1225 quotearg_colon (current_stat_info.file_name)));
1230 /* Extract the archive entry according to its type. */
1232 typeflag = sparse_member_p (¤t_stat_info) ?
1233 GNUTYPE_SPARSE : current_header->header.typeflag;
1235 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1237 if (fun && (*fun) (current_stat_info.file_name, typeflag)
1239 undo_last_backup ();
1246 /* Extract the symbolic links whose final extraction were delayed. */
1248 apply_delayed_links (void)
1250 struct delayed_link *ds;
1252 for (ds = delayed_link_head; ds; )
1254 struct string_list *sources = ds->sources;
1255 char const *valid_source = 0;
1257 for (sources = ds->sources; sources; sources = sources->next)
1259 char const *source = sources->string;
1262 /* Make sure the placeholder file is still there. If not,
1263 don't create a link, as the placeholder was probably
1264 removed by a later extraction. */
1265 if (lstat (source, &st) == 0
1266 && st.st_dev == ds->dev
1267 && st.st_ino == ds->ino
1268 && timespec_cmp (get_stat_mtime (&st), ds->mtime) == 0)
1270 /* Unlink the placeholder, then create a hard link if possible,
1271 a symbolic link otherwise. */
1272 if (unlink (source) != 0)
1273 unlink_error (source);
1274 else if (valid_source && link (valid_source, source) == 0)
1276 else if (!ds->is_symlink)
1278 if (link (ds->target, source) != 0)
1279 link_error (ds->target, source);
1281 else if (symlink (ds->target, source) != 0)
1282 symlink_error (ds->target, source);
1285 struct tar_stat_info st1;
1286 st1.stat.st_uid = ds->uid;
1287 st1.stat.st_gid = ds->gid;
1288 set_stat (source, &st1, NULL, 0, 0, SYMTYPE);
1289 valid_source = source;
1294 for (sources = ds->sources; sources; )
1296 struct string_list *next = sources->next;
1302 struct delayed_link *next = ds->next;
1308 delayed_link_head = 0;
1311 /* Finish the extraction of an archive. */
1313 extract_finish (void)
1315 /* First, fix the status of ordinary directories that need fixing. */
1316 apply_nonancestor_delayed_set_stat ("", 0);
1318 /* Then, apply delayed links, so that they don't affect delayed
1319 directory status-setting for ordinary directories. */
1320 apply_delayed_links ();
1322 /* Finally, fix the status of directories that are ancestors
1323 of delayed links. */
1324 apply_nonancestor_delayed_set_stat ("", 1);
1328 rename_directory (char *src, char *dst)
1330 if (rename (src, dst))
1337 if (make_directories (dst))
1339 if (rename (src, dst) == 0)
1346 /* FIXME: Fall back to recursive copying */
1352 ERROR ((0, e, _("Cannot rename %s to %s"),
1364 error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
1371 error (0, 0, "%s", _("memory exhausted"));