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