1/* Extract files from a tar archive.
2
3   Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4   2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6   Written by John Gilmore, on 1985-11-19.
7
8   This program is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by the
10   Free Software Foundation; either version 2, or (at your option) any later
11   version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
16   Public License for more details.
17
18   You should have received a copy of the GNU General Public License along
19   with this program; if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22#include <system.h>
23#include <quotearg.h>
24#include <utimens.h>
25#include <errno.h>
26#include <xgetcwd.h>
27
28#include "common.h"
29
30#ifdef __APPLE__
31#include <libgen.h>
32#include <sys/queue.h>
33#include <copyfile.h>
34struct copyfile_list_entry_t {
35    char *src;
36    char *dst;
37    char *tmp;
38    LIST_ENTRY(copyfile_list_entry_t) link;
39} *cle;
40extern LIST_HEAD(copyfile_list_t, copyfile_list_entry_t) copyfile_list;
41#endif
42
43#ifdef __APPLE__
44#include <sys/mount.h>
45#endif /* __APPLE__ */
46
47static bool we_are_root;	/* true if our effective uid == 0 */
48static mode_t newdir_umask;	/* umask when creating new directories */
49static mode_t current_umask;	/* current umask (which is set to 0 if -p) */
50
51/* Status of the permissions of a file that we are extracting.  */
52enum permstatus
53{
54  /* This file may have existed already; its permissions are unknown.  */
55  UNKNOWN_PERMSTATUS,
56
57  /* This file was created using the permissions from the archive,
58     except with S_IRWXG | S_IRWXO masked out if 0 < same_owner_option.  */
59  ARCHIVED_PERMSTATUS,
60
61  /* This is an intermediate directory; the archive did not specify
62     its permissions.  */
63  INTERDIR_PERMSTATUS
64};
65
66/* List of directories whose statuses we need to extract after we've
67   finished extracting their subsidiary files.  If you consider each
68   contiguous subsequence of elements of the form [D]?[^D]*, where [D]
69   represents an element where AFTER_LINKS is nonzero and [^D]
70   represents an element where AFTER_LINKS is zero, then the head
71   of the subsequence has the longest name, and each non-head element
72   in the prefix is an ancestor (in the directory hierarchy) of the
73   preceding element.  */
74
75struct delayed_set_stat
76  {
77    struct delayed_set_stat *next;
78    dev_t dev;
79    ino_t ino;
80    mode_t mode;
81    uid_t uid;
82    gid_t gid;
83    struct timespec atime;
84    struct timespec mtime;
85    size_t file_name_len;
86    mode_t invert_permissions;
87    enum permstatus permstatus;
88    bool after_links;
89    char file_name[1];
90  };
91
92static struct delayed_set_stat *delayed_set_stat_head;
93
94/* List of links whose creation we have delayed.  */
95struct delayed_link
96  {
97    /* The next delayed link in the list.  */
98    struct delayed_link *next;
99
100    /* The device, inode number and last-modified time of the placeholder.  */
101    dev_t dev;
102    ino_t ino;
103    struct timespec mtime;
104
105    /* True if the link is symbolic.  */
106    bool is_symlink;
107
108    /* The desired owner and group of the link, if it is a symlink.  */
109    uid_t uid;
110    gid_t gid;
111
112    /* A list of sources for this link.  The sources are all to be
113       hard-linked together.  */
114    struct string_list *sources;
115
116    /* The desired target of the desired link.  */
117    char target[1];
118  };
119
120static struct delayed_link *delayed_link_head;
121
122struct string_list
123  {
124    struct string_list *next;
125    char string[1];
126  };
127
128/*  Set up to extract files.  */
129void
130extr_init (void)
131{
132  we_are_root = geteuid () == 0;
133  same_permissions_option += we_are_root;
134  same_owner_option += we_are_root;
135
136  /* Option -p clears the kernel umask, so it does not affect proper
137     restoration of file permissions.  New intermediate directories will
138     comply with umask at start of program.  */
139
140  newdir_umask = umask (0);
141  if (0 < same_permissions_option)
142    current_umask = 0;
143  else
144    {
145      umask (newdir_umask);	/* restore the kernel umask */
146      current_umask = newdir_umask;
147    }
148}
149
150/* If restoring permissions, restore the mode for FILE_NAME from
151   information given in *STAT_INFO (where *CUR_INFO gives
152   the current status if CUR_INFO is nonzero); otherwise invert the
153   INVERT_PERMISSIONS bits from the file's current permissions.
154   PERMSTATUS specifies the status of the file's permissions.
155   TYPEFLAG specifies the type of the file.  */
156static void
157set_mode (char const *file_name,
158	  struct stat const *stat_info,
159	  struct stat const *cur_info,
160	  mode_t invert_permissions, enum permstatus permstatus,
161	  char typeflag)
162{
163  mode_t mode;
164
165  if (0 < same_permissions_option
166      && permstatus != INTERDIR_PERMSTATUS)
167    {
168      mode = stat_info->st_mode;
169
170      /* If we created the file and it has a mode that we set already
171	 with O_CREAT, then its mode is often set correctly already.
172	 But if we are changing ownership, the mode's group and and
173	 other permission bits were omitted originally, so it's less
174	 likely that the mode is OK now.  Also, on many hosts, some
175	 directories inherit the setgid bits from their parents, so we
176	 we must set directories' modes explicitly.  */
177      if ((permstatus == ARCHIVED_PERMSTATUS
178	   && ! (mode & ~ (0 < same_owner_option ? S_IRWXU : MODE_RWX)))
179	  && typeflag != DIRTYPE
180	  && typeflag != GNUTYPE_DUMPDIR)
181	return;
182    }
183  else if (! invert_permissions)
184    return;
185  else
186    {
187      /* We must inspect a directory's current permissions, since the
188	 directory may have inherited its setgid bit from its parent.
189
190	 INVERT_PERMISSIONS happens to be nonzero only for directories
191	 that we created, so there's no point optimizing this code for
192	 other cases.  */
193      struct stat st;
194      if (! cur_info)
195	{
196	  if (stat (file_name, &st) != 0)
197	    {
198	      stat_error (file_name);
199	      return;
200	    }
201	  cur_info = &st;
202	}
203      mode = cur_info->st_mode ^ invert_permissions;
204    }
205
206  if (chmod (file_name, mode) != 0)
207    chmod_error_details (file_name, mode);
208}
209
210/* Check time after successfully setting FILE_NAME's time stamp to T.  */
211static void
212check_time (char const *file_name, struct timespec t)
213{
214  if (t.tv_sec <= 0)
215    WARN ((0, 0, _("%s: implausibly old time stamp %s"),
216	   file_name, tartime (t, true)));
217  else if (timespec_cmp (volume_start_time, t) < 0)
218    {
219      struct timespec now;
220      gettime (&now);
221      if (timespec_cmp (now, t) < 0)
222	{
223	  char buf[TIMESPEC_STRSIZE_BOUND];
224	  struct timespec diff;
225	  diff.tv_sec = t.tv_sec - now.tv_sec;
226	  diff.tv_nsec = t.tv_nsec - now.tv_nsec;
227	  if (diff.tv_nsec < 0)
228	    {
229	      diff.tv_nsec += BILLION;
230	      diff.tv_sec--;
231	    }
232	  WARN ((0, 0, _("%s: time stamp %s is %s s in the future"),
233		 file_name, tartime (t, true), code_timespec (diff, buf)));
234	}
235    }
236}
237
238/* Restore stat attributes (owner, group, mode and times) for
239   FILE_NAME, using information given in *ST.
240   If CUR_INFO is nonzero, *CUR_INFO is the
241   file's current status.
242   If not restoring permissions, invert the
243   INVERT_PERMISSIONS bits from the file's current permissions.
244   PERMSTATUS specifies the status of the file's permissions.
245   TYPEFLAG specifies the type of the file.  */
246
247/* FIXME: About proper restoration of symbolic link attributes, we still do
248   not have it right.  Pretesters' reports tell us we need further study and
249   probably more configuration.  For now, just use lchown if it exists, and
250   punt for the rest.  Sigh!  */
251
252static void
253set_stat (char const *file_name,
254	  struct tar_stat_info const *st,
255	  struct stat const *cur_info,
256	  mode_t invert_permissions, enum permstatus permstatus,
257	  char typeflag)
258{
259  if (typeflag != SYMTYPE)
260    {
261      /* We do the utime before the chmod because some versions of utime are
262	 broken and trash the modes of the file.  */
263
264      if (! touch_option && permstatus != INTERDIR_PERMSTATUS)
265	{
266	  /* We set the accessed time to `now', which is really the time we
267	     started extracting files, unless incremental_option is used, in
268	     which case .st_atime is used.  */
269
270	  /* FIXME: incremental_option should set ctime too, but how?  */
271
272	  struct timespec ts[2];
273	  if (incremental_option)
274	    ts[0] = st->atime;
275	  else
276	    ts[0] = start_time;
277	  ts[1] = st->mtime;
278
279	  if (utimens (file_name, ts) != 0)
280	    utime_error (file_name);
281	  else
282	    {
283	      check_time (file_name, ts[0]);
284	      check_time (file_name, ts[1]);
285	    }
286	}
287
288      /* Some systems allow non-root users to give files away.  Once this
289	 done, it is not possible anymore to change file permissions.
290	 However, setting file permissions now would be incorrect, since
291	 they would apply to the wrong user, and there would be a race
292	 condition.  So, don't use systems that allow non-root users to
293	 give files away.  */
294    }
295
296  if (0 < same_owner_option && permstatus != INTERDIR_PERMSTATUS)
297    {
298      /* When lchown exists, it should be used to change the attributes of
299	 the symbolic link itself.  In this case, a mere chown would change
300	 the attributes of the file the symbolic link is pointing to, and
301	 should be avoided.  */
302      int chown_result = 1;
303
304      if (typeflag == SYMTYPE)
305	{
306#if HAVE_LCHOWN
307	  chown_result = lchown (file_name, st->stat.st_uid, st->stat.st_gid);
308#endif
309	}
310      else
311	{
312	  chown_result = chown (file_name, st->stat.st_uid, st->stat.st_gid);
313	}
314
315      if (chown_result == 0)
316	{
317	  /* Changing the owner can flip st_mode bits in some cases, so
318	     ignore cur_info if it might be obsolete now.  */
319	  if (cur_info
320	      && cur_info->st_mode & S_IXUGO
321	      && cur_info->st_mode & (S_ISUID | S_ISGID))
322	    cur_info = NULL;
323	}
324      else if (chown_result < 0)
325	chown_error_details (file_name,
326			     st->stat.st_uid, st->stat.st_gid);
327    }
328
329  if (typeflag != SYMTYPE)
330    set_mode (file_name, &st->stat, cur_info,
331	      invert_permissions, permstatus, typeflag);
332}
333
334/* Remember to restore stat attributes (owner, group, mode and times)
335   for the directory FILE_NAME, using information given in *ST,
336   once we stop extracting files into that directory.
337   If not restoring permissions, remember to invert the
338   INVERT_PERMISSIONS bits from the file's current permissions.
339   PERMSTATUS specifies the status of the file's permissions.
340
341   NOTICE: this works only if the archive has usual member order, i.e.
342   directory, then the files in that directory. Incremental archive have
343   somewhat reversed order: first go subdirectories, then all other
344   members. To help cope with this case the variable
345   delay_directory_restore_option is set by prepare_to_extract.
346
347   If an archive was explicitely created so that its member order is
348   reversed, some directory timestamps can be restored incorrectly,
349   e.g.:
350       tar --no-recursion -cf archive dir dir/file1 foo dir/file2
351*/
352static void
353delay_set_stat (char const *file_name, struct tar_stat_info const *st,
354		mode_t invert_permissions, enum permstatus permstatus)
355{
356  size_t file_name_len = strlen (file_name);
357  struct delayed_set_stat *data =
358    xmalloc (offsetof (struct delayed_set_stat, file_name)
359	     + file_name_len + 1);
360  data->next = delayed_set_stat_head;
361  data->dev = st->stat.st_dev;
362  data->ino = st->stat.st_ino;
363  data->mode = st->stat.st_mode;
364  data->uid = st->stat.st_uid;
365  data->gid = st->stat.st_gid;
366  data->atime = st->atime;
367  data->mtime = st->mtime;
368  data->file_name_len = file_name_len;
369  data->invert_permissions = invert_permissions;
370  data->permstatus = permstatus;
371  data->after_links = 0;
372  strcpy (data->file_name, file_name);
373  delayed_set_stat_head = data;
374}
375
376/* Update the delayed_set_stat info for an intermediate directory
377   created within the file name of DIR.  The intermediate directory turned
378   out to be the same as this directory, e.g. due to ".." or symbolic
379   links.  *DIR_STAT_INFO is the status of the directory.  */
380static void
381repair_delayed_set_stat (char const *dir,
382			 struct stat const *dir_stat_info)
383{
384  struct delayed_set_stat *data;
385  for (data = delayed_set_stat_head;  data;  data = data->next)
386    {
387      struct stat st;
388      if (stat (data->file_name, &st) != 0)
389	{
390	  stat_error (data->file_name);
391	  return;
392	}
393
394      if (st.st_dev == dir_stat_info->st_dev
395	  && st.st_ino == dir_stat_info->st_ino)
396	{
397	  data->dev = current_stat_info.stat.st_dev;
398	  data->ino = current_stat_info.stat.st_ino;
399	  data->mode = current_stat_info.stat.st_mode;
400	  data->uid = current_stat_info.stat.st_uid;
401	  data->gid = current_stat_info.stat.st_gid;
402	  data->atime = current_stat_info.atime;
403	  data->mtime = current_stat_info.mtime;
404	  data->invert_permissions =
405	    ((current_stat_info.stat.st_mode ^ st.st_mode)
406	     & MODE_RWX & ~ current_umask);
407	  data->permstatus = ARCHIVED_PERMSTATUS;
408	  return;
409	}
410    }
411
412  ERROR ((0, 0, _("%s: Unexpected inconsistency when making directory"),
413	  quotearg_colon (dir)));
414}
415
416#if HAVE_QUARANTINE
417void
418apply_qtn(int fd)
419{
420	int stat_ok;
421	struct stat sb;
422	int qstatus;
423
424	if (archive_qtn_file != NULL) {
425		stat_ok = (fstat(fd, &sb) == 0);
426
427		if (stat_ok) fchmod(fd, sb.st_mode | S_IWUSR);
428		qstatus = qtn_file_apply_to_fd(archive_qtn_file, fd);
429		if (stat_ok) fchmod(fd, sb.st_mode);
430
431		if (qstatus) {
432			warnx("qtn_file_apply_to_fd: %s", qtn_error(qstatus));
433		}
434	}
435}
436
437void
438apply_qtn_to_path(char *path)
439{
440	int stat_ok;
441	struct stat sb;
442	int qstatus;
443
444	if (archive_qtn_file != NULL) {
445		stat_ok = (stat(path, &sb) == 0);
446
447		if (stat_ok) chmod(path, sb.st_mode | S_IWUSR);
448		qstatus = qtn_file_apply_to_path(archive_qtn_file, path);
449		if (stat_ok) chmod(path, sb.st_mode);
450
451		if (qstatus) {
452			warnx("qtn_file_apply_to_path: %s", qtn_error(qstatus));
453		}
454	}
455}
456#else
457void apply_qtn(int fd) {}
458void apply_qtn_to_path(char *path) {}
459#endif
460
461/* After a file/link/directory creation has failed, see if
462   it's because some required directory was not present, and if so,
463   create all required directories.  Return non-zero if a directory
464   was created.  */
465static int
466make_directories (char *file_name)
467{
468  char *cursor0 = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
469  char *cursor;	        	/* points into the file name */
470  int did_something = 0;	/* did we do anything yet? */
471  int mode;
472  int invert_permissions;
473  int status;
474
475  for (cursor = cursor0; *cursor; cursor++)
476    {
477      if (! ISSLASH (*cursor))
478	continue;
479
480      /* Avoid mkdir of empty string, if leading or double '/'.  */
481
482      if (cursor == cursor0 || ISSLASH (cursor[-1]))
483	continue;
484
485      /* Avoid mkdir where last part of file name is "." or "..".  */
486
487      if (cursor[-1] == '.'
488	  && (cursor == cursor0 + 1 || ISSLASH (cursor[-2])
489	      || (cursor[-2] == '.'
490		  && (cursor == cursor0 + 2 || ISSLASH (cursor[-3])))))
491	continue;
492
493      *cursor = '\0';		/* truncate the name there */
494      mode = MODE_RWX & ~ newdir_umask;
495      invert_permissions = we_are_root ? 0 : MODE_WXUSR & ~ mode;
496      status = mkdir (file_name, mode ^ invert_permissions);
497
498      if (status == 0)
499	{
500	  apply_qtn_to_path(file_name);
501
502	  /* Create a struct delayed_set_stat even if
503	     invert_permissions is zero, because
504	     repair_delayed_set_stat may need to update the struct.  */
505	  delay_set_stat (file_name,
506			  &current_stat_info,
507			  invert_permissions, INTERDIR_PERMSTATUS);
508
509	  print_for_mkdir (file_name, cursor - file_name, mode);
510	  did_something = 1;
511
512	  *cursor = '/';
513	  continue;
514	}
515
516      *cursor = '/';
517
518      if (errno == EEXIST)
519	continue;	        /* Directory already exists.  */
520      else if ((errno == ENOSYS /* Automounted dirs on Solaris return
521				   this. Reported by Warren Hyde
522				   <Warren.Hyde@motorola.com> */
523	       || ERRNO_IS_EACCES)  /* Turbo C mkdir gives a funny errno.  */
524	       && access (file_name, W_OK) == 0)
525	continue;
526
527      /* Some other error in the mkdir.  We return to the caller.  */
528      break;
529    }
530
531  return did_something;		/* tell them to retry if we made one */
532}
533
534static bool
535file_newer_p (const char *file_name, struct tar_stat_info *tar_stat)
536{
537  struct stat st;
538
539  if (stat (file_name, &st))
540    {
541      stat_warn (file_name);
542      /* Be on the safe side: if the file does exist assume it is newer */
543      return errno != ENOENT;
544    }
545  if (!S_ISDIR (st.st_mode)
546      && tar_timespec_cmp (tar_stat->mtime, get_stat_mtime (&st)) <= 0)
547    {
548      return true;
549    }
550  return false;
551}
552
553/* Attempt repairing what went wrong with the extraction.  Delete an
554   already existing file or create missing intermediate directories.
555   Return nonzero if we somewhat increased our chances at a successful
556   extraction.  errno is properly restored on zero return.  */
557static int
558maybe_recoverable (char *file_name, int *interdir_made)
559{
560  int e = errno;
561
562  if (*interdir_made)
563    return 0;
564
565  switch (errno)
566    {
567    case EEXIST:
568      /* Remove an old file, if the options allow this.  */
569
570      switch (old_files_option)
571	{
572	case KEEP_OLD_FILES:
573	  return 0;
574
575	case KEEP_NEWER_FILES:
576	  if (file_newer_p (file_name, &current_stat_info))
577	    {
578	      errno = e;
579	      return 0;
580	    }
581	  /* FALL THROUGH */
582
583	case DEFAULT_OLD_FILES:
584	case NO_OVERWRITE_DIR_OLD_FILES:
585	case OVERWRITE_OLD_FILES:
586	  {
587	    int r = remove_any_file (file_name, ORDINARY_REMOVE_OPTION);
588	    errno = EEXIST;
589	    return r;
590	  }
591
592	case UNLINK_FIRST_OLD_FILES:
593	  break;
594	}
595
596    case ENOENT:
597      /* Attempt creating missing intermediate directories.  */
598      if (! make_directories (file_name))
599	{
600	  errno = ENOENT;
601	  return 0;
602	}
603      *interdir_made = 1;
604      return 1;
605
606    default:
607      /* Just say we can't do anything about it...  */
608
609      return 0;
610    }
611}
612
613/* Fix the statuses of all directories whose statuses need fixing, and
614   which are not ancestors of FILE_NAME.  If AFTER_LINKS is
615   nonzero, do this for all such directories; otherwise, stop at the
616   first directory that is marked to be fixed up only after delayed
617   links are applied.  */
618static void
619apply_nonancestor_delayed_set_stat (char const *file_name, bool after_links)
620{
621  size_t file_name_len = strlen (file_name);
622  bool check_for_renamed_directories = 0;
623
624  while (delayed_set_stat_head)
625    {
626      struct delayed_set_stat *data = delayed_set_stat_head;
627      bool skip_this_one = 0;
628      struct stat st;
629      struct stat const *cur_info = 0;
630
631      check_for_renamed_directories |= data->after_links;
632
633      if (after_links < data->after_links
634	  || (data->file_name_len < file_name_len
635	      && file_name[data->file_name_len]
636	      && (ISSLASH (file_name[data->file_name_len])
637		  || ISSLASH (file_name[data->file_name_len - 1]))
638	      && memcmp (file_name, data->file_name, data->file_name_len) == 0))
639	break;
640
641      if (check_for_renamed_directories)
642	{
643	  cur_info = &st;
644	  if (stat (data->file_name, &st) != 0)
645	    {
646	      stat_error (data->file_name);
647	      skip_this_one = 1;
648	    }
649	  else if (! (st.st_dev == data->dev && st.st_ino == data->ino))
650	    {
651	      ERROR ((0, 0,
652		      _("%s: Directory renamed before its status could be extracted"),
653		      quotearg_colon (data->file_name)));
654	      skip_this_one = 1;
655	    }
656	}
657
658      if (! skip_this_one)
659	{
660	  struct tar_stat_info st;
661	  st.stat.st_mode = data->mode;
662	  st.stat.st_uid = data->uid;
663	  st.stat.st_gid = data->gid;
664	  st.atime = data->atime;
665	  st.mtime = data->mtime;
666	  set_stat (data->file_name, &st, cur_info,
667		    data->invert_permissions, data->permstatus, DIRTYPE);
668	}
669
670      delayed_set_stat_head = data->next;
671      free (data);
672    }
673}
674
675
676
677/* Extractor functions for various member types */
678
679static int
680extract_dir (char *file_name, int typeflag)
681{
682  int status;
683  mode_t mode;
684  int interdir_made = 0;
685
686  /* Save 'root device' to avoid purging mount points. */
687  if (one_file_system_option && root_device == 0)
688    {
689      struct stat st;
690      char *dir = xgetcwd ();
691
692      if (deref_stat (true, dir, &st))
693	stat_diag (dir);
694      else
695	root_device = st.st_dev;
696      free (dir);
697    }
698
699  if (incremental_option)
700    /* Read the entry and delete files that aren't listed in the archive.  */
701    purge_directory (file_name);
702  else if (typeflag == GNUTYPE_DUMPDIR)
703    skip_member ();
704
705  mode = current_stat_info.stat.st_mode | (we_are_root ? 0 : MODE_WXUSR);
706  if (0 < same_owner_option || current_stat_info.stat.st_mode & ~ MODE_RWX)
707    mode &= S_IRWXU;
708
709  while ((status = mkdir (file_name, mode)))
710    {
711      if (errno == EEXIST
712	  && (interdir_made
713	      || old_files_option == DEFAULT_OLD_FILES
714	      || old_files_option == OVERWRITE_OLD_FILES))
715	{
716	  struct stat st;
717	  if (stat (file_name, &st) == 0)
718	    {
719	      if (interdir_made)
720		{
721		  repair_delayed_set_stat (file_name, &st);
722		  return 0;
723		}
724	      if (S_ISDIR (st.st_mode))
725		{
726		  mode = st.st_mode;
727		  break;
728		}
729	    }
730	  errno = EEXIST;
731	}
732
733      if (maybe_recoverable (file_name, &interdir_made))
734	continue;
735
736      if (errno != EEXIST)
737	{
738	  mkdir_error (file_name);
739	  return 1;
740	}
741      break;
742    }
743
744  if (status == 0
745      || old_files_option == DEFAULT_OLD_FILES
746      || old_files_option == OVERWRITE_OLD_FILES)
747    {
748      apply_qtn_to_path(file_name);
749      if (status == 0)
750	delay_set_stat (file_name, &current_stat_info,
751			((mode ^ current_stat_info.stat.st_mode)
752			 & MODE_RWX & ~ current_umask),
753			ARCHIVED_PERMSTATUS);
754      else /* For an already existing directory, invert_perms must be 0 */
755	delay_set_stat (file_name, &current_stat_info,
756			0,
757			UNKNOWN_PERMSTATUS);
758    }
759  return status;
760}
761
762
763static int
764open_output_file (char *file_name, int typeflag, mode_t mode)
765{
766  int fd;
767  int openflag = (O_WRONLY | O_BINARY | O_CREAT
768		  | (old_files_option == OVERWRITE_OLD_FILES
769		     ? O_TRUNC
770		     : O_EXCL));
771
772#if O_CTG
773  /* Contiguous files (on the Masscomp) have to specify the size in
774     the open call that creates them.  */
775
776  if (typeflag == CONTTYPE)
777    fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
778  else
779    fd = open (file_name, openflag, mode);
780
781#else /* not O_CTG */
782  if (typeflag == CONTTYPE)
783    {
784      static int conttype_diagnosed;
785
786#ifdef __APPLE__
787      /* XXX Would be nice to suppress this warning if we pre-allocate. */
788#endif /* __APPLE__ */
789
790      if (!conttype_diagnosed)
791	{
792	  conttype_diagnosed = 1;
793	  WARN ((0, 0, _("Extracting contiguous files as regular files")));
794	}
795    }
796  fd = open (file_name, openflag, mode);
797
798#endif /* not O_CTG */
799
800  return fd;
801}
802
803static int
804extract_file (char *file_name, int typeflag)
805{
806  int fd;
807  off_t size;
808  union block *data_block;
809  int status;
810  size_t count;
811  size_t written;
812  int interdir_made = 0;
813  mode_t mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
814  mode_t invert_permissions =
815    0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
816
817  /* FIXME: deal with protection issues.  */
818
819  if (to_stdout_option)
820    fd = STDOUT_FILENO;
821  else if (to_command_option)
822    {
823      fd = sys_exec_command (file_name, 'f', &current_stat_info);
824      if (fd < 0)
825	{
826	  skip_member ();
827	  return 0;
828	}
829    }
830  else
831    {
832      do
833	fd = open_output_file (file_name, typeflag, mode ^ invert_permissions);
834      while (fd < 0 && maybe_recoverable (file_name, &interdir_made));
835
836      if (fd < 0)
837	{
838	  skip_member ();
839	  open_error (file_name);
840	  return 1;
841	}
842    }
843
844#ifdef __APPLE__
845  /* Attempts to pre-allocate blocks for the destination file. */
846  if (!current_stat_info.is_sparse) {
847    fstore_t fst;
848
849    fst.fst_flags = 0;
850    fst.fst_posmode = F_PEOFPOSMODE;
851    fst.fst_offset = 0;
852    fst.fst_length = current_stat_info.stat.st_size;
853
854    (void)fcntl(fd, F_PREALLOCATE, &fst);
855  }
856#endif /* __APPLE__ */
857
858  apply_qtn(fd);
859  mv_begin (&current_stat_info);
860  if (current_stat_info.is_sparse)
861    sparse_extract_file (fd, &current_stat_info, &size);
862  else
863    for (size = current_stat_info.stat.st_size; size > 0; )
864      {
865	mv_size_left (size);
866
867	/* Locate data, determine max length writeable, write it,
868	   block that we have used the data, then check if the write
869	   worked.  */
870
871	data_block = find_next_block ();
872	if (! data_block)
873	  {
874	    ERROR ((0, 0, _("Unexpected EOF in archive")));
875	    break;		/* FIXME: What happens, then?  */
876	  }
877
878	written = available_space_after (data_block);
879
880	if (written > size)
881	  written = size;
882	errno = 0;
883	count = full_write (fd, data_block->buffer, written);
884	size -= written;
885
886	set_next_block_after ((union block *)
887			      (data_block->buffer + written - 1));
888	if (count != written)
889	  {
890	    if (!to_command_option)
891	      write_error_details (file_name, count, written);
892	    /* FIXME: shouldn't we restore from backup? */
893	    break;
894	  }
895      }
896
897  skip_file (size);
898
899  mv_end ();
900
901  /* If writing to stdout, don't try to do anything to the filename;
902     it doesn't exist, or we don't want to touch it anyway.  */
903
904  if (to_stdout_option)
905    return 0;
906
907  status = close (fd);
908  if (status < 0)
909    close_error (file_name);
910
911  if (to_command_option)
912    sys_wait_command ();
913  else
914    set_stat (file_name, &current_stat_info, NULL, invert_permissions,
915	      (old_files_option == OVERWRITE_OLD_FILES ?
916	       UNKNOWN_PERMSTATUS : ARCHIVED_PERMSTATUS),
917	      typeflag);
918
919  return status;
920}
921
922/* Create a placeholder file with name FILE_NAME, which will be
923   replaced after other extraction is done by a symbolic link if
924   IS_SYMLINK is true, and by a hard link otherwise.  Set
925   *INTERDIR_MADE if an intermediate directory is made in the
926   process.  */
927
928static int
929create_placeholder_file (char *file_name, bool is_symlink, int *interdir_made)
930{
931  int fd;
932  struct stat st;
933
934  while ((fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
935    if (! maybe_recoverable (file_name, interdir_made))
936      break;
937
938  if (fd < 0)
939    open_error (file_name);
940  else if (fstat (fd, &st) != 0)
941    {
942      stat_error (file_name);
943      close (fd);
944    }
945  else if (close (fd) != 0)
946    close_error (file_name);
947  else
948    {
949      struct delayed_set_stat *h;
950      struct delayed_link *p =
951	xmalloc (offsetof (struct delayed_link, target)
952		 + strlen (current_stat_info.link_name)
953		 + 1);
954      p->next = delayed_link_head;
955      delayed_link_head = p;
956      p->dev = st.st_dev;
957      p->ino = st.st_ino;
958      p->mtime = get_stat_mtime (&st);
959      p->is_symlink = is_symlink;
960      if (is_symlink)
961	{
962	  p->uid = current_stat_info.stat.st_uid;
963	  p->gid = current_stat_info.stat.st_gid;
964	}
965      p->sources = xmalloc (offsetof (struct string_list, string)
966			    + strlen (file_name) + 1);
967      p->sources->next = 0;
968      strcpy (p->sources->string, file_name);
969      strcpy (p->target, current_stat_info.link_name);
970
971      h = delayed_set_stat_head;
972      if (h && ! h->after_links
973	  && strncmp (file_name, h->file_name, h->file_name_len) == 0
974	  && ISSLASH (file_name[h->file_name_len])
975	  && (last_component (file_name) == file_name + h->file_name_len + 1))
976	{
977	  do
978	    {
979	      h->after_links = 1;
980
981	      if (stat (h->file_name, &st) != 0)
982		stat_error (h->file_name);
983	      else
984		{
985		  h->dev = st.st_dev;
986		  h->ino = st.st_ino;
987		}
988	    }
989	  while ((h = h->next) && ! h->after_links);
990	}
991
992      return 0;
993    }
994
995  return -1;
996}
997
998static int
999extract_link (char *file_name, int typeflag)
1000{
1001  int interdir_made = 0;
1002  char const *link_name;
1003
1004  transform_member_name (&current_stat_info.link_name, xform_link);
1005  link_name = current_stat_info.link_name;
1006
1007  if (! absolute_names_option && contains_dot_dot (link_name))
1008    return create_placeholder_file (file_name, false, &interdir_made);
1009
1010  do
1011    {
1012      struct stat st1, st2;
1013      int e;
1014      int status = link (link_name, file_name);
1015      e = errno;
1016
1017      if (status == 0)
1018	{
1019	  struct delayed_link *ds = delayed_link_head;
1020	  if (ds && lstat (link_name, &st1) == 0)
1021	    for (; ds; ds = ds->next)
1022	      if (ds->dev == st1.st_dev
1023		  && ds->ino == st1.st_ino
1024		  && timespec_cmp (ds->mtime, get_stat_mtime (&st1)) == 0)
1025		{
1026		  struct string_list *p =  xmalloc (offsetof (struct string_list, string)
1027						    + strlen (file_name) + 1);
1028		  strcpy (p->string, file_name);
1029		  p->next = ds->sources;
1030		  ds->sources = p;
1031		  break;
1032		}
1033	  return 0;
1034	}
1035      else if ((e == EEXIST && strcmp (link_name, file_name) == 0)
1036	       || (lstat (link_name, &st1) == 0
1037		   && lstat (file_name, &st2) == 0
1038		   && st1.st_dev == st2.st_dev
1039		   && st1.st_ino == st2.st_ino))
1040	return 0;
1041
1042      errno = e;
1043    }
1044  while (maybe_recoverable (file_name, &interdir_made));
1045
1046  if (!(incremental_option && errno == EEXIST))
1047    {
1048      link_error (link_name, file_name);
1049      return 1;
1050    }
1051  return 0;
1052}
1053
1054static int
1055extract_symlink (char *file_name, int typeflag)
1056{
1057#ifdef HAVE_SYMLINK
1058  int status;
1059  int interdir_made = 0;
1060
1061  transform_member_name (&current_stat_info.link_name, xform_symlink);
1062
1063  if (! absolute_names_option
1064      && (IS_ABSOLUTE_FILE_NAME (current_stat_info.link_name)
1065	  || contains_dot_dot (current_stat_info.link_name)))
1066    return create_placeholder_file (file_name, true, &interdir_made);
1067
1068  while ((status = symlink (current_stat_info.link_name, file_name)))
1069    if (!maybe_recoverable (file_name, &interdir_made))
1070      break;
1071
1072  if (status == 0)
1073    set_stat (file_name, &current_stat_info, NULL, 0, 0, SYMTYPE);
1074  else
1075    symlink_error (current_stat_info.link_name, file_name);
1076  return status;
1077
1078#else
1079  static int warned_once;
1080
1081  if (!warned_once)
1082    {
1083      warned_once = 1;
1084      WARN ((0, 0, _("Attempting extraction of symbolic links as hard links")));
1085    }
1086  return extract_link (file_name, typeflag);
1087#endif
1088}
1089
1090#if S_IFCHR || S_IFBLK
1091static int
1092extract_node (char *file_name, int typeflag)
1093{
1094  int status;
1095  int interdir_made = 0;
1096  mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
1097  mode_t invert_permissions =
1098    0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
1099
1100  do
1101    status = mknod (file_name, mode ^ invert_permissions,
1102		    current_stat_info.stat.st_rdev);
1103  while (status && maybe_recoverable (file_name, &interdir_made));
1104
1105  if (status != 0)
1106    mknod_error (file_name);
1107  else
1108    set_stat (file_name, &current_stat_info, NULL, invert_permissions,
1109	      ARCHIVED_PERMSTATUS, typeflag);
1110  return status;
1111}
1112#endif
1113
1114#if HAVE_MKFIFO || defined mkfifo
1115static int
1116extract_fifo (char *file_name, int typeflag)
1117{
1118  int status;
1119  int interdir_made = 0;
1120  mode_t mode = current_stat_info.stat.st_mode & ~ current_umask;
1121  mode_t invert_permissions =
1122    0 < same_owner_option ? mode & (S_IRWXG | S_IRWXO) : 0;
1123
1124  while ((status = mkfifo (file_name, mode)) != 0)
1125    if (!maybe_recoverable (file_name, &interdir_made))
1126      break;
1127
1128  if (status == 0)
1129    set_stat (file_name, &current_stat_info, NULL, invert_permissions,
1130	      ARCHIVED_PERMSTATUS, typeflag);
1131  else
1132    mkfifo_error (file_name);
1133  return status;
1134}
1135#endif
1136
1137static int
1138extract_volhdr (char *file_name, int typeflag)
1139{
1140  if (verbose_option)
1141    fprintf (stdlis, _("Reading %s\n"), quote (current_stat_info.file_name));
1142  skip_member ();
1143  return 0;
1144}
1145
1146static int
1147extract_failure (char *file_name, int typeflag)
1148{
1149  return 1;
1150}
1151
1152typedef int (*tar_extractor_t) (char *file_name, int typeflag);
1153
1154
1155
1156/* Prepare to extract a file. Find extractor function.
1157   Return zero if extraction should not proceed.  */
1158
1159static int
1160prepare_to_extract (char const *file_name, int typeflag, tar_extractor_t *fun)
1161{
1162  int rc = 1;
1163
1164  if (EXTRACT_OVER_PIPE)
1165    rc = 0;
1166
1167  /* Select the extractor */
1168  switch (typeflag)
1169    {
1170    case GNUTYPE_SPARSE:
1171      *fun = extract_file;
1172      rc = 1;
1173      break;
1174
1175    case AREGTYPE:
1176    case REGTYPE:
1177    case CONTTYPE:
1178      /* Appears to be a file.  But BSD tar uses the convention that a slash
1179	 suffix means a directory.  */
1180      if (current_stat_info.had_trailing_slash)
1181	*fun = extract_dir;
1182      else
1183	{
1184	  *fun = extract_file;
1185	  rc = 1;
1186	}
1187      break;
1188
1189    case SYMTYPE:
1190      *fun = extract_symlink;
1191      break;
1192
1193    case LNKTYPE:
1194      *fun = extract_link;
1195      break;
1196
1197#if S_IFCHR
1198    case CHRTYPE:
1199      current_stat_info.stat.st_mode |= S_IFCHR;
1200      *fun = extract_node;
1201      break;
1202#endif
1203
1204#if S_IFBLK
1205    case BLKTYPE:
1206      current_stat_info.stat.st_mode |= S_IFBLK;
1207      *fun = extract_node;
1208      break;
1209#endif
1210
1211#if HAVE_MKFIFO || defined mkfifo
1212    case FIFOTYPE:
1213      *fun = extract_fifo;
1214      break;
1215#endif
1216
1217    case DIRTYPE:
1218    case GNUTYPE_DUMPDIR:
1219      *fun = extract_dir;
1220      if (current_stat_info.is_dumpdir)
1221	delay_directory_restore_option = true;
1222      break;
1223
1224    case GNUTYPE_VOLHDR:
1225      *fun = extract_volhdr;
1226      break;
1227
1228    case GNUTYPE_MULTIVOL:
1229      ERROR ((0, 0,
1230	      _("%s: Cannot extract -- file is continued from another volume"),
1231	      quotearg_colon (current_stat_info.file_name)));
1232      *fun = extract_failure;
1233      break;
1234
1235    case GNUTYPE_LONGNAME:
1236    case GNUTYPE_LONGLINK:
1237      ERROR ((0, 0, _("Unexpected long name header")));
1238      *fun = extract_failure;
1239      break;
1240
1241    default:
1242      WARN ((0, 0,
1243	     _("%s: Unknown file type `%c', extracted as normal file"),
1244	     quotearg_colon (file_name), typeflag));
1245      *fun = extract_file;
1246    }
1247
1248  /* Determine whether the extraction should proceed */
1249  if (rc == 0)
1250    return 0;
1251
1252  switch (old_files_option)
1253    {
1254    case UNLINK_FIRST_OLD_FILES:
1255      if (!remove_any_file (file_name,
1256                            recursive_unlink_option ? RECURSIVE_REMOVE_OPTION
1257                                                      : ORDINARY_REMOVE_OPTION)
1258	  && errno && errno != ENOENT)
1259	{
1260	  unlink_error (file_name);
1261	  return 0;
1262	}
1263      break;
1264
1265    case KEEP_NEWER_FILES:
1266      if (file_newer_p (file_name, &current_stat_info))
1267	{
1268	  WARN ((0, 0, _("Current %s is newer or same age"),
1269		 quote (file_name)));
1270	  return 0;
1271	}
1272      break;
1273
1274    default:
1275      break;
1276    }
1277
1278  return 1;
1279}
1280
1281/* Extract a file from the archive.  */
1282void
1283extract_archive (void)
1284{
1285  char typeflag;
1286  tar_extractor_t fun;
1287#ifdef __APPLE__
1288  struct copyfile_list_entry_t *cle = NULL;
1289#endif
1290
1291  set_next_block_after (current_header);
1292  decode_header (current_header, &current_stat_info, &current_format, 1);
1293  if (!current_stat_info.file_name[0]
1294      || (interactive_option
1295	  && !confirm ("extract", current_stat_info.file_name)))
1296    {
1297      skip_member ();
1298      return;
1299    }
1300
1301  /* Print the block from current_header and current_stat.  */
1302  if (verbose_option)
1303    print_header (&current_stat_info, -1);
1304
1305  /* Restore stats for all non-ancestor directories, unless
1306     it is an incremental archive.
1307     (see NOTICE in the comment to delay_set_stat above) */
1308  if (!delay_directory_restore_option)
1309    apply_nonancestor_delayed_set_stat (current_stat_info.file_name, 0);
1310
1311  /* Take a safety backup of a previously existing file.  */
1312
1313  if (backup_option)
1314    if (!maybe_backup_file (current_stat_info.file_name, 0))
1315      {
1316	int e = errno;
1317	ERROR ((0, e, _("%s: Was unable to backup this file"),
1318		quotearg_colon (current_stat_info.file_name)));
1319	skip_member ();
1320	return;
1321      }
1322
1323  /* Extract the archive entry according to its type.  */
1324  /* KLUDGE */
1325  typeflag = sparse_member_p (&current_stat_info) ?
1326                  GNUTYPE_SPARSE : current_header->header.typeflag;
1327
1328  if (prepare_to_extract (current_stat_info.file_name, typeflag, &fun))
1329    {
1330#ifdef __APPLE__
1331		if (strncmp(basename(current_stat_info.file_name), "._", 2) == 0) {
1332			if ((cle = calloc(1, sizeof(struct copyfile_list_entry_t))) == NULL)
1333				goto err;
1334			if ((cle->src = strdup(current_stat_info.file_name)) == NULL)
1335				goto err;
1336			if (asprintf(&cle->tmp, "%s.XXX", current_stat_info.file_name) == -1)
1337				goto err;
1338			if (mktemp(cle->tmp) == NULL)
1339				goto err;
1340			if (asprintf(&cle->dst, "%s/%s", dirname(current_stat_info.file_name), basename(current_stat_info.file_name) + 2) != -1)
1341				LIST_INSERT_HEAD(&copyfile_list, cle, link);
1342			else {
1343err:
1344				if (cle->src) free(cle->src);
1345				if (cle->dst) free(cle->dst);
1346				if (cle->tmp) free(cle->tmp);
1347				if (cle) {
1348					free(cle);
1349					cle = NULL;
1350				}
1351			}
1352		}
1353#endif
1354      if (fun && (*fun) (cle ? cle->tmp : current_stat_info.file_name, typeflag)
1355	  && backup_option)
1356	undo_last_backup ();
1357    }
1358  else
1359    skip_member ();
1360
1361}
1362
1363/* Extract the symbolic links whose final extraction were delayed.  */
1364static void
1365apply_delayed_links (void)
1366{
1367  struct delayed_link *ds;
1368
1369  for (ds = delayed_link_head; ds; )
1370    {
1371      struct string_list *sources = ds->sources;
1372      char const *valid_source = 0;
1373
1374      for (sources = ds->sources; sources; sources = sources->next)
1375	{
1376	  char const *source = sources->string;
1377	  struct stat st;
1378
1379	  /* Make sure the placeholder file is still there.  If not,
1380	     don't create a link, as the placeholder was probably
1381	     removed by a later extraction.  */
1382	  if (lstat (source, &st) == 0
1383	      && st.st_dev == ds->dev
1384	      && st.st_ino == ds->ino
1385	      && timespec_cmp (get_stat_mtime (&st), ds->mtime) == 0)
1386	    {
1387	      /* Unlink the placeholder, then create a hard link if possible,
1388		 a symbolic link otherwise.  */
1389	      if (unlink (source) != 0)
1390		unlink_error (source);
1391	      else if (valid_source && link (valid_source, source) == 0)
1392		;
1393	      else if (!ds->is_symlink)
1394		{
1395		  if (link (ds->target, source) != 0)
1396		    link_error (ds->target, source);
1397		}
1398	      else if (symlink (ds->target, source) != 0)
1399		symlink_error (ds->target, source);
1400	      else
1401		{
1402		  struct tar_stat_info st1;
1403		  st1.stat.st_uid = ds->uid;
1404		  st1.stat.st_gid = ds->gid;
1405		  set_stat (source, &st1, NULL, 0, 0, SYMTYPE);
1406		  valid_source = source;
1407		}
1408	    }
1409	}
1410
1411      for (sources = ds->sources; sources; )
1412	{
1413	  struct string_list *next = sources->next;
1414	  free (sources);
1415	  sources = next;
1416	}
1417
1418      {
1419	struct delayed_link *next = ds->next;
1420	free (ds);
1421	ds = next;
1422      }
1423    }
1424
1425  delayed_link_head = 0;
1426}
1427
1428/* Finish the extraction of an archive.  */
1429void
1430extract_finish (void)
1431{
1432  /* First, fix the status of ordinary directories that need fixing.  */
1433  apply_nonancestor_delayed_set_stat ("", 0);
1434
1435  /* Then, apply delayed links, so that they don't affect delayed
1436     directory status-setting for ordinary directories.  */
1437  apply_delayed_links ();
1438
1439  /* Finally, fix the status of directories that are ancestors
1440     of delayed links.  */
1441  apply_nonancestor_delayed_set_stat ("", 1);
1442}
1443
1444bool
1445rename_directory (char *src, char *dst)
1446{
1447  if (rename (src, dst))
1448    {
1449      int e = errno;
1450
1451      switch (e)
1452	{
1453	case ENOENT:
1454	  if (make_directories (dst))
1455	    {
1456	      if (rename (src, dst) == 0)
1457		return true;
1458	      e = errno;
1459	    }
1460	  break;
1461
1462	case EXDEV:
1463	  /* FIXME: Fall back to recursive copying */
1464
1465	default:
1466	  break;
1467	}
1468
1469      ERROR ((0, e, _("Cannot rename %s to %s"),
1470	      quote_n (0, src),
1471	      quote_n (1, dst)));
1472      return false;
1473    }
1474  return true;
1475}
1476
1477void
1478fatal_exit (void)
1479{
1480  extract_finish ();
1481  error (TAREXIT_FAILURE, 0, _("Error is not recoverable: exiting now"));
1482  abort ();
1483}
1484
1485void
1486xalloc_die (void)
1487{
1488  error (0, 0, "%s", _("memory exhausted"));
1489  fatal_exit ();
1490}
1491