(root)/
tar-1.35/
src/
misc.c
       1  /* Miscellaneous functions, not really specific to GNU tar.
       2  
       3     Copyright 1988-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software; you can redistribute it and/or modify it
       6     under the terms of the GNU General Public License as published by the
       7     Free Software Foundation; either version 3, or (at your option) any later
       8     version.
       9  
      10     This program is distributed in the hope that it will be useful, but
      11     WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
      13     Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License along
      16     with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      17  
      18  #define COMMON_INLINE _GL_EXTERN_INLINE
      19  #include <system.h>
      20  #include <rmt.h>
      21  #include "common.h"
      22  #include <quotearg.h>
      23  #include <xgetcwd.h>
      24  #include <unlinkdir.h>
      25  #include <utimens.h>
      26  
      27  #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
      28  # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
      29  #endif
      30  
      31  static void namebuf_add_dir (namebuf_t, char const *);
      32  static char *namebuf_finish (namebuf_t);
      33  static const char *tar_getcdpath (int);
      34  
      35  char const *
      36  quote_n_colon (int n, char const *arg)
      37  {
      38    return quotearg_n_style_colon (n, get_quoting_style (NULL), arg);
      39  }
      40  
      41  /* Handling strings.  */
      42  
      43  /* Assign STRING to a copy of VALUE if not zero, or to zero.  If
      44     STRING was nonzero, it is freed first.  */
      45  void
      46  assign_string_or_null (char **string, const char *value)
      47  {
      48    if (value)
      49      assign_string (string, value);
      50    else
      51      assign_null (string);
      52  }
      53  
      54  void
      55  assign_string (char **string, const char *value)
      56  {
      57    free (*string);
      58    *string = xstrdup (value);
      59  }
      60  
      61  void
      62  assign_null (char **string)
      63  {
      64    char *old = *string;
      65    *string = NULL;
      66    free (old);
      67  }
      68  
      69  void
      70  assign_string_n (char **string, const char *value, size_t n)
      71  {
      72    free (*string);
      73    if (value)
      74      {
      75        size_t l = strnlen (value, n);
      76        char *p = xmalloc (l + 1);
      77        memcpy (p, value, l);
      78        p[l] = 0;
      79        *string = p;
      80      }
      81    else
      82      *string = NULL;
      83  }
      84  
      85  #if 0
      86  /* This function is currently unused; perhaps it should be removed?  */
      87  
      88  /* Allocate a copy of the string quoted as in C, and returns that.  If
      89     the string does not have to be quoted, it returns a null pointer.
      90     The allocated copy should normally be freed with free() after the
      91     caller is done with it.
      92  
      93     This is used in one context only: generating the directory file in
      94     incremental dumps.  The quoted string is not intended for human
      95     consumption; it is intended only for unquote_string.  The quoting
      96     is locale-independent, so that users needn't worry about locale
      97     when reading directory files.  This means that we can't use
      98     quotearg, as quotearg is locale-dependent and is meant for human
      99     consumption.  */
     100  static char *
     101  quote_copy_string (const char *string)
     102  {
     103    const char *source = string;
     104    char *destination = 0;
     105    char *buffer = 0;
     106    int copying = 0;
     107  
     108    while (*source)
     109      {
     110        int character = *source++;
     111  
     112        switch (character)
     113  	{
     114  	case '\n': case '\\':
     115  	  if (!copying)
     116  	    {
     117  	      size_t length = (source - string) - 1;
     118  
     119  	      copying = 1;
     120  	      buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
     121  	      memcpy (buffer, string, length);
     122  	      destination = buffer + length;
     123  	    }
     124  	  *destination++ = '\\';
     125  	  *destination++ = character == '\\' ? '\\' : 'n';
     126  	  break;
     127  
     128  	default:
     129  	  if (copying)
     130  	    *destination++ = character;
     131  	  break;
     132  	}
     133      }
     134    if (copying)
     135      {
     136        *destination = '\0';
     137        return buffer;
     138      }
     139    return 0;
     140  }
     141  #endif
     142  
     143  /* Takes a quoted C string (like those produced by quote_copy_string)
     144     and turns it back into the un-quoted original.  This is done in
     145     place.  Returns 0 only if the string was not properly quoted, but
     146     completes the unquoting anyway.
     147  
     148     This is used for reading the saved directory file in incremental
     149     dumps.  It is used for decoding old 'N' records (demangling names).
     150     But also, it is used for decoding file arguments, would they come
     151     from the shell or a -T file, and for decoding the --exclude
     152     argument.  */
     153  int
     154  unquote_string (char *string)
     155  {
     156    int result = 1;
     157    char *source = string;
     158    char *destination = string;
     159  
     160    /* Escape sequences other than \\ and \n are no longer generated by
     161       quote_copy_string, but accept them for backwards compatibility,
     162       and also because unquote_string is used for purposes other than
     163       parsing the output of quote_copy_string.  */
     164  
     165    while (*source)
     166      if (*source == '\\')
     167        switch (*++source)
     168  	{
     169  	case '\\':
     170  	  *destination++ = '\\';
     171  	  source++;
     172  	  break;
     173  
     174  	case 'a':
     175  	  *destination++ = '\a';
     176  	  source++;
     177  	  break;
     178  
     179  	case 'b':
     180  	  *destination++ = '\b';
     181  	  source++;
     182  	  break;
     183  
     184  	case 'f':
     185  	  *destination++ = '\f';
     186  	  source++;
     187  	  break;
     188  
     189  	case 'n':
     190  	  *destination++ = '\n';
     191  	  source++;
     192  	  break;
     193  
     194  	case 'r':
     195  	  *destination++ = '\r';
     196  	  source++;
     197  	  break;
     198  
     199  	case 't':
     200  	  *destination++ = '\t';
     201  	  source++;
     202  	  break;
     203  
     204  	case 'v':
     205  	  *destination++ = '\v';
     206  	  source++;
     207  	  break;
     208  
     209  	case '?':
     210  	  *destination++ = 0177;
     211  	  source++;
     212  	  break;
     213  
     214  	case '0':
     215  	case '1':
     216  	case '2':
     217  	case '3':
     218  	case '4':
     219  	case '5':
     220  	case '6':
     221  	case '7':
     222  	  {
     223  	    int value = *source++ - '0';
     224  
     225  	    if (*source < '0' || *source > '7')
     226  	      {
     227  		*destination++ = value;
     228  		break;
     229  	      }
     230  	    value = value * 8 + *source++ - '0';
     231  	    if (*source < '0' || *source > '7')
     232  	      {
     233  		*destination++ = value;
     234  		break;
     235  	      }
     236  	    value = value * 8 + *source++ - '0';
     237  	    *destination++ = value;
     238  	    break;
     239  	  }
     240  
     241  	default:
     242  	  result = 0;
     243  	  *destination++ = '\\';
     244  	  if (*source)
     245  	    *destination++ = *source++;
     246  	  break;
     247  	}
     248      else if (source != destination)
     249        *destination++ = *source++;
     250      else
     251        source++, destination++;
     252  
     253    if (source != destination)
     254      *destination = '\0';
     255    return result;
     256  }
     257  
     258  /* Zap trailing slashes.  */
     259  char *
     260  zap_slashes (char *name)
     261  {
     262    char *q;
     263  
     264    if (!name || *name == 0)
     265      return name;
     266    q = name + strlen (name) - 1;
     267    while (q > name && ISSLASH (*q))
     268      *q-- = '\0';
     269    return name;
     270  }
     271  
     272  /* Normalize FILE_NAME by removing redundant slashes and "."
     273     components, including redundant trailing slashes.
     274     Leave ".." alone, as it may be significant in the presence
     275     of symlinks and on platforms where "/.." != "/".
     276  
     277     Destructive version: modifies its argument. */
     278  void
     279  normalize_filename_x (char *file_name)
     280  {
     281    char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
     282    char *p;
     283    char const *q;
     284    char c;
     285  
     286    /* Don't squeeze leading "//" to "/", on hosts where they're distinct.  */
     287    name += (DOUBLE_SLASH_IS_DISTINCT_ROOT
     288  	   && ISSLASH (*name) && ISSLASH (name[1]) && ! ISSLASH (name[2]));
     289  
     290    /* Omit redundant leading "." components.  */
     291    for (q = p = name; (*p = *q) == '.' && ISSLASH (q[1]); p += !*q)
     292      for (q += 2; ISSLASH (*q); q++)
     293        continue;
     294  
     295    /* Copy components from Q to P, omitting redundant slashes and
     296       internal "."  components.  */
     297    while ((*p++ = c = *q++) != '\0')
     298      if (ISSLASH (c))
     299        while (ISSLASH (q[*q == '.']))
     300  	q += (*q == '.') + 1;
     301  
     302    /* Omit redundant trailing "." component and slash.  */
     303    if (2 < p - name)
     304      {
     305        p -= p[-2] == '.' && ISSLASH (p[-3]);
     306        p -= 2 < p - name && ISSLASH (p[-2]);
     307        p[-1] = '\0';
     308      }
     309  }
     310  
     311  /* Normalize NAME by removing redundant slashes and "." components,
     312     including redundant trailing slashes.
     313  
     314     Return a normalized newly-allocated copy.  */
     315  
     316  char *
     317  normalize_filename (int cdidx, const char *name)
     318  {
     319    char *copy = NULL;
     320  
     321    if (IS_RELATIVE_FILE_NAME (name))
     322      {
     323        /* Set COPY to the absolute path for this name.
     324  
     325           FIXME: There should be no need to get the absolute file name.
     326           tar_getcdpath does not return a true "canonical" path, so
     327           this following approach may lead to situations where the same
     328           file or directory is processed twice under different absolute
     329           paths without that duplication being detected.  Perhaps we
     330           should use dev+ino pairs instead of names?  (See listed03.at for
     331           a related test case.) */
     332        const char *cdpath = tar_getcdpath (cdidx);
     333        size_t copylen;
     334        bool need_separator;
     335  
     336        copylen = strlen (cdpath);
     337        need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
     338  			  && copylen == 2 && ISSLASH (cdpath[1]));
     339        copy = xmalloc (copylen + need_separator + strlen (name) + 1);
     340        strcpy (copy, cdpath);
     341        copy[copylen] = DIRECTORY_SEPARATOR;
     342        strcpy (copy + copylen + need_separator, name);
     343      }
     344  
     345    if (!copy)
     346      copy = xstrdup (name);
     347    normalize_filename_x (copy);
     348    return copy;
     349  }
     350  
     351  
     352  void
     353  replace_prefix (char **pname, const char *samp, size_t slen,
     354  		const char *repl, size_t rlen)
     355  {
     356    char *name = *pname;
     357    size_t nlen = strlen (name);
     358    if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
     359      {
     360        if (rlen > slen)
     361  	{
     362  	  name = xrealloc (name, nlen - slen + rlen + 1);
     363  	  *pname = name;
     364  	}
     365        memmove (name + rlen, name + slen, nlen - slen + 1);
     366        memcpy (name, repl, rlen);
     367      }
     368  }
     369  
     370  
     371  /* Handling numbers.  */
     372  
     373  /* Convert VALUE, which is converted from a system integer type whose
     374     minimum value is MINVAL and maximum MINVAL, to an decimal
     375     integer string.  Use the storage in BUF and return a pointer to the
     376     converted string.  If VALUE is converted from a negative integer in
     377     the range MINVAL .. -1, represent it with a string representation
     378     of the negative integer, using leading '-'.  */
     379  #if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
     380  # error "sysinttostr: uintmax_t cannot represent all intmax_t values"
     381  #endif
     382  char *
     383  sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
     384  	     char buf[SYSINT_BUFSIZE])
     385  {
     386    if (value <= maxval)
     387      return umaxtostr (value, buf);
     388    else
     389      {
     390        intmax_t i = value - minval;
     391        return imaxtostr (i + minval, buf);
     392      }
     393  }
     394  
     395  /* Convert a prefix of the string ARG to a system integer type whose
     396     minimum value is MINVAL and maximum MAXVAL.  If MINVAL is negative,
     397     negative integers MINVAL .. -1 are assumed to be represented using
     398     leading '-' in the usual way.  If the represented value exceeds
     399     INTMAX_MAX, return a negative integer V such that (uintmax_t) V
     400     yields the represented value.  If ARGLIM is nonnull, store into
     401     *ARGLIM a pointer to the first character after the prefix.
     402  
     403     This is the inverse of sysinttostr.
     404  
     405     On a normal return, set errno = 0.
     406     On conversion error, return 0 and set errno = EINVAL.
     407     On overflow, return an extreme value and set errno = ERANGE.  */
     408  #if ! (INTMAX_MAX <= UINTMAX_MAX)
     409  # error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
     410  #endif
     411  intmax_t
     412  strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
     413  {
     414    errno = 0;
     415    if (maxval <= INTMAX_MAX)
     416      {
     417        if (ISDIGIT (arg[*arg == '-']))
     418  	{
     419  	  intmax_t i = strtoimax (arg, arglim, 10);
     420  	  intmax_t imaxval = maxval;
     421  	  if (minval <= i && i <= imaxval)
     422  	    return i;
     423  	  errno = ERANGE;
     424  	  return i < minval ? minval : maxval;
     425  	}
     426      }
     427    else
     428      {
     429        if (ISDIGIT (*arg))
     430  	{
     431  	  uintmax_t i = strtoumax (arg, arglim, 10);
     432  	  if (i <= maxval)
     433  	    return represent_uintmax (i);
     434  	  errno = ERANGE;
     435  	  return maxval;
     436  	}
     437      }
     438  
     439    errno = EINVAL;
     440    return 0;
     441  }
     442  
     443  /* Output fraction and trailing digits appropriate for a nanoseconds
     444     count equal to NS, but don't output unnecessary '.' or trailing
     445     zeros.  */
     446  
     447  void
     448  code_ns_fraction (int ns, char *p)
     449  {
     450    if (ns == 0)
     451      *p = '\0';
     452    else
     453      {
     454        int i = 9;
     455        *p++ = '.';
     456  
     457        while (ns % 10 == 0)
     458  	{
     459  	  ns /= 10;
     460  	  i--;
     461  	}
     462  
     463        p[i] = '\0';
     464  
     465        for (;;)
     466  	{
     467  	  p[--i] = '0' + ns % 10;
     468  	  if (i == 0)
     469  	    break;
     470  	  ns /= 10;
     471  	}
     472      }
     473  }
     474  
     475  char const *
     476  code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
     477  {
     478    time_t s = t.tv_sec;
     479    int ns = t.tv_nsec;
     480    char *np;
     481    bool negative = s < 0;
     482  
     483    /* ignore invalid values of ns */
     484    if (BILLION <= ns || ns < 0)
     485      ns = 0;
     486  
     487    if (negative && ns != 0)
     488      {
     489        s++;
     490        ns = BILLION - ns;
     491      }
     492  
     493    np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
     494    if (negative)
     495      *--np = '-';
     496    code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
     497    return np;
     498  }
     499  
     500  struct timespec
     501  decode_timespec (char const *arg, char **arg_lim, bool parse_fraction)
     502  {
     503    time_t s = TYPE_MINIMUM (time_t);
     504    int ns = -1;
     505    char const *p = arg;
     506    bool negative = *arg == '-';
     507    struct timespec r;
     508  
     509    if (! ISDIGIT (arg[negative]))
     510      errno = EINVAL;
     511    else
     512      {
     513        errno = 0;
     514  
     515        if (negative)
     516  	{
     517  	  intmax_t i = strtoimax (arg, arg_lim, 10);
     518  	  if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i : 0 <= i)
     519  	    s = i;
     520  	  else
     521  	    errno = ERANGE;
     522  	}
     523        else
     524  	{
     525  	  uintmax_t i = strtoumax (arg, arg_lim, 10);
     526  	  if (i <= TYPE_MAXIMUM (time_t))
     527  	    s = i;
     528  	  else
     529  	    errno = ERANGE;
     530  	}
     531  
     532        p = *arg_lim;
     533        ns = 0;
     534  
     535        if (parse_fraction && *p == '.')
     536  	{
     537  	  int digits = 0;
     538  	  bool trailing_nonzero = false;
     539  
     540  	  while (ISDIGIT (*++p))
     541  	    if (digits < LOG10_BILLION)
     542  	      digits++, ns = 10 * ns + (*p - '0');
     543  	    else
     544  	      trailing_nonzero |= *p != '0';
     545  
     546  	  while (digits < LOG10_BILLION)
     547  	    digits++, ns *= 10;
     548  
     549  	  if (negative)
     550  	    {
     551  	      /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
     552  		 I.e., truncate time stamps towards minus infinity while
     553  		 converting them to internal form.  */
     554  	      ns += trailing_nonzero;
     555  	      if (ns != 0)
     556  		{
     557  		  if (s == TYPE_MINIMUM (time_t))
     558  		    ns = -1;
     559  		  else
     560  		    {
     561  		      s--;
     562  		      ns = BILLION - ns;
     563  		    }
     564  		}
     565  	    }
     566  	}
     567  
     568        if (errno == ERANGE)
     569  	ns = -1;
     570      }
     571  
     572    *arg_lim = (char *) p;
     573    r.tv_sec = s;
     574    r.tv_nsec = ns;
     575    return r;
     576  }
     577  
     578  /* File handling.  */
     579  
     580  /* Saved names in case backup needs to be undone.  */
     581  static char *before_backup_name;
     582  static char *after_backup_name;
     583  
     584  /* Return 1 if FILE_NAME is obviously "." or "/".  */
     585  bool
     586  must_be_dot_or_slash (char const *file_name)
     587  {
     588    file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
     589  
     590    if (ISSLASH (file_name[0]))
     591      {
     592        for (;;)
     593  	if (ISSLASH (file_name[1]))
     594  	  file_name++;
     595  	else if (file_name[1] == '.'
     596                   && ISSLASH (file_name[2 + (file_name[2] == '.')]))
     597  	  file_name += 2 + (file_name[2] == '.');
     598  	else
     599  	  return ! file_name[1];
     600      }
     601    else
     602      {
     603        while (file_name[0] == '.' && ISSLASH (file_name[1]))
     604  	{
     605  	  file_name += 2;
     606  	  while (ISSLASH (*file_name))
     607  	    file_name++;
     608  	}
     609  
     610        return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
     611      }
     612  }
     613  
     614  /* Some implementations of rmdir let you remove '.' or '/'.
     615     Report an error with errno set to zero for obvious cases of this;
     616     otherwise call rmdir.  */
     617  static int
     618  safer_rmdir (const char *file_name)
     619  {
     620    if (must_be_dot_or_slash (file_name))
     621      {
     622        errno = 0;
     623        return -1;
     624      }
     625  
     626    if (unlinkat (chdir_fd, file_name, AT_REMOVEDIR) == 0)
     627      {
     628        remove_delayed_set_stat (file_name);
     629        return 0;
     630      }
     631    return -1;
     632  }
     633  
     634  /* Remove FILE_NAME, returning 1 on success.  If FILE_NAME is a directory,
     635     then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
     636     recursively; otherwise, remove it only if it is empty.  If FILE_NAME is
     637     a directory that cannot be removed (e.g., because it is nonempty)
     638     and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
     639     Return 0 on error, with errno set; if FILE_NAME is obviously the working
     640     directory return zero with errno set to zero.  */
     641  int
     642  remove_any_file (const char *file_name, enum remove_option option)
     643  {
     644    /* Try unlink first if we cannot unlink directories, as this saves
     645       us a system call in the common case where we're removing a
     646       non-directory.  */
     647    bool try_unlink_first = cannot_unlink_dir ();
     648  
     649    if (try_unlink_first)
     650      {
     651        if (unlinkat (chdir_fd, file_name, 0) == 0)
     652  	return 1;
     653  
     654        /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
     655  	 directory without appropriate privileges, but many Linux
     656  	 kernels return the more-sensible EISDIR.  */
     657        if (errno != EPERM && errno != EISDIR)
     658  	return 0;
     659      }
     660  
     661    if (safer_rmdir (file_name) == 0)
     662      return 1;
     663  
     664    switch (errno)
     665      {
     666      case ENOTDIR:
     667        return !try_unlink_first && unlinkat (chdir_fd, file_name, 0) == 0;
     668  
     669      case 0:
     670      case EEXIST:
     671  #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
     672      case ENOTEMPTY:
     673  #endif
     674        switch (option)
     675  	{
     676  	case ORDINARY_REMOVE_OPTION:
     677  	  break;
     678  
     679  	case WANT_DIRECTORY_REMOVE_OPTION:
     680  	  return -1;
     681  
     682  	case RECURSIVE_REMOVE_OPTION:
     683  	  {
     684  	    char *directory = tar_savedir (file_name, 0);
     685  	    char const *entry;
     686  	    size_t entrylen;
     687  
     688  	    if (! directory)
     689  	      return 0;
     690  
     691  	    for (entry = directory;
     692  		 (entrylen = strlen (entry)) != 0;
     693  		 entry += entrylen + 1)
     694  	      {
     695  		char *file_name_buffer = make_file_name (file_name, entry);
     696  		int r = remove_any_file (file_name_buffer,
     697                                           RECURSIVE_REMOVE_OPTION);
     698  		free (file_name_buffer);
     699  
     700  		if (! r)
     701  		  {
     702  		    free (directory);
     703  		    return 0;
     704  		  }
     705  	      }
     706  
     707  	    free (directory);
     708  	    return safer_rmdir (file_name) == 0;
     709  	  }
     710  	}
     711        break;
     712      }
     713  
     714    return 0;
     715  }
     716  
     717  /* Check if FILE_NAME already exists and make a backup of it right now.
     718     Return success (nonzero) only if the backup is either unneeded, or
     719     successful.  For now, directories are considered to never need
     720     backup.  If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
     721     so, we do not have to backup block or character devices, nor remote
     722     entities.  */
     723  bool
     724  maybe_backup_file (const char *file_name, bool this_is_the_archive)
     725  {
     726    struct stat file_stat;
     727  
     728    assign_string (&before_backup_name, file_name);
     729  
     730    /* A run situation may exist between Emacs or other GNU programs trying to
     731       make a backup for the same file simultaneously.  If theoretically
     732       possible, real problems are unlikely.  Doing any better would require a
     733       convention, GNU-wide, for all programs doing backups.  */
     734  
     735    assign_null (&after_backup_name);
     736  
     737    /* Check if we really need to backup the file.  */
     738  
     739    if (this_is_the_archive && _remdev (file_name))
     740      return true;
     741  
     742    if (deref_stat (file_name, &file_stat) != 0)
     743      {
     744        if (errno == ENOENT)
     745  	return true;
     746  
     747        stat_error (file_name);
     748        return false;
     749      }
     750  
     751    if (S_ISDIR (file_stat.st_mode))
     752      return true;
     753  
     754    if (this_is_the_archive
     755        && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
     756      return true;
     757  
     758    after_backup_name = find_backup_file_name (chdir_fd, file_name, backup_type);
     759    if (! after_backup_name)
     760      xalloc_die ();
     761  
     762    if (renameat (chdir_fd, before_backup_name, chdir_fd, after_backup_name)
     763        == 0)
     764      {
     765        if (verbose_option)
     766  	fprintf (stdlis, _("Renaming %s to %s\n"),
     767  		 quote_n (0, before_backup_name),
     768  		 quote_n (1, after_backup_name));
     769        return true;
     770      }
     771    else
     772      {
     773        /* The backup operation failed.  */
     774        int e = errno;
     775        ERROR ((0, e, _("%s: Cannot rename to %s"),
     776  	      quotearg_colon (before_backup_name),
     777  	      quote_n (1, after_backup_name)));
     778        assign_null (&after_backup_name);
     779        return false;
     780      }
     781  }
     782  
     783  /* Try to restore the recently backed up file to its original name.
     784     This is usually only needed after a failed extraction.  */
     785  void
     786  undo_last_backup (void)
     787  {
     788    if (after_backup_name)
     789      {
     790        if (renameat (chdir_fd, after_backup_name, chdir_fd, before_backup_name)
     791  	  != 0)
     792  	{
     793  	  int e = errno;
     794  	  ERROR ((0, e, _("%s: Cannot rename to %s"),
     795  		  quotearg_colon (after_backup_name),
     796  		  quote_n (1, before_backup_name)));
     797  	}
     798        if (verbose_option)
     799  	fprintf (stdlis, _("Renaming %s back to %s\n"),
     800  		 quote_n (0, after_backup_name),
     801  		 quote_n (1, before_backup_name));
     802        assign_null (&after_backup_name);
     803      }
     804  }
     805  
     806  /* Apply either stat or lstat to (NAME, BUF), depending on the
     807     presence of the --dereference option.  NAME is relative to the
     808     most-recent argument to chdir_do.  */
     809  int
     810  deref_stat (char const *name, struct stat *buf)
     811  {
     812    return fstatat (chdir_fd, name, buf, fstatat_flags);
     813  }
     814  
     815  /* Read from FD into the buffer BUF with COUNT bytes.  Attempt to fill
     816     BUF.  Wait until input is available; this matters because files are
     817     opened O_NONBLOCK for security reasons, and on some file systems
     818     this can cause read to fail with errno == EAGAIN.  Return the
     819     actual number of bytes read, zero for EOF, or
     820     SAFE_READ_ERROR upon error.  */
     821  size_t
     822  blocking_read (int fd, void *buf, size_t count)
     823  {
     824    size_t bytes = full_read (fd, buf, count);
     825  
     826  #if defined F_SETFL && O_NONBLOCK
     827    if (bytes == SAFE_READ_ERROR && errno == EAGAIN)
     828      {
     829        int flags = fcntl (fd, F_GETFL);
     830        if (0 <= flags && flags & O_NONBLOCK
     831  	  && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
     832  	bytes = full_read (fd, buf, count);
     833      }
     834  #endif
     835  
     836    if (bytes == 0 && errno != 0)
     837      bytes = SAFE_READ_ERROR;
     838    return bytes;
     839  }
     840  
     841  /* Write to FD from the buffer BUF with COUNT bytes.  Do a full write.
     842     Wait until an output buffer is available; this matters because
     843     files are opened O_NONBLOCK for security reasons, and on some file
     844     systems this can cause write to fail with errno == EAGAIN.  Return
     845     the actual number of bytes written, setting errno if that is less
     846     than COUNT.  */
     847  size_t
     848  blocking_write (int fd, void const *buf, size_t count)
     849  {
     850    size_t bytes = full_write (fd, buf, count);
     851  
     852  #if defined F_SETFL && O_NONBLOCK
     853    if (bytes < count && errno == EAGAIN)
     854      {
     855        int flags = fcntl (fd, F_GETFL);
     856        if (0 <= flags && flags & O_NONBLOCK
     857  	  && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
     858  	{
     859  	  char const *buffer = buf;
     860  	  bytes += full_write (fd, buffer + bytes, count - bytes);
     861  	}
     862      }
     863  #endif
     864  
     865    return bytes;
     866  }
     867  
     868  /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
     869     access time to ATIME.  */
     870  int
     871  set_file_atime (int fd, int parentfd, char const *file, struct timespec atime)
     872  {
     873    struct timespec ts[2];
     874    ts[0] = atime;
     875    ts[1].tv_nsec = UTIME_OMIT;
     876    return fdutimensat (fd, parentfd, file, ts, fstatat_flags);
     877  }
     878  
     879  /* A description of a working directory.  */
     880  struct wd
     881  {
     882    /* The directory's name.  */
     883    char const *name;
     884    /* "Absolute" path representing this directory; in the contrast to
     885       the real absolute pathname, it can contain /../ components (see
     886       normalize_filename_x for the reason of it).  It is NULL if the
     887       absolute path could not be determined.  */
     888    char *abspath;
     889    /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
     890       the working directory.  If zero, the directory needs to be opened
     891       to be used.  */
     892    int fd;
     893  };
     894  
     895  /* A vector of chdir targets.  wd[0] is the initial working directory.  */
     896  static struct wd *wd;
     897  
     898  /* The number of working directories in the vector.  */
     899  static size_t wd_count;
     900  
     901  /* The allocated size of the vector.  */
     902  static size_t wd_alloc;
     903  
     904  /* The maximum number of chdir targets with open directories.
     905     Don't make it too large, as many operating systems have a small
     906     limit on the number of open file descriptors.  Also, the current
     907     implementation does not scale well.  */
     908  enum { CHDIR_CACHE_SIZE = 16 };
     909  
     910  /* Indexes into WD of chdir targets with open file descriptors, sorted
     911     most-recently used first.  Zero indexes are unused.  */
     912  static int wdcache[CHDIR_CACHE_SIZE];
     913  
     914  /* Number of nonzero entries in WDCACHE.  */
     915  static size_t wdcache_count;
     916  
     917  int
     918  chdir_count (void)
     919  {
     920    if (wd_count == 0)
     921      return wd_count;
     922    return wd_count - 1;
     923  }
     924  
     925  /* DIR is the operand of a -C option; add it to vector of chdir targets,
     926     and return the index of its location.  */
     927  int
     928  chdir_arg (char const *dir)
     929  {
     930    if (wd_count == wd_alloc)
     931      {
     932        if (wd_alloc == 0)
     933  	wd_alloc = 2;
     934        wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
     935  
     936        if (! wd_count)
     937  	{
     938  	  wd[wd_count].name = ".";
     939  	  wd[wd_count].abspath = NULL;
     940  	  wd[wd_count].fd = AT_FDCWD;
     941  	  wd_count++;
     942  	}
     943      }
     944  
     945    /* Optimize the common special case of the working directory,
     946       or the working directory as a prefix.  */
     947    if (dir[0])
     948      {
     949        while (dir[0] == '.' && ISSLASH (dir[1]))
     950  	for (dir += 2;  ISSLASH (*dir);  dir++)
     951  	  continue;
     952        if (! dir[dir[0] == '.'])
     953  	return wd_count - 1;
     954      }
     955  
     956    wd[wd_count].name = dir;
     957    wd[wd_count].abspath = NULL;
     958    wd[wd_count].fd = 0;
     959    return wd_count++;
     960  }
     961  
     962  /* Index of current directory.  */
     963  int chdir_current;
     964  
     965  /* Value suitable for use as the first argument to openat, and in
     966     similar locations for fstatat, etc.  This is an open file
     967     descriptor, or AT_FDCWD if the working directory is current.  It is
     968     valid until the next invocation of chdir_do.  */
     969  int chdir_fd = AT_FDCWD;
     970  
     971  /* Change to directory I, in a virtual way.  This does not actually
     972     invoke chdir; it merely sets chdir_fd to an int suitable as the
     973     first argument for openat, etc.  If I is 0, change to the initial
     974     working directory; otherwise, I must be a value returned by
     975     chdir_arg.  */
     976  void
     977  chdir_do (int i)
     978  {
     979    if (chdir_current != i)
     980      {
     981        struct wd *curr = &wd[i];
     982        int fd = curr->fd;
     983  
     984        if (! fd)
     985  	{
     986  	  if (! IS_ABSOLUTE_FILE_NAME (curr->name))
     987  	    chdir_do (i - 1);
     988  	  fd = openat (chdir_fd, curr->name,
     989  		       open_searchdir_flags & ~ O_NOFOLLOW);
     990  	  if (fd < 0)
     991  	    open_fatal (curr->name);
     992  
     993  	  curr->fd = fd;
     994  
     995  	  /* Add I to the cache, tossing out the lowest-ranking entry if the
     996  	     cache is full.  */
     997  	  if (wdcache_count < CHDIR_CACHE_SIZE)
     998  	    wdcache[wdcache_count++] = i;
     999  	  else
    1000  	    {
    1001  	      struct wd *stale = &wd[wdcache[CHDIR_CACHE_SIZE - 1]];
    1002  	      if (close (stale->fd) != 0)
    1003  		close_diag (stale->name);
    1004  	      stale->fd = 0;
    1005  	      wdcache[CHDIR_CACHE_SIZE - 1] = i;
    1006  	    }
    1007  	}
    1008  
    1009        if (0 < fd)
    1010  	{
    1011  	  /* Move the i value to the front of the cache.  This is
    1012  	     O(CHDIR_CACHE_SIZE), but the cache is small.  */
    1013  	  size_t ci;
    1014  	  int prev = wdcache[0];
    1015  	  for (ci = 1; prev != i; ci++)
    1016  	    {
    1017  	      int cur = wdcache[ci];
    1018  	      wdcache[ci] = prev;
    1019  	      if (cur == i)
    1020  		break;
    1021  	      prev = cur;
    1022  	    }
    1023  	  wdcache[0] = i;
    1024  	}
    1025  
    1026        chdir_current = i;
    1027        chdir_fd = fd;
    1028      }
    1029  }
    1030  
    1031  const char *
    1032  tar_dirname (void)
    1033  {
    1034    return wd[chdir_current].name;
    1035  }
    1036  
    1037  /* Return the absolute path that represents the working
    1038     directory referenced by IDX.
    1039  
    1040     If wd is empty, then there were no -C options given, and
    1041     chdir_args() has never been called, so we simply return the
    1042     process's actual cwd.  (Note that in this case IDX is ignored,
    1043     since it should always be 0.) */
    1044  static const char *
    1045  tar_getcdpath (int idx)
    1046  {
    1047    if (!wd)
    1048      {
    1049        static char *cwd;
    1050        if (!cwd)
    1051  	{
    1052  	  cwd = xgetcwd ();
    1053  	  if (!cwd)
    1054  	    call_arg_fatal ("getcwd", ".");
    1055  	}
    1056        return cwd;
    1057      }
    1058  
    1059    if (!wd[idx].abspath)
    1060      {
    1061        int i;
    1062        int save_cwdi = chdir_current;
    1063  
    1064        for (i = idx; i >= 0; i--)
    1065  	if (wd[i].abspath)
    1066  	  break;
    1067  
    1068        while (++i <= idx)
    1069  	{
    1070  	  chdir_do (i);
    1071  	  if (i == 0)
    1072  	    {
    1073  	      if ((wd[i].abspath = xgetcwd ()) == NULL)
    1074  		call_arg_fatal ("getcwd", ".");
    1075  	    }
    1076  	  else if (IS_ABSOLUTE_FILE_NAME (wd[i].name))
    1077  	    /* If the given name is absolute, use it to represent this
    1078  	       directory; otherwise, construct a name based on the
    1079  	       previous -C option.  */
    1080  	    wd[i].abspath = xstrdup (wd[i].name);
    1081  	  else
    1082  	    {
    1083  	      namebuf_t nbuf = namebuf_create (wd[i - 1].abspath);
    1084  	      namebuf_add_dir (nbuf, wd[i].name);
    1085  	      wd[i].abspath = namebuf_finish (nbuf);
    1086  	    }
    1087  	}
    1088  
    1089        chdir_do (save_cwdi);
    1090      }
    1091  
    1092    return wd[idx].abspath;
    1093  }
    1094  
    1095  void
    1096  close_diag (char const *name)
    1097  {
    1098    if (ignore_failed_read_option)
    1099      {
    1100        if (WARNING_ENABLED(WARN_FAILED_READ))
    1101  	close_warn (name);
    1102      }
    1103    else
    1104      close_error (name);
    1105  }
    1106  
    1107  void
    1108  open_diag (char const *name)
    1109  {
    1110    if (ignore_failed_read_option)
    1111      {
    1112        if (WARNING_ENABLED(WARN_FAILED_READ))
    1113  	open_warn (name);
    1114      }
    1115    else
    1116      open_error (name);
    1117  }
    1118  
    1119  void
    1120  read_diag_details (char const *name, off_t offset, size_t size)
    1121  {
    1122    if (ignore_failed_read_option)
    1123      {
    1124        if (WARNING_ENABLED(WARN_FAILED_READ))
    1125  	read_warn_details (name, offset, size);
    1126      }
    1127    else
    1128      read_error_details (name, offset, size);
    1129  }
    1130  
    1131  void
    1132  readlink_diag (char const *name)
    1133  {
    1134    if (ignore_failed_read_option)
    1135      {
    1136        if (WARNING_ENABLED(WARN_FAILED_READ))
    1137  	readlink_warn (name);
    1138      }
    1139    else
    1140      readlink_error (name);
    1141  }
    1142  
    1143  void
    1144  savedir_diag (char const *name)
    1145  {
    1146    if (ignore_failed_read_option)
    1147      {
    1148        if (WARNING_ENABLED(WARN_FAILED_READ))
    1149  	savedir_warn (name);
    1150      }
    1151    else
    1152      savedir_error (name);
    1153  }
    1154  
    1155  void
    1156  seek_diag_details (char const *name, off_t offset)
    1157  {
    1158    if (ignore_failed_read_option)
    1159      {
    1160        if (WARNING_ENABLED(WARN_FAILED_READ))
    1161  	seek_warn_details (name, offset);
    1162      }
    1163    else
    1164      seek_error_details (name, offset);
    1165  }
    1166  
    1167  void
    1168  stat_diag (char const *name)
    1169  {
    1170    if (ignore_failed_read_option)
    1171      {
    1172        if (WARNING_ENABLED(WARN_FAILED_READ))
    1173  	stat_warn (name);
    1174      }
    1175    else
    1176      stat_error (name);
    1177  }
    1178  
    1179  void
    1180  file_removed_diag (const char *name, bool top_level,
    1181  		   void (*diagfn) (char const *name))
    1182  {
    1183    if (!top_level && errno == ENOENT)
    1184      {
    1185        WARNOPT (WARN_FILE_REMOVED,
    1186  	       (0, 0, _("%s: File removed before we read it"),
    1187  		quotearg_colon (name)));
    1188        set_exit_status (TAREXIT_DIFFERS);
    1189      }
    1190    else
    1191      diagfn (name);
    1192  }
    1193  
    1194  /* Fork, aborting if unsuccessful.  */
    1195  pid_t
    1196  xfork (void)
    1197  {
    1198    pid_t p = fork ();
    1199    if (p == (pid_t) -1)
    1200      call_arg_fatal ("fork", _("child process"));
    1201    return p;
    1202  }
    1203  
    1204  /* Create a pipe, aborting if unsuccessful.  */
    1205  void
    1206  xpipe (int fd[2])
    1207  {
    1208    if (pipe (fd) < 0)
    1209      call_arg_fatal ("pipe", _("interprocess channel"));
    1210  }
    1211  
    1212  /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
    1213     ALIGNMENT must be nonzero.  The caller must arrange for ((char *)
    1214     PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
    1215     locations.  */
    1216  
    1217  static void *
    1218  ptr_align (void *ptr, size_t alignment)
    1219  {
    1220    char *p0 = ptr;
    1221    char *p1 = p0 + alignment - 1;
    1222    return p1 - (size_t) p1 % alignment;
    1223  }
    1224  
    1225  /* Return the address of a page-aligned buffer of at least SIZE bytes.
    1226     The caller should free *PTR when done with the buffer.  */
    1227  
    1228  void *
    1229  page_aligned_alloc (void **ptr, size_t size)
    1230  {
    1231    size_t alignment = getpagesize ();
    1232    size_t size1 = size + alignment;
    1233    if (size1 < size)
    1234      xalloc_die ();
    1235    *ptr = xmalloc (size1);
    1236    return ptr_align (*ptr, alignment);
    1237  }
    1238  
    1239  
    1240  
    1241  struct namebuf
    1242  {
    1243    char *buffer;		/* directory, '/', and directory member */
    1244    size_t buffer_size;	/* allocated size of name_buffer */
    1245    size_t dir_length;	/* length of directory part in buffer */
    1246  };
    1247  
    1248  namebuf_t
    1249  namebuf_create (const char *dir)
    1250  {
    1251    namebuf_t buf = xmalloc (sizeof (*buf));
    1252    buf->buffer_size = strlen (dir) + 2;
    1253    buf->buffer = xmalloc (buf->buffer_size);
    1254    strcpy (buf->buffer, dir);
    1255    buf->dir_length = strlen (buf->buffer);
    1256    if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
    1257      buf->buffer[buf->dir_length++] = DIRECTORY_SEPARATOR;
    1258    return buf;
    1259  }
    1260  
    1261  void
    1262  namebuf_free (namebuf_t buf)
    1263  {
    1264    free (buf->buffer);
    1265    free (buf);
    1266  }
    1267  
    1268  char *
    1269  namebuf_name (namebuf_t buf, const char *name)
    1270  {
    1271    size_t len = strlen (name);
    1272    while (buf->dir_length + len + 1 >= buf->buffer_size)
    1273      buf->buffer = x2realloc (buf->buffer, &buf->buffer_size);
    1274    strcpy (buf->buffer + buf->dir_length, name);
    1275    return buf->buffer;
    1276  }
    1277  
    1278  static void
    1279  namebuf_add_dir (namebuf_t buf, const char *name)
    1280  {
    1281    static char dirsep[] = { DIRECTORY_SEPARATOR, 0 };
    1282    if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
    1283      {
    1284        namebuf_name (buf, dirsep);
    1285        buf->dir_length++;
    1286      }
    1287    namebuf_name (buf, name);
    1288    buf->dir_length += strlen (name);
    1289  }
    1290  
    1291  static char *
    1292  namebuf_finish (namebuf_t buf)
    1293  {
    1294    char *res = buf->buffer;
    1295  
    1296    if (ISSLASH (buf->buffer[buf->dir_length - 1]))
    1297      buf->buffer[buf->dir_length] = 0;
    1298    free (buf);
    1299    return res;
    1300  }
    1301  
    1302  /* Return the filenames in directory NAME, relative to the chdir_fd.
    1303     If the directory does not exist, report error if MUST_EXIST is
    1304     true.
    1305  
    1306     Return NULL on errors.
    1307  */
    1308  char *
    1309  tar_savedir (const char *name, int must_exist)
    1310  {
    1311    char *ret = NULL;
    1312    DIR *dir = NULL;
    1313    int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY);
    1314    if (fd < 0)
    1315      {
    1316        if (!must_exist && errno == ENOENT)
    1317  	return NULL;
    1318        open_error (name);
    1319      }
    1320    else if (! ((dir = fdopendir (fd))
    1321  	      && (ret = streamsavedir (dir, savedir_sort_order))))
    1322      savedir_error (name);
    1323  
    1324    if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0)
    1325      savedir_error (name);
    1326  
    1327    return ret;
    1328  }