aboutsummaryrefslogtreecommitdiff
path: root/binutils/rename.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@gotplt.org>2021-02-19 08:05:33 +0530
committerSiddhesh Poyarekar <siddhesh@gotplt.org>2021-02-19 08:05:33 +0530
commit3685de750e6a091663a0abe42528cad29e960e35 (patch)
tree2c71a3c3f24af21550fde8cef7431a3f9d248659 /binutils/rename.c
parent668c18f17fc4ea91675a2b638c31bfbefa29665f (diff)
downloadgdb-3685de750e6a091663a0abe42528cad29e960e35.zip
gdb-3685de750e6a091663a0abe42528cad29e960e35.tar.gz
gdb-3685de750e6a091663a0abe42528cad29e960e35.tar.bz2
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.
Diffstat (limited to 'binutils/rename.c')
-rw-r--r--binutils/rename.c95
1 files changed, 9 insertions, 86 deletions
diff --git a/binutils/rename.c b/binutils/rename.c
index e36b751..2ff092e 100644
--- a/binutils/rename.c
+++ b/binutils/rename.c
@@ -122,61 +122,13 @@ set_times (const char *destination, const struct stat *statbuf)
non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
}
-#ifndef S_ISLNK
-#ifdef S_IFLNK
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
-#else
-#define S_ISLNK(m) 0
-#define lstat stat
-#endif
-#endif
-
-#if !defined (_WIN32) || defined (__CYGWIN32__)
-/* Try to preserve the permission bits and ownership of an existing file when
- rename overwrites it. FD is the file being renamed and TARGET_STAT has the
- status of the file that was overwritten. */
-static void
-try_preserve_permissions (int fd, struct stat *target_stat)
-{
- struct stat from_stat;
- int ret = 0;
-
- if (fstat (fd, &from_stat) != 0)
- return;
-
- int from_mode = from_stat.st_mode & 0777;
- int to_mode = target_stat->st_mode & 0777;
-
- /* Fix up permissions before we potentially lose ownership with fchown.
- Clear the setxid bits because in case the fchown below fails then we don't
- want to end up with a sxid file owned by the invoking user. If the user
- hasn't changed or if fchown succeeded, we add back the sxid bits at the
- end. */
- if (from_mode != to_mode)
- fchmod (fd, to_mode);
-
- /* Fix up ownership, this will clear the setxid bits. */
- if (from_stat.st_uid != target_stat->st_uid
- || from_stat.st_gid != target_stat->st_gid)
- ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
-
- /* Fix up the sxid bits if either the fchown wasn't needed or it
- succeeded. */
- if (ret == 0)
- fchmod (fd, target_stat->st_mode & 07777);
-}
-#endif
-
-/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
- FD is an open file descriptor pointing to FROM that we can use to safely fix
- up permissions of the file after renaming. TARGET_STAT has the file status
- that is used to fix up permissions and timestamps after rename. Return 0 if
- ok, -1 if error and FD is closed before returning. */
+/* Rename FROM to TO, copying if TO exists. TARGET_STAT has the file status
+ that, if non-NULL, is used to fix up timestamps after rename. Return 0 if
+ ok, -1 if error. */
int
-smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
- struct stat *target_stat ATTRIBUTE_UNUSED,
- int preserve_dates ATTRIBUTE_UNUSED)
+smart_rename (const char *from, const char *to,
+ struct stat *target_stat ATTRIBUTE_UNUSED)
{
int ret = 0;
struct stat to_stat;
@@ -199,37 +151,10 @@ smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
unlink (from);
}
#else
- /* Avoid a full copy and use rename if we can fix up permissions of the
- file after renaming, i.e.:
-
- - TO is not a symbolic link
- - TO is a regular file with only one hard link
- - We have permission to write to TO
- - FD is available to safely fix up permissions to be the same as the file
- we overwrote with the rename.
-
- Note though that the actual file on disk that TARGET_STAT describes may
- have changed and we're only trying to preserve the status we know about.
- At no point do we try to interact with the new file changes, so there can
- only be two outcomes, i.e. either the external file change survives
- without knowledge of our change (if it happens after the rename syscall)
- or our rename and permissions fixup survive without any knowledge of the
- external change. */
- if (! exists
- || (fd >= 0
- && !S_ISLNK (to_stat.st_mode)
- && S_ISREG (to_stat.st_mode)
- && (to_stat.st_mode & S_IWUSR)
- && to_stat.st_nlink == 1)
- )
+ /* Avoid a full copy and use rename if TO does not exist. */
+ if (!exists)
{
- ret = rename (from, to);
- if (ret == 0)
- {
- if (exists && target_stat != NULL)
- try_preserve_permissions (fd, target_stat);
- }
- else
+ if ((ret = rename (from, to)) != 0)
{
/* We have to clean up here. */
non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
@@ -242,12 +167,10 @@ smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
if (ret != 0)
non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
- if (preserve_dates && target_stat != NULL)
+ if (target_stat != NULL)
set_times (to, target_stat);
unlink (from);
}
- if (fd >= 0)
- close (fd);
#endif /* _WIN32 && !__CYGWIN32__ */
return ret;