1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005 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>
28 /* Handling strings. */
30 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
31 STRING was nonzero, it is freed first. */
33 assign_string (char **string
, const char *value
)
37 *string
= value
? xstrdup (value
) : 0;
40 /* Allocate a copy of the string quoted as in C, and returns that. If
41 the string does not have to be quoted, it returns a null pointer.
42 The allocated copy should normally be freed with free() after the
43 caller is done with it.
45 This is used in one context only: generating the directory file in
46 incremental dumps. The quoted string is not intended for human
47 consumption; it is intended only for unquote_string. The quoting
48 is locale-independent, so that users needn't worry about locale
49 when reading directory files. This means that we can't use
50 quotearg, as quotearg is locale-dependent and is meant for human
53 quote_copy_string (const char *string
)
55 const char *source
= string
;
56 char *destination
= 0;
62 int character
= *source
++;
69 size_t length
= (source
- string
) - 1;
72 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
73 memcpy (buffer
, string
, length
);
74 destination
= buffer
+ length
;
76 *destination
++ = '\\';
77 *destination
++ = character
== '\\' ? '\\' : 'n';
82 *destination
++ = character
;
94 /* Takes a quoted C string (like those produced by quote_copy_string)
95 and turns it back into the un-quoted original. This is done in
96 place. Returns 0 only if the string was not properly quoted, but
97 completes the unquoting anyway.
99 This is used for reading the saved directory file in incremental
100 dumps. It is used for decoding old `N' records (demangling names).
101 But also, it is used for decoding file arguments, would they come
102 from the shell or a -T file, and for decoding the --exclude
105 unquote_string (char *string
)
108 char *source
= string
;
109 char *destination
= string
;
111 /* Escape sequences other than \\ and \n are no longer generated by
112 quote_copy_string, but accept them for backwards compatibility,
113 and also because unquote_string is used for purposes other than
114 parsing the output of quote_copy_string. */
121 *destination
++ = '\\';
126 *destination
++ = '\a';
131 *destination
++ = '\b';
136 *destination
++ = '\f';
141 *destination
++ = '\n';
146 *destination
++ = '\r';
151 *destination
++ = '\t';
156 *destination
++ = '\v';
161 *destination
++ = 0177;
174 int value
= *source
++ - '0';
176 if (*source
< '0' || *source
> '7')
178 *destination
++ = value
;
181 value
= value
* 8 + *source
++ - '0';
182 if (*source
< '0' || *source
> '7')
184 *destination
++ = value
;
187 value
= value
* 8 + *source
++ - '0';
188 *destination
++ = value
;
194 *destination
++ = '\\';
196 *destination
++ = *source
++;
199 else if (source
!= destination
)
200 *destination
++ = *source
++;
202 source
++, destination
++;
204 if (source
!= destination
)
211 /* Saved names in case backup needs to be undone. */
212 static char *before_backup_name
;
213 static char *after_backup_name
;
215 /* Return 1 if FILE_NAME is obviously "." or "/". */
217 must_be_dot_or_slash (char const *file_name
)
219 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
221 if (ISSLASH (file_name
[0]))
224 if (ISSLASH (file_name
[1]))
226 else if (file_name
[1] == '.'
227 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
228 file_name
+= 2 + (file_name
[2] == '.');
230 return ! file_name
[1];
234 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
237 while (ISSLASH (*file_name
))
241 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
245 /* Some implementations of rmdir let you remove '.' or '/'.
246 Report an error with errno set to zero for obvious cases of this;
247 otherwise call rmdir. */
249 safer_rmdir (const char *file_name
)
251 if (must_be_dot_or_slash (file_name
))
257 return rmdir (file_name
);
260 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
261 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
262 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
263 a directory that cannot be removed (e.g., because it is nonempty)
264 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
265 Return 0 on error, with errno set; if FILE_NAME is obviously the working
266 directory return zero with errno set to zero. */
268 remove_any_file (const char *file_name
, enum remove_option option
)
270 /* Try unlink first if we cannot unlink directories, as this saves
271 us a system call in the common case where we're removing a
273 bool try_unlink_first
= cannot_unlink_dir ();
275 if (try_unlink_first
)
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 !try_unlink_first
&& 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
);
525 close_diag (char const *name
)
527 if (ignore_failed_read_option
)
534 open_diag (char const *name
)
536 if (ignore_failed_read_option
)
543 read_diag_details (char const *name
, off_t offset
, size_t size
)
545 if (ignore_failed_read_option
)
546 read_warn_details (name
, offset
, size
);
548 read_error_details (name
, offset
, size
);
552 readlink_diag (char const *name
)
554 if (ignore_failed_read_option
)
555 readlink_warn (name
);
557 readlink_error (name
);
561 savedir_diag (char const *name
)
563 if (ignore_failed_read_option
)
566 savedir_error (name
);
570 seek_diag_details (char const *name
, off_t offset
)
572 if (ignore_failed_read_option
)
573 seek_warn_details (name
, offset
);
575 seek_error_details (name
, offset
);
579 stat_diag (char const *name
)
581 if (ignore_failed_read_option
)
588 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
590 write_error_details (name
, status
, size
);
594 /* Fork, aborting if unsuccessful. */
600 call_arg_fatal ("fork", _("child process"));
604 /* Create a pipe, aborting if unsuccessful. */
609 call_arg_fatal ("pipe", _("interprocess channel"));
612 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
613 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
614 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
618 ptr_align (void *ptr
, size_t alignment
)
621 char *p1
= p0
+ alignment
- 1;
622 return p1
- (size_t) p1
% alignment
;
625 /* Return the address of a page-aligned buffer of at least SIZE bytes.
626 The caller should free *PTR when done with the buffer. */
629 page_aligned_alloc (void **ptr
, size_t size
)
631 size_t alignment
= getpagesize ();
632 size_t size1
= size
+ alignment
;
635 *ptr
= xmalloc (size1
);
636 return ptr_align (*ptr
, alignment
);