binutils: Avoid renaming over existing files
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Fri, 19 Feb 2021 02:35:33 +0000 (08:05 +0530)
committerSiddhesh Poyarekar <siddhesh@gotplt.org>
Fri, 19 Feb 2021 02:35:33 +0000 (08:05 +0530)
commit3685de750e6a091663a0abe42528cad29e960e35
tree2c71a3c3f24af21550fde8cef7431a3f9d248659
parent668c18f17fc4ea91675a2b638c31bfbefa29665f
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.
binutils/ChangeLog
binutils/ar.c
binutils/arsup.c
binutils/bucomm.h
binutils/objcopy.c
binutils/rename.c