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