re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / src / extract.c
1 /* Extract files from a tar archive.
2
3    Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2014,
4    2016 Free Software Foundation, Inc.
5
6    This file is part of GNU tar.
7
8    GNU tar is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    GNU tar is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21    Written by John Gilmore, on 1985-11-19.  */
22
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>
29
30 #include "common.h"
31
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) */
35
36 #define ALL_MODE_BITS ((mode_t) ~ (mode_t) 0)
37
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
44
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)
49 {
50   return ! (err == ENOSYS
51             || err == ENOTSUP
52             || (EOPNOTSUPP != ENOTSUP && err == EOPNOTSUPP));
53 }
54
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.  */
63
64 struct delayed_set_stat
65   {
66     /* Next directory in list.  */
67     struct delayed_set_stat *next;
68
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;
77
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;
84
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;
90
91     /* Whether symbolic links should be followed when accessing the
92        directory.  */
93     int atflag;
94
95     /* Do not set the status of this directory until after delayed
96        links are created.  */
97     bool after_links;
98
99     /* Directory that the name is relative to.  */
100     int change_dir;
101
102     /* extended attributes*/
103     char *cntx_name;
104     char *acls_a_ptr;
105     size_t acls_a_len;
106     char *acls_d_ptr;
107     size_t acls_d_len;
108     size_t xattr_map_size;
109     struct xattr_array *xattr_map;
110     /* Length and contents of name.  */
111     size_t file_name_len;
112     char *file_name;
113   };
114
115 static struct delayed_set_stat *delayed_set_stat_head;
116
117 /* List of links whose creation we have delayed.  */
118 struct delayed_link
119   {
120     /* The next delayed link in the list.  */
121     struct delayed_link *next;
122
123     /* The device, inode number and birthtime of the placeholder.
124        birthtime.tv_nsec is negative if the birthtime is not available.
125        Don't use mtime as this would allow for false matches if some
126        other process removes the placeholder.  Don't use ctime as
127        this would cause race conditions and other screwups, e.g.,
128        when restoring hard-linked symlinks.  */
129     dev_t dev;
130     ino_t ino;
131     struct timespec birthtime;
132
133     /* True if the link is symbolic.  */
134     bool is_symlink;
135
136     /* The desired metadata, valid only the link is symbolic.  */
137     mode_t mode;
138     uid_t uid;
139     gid_t gid;
140     struct timespec atime;
141     struct timespec mtime;
142
143     /* The directory that the sources and target are relative to.  */
144     int change_dir;
145
146     /* A list of sources for this link.  The sources are all to be
147        hard-linked together.  */
148     struct string_list *sources;
149
150     /* SELinux context */
151     char *cntx_name;
152
153     /* ACLs */
154     char *acls_a_ptr;
155     size_t acls_a_len;
156     char *acls_d_ptr;
157     size_t acls_d_len;
158
159     size_t xattr_map_size;
160     struct xattr_array *xattr_map;
161
162     /* The desired target of the desired link.  */
163     char target[1];
164   };
165
166 static struct delayed_link *delayed_link_head;
167
168 struct string_list
169   {
170     struct string_list *next;
171     char string[1];
172   };
173
174 /*  Set up to extract files.  */
175 void
176 extr_init (void)
177 {
178   we_are_root = geteuid () == ROOT_UID;
179   same_permissions_option += we_are_root;
180   same_owner_option += we_are_root;
181
182   /* Option -p clears the kernel umask, so it does not affect proper
183      restoration of file permissions.  New intermediate directories will
184      comply with umask at start of program.  */
185
186   newdir_umask = umask (0);
187   if (0 < same_permissions_option)
188     current_umask = 0;
189   else
190     {
191       umask (newdir_umask);     /* restore the kernel umask */
192       current_umask = newdir_umask;
193     }
194 }
195
196 /* Use fchmod if possible, fchmodat otherwise.  */
197 static int
198 fd_chmod (int fd, char const *file, mode_t mode, int atflag)
199 {
200   if (0 <= fd)
201     {
202       int result = fchmod (fd, mode);
203       if (result == 0 || implemented (errno))
204         return result;
205     }
206   return fchmodat (chdir_fd, file, mode, atflag);
207 }
208
209 /* Use fchown if possible, fchownat otherwise.  */
210 static int
211 fd_chown (int fd, char const *file, uid_t uid, gid_t gid, int atflag)
212 {
213   if (0 <= fd)
214     {
215       int result = fchown (fd, uid, gid);
216       if (result == 0 || implemented (errno))
217         return result;
218     }
219   return fchownat (chdir_fd, file, uid, gid, atflag);
220 }
221
222 /* Use fstat if possible, fstatat otherwise.  */
223 static int
224 fd_stat (int fd, char const *file, struct stat *st, int atflag)
225 {
226   return (0 <= fd
227           ? fstat (fd, st)
228           : fstatat (chdir_fd, file, st, atflag));
229 }
230
231 /* Set the mode for FILE_NAME to MODE.
232    MODE_MASK specifies the bits of MODE that we care about;
233    thus if MODE_MASK is zero, do nothing.
234    If FD is nonnegative, it is a file descriptor for the file.
235    CURRENT_MODE and CURRENT_MODE_MASK specify information known about
236    the file's current mode, using the style of struct delayed_set_stat.
237    TYPEFLAG specifies the type of the file.
238    ATFLAG specifies the flag to use when statting the file.  */
239 static void
240 set_mode (char const *file_name,
241           mode_t mode, mode_t mode_mask, int fd,
242           mode_t current_mode, mode_t current_mode_mask,
243           char typeflag, int atflag)
244 {
245   if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
246     {
247       if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)
248         {
249           struct stat st;
250           if (fd_stat (fd, file_name, &st, atflag) != 0)
251             {
252               stat_error (file_name);
253               return;
254             }
255           current_mode = st.st_mode;
256         }
257
258       current_mode &= MODE_ALL;
259       mode = (current_mode & ~ mode_mask) | (mode & mode_mask);
260
261       if (current_mode != mode)
262         {
263           int chmod_errno =
264             fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
265
266           /* On Solaris, chmod may fail if we don't have PRIV_ALL, because
267              setuid-root files would otherwise be a backdoor.  See
268              http://opensolaris.org/jive/thread.jspa?threadID=95826
269              (2009-09-03).  */
270           if (chmod_errno == EPERM && (mode & S_ISUID)
271               && priv_set_restore_linkdir () == 0)
272             {
273               chmod_errno =
274                 fd_chmod (fd, file_name, mode, atflag) == 0 ? 0 : errno;
275               priv_set_remove_linkdir ();
276             }
277
278           /* Linux fchmodat does not support AT_SYMLINK_NOFOLLOW, and
279              returns ENOTSUP even when operating on non-symlinks, try
280              again with the flag disabled if it does not appear to be
281              supported and if the file is not a symlink.  This
282              introduces a race, alas.  */
283           if (atflag && typeflag != SYMTYPE && ! implemented (chmod_errno))
284             chmod_errno = fd_chmod (fd, file_name, mode, 0) == 0 ? 0 : errno;
285
286           if (chmod_errno
287               && (typeflag != SYMTYPE || implemented (chmod_errno)))
288             {
289               errno = chmod_errno;
290               chmod_error_details (file_name, mode);
291             }
292         }
293     }
294 }
295
296 /* Check time after successfully setting FILE_NAME's time stamp to T.  */
297 static void
298 check_time (char const *file_name, struct timespec t)
299 {
300   if (t.tv_sec < 0)
301     WARNOPT (WARN_TIMESTAMP,
302              (0, 0, _("%s: implausibly old time stamp %s"),
303               file_name, tartime (t, true)));
304   else if (timespec_cmp (volume_start_time, t) < 0)
305     {
306       struct timespec now;
307       gettime (&now);
308       if (timespec_cmp (now, t) < 0)
309         {
310           char buf[TIMESPEC_STRSIZE_BOUND];
311           struct timespec diff;
312           diff.tv_sec = t.tv_sec - now.tv_sec;
313           diff.tv_nsec = t.tv_nsec - now.tv_nsec;
314           if (diff.tv_nsec < 0)
315             {
316               diff.tv_nsec += BILLION;
317               diff.tv_sec--;
318             }
319           WARNOPT (WARN_TIMESTAMP,
320                    (0, 0, _("%s: time stamp %s is %s s in the future"),
321                     file_name, tartime (t, true), code_timespec (diff, buf)));
322         }
323     }
324 }
325
326 /* Restore stat attributes (owner, group, mode and times) for
327    FILE_NAME, using information given in *ST.
328    If FD is nonnegative, it is a file descriptor for the file.
329    CURRENT_MODE and CURRENT_MODE_MASK specify information known about
330    the file's current mode, using the style of struct delayed_set_stat.
331    TYPEFLAG specifies the type of the file.
332    If INTERDIR, this is an intermediate directory.
333    ATFLAG specifies the flag to use when statting the file.  */
334
335 static void
336 set_stat (char const *file_name,
337           struct tar_stat_info const *st,
338           int fd, mode_t current_mode, mode_t current_mode_mask,
339           char typeflag, bool interdir, int atflag)
340 {
341   /* Do the utime before the chmod because some versions of utime are
342      broken and trash the modes of the file.  */
343
344   if (! touch_option && ! interdir)
345     {
346       struct timespec ts[2];
347       if (incremental_option)
348         ts[0] = st->atime;
349       else
350         ts[0].tv_nsec = UTIME_OMIT;
351       ts[1] = st->mtime;
352
353       if (fdutimensat (fd, chdir_fd, file_name, ts, atflag) == 0)
354         {
355           if (incremental_option)
356             check_time (file_name, ts[0]);
357           check_time (file_name, ts[1]);
358         }
359       else if (typeflag != SYMTYPE || implemented (errno))
360         utime_error (file_name);
361     }
362
363   if (0 < same_owner_option && ! interdir)
364     {
365       /* Some systems allow non-root users to give files away.  Once this
366          done, it is not possible anymore to change file permissions.
367          However, setting file permissions now would be incorrect, since
368          they would apply to the wrong user, and there would be a race
369          condition.  So, don't use systems that allow non-root users to
370          give files away.  */
371       uid_t uid = st->stat.st_uid;
372       gid_t gid = st->stat.st_gid;
373
374       if (fd_chown (fd, file_name, uid, gid, atflag) == 0)
375         {
376           /* Changing the owner can clear st_mode bits in some cases.  */
377           if ((current_mode | ~ current_mode_mask) & S_IXUGO)
378             current_mode_mask &= ~ (current_mode & (S_ISUID | S_ISGID));
379         }
380       else if (typeflag != SYMTYPE || implemented (errno))
381         chown_error_details (file_name, uid, gid);
382     }
383
384   set_mode (file_name,
385             st->stat.st_mode & ~ current_umask,
386             0 < same_permissions_option && ! interdir ? MODE_ALL : MODE_RWX,
387             fd, current_mode, current_mode_mask, typeflag, atflag);
388
389   /* these three calls must be done *after* fd_chown() call because fd_chown
390      causes that linux capabilities becomes cleared. */
391   xattrs_xattrs_set (st, file_name, typeflag, 1);
392   xattrs_acls_set (st, file_name, typeflag);
393   xattrs_selinux_set (st, file_name, typeflag);
394 }
395
396 /* For each entry H in the leading prefix of entries in HEAD that do
397    not have after_links marked, mark H and fill in its dev and ino
398    members.  Assume HEAD && ! HEAD->after_links.  */
399 static void
400 mark_after_links (struct delayed_set_stat *head)
401 {
402   struct delayed_set_stat *h = head;
403
404   do
405     {
406       struct stat st;
407       h->after_links = 1;
408
409       if (deref_stat (h->file_name, &st) != 0)
410         stat_error (h->file_name);
411       else
412         {
413           h->dev = st.st_dev;
414           h->ino = st.st_ino;
415         }
416     }
417   while ((h = h->next) && ! h->after_links);
418 }
419
420 /* Remember to restore stat attributes (owner, group, mode and times)
421    for the directory FILE_NAME, using information given in *ST,
422    once we stop extracting files into that directory.
423
424    If ST is null, merely create a placeholder node for an intermediate
425    directory that was created by make_directories.
426
427    NOTICE: this works only if the archive has usual member order, i.e.
428    directory, then the files in that directory. Incremental archive have
429    somewhat reversed order: first go subdirectories, then all other
430    members. To help cope with this case the variable
431    delay_directory_restore_option is set by prepare_to_extract.
432
433    If an archive was explicitely created so that its member order is
434    reversed, some directory timestamps can be restored incorrectly,
435    e.g.:
436        tar --no-recursion -cf archive dir dir/file1 foo dir/file2
437 */
438 static void
439 delay_set_stat (char const *file_name, struct tar_stat_info const *st,
440                 mode_t current_mode, mode_t current_mode_mask,
441                 mode_t mode, int atflag)
442 {
443   size_t file_name_len = strlen (file_name);
444   struct delayed_set_stat *data = xmalloc (sizeof (*data));
445   data->next = delayed_set_stat_head;
446   data->mode = mode;
447   if (st)
448     {
449       data->dev = st->stat.st_dev;
450       data->ino = st->stat.st_ino;
451       data->uid = st->stat.st_uid;
452       data->gid = st->stat.st_gid;
453       data->atime = st->atime;
454       data->mtime = st->mtime;
455     }
456   data->file_name_len = file_name_len;
457   data->file_name = xstrdup (file_name);
458   data->current_mode = current_mode;
459   data->current_mode_mask = current_mode_mask;
460   data->interdir = ! st;
461   data->atflag = atflag;
462   data->after_links = 0;
463   data->change_dir = chdir_current;
464   data->cntx_name = NULL;
465   if (st)
466     assign_string (&data->cntx_name, st->cntx_name);
467   if (st && st->acls_a_ptr)
468     {
469       data->acls_a_ptr = xmemdup (st->acls_a_ptr, st->acls_a_len + 1);
470       data->acls_a_len = st->acls_a_len;
471     }
472   else
473     {
474       data->acls_a_ptr = NULL;
475       data->acls_a_len = 0;
476     }
477   if (st && st->acls_d_ptr)
478     {
479       data->acls_d_ptr = xmemdup (st->acls_d_ptr, st->acls_d_len + 1);
480       data->acls_d_len = st->acls_d_len;
481     }
482   else
483     {
484       data->acls_d_ptr = NULL;
485       data->acls_d_len = 0;
486     }
487   if (st)
488     xheader_xattr_copy (st, &data->xattr_map, &data->xattr_map_size);
489   else
490     {
491       data->xattr_map = NULL;
492       data->xattr_map_size = 0;
493     }
494   strcpy (data->file_name, file_name);
495   delayed_set_stat_head = data;
496   if (must_be_dot_or_slash (file_name))
497     mark_after_links (data);
498 }
499
500 /* Update the delayed_set_stat info for an intermediate directory
501    created within the file name of DIR.  The intermediate directory turned
502    out to be the same as this directory, e.g. due to ".." or symbolic
503    links.  *DIR_STAT_INFO is the status of the directory.  */
504 static void
505 repair_delayed_set_stat (char const *dir,
506                          struct stat const *dir_stat_info)
507 {
508   struct delayed_set_stat *data;
509   for (data = delayed_set_stat_head;  data;  data = data->next)
510     {
511       struct stat st;
512       if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
513         {
514           stat_error (data->file_name);
515           return;
516         }
517
518       if (st.st_dev == dir_stat_info->st_dev
519           && st.st_ino == dir_stat_info->st_ino)
520         {
521           data->dev = current_stat_info.stat.st_dev;
522           data->ino = current_stat_info.stat.st_ino;
523           data->mode = current_stat_info.stat.st_mode;
524           data->uid = current_stat_info.stat.st_uid;
525           data->gid = current_stat_info.stat.st_gid;
526           data->atime = current_stat_info.atime;
527           data->mtime = current_stat_info.mtime;
528           data->current_mode = st.st_mode;
529           data->current_mode_mask = ALL_MODE_BITS;
530           data->interdir = false;
531           return;
532         }
533     }
534
535   ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
536           quotearg_colon (dir)));
537 }
538
539 static void
540 free_delayed_set_stat (struct delayed_set_stat *data)
541 {
542   free (data->file_name);
543   xheader_xattr_free (data->xattr_map, data->xattr_map_size);
544   free (data->cntx_name);
545   free (data->acls_a_ptr);
546   free (data->acls_d_ptr);
547   free (data);
548 }
549
550 void
551 remove_delayed_set_stat (const char *fname)
552 {
553   struct delayed_set_stat *data, *next, *prev = NULL;
554   for (data = delayed_set_stat_head; data; data = next)
555     {
556       next = data->next;
557       if (chdir_current == data->change_dir
558           && strcmp (data->file_name, fname) == 0)
559         {
560           free_delayed_set_stat (data);
561           if (prev)
562             prev->next = next;
563           else
564             delayed_set_stat_head = next;
565           return;
566         }
567       else
568         prev = data;
569     }
570 }
571
572 static void
573 fixup_delayed_set_stat (char const *src, char const *dst)
574 {
575   struct delayed_set_stat *data;
576   for (data = delayed_set_stat_head; data; data = data->next)
577     {
578       if (chdir_current == data->change_dir
579           && strcmp (data->file_name, src) == 0)
580         {
581           free (data->file_name);
582           data->file_name = xstrdup (dst);
583           data->file_name_len = strlen (dst);
584           return;
585         }
586     }
587 }
588
589 /* After a file/link/directory creation has failed, see if
590    it's because some required directory was not present, and if so,
591    create all required directories.  Return zero if all the required
592    directories were created, nonzero (issuing a diagnostic) otherwise.
593    Set *INTERDIR_MADE if at least one directory was created.  */
594 static int
595 make_directories (char *file_name, bool *interdir_made)
596 {
597   char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
598   char *cursor;                 /* points into the file name */
599
600   for (cursor = cursor0; *cursor; cursor++)
601     {
602       mode_t mode;
603       mode_t desired_mode;
604       int status;
605
606       if (! ISSLASH (*cursor))
607         continue;
608
609       /* Avoid mkdir of empty string, if leading or double '/'.  */
610
611       if (cursor == cursor0 || ISSLASH (cursor[-1]))
612         continue;
613
614       /* Avoid mkdir where last part of file name is "." or "..".  */
615
616       if (cursor[-1] == '.'
617           && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
618               || (cursor[-2] == '.'
619                   && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
620         continue;
621
622       *cursor = '\0';           /* truncate the name there */
623       desired_mode = MODE_RWX & ~ newdir_umask;
624       mode = desired_mode | (we_are_root ? 0 : MODE_WXUSR);
625       status = mkdirat (chdir_fd, file_name, mode);
626
627       if (status == 0)
628         {
629           /* Create a struct delayed_set_stat even if
630              mode == desired_mode, because
631              repair_delayed_set_stat may need to update the struct.  */
632           delay_set_stat (file_name,
633                           0, mode & ~ current_umask, MODE_RWX,
634                           desired_mode, AT_SYMLINK_NOFOLLOW);
635
636           print_for_mkdir (file_name, cursor - file_name, desired_mode);
637           *interdir_made = true;
638         }
639       else if (errno == EEXIST)
640         status = 0;
641       else
642         {
643           /* Check whether the desired file exists.  Even when the
644              file exists, mkdir can fail with some errno value E other
645              than EEXIST, so long as E describes an error condition
646              that also applies.  */
647           int e = errno;
648           struct stat st;
649           status = fstatat (chdir_fd, file_name, &st, 0);
650           if (status)
651             {
652               errno = e;
653               mkdir_error (file_name);
654             }
655         }
656
657       *cursor = '/';
658       if (status)
659         return status;
660     }
661
662   return 0;
663 }
664
665 /* Return true if FILE_NAME (with status *STP, if STP) is not a
666    directory, and has a time stamp newer than (or equal to) that of
667    TAR_STAT.  */
668 static bool
669 file_newer_p (const char *file_name, struct stat const *stp,
670               struct tar_stat_info *tar_stat)
671 {
672   struct stat st;
673
674   if (!stp)
675     {
676       if (deref_stat (file_name, &st) != 0)
677         {
678           if (errno != ENOENT)
679             {
680               stat_warn (file_name);
681               /* Be safer: if the file exists, assume it is newer.  */
682               return true;
683             }
684           return false;
685         }
686       stp = &st;
687     }
688
689   return (! S_ISDIR (stp->st_mode)
690           && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (stp)) <= 0);
691 }
692
693 #define RECOVER_NO 0
694 #define RECOVER_OK 1
695 #define RECOVER_SKIP 2
696
697 /* Attempt repairing what went wrong with the extraction.  Delete an
698    already existing file or create missing intermediate directories.
699    Return RECOVER_OK if we somewhat increased our chances at a successful
700    extraction, RECOVER_NO if there are no chances, and RECOVER_SKIP if the
701    caller should skip extraction of that member.  The value of errno is
702    properly restored on returning RECOVER_NO.
703
704    If REGULAR, the caller was trying to extract onto a regular file.
705
706    Set *INTERDIR_MADE if an intermediate directory is made as part of
707    the recovery process.  */
708
709 static int
710 maybe_recoverable (char *file_name, bool regular, bool *interdir_made)
711 {
712   int e = errno;
713   struct stat st;
714   struct stat const *stp = 0;
715
716   if (*interdir_made)
717     return RECOVER_NO;
718
719   switch (e)
720     {
721     case ELOOP:
722
723       /* With open ("symlink", O_NOFOLLOW|...), POSIX says errno == ELOOP,
724          but some operating systems do not conform to the standard.  */
725 #ifdef EFTYPE
726       /* NetBSD uses errno == EFTYPE; see <http://gnats.netbsd.org/43154>.  */
727     case EFTYPE:
728 #endif
729       /* FreeBSD 8.1 uses errno == EMLINK.  */
730     case EMLINK:
731       /* Tru64 5.1B uses errno == ENOTSUP.  */
732     case ENOTSUP:
733
734       if (! regular
735           || old_files_option != OVERWRITE_OLD_FILES || dereference_option)
736         break;
737       if (strchr (file_name, '/'))
738         {
739           if (deref_stat (file_name, &st) != 0)
740             break;
741           stp = &st;
742         }
743
744       /* The caller tried to open a symbolic link with O_NOFOLLOW.
745          Fall through, treating it as an already-existing file.  */
746
747     case EEXIST:
748       /* Remove an old file, if the options allow this.  */
749
750       switch (old_files_option)
751         {
752         case SKIP_OLD_FILES:
753           WARNOPT (WARN_EXISTING_FILE,
754                    (0, 0, _("%s: skipping existing file"), file_name));
755           return RECOVER_SKIP;
756
757         case KEEP_OLD_FILES:
758           return RECOVER_NO;
759
760         case KEEP_NEWER_FILES:
761           if (file_newer_p (file_name, stp, &current_stat_info))
762             break;
763           /* FALL THROUGH */
764
765         case DEFAULT_OLD_FILES:
766         case NO_OVERWRITE_DIR_OLD_FILES:
767         case OVERWRITE_OLD_FILES:
768           if (0 < remove_any_file (file_name, ORDINARY_REMOVE_OPTION))
769             return RECOVER_OK;
770           break;
771
772         case UNLINK_FIRST_OLD_FILES:
773           break;
774         }
775
776     case ENOENT:
777       /* Attempt creating missing intermediate directories.  */
778       if (make_directories (file_name, interdir_made) == 0 && *interdir_made)
779         return RECOVER_OK;
780       break;
781
782     default:
783       /* Just say we can't do anything about it...  */
784       break;
785     }
786
787   errno = e;
788   return RECOVER_NO;
789 }
790
791 /* Restore stat extended attributes (xattr) for FILE_NAME, using information
792    given in *ST.  Restore before extraction because they may affect file layout
793    (e.g. on Lustre distributed parallel filesystem - setting info about how many
794    servers is this file striped over, stripe size, mirror copies, etc.
795    in advance dramatically improves the following  performance of reading and
796    writing a file).  If not restoring permissions, invert the INVERT_PERMISSIONS
797    bits from the file's current permissions.  TYPEFLAG specifies the type of the
798    file.  FILE_CREATED indicates set_xattr has created the file */
799 static int
800 set_xattr (char const *file_name, struct tar_stat_info const *st,
801            mode_t invert_permissions, char typeflag, int *file_created)
802 {
803   int status = 0;
804
805 #ifdef HAVE_XATTRS
806   bool interdir_made = false;
807
808   if ((xattrs_option > 0) && st->xattr_map_size)
809     {
810       mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
811
812       do
813         status = mknodat (chdir_fd, file_name, mode ^ invert_permissions, 0);
814       while (status && maybe_recoverable ((char *)file_name, false,
815                                           &interdir_made));
816
817       xattrs_xattrs_set (st, file_name, typeflag, 0);
818       *file_created = 1;
819     }
820 #endif
821
822   return(status);
823 }
824
825 /* Fix the statuses of all directories whose statuses need fixing, and
826    which are not ancestors of FILE_NAME.  If AFTER_LINKS is
827    nonzero, do this for all such directories; otherwise, stop at the
828    first directory that is marked to be fixed up only after delayed
829    links are applied.  */
830 static void
831 apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
832 {
833   size_t file_name_len = strlen (file_name);
834   bool check_for_renamed_directories = 0;
835
836   while (delayed_set_stat_head)
837     {
838       struct delayed_set_stat *data = delayed_set_stat_head;
839       bool skip_this_one = 0;
840       struct stat st;
841       mode_t current_mode = data->current_mode;
842       mode_t current_mode_mask = data->current_mode_mask;
843
844       check_for_renamed_directories |= data->after_links;
845
846       if (after_links < data->after_links
847           || (data->file_name_len < file_name_len
848               && file_name[data->file_name_len]
849               && (ISSLASH (file_name[data->file_name_len])
850                   || ISSLASH (file_name[data->file_name_len - 1]))
851               && memcmp (file_name, data->file_name, data->file_name_len) == 0))
852         break;
853
854       chdir_do (data->change_dir);
855
856       if (check_for_renamed_directories)
857         {
858           if (fstatat (chdir_fd, data->file_name, &st, data->atflag) != 0)
859             {
860               stat_error (data->file_name);
861               skip_this_one = 1;
862             }
863           else
864             {
865               current_mode = st.st_mode;
866               current_mode_mask = ALL_MODE_BITS;
867               if (! (st.st_dev == data->dev && st.st_ino == data->ino))
868                 {
869                   ERROR ((0, 0,
870                           _("%s: Directory renamed before its status could be extracted"),
871                           quotearg_colon (data->file_name)));
872                   skip_this_one = 1;
873                 }
874             }
875         }
876
877       if (! skip_this_one)
878         {
879           struct tar_stat_info sb;
880           sb.stat.st_mode = data->mode;
881           sb.stat.st_uid = data->uid;
882           sb.stat.st_gid = data->gid;
883           sb.atime = data->atime;
884           sb.mtime = data->mtime;
885           sb.cntx_name = data->cntx_name;
886           sb.acls_a_ptr = data->acls_a_ptr;
887           sb.acls_a_len = data->acls_a_len;
888           sb.acls_d_ptr = data->acls_d_ptr;
889           sb.acls_d_len = data->acls_d_len;
890           sb.xattr_map = data->xattr_map;
891           sb.xattr_map_size = data->xattr_map_size;
892           set_stat (data->file_name, &sb,
893                     -1, current_mode, current_mode_mask,
894                     DIRTYPE, data->interdir, data->atflag);
895         }
896
897       delayed_set_stat_head = data->next;
898       free_delayed_set_stat (data);
899     }
900 }
901
902 \f
903 static bool
904 is_directory_link (const char *file_name)
905 {
906   struct stat st;
907   int e = errno;
908   int res;
909   
910   res = (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
911          S_ISLNK (st.st_mode) &&
912          fstatat (chdir_fd, file_name, &st, 0) == 0 &&
913          S_ISDIR (st.st_mode));
914   errno = e;
915   return res;
916 }
917 \f
918 /* Extractor functions for various member types */
919
920 static int
921 extract_dir (char *file_name, int typeflag)
922 {
923   int status;
924   mode_t mode;
925   mode_t current_mode = 0;
926   mode_t current_mode_mask = 0;
927   int atflag = 0;
928   bool interdir_made = false;
929
930   /* Save 'root device' to avoid purging mount points. */
931   if (one_file_system_option && root_device == 0)
932     {
933       struct stat st;
934
935       if (fstatat (chdir_fd, ".", &st, 0) != 0)
936         stat_diag (".");
937       else
938         root_device = st.st_dev;
939     }
940
941   if (incremental_option)
942     /* Read the entry and delete files that aren't listed in the archive.  */
943     purge_directory (file_name);
944   else if (typeflag == GNUTYPE_DUMPDIR)
945     skip_member ();
946
947   /* If ownership or permissions will be restored later, create the
948      directory with restrictive permissions at first, so that in the
949      meantime processes owned by other users do not inadvertently
950      create files under this directory that inherit the wrong owner,
951      group, or permissions from the directory.  If not root, though,
952      make the directory writeable and searchable at first, so that
953      files can be created under it.  */
954   mode = ((current_stat_info.stat.st_mode
955            & (0 < same_owner_option || 0 < same_permissions_option
956               ? S_IRWXU
957               : MODE_RWX))
958           | (we_are_root ? 0 : MODE_WXUSR));
959
960   for (;;)
961     {
962       status = mkdirat (chdir_fd, file_name, mode);
963       if (status == 0)
964         {
965           current_mode = mode & ~ current_umask;
966           current_mode_mask = MODE_RWX;
967           atflag = AT_SYMLINK_NOFOLLOW;
968           break;
969         }
970
971       if (errno == EEXIST
972           && (interdir_made
973               || keep_directory_symlink_option
974               || old_files_option == DEFAULT_OLD_FILES
975               || old_files_option == OVERWRITE_OLD_FILES))
976         {
977           struct stat st;
978
979           if (keep_directory_symlink_option && is_directory_link (file_name))
980             return 0;
981           
982           if (deref_stat (file_name, &st) == 0)
983             {
984               current_mode = st.st_mode;
985               current_mode_mask = ALL_MODE_BITS;
986
987               if (S_ISDIR (current_mode))
988                 {
989                   if (interdir_made)
990                     {
991                       repair_delayed_set_stat (file_name, &st);
992                       return 0;
993                     }
994                   break;
995                 }
996             }
997           errno = EEXIST;
998         }
999
1000       switch (maybe_recoverable (file_name, false, &interdir_made))
1001         {
1002         case RECOVER_OK:
1003           continue;
1004
1005         case RECOVER_SKIP:
1006           break;
1007
1008         case RECOVER_NO:
1009           if (errno != EEXIST)
1010             {
1011               mkdir_error (file_name);
1012               return 1;
1013             }
1014           break;
1015         }
1016       break;
1017     }
1018
1019   if (status == 0
1020       || old_files_option == DEFAULT_OLD_FILES
1021       || old_files_option == OVERWRITE_OLD_FILES)
1022     delay_set_stat (file_name, &current_stat_info,
1023                     current_mode, current_mode_mask,
1024                     current_stat_info.stat.st_mode, atflag);
1025   return status;
1026 }
1027
1028
1029
1030 static int
1031 open_output_file (char const *file_name, int typeflag, mode_t mode,
1032                   int file_created, mode_t *current_mode,
1033                   mode_t *current_mode_mask)
1034 {
1035   int fd;
1036   bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
1037   int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
1038                   | O_CREAT
1039                   | (overwriting_old_files
1040                      ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
1041                      : O_EXCL));
1042
1043   /* File might be created in set_xattr. So clear O_EXCL to avoid open() fail */
1044   if (file_created)
1045     openflag = openflag & ~O_EXCL;
1046
1047   if (typeflag == CONTTYPE)
1048     {
1049       static int conttype_diagnosed;
1050
1051       if (!conttype_diagnosed)
1052         {
1053           conttype_diagnosed = 1;
1054           WARNOPT (WARN_CONTIGUOUS_CAST,
1055                    (0, 0, _("Extracting contiguous files as regular files")));
1056         }
1057     }
1058
1059   /* If O_NOFOLLOW is needed but does not work, check for a symlink
1060      separately.  There's a race condition, but that cannot be avoided
1061      on hosts lacking O_NOFOLLOW.  */
1062   if (! HAVE_WORKING_O_NOFOLLOW
1063       && overwriting_old_files && ! dereference_option)
1064     {
1065       struct stat st;
1066       if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
1067           && S_ISLNK (st.st_mode))
1068         {
1069           errno = ELOOP;
1070           return -1;
1071         }
1072     }
1073
1074   fd = openat (chdir_fd, file_name, openflag, mode);
1075   if (0 <= fd)
1076     {
1077       if (overwriting_old_files)
1078         {
1079           struct stat st;
1080           if (fstat (fd, &st) != 0)
1081             {
1082               int e = errno;
1083               close (fd);
1084               errno = e;
1085               return -1;
1086             }
1087           if (! S_ISREG (st.st_mode))
1088             {
1089               close (fd);
1090               errno = EEXIST;
1091               return -1;
1092             }
1093           *current_mode = st.st_mode;
1094           *current_mode_mask = ALL_MODE_BITS;
1095         }
1096       else
1097         {
1098           *current_mode = mode & ~ current_umask;
1099           *current_mode_mask = MODE_RWX;
1100         }
1101     }
1102
1103   return fd;
1104 }
1105
1106 static int
1107 extract_file (char *file_name, int typeflag)
1108 {
1109   int fd;
1110   off_t size;
1111   union block *data_block;
1112   int status;
1113   size_t count;
1114   size_t written;
1115   bool interdir_made = false;
1116   mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1117                  & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1118   mode_t invert_permissions = 0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO)
1119                                                     : 0;
1120   mode_t current_mode = 0;
1121   mode_t current_mode_mask = 0;
1122
1123   if (to_stdout_option)
1124     fd = STDOUT_FILENO;
1125   else if (to_command_option)
1126     {
1127       fd = sys_exec_command (file_name, 'f', &current_stat_info);
1128       if (fd < 0)
1129         {
1130           skip_member ();
1131           return 0;
1132         }
1133     }
1134   else
1135     {
1136       int file_created = 0;
1137       if (set_xattr (file_name, &current_stat_info, invert_permissions,
1138                      typeflag, &file_created))
1139         {
1140           skip_member ();
1141           open_error (file_name);
1142           return 1;
1143         }
1144
1145       while ((fd = open_output_file (file_name, typeflag, mode,
1146                                      file_created, &current_mode,
1147                                      &current_mode_mask))
1148              < 0)
1149         {
1150           int recover = maybe_recoverable (file_name, true, &interdir_made);
1151           if (recover != RECOVER_OK)
1152             {
1153               skip_member ();
1154               if (recover == RECOVER_SKIP)
1155                 return 0;
1156               open_error (file_name);
1157               return 1;
1158             }
1159         }
1160     }
1161
1162   mv_begin_read (&current_stat_info);
1163   if (current_stat_info.is_sparse)
1164     sparse_extract_file (fd, &current_stat_info, &size);
1165   else
1166     for (size = current_stat_info.stat.st_size; size > 0; )
1167       {
1168         mv_size_left (size);
1169
1170         /* Locate data, determine max length writeable, write it,
1171            block that we have used the data, then check if the write
1172            worked.  */
1173
1174         data_block = find_next_block ();
1175         if (! data_block)
1176           {
1177             ERROR ((0, 0, _("Unexpected EOF in archive")));
1178             break;              /* FIXME: What happens, then?  */
1179           }
1180
1181         written = available_space_after (data_block);
1182
1183         if (written > size)
1184           written = size;
1185         errno = 0;
1186         count = blocking_write (fd, data_block->buffer, written);
1187         size -= written;
1188
1189         set_next_block_after ((union block *)
1190                               (data_block->buffer + written - 1));
1191         if (count != written)
1192           {
1193             if (!to_command_option)
1194               write_error_details (file_name, count, written);
1195             /* FIXME: shouldn't we restore from backup? */
1196             break;
1197           }
1198       }
1199
1200   skip_file (size);
1201
1202   mv_end ();
1203
1204   /* If writing to stdout, don't try to do anything to the filename;
1205      it doesn't exist, or we don't want to touch it anyway.  */
1206
1207   if (to_stdout_option)
1208     return 0;
1209
1210   if (! to_command_option)
1211     set_stat (file_name, &current_stat_info, fd,
1212               current_mode, current_mode_mask, typeflag, false,
1213               (old_files_option == OVERWRITE_OLD_FILES
1214                ? 0 : AT_SYMLINK_NOFOLLOW));
1215
1216   status = close (fd);
1217   if (status < 0)
1218     close_error (file_name);
1219
1220   if (to_command_option)
1221     sys_wait_command ();
1222
1223   return status;
1224 }
1225
1226 /* Create a placeholder file with name FILE_NAME, which will be
1227    replaced after other extraction is done by a symbolic link if
1228    IS_SYMLINK is true, and by a hard link otherwise.  Set
1229    *INTERDIR_MADE if an intermediate directory is made in the
1230    process.  */
1231
1232 static int
1233 create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
1234 {
1235   int fd;
1236   struct stat st;
1237
1238   while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
1239     {
1240       switch (maybe_recoverable (file_name, false, interdir_made))
1241         {
1242         case RECOVER_OK:
1243           continue;
1244
1245         case RECOVER_SKIP:
1246           return 0;
1247
1248         case RECOVER_NO:
1249           open_error (file_name);
1250           return -1;
1251         }
1252       }
1253
1254   if (fstat (fd, &st) != 0)
1255     {
1256       stat_error (file_name);
1257       close (fd);
1258     }
1259   else if (close (fd) != 0)
1260     close_error (file_name);
1261   else
1262     {
1263       struct delayed_set_stat *h;
1264       struct delayed_link *p =
1265         xmalloc (offsetof (struct delayed_link, target)
1266                  + strlen (current_stat_info.link_name)
1267                  + 1);
1268       p->next = delayed_link_head;
1269       delayed_link_head = p;
1270       p->dev = st.st_dev;
1271       p->ino = st.st_ino;
1272       p->birthtime = get_stat_birthtime (&st);
1273       p->is_symlink = is_symlink;
1274       if (is_symlink)
1275         {
1276           p->mode = current_stat_info.stat.st_mode;
1277           p->uid = current_stat_info.stat.st_uid;
1278           p->gid = current_stat_info.stat.st_gid;
1279           p->atime = current_stat_info.atime;
1280           p->mtime = current_stat_info.mtime;
1281         }
1282       p->change_dir = chdir_current;
1283       p->sources = xmalloc (offsetof (struct string_list, string)
1284                             + strlen (file_name) + 1);
1285       p->sources->next = 0;
1286       strcpy (p->sources->string, file_name);
1287       p->cntx_name = NULL;
1288       assign_string (&p->cntx_name, current_stat_info.cntx_name);
1289       p->acls_a_ptr = NULL;
1290       p->acls_a_len = 0;
1291       p->acls_d_ptr = NULL;
1292       p->acls_d_len = 0;
1293       xheader_xattr_copy (&current_stat_info, &p->xattr_map, &p->xattr_map_size);
1294       strcpy (p->target, current_stat_info.link_name);
1295
1296       h = delayed_set_stat_head;
1297       if (h && ! h->after_links
1298           && strncmp (file_name, h->file_name, h->file_name_len) == 0
1299           && ISSLASH (file_name[h->file_name_len])
1300           && (last_component (file_name) == file_name + h->file_name_len + 1))
1301         mark_after_links (h);
1302
1303       return 0;
1304     }
1305
1306   return -1;
1307 }
1308
1309 static int
1310 extract_link (char *file_name, int typeflag)
1311 {
1312   bool interdir_made = false;
1313   char const *link_name;
1314   int rc;
1315
1316   link_name = current_stat_info.link_name;
1317
1318   if (! absolute_names_option && contains_dot_dot (link_name))
1319     return create_placeholder_file (file_name, false, &interdir_made);
1320
1321   do
1322     {
1323       struct stat st1, st2;
1324       int e;
1325       int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
1326       e = errno;
1327
1328       if (status == 0)
1329         {
1330           struct delayed_link *ds = delayed_link_head;
1331           if (ds
1332               && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
1333             for (; ds; ds = ds->next)
1334               if (ds->change_dir == chdir_current
1335                   && ds->dev == st1.st_dev
1336                   && ds->ino == st1.st_ino
1337                   && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
1338                       == 0))
1339                 {
1340                   struct string_list *p =  xmalloc (offsetof (struct string_list, string)
1341                                                     + strlen (file_name) + 1);
1342                   strcpy (p->string, file_name);
1343                   p->next = ds->sources;
1344                   ds->sources = p;
1345                   break;
1346                 }
1347           return 0;
1348         }
1349       else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1350                || ((fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW)
1351                     == 0)
1352                    && (fstatat (chdir_fd, file_name, &st2, AT_SYMLINK_NOFOLLOW)
1353                        == 0)
1354                    && st1.st_dev == st2.st_dev
1355                    && st1.st_ino == st2.st_ino))
1356         return 0;
1357
1358       errno = e;
1359     }
1360   while ((rc = maybe_recoverable (file_name, false, &interdir_made))
1361          == RECOVER_OK);
1362
1363   if (rc == RECOVER_SKIP)
1364     return 0;
1365   if (!(incremental_option && errno == EEXIST))
1366     {
1367       link_error (link_name, file_name);
1368       return 1;
1369     }
1370   return 0;
1371 }
1372
1373 static int
1374 extract_symlink (char *file_name, int typeflag)
1375 {
1376 #ifdef HAVE_SYMLINK
1377   bool interdir_made = false;
1378
1379   if (! absolute_names_option
1380       && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1381           || contains_dot_dot (current_stat_info.link_name)))
1382     return create_placeholder_file (file_name, true, &interdir_made);
1383
1384   while (symlinkat (current_stat_info.link_name, chdir_fd, file_name) != 0)
1385     switch (maybe_recoverable (file_name, false, &interdir_made))
1386       {
1387       case RECOVER_OK:
1388         continue;
1389
1390       case RECOVER_SKIP:
1391         return 0;
1392
1393       case RECOVER_NO:
1394         symlink_error (current_stat_info.link_name, file_name);
1395         return -1;
1396       }
1397
1398   set_stat (file_name, &current_stat_info, -1, 0, 0,
1399             SYMTYPE, false, AT_SYMLINK_NOFOLLOW);
1400   return 0;
1401
1402 #else
1403   static int warned_once;
1404
1405   if (!warned_once)
1406     {
1407       warned_once = 1;
1408       WARNOPT (WARN_SYMLINK_CAST,
1409                (0, 0,
1410                 _("Attempting extraction of symbolic links as hard links")));
1411     }
1412   return extract_link (file_name, typeflag);
1413 #endif
1414 }
1415
1416 #if S_IFCHR || S_IFBLK
1417 static int
1418 extract_node (char *file_name, int typeflag)
1419 {
1420   bool interdir_made = false;
1421   mode_t mode = (current_stat_info.stat.st_mode & (MODE_RWX | S_IFBLK | S_IFCHR)
1422                  & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1423
1424   while (mknodat (chdir_fd, file_name, mode, current_stat_info.stat.st_rdev)
1425          != 0)
1426     switch (maybe_recoverable (file_name, false, &interdir_made))
1427       {
1428       case RECOVER_OK:
1429         continue;
1430
1431       case RECOVER_SKIP:
1432         return 0;
1433
1434       case RECOVER_NO:
1435         mknod_error (file_name);
1436         return -1;
1437       }
1438
1439   set_stat (file_name, &current_stat_info, -1,
1440             mode & ~ current_umask, MODE_RWX,
1441             typeflag, false, AT_SYMLINK_NOFOLLOW);
1442   return 0;
1443 }
1444 #endif
1445
1446 #if HAVE_MKFIFO || defined mkfifo
1447 static int
1448 extract_fifo (char *file_name, int typeflag)
1449 {
1450   bool interdir_made = false;
1451   mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
1452                  & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
1453
1454   while (mkfifoat (chdir_fd, file_name, mode) != 0)
1455     switch (maybe_recoverable (file_name, false, &interdir_made))
1456       {
1457       case RECOVER_OK:
1458         continue;
1459
1460       case RECOVER_SKIP:
1461         return 0;
1462
1463       case RECOVER_NO:
1464         mkfifo_error (file_name);
1465         return -1;
1466       }
1467
1468   set_stat (file_name, &current_stat_info, -1,
1469             mode & ~ current_umask, MODE_RWX,
1470             typeflag, false, AT_SYMLINK_NOFOLLOW);
1471   return 0;
1472 }
1473 #endif
1474
1475 static int
1476 extract_volhdr (char *file_name, int typeflag)
1477 {
1478   skip_member ();
1479   return 0;
1480 }
1481
1482 static int
1483 extract_failure (char *file_name, int typeflag)
1484 {
1485   return 1;
1486 }
1487
1488 static int
1489 extract_skip (char *file_name, int typeflag)
1490 {
1491   skip_member ();
1492   return 0;
1493 }
1494
1495 typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1496
1497 \f
1498
1499 /* Prepare to extract a file. Find extractor function.
1500    Return zero if extraction should not proceed.  */
1501
1502 static int
1503 prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1504 {
1505   int rc = 1;
1506
1507   if (EXTRACT_OVER_PIPE)
1508     rc = 0;
1509
1510   /* Select the extractor */
1511   switch (typeflag)
1512     {
1513     case GNUTYPE_SPARSE:
1514       *fun = extract_file;
1515       rc = 1;
1516       break;
1517
1518     case AREGTYPE:
1519     case REGTYPE:
1520     case CONTTYPE:
1521       /* Appears to be a file.  But BSD tar uses the convention that a slash
1522          suffix means a directory.  */
1523       if (current_stat_info.had_trailing_slash)
1524         *fun = extract_dir;
1525       else
1526         {
1527           *fun = extract_file;
1528           rc = 1;
1529         }
1530       break;
1531
1532     case SYMTYPE:
1533       *fun = extract_symlink;
1534       break;
1535
1536     case LNKTYPE:
1537       *fun = extract_link;
1538       break;
1539
1540 #if S_IFCHR
1541     case CHRTYPE:
1542       current_stat_info.stat.st_mode |= S_IFCHR;
1543       *fun = extract_node;
1544       break;
1545 #endif
1546
1547 #if S_IFBLK
1548     case BLKTYPE:
1549       current_stat_info.stat.st_mode |= S_IFBLK;
1550       *fun = extract_node;
1551       break;
1552 #endif
1553
1554 #if HAVE_MKFIFO || defined mkfifo
1555     case FIFOTYPE:
1556       *fun = extract_fifo;
1557       break;
1558 #endif
1559
1560     case DIRTYPE:
1561     case GNUTYPE_DUMPDIR:
1562       *fun = extract_dir;
1563       if (current_stat_info.is_dumpdir)
1564         delay_directory_restore_option = true;
1565       break;
1566
1567     case GNUTYPE_VOLHDR:
1568       *fun = extract_volhdr;
1569       break;
1570
1571     case GNUTYPE_MULTIVOL:
1572       ERROR ((0, 0,
1573               _("%s: Cannot extract -- file is continued from another volume"),
1574               quotearg_colon (current_stat_info.file_name)));
1575       *fun = extract_skip;
1576       break;
1577
1578     case GNUTYPE_LONGNAME:
1579     case GNUTYPE_LONGLINK:
1580       ERROR ((0, 0, _("Unexpected long name header")));
1581       *fun = extract_failure;
1582       break;
1583
1584     default:
1585       WARNOPT (WARN_UNKNOWN_CAST,
1586                (0, 0,
1587                 _("%s: Unknown file type '%c', extracted as normal file"),
1588                 quotearg_colon (file_name), typeflag));
1589       *fun = extract_file;
1590     }
1591
1592   /* Determine whether the extraction should proceed */
1593   if (rc == 0)
1594     return 0;
1595
1596   switch (old_files_option)
1597     {
1598     case UNLINK_FIRST_OLD_FILES:
1599       if (!remove_any_file (file_name,
1600                             recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1601                                                       : ORDINARY_REMOVE_OPTION)
1602           && errno && errno != ENOENT)
1603         {
1604           unlink_error (file_name);
1605           return 0;
1606         }
1607       break;
1608
1609     case KEEP_NEWER_FILES:
1610       if (file_newer_p (file_name, 0, &current_stat_info))
1611         {
1612           WARNOPT (WARN_IGNORE_NEWER,
1613                    (0, 0, _("Current %s is newer or same age"),
1614                     quote (file_name)));
1615           return 0;
1616         }
1617       break;
1618
1619     default:
1620       break;
1621     }
1622
1623   return 1;
1624 }
1625
1626 /* Extract a file from the archive.  */
1627 void
1628 extract_archive (void)
1629 {
1630   char typeflag;
1631   tar_extractor_t fun;
1632
1633   fatal_exit_hook = extract_finish;
1634
1635   set_next_block_after (current_header);
1636
1637   if (!current_stat_info.file_name[0]
1638       || (interactive_option
1639           && !confirm ("extract", current_stat_info.file_name)))
1640     {
1641       skip_member ();
1642       return;
1643     }
1644
1645   /* Print the block from current_header and current_stat.  */
1646   if (verbose_option)
1647     print_header (&current_stat_info, current_header, -1);
1648
1649   /* Restore stats for all non-ancestor directories, unless
1650      it is an incremental archive.
1651      (see NOTICE in the comment to delay_set_stat above) */
1652   if (!delay_directory_restore_option)
1653     {
1654       int dir = chdir_current;
1655       apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1656       chdir_do (dir);
1657     }
1658
1659   /* Take a safety backup of a previously existing file.  */
1660
1661   if (backup_option)
1662     if (!maybe_backup_file (current_stat_info.file_name, 0))
1663       {
1664         int e = errno;
1665         ERROR ((0, e, _("%s: Was unable to backup this file"),
1666                 quotearg_colon (current_stat_info.file_name)));
1667         skip_member ();
1668         return;
1669       }
1670
1671   /* Extract the archive entry according to its type.  */
1672   /* KLUDGE */
1673   typeflag = sparse_member_p (&current_stat_info) ?
1674                   GNUTYPE_SPARSE : current_header->header.typeflag;
1675
1676   if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1677     {
1678       if (fun && (*fun) (current_stat_info.file_name, typeflag)
1679           && backup_option)
1680         undo_last_backup ();
1681     }
1682   else
1683     skip_member ();
1684
1685 }
1686
1687 /* Extract the links whose final extraction were delayed.  */
1688 static void
1689 apply_delayed_links (void)
1690 {
1691   struct delayed_link *ds;
1692
1693   for (ds = delayed_link_head; ds; )
1694     {
1695       struct string_list *sources = ds->sources;
1696       char const *valid_source = 0;
1697
1698       chdir_do (ds->change_dir);
1699
1700       for (sources = ds->sources; sources; sources = sources->next)
1701         {
1702           char const *source = sources->string;
1703           struct stat st;
1704
1705           /* Make sure the placeholder file is still there.  If not,
1706              don't create a link, as the placeholder was probably
1707              removed by a later extraction.  */
1708           if (fstatat (chdir_fd, source, &st, AT_SYMLINK_NOFOLLOW) == 0
1709               && st.st_dev == ds->dev
1710               && st.st_ino == ds->ino
1711               && timespec_cmp (get_stat_birthtime (&st), ds->birthtime) == 0)
1712             {
1713               /* Unlink the placeholder, then create a hard link if possible,
1714                  a symbolic link otherwise.  */
1715               if (unlinkat (chdir_fd, source, 0) != 0)
1716                 unlink_error (source);
1717               else if (valid_source
1718                        && (linkat (chdir_fd, valid_source, chdir_fd, source, 0)
1719                            == 0))
1720                 ;
1721               else if (!ds->is_symlink)
1722                 {
1723                   if (linkat (chdir_fd, ds->target, chdir_fd, source, 0) != 0)
1724                     link_error (ds->target, source);
1725                 }
1726               else if (symlinkat (ds->target, chdir_fd, source) != 0)
1727                 symlink_error (ds->target, source);
1728               else
1729                 {
1730                   struct tar_stat_info st1;
1731                   st1.stat.st_mode = ds->mode;
1732                   st1.stat.st_uid = ds->uid;
1733                   st1.stat.st_gid = ds->gid;
1734                   st1.atime = ds->atime;
1735                   st1.mtime = ds->mtime;
1736                   st1.cntx_name = ds->cntx_name;
1737                   st1.acls_a_ptr = ds->acls_a_ptr;
1738                   st1.acls_a_len = ds->acls_a_len;
1739                   st1.acls_d_ptr = ds->acls_d_ptr;
1740                   st1.acls_d_len = ds->acls_d_len;
1741                   st1.xattr_map = ds->xattr_map;
1742                   st1.xattr_map_size = ds->xattr_map_size;
1743                   set_stat (source, &st1, -1, 0, 0, SYMTYPE,
1744                             false, AT_SYMLINK_NOFOLLOW);
1745                   valid_source = source;
1746                 }
1747             }
1748         }
1749
1750       for (sources = ds->sources; sources; )
1751         {
1752           struct string_list *next = sources->next;
1753           free (sources);
1754           sources = next;
1755         }
1756
1757    xheader_xattr_free (ds->xattr_map, ds->xattr_map_size);
1758    free (ds->cntx_name);
1759
1760       {
1761         struct delayed_link *next = ds->next;
1762         free (ds);
1763         ds = next;
1764       }
1765     }
1766
1767   delayed_link_head = 0;
1768 }
1769
1770 /* Finish the extraction of an archive.  */
1771 void
1772 extract_finish (void)
1773 {
1774   /* First, fix the status of ordinary directories that need fixing.  */
1775   apply_nonancestor_delayed_set_stat ("", 0);
1776
1777   /* Then, apply delayed links, so that they don't affect delayed
1778      directory status-setting for ordinary directories.  */
1779   apply_delayed_links ();
1780
1781   /* Finally, fix the status of directories that are ancestors
1782      of delayed links.  */
1783   apply_nonancestor_delayed_set_stat ("", 1);
1784 }
1785
1786 bool
1787 rename_directory (char *src, char *dst)
1788 {
1789   if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1790     fixup_delayed_set_stat (src, dst);
1791   else
1792     {
1793       int e = errno;
1794       bool interdir_made;
1795
1796       switch (e)
1797         {
1798         case ENOENT:
1799           if (make_directories (dst, &interdir_made) == 0)
1800             {
1801               if (renameat (chdir_fd, src, chdir_fd, dst) == 0)
1802                 return true;
1803               e = errno;
1804             }
1805           break;
1806
1807         case EXDEV:
1808           /* FIXME: Fall back to recursive copying */
1809
1810         default:
1811           break;
1812         }
1813
1814       ERROR ((0, e, _("Cannot rename %s to %s"),
1815               quote_n (0, src),
1816               quote_n (1, dst)));
1817       return false;
1818     }
1819   return true;
1820 }