binutils-gdb/binutils/rename.c

255 lines
7.0 KiB
C
Raw Normal View History

1999-05-03 15:29:11 +08:00
/* rename.c -- rename a file, preserving symlinks.
Copyright (C) 1999-2021 Free Software Foundation, Inc.
1999-05-03 15:29:11 +08:00
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
2007-07-06 00:54:46 +08:00
the Free Software Foundation; either version 3 of the License, or
1999-05-03 15:29:11 +08:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
2005-05-08 22:17:41 +08:00
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
1999-05-03 15:29:11 +08:00
#include "sysdep.h"
1999-05-03 15:29:11 +08:00
#include "bfd.h"
#include "bucomm.h"
#ifdef HAVE_GOOD_UTIME_H
#include <utime.h>
#else /* ! HAVE_GOOD_UTIME_H */
#ifdef HAVE_UTIMES
#include <sys/time.h>
#endif /* HAVE_UTIMES */
#endif /* ! HAVE_GOOD_UTIME_H */
#if ! defined (_WIN32) || defined (__CYGWIN32__)
static int simple_copy (const char *, const char *);
1999-05-03 15:29:11 +08:00
/* The number of bytes to copy at once. */
#define COPY_BUF 8192
/* Copy file FROM to file TO, performing no translations.
Return 0 if ok, -1 if error. */
static int
simple_copy (const char *from, const char *to)
1999-05-03 15:29:11 +08:00
{
int fromfd, tofd, nread;
int saved;
char buf[COPY_BUF];
fromfd = open (from, O_RDONLY | O_BINARY);
1999-05-03 15:29:11 +08:00
if (fromfd < 0)
return -1;
#ifdef O_CREAT
tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
#else
1999-05-03 15:29:11 +08:00
tofd = creat (to, 0777);
#endif
1999-05-03 15:29:11 +08:00
if (tofd < 0)
{
saved = errno;
close (fromfd);
errno = saved;
return -1;
}
while ((nread = read (fromfd, buf, sizeof buf)) > 0)
{
if (write (tofd, buf, nread) != nread)
{
saved = errno;
close (fromfd);
close (tofd);
errno = saved;
return -1;
}
}
saved = errno;
close (fromfd);
close (tofd);
if (nread < 0)
{
errno = saved;
return -1;
}
return 0;
}
#endif /* __CYGWIN32__ or not _WIN32 */
1999-05-03 15:29:11 +08:00
/* Set the times of the file DESTINATION to be the same as those in
STATBUF. */
void
set_times (const char *destination, const struct stat *statbuf)
1999-05-03 15:29:11 +08:00
{
int result;
{
#ifdef HAVE_GOOD_UTIME_H
struct utimbuf tb;
tb.actime = statbuf->st_atime;
tb.modtime = statbuf->st_mtime;
result = utime (destination, &tb);
#else /* ! HAVE_GOOD_UTIME_H */
#ifndef HAVE_UTIMES
long tb[2];
tb[0] = statbuf->st_atime;
tb[1] = statbuf->st_mtime;
result = utime (destination, tb);
#else /* HAVE_UTIMES */
struct timeval tv[2];
tv[0].tv_sec = statbuf->st_atime;
tv[0].tv_usec = 0;
tv[1].tv_sec = statbuf->st_mtime;
tv[1].tv_usec = 0;
result = utimes (destination, tv);
#endif /* HAVE_UTIMES */
#endif /* ! HAVE_GOOD_UTIME_H */
}
if (result != 0)
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
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
#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. */
1999-05-03 15:29:11 +08:00
int
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
struct stat *target_stat ATTRIBUTE_UNUSED,
int preserve_dates ATTRIBUTE_UNUSED)
1999-05-03 15:29:11 +08:00
{
int ret = 0;
struct stat to_stat;
bfd_boolean exists;
exists = lstat (to, &to_stat) == 0;
1999-05-03 15:29:11 +08:00
#if defined (_WIN32) && !defined (__CYGWIN32__)
/* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
fail instead. Also, chown is not present. */
if (exists)
1999-05-03 15:29:11 +08:00
remove (to);
ret = rename (from, to);
if (ret != 0)
{
/* We have to clean up here. */
non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
1999-05-03 15:29:11 +08:00
unlink (from);
}
#else
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
/* 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
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
|| (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)
)
1999-05-03 15:29:11 +08:00
{
ret = rename (from, to);
if (ret == 0)
{
if (exists && target_stat != NULL)
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
try_preserve_permissions (fd, target_stat);
1999-05-03 15:29:11 +08:00
}
else
{
/* We have to clean up here. */
non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
1999-05-03 15:29:11 +08:00
unlink (from);
}
}
else
{
ret = simple_copy (from, to);
if (ret != 0)
non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
1999-05-03 15:29:11 +08:00
if (preserve_dates && target_stat != NULL)
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
set_times (to, target_stat);
1999-05-03 15:29:11 +08:00
unlink (from);
}
binutils: Make smart_rename safe too smart_rename is capable of handling symlinks by copying and it also tries to preserve ownership and permissions of files when they're overwritten during the rename. This is useful in objcopy where the file properties need to be preserved. However because smart_rename does this using file names, it leaves a race window between renames and permission fixes. This change removes this race window by using file descriptors from the original BFDs that were used to manipulate these files wherever possible. The file that is to be renamed is also passed as a file descriptor so that we use fchown/fchmod on the file descriptor, thus making sure that we only modify the file we have opened to write. Further, in case the file is to be overwritten (as is the case in ar or objcopy), the permissions that need to be restored are taken from the file descriptor that was opened for input so that integrity of the file status is maintained all the way through to the rename. binutils/ * rename.c * ar.c (write_archive) [!defined (_WIN32) || defined (__CYGWIN32__)]: Initialize TARGET_STAT and OFD to pass to SMART_RENAME. * arsup.c (ar_save) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * bucomm.h (smart_rename): Add new arguments to declaration. * objcopy.c (strip_main)[defined (_WIN32) || defined (__CYGWIN32__)]: Initialize COPYFD and pass to SMART_RENAME. (copy_main) [defined (_WIN32) || defined (__CYGWIN32__)]: Likewise. * rename.c (try_preserve_permissions): New function. (smart_rename): Use it and add new arguments.
2020-12-07 23:18:33 +08:00
if (fd >= 0)
close (fd);
1999-05-03 15:29:11 +08:00
#endif /* _WIN32 && !__CYGWIN32__ */
return ret;
}