mirror of
https://github.com/bminor/binutils-gdb.git
synced 2025-12-27 09:38:57 +00:00
binutils: Avoid renaming over existing files
Renaming over existing files needs additional care to restore permissions and ownership, which may not always succeed. Additionally, other properties of the file such as extended attributes may be lost, making the operation flaky. For predictable results, resort to rename() only if the file does not exist, otherwise copy the file contents into the existing file. This ensures that no additional tricks are needed to retain file properties. This also allows dropping of the redundant set_times on the tmpfile in objcopy/strip since now we no longer rename over existing files. binutils/ * ar.c (write_archive): Remove TARGET_STAT. Adjust call to SMART_RENAME. * arsup.c (ar_save): Likewise. * objcopy (strip_main): Don't copy TMPFD. Don't set times on temporary file and adjust call to SMART_RENAME. (copy_main): Likewise. * rename.c [!S_ISLNK]: Remove definitions. (try_preserve_permissions): Remove function. (smart_rename): Remove FD, PRESERVE_DATES arguments. Use rename system call only if TO does not exist. * bucomm.h (smart_rename): Adjust declaration.
This commit is contained in:
@@ -343,18 +343,11 @@ ar_save (void)
|
||||
}
|
||||
else
|
||||
{
|
||||
bfd_boolean skip_stat = FALSE;
|
||||
struct stat target_stat;
|
||||
int ofd = real_ofd;
|
||||
|
||||
if (deterministic > 0)
|
||||
obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
|
||||
|
||||
#if !defined (_WIN32) || defined (__CYGWIN32__)
|
||||
/* It's OK to fail; at worst it will result in SMART_RENAME using a slow
|
||||
copy fallback to write the output. */
|
||||
ofd = dup (ofd);
|
||||
#endif
|
||||
bfd_close (obfd);
|
||||
|
||||
if (stat (real_name, &target_stat) != 0)
|
||||
@@ -363,9 +356,6 @@ ar_save (void)
|
||||
Create the real empty output file here so smart_rename will
|
||||
update the mode according to the process umask. */
|
||||
obfd = bfd_openw (real_name, NULL);
|
||||
if (obfd == NULL
|
||||
|| bfd_stat (obfd, &target_stat) != 0)
|
||||
skip_stat = TRUE;
|
||||
if (obfd != NULL)
|
||||
{
|
||||
bfd_set_format (obfd, bfd_archive);
|
||||
@@ -373,8 +363,7 @@ ar_save (void)
|
||||
}
|
||||
}
|
||||
|
||||
smart_rename (temp_name, real_name, ofd,
|
||||
skip_stat ? NULL : &target_stat, 0);
|
||||
smart_rename (temp_name, real_name, NULL);
|
||||
obfd = 0;
|
||||
free (temp_name);
|
||||
free (real_name);
|
||||
|
||||
Reference in New Issue
Block a user