Various formatting fixes
[tar.git] / src / misc.c
blob2c5dec9a9926b5304ce331a439a1c64e1a708349
1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright 1988-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #define COMMON_INLINE _GL_EXTERN_INLINE
19 #include <system.h>
20 #include <rmt.h>
21 #include "common.h"
22 #include <c-ctype.h>
23 #include <quotearg.h>
24 #include <xgetcwd.h>
25 #include <unlinkdir.h>
26 #include <utimens.h>
28 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
29 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
30 #endif
32 static void namebuf_add_dir (namebuf_t, char const *);
33 static char *namebuf_finish (namebuf_t);
34 static const char *tar_getcdpath (int);
36 char const *
37 quote_n_colon (int n, char const *arg)
39 return quotearg_n_style_colon (n, get_quoting_style (NULL), arg);
42 /* Handling strings. */
44 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
45 STRING was nonzero, it is freed first. */
46 void
47 assign_string_or_null (char **string, const char *value)
49 if (value)
50 assign_string (string, value);
51 else
52 assign_null (string);
55 void
56 assign_string (char **string, const char *value)
58 free (*string);
59 *string = xstrdup (value);
62 void
63 assign_null (char **string)
65 char *old = *string;
66 *string = NULL;
67 free (old);
70 void
71 assign_string_n (char **string, const char *value, size_t n)
73 free (*string);
74 if (value)
76 size_t l = strnlen (value, n);
77 char *p = xmalloc (l + 1);
78 memcpy (p, value, l);
79 p[l] = 0;
80 *string = p;
82 else
83 *string = NULL;
86 #if 0
87 /* This function is currently unused; perhaps it should be removed? */
89 /* Allocate a copy of the string quoted as in C, and returns that. If
90 the string does not have to be quoted, it returns a null pointer.
91 The allocated copy should normally be freed with free() after the
92 caller is done with it.
94 This is used in one context only: generating the directory file in
95 incremental dumps. The quoted string is not intended for human
96 consumption; it is intended only for unquote_string. The quoting
97 is locale-independent, so that users needn't worry about locale
98 when reading directory files. This means that we can't use
99 quotearg, as quotearg is locale-dependent and is meant for human
100 consumption. */
101 static char *
102 quote_copy_string (const char *string)
104 const char *source = string;
105 char *destination = 0;
106 char *buffer = 0;
107 int copying = 0;
109 while (*source)
111 int character = *source++;
113 switch (character)
115 case '\n': case '\\':
116 if (!copying)
118 size_t length = (source - string) - 1;
120 copying = 1;
121 buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
122 memcpy (buffer, string, length);
123 destination = buffer + length;
125 *destination++ = '\\';
126 *destination++ = character == '\\' ? '\\' : 'n';
127 break;
129 default:
130 if (copying)
131 *destination++ = character;
132 break;
135 if (copying)
137 *destination = '\0';
138 return buffer;
140 return 0;
142 #endif
144 /* Takes a quoted C string (like those produced by quote_copy_string)
145 and turns it back into the un-quoted original. This is done in
146 place. Returns 0 only if the string was not properly quoted, but
147 completes the unquoting anyway.
149 This is used for reading the saved directory file in incremental
150 dumps. It is used for decoding old 'N' records (demangling names).
151 But also, it is used for decoding file arguments, would they come
152 from the shell or a -T file, and for decoding the --exclude
153 argument. */
155 unquote_string (char *string)
157 int result = 1;
158 char *source = string;
159 char *destination = string;
161 /* Escape sequences other than \\ and \n are no longer generated by
162 quote_copy_string, but accept them for backwards compatibility,
163 and also because unquote_string is used for purposes other than
164 parsing the output of quote_copy_string. */
166 while (*source)
167 if (*source == '\\')
168 switch (*++source)
170 case '\\':
171 *destination++ = '\\';
172 source++;
173 break;
175 case 'a':
176 *destination++ = '\a';
177 source++;
178 break;
180 case 'b':
181 *destination++ = '\b';
182 source++;
183 break;
185 case 'f':
186 *destination++ = '\f';
187 source++;
188 break;
190 case 'n':
191 *destination++ = '\n';
192 source++;
193 break;
195 case 'r':
196 *destination++ = '\r';
197 source++;
198 break;
200 case 't':
201 *destination++ = '\t';
202 source++;
203 break;
205 case 'v':
206 *destination++ = '\v';
207 source++;
208 break;
210 case '?':
211 *destination++ = 0177;
212 source++;
213 break;
215 case '0':
216 case '1':
217 case '2':
218 case '3':
219 case '4':
220 case '5':
221 case '6':
222 case '7':
224 int value = *source++ - '0';
226 if (*source < '0' || *source > '7')
228 *destination++ = value;
229 break;
231 value = value * 8 + *source++ - '0';
232 if (*source < '0' || *source > '7')
234 *destination++ = value;
235 break;
237 value = value * 8 + *source++ - '0';
238 *destination++ = value;
239 break;
242 default:
243 result = 0;
244 *destination++ = '\\';
245 if (*source)
246 *destination++ = *source++;
247 break;
249 else if (source != destination)
250 *destination++ = *source++;
251 else
252 source++, destination++;
254 if (source != destination)
255 *destination = '\0';
256 return result;
259 /* Zap trailing slashes. */
260 char *
261 zap_slashes (char *name)
263 char *q;
265 if (!name || *name == 0)
266 return name;
267 q = name + strlen (name) - 1;
268 while (q > name && ISSLASH (*q))
269 *q-- = '\0';
270 return name;
273 /* Normalize FILE_NAME by removing redundant slashes and "."
274 components, including redundant trailing slashes.
275 Leave ".." alone, as it may be significant in the presence
276 of symlinks and on platforms where "/.." != "/".
278 Destructive version: modifies its argument. */
279 void
280 normalize_filename_x (char *file_name)
282 char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
283 char *p;
284 char const *q;
285 char c;
287 /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
288 name += (DOUBLE_SLASH_IS_DISTINCT_ROOT
289 && ISSLASH (*name) && ISSLASH (name[1]) && ! ISSLASH (name[2]));
291 /* Omit redundant leading "." components. */
292 for (q = p = name; (*p = *q) == '.' && ISSLASH (q[1]); p += !*q)
293 for (q += 2; ISSLASH (*q); q++)
294 continue;
296 /* Copy components from Q to P, omitting redundant slashes and
297 internal "." components. */
298 while ((*p++ = c = *q++) != '\0')
299 if (ISSLASH (c))
300 while (ISSLASH (q[*q == '.']))
301 q += (*q == '.') + 1;
303 /* Omit redundant trailing "." component and slash. */
304 if (2 < p - name)
306 p -= p[-2] == '.' && ISSLASH (p[-3]);
307 p -= 2 < p - name && ISSLASH (p[-2]);
308 p[-1] = '\0';
312 /* Normalize NAME by removing redundant slashes and "." components,
313 including redundant trailing slashes.
315 Return a normalized newly-allocated copy. */
317 char *
318 normalize_filename (int cdidx, const char *name)
320 char *copy = NULL;
322 if (IS_RELATIVE_FILE_NAME (name))
324 /* Set COPY to the absolute path for this name.
326 FIXME: There should be no need to get the absolute file name.
327 tar_getcdpath does not return a true "canonical" path, so
328 this following approach may lead to situations where the same
329 file or directory is processed twice under different absolute
330 paths without that duplication being detected. Perhaps we
331 should use dev+ino pairs instead of names? (See listed03.at for
332 a related test case.) */
333 const char *cdpath = tar_getcdpath (cdidx);
334 size_t copylen;
335 bool need_separator;
337 copylen = strlen (cdpath);
338 need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
339 && copylen == 2 && ISSLASH (cdpath[1]));
340 copy = xmalloc (copylen + need_separator + strlen (name) + 1);
341 strcpy (copy, cdpath);
342 copy[copylen] = DIRECTORY_SEPARATOR;
343 strcpy (copy + copylen + need_separator, name);
346 if (!copy)
347 copy = xstrdup (name);
348 normalize_filename_x (copy);
349 return copy;
353 void
354 replace_prefix (char **pname, const char *samp, size_t slen,
355 const char *repl, size_t rlen)
357 char *name = *pname;
358 size_t nlen = strlen (name);
359 if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
361 if (rlen > slen)
363 name = xrealloc (name, nlen - slen + rlen + 1);
364 *pname = name;
366 memmove (name + rlen, name + slen, nlen - slen + 1);
367 memcpy (name, repl, rlen);
372 /* Handling numbers. */
374 /* Convert VALUE, which is converted from a system integer type whose
375 minimum value is MINVAL and maximum MINVAL, to an decimal
376 integer string. Use the storage in BUF and return a pointer to the
377 converted string. If VALUE is converted from a negative integer in
378 the range MINVAL .. -1, represent it with a string representation
379 of the negative integer, using leading '-'. */
380 #if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
381 # error "sysinttostr: uintmax_t cannot represent all intmax_t values"
382 #endif
383 char *
384 sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
385 char buf[SYSINT_BUFSIZE])
387 if (value <= maxval)
388 return umaxtostr (value, buf);
389 else
391 intmax_t i = value - minval;
392 return imaxtostr (i + minval, buf);
396 /* Convert a prefix of the string ARG to a system integer type whose
397 minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
398 negative integers MINVAL .. -1 are assumed to be represented using
399 leading '-' in the usual way. If the represented value exceeds
400 INTMAX_MAX, return a negative integer V such that (uintmax_t) V
401 yields the represented value. If ARGLIM is nonnull, store into
402 *ARGLIM a pointer to the first character after the prefix.
404 This is the inverse of sysinttostr.
406 On a normal return, set errno = 0.
407 On conversion error, return 0 and set errno = EINVAL.
408 On overflow, return an extreme value and set errno = ERANGE. */
409 #if ! (INTMAX_MAX <= UINTMAX_MAX)
410 # error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
411 #endif
412 intmax_t
413 strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
415 errno = 0;
416 if (maxval <= INTMAX_MAX)
418 if (c_isdigit (arg[*arg == '-']))
420 intmax_t i = strtoimax (arg, arglim, 10);
421 intmax_t imaxval = maxval;
422 if (minval <= i && i <= imaxval)
423 return i;
424 errno = ERANGE;
425 return i < minval ? minval : maxval;
428 else
430 if (c_isdigit (*arg))
432 uintmax_t i = strtoumax (arg, arglim, 10);
433 if (i <= maxval)
434 return represent_uintmax (i);
435 errno = ERANGE;
436 return maxval;
440 errno = EINVAL;
441 return 0;
444 /* Output fraction and trailing digits appropriate for a nanoseconds
445 count equal to NS, but don't output unnecessary '.' or trailing
446 zeros. */
448 void
449 code_ns_fraction (int ns, char *p)
451 if (ns == 0)
452 *p = '\0';
453 else
455 int i = 9;
456 *p++ = '.';
458 while (ns % 10 == 0)
460 ns /= 10;
461 i--;
464 p[i] = '\0';
466 for (;;)
468 p[--i] = '0' + ns % 10;
469 if (i == 0)
470 break;
471 ns /= 10;
476 char const *
477 code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
479 time_t s = t.tv_sec;
480 int ns = t.tv_nsec;
481 char *np;
482 bool negative = s < 0;
484 /* ignore invalid values of ns */
485 if (BILLION <= ns || ns < 0)
486 ns = 0;
488 if (negative && ns != 0)
490 s++;
491 ns = BILLION - ns;
494 np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
495 if (negative)
496 *--np = '-';
497 code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
498 return np;
501 struct timespec
502 decode_timespec (char const *arg, char **arg_lim, bool parse_fraction)
504 time_t s = TYPE_MINIMUM (time_t);
505 int ns = -1;
506 char const *p = arg;
507 bool negative = *arg == '-';
508 struct timespec r;
510 if (! c_isdigit (arg[negative]))
511 errno = EINVAL;
512 else
514 errno = 0;
516 if (negative)
518 intmax_t i = strtoimax (arg, arg_lim, 10);
519 if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i : 0 <= i)
520 s = i;
521 else
522 errno = ERANGE;
524 else
526 uintmax_t i = strtoumax (arg, arg_lim, 10);
527 if (i <= TYPE_MAXIMUM (time_t))
528 s = i;
529 else
530 errno = ERANGE;
533 p = *arg_lim;
534 ns = 0;
536 if (parse_fraction && *p == '.')
538 int digits = 0;
539 bool trailing_nonzero = false;
541 while (c_isdigit (*++p))
542 if (digits < LOG10_BILLION)
543 digits++, ns = 10 * ns + (*p - '0');
544 else
545 trailing_nonzero |= *p != '0';
547 while (digits < LOG10_BILLION)
548 digits++, ns *= 10;
550 if (negative)
552 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
553 I.e., truncate time stamps towards minus infinity while
554 converting them to internal form. */
555 ns += trailing_nonzero;
556 if (ns != 0)
558 if (s == TYPE_MINIMUM (time_t))
559 ns = -1;
560 else
562 s--;
563 ns = BILLION - ns;
569 if (errno == ERANGE)
570 ns = -1;
573 *arg_lim = (char *) p;
574 r.tv_sec = s;
575 r.tv_nsec = ns;
576 return r;
579 /* File handling. */
581 /* Saved names in case backup needs to be undone. */
582 static char *before_backup_name;
583 static char *after_backup_name;
585 /* Return 1 if FILE_NAME is obviously "." or "/". */
586 bool
587 must_be_dot_or_slash (char const *file_name)
589 file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
591 if (ISSLASH (file_name[0]))
593 for (;;)
594 if (ISSLASH (file_name[1]))
595 file_name++;
596 else if (file_name[1] == '.'
597 && ISSLASH (file_name[2 + (file_name[2] == '.')]))
598 file_name += 2 + (file_name[2] == '.');
599 else
600 return ! file_name[1];
602 else
604 while (file_name[0] == '.' && ISSLASH (file_name[1]))
606 file_name += 2;
607 while (ISSLASH (*file_name))
608 file_name++;
611 return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
615 /* Some implementations of rmdir let you remove '.' or '/'.
616 Report an error with errno set to zero for obvious cases of this;
617 otherwise call rmdir. */
618 static int
619 safer_rmdir (const char *file_name)
621 if (must_be_dot_or_slash (file_name))
623 errno = 0;
624 return -1;
627 if (unlinkat (chdir_fd, file_name, AT_REMOVEDIR) == 0)
629 remove_delayed_set_stat (file_name);
630 return 0;
632 return -1;
635 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
636 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
637 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
638 a directory that cannot be removed (e.g., because it is nonempty)
639 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
640 Return 0 on error, with errno set; if FILE_NAME is obviously the working
641 directory return zero with errno set to zero. */
643 remove_any_file (const char *file_name, enum remove_option option)
645 /* Try unlink first if we cannot unlink directories, as this saves
646 us a system call in the common case where we're removing a
647 non-directory. */
648 bool try_unlink_first = cannot_unlink_dir ();
650 if (try_unlink_first)
652 if (unlinkat (chdir_fd, file_name, 0) == 0)
653 return 1;
655 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
656 directory without appropriate privileges, but many Linux
657 kernels return the more-sensible EISDIR. */
658 if (errno != EPERM && errno != EISDIR)
659 return 0;
662 if (safer_rmdir (file_name) == 0)
663 return 1;
665 switch (errno)
667 case ENOTDIR:
668 return !try_unlink_first && unlinkat (chdir_fd, file_name, 0) == 0;
670 case 0:
671 case EEXIST:
672 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
673 case ENOTEMPTY:
674 #endif
675 switch (option)
677 case ORDINARY_REMOVE_OPTION:
678 break;
680 case WANT_DIRECTORY_REMOVE_OPTION:
681 return -1;
683 case RECURSIVE_REMOVE_OPTION:
685 char *directory = tar_savedir (file_name, 0);
686 char const *entry;
687 size_t entrylen;
689 if (! directory)
690 return 0;
692 for (entry = directory;
693 (entrylen = strlen (entry)) != 0;
694 entry += entrylen + 1)
696 char *file_name_buffer = make_file_name (file_name, entry);
697 int r = remove_any_file (file_name_buffer,
698 RECURSIVE_REMOVE_OPTION);
699 free (file_name_buffer);
701 if (! r)
703 free (directory);
704 return 0;
708 free (directory);
709 return safer_rmdir (file_name) == 0;
712 break;
715 return 0;
718 /* Check if FILE_NAME already exists and make a backup of it right now.
719 Return success (nonzero) only if the backup is either unneeded, or
720 successful. For now, directories are considered to never need
721 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
722 so, we do not have to backup block or character devices, nor remote
723 entities. */
724 bool
725 maybe_backup_file (const char *file_name, bool this_is_the_archive)
727 struct stat file_stat;
729 assign_string (&before_backup_name, file_name);
731 /* A run situation may exist between Emacs or other GNU programs trying to
732 make a backup for the same file simultaneously. If theoretically
733 possible, real problems are unlikely. Doing any better would require a
734 convention, GNU-wide, for all programs doing backups. */
736 assign_null (&after_backup_name);
738 /* Check if we really need to backup the file. */
740 if (this_is_the_archive && _remdev (file_name))
741 return true;
743 if (deref_stat (file_name, &file_stat) != 0)
745 if (errno == ENOENT)
746 return true;
748 stat_error (file_name);
749 return false;
752 if (S_ISDIR (file_stat.st_mode))
753 return true;
755 if (this_is_the_archive
756 && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
757 return true;
759 after_backup_name = find_backup_file_name (chdir_fd, file_name, backup_type);
760 if (! after_backup_name)
761 xalloc_die ();
763 if (renameat (chdir_fd, before_backup_name, chdir_fd, after_backup_name)
764 == 0)
766 if (verbose_option)
767 fprintf (stdlis, _("Renaming %s to %s\n"),
768 quote_n (0, before_backup_name),
769 quote_n (1, after_backup_name));
770 return true;
772 else
774 /* The backup operation failed. */
775 int e = errno;
776 ERROR ((0, e, _("%s: Cannot rename to %s"),
777 quotearg_colon (before_backup_name),
778 quote_n (1, after_backup_name)));
779 assign_null (&after_backup_name);
780 return false;
784 /* Try to restore the recently backed up file to its original name.
785 This is usually only needed after a failed extraction. */
786 void
787 undo_last_backup (void)
789 if (after_backup_name)
791 if (renameat (chdir_fd, after_backup_name, chdir_fd, before_backup_name)
792 != 0)
794 int e = errno;
795 ERROR ((0, e, _("%s: Cannot rename to %s"),
796 quotearg_colon (after_backup_name),
797 quote_n (1, before_backup_name)));
799 if (verbose_option)
800 fprintf (stdlis, _("Renaming %s back to %s\n"),
801 quote_n (0, after_backup_name),
802 quote_n (1, before_backup_name));
803 assign_null (&after_backup_name);
807 /* Apply either stat or lstat to (NAME, BUF), depending on the
808 presence of the --dereference option. NAME is relative to the
809 most-recent argument to chdir_do. */
811 deref_stat (char const *name, struct stat *buf)
813 return fstatat (chdir_fd, name, buf, fstatat_flags);
816 /* Read from FD into the buffer BUF with COUNT bytes. Attempt to fill
817 BUF. Wait until input is available; this matters because files are
818 opened O_NONBLOCK for security reasons, and on some file systems
819 this can cause read to fail with errno == EAGAIN. Return the
820 actual number of bytes read, zero for EOF, or
821 SAFE_READ_ERROR upon error. */
822 size_t
823 blocking_read (int fd, void *buf, size_t count)
825 size_t bytes = full_read (fd, buf, count);
827 #if defined F_SETFL && O_NONBLOCK
828 if (bytes == SAFE_READ_ERROR && errno == EAGAIN)
830 int flags = fcntl (fd, F_GETFL);
831 if (0 <= flags && flags & O_NONBLOCK
832 && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
833 bytes = full_read (fd, buf, count);
835 #endif
837 if (bytes == 0 && errno != 0)
838 bytes = SAFE_READ_ERROR;
839 return bytes;
842 /* Write to FD from the buffer BUF with COUNT bytes. Do a full write.
843 Wait until an output buffer is available; this matters because
844 files are opened O_NONBLOCK for security reasons, and on some file
845 systems this can cause write to fail with errno == EAGAIN. Return
846 the actual number of bytes written, setting errno if that is less
847 than COUNT. */
848 size_t
849 blocking_write (int fd, void const *buf, size_t count)
851 size_t bytes = full_write (fd, buf, count);
853 #if defined F_SETFL && O_NONBLOCK
854 if (bytes < count && errno == EAGAIN)
856 int flags = fcntl (fd, F_GETFL);
857 if (0 <= flags && flags & O_NONBLOCK
858 && fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
860 char const *buffer = buf;
861 bytes += full_write (fd, buffer + bytes, count - bytes);
864 #endif
866 return bytes;
869 /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
870 access time to ATIME. */
872 set_file_atime (int fd, int parentfd, char const *file, struct timespec atime)
874 struct timespec ts[2];
875 ts[0] = atime;
876 ts[1].tv_nsec = UTIME_OMIT;
877 return fdutimensat (fd, parentfd, file, ts, fstatat_flags);
880 /* A description of a working directory. */
881 struct wd
883 /* The directory's name. */
884 char const *name;
885 /* "Absolute" path representing this directory; in the contrast to
886 the real absolute pathname, it can contain /../ components (see
887 normalize_filename_x for the reason of it). It is NULL if the
888 absolute path could not be determined. */
889 char *abspath;
890 /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
891 the working directory. If zero, the directory needs to be opened
892 to be used. */
893 int fd;
896 /* A vector of chdir targets. wd[0] is the initial working directory. */
897 static struct wd *wd;
899 /* The number of working directories in the vector. */
900 static size_t wd_count;
902 /* The allocated size of the vector. */
903 static size_t wd_alloc;
905 /* The maximum number of chdir targets with open directories.
906 Don't make it too large, as many operating systems have a small
907 limit on the number of open file descriptors. Also, the current
908 implementation does not scale well. */
909 enum { CHDIR_CACHE_SIZE = 16 };
911 /* Indexes into WD of chdir targets with open file descriptors, sorted
912 most-recently used first. Zero indexes are unused. */
913 static int wdcache[CHDIR_CACHE_SIZE];
915 /* Number of nonzero entries in WDCACHE. */
916 static size_t wdcache_count;
919 chdir_count (void)
921 if (wd_count == 0)
922 return wd_count;
923 return wd_count - 1;
926 /* DIR is the operand of a -C option; add it to vector of chdir targets,
927 and return the index of its location. */
929 chdir_arg (char const *dir)
931 if (wd_count == wd_alloc)
933 if (wd_alloc == 0)
934 wd_alloc = 2;
935 wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
937 if (! wd_count)
939 wd[wd_count].name = ".";
940 wd[wd_count].abspath = NULL;
941 wd[wd_count].fd = AT_FDCWD;
942 wd_count++;
946 /* Optimize the common special case of the working directory,
947 or the working directory as a prefix. */
948 if (dir[0])
950 while (dir[0] == '.' && ISSLASH (dir[1]))
951 for (dir += 2; ISSLASH (*dir); dir++)
952 continue;
953 if (! dir[dir[0] == '.'])
954 return wd_count - 1;
957 wd[wd_count].name = dir;
958 wd[wd_count].abspath = NULL;
959 wd[wd_count].fd = 0;
960 return wd_count++;
963 /* Index of current directory. */
964 int chdir_current;
966 /* Value suitable for use as the first argument to openat, and in
967 similar locations for fstatat, etc. This is an open file
968 descriptor, or AT_FDCWD if the working directory is current. It is
969 valid until the next invocation of chdir_do. */
970 int chdir_fd = AT_FDCWD;
972 /* Change to directory I, in a virtual way. This does not actually
973 invoke chdir; it merely sets chdir_fd to an int suitable as the
974 first argument for openat, etc. If I is 0, change to the initial
975 working directory; otherwise, I must be a value returned by
976 chdir_arg. */
977 void
978 chdir_do (int i)
980 if (chdir_current != i)
982 struct wd *curr = &wd[i];
983 int fd = curr->fd;
985 if (! fd)
987 if (! IS_ABSOLUTE_FILE_NAME (curr->name))
988 chdir_do (i - 1);
989 fd = openat (chdir_fd, curr->name,
990 open_searchdir_flags & ~ O_NOFOLLOW);
991 if (fd < 0)
992 open_fatal (curr->name);
994 curr->fd = fd;
996 /* Add I to the cache, tossing out the lowest-ranking entry if the
997 cache is full. */
998 if (wdcache_count < CHDIR_CACHE_SIZE)
999 wdcache[wdcache_count++] = i;
1000 else
1002 struct wd *stale = &wd[wdcache[CHDIR_CACHE_SIZE - 1]];
1003 if (close (stale->fd) != 0)
1004 close_diag (stale->name);
1005 stale->fd = 0;
1006 wdcache[CHDIR_CACHE_SIZE - 1] = i;
1010 if (0 < fd)
1012 /* Move the i value to the front of the cache. This is
1013 O(CHDIR_CACHE_SIZE), but the cache is small. */
1014 size_t ci;
1015 int prev = wdcache[0];
1016 for (ci = 1; prev != i; ci++)
1018 int cur = wdcache[ci];
1019 wdcache[ci] = prev;
1020 if (cur == i)
1021 break;
1022 prev = cur;
1024 wdcache[0] = i;
1027 chdir_current = i;
1028 chdir_fd = fd;
1032 const char *
1033 tar_dirname (void)
1035 return wd[chdir_current].name;
1038 /* Return the absolute path that represents the working
1039 directory referenced by IDX.
1041 If wd is empty, then there were no -C options given, and
1042 chdir_args() has never been called, so we simply return the
1043 process's actual cwd. (Note that in this case IDX is ignored,
1044 since it should always be 0.) */
1045 static const char *
1046 tar_getcdpath (int idx)
1048 if (!wd)
1050 static char *cwd;
1051 if (!cwd)
1053 cwd = xgetcwd ();
1054 if (!cwd)
1055 call_arg_fatal ("getcwd", ".");
1057 return cwd;
1060 if (!wd[idx].abspath)
1062 int i;
1063 int save_cwdi = chdir_current;
1065 for (i = idx; i >= 0; i--)
1066 if (wd[i].abspath)
1067 break;
1069 while (++i <= idx)
1071 chdir_do (i);
1072 if (i == 0)
1074 if ((wd[i].abspath = xgetcwd ()) == NULL)
1075 call_arg_fatal ("getcwd", ".");
1077 else if (IS_ABSOLUTE_FILE_NAME (wd[i].name))
1078 /* If the given name is absolute, use it to represent this
1079 directory; otherwise, construct a name based on the
1080 previous -C option. */
1081 wd[i].abspath = xstrdup (wd[i].name);
1082 else
1084 namebuf_t nbuf = namebuf_create (wd[i - 1].abspath);
1085 namebuf_add_dir (nbuf, wd[i].name);
1086 wd[i].abspath = namebuf_finish (nbuf);
1090 chdir_do (save_cwdi);
1093 return wd[idx].abspath;
1096 void
1097 close_diag (char const *name)
1099 if (ignore_failed_read_option)
1101 if (WARNING_ENABLED(WARN_FAILED_READ))
1102 close_warn (name);
1104 else
1105 close_error (name);
1108 void
1109 open_diag (char const *name)
1111 if (ignore_failed_read_option)
1113 if (WARNING_ENABLED(WARN_FAILED_READ))
1114 open_warn (name);
1116 else
1117 open_error (name);
1120 void
1121 read_diag_details (char const *name, off_t offset, size_t size)
1123 if (ignore_failed_read_option)
1125 if (WARNING_ENABLED(WARN_FAILED_READ))
1126 read_warn_details (name, offset, size);
1128 else
1129 read_error_details (name, offset, size);
1132 void
1133 readlink_diag (char const *name)
1135 if (ignore_failed_read_option)
1137 if (WARNING_ENABLED(WARN_FAILED_READ))
1138 readlink_warn (name);
1140 else
1141 readlink_error (name);
1144 void
1145 savedir_diag (char const *name)
1147 if (ignore_failed_read_option)
1149 if (WARNING_ENABLED(WARN_FAILED_READ))
1150 savedir_warn (name);
1152 else
1153 savedir_error (name);
1156 void
1157 seek_diag_details (char const *name, off_t offset)
1159 if (ignore_failed_read_option)
1161 if (WARNING_ENABLED(WARN_FAILED_READ))
1162 seek_warn_details (name, offset);
1164 else
1165 seek_error_details (name, offset);
1168 void
1169 stat_diag (char const *name)
1171 if (ignore_failed_read_option)
1173 if (WARNING_ENABLED(WARN_FAILED_READ))
1174 stat_warn (name);
1176 else
1177 stat_error (name);
1180 void
1181 file_removed_diag (const char *name, bool top_level,
1182 void (*diagfn) (char const *name))
1184 if (!top_level && errno == ENOENT)
1186 WARNOPT (WARN_FILE_REMOVED,
1187 (0, 0, _("%s: File removed before we read it"),
1188 quotearg_colon (name)));
1189 set_exit_status (TAREXIT_DIFFERS);
1191 else
1192 diagfn (name);
1195 /* Fork, aborting if unsuccessful. */
1196 pid_t
1197 xfork (void)
1199 pid_t p = fork ();
1200 if (p == (pid_t) -1)
1201 call_arg_fatal ("fork", _("child process"));
1202 return p;
1205 /* Create a pipe, aborting if unsuccessful. */
1206 void
1207 xpipe (int fd[2])
1209 if (pipe (fd) < 0)
1210 call_arg_fatal ("pipe", _("interprocess channel"));
1213 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
1214 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
1215 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
1216 locations. */
1218 static void *
1219 ptr_align (void *ptr, size_t alignment)
1221 char *p0 = ptr;
1222 char *p1 = p0 + alignment - 1;
1223 return p1 - (size_t) p1 % alignment;
1226 /* Return the address of a page-aligned buffer of at least SIZE bytes.
1227 The caller should free *PTR when done with the buffer. */
1229 void *
1230 page_aligned_alloc (void **ptr, size_t size)
1232 size_t alignment = getpagesize ();
1233 size_t size1 = size + alignment;
1234 if (size1 < size)
1235 xalloc_die ();
1236 *ptr = xmalloc (size1);
1237 return ptr_align (*ptr, alignment);
1242 struct namebuf
1244 char *buffer; /* directory, '/', and directory member */
1245 size_t buffer_size; /* allocated size of name_buffer */
1246 size_t dir_length; /* length of directory part in buffer */
1249 namebuf_t
1250 namebuf_create (const char *dir)
1252 namebuf_t buf = xmalloc (sizeof (*buf));
1253 buf->buffer_size = strlen (dir) + 2;
1254 buf->buffer = xmalloc (buf->buffer_size);
1255 strcpy (buf->buffer, dir);
1256 buf->dir_length = strlen (buf->buffer);
1257 if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
1258 buf->buffer[buf->dir_length++] = DIRECTORY_SEPARATOR;
1259 return buf;
1262 void
1263 namebuf_free (namebuf_t buf)
1265 free (buf->buffer);
1266 free (buf);
1269 char *
1270 namebuf_name (namebuf_t buf, const char *name)
1272 size_t len = strlen (name);
1273 while (buf->dir_length + len + 1 >= buf->buffer_size)
1274 buf->buffer = x2realloc (buf->buffer, &buf->buffer_size);
1275 strcpy (buf->buffer + buf->dir_length, name);
1276 return buf->buffer;
1279 static void
1280 namebuf_add_dir (namebuf_t buf, const char *name)
1282 static char dirsep[] = { DIRECTORY_SEPARATOR, 0 };
1283 if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
1285 namebuf_name (buf, dirsep);
1286 buf->dir_length++;
1288 namebuf_name (buf, name);
1289 buf->dir_length += strlen (name);
1292 static char *
1293 namebuf_finish (namebuf_t buf)
1295 char *res = buf->buffer;
1297 if (ISSLASH (buf->buffer[buf->dir_length - 1]))
1298 buf->buffer[buf->dir_length] = 0;
1299 free (buf);
1300 return res;
1303 /* Return the filenames in directory NAME, relative to the chdir_fd.
1304 If the directory does not exist, report error if MUST_EXIST is
1305 true.
1307 Return NULL on errors.
1309 char *
1310 tar_savedir (const char *name, int must_exist)
1312 char *ret = NULL;
1313 DIR *dir = NULL;
1314 int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY);
1315 if (fd < 0)
1317 if (!must_exist && errno == ENOENT)
1318 return NULL;
1319 open_error (name);
1321 else if (! ((dir = fdopendir (fd))
1322 && (ret = streamsavedir (dir, savedir_sort_order))))
1323 savedir_error (name);
1325 if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0)
1326 savedir_error (name);
1328 return ret;