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