1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2009, 2010 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, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #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
)
42 *string
= value
? xstrdup (value
) : 0;
46 /* This function is currently unused; perhaps it should be removed? */
48 /* Allocate a copy of the string quoted as in C, and returns that. If
49 the string does not have to be quoted, it returns a null pointer.
50 The allocated copy should normally be freed with free() after the
51 caller is done with it.
53 This is used in one context only: generating the directory file in
54 incremental dumps. The quoted string is not intended for human
55 consumption; it is intended only for unquote_string. The quoting
56 is locale-independent, so that users needn't worry about locale
57 when reading directory files. This means that we can't use
58 quotearg, as quotearg is locale-dependent and is meant for human
61 quote_copy_string (const char *string
)
63 const char *source
= string
;
64 char *destination
= 0;
70 int character
= *source
++;
77 size_t length
= (source
- string
) - 1;
80 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
81 memcpy (buffer
, string
, length
);
82 destination
= buffer
+ length
;
84 *destination
++ = '\\';
85 *destination
++ = character
== '\\' ? '\\' : 'n';
90 *destination
++ = character
;
103 /* Takes a quoted C string (like those produced by quote_copy_string)
104 and turns it back into the un-quoted original. This is done in
105 place. Returns 0 only if the string was not properly quoted, but
106 completes the unquoting anyway.
108 This is used for reading the saved directory file in incremental
109 dumps. It is used for decoding old `N' records (demangling names).
110 But also, it is used for decoding file arguments, would they come
111 from the shell or a -T file, and for decoding the --exclude
114 unquote_string (char *string
)
117 char *source
= string
;
118 char *destination
= string
;
120 /* Escape sequences other than \\ and \n are no longer generated by
121 quote_copy_string, but accept them for backwards compatibility,
122 and also because unquote_string is used for purposes other than
123 parsing the output of quote_copy_string. */
130 *destination
++ = '\\';
135 *destination
++ = '\a';
140 *destination
++ = '\b';
145 *destination
++ = '\f';
150 *destination
++ = '\n';
155 *destination
++ = '\r';
160 *destination
++ = '\t';
165 *destination
++ = '\v';
170 *destination
++ = 0177;
183 int value
= *source
++ - '0';
185 if (*source
< '0' || *source
> '7')
187 *destination
++ = value
;
190 value
= value
* 8 + *source
++ - '0';
191 if (*source
< '0' || *source
> '7')
193 *destination
++ = value
;
196 value
= value
* 8 + *source
++ - '0';
197 *destination
++ = value
;
203 *destination
++ = '\\';
205 *destination
++ = *source
++;
208 else if (source
!= destination
)
209 *destination
++ = *source
++;
211 source
++, destination
++;
213 if (source
!= destination
)
218 /* Zap trailing slashes. */
220 zap_slashes (char *name
)
224 if (!name
|| *name
== 0)
226 q
= name
+ strlen (name
) - 1;
227 while (q
> name
&& ISSLASH (*q
))
232 /* Normalize FILE_NAME by removing redundant slashes and "."
233 components, including redundant trailing slashes. Leave ".."
234 alone, as it may be significant in the presence of symlinks and on
235 platforms where "/.." != "/". Destructive version: modifies its
238 normalize_filename_x (char *file_name
)
240 char *name
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
245 /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
246 name
+= (DOUBLE_SLASH_IS_DISTINCT_ROOT
247 && ISSLASH (*name
) && ISSLASH (name
[1]) && ! ISSLASH (name
[2]));
249 /* Omit redundant leading "." components. */
250 for (q
= p
= name
; (*p
= *q
) == '.' && ISSLASH (q
[1]); p
+= !*q
)
251 for (q
+= 2; ISSLASH (*q
); q
++)
254 /* Copy components from Q to P, omitting redundant slashes and
255 internal "." components. */
256 while ((*p
++ = c
= *q
++) != '\0')
258 while (ISSLASH (q
[*q
== '.']))
259 q
+= (*q
== '.') + 1;
261 /* Omit redundant trailing "." component and slash. */
264 p
-= p
[-2] == '.' && ISSLASH (p
[-3]);
265 p
-= 2 < p
- name
&& ISSLASH (p
[-2]);
270 /* Normalize NAME by removing redundant slashes and "." components,
271 including redundant trailing slashes. Return a normalized
272 newly-allocated copy. */
275 normalize_filename (const char *name
)
279 if (IS_RELATIVE_FILE_NAME (name
))
281 /* Set COPY to the absolute file name if possible.
283 FIXME: There should be no need to get the absolute file name.
284 getcwd is slow, it might fail, and it does not necessarily
285 return a canonical name even when it succeeds. Perhaps we
286 can use dev+ino pairs instead of names? */
290 size_t copylen
= strlen (copy
);
291 bool need_separator
= ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
292 && copylen
== 2 && ISSLASH (copy
[1]));
293 copy
= xrealloc (copy
, copylen
+ need_separator
+ strlen (name
) + 1);
294 copy
[copylen
] = DIRECTORY_SEPARATOR
;
295 strcpy (copy
+ copylen
+ need_separator
, name
);
298 WARN ((0, errno
, _("Cannot get working directory")));
302 copy
= xstrdup (name
);
303 normalize_filename_x (copy
);
309 replace_prefix (char **pname
, const char *samp
, size_t slen
,
310 const char *repl
, size_t rlen
)
313 size_t nlen
= strlen (name
);
314 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
318 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
321 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
322 memcpy (name
, repl
, rlen
);
327 /* Handling numbers. */
329 /* Output fraction and trailing digits appropriate for a nanoseconds
330 count equal to NS, but don't output unnecessary '.' or trailing
334 code_ns_fraction (int ns
, char *p
)
353 p
[--i
] = '0' + ns
% 10;
362 code_timespec (struct timespec t
, char sbuf
[TIMESPEC_STRSIZE_BOUND
])
367 bool negative
= s
< 0;
369 /* ignore invalid values of ns */
370 if (BILLION
<= ns
|| ns
< 0)
373 if (negative
&& ns
!= 0)
379 np
= umaxtostr (negative
? - (uintmax_t) s
: (uintmax_t) s
, sbuf
+ 1);
382 code_ns_fraction (ns
, sbuf
+ UINTMAX_STRSIZE_BOUND
);
388 /* Saved names in case backup needs to be undone. */
389 static char *before_backup_name
;
390 static char *after_backup_name
;
392 /* Return 1 if FILE_NAME is obviously "." or "/". */
394 must_be_dot_or_slash (char const *file_name
)
396 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
398 if (ISSLASH (file_name
[0]))
401 if (ISSLASH (file_name
[1]))
403 else if (file_name
[1] == '.'
404 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
405 file_name
+= 2 + (file_name
[2] == '.');
407 return ! file_name
[1];
411 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
414 while (ISSLASH (*file_name
))
418 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
422 /* Some implementations of rmdir let you remove '.' or '/'.
423 Report an error with errno set to zero for obvious cases of this;
424 otherwise call rmdir. */
426 safer_rmdir (const char *file_name
)
428 if (must_be_dot_or_slash (file_name
))
434 return rmdir (file_name
);
437 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
438 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
439 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
440 a directory that cannot be removed (e.g., because it is nonempty)
441 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
442 Return 0 on error, with errno set; if FILE_NAME is obviously the working
443 directory return zero with errno set to zero. */
445 remove_any_file (const char *file_name
, enum remove_option option
)
447 /* Try unlink first if we cannot unlink directories, as this saves
448 us a system call in the common case where we're removing a
450 bool try_unlink_first
= cannot_unlink_dir ();
452 if (try_unlink_first
)
454 if (unlink (file_name
) == 0)
457 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
458 directory without appropriate privileges, but many Linux
459 kernels return the more-sensible EISDIR. */
460 if (errno
!= EPERM
&& errno
!= EISDIR
)
464 if (safer_rmdir (file_name
) == 0)
470 return !try_unlink_first
&& unlink (file_name
) == 0;
474 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
479 case ORDINARY_REMOVE_OPTION
:
482 case WANT_DIRECTORY_REMOVE_OPTION
:
485 case RECURSIVE_REMOVE_OPTION
:
487 char *directory
= savedir (file_name
);
494 for (entry
= directory
;
495 (entrylen
= strlen (entry
)) != 0;
496 entry
+= entrylen
+ 1)
498 char *file_name_buffer
= new_name (file_name
, entry
);
499 int r
= remove_any_file (file_name_buffer
,
500 RECURSIVE_REMOVE_OPTION
);
502 free (file_name_buffer
);
513 return safer_rmdir (file_name
) == 0;
522 /* Check if FILE_NAME already exists and make a backup of it right now.
523 Return success (nonzero) only if the backup is either unneeded, or
524 successful. For now, directories are considered to never need
525 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
526 so, we do not have to backup block or character devices, nor remote
529 maybe_backup_file (const char *file_name
, bool this_is_the_archive
)
531 struct stat file_stat
;
533 assign_string (&before_backup_name
, file_name
);
535 /* A run situation may exist between Emacs or other GNU programs trying to
536 make a backup for the same file simultaneously. If theoretically
537 possible, real problems are unlikely. Doing any better would require a
538 convention, GNU-wide, for all programs doing backups. */
540 assign_string (&after_backup_name
, 0);
542 /* Check if we really need to backup the file. */
544 if (this_is_the_archive
&& _remdev (file_name
))
547 if (stat (file_name
, &file_stat
))
552 stat_error (file_name
);
556 if (S_ISDIR (file_stat
.st_mode
))
559 if (this_is_the_archive
560 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
563 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
564 if (! after_backup_name
)
567 if (rename (before_backup_name
, after_backup_name
) == 0)
570 fprintf (stdlis
, _("Renaming %s to %s\n"),
571 quote_n (0, before_backup_name
),
572 quote_n (1, after_backup_name
));
577 /* The backup operation failed. */
579 ERROR ((0, e
, _("%s: Cannot rename to %s"),
580 quotearg_colon (before_backup_name
),
581 quote_n (1, after_backup_name
)));
582 assign_string (&after_backup_name
, 0);
587 /* Try to restore the recently backed up file to its original name.
588 This is usually only needed after a failed extraction. */
590 undo_last_backup (void)
592 if (after_backup_name
)
594 if (rename (after_backup_name
, before_backup_name
) != 0)
597 ERROR ((0, e
, _("%s: Cannot rename to %s"),
598 quotearg_colon (after_backup_name
),
599 quote_n (1, before_backup_name
)));
602 fprintf (stdlis
, _("Renaming %s back to %s\n"),
603 quote_n (0, after_backup_name
),
604 quote_n (1, before_backup_name
));
605 assign_string (&after_backup_name
, 0);
609 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
611 deref_stat (bool deref
, char const *name
, struct stat
*buf
)
613 return deref
? stat (name
, buf
) : lstat (name
, buf
);
616 /* Use futimens if possible, utimensat otherwise. */
618 fd_utimensat (int fd
, int parentfd
, char const *file
,
619 struct timespec
const ts
[2], int atflag
)
623 int result
= futimens (fd
, ts
);
624 if (! (result
< 0 && errno
== ENOSYS
))
628 return utimensat (parentfd
, file
, ts
, atflag
);
631 /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
632 access time to ATIME. ATFLAG controls symbolic-link following, in
633 the style of openat. */
635 set_file_atime (int fd
, int parentfd
, char const *file
, struct timespec atime
,
638 struct timespec ts
[2];
640 ts
[1].tv_nsec
= UTIME_OMIT
;
641 return fd_utimensat (fd
, parentfd
, file
, ts
, atflag
);
644 /* A description of a working directory. */
647 /* The directory's name. */
650 /* A negative value if no attempt has been made to save the
651 directory, 0 if it was saved successfully, and a positive errno
652 value if it was not saved successfully. */
655 /* The saved version of the directory, if ERR == 0. */
656 struct saved_cwd saved_cwd
;
659 /* A vector of chdir targets. wd[0] is the initial working directory. */
660 static struct wd
*wd
;
662 /* The number of working directories in the vector. */
663 static size_t wd_count
;
665 /* The allocated size of the vector. */
666 static size_t wd_alloc
;
676 /* DIR is the operand of a -C option; add it to vector of chdir targets,
677 and return the index of its location. */
679 chdir_arg (char const *dir
)
681 if (wd_count
== wd_alloc
)
686 wd
= xmalloc (sizeof *wd
* wd_alloc
);
689 wd
= x2nrealloc (wd
, &wd_alloc
, sizeof *wd
);
693 wd
[wd_count
].name
= ".";
694 wd
[wd_count
].err
= -1;
699 /* Optimize the common special case of the working directory,
700 or the working directory as a prefix. */
703 while (dir
[0] == '.' && ISSLASH (dir
[1]))
704 for (dir
+= 2; ISSLASH (*dir
); dir
++)
706 if (! dir
[dir
[0] == '.'])
710 wd
[wd_count
].name
= dir
;
711 wd
[wd_count
].err
= -1;
715 /* Index of current directory. */
718 /* Change to directory I. If I is 0, change to the initial working
719 directory; otherwise, I must be a value returned by chdir_arg. */
723 if (chdir_current
!= i
)
725 struct wd
*prev
= &wd
[chdir_current
];
726 struct wd
*curr
= &wd
[i
];
731 if (save_cwd (&prev
->saved_cwd
) != 0)
733 else if (0 <= prev
->saved_cwd
.desc
)
735 /* Make sure we still have at least one descriptor available. */
736 int fd1
= prev
->saved_cwd
.desc
;
740 else if (errno
== EMFILE
)
742 /* Force restore_cwd to use chdir_long. */
744 prev
->saved_cwd
.desc
= -1;
745 prev
->saved_cwd
.name
= xgetcwd ();
746 if (! prev
->saved_cwd
.name
)
757 if (err
== 0 && restore_cwd (&curr
->saved_cwd
) != 0)
760 FATAL_ERROR ((0, err
, _("Cannot restore working directory")));
764 if (i
&& ! ISSLASH (curr
->name
[0]))
766 if (chdir (curr
->name
) != 0)
767 chdir_fatal (curr
->name
);
775 close_diag (char const *name
)
777 if (ignore_failed_read_option
)
784 open_diag (char const *name
)
786 if (ignore_failed_read_option
)
793 read_diag_details (char const *name
, off_t offset
, size_t size
)
795 if (ignore_failed_read_option
)
796 read_warn_details (name
, offset
, size
);
798 read_error_details (name
, offset
, size
);
802 readlink_diag (char const *name
)
804 if (ignore_failed_read_option
)
805 readlink_warn (name
);
807 readlink_error (name
);
811 savedir_diag (char const *name
)
813 if (ignore_failed_read_option
)
816 savedir_error (name
);
820 seek_diag_details (char const *name
, off_t offset
)
822 if (ignore_failed_read_option
)
823 seek_warn_details (name
, offset
);
825 seek_error_details (name
, offset
);
829 stat_diag (char const *name
)
831 if (ignore_failed_read_option
)
838 file_removed_diag (const char *name
, bool top_level
,
839 void (*diagfn
) (char const *name
))
841 if (!top_level
&& errno
== ENOENT
)
843 WARNOPT (WARN_FILE_REMOVED
,
844 (0, 0, _("%s: File removed before we read it"),
845 quotearg_colon (name
)));
846 set_exit_status (TAREXIT_DIFFERS
);
853 dir_removed_diag (const char *name
, bool top_level
,
854 void (*diagfn
) (char const *name
))
856 if (!top_level
&& errno
== ENOENT
)
858 WARNOPT (WARN_FILE_REMOVED
,
859 (0, 0, _("%s: Directory removed before we read it"),
860 quotearg_colon (name
)));
861 set_exit_status (TAREXIT_DIFFERS
);
868 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
870 write_error_details (name
, status
, size
);
874 /* Fork, aborting if unsuccessful. */
880 call_arg_fatal ("fork", _("child process"));
884 /* Create a pipe, aborting if unsuccessful. */
889 call_arg_fatal ("pipe", _("interprocess channel"));
892 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
893 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
894 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
898 ptr_align (void *ptr
, size_t alignment
)
901 char *p1
= p0
+ alignment
- 1;
902 return p1
- (size_t) p1
% alignment
;
905 /* Return the address of a page-aligned buffer of at least SIZE bytes.
906 The caller should free *PTR when done with the buffer. */
909 page_aligned_alloc (void **ptr
, size_t size
)
911 size_t alignment
= getpagesize ();
912 size_t size1
= size
+ alignment
;
915 *ptr
= xmalloc (size1
);
916 return ptr_align (*ptr
, alignment
);
923 char *buffer
; /* directory, `/', and directory member */
924 size_t buffer_size
; /* allocated size of name_buffer */
925 size_t dir_length
; /* length of directory part in buffer */
929 namebuf_create (const char *dir
)
931 namebuf_t buf
= xmalloc (sizeof (*buf
));
932 buf
->buffer_size
= strlen (dir
) + 2;
933 buf
->buffer
= xmalloc (buf
->buffer_size
);
934 strcpy (buf
->buffer
, dir
);
935 buf
->dir_length
= strlen (buf
->buffer
);
936 if (!ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
937 buf
->buffer
[buf
->dir_length
++] = DIRECTORY_SEPARATOR
;
942 namebuf_free (namebuf_t buf
)
949 namebuf_name (namebuf_t buf
, const char *name
)
951 size_t len
= strlen (name
);
952 while (buf
->dir_length
+ len
+ 1 >= buf
->buffer_size
)
953 buf
->buffer
= x2realloc (buf
->buffer
, &buf
->buffer_size
);
954 strcpy (buf
->buffer
+ buf
->dir_length
, name
);