VIR_DIR_CREATE_FORCE_PERMS |
VIR_DIR_CREATE_ALLOW_EXIST |
(pool->def->type == VIR_STORAGE_POOL_NETFS
- ? VIR_DIR_CREATE_AS_UID : 0)) != 0)) {
- virReportSystemError(err, _("cannot create path '%s'"),
+ ? VIR_DIR_CREATE_AS_UID : 0)) < 0)) {
+ virReportSystemError(-err, _("cannot create path '%s'"),
pool->def->target.path);
goto error;
}
uid, gid,
VIR_DIR_CREATE_FORCE_PERMS |
(pool->def->type == VIR_STORAGE_POOL_NETFS
- ? VIR_DIR_CREATE_AS_UID : 0))) != 0) {
- virReportSystemError(err, _("cannot create path '%s'"),
+ ? VIR_DIR_CREATE_AS_UID : 0))) < 0) {
+ virReportSystemError(-err, _("cannot create path '%s'"),
vol->target.path);
return -1;
}
return ret;
}
+/* return -errno on failure, or 0 on success */
static int virDirCreateNoFork(const char *path, mode_t mode, uid_t uid, gid_t gid,
unsigned int flags) {
int ret = 0;
if ((mkdir(path, mode) < 0)
&& !((errno == EEXIST) && (flags & VIR_DIR_CREATE_ALLOW_EXIST)))
{
- ret = errno;
+ ret = -errno;
virReportSystemError(errno, _("failed to create directory '%s'"),
path);
goto error;
}
if (stat(path, &st) == -1) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno, _("stat of '%s' failed"), path);
goto error;
}
if (((st.st_uid != uid) || (st.st_gid != gid))
&& (chown(path, uid, gid) < 0)) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"),
path, (unsigned int) uid, (unsigned int) gid);
goto error;
}
if ((flags & VIR_DIR_CREATE_FORCE_PERMS)
&& (chmod(path, mode) < 0)) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno,
_("cannot set mode of '%s' to %04o"),
path, mode);
}
+/* return -errno on failure, or 0 on success */
int virDirCreate(const char *path, mode_t mode,
uid_t uid, gid_t gid, unsigned int flags) {
struct stat st;
int forkRet = virFork(&pid);
if (pid < 0) {
- ret = errno;
+ ret = -errno;
return ret;
}
/* wait for child to complete, and retrieve its exit code */
while ((waitret = waitpid(pid, &status, 0) == -1) && (errno == EINTR));
if (waitret == -1) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno,
_("failed to wait for child creating '%s'"),
path);
goto parenterror;
}
- ret = WEXITSTATUS(status);
- if (!WIFEXITED(status) || (ret == EACCES)) {
+ ret = -WEXITSTATUS(status);
+ if (!WIFEXITED(status) || (ret == -EACCES)) {
/* fall back to the simpler method, which works better in
* some cases */
return virDirCreateNoFork(path, mode, uid, gid, flags);
}
- if (ret != 0) {
+ if (ret < 0) {
goto parenterror;
}
parenterror:
/* set desired uid/gid, then attempt to create the directory */
if ((gid != 0) && (setgid(gid) != 0)) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno, _("cannot set gid %u creating '%s'"),
(unsigned int) gid, path);
goto childerror;
}
if ((uid != 0) && (setuid(uid) != 0)) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno, _("cannot set uid %u creating '%s'"),
(unsigned int) uid, path);
goto childerror;
}
if (mkdir(path, mode) < 0) {
- ret = errno;
- if (ret != EACCES) {
+ ret = -errno;
+ if (ret != -EACCES) {
/* in case of EACCES, the parent will retry */
virReportSystemError(errno, _("child failed to create directory '%s'"),
path);
/* check if group was set properly by creating after
* setgid. If not, try doing it with chown */
if (stat(path, &st) == -1) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno,
_("stat of '%s' failed"), path);
goto childerror;
}
if ((st.st_gid != gid) && (chown(path, -1, gid) < 0)) {
- ret = errno;
+ ret = -errno;
virReportSystemError(errno,
_("cannot chown '%s' to group %u"),
path, (unsigned int) gid);