Add basic suuport for extended attributes.
[tar.git] / src / extract.c
blob2d8e175985e2cb0d9b2719f305628884e81b47fd
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, 2012
5 Free Software Foundation, Inc.
7 Written by John Gilmore, on 1985-11-19.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any later
12 version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <system.h>
24 #include <quotearg.h>
25 #include <errno.h>
26 #include <priv-set.h>
27 #include <root-uid.h>
28 #include <utimens.h>
30 #include "common.h"
32 static bool we_are_root; /* true if our effective uid == 0 */
33 static mode_t newdir_umask; /* umask when creating new directories */
34 static mode_t current_umask; /* current umask (which is set to 0 if -p) */
36 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
38 #if ! HAVE_FCHMOD && ! defined fchmod
39 # define fchmod(fd, mode) (errno = ENOSYS, -1)
40 #endif
41 #if ! HAVE_FCHOWN && ! defined fchown
42 # define fchown(fd, uid, gid) (errno = ENOSYS, -1)
43 #endif
45 /* Return true if an error number ERR means the system call is
46 supported in this case. */
47 static bool
48 implemented (int err)
50 return ! (err == ENOSYS
51 || err == ENOTSUP
52 || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
55 /* List of directories whose statuses we need to extract after we've
56 finished extracting their subsidiary files. If you consider each
57 contiguous subsequence of elements of the form [D]?[^D]*, where [D]
58 represents an element where AFTER_LINKS is nonzero and [^D]
59 represents an element where AFTER_LINKS is zero, then the head
60 of the subsequence has the longest name, and each non-head element
61 in the prefix is an ancestor (in the directory hierarchy) of the
62 preceding element. */
64 struct delayed_set_stat
66 /* Next directory in list. */
67 struct delayed_set_stat *next;
69 /* Metadata for this directory. */
70 dev_t dev;
71 ino_t ino;
72 mode_t mode; /* The desired mode is MODE & ~ current_umask. */
73 uid_t uid;
74 gid_t gid;
75 struct timespec atime;
76 struct timespec mtime;
78 /* An estimate of the directory's current mode, along with a mask
79 specifying which bits of this estimate are known to be correct.
80 If CURRENT_MODE_MASK is zero, CURRENT_MODE's value doesn't
81 matter. */
82 mode_t current_mode;
83 mode_t current_mode_mask;
85 /* This directory is an intermediate directory that was created
86 as an ancestor of some other directory; it was not mentioned
87 in the archive, so do not set its uid, gid, atime, or mtime,
88 and don't alter its mode outside of MODE_RWX. */
89 bool interdir;
91 /* Whether symbolic links should be followed when accessing the
92 directory. */
93 int atflag;
95 /* Do not set the status of this directory until after delayed
96 links are created. */
97 bool after_links;
99 /* Directory that the name is relative to. */
100 int change_dir;
102 /* extended attributes*/
103 size_t xattr_map_size;
104 struct xattr_array *xattr_map;
105 /* Length and contents of name. */
106 size_t file_name_len;
107 char file_name[1];
110 static struct delayed_set_stat *delayed_set_stat_head;
112 /* List of links whose creation we have delayed. */
113 struct delayed_link
115 /* The next delayed link in the list. */
116 struct delayed_link *next;
118 /* The device, inode number and birthtime of the placeholder.
119 birthtime.tv_nsec is negative if the birthtime is not available.
120 Don't use mtime as this would allow for false matches if some
121 other process removes the placeholder. Don't use ctime as
122 this would cause race conditions and other screwups, e.g.,
123 when restoring hard-linked symlinks. */
124 dev_t dev;
125 ino_t ino;
126 struct timespec birthtime;
128 /* True if the link is symbolic. */
129 bool is_symlink;
131 /* The desired metadata, valid only the link is symbolic. */
132 mode_t mode;
133 uid_t uid;
134 gid_t gid;
135 struct timespec atime;
136 struct timespec mtime;
138 /* The directory that the sources and target are relative to. */
139 int change_dir;
141 /* A list of sources for this link. The sources are all to be
142 hard-linked together. */
143 struct string_list *sources;
145 size_t xattr_map_size;
146 struct xattr_array *xattr_map;
148 /* The desired target of the desired link. */
149 char target[1];
152 static struct delayed_link *delayed_link_head;
154 struct string_list
156 struct string_list *next;
157 char string[1];
160 /* Set up to extract files. */
161 void
162 extr_init (void)
164 we_are_root = geteuid () == ROOT_UID;
165 same_permissions_option += we_are_root;
166 same_owner_option += we_are_root;
168 /* Option -p clears the kernel umask, so it does not affect proper
169 restoration of file permissions. New intermediate directories will
170 comply with umask at start of program. */
172 newdir_umask = umask (0);
173 if (0 < same_permissions_option)
174 current_umask = 0;
175 else
177 umask (newdir_umask); /* restore the kernel umask */
178 current_umask = newdir_umask;
182 /* Use fchmod if possible, fchmodat otherwise. */
183 static int
184 fd_chmod (int fd, char const *file, mode_t mode, int atflag)
186 if (0 <= fd)
188 int result = fchmod (fd, mode);
189 if (result == 0 || implemented (errno))
190 return result;
192 return fchmodat (chdir_fd, file, mode, atflag);
195 /* Use fchown if possible, fchownat otherwise. */
196 static int
197 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
199 if (0 <= fd)
201 int result = fchown (fd, uid, gid);
202 if (result == 0 || implemented (errno))
203 return result;
205 return fchownat (chdir_fd, file, uid, gid, atflag);
208 /* Use fstat if possible, fstatat otherwise. */
209 static int
210 fd_stat (int fd, char const *file, struct stat *st, int atflag)
212 return (0 <= fd
213 ? fstat (fd, st)
214 : fstatat (chdir_fd, file, st, atflag));
217 /* Set the mode for FILE_NAME to MODE.
218 MODE_MASK specifies the bits of MODE that we care about;
219 thus if MODE_MASK is zero, do nothing.
220 If FD is nonnegative, it is a file descriptor for the file.
221 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
222 the file's current mode, using the style of struct delayed_set_stat.
223 TYPEFLAG specifies the type of the file.
224 ATFLAG specifies the flag to use when statting the file. */
225 static void
226 set_mode (char const *file_name,
227 mode_t mode, mode_t mode_mask, int fd,
228 mode_t current_mode, mode_t current_mode_mask,
229 char typeflag, int atflag)
231 if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
233 if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
235 struct stat st;
236 if (fd_stat (fd, file_name, &st, atflag) != 0)
238 stat_error (file_name);
239 return;
241 current_mode = st.st_mode;
244 current_mode &= MODE_ALL;
245 mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
247 if (current_mode != mode)
249 int chmod_errno =
250 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
252 /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
253 setuid-root files would otherwise be a backdoor. See
254 http://opensolaris.org/jive/thread.jspa?threadID=95826
255 (2009-09-03). */
256 if (chmod_errno == EPERM && (mode & S_ISUID)
257 && priv_set_restore_linkdir () == 0)
259 chmod_errno =
260 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
261 priv_set_remove_linkdir ();
264 /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
265 returns ENOTSUP even when operating on non-symlinks, try
266 again with the flag disabled if it does not appear to be
267 supported and if the file is not a symlink. This
268 introduces a race, alas. */
269 if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
270 chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
272 if (chmod_errno
273 && (typeflag != SYMTYPE || implemented (chmod_errno)))
275 errno = chmod_errno;
276 chmod_error_details (file_name, mode);
282 /* Check time after successfully setting FILE_NAME's time stamp to T. */
283 static void
284 check_time (char const *file_name, struct timespec t)
286 if (t.tv_sec <= 0)
287 WARNOPT (WARN_TIMESTAMP,
288 (0, 0, _("%s: implausibly old time stamp %s"),
289 file_name, tartime (t, true)));
290 else if (timespec_cmp (volume_start_time, t) < 0)
292 struct timespec now;
293 gettime (&now);
294 if (timespec_cmp (now, t) < 0)
296 char buf[TIMESPEC_STRSIZE_BOUND];
297 struct timespec diff;
298 diff.tv_sec = t.tv_sec - now.tv_sec;
299 diff.tv_nsec = t.tv_nsec - now.tv_nsec;
300 if (diff.tv_nsec < 0)
302 diff.tv_nsec += BILLION;
303 diff.tv_sec--;
305 WARNOPT (WARN_TIMESTAMP,
306 (0, 0, _("%s: time stamp %s is %s s in the future"),
307 file_name, tartime (t, true), code_timespec (diff, buf)));
312 /* Restore stat attributes (owner, group, mode and times) for
313 FILE_NAME, using information given in *ST.
314 If FD is nonnegative, it is a file descriptor for the file.
315 CURRENT_MODE and CURRENT_MODE_MASK specify information known about
316 the file's current mode, using the style of struct delayed_set_stat.
317 TYPEFLAG specifies the type of the file.
318 If INTERDIR, this is an intermediate directory.
319 ATFLAG specifies the flag to use when statting the file. */
321 static void
322 set_stat (char const *file_name,
323 struct tar_stat_info const *st,
324 int fd, mode_t current_mode, mode_t current_mode_mask,
325 char typeflag, bool interdir, int atflag)
327 /* Do the utime before the chmod because some versions of utime are
328 broken and trash the modes of the file. */
330 if (! touch_option && ! interdir)
332 struct timespec ts[2];
333 if (incremental_option)
334 ts[0] = st->atime;
335 else
336 ts[0].tv_nsec = UTIME_OMIT;
337 ts[1] = st->mtime;
339 if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
341 if (incremental_option)
342 check_time (file_name, ts[0]);
343 check_time (file_name, ts[1]);
345 else if (typeflag != SYMTYPE || implemented (errno))
346 utime_error (file_name);
349 if (0 < same_owner_option && ! interdir)
351 /* Some systems allow non-root users to give files away. Once this
352 done, it is not possible anymore to change file permissions.
353 However, setting file permissions now would be incorrect, since
354 they would apply to the wrong user, and there would be a race
355 condition. So, don't use systems that allow non-root users to
356 give files away. */
357 uid_t uid = st->stat.st_uid;
358 gid_t gid = st->stat.st_gid;
360 if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
362 /* Changing the owner can clear st_mode bits in some cases. */
363 if ((current_mode | ~ current_mode_mask) & S_IXUGO)
364 current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
366 else if (typeflag != SYMTYPE || implemented (errno))
367 chown_error_details (file_name, uid, gid);
370 set_mode (file_name,
371 st->stat.st_mode & ~ current_umask,
372 0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
373 fd, current_mode, current_mode_mask, typeflag, atflag);
375 /* these three calls must be done *after* fd_chown() call because fd_chown
376 causes that linux capabilities becomes cleared. */
377 xattrs_xattrs_set (st, file_name, typeflag, 1);
380 /* For each entry H in the leading prefix of entries in HEAD that do
381 not have after_links marked, mark H and fill in its dev and ino
382 members. Assume HEAD && ! HEAD->after_links. */
383 static void
384 mark_after_links (struct delayed_set_stat *head)
386 struct delayed_set_stat *h = head;
390 struct stat st;
391 h->after_links = 1;
393 if (deref_stat (h->file_name, &st) != 0)
394 stat_error (h->file_name);
395 else
397 h->dev = st.st_dev;
398 h->ino = st.st_ino;
401 while ((h = h->next) && ! h->after_links);
404 /* Remember to restore stat attributes (owner, group, mode and times)
405 for the directory FILE_NAME, using information given in *ST,
406 once we stop extracting files into that directory.
408 If ST is null, merely create a placeholder node for an intermediate
409 directory that was created by make_directories.
411 NOTICE: this works only if the archive has usual member order, i.e.
412 directory, then the files in that directory. Incremental archive have
413 somewhat reversed order: first go subdirectories, then all other
414 members. To help cope with this case the variable
415 delay_directory_restore_option is set by prepare_to_extract.
417 If an archive was explicitely created so that its member order is
418 reversed, some directory timestamps can be restored incorrectly,
419 e.g.:
420 tar --no-recursion -cf archive dir dir/file1 foo dir/file2
422 static void
423 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
424 mode_t current_mode, mode_t current_mode_mask,
425 mode_t mode, int atflag)
427 size_t file_name_len = strlen (file_name);
428 struct delayed_set_stat *data =
429 xmalloc (offsetof (struct delayed_set_stat, file_name)
430 + file_name_len + 1);
431 data->next = delayed_set_stat_head;
432 data->mode = mode;
433 if (st)
435 data->dev = st->stat.st_dev;
436 data->ino = st->stat.st_ino;
437 data->uid = st->stat.st_uid;
438 data->gid = st->stat.st_gid;
439 data->atime = st->atime;
440 data->mtime = st->mtime;
442 data->file_name_len = file_name_len;
443 data->current_mode = current_mode;
444 data->current_mode_mask = current_mode_mask;
445 data->interdir = ! st;
446 data->atflag = atflag;
447 data->after_links = 0;
448 data->change_dir = chdir_current;
449 if (st)
450 xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
451 else
453 data->xattr_map = NULL;
454 data->xattr_map_size = 0;
456 strcpy (data->file_name, file_name);
457 delayed_set_stat_head = data;
458 if (must_be_dot_or_slash (file_name))
459 mark_after_links (data);
462 /* Update the delayed_set_stat info for an intermediate directory
463 created within the file name of DIR. The intermediate directory turned
464 out to be the same as this directory, e.g. due to ".." or symbolic
465 links. *DIR_STAT_INFO is the status of the directory. */
466 static void
467 repair_delayed_set_stat (char const *dir,
468 struct stat const *dir_stat_info)
470 struct delayed_set_stat *data;
471 for (data = delayed_set_stat_head; data; data = data->next)
473 struct stat st;
474 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
476 stat_error (data->file_name);
477 return;
480 if (st.st_dev == dir_stat_info->st_dev
481 && st.st_ino == dir_stat_info->st_ino)
483 data->dev = current_stat_info.stat.st_dev;
484 data->ino = current_stat_info.stat.st_ino;
485 data->mode = current_stat_info.stat.st_mode;
486 data->uid = current_stat_info.stat.st_uid;
487 data->gid = current_stat_info.stat.st_gid;
488 data->atime = current_stat_info.atime;
489 data->mtime = current_stat_info.mtime;
490 data->current_mode = st.st_mode;
491 data->current_mode_mask = ALL_MODE_BITS;
492 data->interdir = false;
493 return;
497 ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
498 quotearg_colon (dir)));
501 /* After a file/link/directory creation has failed, see if
502 it's because some required directory was not present, and if so,
503 create all required directories. Return zero if all the required
504 directories were created, nonzero (issuing a diagnostic) otherwise.
505 Set *INTERDIR_MADE if at least one directory was created. */
506 static int
507 make_directories (char *file_name, bool *interdir_made)
509 char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
510 char *cursor; /* points into the file name */
512 for (cursor = cursor0; *cursor; cursor++)
514 mode_t mode;
515 mode_t desired_mode;
516 int status;
518 if (! ISSLASH (*cursor))
519 continue;
521 /* Avoid mkdir of empty string, if leading or double '/'. */
523 if (cursor == cursor0 || ISSLASH (cursor[-1]))
524 continue;
526 /* Avoid mkdir where last part of file name is "." or "..". */
528 if (cursor[-1] == '.'
529 && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
530 || (cursor[-2] == '.'
531 && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
532 continue;
534 *cursor = '\0'; /* truncate the name there */
535 desired_mode = MODE_RWX & ~ newdir_umask;
536 mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
537 status = mkdirat (chdir_fd, file_name, mode);
539 if (status == 0)
541 /* Create a struct delayed_set_stat even if
542 mode == desired_mode, because
543 repair_delayed_set_stat may need to update the struct. */
544 delay_set_stat (file_name,
545 0, mode & ~ current_umask, MODE_RWX,
546 desired_mode, AT_SYMLINK_NOFOLLOW);
548 print_for_mkdir (file_name, cursor - file_name, desired_mode);
549 *interdir_made = true;
551 else if (errno == EEXIST)
552 status = 0;
553 else
555 /* Check whether the desired file exists. Even when the
556 file exists, mkdir can fail with some errno value E other
557 than EEXIST, so long as E describes an error condition
558 that also applies. */
559 int e = errno;
560 struct stat st;
561 status = fstatat (chdir_fd, file_name, &st, 0);
562 if (status)
564 errno = e;
565 mkdir_error (file_name);
569 *cursor = '/';
570 if (status)
571 return status;
574 return 0;
577 /* Return true if FILE_NAME (with status *STP, if STP) is not a
578 directory, and has a time stamp newer than (or equal to) that of
579 TAR_STAT. */
580 static bool
581 file_newer_p (const char *file_name, struct stat const *stp,
582 struct tar_stat_info *tar_stat)
584 struct stat st;
586 if (!stp)
588 if (deref_stat (file_name, &st) != 0)
590 if (errno != ENOENT)
592 stat_warn (file_name);
593 /* Be safer: if the file exists, assume it is newer. */
594 return true;
596 return false;
598 stp = &st;
601 return (! S_ISDIR (stp->st_mode)
602 && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
605 #define RECOVER_NO 0
606 #define RECOVER_OK 1
607 #define RECOVER_SKIP 2
609 /* Attempt repairing what went wrong with the extraction. Delete an
610 already existing file or create missing intermediate directories.
611 Return RECOVER_OK if we somewhat increased our chances at a successful
612 extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
613 caller should skip extraction of that member. The value of errno is
614 properly restored on returning RECOVER_NO.
616 If REGULAR, the caller was trying to extract onto a regular file.
618 Set *INTERDIR_MADE if an intermediate directory is made as part of
619 the recovery process. */
621 static int
622 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
624 int e = errno;
625 struct stat st;
626 struct stat const *stp = 0;
628 if (*interdir_made)
629 return RECOVER_NO;
631 switch (e)
633 case ELOOP:
635 /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
636 but some operating systems do not conform to the standard. */
637 #ifdef EFTYPE
638 /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>. */
639 case EFTYPE:
640 #endif
641 /* FreeBSD 8.1 uses errno == EMLINK. */
642 case EMLINK:
643 /* Tru64 5.1B uses errno == ENOTSUP. */
644 case ENOTSUP:
646 if (! regular
647 || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
648 break;
649 if (strchr (file_name, '/'))
651 if (deref_stat (file_name, &st) != 0)
652 break;
653 stp = &st;
656 /* The caller tried to open a symbolic link with O_NOFOLLOW.
657 Fall through, treating it as an already-existing file. */
659 case EEXIST:
660 /* Remove an old file, if the options allow this. */
662 switch (old_files_option)
664 case SKIP_OLD_FILES:
665 WARNOPT (WARN_EXISTING_FILE,
666 (0, 0, _("%s: skipping existing file"), file_name));
667 return RECOVER_SKIP;
669 case KEEP_OLD_FILES:
670 return RECOVER_NO;
672 case KEEP_NEWER_FILES:
673 if (file_newer_p (file_name, stp, &current_stat_info))
674 break;
675 /* FALL THROUGH */
677 case DEFAULT_OLD_FILES:
678 case NO_OVERWRITE_DIR_OLD_FILES:
679 case OVERWRITE_OLD_FILES:
680 if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
681 return RECOVER_OK;
682 break;
684 case UNLINK_FIRST_OLD_FILES:
685 break;
688 case ENOENT:
689 /* Attempt creating missing intermediate directories. */
690 if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
691 return RECOVER_OK;
692 break;
694 default:
695 /* Just say we can't do anything about it... */
696 break;
699 errno = e;
700 return RECOVER_NO;
703 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
704 given in *ST. Restore before extraction because they may affect file layout
705 (e.g. on Lustre distributed parallel filesystem - setting info about how many
706 servers is this file striped over, stripe size, mirror copies, etc.
707 in advance dramatically improves the following performance of reading and
708 writing a file). If not restoring permissions, invert the INVERT_PERMISSIONS
709 bits from the file's current permissions. TYPEFLAG specifies the type of the
710 file. FILE_CREATED indicates set_xattr has created the file */
711 static int
712 set_xattr (char const *file_name, struct tar_stat_info const *st,
713 mode_t invert_permissions, char typeflag, int *file_created)
715 int status = 0;
717 #ifdef HAVE_XATTRS
718 bool interdir_made = false;
720 if ((xattrs_option > 0) && st->xattr_map_size)
722 mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
725 status = mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0);
726 while (status && maybe_recoverable ((char *)file_name, false,
727 &interdir_made));
729 xattrs_xattrs_set (st, file_name, typeflag, 0);
730 *file_created = 1;
732 #endif
734 return(status);
737 /* Fix the statuses of all directories whose statuses need fixing, and
738 which are not ancestors of FILE_NAME. If AFTER_LINKS is
739 nonzero, do this for all such directories; otherwise, stop at the
740 first directory that is marked to be fixed up only after delayed
741 links are applied. */
742 static void
743 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
745 size_t file_name_len = strlen (file_name);
746 bool check_for_renamed_directories = 0;
748 while (delayed_set_stat_head)
750 struct delayed_set_stat *data = delayed_set_stat_head;
751 bool skip_this_one = 0;
752 struct stat st;
753 mode_t current_mode = data->current_mode;
754 mode_t current_mode_mask = data->current_mode_mask;
756 check_for_renamed_directories |= data->after_links;
758 if (after_links < data->after_links
759 || (data->file_name_len < file_name_len
760 && file_name[data->file_name_len]
761 && (ISSLASH (file_name[data->file_name_len])
762 || ISSLASH (file_name[data->file_name_len - 1]))
763 && memcmp (file_name, data->file_name, data->file_name_len) == 0))
764 break;
766 chdir_do (data->change_dir);
768 if (check_for_renamed_directories)
770 if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
772 stat_error (data->file_name);
773 skip_this_one = 1;
775 else
777 current_mode = st.st_mode;
778 current_mode_mask = ALL_MODE_BITS;
779 if (! (st.st_dev == data->dev && st.st_ino == data->ino))
781 ERROR ((0, 0,
782 _("%s: Directory renamed before its status could be extracted"),
783 quotearg_colon (data->file_name)));
784 skip_this_one = 1;
789 if (! skip_this_one)
791 struct tar_stat_info sb;
792 sb.stat.st_mode = data->mode;
793 sb.stat.st_uid = data->uid;
794 sb.stat.st_gid = data->gid;
795 sb.atime = data->atime;
796 sb.mtime = data->mtime;
797 sb.xattr_map = data->xattr_map;
798 sb.xattr_map_size = data->xattr_map_size;
799 set_stat (data->file_name, &sb,
800 -1, current_mode, current_mode_mask,
801 DIRTYPE, data->interdir, data->atflag);
804 delayed_set_stat_head = data->next;
805 xheader_xattr_free (data->xattr_map, data->xattr_map_size);
806 free (data);
812 /* Extractor functions for various member types */
814 static int
815 extract_dir (char *file_name, int typeflag)
817 int status;
818 mode_t mode;
819 mode_t current_mode = 0;
820 mode_t current_mode_mask = 0;
821 int atflag = 0;
822 bool interdir_made = false;
824 /* Save 'root device' to avoid purging mount points. */
825 if (one_file_system_option && root_device == 0)
827 struct stat st;
829 if (fstatat (chdir_fd, ".", &st, 0) != 0)
830 stat_diag (".");
831 else
832 root_device = st.st_dev;
835 if (incremental_option)
836 /* Read the entry and delete files that aren't listed in the archive. */
837 purge_directory (file_name);
838 else if (typeflag == GNUTYPE_DUMPDIR)
839 skip_member ();
841 /* If ownership or permissions will be restored later, create the
842 directory with restrictive permissions at first, so that in the
843 meantime processes owned by other users do not inadvertently
844 create files under this directory that inherit the wrong owner,
845 group, or permissions from the directory. If not root, though,
846 make the directory writeable and searchable at first, so that
847 files can be created under it. */
848 mode = ((current_stat_info.stat.st_mode
849 & (0 < same_owner_option || 0 < same_permissions_option
850 ? S_IRWXU
851 : MODE_RWX))
852 | (we_are_root ? 0 : MODE_WXUSR));
854 for (;;)
856 status = mkdirat (chdir_fd, file_name, mode);
857 if (status == 0)
859 current_mode = mode & ~ current_umask;
860 current_mode_mask = MODE_RWX;
861 atflag = AT_SYMLINK_NOFOLLOW;
862 break;
865 if (errno == EEXIST
866 && (interdir_made
867 || old_files_option == DEFAULT_OLD_FILES
868 || old_files_option == OVERWRITE_OLD_FILES))
870 struct stat st;
871 if (deref_stat (file_name, &st) == 0)
873 current_mode = st.st_mode;
874 current_mode_mask = ALL_MODE_BITS;
876 if (S_ISDIR (current_mode))
878 if (interdir_made)
880 repair_delayed_set_stat (file_name, &st);
881 return 0;
883 break;
886 errno = EEXIST;
889 switch (maybe_recoverable (file_name, false, &interdir_made))
891 case RECOVER_OK:
892 continue;
894 case RECOVER_SKIP:
895 break;
897 case RECOVER_NO:
898 if (errno != EEXIST)
900 mkdir_error (file_name);
901 return 1;
903 break;
905 break;
908 if (status == 0
909 || old_files_option == DEFAULT_OLD_FILES
910 || old_files_option == OVERWRITE_OLD_FILES)
911 delay_set_stat (file_name, &current_stat_info,
912 current_mode, current_mode_mask,
913 current_stat_info.stat.st_mode, atflag);
914 return status;
919 static int
920 open_output_file (char const *file_name, int typeflag, mode_t mode,
921 int file_created, mode_t *current_mode,
922 mode_t *current_mode_mask)
924 int fd;
925 bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
926 int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
927 | O_CREAT
928 | (overwriting_old_files
929 ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
930 : O_EXCL));
932 /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
933 if (file_created)
934 openflag = openflag & ~O_EXCL;
936 if (typeflag == CONTTYPE)
938 static int conttype_diagnosed;
940 if (!conttype_diagnosed)
942 conttype_diagnosed = 1;
943 WARNOPT (WARN_CONTIGUOUS_CAST,
944 (0, 0, _("Extracting contiguous files as regular files")));
948 /* If O_NOFOLLOW is needed but does not work, check for a symlink
949 separately. There's a race condition, but that cannot be avoided
950 on hosts lacking O_NOFOLLOW. */
951 if (! HAVE_WORKING_O_NOFOLLOW
952 && overwriting_old_files && ! dereference_option)
954 struct stat st;
955 if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
956 && S_ISLNK (st.st_mode))
958 errno = ELOOP;
959 return -1;
963 fd = openat (chdir_fd, file_name, openflag, mode);
964 if (0 <= fd)
966 if (overwriting_old_files)
968 struct stat st;
969 if (fstat (fd, &st) != 0)
971 int e = errno;
972 close (fd);
973 errno = e;
974 return -1;
976 if (! S_ISREG (st.st_mode))
978 close (fd);
979 errno = EEXIST;
980 return -1;
982 *current_mode = st.st_mode;
983 *current_mode_mask = ALL_MODE_BITS;
985 else
987 *current_mode = mode & ~ current_umask;
988 *current_mode_mask = MODE_RWX;
992 return fd;
995 static int
996 extract_file (char *file_name, int typeflag)
998 int fd;
999 off_t size;
1000 union block *data_block;
1001 int status;
1002 size_t count;
1003 size_t written;
1004 bool interdir_made = false;
1005 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1006 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1007 mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
1008 : 0;
1009 mode_t current_mode = 0;
1010 mode_t current_mode_mask = 0;
1012 if (to_stdout_option)
1013 fd = STDOUT_FILENO;
1014 else if (to_command_option)
1016 fd = sys_exec_command (file_name, 'f', &current_stat_info);
1017 if (fd < 0)
1019 skip_member ();
1020 return 0;
1023 else
1025 int file_created = 0;
1026 if (set_xattr (file_name, &current_stat_info, invert_permissions,
1027 typeflag, &file_created))
1029 skip_member ();
1030 open_error (file_name);
1031 return 1;
1034 while ((fd = open_output_file (file_name, typeflag, mode,
1035 file_created, &current_mode,
1036 &current_mode_mask))
1037 < 0)
1039 int recover = maybe_recoverable (file_name, true, &interdir_made);
1040 if (recover != RECOVER_OK)
1042 skip_member ();
1043 if (recover == RECOVER_SKIP)
1044 return 0;
1045 open_error (file_name);
1046 return 1;
1051 mv_begin_read (&current_stat_info);
1052 if (current_stat_info.is_sparse)
1053 sparse_extract_file (fd, &current_stat_info, &size);
1054 else
1055 for (size = current_stat_info.stat.st_size; size > 0; )
1057 mv_size_left (size);
1059 /* Locate data, determine max length writeable, write it,
1060 block that we have used the data, then check if the write
1061 worked. */
1063 data_block = find_next_block ();
1064 if (! data_block)
1066 ERROR ((0, 0, _("Unexpected EOF in archive")));
1067 break; /* FIXME: What happens, then? */
1070 written = available_space_after (data_block);
1072 if (written > size)
1073 written = size;
1074 errno = 0;
1075 count = blocking_write (fd, data_block->buffer, written);
1076 size -= written;
1078 set_next_block_after ((union block *)
1079 (data_block->buffer + written - 1));
1080 if (count != written)
1082 if (!to_command_option)
1083 write_error_details (file_name, count, written);
1084 /* FIXME: shouldn't we restore from backup? */
1085 break;
1089 skip_file (size);
1091 mv_end ();
1093 /* If writing to stdout, don't try to do anything to the filename;
1094 it doesn't exist, or we don't want to touch it anyway. */
1096 if (to_stdout_option)
1097 return 0;
1099 if (! to_command_option)
1100 set_stat (file_name, &current_stat_info, fd,
1101 current_mode, current_mode_mask, typeflag, false,
1102 (old_files_option == OVERWRITE_OLD_FILES
1103 ? 0 : AT_SYMLINK_NOFOLLOW));
1105 status = close (fd);
1106 if (status < 0)
1107 close_error (file_name);
1109 if (to_command_option)
1110 sys_wait_command ();
1112 return status;
1115 /* Create a placeholder file with name FILE_NAME, which will be
1116 replaced after other extraction is done by a symbolic link if
1117 IS_SYMLINK is true, and by a hard link otherwise. Set
1118 *INTERDIR_MADE if an intermediate directory is made in the
1119 process. */
1121 static int
1122 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
1124 int fd;
1125 struct stat st;
1127 while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1129 switch (maybe_recoverable (file_name, false, interdir_made))
1131 case RECOVER_OK:
1132 continue;
1134 case RECOVER_SKIP:
1135 return 0;
1137 case RECOVER_NO:
1138 open_error (file_name);
1139 return -1;
1143 if (fstat (fd, &st) != 0)
1145 stat_error (file_name);
1146 close (fd);
1148 else if (close (fd) != 0)
1149 close_error (file_name);
1150 else
1152 struct delayed_set_stat *h;
1153 struct delayed_link *p =
1154 xmalloc (offsetof (struct delayed_link, target)
1155 + strlen (current_stat_info.link_name)
1156 + 1);
1157 p->next = delayed_link_head;
1158 delayed_link_head = p;
1159 p->dev = st.st_dev;
1160 p->ino = st.st_ino;
1161 p->birthtime = get_stat_birthtime (&st);
1162 p->is_symlink = is_symlink;
1163 if (is_symlink)
1165 p->mode = current_stat_info.stat.st_mode;
1166 p->uid = current_stat_info.stat.st_uid;
1167 p->gid = current_stat_info.stat.st_gid;
1168 p->atime = current_stat_info.atime;
1169 p->mtime = current_stat_info.mtime;
1171 p->change_dir = chdir_current;
1172 p->sources = xmalloc (offsetof (struct string_list, string)
1173 + strlen (file_name) + 1);
1174 p->sources->next = 0;
1175 strcpy (p->sources->string, file_name);
1176 xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
1177 strcpy (p->target, current_stat_info.link_name);
1179 h = delayed_set_stat_head;
1180 if (h && ! h->after_links
1181 && strncmp (file_name, h->file_name, h->file_name_len) == 0
1182 && ISSLASH (file_name[h->file_name_len])
1183 && (last_component (file_name) == file_name + h->file_name_len + 1))
1184 mark_after_links (h);
1186 return 0;
1189 return -1;
1192 static int
1193 extract_link (char *file_name, int typeflag)
1195 bool interdir_made = false;
1196 char const *link_name;
1197 int rc;
1199 link_name = current_stat_info.link_name;
1201 if (! absolute_names_option && contains_dot_dot (link_name))
1202 return create_placeholder_file (file_name, false, &interdir_made);
1206 struct stat st1, st2;
1207 int e;
1208 int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1209 e = errno;
1211 if (status == 0)
1213 struct delayed_link *ds = delayed_link_head;
1214 if (ds
1215 && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1216 for (; ds; ds = ds->next)
1217 if (ds->change_dir == chdir_current
1218 && ds->dev == st1.st_dev
1219 && ds->ino == st1.st_ino
1220 && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
1221 == 0))
1223 struct string_list *p = xmalloc (offsetof (struct string_list, string)
1224 + strlen (file_name) + 1);
1225 strcpy (p->string, file_name);
1226 p->next = ds->sources;
1227 ds->sources = p;
1228 break;
1230 return 0;
1232 else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1233 || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1234 == 0)
1235 && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1236 == 0)
1237 && st1.st_dev == st2.st_dev
1238 && st1.st_ino == st2.st_ino))
1239 return 0;
1241 errno = e;
1243 while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1244 == RECOVER_OK);
1246 if (rc == RECOVER_SKIP)
1247 return 0;
1248 if (!(incremental_option && errno == EEXIST))
1250 link_error (link_name, file_name);
1251 return 1;
1253 return 0;
1256 static int
1257 extract_symlink (char *file_name, int typeflag)
1259 #ifdef HAVE_SYMLINK
1260 bool interdir_made = false;
1262 if (! absolute_names_option
1263 && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1264 || contains_dot_dot (current_stat_info.link_name)))
1265 return create_placeholder_file (file_name, true, &interdir_made);
1267 while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1268 switch (maybe_recoverable (file_name, false, &interdir_made))
1270 case RECOVER_OK:
1271 continue;
1273 case RECOVER_SKIP:
1274 return 0;
1276 case RECOVER_NO:
1277 symlink_error (current_stat_info.link_name, file_name);
1278 return -1;
1281 set_stat (file_name, &current_stat_info, -1, 0, 0,
1282 SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1283 return 0;
1285 #else
1286 static int warned_once;
1288 if (!warned_once)
1290 warned_once = 1;
1291 WARNOPT (WARN_SYMBOLIC_CAST,
1292 (0, 0,
1293 _("Attempting extraction of symbolic links as hard links")));
1295 return extract_link (file_name, typeflag);
1296 #endif
1299 #if S_IFCHR || S_IFBLK
1300 static int
1301 extract_node (char *file_name, int typeflag)
1303 bool interdir_made = false;
1304 mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1305 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1307 while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1308 != 0)
1309 switch (maybe_recoverable (file_name, false, &interdir_made))
1311 case RECOVER_OK:
1312 continue;
1314 case RECOVER_SKIP:
1315 return 0;
1317 case RECOVER_NO:
1318 mknod_error (file_name);
1319 return -1;
1322 set_stat (file_name, &current_stat_info, -1,
1323 mode & ~ current_umask, MODE_RWX,
1324 typeflag, false, AT_SYMLINK_NOFOLLOW);
1325 return 0;
1327 #endif
1329 #if HAVE_MKFIFO || defined mkfifo
1330 static int
1331 extract_fifo (char *file_name, int typeflag)
1333 bool interdir_made = false;
1334 mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1335 & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1337 while (mkfifoat (chdir_fd, file_name, mode) != 0)
1338 switch (maybe_recoverable (file_name, false, &interdir_made))
1340 case RECOVER_OK:
1341 continue;
1343 case RECOVER_SKIP:
1344 return 0;
1346 case RECOVER_NO:
1347 mkfifo_error (file_name);
1348 return -1;
1351 set_stat (file_name, &current_stat_info, -1,
1352 mode & ~ current_umask, MODE_RWX,
1353 typeflag, false, AT_SYMLINK_NOFOLLOW);
1354 return 0;
1356 #endif
1358 static int
1359 extract_volhdr (char *file_name, int typeflag)
1361 skip_member ();
1362 return 0;
1365 static int
1366 extract_failure (char *file_name, int typeflag)
1368 return 1;
1371 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1375 /* Prepare to extract a file. Find extractor function.
1376 Return zero if extraction should not proceed. */
1378 static int
1379 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1381 int rc = 1;
1383 if (EXTRACT_OVER_PIPE)
1384 rc = 0;
1386 /* Select the extractor */
1387 switch (typeflag)
1389 case GNUTYPE_SPARSE:
1390 *fun = extract_file;
1391 rc = 1;
1392 break;
1394 case AREGTYPE:
1395 case REGTYPE:
1396 case CONTTYPE:
1397 /* Appears to be a file. But BSD tar uses the convention that a slash
1398 suffix means a directory. */
1399 if (current_stat_info.had_trailing_slash)
1400 *fun = extract_dir;
1401 else
1403 *fun = extract_file;
1404 rc = 1;
1406 break;
1408 case SYMTYPE:
1409 *fun = extract_symlink;
1410 break;
1412 case LNKTYPE:
1413 *fun = extract_link;
1414 break;
1416 #if S_IFCHR
1417 case CHRTYPE:
1418 current_stat_info.stat.st_mode |= S_IFCHR;
1419 *fun = extract_node;
1420 break;
1421 #endif
1423 #if S_IFBLK
1424 case BLKTYPE:
1425 current_stat_info.stat.st_mode |= S_IFBLK;
1426 *fun = extract_node;
1427 break;
1428 #endif
1430 #if HAVE_MKFIFO || defined mkfifo
1431 case FIFOTYPE:
1432 *fun = extract_fifo;
1433 break;
1434 #endif
1436 case DIRTYPE:
1437 case GNUTYPE_DUMPDIR:
1438 *fun = extract_dir;
1439 if (current_stat_info.is_dumpdir)
1440 delay_directory_restore_option = true;
1441 break;
1443 case GNUTYPE_VOLHDR:
1444 *fun = extract_volhdr;
1445 break;
1447 case GNUTYPE_MULTIVOL:
1448 ERROR ((0, 0,
1449 _("%s: Cannot extract -- file is continued from another volume"),
1450 quotearg_colon (current_stat_info.file_name)));
1451 *fun = extract_failure;
1452 break;
1454 case GNUTYPE_LONGNAME:
1455 case GNUTYPE_LONGLINK:
1456 ERROR ((0, 0, _("Unexpected long name header")));
1457 *fun = extract_failure;
1458 break;
1460 default:
1461 WARNOPT (WARN_UNKNOWN_CAST,
1462 (0, 0,
1463 _("%s: Unknown file type '%c', extracted as normal file"),
1464 quotearg_colon (file_name), typeflag));
1465 *fun = extract_file;
1468 /* Determine whether the extraction should proceed */
1469 if (rc == 0)
1470 return 0;
1472 switch (old_files_option)
1474 case UNLINK_FIRST_OLD_FILES:
1475 if (!remove_any_file (file_name,
1476 recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1477 : ORDINARY_REMOVE_OPTION)
1478 && errno && errno != ENOENT)
1480 unlink_error (file_name);
1481 return 0;
1483 break;
1485 case KEEP_NEWER_FILES:
1486 if (file_newer_p (file_name, 0, &current_stat_info))
1488 WARNOPT (WARN_IGNORE_NEWER,
1489 (0, 0, _("Current %s is newer or same age"),
1490 quote (file_name)));
1491 return 0;
1493 break;
1495 default:
1496 break;
1499 return 1;
1502 /* Extract a file from the archive. */
1503 void
1504 extract_archive (void)
1506 char typeflag;
1507 tar_extractor_t fun;
1509 fatal_exit_hook = extract_finish;
1511 set_next_block_after (current_header);
1513 if (!current_stat_info.file_name[0]
1514 || (interactive_option
1515 && !confirm ("extract", current_stat_info.file_name)))
1517 skip_member ();
1518 return;
1521 /* Print the block from current_header and current_stat. */
1522 if (verbose_option)
1523 print_header (&current_stat_info, current_header, -1);
1525 /* Restore stats for all non-ancestor directories, unless
1526 it is an incremental archive.
1527 (see NOTICE in the comment to delay_set_stat above) */
1528 if (!delay_directory_restore_option)
1530 int dir = chdir_current;
1531 apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1532 chdir_do (dir);
1535 /* Take a safety backup of a previously existing file. */
1537 if (backup_option)
1538 if (!maybe_backup_file (current_stat_info.file_name, 0))
1540 int e = errno;
1541 ERROR ((0, e, _("%s: Was unable to backup this file"),
1542 quotearg_colon (current_stat_info.file_name)));
1543 skip_member ();
1544 return;
1547 /* Extract the archive entry according to its type. */
1548 /* KLUDGE */
1549 typeflag = sparse_member_p (&current_stat_info) ?
1550 GNUTYPE_SPARSE : current_header->header.typeflag;
1552 if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1554 if (fun && (*fun) (current_stat_info.file_name, typeflag)
1555 && backup_option)
1556 undo_last_backup ();
1558 else
1559 skip_member ();
1563 /* Extract the links whose final extraction were delayed. */
1564 static void
1565 apply_delayed_links (void)
1567 struct delayed_link *ds;
1569 for (ds = delayed_link_head; ds; )
1571 struct string_list *sources = ds->sources;
1572 char const *valid_source = 0;
1574 chdir_do (ds->change_dir);
1576 for (sources = ds->sources; sources; sources = sources->next)
1578 char const *source = sources->string;
1579 struct stat st;
1581 /* Make sure the placeholder file is still there. If not,
1582 don't create a link, as the placeholder was probably
1583 removed by a later extraction. */
1584 if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1585 && st.st_dev == ds->dev
1586 && st.st_ino == ds->ino
1587 && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
1589 /* Unlink the placeholder, then create a hard link if possible,
1590 a symbolic link otherwise. */
1591 if (unlinkat (chdir_fd, source, 0) != 0)
1592 unlink_error (source);
1593 else if (valid_source
1594 && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1595 == 0))
1597 else if (!ds->is_symlink)
1599 if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1600 link_error (ds->target, source);
1602 else if (symlinkat (ds->target, chdir_fd, source) != 0)
1603 symlink_error (ds->target, source);
1604 else
1606 struct tar_stat_info st1;
1607 st1.stat.st_mode = ds->mode;
1608 st1.stat.st_uid = ds->uid;
1609 st1.stat.st_gid = ds->gid;
1610 st1.atime = ds->atime;
1611 st1.mtime = ds->mtime;
1612 st1.xattr_map = ds->xattr_map;
1613 st1.xattr_map_size = ds->xattr_map_size;
1614 set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1615 false, AT_SYMLINK_NOFOLLOW);
1616 valid_source = source;
1621 for (sources = ds->sources; sources; )
1623 struct string_list *next = sources->next;
1624 free (sources);
1625 sources = next;
1628 xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
1631 struct delayed_link *next = ds->next;
1632 free (ds);
1633 ds = next;
1637 delayed_link_head = 0;
1640 /* Finish the extraction of an archive. */
1641 void
1642 extract_finish (void)
1644 /* First, fix the status of ordinary directories that need fixing. */
1645 apply_nonancestor_delayed_set_stat ("", 0);
1647 /* Then, apply delayed links, so that they don't affect delayed
1648 directory status-setting for ordinary directories. */
1649 apply_delayed_links ();
1651 /* Finally, fix the status of directories that are ancestors
1652 of delayed links. */
1653 apply_nonancestor_delayed_set_stat ("", 1);
1656 bool
1657 rename_directory (char *src, char *dst)
1659 if (renameat (chdir_fd, src, chdir_fd, dst) != 0)
1661 int e = errno;
1662 bool interdir_made;
1664 switch (e)
1666 case ENOENT:
1667 if (make_directories (dst, &interdir_made) == 0)
1669 if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1670 return true;
1671 e = errno;
1673 break;
1675 case EXDEV:
1676 /* FIXME: Fall back to recursive copying */
1678 default:
1679 break;
1682 ERROR ((0, e, _("Cannot rename %s to %s"),
1683 quote_n (0, src),
1684 quote_n (1, dst)));
1685 return false;
1687 return true;