Don't attempt to change owner/mode/flags that don't need to changed.

This should calm down attempts to `cp -p' to a nfs mount or some other
filesystem that doesn't accept flags or all combinations of flags.
It will warn if it fails to change flags though.
This commit is contained in:
Peter Wemm 1998-06-10 06:29:23 +00:00
parent d2b4b1fa20
commit 4faaa03777
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=36838

View File

@ -36,7 +36,7 @@
static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
#endif
static const char rcsid[] =
"$Id: utils.c,v 1.18 1998/05/13 07:25:17 charnier Exp $";
"$Id: utils.c,v 1.19 1998/06/09 03:38:26 imp Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -263,7 +263,9 @@ setfile(fs, fd)
int fd;
{
static struct timeval tv[2];
struct stat ts;
int rval;
int gotstat;
rval = 0;
fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
@ -275,30 +277,42 @@ setfile(fs, fd)
warn("utimes: %s", to.p_path);
rval = 1;
}
if (fd ? fstat(fd, &ts) : stat(to.p_path, &ts))
gotstat = 0;
else {
gotstat = 1;
ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
S_IRWXU | S_IRWXG | S_IRWXO;
}
/*
* Changing the ownership probably won't succeed, unless we're root
* or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
* the mode; current BSD behavior is to remove all setuid bits on
* chown. If chown fails, lose setuid/setgid bits.
*/
if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
chown(to.p_path, fs->st_uid, fs->st_gid)) {
if (errno != EPERM) {
if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
chown(to.p_path, fs->st_uid, fs->st_gid)) {
if (errno != EPERM) {
warn("chown: %s", to.p_path);
rval = 1;
}
fs->st_mode &= ~(S_ISUID | S_ISGID);
}
if (!gotstat || fs->st_mode != ts.st_mode)
if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
warn("chown: %s", to.p_path);
rval = 1;
}
fs->st_mode &= ~(S_ISUID | S_ISGID);
}
if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
warn("chown: %s", to.p_path);
rval = 1;
}
if (fd ?
fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
warn("chflags: %s", to.p_path);
rval = 1;
}
if (!gotstat || fs->st_flags != ts.st_flags)
if (fd ?
fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
warn("chflags: %s", to.p_path);
rval = 1;
}
return (rval);
}