- if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
-#ifdef __APPLE__
- chown(to.p_path, fs->st_uid, fs->st_gid)) {
-#else
- lchown(to.p_path, fs->st_uid, fs->st_gid)) {
-#endif
- if (errno != EPERM) {
- warn("chown: %s", to.p_path);
+ if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
+ if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
+ (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
+ chown(to.p_path, fs->st_uid, fs->st_gid))) {
+ if (errno != EPERM) {
+ warn("%schown: %s", islink ? "l" : "", to.p_path);
+ rval = 1;
+ }
+ fs->st_mode &= ~(S_ISUID | S_ISGID);
+ }
+
+ if (!gotstat || fs->st_mode != ts.st_mode)
+ if (fdval ? fchmod(fd, fs->st_mode) :
+ (islink ? lchmod(to.p_path, fs->st_mode) :
+ chmod(to.p_path, fs->st_mode))) {
+ warn("%schmod: %s", islink ? "l" : "", to.p_path);
+ rval = 1;
+ }
+
+ if (!gotstat || fs->st_flags != ts.st_flags)
+ if (fdval ?
+ fchflags(fd, fs->st_flags) :
+ (islink ? lchflags(to.p_path, fs->st_flags) :
+ chflags(to.p_path, fs->st_flags))) {
+ warn("%schflags: %s", islink ? "l" : "", to.p_path);