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 FILE_NAME is obviously "." or "/". */
208 must_be_dot_or_slash (char const *file_name
)
210 file_name
+= FILESYSTEM_PREFIX_LEN (file_name
);
212 if (ISSLASH (file_name
[0]))
215 if (ISSLASH (file_name
[1]))
217 else if (file_name
[1] == '.'
218 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
219 file_name
+= 2 + (file_name
[2] == '.');
221 return ! file_name
[1];
225 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
228 while (ISSLASH (*file_name
))
232 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
236 /* Some implementations of rmdir let you remove '.' or '/'.
237 Report an error with errno set to zero for obvious cases of this;
238 otherwise call rmdir. */
240 safer_rmdir (const char *file_name
)
242 if (must_be_dot_or_slash (file_name
))
248 return rmdir (file_name
);
251 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
252 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
253 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
254 a directory that cannot be removed (e.g., because it is nonempty)
255 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
256 Return 0 on error, with errno set; if FILE_NAME is obviously the working
257 directory return zero with errno set to zero. */
259 remove_any_file (const char *file_name
, enum remove_option option
)
261 /* Try unlink first if we are not root, as this saves us a system
262 call in the common case where we're removing a non-directory. */
265 if (unlink (file_name
) == 0)
268 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
269 directory without appropriate privileges, but many Linux
270 kernels return the more-sensible EISDIR. */
271 if (errno
!= EPERM
&& errno
!= EISDIR
)
275 if (safer_rmdir (file_name
) == 0)
281 return we_are_root
&& unlink (file_name
) == 0;
285 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
290 case ORDINARY_REMOVE_OPTION
:
293 case WANT_DIRECTORY_REMOVE_OPTION
:
296 case RECURSIVE_REMOVE_OPTION
:
298 char *directory
= savedir (file_name
);
305 for (entry
= directory
;
306 (entrylen
= strlen (entry
)) != 0;
307 entry
+= entrylen
+ 1)
309 char *file_name_buffer
= new_name (file_name
, entry
);
310 int r
= remove_any_file (file_name_buffer
, 1);
312 free (file_name_buffer
);
323 return safer_rmdir (file_name
) == 0;
332 /* Check if FILE_NAME already exists and make a backup of it right now.
333 Return success (nonzero) only if the backup is either unneeded, or
334 successful. For now, directories are considered to never need
335 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
336 so, we do not have to backup block or character devices, nor remote
339 maybe_backup_file (const char *file_name
, int this_is_the_archive
)
341 struct stat file_stat
;
343 /* Check if we really need to backup the file. */
345 if (this_is_the_archive
&& _remdev (file_name
))
348 if (stat (file_name
, &file_stat
))
353 stat_error (file_name
);
357 if (S_ISDIR (file_stat
.st_mode
))
360 if (this_is_the_archive
361 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
364 assign_string (&before_backup_name
, file_name
);
366 /* A run situation may exist between Emacs or other GNU programs trying to
367 make a backup for the same file simultaneously. If theoretically
368 possible, real problems are unlikely. Doing any better would require a
369 convention, GNU-wide, for all programs doing backups. */
371 assign_string (&after_backup_name
, 0);
372 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
373 if (! after_backup_name
)
376 if (rename (before_backup_name
, after_backup_name
) == 0)
379 fprintf (stdlis
, _("Renaming %s to %s\n"),
380 quote_n (0, before_backup_name
),
381 quote_n (1, after_backup_name
));
386 /* The backup operation failed. */
388 ERROR ((0, e
, _("%s: Cannot rename to %s"),
389 quotearg_colon (before_backup_name
),
390 quote_n (1, after_backup_name
)));
391 assign_string (&after_backup_name
, 0);
396 /* Try to restore the recently backed up file to its original name.
397 This is usually only needed after a failed extraction. */
399 undo_last_backup (void)
401 if (after_backup_name
)
403 if (rename (after_backup_name
, before_backup_name
) != 0)
406 ERROR ((0, e
, _("%s: Cannot rename to %s"),
407 quotearg_colon (after_backup_name
),
408 quote_n (1, before_backup_name
)));
411 fprintf (stdlis
, _("Renaming %s back to %s\n"),
412 quote_n (0, after_backup_name
),
413 quote_n (1, before_backup_name
));
414 assign_string (&after_backup_name
, 0);
418 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
420 deref_stat (bool deref
, char const *name
, struct stat
*buf
)
422 return deref
? stat (name
, buf
) : lstat (name
, buf
);
425 /* A description of a working directory. */
430 struct saved_cwd saved_cwd
;
433 /* A vector of chdir targets. wd[0] is the initial working directory. */
434 static struct wd
*wd
;
436 /* The number of working directories in the vector. */
439 /* The allocated size of the vector. */
440 static size_t wd_alloc
;
442 /* DIR is the operand of a -C option; add it to vector of chdir targets,
443 and return the index of its location. */
445 chdir_arg (char const *dir
)
449 wd_alloc
= 2 * (wd_alloc
+ 1);
450 wd
= xrealloc (wd
, sizeof *wd
* wd_alloc
);
459 /* Optimize the common special case of the working directory,
460 or the working directory as a prefix. */
463 while (dir
[0] == '.' && ISSLASH (dir
[1]))
464 for (dir
+= 2; ISSLASH (*dir
); dir
++)
466 if (! dir
[dir
[0] == '.'])
475 /* Change to directory I. If I is 0, change to the initial working
476 directory; otherwise, I must be a value returned by chdir_arg. */
484 struct wd
*prev
= &wd
[previous
];
485 struct wd
*curr
= &wd
[i
];
490 if (save_cwd (&prev
->saved_cwd
) != 0)
491 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
496 if (restore_cwd (&curr
->saved_cwd
))
497 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
501 if (i
&& ! ISSLASH (curr
->name
[0]))
503 if (chdir (curr
->name
) != 0)
504 chdir_fatal (curr
->name
);
511 /* Decode MODE from its binary form in a stat structure, and encode it
512 into a 9-byte string STRING, terminated with a NUL. */
515 decode_mode (mode_t mode
, char *string
)
517 *string
++ = mode
& S_IRUSR
? 'r' : '-';
518 *string
++ = mode
& S_IWUSR
? 'w' : '-';
519 *string
++ = (mode
& S_ISUID
520 ? (mode
& S_IXUSR
? 's' : 'S')
521 : (mode
& S_IXUSR
? 'x' : '-'));
522 *string
++ = mode
& S_IRGRP
? 'r' : '-';
523 *string
++ = mode
& S_IWGRP
? 'w' : '-';
524 *string
++ = (mode
& S_ISGID
525 ? (mode
& S_IXGRP
? 's' : 'S')
526 : (mode
& S_IXGRP
? 'x' : '-'));
527 *string
++ = mode
& S_IROTH
? 'r' : '-';
528 *string
++ = mode
& S_IWOTH
? 'w' : '-';
529 *string
++ = (mode
& S_ISVTX
530 ? (mode
& S_IXOTH
? 't' : 'T')
531 : (mode
& S_IXOTH
? 'x' : '-'));
535 /* Report an error associated with the system call CALL and the
536 optional name NAME. */
538 call_arg_error (char const *call
, char const *name
)
541 ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
544 /* Report a fatal error associated with the system call CALL and
545 the optional file name NAME. */
547 call_arg_fatal (char const *call
, char const *name
)
550 FATAL_ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
553 /* Report a warning associated with the system call CALL and
554 the optional file name NAME. */
556 call_arg_warn (char const *call
, char const *name
)
559 WARN ((0, e
, _("%s: Warning: Cannot %s"), quotearg_colon (name
), call
));
563 chdir_fatal (char const *name
)
565 call_arg_fatal ("chdir", name
);
569 chmod_error_details (char const *name
, mode_t mode
)
573 decode_mode (mode
, buf
);
574 ERROR ((0, e
, _("%s: Cannot change mode to %s"),
575 quotearg_colon (name
), buf
));
579 chown_error_details (char const *name
, uid_t uid
, gid_t gid
)
582 ERROR ((0, e
, _("%s: Cannot change ownership to uid %lu, gid %lu"),
583 quotearg_colon (name
), (unsigned long) uid
, (unsigned long) gid
));
587 close_error (char const *name
)
589 call_arg_error ("close", name
);
593 close_warn (char const *name
)
595 call_arg_warn ("close", name
);
599 close_diag (char const *name
)
601 if (ignore_failed_read_option
)
608 exec_fatal (char const *name
)
610 call_arg_fatal ("exec", name
);
614 link_error (char const *target
, char const *source
)
617 ERROR ((0, e
, _("%s: Cannot hard link to %s"),
618 quotearg_colon (source
), quote_n (1, target
)));
622 mkdir_error (char const *name
)
624 call_arg_error ("mkdir", name
);
628 mkfifo_error (char const *name
)
630 call_arg_error ("mkfifo", name
);
634 mknod_error (char const *name
)
636 call_arg_error ("mknod", name
);
640 open_error (char const *name
)
642 call_arg_error ("open", name
);
646 open_fatal (char const *name
)
648 call_arg_fatal ("open", name
);
652 open_warn (char const *name
)
654 call_arg_warn ("open", name
);
658 open_diag (char const *name
)
660 if (ignore_failed_read_option
)
667 read_error (char const *name
)
669 call_arg_error ("read", name
);
673 read_error_details (char const *name
, off_t offset
, size_t size
)
675 char buf
[UINTMAX_STRSIZE_BOUND
];
678 ngettext ("%s: Read error at byte %s, reading %lu byte",
679 "%s: Read error at byte %s, reading %lu bytes",
681 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
682 (unsigned long) size
));
686 read_warn_details (char const *name
, off_t offset
, size_t size
)
688 char buf
[UINTMAX_STRSIZE_BOUND
];
691 ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
692 "%s: Warning: Read error at byte %s, reading %lu bytes",
694 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
695 (unsigned long) size
));
699 read_diag_details (char const *name
, off_t offset
, size_t size
)
701 if (ignore_failed_read_option
)
702 read_warn_details (name
, offset
, size
);
704 read_error_details (name
, offset
, size
);
708 read_fatal (char const *name
)
710 call_arg_fatal ("read", name
);
714 read_fatal_details (char const *name
, off_t offset
, size_t size
)
716 char buf
[UINTMAX_STRSIZE_BOUND
];
719 ngettext ("%s: Read error at byte %s, reading %lu byte",
720 "%s: Read error at byte %s, reading %lu bytes",
722 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
723 (unsigned long) size
));
727 readlink_error (char const *name
)
729 call_arg_error ("readlink", name
);
733 readlink_warn (char const *name
)
735 call_arg_warn ("readlink", name
);
739 readlink_diag (char const *name
)
741 if (ignore_failed_read_option
)
742 readlink_warn (name
);
744 readlink_error (name
);
748 savedir_error (char const *name
)
750 call_arg_error ("savedir", name
);
754 savedir_warn (char const *name
)
756 call_arg_warn ("savedir", name
);
760 savedir_diag (char const *name
)
762 if (ignore_failed_read_option
)
765 savedir_error (name
);
769 seek_error (char const *name
)
771 call_arg_error ("seek", name
);
775 seek_error_details (char const *name
, off_t offset
)
777 char buf
[UINTMAX_STRSIZE_BOUND
];
779 ERROR ((0, e
, _("%s: Cannot seek to %s"),
780 quotearg_colon (name
),
781 STRINGIFY_BIGINT (offset
, buf
)));
785 seek_warn (char const *name
)
787 call_arg_warn ("seek", name
);
791 seek_warn_details (char const *name
, off_t offset
)
793 char buf
[UINTMAX_STRSIZE_BOUND
];
795 WARN ((0, e
, _("%s: Warning: Cannot seek to %s"),
796 quotearg_colon (name
),
797 STRINGIFY_BIGINT (offset
, buf
)));
801 seek_diag_details (char const *name
, off_t offset
)
803 if (ignore_failed_read_option
)
804 seek_warn_details (name
, offset
);
806 seek_error_details (name
, offset
);
810 symlink_error (char const *contents
, char const *name
)
813 ERROR ((0, e
, _("%s: Cannot create symlink to %s"),
814 quotearg_colon (name
), quote_n (1, contents
)));
818 stat_error (char const *name
)
820 call_arg_error ("stat", name
);
824 stat_warn (char const *name
)
826 call_arg_warn ("stat", name
);
830 stat_diag (char const *name
)
832 if (ignore_failed_read_option
)
839 truncate_error (char const *name
)
841 call_arg_error ("truncate", name
);
845 truncate_warn (char const *name
)
847 call_arg_warn ("truncate", name
);
851 unlink_error (char const *name
)
853 call_arg_error ("unlink", name
);
857 utime_error (char const *name
)
859 call_arg_error ("utime", name
);
863 waitpid_error (char const *name
)
865 call_arg_error ("waitpid", name
);
869 write_error (char const *name
)
871 call_arg_error ("write", name
);
875 write_error_details (char const *name
, size_t status
, size_t size
)
881 ngettext ("%s: Wrote only %lu of %lu byte",
882 "%s: Wrote only %lu of %lu bytes",
884 name
, (unsigned long int) status
, (unsigned long int) size
));
888 write_fatal (char const *name
)
890 call_arg_fatal ("write", name
);
894 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
896 write_error_details (name
, status
, size
);
901 /* Fork, aborting if unsuccessful. */
907 call_arg_fatal ("fork", _("child process"));
911 /* Create a pipe, aborting if unsuccessful. */
916 call_arg_fatal ("pipe", _("interprocess channel"));
919 /* Return an unambiguous printable representation, allocated in slot N,
920 for NAME, suitable for diagnostics. */
922 quote_n (int n
, char const *name
)
924 return quotearg_n_style (n
, locale_quoting_style
, name
);
927 /* Return an unambiguous printable representation of NAME, suitable
930 quote (char const *name
)
932 return quote_n (0, name
);