1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001 Free
4 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. */
20 /* $FreeBSD: src/contrib/tar/src/misc.c,v 1.2.2.2 2002/10/19 09:37:29 sobomax Exp $ */
28 static void call_arg_fatal PARAMS ((char const *, char const *))
29 __attribute__ ((noreturn));
31 /* Handling strings. */
33 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
34 STRING was nonzero, it is freed first. */
36 assign_string (char **string, const char *value)
40 *string = value ? xstrdup (value) : 0;
43 /* Allocate a copy of the string quoted as in C, and returns that. If
44 the string does not have to be quoted, it returns a null pointer.
45 The allocated copy should normally be freed with free() after the
46 caller is done with it.
48 This is used in one context only: generating the directory file in
49 incremental dumps. The quoted string is not intended for human
50 consumption; it is intended only for unquote_string. The quoting
51 is locale-independent, so that users needn't worry about locale
52 when reading directory files. This means that we can't use
53 quotearg, as quotearg is locale-dependent and is meant for human
56 quote_copy_string (const char *string)
58 const char *source = string;
59 char *destination = 0;
65 int character = *source++;
72 size_t length = (source - string) - 1;
75 buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
76 memcpy (buffer, string, length);
77 destination = buffer + length;
79 *destination++ = '\\';
80 *destination++ = character == '\\' ? '\\' : 'n';
85 *destination++ = character;
97 /* Takes a quoted C string (like those produced by quote_copy_string)
98 and turns it back into the un-quoted original. This is done in
99 place. Returns 0 only if the string was not properly quoted, but
100 completes the unquoting anyway.
102 This is used for reading the saved directory file in incremental
103 dumps. It is used for decoding old `N' records (demangling names).
104 But also, it is used for decoding file arguments, would they come
105 from the shell or a -T file, and for decoding the --exclude
108 unquote_string (char *string)
111 char *source = string;
112 char *destination = string;
114 /* Escape sequences other than \\ and \n are no longer generated by
115 quote_copy_string, but accept them for backwards compatibility,
116 and also because unquote_string is used for purposes other than
117 parsing the output of quote_copy_string. */
124 *destination++ = '\\';
129 *destination++ = '\n';
134 *destination++ = '\t';
139 *destination++ = '\f';
144 *destination++ = '\b';
149 *destination++ = '\r';
154 *destination++ = 0177;
167 int value = *source++ - '0';
169 if (*source < '0' || *source > '7')
171 *destination++ = value;
174 value = value * 8 + *source++ - '0';
175 if (*source < '0' || *source > '7')
177 *destination++ = value;
180 value = value * 8 + *source++ - '0';
181 *destination++ = value;
187 *destination++ = '\\';
189 *destination++ = *source++;
192 else if (source != destination)
193 *destination++ = *source++;
195 source++, destination++;
197 if (source != destination)
202 /* Return nonzero if NAME contains ".." as a path name component. */
204 contains_dot_dot (char const *name)
206 char const *p = name + FILESYSTEM_PREFIX_LEN (name);
210 if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
218 while (! ISSLASH (*p));
225 while ( ISSLASH (*p));
231 /* Saved names in case backup needs to be undone. */
232 static char *before_backup_name;
233 static char *after_backup_name;
235 /* Some implementations of rmdir let you remove the working directory.
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 while (path[0] == '.' && ISSLASH (path[1]))
246 while (ISSLASH (*path));
249 if (! path[0] || (path[0] == '.' && ! path[1]))
258 /* Remove PATH. If PATH is a directory, then if RECURSE is set remove
259 it recursively; otherwise, remove it only if it is empty. Return 0
260 on error, with errno set; if PATH is obviously the working
261 directory return zero with errno set to zero. */
263 remove_any_file (const char *path, int recurse)
265 /* Try unlink first if we are not root, as this saves us a system
266 call in the common case where we're removing a non-directory. */
269 if (unlink (path) == 0)
275 if (safer_rmdir (path) == 0)
281 return we_are_root && unlink (path) == 0;
285 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
290 char *directory = savedir (path);
297 for (entry = directory;
298 (entrylen = strlen (entry)) != 0;
299 entry += entrylen + 1)
301 char *path_buffer = new_name (path, entry);
302 int r = remove_any_file (path_buffer, 1);
315 return safer_rmdir (path) == 0;
323 /* Check if PATH already exists and make a backup of it right now.
324 Return success (nonzero) only if the backup in either unneeded, or
325 successful. For now, directories are considered to never need
326 backup. If ARCHIVE is nonzero, this is the archive and so, we do
327 not have to backup block or character devices, nor remote entities. */
329 maybe_backup_file (const char *path, int archive)
331 struct stat file_stat;
333 /* Check if we really need to backup the file. */
335 if (archive && _remdev (path))
338 if (stat (path, &file_stat))
347 if (S_ISDIR (file_stat.st_mode))
350 if (archive && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
353 assign_string (&before_backup_name, path);
355 /* A run situation may exist between Emacs or other GNU programs trying to
356 make a backup for the same file simultaneously. If theoretically
357 possible, real problems are unlikely. Doing any better would require a
358 convention, GNU-wide, for all programs doing backups. */
360 assign_string (&after_backup_name, 0);
361 after_backup_name = find_backup_file_name (path, backup_type);
362 if (! after_backup_name)
365 if (rename (before_backup_name, after_backup_name) == 0)
368 fprintf (stdlis, _("Renaming %s to %s\n"),
369 quote_n (0, before_backup_name),
370 quote_n (1, after_backup_name));
375 /* The backup operation failed. */
377 ERROR ((0, e, _("%s: Cannot rename to %s"),
378 quotearg_colon (before_backup_name),
379 quote_n (1, after_backup_name)));
380 assign_string (&after_backup_name, 0);
385 /* Try to restore the recently backed up file to its original name.
386 This is usually only needed after a failed extraction. */
388 undo_last_backup (void)
390 if (after_backup_name)
392 if (rename (after_backup_name, before_backup_name) != 0)
395 ERROR ((0, e, _("%s: Cannot rename to %s"),
396 quotearg_colon (after_backup_name),
397 quote_n (1, before_backup_name)));
400 fprintf (stdlis, _("Renaming %s back to %s\n"),
401 quote_n (0, after_backup_name),
402 quote_n (1, before_backup_name));
403 assign_string (&after_backup_name, 0);
407 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
409 deref_stat (int deref, char const *name, struct stat *buf)
411 return deref ? stat (name, buf) : lstat (name, buf);
414 /* A description of a working directory. */
419 struct saved_cwd saved_cwd;
422 /* A vector of chdir targets. wd[0] is the initial working directory. */
423 static struct wd *wd;
425 /* The number of working directories in the vector. */
428 /* The allocated size of the vector. */
429 static size_t wd_alloc;
431 /* DIR is the operand of a -C option; add it to vector of chdir targets,
432 and return the index of its location. */
434 chdir_arg (char const *dir)
438 wd_alloc = 2 * (wd_alloc + 1);
439 wd = xrealloc (wd, sizeof *wd * wd_alloc);
448 /* Optimize the common special case of the working directory,
449 or the working directory as a prefix. */
452 while (dir[0] == '.' && ISSLASH (dir[1]))
453 for (dir += 2; ISSLASH (*dir); dir++)
455 if (! dir[dir[0] == '.'])
464 /* Change to directory I. If I is 0, change to the initial working
465 directory; otherwise, I must be a value returned by chdir_arg. */
473 struct wd *prev = &wd[previous];
474 struct wd *curr = &wd[i];
479 if (save_cwd (&prev->saved_cwd) != 0)
480 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
485 if (restore_cwd (&curr->saved_cwd, curr->name, prev->name))
486 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
490 if (i && ! ISSLASH (curr->name[0]))
492 if (chdir (curr->name) != 0)
493 chdir_fatal (curr->name);
500 /* Decode MODE from its binary form in a stat structure, and encode it
501 into a 9-byte string STRING, terminated with a NUL. */
504 decode_mode (mode_t mode, char *string)
506 *string++ = mode & S_IRUSR ? 'r' : '-';
507 *string++ = mode & S_IWUSR ? 'w' : '-';
508 *string++ = (mode & S_ISUID
509 ? (mode & S_IXUSR ? 's' : 'S')
510 : (mode & S_IXUSR ? 'x' : '-'));
511 *string++ = mode & S_IRGRP ? 'r' : '-';
512 *string++ = mode & S_IWGRP ? 'w' : '-';
513 *string++ = (mode & S_ISGID
514 ? (mode & S_IXGRP ? 's' : 'S')
515 : (mode & S_IXGRP ? 'x' : '-'));
516 *string++ = mode & S_IROTH ? 'r' : '-';
517 *string++ = mode & S_IWOTH ? 'w' : '-';
518 *string++ = (mode & S_ISVTX
519 ? (mode & S_IXOTH ? 't' : 'T')
520 : (mode & S_IXOTH ? 'x' : '-'));
524 /* Report an error associated with the system call CALL and the
525 optional name NAME. */
527 call_arg_error (char const *call, char const *name)
530 ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
533 /* Report a fatal error associated with the system call CALL and
534 the optional file name NAME. */
536 call_arg_fatal (char const *call, char const *name)
539 FATAL_ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
542 /* Report a warning associated with the system call CALL and
543 the optional file name NAME. */
545 call_arg_warn (char const *call, char const *name)
548 WARN ((0, e, _("%s: Warning: Cannot %s"), quotearg_colon (name), call));
552 chdir_fatal (char const *name)
554 call_arg_fatal ("chdir", name);
558 chmod_error_details (char const *name, mode_t mode)
561 ERROR ((0, e, _("%s: Cannot change mode to 0%o"),
562 quotearg_colon (name), mode));
566 chown_error_details (char const *name, uid_t uid, gid_t gid)
569 ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"),
570 quotearg_colon (name), (unsigned long) uid, (unsigned long) gid));
574 close_error (char const *name)
576 call_arg_error ("close", name);
580 close_fatal (char const *name)
582 call_arg_fatal ("close", name);
586 close_warn (char const *name)
588 call_arg_warn ("close", name);
592 exec_fatal (char const *name)
594 call_arg_fatal ("exec", name);
598 link_error (char const *target, char const *source)
601 ERROR ((0, e, _("%s: Cannot hard link to %s"),
602 quotearg_colon (source), quote_n (1, target)));
606 mkdir_error (char const *name)
608 call_arg_error ("mkdir", name);
612 mkfifo_error (char const *name)
614 call_arg_error ("mkfifo", name);
618 mknod_error (char const *name)
620 call_arg_error ("mknod", name);
624 open_error (char const *name)
626 call_arg_error ("open", name);
630 open_fatal (char const *name)
632 call_arg_fatal ("open", name);
636 open_warn (char const *name)
638 call_arg_warn ("open", name);
642 read_error (char const *name)
644 call_arg_error ("read", name);
648 read_error_details (char const *name, off_t offset, size_t size)
650 char buf[UINTMAX_STRSIZE_BOUND];
653 _("%s: Read error at byte %s, reading %lu bytes"),
654 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
655 (unsigned long) size));
659 read_warn_details (char const *name, off_t offset, size_t size)
661 char buf[UINTMAX_STRSIZE_BOUND];
664 _("%s: Warning: Read error at byte %s, reading %lu bytes"),
665 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
666 (unsigned long) size));
670 read_fatal (char const *name)
672 call_arg_fatal ("read", name);
676 read_fatal_details (char const *name, off_t offset, size_t size)
678 char buf[UINTMAX_STRSIZE_BOUND];
681 _("%s: Read error at byte %s, reading %lu bytes"),
682 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
683 (unsigned long) size));
687 readlink_error (char const *name)
689 call_arg_error ("readlink", name);
693 readlink_warn (char const *name)
695 call_arg_warn ("readlink", name);
699 savedir_error (char const *name)
701 call_arg_error ("savedir", name);
705 savedir_warn (char const *name)
707 call_arg_warn ("savedir", name);
711 seek_error (char const *name)
713 call_arg_error ("seek", name);
717 seek_error_details (char const *name, off_t offset)
719 char buf[UINTMAX_STRSIZE_BOUND];
721 ERROR ((0, e, _("%s: Cannot seek to %s"),
722 quotearg_colon (name),
723 STRINGIFY_BIGINT (offset, buf)));
727 seek_warn (char const *name)
729 call_arg_warn ("seek", name);
733 seek_warn_details (char const *name, off_t offset)
735 char buf[UINTMAX_STRSIZE_BOUND];
737 WARN ((0, e, _("%s: Warning: Cannot seek to %s"),
738 quotearg_colon (name),
739 STRINGIFY_BIGINT (offset, buf)));
743 symlink_error (char const *contents, char const *name)
746 ERROR ((0, e, _("%s: Cannot create symlink to %s"),
747 quotearg_colon (name), quote_n (1, contents)));
751 stat_error (char const *name)
753 call_arg_error ("stat", name);
757 stat_warn (char const *name)
759 call_arg_warn ("stat", name);
763 truncate_error (char const *name)
765 call_arg_error ("truncate", name);
769 truncate_warn (char const *name)
771 call_arg_warn ("truncate", name);
775 unlink_error (char const *name)
777 call_arg_error ("unlink", name);
781 utime_error (char const *name)
783 call_arg_error ("utime", name);
787 waitpid_error (char const *name)
789 call_arg_error ("waitpid", name);
793 write_error (char const *name)
795 call_arg_error ("write", name);
799 write_error_details (char const *name, ssize_t status, size_t size)
804 ERROR ((0, 0, _("%s: Wrote only %lu of %lu bytes"),
805 name, (unsigned long) status, (unsigned long) record_size));
809 write_fatal (char const *name)
811 call_arg_fatal ("write", name);
815 write_fatal_details (char const *name, ssize_t status, size_t size)
817 write_error_details (name, status, size);
822 /* Fork, aborting if unsuccessful. */
828 call_arg_fatal ("fork", _("child process"));
832 /* Create a pipe, aborting if unsuccessful. */
837 call_arg_fatal ("pipe", _("interprocess channel"));
840 /* Return an unambiguous printable representation, allocated in slot N,
841 for NAME, suitable for diagnostics. */
843 quote_n (int n, char const *name)
845 return quotearg_n_style (n, locale_quoting_style, name);
848 /* Return an unambiguous printable representation of NAME, suitable
851 quote (char const *name)
853 return quote_n (0, name);