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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include <unlinkdir.h>
27 static void call_arg_fatal (char const *, char const *)
28 __attribute__ ((noreturn
));
30 /* Handling strings. */
32 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
33 STRING was nonzero, it is freed first. */
35 assign_string (char **string
, const char *value
)
39 *string
= value
? xstrdup (value
) : 0;
42 /* Allocate a copy of the string quoted as in C, and returns that. If
43 the string does not have to be quoted, it returns a null pointer.
44 The allocated copy should normally be freed with free() after the
45 caller is done with it.
47 This is used in one context only: generating the directory file in
48 incremental dumps. The quoted string is not intended for human
49 consumption; it is intended only for unquote_string. The quoting
50 is locale-independent, so that users needn't worry about locale
51 when reading directory files. This means that we can't use
52 quotearg, as quotearg is locale-dependent and is meant for human
55 quote_copy_string (const char *string
)
57 const char *source
= string
;
58 char *destination
= 0;
64 int character
= *source
++;
71 size_t length
= (source
- string
) - 1;
74 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
75 memcpy (buffer
, string
, length
);
76 destination
= buffer
+ length
;
78 *destination
++ = '\\';
79 *destination
++ = character
== '\\' ? '\\' : 'n';
84 *destination
++ = character
;
96 /* Takes a quoted C string (like those produced by quote_copy_string)
97 and turns it back into the un-quoted original. This is done in
98 place. Returns 0 only if the string was not properly quoted, but
99 completes the unquoting anyway.
101 This is used for reading the saved directory file in incremental
102 dumps. It is used for decoding old `N' records (demangling names).
103 But also, it is used for decoding file arguments, would they come
104 from the shell or a -T file, and for decoding the --exclude
107 unquote_string (char *string
)
110 char *source
= string
;
111 char *destination
= string
;
113 /* Escape sequences other than \\ and \n are no longer generated by
114 quote_copy_string, but accept them for backwards compatibility,
115 and also because unquote_string is used for purposes other than
116 parsing the output of quote_copy_string. */
123 *destination
++ = '\\';
128 *destination
++ = '\a';
133 *destination
++ = '\b';
138 *destination
++ = '\f';
143 *destination
++ = '\n';
148 *destination
++ = '\r';
153 *destination
++ = '\t';
158 *destination
++ = '\v';
163 *destination
++ = 0177;
176 int value
= *source
++ - '0';
178 if (*source
< '0' || *source
> '7')
180 *destination
++ = value
;
183 value
= value
* 8 + *source
++ - '0';
184 if (*source
< '0' || *source
> '7')
186 *destination
++ = value
;
189 value
= value
* 8 + *source
++ - '0';
190 *destination
++ = value
;
196 *destination
++ = '\\';
198 *destination
++ = *source
++;
201 else if (source
!= destination
)
202 *destination
++ = *source
++;
204 source
++, destination
++;
206 if (source
!= destination
)
213 /* Saved names in case backup needs to be undone. */
214 static char *before_backup_name
;
215 static char *after_backup_name
;
217 /* Return 1 if FILE_NAME is obviously "." or "/". */
219 must_be_dot_or_slash (char const *file_name
)
221 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
223 if (ISSLASH (file_name
[0]))
226 if (ISSLASH (file_name
[1]))
228 else if (file_name
[1] == '.'
229 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
230 file_name
+= 2 + (file_name
[2] == '.');
232 return ! file_name
[1];
236 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
239 while (ISSLASH (*file_name
))
243 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
247 /* Some implementations of rmdir let you remove '.' or '/'.
248 Report an error with errno set to zero for obvious cases of this;
249 otherwise call rmdir. */
251 safer_rmdir (const char *file_name
)
253 if (must_be_dot_or_slash (file_name
))
259 return rmdir (file_name
);
262 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
263 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
264 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
265 a directory that cannot be removed (e.g., because it is nonempty)
266 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
267 Return 0 on error, with errno set; if FILE_NAME is obviously the working
268 directory return zero with errno set to zero. */
270 remove_any_file (const char *file_name
, enum remove_option option
)
272 /* Try unlink first if we cannot unlink directories, as this saves
273 us a system call in the common case where we're removing a
275 if (cannot_unlink_dir ())
277 if (unlink (file_name
) == 0)
280 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
281 directory without appropriate privileges, but many Linux
282 kernels return the more-sensible EISDIR. */
283 if (errno
!= EPERM
&& errno
!= EISDIR
)
287 if (safer_rmdir (file_name
) == 0)
293 return cannot_unlink_dir () && unlink (file_name
) == 0;
297 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
302 case ORDINARY_REMOVE_OPTION
:
305 case WANT_DIRECTORY_REMOVE_OPTION
:
308 case RECURSIVE_REMOVE_OPTION
:
310 char *directory
= savedir (file_name
);
317 for (entry
= directory
;
318 (entrylen
= strlen (entry
)) != 0;
319 entry
+= entrylen
+ 1)
321 char *file_name_buffer
= new_name (file_name
, entry
);
322 int r
= remove_any_file (file_name_buffer
,
323 RECURSIVE_REMOVE_OPTION
);
325 free (file_name_buffer
);
336 return safer_rmdir (file_name
) == 0;
345 /* Check if FILE_NAME already exists and make a backup of it right now.
346 Return success (nonzero) only if the backup is either unneeded, or
347 successful. For now, directories are considered to never need
348 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
349 so, we do not have to backup block or character devices, nor remote
352 maybe_backup_file (const char *file_name
, int this_is_the_archive
)
354 struct stat file_stat
;
356 /* Check if we really need to backup the file. */
358 if (this_is_the_archive
&& _remdev (file_name
))
361 if (stat (file_name
, &file_stat
))
366 stat_error (file_name
);
370 if (S_ISDIR (file_stat
.st_mode
))
373 if (this_is_the_archive
374 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
377 assign_string (&before_backup_name
, file_name
);
379 /* A run situation may exist between Emacs or other GNU programs trying to
380 make a backup for the same file simultaneously. If theoretically
381 possible, real problems are unlikely. Doing any better would require a
382 convention, GNU-wide, for all programs doing backups. */
384 assign_string (&after_backup_name
, 0);
385 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
386 if (! after_backup_name
)
389 if (rename (before_backup_name
, after_backup_name
) == 0)
392 fprintf (stdlis
, _("Renaming %s to %s\n"),
393 quote_n (0, before_backup_name
),
394 quote_n (1, after_backup_name
));
399 /* The backup operation failed. */
401 ERROR ((0, e
, _("%s: Cannot rename to %s"),
402 quotearg_colon (before_backup_name
),
403 quote_n (1, after_backup_name
)));
404 assign_string (&after_backup_name
, 0);
409 /* Try to restore the recently backed up file to its original name.
410 This is usually only needed after a failed extraction. */
412 undo_last_backup (void)
414 if (after_backup_name
)
416 if (rename (after_backup_name
, before_backup_name
) != 0)
419 ERROR ((0, e
, _("%s: Cannot rename to %s"),
420 quotearg_colon (after_backup_name
),
421 quote_n (1, before_backup_name
)));
424 fprintf (stdlis
, _("Renaming %s back to %s\n"),
425 quote_n (0, after_backup_name
),
426 quote_n (1, before_backup_name
));
427 assign_string (&after_backup_name
, 0);
431 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
433 deref_stat (bool deref
, char const *name
, struct stat
*buf
)
435 return deref
? stat (name
, buf
) : lstat (name
, buf
);
438 /* A description of a working directory. */
443 struct saved_cwd saved_cwd
;
446 /* A vector of chdir targets. wd[0] is the initial working directory. */
447 static struct wd
*wd
;
449 /* The number of working directories in the vector. */
452 /* The allocated size of the vector. */
453 static size_t wd_alloc
;
455 /* DIR is the operand of a -C option; add it to vector of chdir targets,
456 and return the index of its location. */
458 chdir_arg (char const *dir
)
462 wd_alloc
= 2 * (wd_alloc
+ 1);
463 wd
= xrealloc (wd
, sizeof *wd
* wd_alloc
);
472 /* Optimize the common special case of the working directory,
473 or the working directory as a prefix. */
476 while (dir
[0] == '.' && ISSLASH (dir
[1]))
477 for (dir
+= 2; ISSLASH (*dir
); dir
++)
479 if (! dir
[dir
[0] == '.'])
488 /* Change to directory I. If I is 0, change to the initial working
489 directory; otherwise, I must be a value returned by chdir_arg. */
497 struct wd
*prev
= &wd
[previous
];
498 struct wd
*curr
= &wd
[i
];
503 if (save_cwd (&prev
->saved_cwd
) != 0)
504 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
509 if (restore_cwd (&curr
->saved_cwd
))
510 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
514 if (i
&& ! ISSLASH (curr
->name
[0]))
516 if (chdir (curr
->name
) != 0)
517 chdir_fatal (curr
->name
);
524 /* Decode MODE from its binary form in a stat structure, and encode it
525 into a 9-byte string STRING, terminated with a NUL. */
528 decode_mode (mode_t mode
, char *string
)
530 *string
++ = mode
& S_IRUSR
? 'r' : '-';
531 *string
++ = mode
& S_IWUSR
? 'w' : '-';
532 *string
++ = (mode
& S_ISUID
533 ? (mode
& S_IXUSR
? 's' : 'S')
534 : (mode
& S_IXUSR
? 'x' : '-'));
535 *string
++ = mode
& S_IRGRP
? 'r' : '-';
536 *string
++ = mode
& S_IWGRP
? 'w' : '-';
537 *string
++ = (mode
& S_ISGID
538 ? (mode
& S_IXGRP
? 's' : 'S')
539 : (mode
& S_IXGRP
? 'x' : '-'));
540 *string
++ = mode
& S_IROTH
? 'r' : '-';
541 *string
++ = mode
& S_IWOTH
? 'w' : '-';
542 *string
++ = (mode
& S_ISVTX
543 ? (mode
& S_IXOTH
? 't' : 'T')
544 : (mode
& S_IXOTH
? 'x' : '-'));
548 /* Report an error associated with the system call CALL and the
549 optional name NAME. */
551 call_arg_error (char const *call
, char const *name
)
554 /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
555 Directly translating this to another language will not work, first because
556 %s itself is not translated.
557 Translate it as `%s: Function %s failed'. */
558 ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
561 /* Report a fatal error associated with the system call CALL and
562 the optional file name NAME. */
564 call_arg_fatal (char const *call
, char const *name
)
567 /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
568 Directly translating this to another language will not work, first because
569 %s itself is not translated.
570 Translate it as `%s: Function %s failed'. */
571 FATAL_ERROR ((0, e
, _("%s: Cannot %s"), quotearg_colon (name
), call
));
574 /* Report a warning associated with the system call CALL and
575 the optional file name NAME. */
577 call_arg_warn (char const *call
, char const *name
)
580 /* TRANSLATORS: %s after `Cannot' is a function name, e.g. `Cannot open'.
581 Directly translating this to another language will not work, first because
582 %s itself is not translated.
583 Translate it as `%s: Function %s failed'. */
584 WARN ((0, e
, _("%s: Warning: Cannot %s"), quotearg_colon (name
), call
));
588 chdir_fatal (char const *name
)
590 call_arg_fatal ("chdir", name
);
594 chmod_error_details (char const *name
, mode_t mode
)
598 decode_mode (mode
, buf
);
599 ERROR ((0, e
, _("%s: Cannot change mode to %s"),
600 quotearg_colon (name
), buf
));
604 chown_error_details (char const *name
, uid_t uid
, gid_t gid
)
607 ERROR ((0, e
, _("%s: Cannot change ownership to uid %lu, gid %lu"),
608 quotearg_colon (name
), (unsigned long) uid
, (unsigned long) gid
));
612 close_error (char const *name
)
614 call_arg_error ("close", name
);
618 close_warn (char const *name
)
620 call_arg_warn ("close", name
);
624 close_diag (char const *name
)
626 if (ignore_failed_read_option
)
633 exec_fatal (char const *name
)
635 call_arg_fatal ("exec", name
);
639 link_error (char const *target
, char const *source
)
642 ERROR ((0, e
, _("%s: Cannot hard link to %s"),
643 quotearg_colon (source
), quote_n (1, target
)));
647 mkdir_error (char const *name
)
649 call_arg_error ("mkdir", name
);
653 mkfifo_error (char const *name
)
655 call_arg_error ("mkfifo", name
);
659 mknod_error (char const *name
)
661 call_arg_error ("mknod", name
);
665 open_error (char const *name
)
667 call_arg_error ("open", name
);
671 open_fatal (char const *name
)
673 call_arg_fatal ("open", name
);
677 open_warn (char const *name
)
679 call_arg_warn ("open", name
);
683 open_diag (char const *name
)
685 if (ignore_failed_read_option
)
692 read_error (char const *name
)
694 call_arg_error ("read", name
);
698 read_error_details (char const *name
, off_t offset
, size_t size
)
700 char buf
[UINTMAX_STRSIZE_BOUND
];
703 ngettext ("%s: Read error at byte %s, while reading %lu byte",
704 "%s: Read error at byte %s, while reading %lu bytes",
706 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
707 (unsigned long) size
));
711 read_warn_details (char const *name
, off_t offset
, size_t size
)
713 char buf
[UINTMAX_STRSIZE_BOUND
];
716 ngettext ("%s: Warning: Read error at byte %s, while reading %lu byte",
717 "%s: Warning: Read error at byte %s, while reading %lu bytes",
719 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
720 (unsigned long) size
));
724 read_diag_details (char const *name
, off_t offset
, size_t size
)
726 if (ignore_failed_read_option
)
727 read_warn_details (name
, offset
, size
);
729 read_error_details (name
, offset
, size
);
733 read_fatal (char const *name
)
735 call_arg_fatal ("read", name
);
739 read_fatal_details (char const *name
, off_t offset
, size_t size
)
741 char buf
[UINTMAX_STRSIZE_BOUND
];
744 ngettext ("%s: Read error at byte %s, reading %lu byte",
745 "%s: Read error at byte %s, reading %lu bytes",
747 quotearg_colon (name
), STRINGIFY_BIGINT (offset
, buf
),
748 (unsigned long) size
));
752 readlink_error (char const *name
)
754 call_arg_error ("readlink", name
);
758 readlink_warn (char const *name
)
760 call_arg_warn ("readlink", name
);
764 readlink_diag (char const *name
)
766 if (ignore_failed_read_option
)
767 readlink_warn (name
);
769 readlink_error (name
);
773 savedir_error (char const *name
)
775 call_arg_error ("savedir", name
);
779 savedir_warn (char const *name
)
781 call_arg_warn ("savedir", name
);
785 savedir_diag (char const *name
)
787 if (ignore_failed_read_option
)
790 savedir_error (name
);
794 seek_error (char const *name
)
796 call_arg_error ("seek", name
);
800 seek_error_details (char const *name
, off_t offset
)
802 char buf
[UINTMAX_STRSIZE_BOUND
];
804 ERROR ((0, e
, _("%s: Cannot seek to %s"),
805 quotearg_colon (name
),
806 STRINGIFY_BIGINT (offset
, buf
)));
810 seek_warn (char const *name
)
812 call_arg_warn ("seek", name
);
816 seek_warn_details (char const *name
, off_t offset
)
818 char buf
[UINTMAX_STRSIZE_BOUND
];
820 WARN ((0, e
, _("%s: Warning: Cannot seek to %s"),
821 quotearg_colon (name
),
822 STRINGIFY_BIGINT (offset
, buf
)));
826 seek_diag_details (char const *name
, off_t offset
)
828 if (ignore_failed_read_option
)
829 seek_warn_details (name
, offset
);
831 seek_error_details (name
, offset
);
835 symlink_error (char const *contents
, char const *name
)
838 ERROR ((0, e
, _("%s: Cannot create symlink to %s"),
839 quotearg_colon (name
), quote_n (1, contents
)));
843 stat_fatal (char const *name
)
845 call_arg_fatal ("stat", name
);
849 stat_error (char const *name
)
851 call_arg_error ("stat", name
);
855 stat_warn (char const *name
)
857 call_arg_warn ("stat", name
);
861 stat_diag (char const *name
)
863 if (ignore_failed_read_option
)
870 truncate_error (char const *name
)
872 call_arg_error ("truncate", name
);
876 truncate_warn (char const *name
)
878 call_arg_warn ("truncate", name
);
882 unlink_error (char const *name
)
884 call_arg_error ("unlink", name
);
888 utime_error (char const *name
)
890 call_arg_error ("utime", name
);
894 waitpid_error (char const *name
)
896 call_arg_error ("waitpid", name
);
900 write_error (char const *name
)
902 call_arg_error ("write", name
);
906 write_error_details (char const *name
, size_t status
, size_t size
)
912 ngettext ("%s: Wrote only %lu of %lu byte",
913 "%s: Wrote only %lu of %lu bytes",
915 name
, (unsigned long int) status
, (unsigned long int) size
));
919 write_fatal (char const *name
)
921 call_arg_fatal ("write", name
);
925 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
927 write_error_details (name
, status
, size
);
932 /* Fork, aborting if unsuccessful. */
938 call_arg_fatal ("fork", _("child process"));
942 /* Create a pipe, aborting if unsuccessful. */
947 call_arg_fatal ("pipe", _("interprocess channel"));
950 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
951 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
952 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
956 ptr_align (void *ptr
, size_t alignment
)
959 char *p1
= p0
+ alignment
- 1;
960 return p1
- (size_t) p1
% alignment
;
963 /* Return the address of a page-aligned buffer of at least SIZE bytes.
964 The caller should free *PTR when done with the buffer. */
967 page_aligned_alloc (void **ptr
, size_t size
)
969 size_t alignment
= getpagesize ();
970 size_t size1
= size
+ alignment
;
973 *ptr
= xmalloc (size1
);
974 return ptr_align (*ptr
, alignment
);