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
++ = '\a';
132 *destination
++ = '\b';
137 *destination
++ = '\f';
142 *destination
++ = '\n';
147 *destination
++ = '\r';
152 *destination
++ = '\t';
157 *destination
++ = '\v';
162 *destination
++ = 0177;
175 int value
= *source
++ - '0';
177 if (*source
< '0' || *source
> '7')
179 *destination
++ = value
;
182 value
= value
* 8 + *source
++ - '0';
183 if (*source
< '0' || *source
> '7')
185 *destination
++ = value
;
188 value
= value
* 8 + *source
++ - '0';
189 *destination
++ = value
;
195 *destination
++ = '\\';
197 *destination
++ = *source
++;
200 else if (source
!= destination
)
201 *destination
++ = *source
++;
203 source
++, destination
++;
205 if (source
!= destination
)
212 /* Saved names in case backup needs to be undone. */
213 static char *before_backup_name
;
214 static char *after_backup_name
;
216 /* Return 1 if FILE_NAME is obviously "." or "/". */
218 must_be_dot_or_slash (char const *file_name
)
220 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
222 if (ISSLASH (file_name
[0]))
225 if (ISSLASH (file_name
[1]))
227 else if (file_name
[1] == '.'
228 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
229 file_name
+= 2 + (file_name
[2] == '.');
231 return ! file_name
[1];
235 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
238 while (ISSLASH (*file_name
))
242 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
246 /* Some implementations of rmdir let you remove '.' or '/'.
247 Report an error with errno set to zero for obvious cases of this;
248 otherwise call rmdir. */
250 safer_rmdir (const char *file_name
)
252 if (must_be_dot_or_slash (file_name
))
258 return rmdir (file_name
);
261 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
262 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
263 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
264 a directory that cannot be removed (e.g., because it is nonempty)
265 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
266 Return 0 on error, with errno set; if FILE_NAME is obviously the working
267 directory return zero with errno set to zero. */
269 remove_any_file (const char *file_name
, enum remove_option option
)
271 /* Try unlink first if we are not root, as this saves us a system
272 call in the common case where we're removing a non-directory. */
275 if (unlink (file_name
) == 0)
278 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
279 directory without appropriate privileges, but many Linux
280 kernels return the more-sensible EISDIR. */
281 if (errno
!= EPERM
&& errno
!= EISDIR
)
285 if (safer_rmdir (file_name
) == 0)
291 return we_are_root
&& unlink (file_name
) == 0;
295 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
300 case ORDINARY_REMOVE_OPTION
:
303 case WANT_DIRECTORY_REMOVE_OPTION
:
306 case RECURSIVE_REMOVE_OPTION
:
308 char *directory
= savedir (file_name
);
315 for (entry
= directory
;
316 (entrylen
= strlen (entry
)) != 0;
317 entry
+= entrylen
+ 1)
319 char *file_name_buffer
= new_name (file_name
, entry
);
320 int r
= remove_any_file (file_name_buffer
, 1);
322 free (file_name_buffer
);
333 return safer_rmdir (file_name
) == 0;
342 /* Check if FILE_NAME already exists and make a backup of it right now.
343 Return success (nonzero) only if the backup is either unneeded, or
344 successful. For now, directories are considered to never need
345 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
346 so, we do not have to backup block or character devices, nor remote
349 maybe_backup_file (const char *file_name
, int this_is_the_archive
)
351 struct stat file_stat
;
353 /* Check if we really need to backup the file. */
355 if (this_is_the_archive
&& _remdev (file_name
))
358 if (stat (file_name
, &file_stat
))
363 stat_error (file_name
);
367 if (S_ISDIR (file_stat
.st_mode
))
370 if (this_is_the_archive
371 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
374 assign_string (&before_backup_name
, file_name
);
376 /* A run situation may exist between Emacs or other GNU programs trying to
377 make a backup for the same file simultaneously. If theoretically
378 possible, real problems are unlikely. Doing any better would require a
379 convention, GNU-wide, for all programs doing backups. */
381 assign_string (&after_backup_name
, 0);
382 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
383 if (! after_backup_name
)
386 if (rename (before_backup_name
, after_backup_name
) == 0)
389 fprintf (stdlis
, _("Renaming %s to %s\n"),
390 quote_n (0, before_backup_name
),
391 quote_n (1, after_backup_name
));
396 /* The backup operation failed. */
398 ERROR ((0, e
, _("%s: Cannot rename to %s"),
399 quotearg_colon (before_backup_name
),
400 quote_n (1, after_backup_name
)));
401 assign_string (&after_backup_name
, 0);
406 /* Try to restore the recently backed up file to its original name.
407 This is usually only needed after a failed extraction. */
409 undo_last_backup (void)
411 if (after_backup_name
)
413 if (rename (after_backup_name
, before_backup_name
) != 0)
416 ERROR ((0, e
, _("%s: Cannot rename to %s"),
417 quotearg_colon (after_backup_name
),
418 quote_n (1, before_backup_name
)));
421 fprintf (stdlis
, _("Renaming %s back to %s\n"),
422 quote_n (0, after_backup_name
),
423 quote_n (1, before_backup_name
));
424 assign_string (&after_backup_name
, 0);
428 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
430 deref_stat (bool deref
, char const *name
, struct stat
*buf
)
432 return deref
? stat (name
, buf
) : lstat (name
, buf
);
435 /* A description of a working directory. */
440 struct saved_cwd saved_cwd
;
443 /* A vector of chdir targets. wd[0] is the initial working directory. */
444 static struct wd
*wd
;
446 /* The number of working directories in the vector. */
449 /* The allocated size of the vector. */
450 static size_t wd_alloc
;
452 /* DIR is the operand of a -C option; add it to vector of chdir targets,
453 and return the index of its location. */
455 chdir_arg (char const *dir
)
459 wd_alloc
= 2 * (wd_alloc
+ 1);
460 wd
= xrealloc (wd
, sizeof *wd
* wd_alloc
);
469 /* Optimize the common special case of the working directory,
470 or the working directory as a prefix. */
473 while (dir
[0] == '.' && ISSLASH (dir
[1]))
474 for (dir
+= 2; ISSLASH (*dir
); dir
++)
476 if (! dir
[dir
[0] == '.'])
485 /* Change to directory I. If I is 0, change to the initial working
486 directory; otherwise, I must be a value returned by chdir_arg. */
494 struct wd
*prev
= &wd
[previous
];
495 struct wd
*curr
= &wd
[i
];
500 if (save_cwd (&prev
->saved_cwd
) != 0)
501 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
506 if (restore_cwd (&curr
->saved_cwd
))
507 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
511 if (i
&& ! ISSLASH (curr
->name
[0]))
513 if (chdir (curr
->name
) != 0)
514 chdir_fatal (curr
->name
);
521 /* Decode MODE from its binary form in a stat structure, and encode it
522 into a 9-byte string STRING, terminated with a NUL. */
525 decode_mode (mode_t mode
, char *string
)
527 *string
++ = mode
& S_IRUSR
? 'r' : '-';
528 *string
++ = mode
& S_IWUSR
? 'w' : '-';
529 *string
++ = (mode
& S_ISUID
530 ? (mode
& S_IXUSR
? 's' : 'S')
531 : (mode
& S_IXUSR
? 'x' : '-'));
532 *string
++ = mode
& S_IRGRP
? 'r' : '-';
533 *string
++ = mode
& S_IWGRP
? 'w' : '-';
534 *string
++ = (mode
& S_ISGID
535 ? (mode
& S_IXGRP
? 's' : 'S')
536 : (mode
& S_IXGRP
? 'x' : '-'));
537 *string
++ = mode
& S_IROTH
? 'r' : '-';
538 *string
++ = mode
& S_IWOTH
? 'w' : '-';
539 *string
++ = (mode
& S_ISVTX
540 ? (mode
& S_IXOTH
? 't' : 'T')
541 : (mode
& S_IXOTH
? 'x' : '-'));
545 /* Report an error associated with the system call CALL and the
546 optional name NAME. */
548 call_arg_error (char const *call
, char const *name
)
551 ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
554 /* Report a fatal error associated with the system call CALL and
555 the optional file name NAME. */
557 call_arg_fatal (char const *call
, char const *name
)
560 FATAL_ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
563 /* Report a warning associated with the system call CALL and
564 the optional file name NAME. */
566 call_arg_warn (char const *call
, char const *name
)
569 WARN ((0, e
, _("%s: Warning: Cannot %s"), quotearg_colon (name
), call
));
573 chdir_fatal (char const *name
)
575 call_arg_fatal ("chdir", name
);
579 chmod_error_details (char const *name
, mode_t mode
)
583 decode_mode (mode
, buf
);
584 ERROR ((0, e
, _("%s: Cannot change mode to %s"),
585 quotearg_colon (name
), buf
));
589 chown_error_details (char const *name
, uid_t uid
, gid_t gid
)
592 ERROR ((0, e
, _("%s: Cannot change ownership to uid %lu, gid %lu"),
593 quotearg_colon (name
), (unsigned long) uid
, (unsigned long) gid
));
597 close_error (char const *name
)
599 call_arg_error ("close", name
);
603 close_warn (char const *name
)
605 call_arg_warn ("close", name
);
609 close_diag (char const *name
)
611 if (ignore_failed_read_option
)
618 exec_fatal (char const *name
)
620 call_arg_fatal ("exec", name
);
624 link_error (char const *target
, char const *source
)
627 ERROR ((0, e
, _("%s: Cannot hard link to %s"),
628 quotearg_colon (source
), quote_n (1, target
)));
632 mkdir_error (char const *name
)
634 call_arg_error ("mkdir", name
);
638 mkfifo_error (char const *name
)
640 call_arg_error ("mkfifo", name
);
644 mknod_error (char const *name
)
646 call_arg_error ("mknod", name
);
650 open_error (char const *name
)
652 call_arg_error ("open", name
);
656 open_fatal (char const *name
)
658 call_arg_fatal ("open", name
);
662 open_warn (char const *name
)
664 call_arg_warn ("open", name
);
668 open_diag (char const *name
)
670 if (ignore_failed_read_option
)
677 read_error (char const *name
)
679 call_arg_error ("read", name
);
683 read_error_details (char const *name
, off_t offset
, size_t size
)
685 char buf
[UINTMAX_STRSIZE_BOUND
];
688 ngettext ("%s: Read error at byte %s, reading %lu byte",
689 "%s: Read error at byte %s, reading %lu bytes",
691 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
692 (unsigned long) size
));
696 read_warn_details (char const *name
, off_t offset
, size_t size
)
698 char buf
[UINTMAX_STRSIZE_BOUND
];
701 ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
702 "%s: Warning: Read error at byte %s, reading %lu bytes",
704 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
705 (unsigned long) size
));
709 read_diag_details (char const *name
, off_t offset
, size_t size
)
711 if (ignore_failed_read_option
)
712 read_warn_details (name
, offset
, size
);
714 read_error_details (name
, offset
, size
);
718 read_fatal (char const *name
)
720 call_arg_fatal ("read", name
);
724 read_fatal_details (char const *name
, off_t offset
, size_t size
)
726 char buf
[UINTMAX_STRSIZE_BOUND
];
729 ngettext ("%s: Read error at byte %s, reading %lu byte",
730 "%s: Read error at byte %s, reading %lu bytes",
732 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
733 (unsigned long) size
));
737 readlink_error (char const *name
)
739 call_arg_error ("readlink", name
);
743 readlink_warn (char const *name
)
745 call_arg_warn ("readlink", name
);
749 readlink_diag (char const *name
)
751 if (ignore_failed_read_option
)
752 readlink_warn (name
);
754 readlink_error (name
);
758 savedir_error (char const *name
)
760 call_arg_error ("savedir", name
);
764 savedir_warn (char const *name
)
766 call_arg_warn ("savedir", name
);
770 savedir_diag (char const *name
)
772 if (ignore_failed_read_option
)
775 savedir_error (name
);
779 seek_error (char const *name
)
781 call_arg_error ("seek", name
);
785 seek_error_details (char const *name
, off_t offset
)
787 char buf
[UINTMAX_STRSIZE_BOUND
];
789 ERROR ((0, e
, _("%s: Cannot seek to %s"),
790 quotearg_colon (name
),
791 STRINGIFY_BIGINT (offset
, buf
)));
795 seek_warn (char const *name
)
797 call_arg_warn ("seek", name
);
801 seek_warn_details (char const *name
, off_t offset
)
803 char buf
[UINTMAX_STRSIZE_BOUND
];
805 WARN ((0, e
, _("%s: Warning: Cannot seek to %s"),
806 quotearg_colon (name
),
807 STRINGIFY_BIGINT (offset
, buf
)));
811 seek_diag_details (char const *name
, off_t offset
)
813 if (ignore_failed_read_option
)
814 seek_warn_details (name
, offset
);
816 seek_error_details (name
, offset
);
820 symlink_error (char const *contents
, char const *name
)
823 ERROR ((0, e
, _("%s: Cannot create symlink to %s"),
824 quotearg_colon (name
), quote_n (1, contents
)));
828 stat_error (char const *name
)
830 call_arg_error ("stat", name
);
834 stat_warn (char const *name
)
836 call_arg_warn ("stat", name
);
840 stat_diag (char const *name
)
842 if (ignore_failed_read_option
)
849 truncate_error (char const *name
)
851 call_arg_error ("truncate", name
);
855 truncate_warn (char const *name
)
857 call_arg_warn ("truncate", name
);
861 unlink_error (char const *name
)
863 call_arg_error ("unlink", name
);
867 utime_error (char const *name
)
869 call_arg_error ("utime", name
);
873 waitpid_error (char const *name
)
875 call_arg_error ("waitpid", name
);
879 write_error (char const *name
)
881 call_arg_error ("write", name
);
885 write_error_details (char const *name
, size_t status
, size_t size
)
891 ngettext ("%s: Wrote only %lu of %lu byte",
892 "%s: Wrote only %lu of %lu bytes",
894 name
, (unsigned long int) status
, (unsigned long int) size
));
898 write_fatal (char const *name
)
900 call_arg_fatal ("write", name
);
904 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
906 write_error_details (name
, status
, size
);
911 /* Fork, aborting if unsuccessful. */
917 call_arg_fatal ("fork", _("child process"));
921 /* Create a pipe, aborting if unsuccessful. */
926 call_arg_fatal ("pipe", _("interprocess channel"));
929 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
930 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
931 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
935 ptr_align (void *ptr
, size_t alignment
)
938 char *p1
= p0
+ alignment
- 1;
939 return p1
- (size_t) p1
% alignment
;
942 /* Return the address of a page-aligned buffer of at least SIZE bytes.
943 The caller should free *PTR when done with the buffer. */
946 page_aligned_alloc (void **ptr
, size_t size
)
948 size_t alignment
= getpagesize ();
949 size_t size1
= size
+ alignment
;
952 *ptr
= xmalloc (size1
);
953 return ptr_align (*ptr
, alignment
);