1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004 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 2, 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 static void call_arg_fatal (char const *, char const *)
27 __attribute__ ((noreturn
));
29 /* Handling strings. */
31 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
32 STRING was nonzero, it is freed first. */
34 assign_string (char **string
, const char *value
)
38 *string
= value
? xstrdup (value
) : 0;
41 /* Allocate a copy of the string quoted as in C, and returns that. If
42 the string does not have to be quoted, it returns a null pointer.
43 The allocated copy should normally be freed with free() after the
44 caller is done with it.
46 This is used in one context only: generating the directory file in
47 incremental dumps. The quoted string is not intended for human
48 consumption; it is intended only for unquote_string. The quoting
49 is locale-independent, so that users needn't worry about locale
50 when reading directory files. This means that we can't use
51 quotearg, as quotearg is locale-dependent and is meant for human
54 quote_copy_string (const char *string
)
56 const char *source
= string
;
57 char *destination
= 0;
63 int character
= *source
++;
70 size_t length
= (source
- string
) - 1;
73 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
74 memcpy (buffer
, string
, length
);
75 destination
= buffer
+ length
;
77 *destination
++ = '\\';
78 *destination
++ = character
== '\\' ? '\\' : 'n';
83 *destination
++ = character
;
95 /* Takes a quoted C string (like those produced by quote_copy_string)
96 and turns it back into the un-quoted original. This is done in
97 place. Returns 0 only if the string was not properly quoted, but
98 completes the unquoting anyway.
100 This is used for reading the saved directory file in incremental
101 dumps. It is used for decoding old `N' records (demangling names).
102 But also, it is used for decoding file arguments, would they come
103 from the shell or a -T file, and for decoding the --exclude
106 unquote_string (char *string
)
109 char *source
= string
;
110 char *destination
= string
;
112 /* Escape sequences other than \\ and \n are no longer generated by
113 quote_copy_string, but accept them for backwards compatibility,
114 and also because unquote_string is used for purposes other than
115 parsing the output of quote_copy_string. */
122 *destination
++ = '\\';
127 *destination
++ = '\n';
132 *destination
++ = '\t';
137 *destination
++ = '\f';
142 *destination
++ = '\b';
147 *destination
++ = '\r';
152 *destination
++ = 0177;
165 int value
= *source
++ - '0';
167 if (*source
< '0' || *source
> '7')
169 *destination
++ = value
;
172 value
= value
* 8 + *source
++ - '0';
173 if (*source
< '0' || *source
> '7')
175 *destination
++ = value
;
178 value
= value
* 8 + *source
++ - '0';
179 *destination
++ = value
;
185 *destination
++ = '\\';
187 *destination
++ = *source
++;
190 else if (source
!= destination
)
191 *destination
++ = *source
++;
193 source
++, destination
++;
195 if (source
!= destination
)
202 /* Saved names in case backup needs to be undone. */
203 static char *before_backup_name
;
204 static char *after_backup_name
;
206 /* Return 1 if PATH is obviously "." or "/". */
208 must_be_dot_or_slash (char const *path
)
210 path
+= FILESYSTEM_PREFIX_LEN (path
);
212 if (ISSLASH (path
[0]))
215 if (ISSLASH (path
[1]))
217 else if (path
[1] == '.' && ISSLASH (path
[2 + (path
[2] == '.')]))
218 path
+= 2 + (path
[2] == '.');
224 while (path
[0] == '.' && ISSLASH (path
[1]))
227 while (ISSLASH (*path
))
231 return ! path
[0] || (path
[0] == '.' && ! path
[1]);
235 /* Some implementations of rmdir let you remove '.' or '/'.
236 Report an error with errno set to zero for obvious cases of this;
237 otherwise call rmdir. */
239 safer_rmdir (const char *path
)
241 if (must_be_dot_or_slash (path
))
250 /* Remove PATH, returning 1 on success. If PATH is a directory, then
251 if OPTION is RECURSIVE_REMOVE_OPTION is set remove PATH
252 recursively; otherwise, remove it only if it is empty. If PATH is
253 a directory that cannot be removed (e.g., because it is nonempty)
254 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
255 Return 0 on error, with errno set; if PATH is obviously the working
256 directory return zero with errno set to zero. */
258 remove_any_file (const char *path
, enum remove_option option
)
260 /* Try unlink first if we are not root, as this saves us a system
261 call in the common case where we're removing a non-directory. */
264 if (unlink (path
) == 0)
267 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
268 directory without appropriate privileges, but many Linux
269 kernels return the more-sensible EISDIR. */
270 if (errno
!= EPERM
&& errno
!= EISDIR
)
274 if (safer_rmdir (path
) == 0)
280 return we_are_root
&& unlink (path
) == 0;
284 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
289 case ORDINARY_REMOVE_OPTION
:
292 case WANT_DIRECTORY_REMOVE_OPTION
:
295 case RECURSIVE_REMOVE_OPTION
:
297 char *directory
= savedir (path
);
304 for (entry
= directory
;
305 (entrylen
= strlen (entry
)) != 0;
306 entry
+= entrylen
+ 1)
308 char *path_buffer
= new_name (path
, entry
);
309 int r
= remove_any_file (path_buffer
, 1);
322 return safer_rmdir (path
) == 0;
331 /* Check if PATH already exists and make a backup of it right now.
332 Return success (nonzero) only if the backup is either unneeded, or
333 successful. For now, directories are considered to never need
334 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
335 so, we do not have to backup block or character devices, nor remote
338 maybe_backup_file (const char *path
, int this_is_the_archive
)
340 struct stat file_stat
;
342 /* Check if we really need to backup the file. */
344 if (this_is_the_archive
&& _remdev (path
))
347 if (stat (path
, &file_stat
))
356 if (S_ISDIR (file_stat
.st_mode
))
359 if (this_is_the_archive
360 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
363 assign_string (&before_backup_name
, path
);
365 /* A run situation may exist between Emacs or other GNU programs trying to
366 make a backup for the same file simultaneously. If theoretically
367 possible, real problems are unlikely. Doing any better would require a
368 convention, GNU-wide, for all programs doing backups. */
370 assign_string (&after_backup_name
, 0);
371 after_backup_name
= find_backup_file_name (path
, backup_type
);
372 if (! after_backup_name
)
375 if (rename (before_backup_name
, after_backup_name
) == 0)
378 fprintf (stdlis
, _("Renaming %s to %s\n"),
379 quote_n (0, before_backup_name
),
380 quote_n (1, after_backup_name
));
385 /* The backup operation failed. */
387 ERROR ((0, e
, _("%s: Cannot rename to %s"),
388 quotearg_colon (before_backup_name
),
389 quote_n (1, after_backup_name
)));
390 assign_string (&after_backup_name
, 0);
395 /* Try to restore the recently backed up file to its original name.
396 This is usually only needed after a failed extraction. */
398 undo_last_backup (void)
400 if (after_backup_name
)
402 if (rename (after_backup_name
, before_backup_name
) != 0)
405 ERROR ((0, e
, _("%s: Cannot rename to %s"),
406 quotearg_colon (after_backup_name
),
407 quote_n (1, before_backup_name
)));
410 fprintf (stdlis
, _("Renaming %s back to %s\n"),
411 quote_n (0, after_backup_name
),
412 quote_n (1, before_backup_name
));
413 assign_string (&after_backup_name
, 0);
417 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
419 deref_stat (bool deref
, char const *name
, struct stat
*buf
)
421 return deref
? stat (name
, buf
) : lstat (name
, buf
);
424 /* A description of a working directory. */
429 struct saved_cwd saved_cwd
;
432 /* A vector of chdir targets. wd[0] is the initial working directory. */
433 static struct wd
*wd
;
435 /* The number of working directories in the vector. */
438 /* The allocated size of the vector. */
439 static size_t wd_alloc
;
441 /* DIR is the operand of a -C option; add it to vector of chdir targets,
442 and return the index of its location. */
444 chdir_arg (char const *dir
)
448 wd_alloc
= 2 * (wd_alloc
+ 1);
449 wd
= xrealloc (wd
, sizeof *wd
* wd_alloc
);
458 /* Optimize the common special case of the working directory,
459 or the working directory as a prefix. */
462 while (dir
[0] == '.' && ISSLASH (dir
[1]))
463 for (dir
+= 2; ISSLASH (*dir
); dir
++)
465 if (! dir
[dir
[0] == '.'])
474 /* Change to directory I. If I is 0, change to the initial working
475 directory; otherwise, I must be a value returned by chdir_arg. */
483 struct wd
*prev
= &wd
[previous
];
484 struct wd
*curr
= &wd
[i
];
489 if (save_cwd (&prev
->saved_cwd
) != 0)
490 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
495 if (restore_cwd (&curr
->saved_cwd
))
496 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
500 if (i
&& ! ISSLASH (curr
->name
[0]))
502 if (chdir (curr
->name
) != 0)
503 chdir_fatal (curr
->name
);
510 /* Decode MODE from its binary form in a stat structure, and encode it
511 into a 9-byte string STRING, terminated with a NUL. */
514 decode_mode (mode_t mode
, char *string
)
516 *string
++ = mode
& S_IRUSR
? 'r' : '-';
517 *string
++ = mode
& S_IWUSR
? 'w' : '-';
518 *string
++ = (mode
& S_ISUID
519 ? (mode
& S_IXUSR
? 's' : 'S')
520 : (mode
& S_IXUSR
? 'x' : '-'));
521 *string
++ = mode
& S_IRGRP
? 'r' : '-';
522 *string
++ = mode
& S_IWGRP
? 'w' : '-';
523 *string
++ = (mode
& S_ISGID
524 ? (mode
& S_IXGRP
? 's' : 'S')
525 : (mode
& S_IXGRP
? 'x' : '-'));
526 *string
++ = mode
& S_IROTH
? 'r' : '-';
527 *string
++ = mode
& S_IWOTH
? 'w' : '-';
528 *string
++ = (mode
& S_ISVTX
529 ? (mode
& S_IXOTH
? 't' : 'T')
530 : (mode
& S_IXOTH
? 'x' : '-'));
534 /* Report an error associated with the system call CALL and the
535 optional name NAME. */
537 call_arg_error (char const *call
, char const *name
)
540 ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
543 /* Report a fatal error associated with the system call CALL and
544 the optional file name NAME. */
546 call_arg_fatal (char const *call
, char const *name
)
549 FATAL_ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
552 /* Report a warning associated with the system call CALL and
553 the optional file name NAME. */
555 call_arg_warn (char const *call
, char const *name
)
558 WARN ((0, e
, _("%s: Warning: Cannot %s"), quotearg_colon (name
), call
));
562 chdir_fatal (char const *name
)
564 call_arg_fatal ("chdir", name
);
568 chmod_error_details (char const *name
, mode_t mode
)
572 decode_mode (mode
, buf
);
573 ERROR ((0, e
, _("%s: Cannot change mode to %s"),
574 quotearg_colon (name
), buf
));
578 chown_error_details (char const *name
, uid_t uid
, gid_t gid
)
581 ERROR ((0, e
, _("%s: Cannot change ownership to uid %lu, gid %lu"),
582 quotearg_colon (name
), (unsigned long) uid
, (unsigned long) gid
));
586 close_error (char const *name
)
588 call_arg_error ("close", name
);
592 close_warn (char const *name
)
594 call_arg_warn ("close", name
);
598 close_diag (char const *name
)
600 if (ignore_failed_read_option
)
607 exec_fatal (char const *name
)
609 call_arg_fatal ("exec", name
);
613 link_error (char const *target
, char const *source
)
616 ERROR ((0, e
, _("%s: Cannot hard link to %s"),
617 quotearg_colon (source
), quote_n (1, target
)));
621 mkdir_error (char const *name
)
623 call_arg_error ("mkdir", name
);
627 mkfifo_error (char const *name
)
629 call_arg_error ("mkfifo", name
);
633 mknod_error (char const *name
)
635 call_arg_error ("mknod", name
);
639 open_error (char const *name
)
641 call_arg_error ("open", name
);
645 open_fatal (char const *name
)
647 call_arg_fatal ("open", name
);
651 open_warn (char const *name
)
653 call_arg_warn ("open", name
);
657 open_diag (char const *name
)
659 if (ignore_failed_read_option
)
666 read_error (char const *name
)
668 call_arg_error ("read", name
);
672 read_error_details (char const *name
, off_t offset
, size_t size
)
674 char buf
[UINTMAX_STRSIZE_BOUND
];
677 ngettext ("%s: Read error at byte %s, reading %lu byte",
678 "%s: Read error at byte %s, reading %lu bytes",
680 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
681 (unsigned long) size
));
685 read_warn_details (char const *name
, off_t offset
, size_t size
)
687 char buf
[UINTMAX_STRSIZE_BOUND
];
690 ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
691 "%s: Warning: Read error at byte %s, reading %lu bytes",
693 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
694 (unsigned long) size
));
698 read_diag_details (char const *name
, off_t offset
, size_t size
)
700 if (ignore_failed_read_option
)
701 read_warn_details (name
, offset
, size
);
703 read_error_details (name
, offset
, size
);
707 read_fatal (char const *name
)
709 call_arg_fatal ("read", name
);
713 read_fatal_details (char const *name
, off_t offset
, size_t size
)
715 char buf
[UINTMAX_STRSIZE_BOUND
];
718 ngettext ("%s: Read error at byte %s, reading %lu byte",
719 "%s: Read error at byte %s, reading %lu bytes",
721 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
722 (unsigned long) size
));
726 readlink_error (char const *name
)
728 call_arg_error ("readlink", name
);
732 readlink_warn (char const *name
)
734 call_arg_warn ("readlink", name
);
738 readlink_diag (char const *name
)
740 if (ignore_failed_read_option
)
741 readlink_warn (name
);
743 readlink_error (name
);
747 savedir_error (char const *name
)
749 call_arg_error ("savedir", name
);
753 savedir_warn (char const *name
)
755 call_arg_warn ("savedir", name
);
759 savedir_diag (char const *name
)
761 if (ignore_failed_read_option
)
764 savedir_error (name
);
768 seek_error (char const *name
)
770 call_arg_error ("seek", name
);
774 seek_error_details (char const *name
, off_t offset
)
776 char buf
[UINTMAX_STRSIZE_BOUND
];
778 ERROR ((0, e
, _("%s: Cannot seek to %s"),
779 quotearg_colon (name
),
780 STRINGIFY_BIGINT (offset
, buf
)));
784 seek_warn (char const *name
)
786 call_arg_warn ("seek", name
);
790 seek_warn_details (char const *name
, off_t offset
)
792 char buf
[UINTMAX_STRSIZE_BOUND
];
794 WARN ((0, e
, _("%s: Warning: Cannot seek to %s"),
795 quotearg_colon (name
),
796 STRINGIFY_BIGINT (offset
, buf
)));
800 seek_diag_details (char const *name
, off_t offset
)
802 if (ignore_failed_read_option
)
803 seek_warn_details (name
, offset
);
805 seek_error_details (name
, offset
);
809 symlink_error (char const *contents
, char const *name
)
812 ERROR ((0, e
, _("%s: Cannot create symlink to %s"),
813 quotearg_colon (name
), quote_n (1, contents
)));
817 stat_error (char const *name
)
819 call_arg_error ("stat", name
);
823 stat_warn (char const *name
)
825 call_arg_warn ("stat", name
);
829 stat_diag (char const *name
)
831 if (ignore_failed_read_option
)
838 truncate_error (char const *name
)
840 call_arg_error ("truncate", name
);
844 truncate_warn (char const *name
)
846 call_arg_warn ("truncate", name
);
850 unlink_error (char const *name
)
852 call_arg_error ("unlink", name
);
856 utime_error (char const *name
)
858 call_arg_error ("utime", name
);
862 waitpid_error (char const *name
)
864 call_arg_error ("waitpid", name
);
868 write_error (char const *name
)
870 call_arg_error ("write", name
);
874 write_error_details (char const *name
, size_t status
, size_t size
)
880 ngettext ("%s: Wrote only %lu of %lu byte",
881 "%s: Wrote only %lu of %lu bytes",
883 name
, (unsigned long int) status
, (unsigned long int) size
));
887 write_fatal (char const *name
)
889 call_arg_fatal ("write", name
);
893 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
895 write_error_details (name
, status
, size
);
900 /* Fork, aborting if unsuccessful. */
906 call_arg_fatal ("fork", _("child process"));
910 /* Create a pipe, aborting if unsuccessful. */
915 call_arg_fatal ("pipe", _("interprocess channel"));
918 /* Return an unambiguous printable representation, allocated in slot N,
919 for NAME, suitable for diagnostics. */
921 quote_n (int n
, char const *name
)
923 return quotearg_n_style (n
, locale_quoting_style
, name
);
926 /* Return an unambiguous printable representation of NAME, suitable
929 quote (char const *name
)
931 return quote_n (0, name
);