提交 a0f43d82 编写于 作者: M Michal Privoznik

virSecurityDACSetOwnershipInternal: Don't chown so often

It's better if we stat() file that we are about to chown() at
first and check if there's something we need to change. Not that
it would make much difference, but for the upcoming patches we
need to be doing stat() anyway. Moreover, if we do things this
way, we can drop @chown_errno variable which will become
redundant.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
上级 d37d8f78
...@@ -242,7 +242,6 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv, ...@@ -242,7 +242,6 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv,
gid_t gid) gid_t gid)
{ {
int rc; int rc;
int chown_errno;
VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'", VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'",
NULLSTR(src ? src->path : path), (long) uid, (long) gid); NULLSTR(src ? src->path : path), (long) uid, (long) gid);
...@@ -255,9 +254,6 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv, ...@@ -255,9 +254,6 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv,
/* on -2 returned an error was already reported */ /* on -2 returned an error was already reported */
if (rc == -2) if (rc == -2)
return -1; return -1;
/* on -1 only errno was set */
chown_errno = errno;
} else { } else {
struct stat sb; struct stat sb;
...@@ -271,34 +267,34 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv, ...@@ -271,34 +267,34 @@ virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv,
path = src->path; path = src->path;
} }
rc = chown(path, uid, gid); if (stat(path, &sb) < 0) {
chown_errno = errno; virReportSystemError(errno, _("unable to stat: %s"), path);
return -1;
}
if (rc < 0 && if (sb.st_uid == uid && sb.st_gid == gid) {
stat(path, &sb) >= 0) { /* nothing to chown */
if (sb.st_uid == uid && return 0;
sb.st_gid == gid) {
/* It's alright, there's nothing to change anyway. */
return 0;
}
} }
rc = chown(path, uid, gid);
} }
if (rc < 0) { if (rc < 0) {
if (chown_errno == EOPNOTSUPP || chown_errno == EINVAL) { if (errno == EOPNOTSUPP || errno == EINVAL) {
VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not " VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
"supported by filesystem", "supported by filesystem",
(long) uid, (long) gid, path); (long) uid, (long) gid, path);
} else if (chown_errno == EPERM) { } else if (errno == EPERM) {
VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not " VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
"permitted", "permitted",
(long) uid, (long) gid, path); (long) uid, (long) gid, path);
} else if (chown_errno == EROFS) { } else if (errno == EROFS) {
VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not " VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
"possible on readonly filesystem", "possible on readonly filesystem",
(long) uid, (long) gid, path); (long) uid, (long) gid, path);
} else { } else {
virReportSystemError(chown_errno, virReportSystemError(errno,
_("unable to set user and group to '%ld:%ld' " _("unable to set user and group to '%ld:%ld' "
"on '%s'"), "on '%s'"),
(long) uid, (long) gid, path); (long) uid, (long) gid, path);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册