1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright 1988, 1992, 1994-1997, 1999-2001, 2003-2007, 2009-2010,
4 2012-2014 Free Software Foundation, Inc.
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
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.
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/>. */
19 #define COMMON_INLINE _GL_EXTERN_INLINE
25 #include <unlinkdir.h>
28 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
29 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
32 static const char *tar_getcdpath (int);
35 /* Handling strings. */
37 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
38 STRING was nonzero, it is freed first. */
40 assign_string (char **string
, const char *value
)
43 *string
= value
? xstrdup (value
) : 0;
47 /* This function is currently unused; perhaps it should be removed? */
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.
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
62 quote_copy_string (const char *string
)
64 const char *source
= string
;
65 char *destination
= 0;
71 int character
= *source
++;
78 size_t length
= (source
- string
) - 1;
81 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
82 memcpy (buffer
, string
, length
);
83 destination
= buffer
+ length
;
85 *destination
++ = '\\';
86 *destination
++ = character
== '\\' ? '\\' : 'n';
91 *destination
++ = character
;
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.
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
115 unquote_string (char *string
)
118 char *source
= string
;
119 char *destination
= string
;
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. */
131 *destination
++ = '\\';
136 *destination
++ = '\a';
141 *destination
++ = '\b';
146 *destination
++ = '\f';
151 *destination
++ = '\n';
156 *destination
++ = '\r';
161 *destination
++ = '\t';
166 *destination
++ = '\v';
171 *destination
++ = 0177;
184 int value
= *source
++ - '0';
186 if (*source
< '0' || *source
> '7')
188 *destination
++ = value
;
191 value
= value
* 8 + *source
++ - '0';
192 if (*source
< '0' || *source
> '7')
194 *destination
++ = value
;
197 value
= value
* 8 + *source
++ - '0';
198 *destination
++ = value
;
204 *destination
++ = '\\';
206 *destination
++ = *source
++;
209 else if (source
!= destination
)
210 *destination
++ = *source
++;
212 source
++, destination
++;
214 if (source
!= destination
)
219 /* Zap trailing slashes. */
221 zap_slashes (char *name
)
225 if (!name
|| *name
== 0)
227 q
= name
+ strlen (name
) - 1;
228 while (q
> name
&& ISSLASH (*q
))
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 "/.." != "/".
238 Destructive version: modifies its argument. */
240 normalize_filename_x (char *file_name
)
242 char *name
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
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]));
251 /* Omit redundant leading "." components. */
252 for (q
= p
= name
; (*p
= *q
) == '.' && ISSLASH (q
[1]); p
+= !*q
)
253 for (q
+= 2; ISSLASH (*q
); q
++)
256 /* Copy components from Q to P, omitting redundant slashes and
257 internal "." components. */
258 while ((*p
++ = c
= *q
++) != '\0')
260 while (ISSLASH (q
[*q
== '.']))
261 q
+= (*q
== '.') + 1;
263 /* Omit redundant trailing "." component and slash. */
266 p
-= p
[-2] == '.' && ISSLASH (p
[-3]);
267 p
-= 2 < p
- name
&& ISSLASH (p
[-2]);
272 /* Normalize NAME by removing redundant slashes and "." components,
273 including redundant trailing slashes.
275 Return a normalized newly-allocated copy. */
278 normalize_filename (int cdidx
, const char *name
)
282 if (IS_RELATIVE_FILE_NAME (name
))
284 /* Set COPY to the absolute path for this name.
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? (See listed03.at for
292 a related test case.) */
293 const char *cdpath
= tar_getcdpath (cdidx
);
298 call_arg_fatal ("getcwd", ".");
299 copylen
= strlen (cdpath
);
300 need_separator
= ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
301 && copylen
== 2 && ISSLASH (cdpath
[1]));
302 copy
= xmalloc (copylen
+ need_separator
+ strlen (name
) + 1);
303 strcpy (copy
, cdpath
);
304 copy
[copylen
] = DIRECTORY_SEPARATOR
;
305 strcpy (copy
+ copylen
+ need_separator
, name
);
309 copy
= xstrdup (name
);
310 normalize_filename_x (copy
);
316 replace_prefix (char **pname
, const char *samp
, size_t slen
,
317 const char *repl
, size_t rlen
)
320 size_t nlen
= strlen (name
);
321 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
325 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
328 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
329 memcpy (name
, repl
, rlen
);
334 /* Handling numbers. */
336 /* Convert VALUE, which is converted from a system integer type whose
337 minimum value is MINVAL and maximum MINVAL, to an decimal
338 integer string. Use the storage in BUF and return a pointer to the
339 converted string. If VALUE is converted from a negative integer in
340 the range MINVAL .. -1, represent it with a string representation
341 of the negative integer, using leading '-'. */
342 #if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
343 # error "sysinttostr: uintmax_t cannot represent all intmax_t values"
346 sysinttostr (uintmax_t value
, intmax_t minval
, uintmax_t maxval
,
347 char buf
[SYSINT_BUFSIZE
])
350 return umaxtostr (value
, buf
);
353 intmax_t i
= value
- minval
;
354 return imaxtostr (i
+ minval
, buf
);
358 /* Convert a prefix of the string ARG to a system integer type whose
359 minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
360 negative integers MINVAL .. -1 are assumed to be represented using
361 leading '-' in the usual way. If the represented value exceeds
362 INTMAX_MAX, return a negative integer V such that (uintmax_t) V
363 yields the represented value. If ARGLIM is nonnull, store into
364 *ARGLIM a pointer to the first character after the prefix.
366 This is the inverse of sysinttostr.
368 On a normal return, set errno = 0.
369 On conversion error, return 0 and set errno = EINVAL.
370 On overflow, return an extreme value and set errno = ERANGE. */
371 #if ! (INTMAX_MAX <= UINTMAX_MAX)
372 # error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
375 strtosysint (char const *arg
, char **arglim
, intmax_t minval
, uintmax_t maxval
)
378 if (maxval
<= INTMAX_MAX
)
380 if (ISDIGIT (arg
[*arg
== '-']))
382 intmax_t i
= strtoimax (arg
, arglim
, 10);
383 intmax_t imaxval
= maxval
;
384 if (minval
<= i
&& i
<= imaxval
)
387 return i
< minval
? minval
: maxval
;
394 uintmax_t i
= strtoumax (arg
, arglim
, 10);
396 return represent_uintmax (i
);
406 /* Output fraction and trailing digits appropriate for a nanoseconds
407 count equal to NS, but don't output unnecessary '.' or trailing
411 code_ns_fraction (int ns
, char *p
)
430 p
[--i
] = '0' + ns
% 10;
439 code_timespec (struct timespec t
, char sbuf
[TIMESPEC_STRSIZE_BOUND
])
444 bool negative
= s
< 0;
446 /* ignore invalid values of ns */
447 if (BILLION
<= ns
|| ns
< 0)
450 if (negative
&& ns
!= 0)
456 np
= umaxtostr (negative
? - (uintmax_t) s
: (uintmax_t) s
, sbuf
+ 1);
459 code_ns_fraction (ns
, sbuf
+ UINTMAX_STRSIZE_BOUND
);
464 decode_timespec (char const *arg
, char **arg_lim
, bool parse_fraction
)
466 time_t s
= TYPE_MINIMUM (time_t);
469 bool negative
= *arg
== '-';
472 if (! ISDIGIT (arg
[negative
]))
480 intmax_t i
= strtoimax (arg
, arg_lim
, 10);
481 if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i
: 0 <= i
)
488 uintmax_t i
= strtoumax (arg
, arg_lim
, 10);
489 if (i
<= TYPE_MAXIMUM (time_t))
498 if (parse_fraction
&& *p
== '.')
501 bool trailing_nonzero
= false;
503 while (ISDIGIT (*++p
))
504 if (digits
< LOG10_BILLION
)
505 digits
++, ns
= 10 * ns
+ (*p
- '0');
507 trailing_nonzero
|= *p
!= '0';
509 while (digits
< LOG10_BILLION
)
514 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
515 I.e., truncate time stamps towards minus infinity while
516 converting them to internal form. */
517 ns
+= trailing_nonzero
;
520 if (s
== TYPE_MINIMUM (time_t))
535 *arg_lim
= (char *) p
;
543 /* Saved names in case backup needs to be undone. */
544 static char *before_backup_name
;
545 static char *after_backup_name
;
547 /* Return 1 if FILE_NAME is obviously "." or "/". */
549 must_be_dot_or_slash (char const *file_name
)
551 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
553 if (ISSLASH (file_name
[0]))
556 if (ISSLASH (file_name
[1]))
558 else if (file_name
[1] == '.'
559 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
560 file_name
+= 2 + (file_name
[2] == '.');
562 return ! file_name
[1];
566 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
569 while (ISSLASH (*file_name
))
573 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
577 /* Some implementations of rmdir let you remove '.' or '/'.
578 Report an error with errno set to zero for obvious cases of this;
579 otherwise call rmdir. */
581 safer_rmdir (const char *file_name
)
583 if (must_be_dot_or_slash (file_name
))
589 if (unlinkat (chdir_fd
, file_name
, AT_REMOVEDIR
) == 0)
591 remove_delayed_set_stat (file_name
);
597 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
598 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
599 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
600 a directory that cannot be removed (e.g., because it is nonempty)
601 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
602 Return 0 on error, with errno set; if FILE_NAME is obviously the working
603 directory return zero with errno set to zero. */
605 remove_any_file (const char *file_name
, enum remove_option option
)
607 /* Try unlink first if we cannot unlink directories, as this saves
608 us a system call in the common case where we're removing a
610 bool try_unlink_first
= cannot_unlink_dir ();
612 if (try_unlink_first
)
614 if (unlinkat (chdir_fd
, file_name
, 0) == 0)
617 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
618 directory without appropriate privileges, but many Linux
619 kernels return the more-sensible EISDIR. */
620 if (errno
!= EPERM
&& errno
!= EISDIR
)
624 if (safer_rmdir (file_name
) == 0)
630 return !try_unlink_first
&& unlinkat (chdir_fd
, file_name
, 0) == 0;
634 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
639 case ORDINARY_REMOVE_OPTION
:
642 case WANT_DIRECTORY_REMOVE_OPTION
:
645 case RECURSIVE_REMOVE_OPTION
:
647 char *directory
= tar_savedir (file_name
, 0);
654 for (entry
= directory
;
655 (entrylen
= strlen (entry
)) != 0;
656 entry
+= entrylen
+ 1)
658 char *file_name_buffer
= new_name (file_name
, entry
);
659 int r
= remove_any_file (file_name_buffer
,
660 RECURSIVE_REMOVE_OPTION
);
662 free (file_name_buffer
);
673 return safer_rmdir (file_name
) == 0;
682 /* Check if FILE_NAME already exists and make a backup of it right now.
683 Return success (nonzero) only if the backup is either unneeded, or
684 successful. For now, directories are considered to never need
685 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
686 so, we do not have to backup block or character devices, nor remote
689 maybe_backup_file (const char *file_name
, bool this_is_the_archive
)
691 struct stat file_stat
;
693 assign_string (&before_backup_name
, file_name
);
695 /* A run situation may exist between Emacs or other GNU programs trying to
696 make a backup for the same file simultaneously. If theoretically
697 possible, real problems are unlikely. Doing any better would require a
698 convention, GNU-wide, for all programs doing backups. */
700 assign_string (&after_backup_name
, 0);
702 /* Check if we really need to backup the file. */
704 if (this_is_the_archive
&& _remdev (file_name
))
707 if (deref_stat (file_name
, &file_stat
) != 0)
712 stat_error (file_name
);
716 if (S_ISDIR (file_stat
.st_mode
))
719 if (this_is_the_archive
720 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
723 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
724 if (! after_backup_name
)
727 if (renameat (chdir_fd
, before_backup_name
, chdir_fd
, after_backup_name
)
731 fprintf (stdlis
, _("Renaming %s to %s\n"),
732 quote_n (0, before_backup_name
),
733 quote_n (1, after_backup_name
));
738 /* The backup operation failed. */
740 ERROR ((0, e
, _("%s: Cannot rename to %s"),
741 quotearg_colon (before_backup_name
),
742 quote_n (1, after_backup_name
)));
743 assign_string (&after_backup_name
, 0);
748 /* Try to restore the recently backed up file to its original name.
749 This is usually only needed after a failed extraction. */
751 undo_last_backup (void)
753 if (after_backup_name
)
755 if (renameat (chdir_fd
, after_backup_name
, chdir_fd
, before_backup_name
)
759 ERROR ((0, e
, _("%s: Cannot rename to %s"),
760 quotearg_colon (after_backup_name
),
761 quote_n (1, before_backup_name
)));
764 fprintf (stdlis
, _("Renaming %s back to %s\n"),
765 quote_n (0, after_backup_name
),
766 quote_n (1, before_backup_name
));
767 assign_string (&after_backup_name
, 0);
771 /* Apply either stat or lstat to (NAME, BUF), depending on the
772 presence of the --dereference option. NAME is relative to the
773 most-recent argument to chdir_do. */
775 deref_stat (char const *name
, struct stat
*buf
)
777 return fstatat (chdir_fd
, name
, buf
, fstatat_flags
);
780 /* Read from FD into the buffer BUF with COUNT bytes. Attempt to fill
781 BUF. Wait until input is available; this matters because files are
782 opened O_NONBLOCK for security reasons, and on some file systems
783 this can cause read to fail with errno == EAGAIN. Return the
784 actual number of bytes read, zero for EOF, or
785 SAFE_READ_ERROR upon error. */
787 blocking_read (int fd
, void *buf
, size_t count
)
789 size_t bytes
= safe_read (fd
, buf
, count
);
791 #if defined F_SETFL && O_NONBLOCK
792 if (bytes
== SAFE_READ_ERROR
&& errno
== EAGAIN
)
794 int flags
= fcntl (fd
, F_GETFL
);
795 if (0 <= flags
&& flags
& O_NONBLOCK
796 && fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
) != -1)
797 bytes
= safe_read (fd
, buf
, count
);
804 /* Write to FD from the buffer BUF with COUNT bytes. Do a full write.
805 Wait until an output buffer is available; this matters because
806 files are opened O_NONBLOCK for security reasons, and on some file
807 systems this can cause write to fail with errno == EAGAIN. Return
808 the actual number of bytes written, setting errno if that is less
811 blocking_write (int fd
, void const *buf
, size_t count
)
813 size_t bytes
= full_write (fd
, buf
, count
);
815 #if defined F_SETFL && O_NONBLOCK
816 if (bytes
< count
&& errno
== EAGAIN
)
818 int flags
= fcntl (fd
, F_GETFL
);
819 if (0 <= flags
&& flags
& O_NONBLOCK
820 && fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
) != -1)
822 char const *buffer
= buf
;
823 bytes
+= full_write (fd
, buffer
+ bytes
, count
- bytes
);
831 /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
832 access time to ATIME. */
834 set_file_atime (int fd
, int parentfd
, char const *file
, struct timespec atime
)
836 struct timespec ts
[2];
838 ts
[1].tv_nsec
= UTIME_OMIT
;
839 return fdutimensat (fd
, parentfd
, file
, ts
, fstatat_flags
);
842 /* A description of a working directory. */
845 /* The directory's name. */
847 /* "Absolute" path representing this directory; in the contrast to
848 the real absolute pathname, it can contain /../ components (see
849 normalize_filename_x for the reason of it). It is NULL if the
850 absolute path could not be determined. */
852 /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
853 the working directory. If zero, the directory needs to be opened
858 /* A vector of chdir targets. wd[0] is the initial working directory. */
859 static struct wd
*wd
;
861 /* The number of working directories in the vector. */
862 static size_t wd_count
;
864 /* The allocated size of the vector. */
865 static size_t wd_alloc
;
867 /* The maximum number of chdir targets with open directories.
868 Don't make it too large, as many operating systems have a small
869 limit on the number of open file descriptors. Also, the current
870 implementation does not scale well. */
871 enum { CHDIR_CACHE_SIZE
= 16 };
873 /* Indexes into WD of chdir targets with open file descriptors, sorted
874 most-recently used first. Zero indexes are unused. */
875 static int wdcache
[CHDIR_CACHE_SIZE
];
877 /* Number of nonzero entries in WDCACHE. */
878 static size_t wdcache_count
;
888 /* DIR is the operand of a -C option; add it to vector of chdir targets,
889 and return the index of its location. */
891 chdir_arg (char const *dir
)
895 if (wd_count
== wd_alloc
)
899 wd
= x2nrealloc (wd
, &wd_alloc
, sizeof *wd
);
903 wd
[wd_count
].name
= ".";
904 wd
[wd_count
].abspath
= xgetcwd ();
905 wd
[wd_count
].fd
= AT_FDCWD
;
910 /* Optimize the common special case of the working directory,
911 or the working directory as a prefix. */
914 while (dir
[0] == '.' && ISSLASH (dir
[1]))
915 for (dir
+= 2; ISSLASH (*dir
); dir
++)
917 if (! dir
[dir
[0] == '.'])
922 /* If the given name is absolute, use it to represent this directory;
923 otherwise, construct a name based on the previous -C option. */
924 if (IS_ABSOLUTE_FILE_NAME (dir
))
925 absdir
= xstrdup (dir
);
926 else if (wd
[wd_count
- 1].abspath
)
928 namebuf_t nbuf
= namebuf_create (wd
[wd_count
- 1].abspath
);
929 namebuf_add_dir (nbuf
, dir
);
930 absdir
= namebuf_finish (nbuf
);
935 wd
[wd_count
].name
= dir
;
936 wd
[wd_count
].abspath
= absdir
;
941 /* Index of current directory. */
944 /* Value suitable for use as the first argument to openat, and in
945 similar locations for fstatat, etc. This is an open file
946 descriptor, or AT_FDCWD if the working directory is current. It is
947 valid until the next invocation of chdir_do. */
948 int chdir_fd
= AT_FDCWD
;
950 /* Change to directory I, in a virtual way. This does not actually
951 invoke chdir; it merely sets chdir_fd to an int suitable as the
952 first argument for openat, etc. If I is 0, change to the initial
953 working directory; otherwise, I must be a value returned by
958 if (chdir_current
!= i
)
960 struct wd
*curr
= &wd
[i
];
965 if (! IS_ABSOLUTE_FILE_NAME (curr
->name
))
967 fd
= openat (chdir_fd
, curr
->name
,
968 open_searchdir_flags
& ~ O_NOFOLLOW
);
970 open_fatal (curr
->name
);
974 /* Add I to the cache, tossing out the lowest-ranking entry if the
976 if (wdcache_count
< CHDIR_CACHE_SIZE
)
977 wdcache
[wdcache_count
++] = i
;
980 struct wd
*stale
= &wd
[wdcache
[CHDIR_CACHE_SIZE
- 1]];
981 if (close (stale
->fd
) != 0)
982 close_diag (stale
->name
);
984 wdcache
[CHDIR_CACHE_SIZE
- 1] = i
;
990 /* Move the i value to the front of the cache. This is
991 O(CHDIR_CACHE_SIZE), but the cache is small. */
993 int prev
= wdcache
[0];
994 for (ci
= 1; prev
!= i
; ci
++)
996 int cur
= wdcache
[ci
];
1013 return wd
[chdir_current
].name
;
1016 /* Return the absolute path that represents the working
1017 directory referenced by IDX.
1019 If wd is empty, then there were no -C options given, and
1020 chdir_args() has never been called, so we simply return the
1021 process's actual cwd. (Note that in this case IDX is ignored,
1022 since it should always be 0.) */
1024 tar_getcdpath (int idx
)
1033 return wd
[idx
].abspath
;
1037 close_diag (char const *name
)
1039 if (ignore_failed_read_option
)
1046 open_diag (char const *name
)
1048 if (ignore_failed_read_option
)
1055 read_diag_details (char const *name
, off_t offset
, size_t size
)
1057 if (ignore_failed_read_option
)
1058 read_warn_details (name
, offset
, size
);
1060 read_error_details (name
, offset
, size
);
1064 readlink_diag (char const *name
)
1066 if (ignore_failed_read_option
)
1067 readlink_warn (name
);
1069 readlink_error (name
);
1073 savedir_diag (char const *name
)
1075 if (ignore_failed_read_option
)
1076 savedir_warn (name
);
1078 savedir_error (name
);
1082 seek_diag_details (char const *name
, off_t offset
)
1084 if (ignore_failed_read_option
)
1085 seek_warn_details (name
, offset
);
1087 seek_error_details (name
, offset
);
1091 stat_diag (char const *name
)
1093 if (ignore_failed_read_option
)
1100 file_removed_diag (const char *name
, bool top_level
,
1101 void (*diagfn
) (char const *name
))
1103 if (!top_level
&& errno
== ENOENT
)
1105 WARNOPT (WARN_FILE_REMOVED
,
1106 (0, 0, _("%s: File removed before we read it"),
1107 quotearg_colon (name
)));
1108 set_exit_status (TAREXIT_DIFFERS
);
1115 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
1117 write_error_details (name
, status
, size
);
1121 /* Fork, aborting if unsuccessful. */
1126 if (p
== (pid_t
) -1)
1127 call_arg_fatal ("fork", _("child process"));
1131 /* Create a pipe, aborting if unsuccessful. */
1136 call_arg_fatal ("pipe", _("interprocess channel"));
1139 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
1140 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
1141 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
1144 static inline void *
1145 ptr_align (void *ptr
, size_t alignment
)
1148 char *p1
= p0
+ alignment
- 1;
1149 return p1
- (size_t) p1
% alignment
;
1152 /* Return the address of a page-aligned buffer of at least SIZE bytes.
1153 The caller should free *PTR when done with the buffer. */
1156 page_aligned_alloc (void **ptr
, size_t size
)
1158 size_t alignment
= getpagesize ();
1159 size_t size1
= size
+ alignment
;
1162 *ptr
= xmalloc (size1
);
1163 return ptr_align (*ptr
, alignment
);
1170 char *buffer
; /* directory, '/', and directory member */
1171 size_t buffer_size
; /* allocated size of name_buffer */
1172 size_t dir_length
; /* length of directory part in buffer */
1176 namebuf_create (const char *dir
)
1178 namebuf_t buf
= xmalloc (sizeof (*buf
));
1179 buf
->buffer_size
= strlen (dir
) + 2;
1180 buf
->buffer
= xmalloc (buf
->buffer_size
);
1181 strcpy (buf
->buffer
, dir
);
1182 buf
->dir_length
= strlen (buf
->buffer
);
1183 if (!ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1184 buf
->buffer
[buf
->dir_length
++] = DIRECTORY_SEPARATOR
;
1189 namebuf_free (namebuf_t buf
)
1196 namebuf_name (namebuf_t buf
, const char *name
)
1198 size_t len
= strlen (name
);
1199 while (buf
->dir_length
+ len
+ 1 >= buf
->buffer_size
)
1200 buf
->buffer
= x2realloc (buf
->buffer
, &buf
->buffer_size
);
1201 strcpy (buf
->buffer
+ buf
->dir_length
, name
);
1206 namebuf_add_dir (namebuf_t buf
, const char *name
)
1208 static char dirsep
[] = { DIRECTORY_SEPARATOR
, 0 };
1209 if (!ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1211 namebuf_name (buf
, dirsep
);
1214 namebuf_name (buf
, name
);
1215 buf
->dir_length
+= strlen (name
);
1219 namebuf_finish (namebuf_t buf
)
1221 char *res
= buf
->buffer
;
1223 if (ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1224 buf
->buffer
[buf
->dir_length
] = 0;
1229 /* Return the filenames in directory NAME, relative to the chdir_fd.
1230 If the directory does not exist, report error if MUST_EXIST is
1233 Return NULL on errors.
1236 tar_savedir (const char *name
, int must_exist
)
1240 int fd
= openat (chdir_fd
, name
, open_read_flags
| O_DIRECTORY
);
1243 if (!must_exist
&& errno
== ENOENT
)
1247 else if (! ((dir
= fdopendir (fd
))
1248 && (ret
= streamsavedir (dir
, savedir_sort_order
))))
1249 savedir_error (name
);
1251 if (dir
? closedir (dir
) != 0 : 0 <= fd
&& close (fd
) != 0)
1252 savedir_error (name
);