1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright 1988, 1992, 1994-1997, 1999-2001, 2003-2007, 2009-2010,
4 2012-2013 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
33 /* Handling strings. */
35 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
36 STRING was nonzero, it is freed first. */
38 assign_string (char **string
, const char *value
)
41 *string
= value
? xstrdup (value
) : 0;
45 /* This function is currently unused; perhaps it should be removed? */
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.
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
60 quote_copy_string (const char *string
)
62 const char *source
= string
;
63 char *destination
= 0;
69 int character
= *source
++;
76 size_t length
= (source
- string
) - 1;
79 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
80 memcpy (buffer
, string
, length
);
81 destination
= buffer
+ length
;
83 *destination
++ = '\\';
84 *destination
++ = character
== '\\' ? '\\' : 'n';
89 *destination
++ = character
;
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.
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
113 unquote_string (char *string
)
116 char *source
= string
;
117 char *destination
= string
;
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. */
129 *destination
++ = '\\';
134 *destination
++ = '\a';
139 *destination
++ = '\b';
144 *destination
++ = '\f';
149 *destination
++ = '\n';
154 *destination
++ = '\r';
159 *destination
++ = '\t';
164 *destination
++ = '\v';
169 *destination
++ = 0177;
182 int value
= *source
++ - '0';
184 if (*source
< '0' || *source
> '7')
186 *destination
++ = value
;
189 value
= value
* 8 + *source
++ - '0';
190 if (*source
< '0' || *source
> '7')
192 *destination
++ = value
;
195 value
= value
* 8 + *source
++ - '0';
196 *destination
++ = value
;
202 *destination
++ = '\\';
204 *destination
++ = *source
++;
207 else if (source
!= destination
)
208 *destination
++ = *source
++;
210 source
++, destination
++;
212 if (source
!= destination
)
217 /* Zap trailing slashes. */
219 zap_slashes (char *name
)
223 if (!name
|| *name
== 0)
225 q
= name
+ strlen (name
) - 1;
226 while (q
> name
&& ISSLASH (*q
))
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
237 normalize_filename_x (char *file_name
)
239 char *name
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
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]));
248 /* Omit redundant leading "." components. */
249 for (q
= p
= name
; (*p
= *q
) == '.' && ISSLASH (q
[1]); p
+= !*q
)
250 for (q
+= 2; ISSLASH (*q
); q
++)
253 /* Copy components from Q to P, omitting redundant slashes and
254 internal "." components. */
255 while ((*p
++ = c
= *q
++) != '\0')
257 while (ISSLASH (q
[*q
== '.']))
258 q
+= (*q
== '.') + 1;
260 /* Omit redundant trailing "." component and slash. */
263 p
-= p
[-2] == '.' && ISSLASH (p
[-3]);
264 p
-= 2 < p
- name
&& ISSLASH (p
[-2]);
269 /* Normalize NAME by removing redundant slashes and "." components,
270 including redundant trailing slashes. Return a normalized
271 newly-allocated copy. */
274 normalize_filename (const char *name
)
278 if (IS_RELATIVE_FILE_NAME (name
))
280 /* Set COPY to the absolute file name if possible.
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 ();
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);
295 copy
[copylen
] = DIRECTORY_SEPARATOR
;
296 strcpy (copy
+ copylen
+ need_separator
, name
);
300 copy
= xstrdup (name
);
301 normalize_filename_x (copy
);
307 replace_prefix (char **pname
, const char *samp
, size_t slen
,
308 const char *repl
, size_t rlen
)
311 size_t nlen
= strlen (name
);
312 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
316 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
319 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
320 memcpy (name
, repl
, rlen
);
325 /* Handling numbers. */
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"
337 sysinttostr (uintmax_t value
, intmax_t minval
, uintmax_t maxval
,
338 char buf
[SYSINT_BUFSIZE
])
341 return umaxtostr (value
, buf
);
344 intmax_t i
= value
- minval
;
345 return imaxtostr (i
+ minval
, buf
);
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.
357 This is the inverse of sysinttostr.
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"
366 strtosysint (char const *arg
, char **arglim
, intmax_t minval
, uintmax_t maxval
)
369 if (maxval
<= INTMAX_MAX
)
371 if (ISDIGIT (arg
[*arg
== '-']))
373 intmax_t i
= strtoimax (arg
, arglim
, 10);
374 intmax_t imaxval
= maxval
;
375 if (minval
<= i
&& i
<= imaxval
)
378 return i
< minval
? minval
: maxval
;
385 uintmax_t i
= strtoumax (arg
, arglim
, 10);
387 return represent_uintmax (i
);
397 /* Output fraction and trailing digits appropriate for a nanoseconds
398 count equal to NS, but don't output unnecessary '.' or trailing
402 code_ns_fraction (int ns
, char *p
)
421 p
[--i
] = '0' + ns
% 10;
430 code_timespec (struct timespec t
, char sbuf
[TIMESPEC_STRSIZE_BOUND
])
435 bool negative
= s
< 0;
437 /* ignore invalid values of ns */
438 if (BILLION
<= ns
|| ns
< 0)
441 if (negative
&& ns
!= 0)
447 np
= umaxtostr (negative
? - (uintmax_t) s
: (uintmax_t) s
, sbuf
+ 1);
450 code_ns_fraction (ns
, sbuf
+ UINTMAX_STRSIZE_BOUND
);
455 decode_timespec (char const *arg
, char **arg_lim
, bool parse_fraction
)
457 time_t s
= TYPE_MINIMUM (time_t);
460 bool negative
= *arg
== '-';
463 if (! ISDIGIT (arg
[negative
]))
471 intmax_t i
= strtoimax (arg
, arg_lim
, 10);
472 if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i
: 0 <= i
)
479 uintmax_t i
= strtoumax (arg
, arg_lim
, 10);
480 if (i
<= TYPE_MAXIMUM (time_t))
489 if (parse_fraction
&& *p
== '.')
492 bool trailing_nonzero
= false;
494 while (ISDIGIT (*++p
))
495 if (digits
< LOG10_BILLION
)
496 digits
++, ns
= 10 * ns
+ (*p
- '0');
498 trailing_nonzero
|= *p
!= '0';
500 while (digits
< LOG10_BILLION
)
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
;
511 if (s
== TYPE_MINIMUM (time_t))
526 *arg_lim
= (char *) p
;
534 /* Saved names in case backup needs to be undone. */
535 static char *before_backup_name
;
536 static char *after_backup_name
;
538 /* Return 1 if FILE_NAME is obviously "." or "/". */
540 must_be_dot_or_slash (char const *file_name
)
542 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
544 if (ISSLASH (file_name
[0]))
547 if (ISSLASH (file_name
[1]))
549 else if (file_name
[1] == '.'
550 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
551 file_name
+= 2 + (file_name
[2] == '.');
553 return ! file_name
[1];
557 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
560 while (ISSLASH (*file_name
))
564 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
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. */
572 safer_rmdir (const char *file_name
)
574 if (must_be_dot_or_slash (file_name
))
580 return unlinkat (chdir_fd
, file_name
, AT_REMOVEDIR
);
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. */
591 remove_any_file (const char *file_name
, enum remove_option option
)
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
596 bool try_unlink_first
= cannot_unlink_dir ();
598 if (try_unlink_first
)
600 if (unlinkat (chdir_fd
, file_name
, 0) == 0)
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
)
610 if (safer_rmdir (file_name
) == 0)
616 return !try_unlink_first
&& unlinkat (chdir_fd
, file_name
, 0) == 0;
620 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
625 case ORDINARY_REMOVE_OPTION
:
628 case WANT_DIRECTORY_REMOVE_OPTION
:
631 case RECURSIVE_REMOVE_OPTION
:
633 char *directory
= tar_savedir (file_name
, 0);
640 for (entry
= directory
;
641 (entrylen
= strlen (entry
)) != 0;
642 entry
+= entrylen
+ 1)
644 char *file_name_buffer
= new_name (file_name
, entry
);
645 int r
= remove_any_file (file_name_buffer
,
646 RECURSIVE_REMOVE_OPTION
);
648 free (file_name_buffer
);
659 return safer_rmdir (file_name
) == 0;
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
675 maybe_backup_file (const char *file_name
, bool this_is_the_archive
)
677 struct stat file_stat
;
679 assign_string (&before_backup_name
, file_name
);
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. */
686 assign_string (&after_backup_name
, 0);
688 /* Check if we really need to backup the file. */
690 if (this_is_the_archive
&& _remdev (file_name
))
693 if (deref_stat (file_name
, &file_stat
) != 0)
698 stat_error (file_name
);
702 if (S_ISDIR (file_stat
.st_mode
))
705 if (this_is_the_archive
706 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
709 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
710 if (! after_backup_name
)
713 if (renameat (chdir_fd
, before_backup_name
, chdir_fd
, after_backup_name
)
717 fprintf (stdlis
, _("Renaming %s to %s\n"),
718 quote_n (0, before_backup_name
),
719 quote_n (1, after_backup_name
));
724 /* The backup operation failed. */
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);
734 /* Try to restore the recently backed up file to its original name.
735 This is usually only needed after a failed extraction. */
737 undo_last_backup (void)
739 if (after_backup_name
)
741 if (renameat (chdir_fd
, after_backup_name
, chdir_fd
, before_backup_name
)
745 ERROR ((0, e
, _("%s: Cannot rename to %s"),
746 quotearg_colon (after_backup_name
),
747 quote_n (1, before_backup_name
)));
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);
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. */
761 deref_stat (char const *name
, struct stat
*buf
)
763 return fstatat (chdir_fd
, name
, buf
, fstatat_flags
);
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. */
773 blocking_read (int fd
, void *buf
, size_t count
)
775 size_t bytes
= safe_read (fd
, buf
, count
);
777 #if defined F_SETFL && O_NONBLOCK
778 if (bytes
== SAFE_READ_ERROR
&& errno
== EAGAIN
)
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
);
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
797 blocking_write (int fd
, void const *buf
, size_t count
)
799 size_t bytes
= full_write (fd
, buf
, count
);
801 #if defined F_SETFL && O_NONBLOCK
802 if (bytes
< count
&& errno
== EAGAIN
)
804 int flags
= fcntl (fd
, F_GETFL
);
805 if (0 <= flags
&& flags
& O_NONBLOCK
806 && fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
) != -1)
808 char const *buffer
= buf
;
809 bytes
+= full_write (fd
, buffer
+ bytes
, count
- bytes
);
817 /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
818 access time to ATIME. */
820 set_file_atime (int fd
, int parentfd
, char const *file
, struct timespec atime
)
822 struct timespec ts
[2];
824 ts
[1].tv_nsec
= UTIME_OMIT
;
825 return fdutimensat (fd
, parentfd
, file
, ts
, fstatat_flags
);
828 /* A description of a working directory. */
831 /* The directory's name. */
833 /* Current working directory; initialized by tar_getcwd */
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
841 /* A vector of chdir targets. wd[0] is the initial working directory. */
842 static struct wd
*wd
;
844 /* The number of working directories in the vector. */
845 static size_t wd_count
;
847 /* The allocated size of the vector. */
848 static size_t wd_alloc
;
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 };
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
];
860 /* Number of nonzero entries in WDCACHE. */
861 static size_t wdcache_count
;
871 /* DIR is the operand of a -C option; add it to vector of chdir targets,
872 and return the index of its location. */
874 chdir_arg (char const *dir
)
876 if (wd_count
== wd_alloc
)
881 wd
= xmalloc (sizeof *wd
* wd_alloc
);
884 wd
= x2nrealloc (wd
, &wd_alloc
, sizeof *wd
);
888 wd
[wd_count
].name
= ".";
889 wd
[wd_count
].cwd
= NULL
;
890 wd
[wd_count
].fd
= AT_FDCWD
;
895 /* Optimize the common special case of the working directory,
896 or the working directory as a prefix. */
899 while (dir
[0] == '.' && ISSLASH (dir
[1]))
900 for (dir
+= 2; ISSLASH (*dir
); dir
++)
902 if (! dir
[dir
[0] == '.'])
906 wd
[wd_count
].name
= dir
;
907 wd
[wd_count
].cwd
= NULL
;
912 /* Index of current directory. */
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
;
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
929 if (chdir_current
!= i
)
931 struct wd
*curr
= &wd
[i
];
936 if (! IS_ABSOLUTE_FILE_NAME (curr
->name
))
938 fd
= openat (chdir_fd
, curr
->name
,
939 open_searchdir_flags
& ~ O_NOFOLLOW
);
941 open_fatal (curr
->name
);
945 /* Add I to the cache, tossing out the lowest-ranking entry if the
947 if (wdcache_count
< CHDIR_CACHE_SIZE
)
948 wdcache
[wdcache_count
++] = i
;
951 struct wd
*stale
= &wd
[wdcache
[CHDIR_CACHE_SIZE
- 1]];
952 if (close (stale
->fd
) != 0)
953 close_diag (stale
->name
);
955 wdcache
[CHDIR_CACHE_SIZE
- 1] = i
;
961 /* Move the i value to the front of the cache. This is
962 O(CHDIR_CACHE_SIZE), but the cache is small. */
964 int prev
= wdcache
[0];
965 for (ci
= 1; prev
!= i
; ci
++)
967 int cur
= wdcache
[ci
];
993 if (0 == chdir_current
|| !wd
[chdir_current
].cwd
)
995 if (IS_ABSOLUTE_FILE_NAME (wd
[chdir_current
].name
))
996 return wd
[chdir_current
].name
;
1001 for (i
= chdir_current
- 1; i
> 0; i
--)
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
);
1010 return wd
[chdir_current
].cwd
;
1014 close_diag (char const *name
)
1016 if (ignore_failed_read_option
)
1023 open_diag (char const *name
)
1025 if (ignore_failed_read_option
)
1032 read_diag_details (char const *name
, off_t offset
, size_t size
)
1034 if (ignore_failed_read_option
)
1035 read_warn_details (name
, offset
, size
);
1037 read_error_details (name
, offset
, size
);
1041 readlink_diag (char const *name
)
1043 if (ignore_failed_read_option
)
1044 readlink_warn (name
);
1046 readlink_error (name
);
1050 savedir_diag (char const *name
)
1052 if (ignore_failed_read_option
)
1053 savedir_warn (name
);
1055 savedir_error (name
);
1059 seek_diag_details (char const *name
, off_t offset
)
1061 if (ignore_failed_read_option
)
1062 seek_warn_details (name
, offset
);
1064 seek_error_details (name
, offset
);
1068 stat_diag (char const *name
)
1070 if (ignore_failed_read_option
)
1077 file_removed_diag (const char *name
, bool top_level
,
1078 void (*diagfn
) (char const *name
))
1080 if (!top_level
&& errno
== ENOENT
)
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
);
1092 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
1094 write_error_details (name
, status
, size
);
1098 /* Fork, aborting if unsuccessful. */
1103 if (p
== (pid_t
) -1)
1104 call_arg_fatal ("fork", _("child process"));
1108 /* Create a pipe, aborting if unsuccessful. */
1113 call_arg_fatal ("pipe", _("interprocess channel"));
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
1121 static inline void *
1122 ptr_align (void *ptr
, size_t alignment
)
1125 char *p1
= p0
+ alignment
- 1;
1126 return p1
- (size_t) p1
% alignment
;
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. */
1133 page_aligned_alloc (void **ptr
, size_t size
)
1135 size_t alignment
= getpagesize ();
1136 size_t size1
= size
+ alignment
;
1139 *ptr
= xmalloc (size1
);
1140 return ptr_align (*ptr
, alignment
);
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 */
1153 namebuf_create (const char *dir
)
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
;
1166 namebuf_free (namebuf_t buf
)
1173 namebuf_name (namebuf_t buf
, const char *name
)
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
);
1183 namebuf_add_dir (namebuf_t buf
, const char *name
)
1185 static char dirsep
[] = { DIRECTORY_SEPARATOR
, 0 };
1186 if (!ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1188 namebuf_name (buf
, dirsep
);
1191 namebuf_name (buf
, name
);
1192 buf
->dir_length
+= strlen (name
);
1196 namebuf_finish (namebuf_t buf
)
1198 char *res
= buf
->buffer
;
1200 if (ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1201 buf
->buffer
[buf
->dir_length
] = 0;
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
1210 Return NULL on errors.
1213 tar_savedir (const char *name
, int must_exist
)
1217 int fd
= openat (chdir_fd
, name
, open_read_flags
| O_DIRECTORY
);
1220 if (!must_exist
&& errno
== ENOENT
)
1224 else if (! ((dir
= fdopendir (fd
))
1225 && (ret
= streamsavedir (dir
))))
1226 savedir_error (name
);
1228 if (dir
? closedir (dir
) != 0 : 0 <= fd
&& close (fd
) != 0)
1229 savedir_error (name
);