[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 3/4] Change virDirCreate to return -errno on failure.



virDirCreate also previously returned 0 on success and errno on
failure. This makes it fit the recommended convention of returning 0
on success, -errno (ie a negative number) on failure.
---
 src/storage/storage_backend_fs.c |    8 ++++----
 src/util/util.c                  |   32 +++++++++++++++++---------------
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index ffb0071..0761bd8 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -567,8 +567,8 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
                                 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;
         }
@@ -793,8 +793,8 @@ static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
                             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;
     }
diff --git a/src/util/util.c b/src/util/util.c
index ee5dd5e..8f2a17e 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1319,6 +1319,7 @@ error:
     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;
@@ -1327,27 +1328,27 @@ static int virDirCreateNoFork(const char *path, mode_t mode, uid_t uid, gid_t gi
     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);
@@ -1476,6 +1477,7 @@ childerror:
 
 }
 
+/* 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;
@@ -1493,7 +1495,7 @@ int virDirCreate(const char *path, mode_t mode,
     int forkRet = virFork(&pid);
 
     if (pid < 0) {
-        ret = errno;
+        ret = -errno;
         return ret;
     }
 
@@ -1501,19 +1503,19 @@ int virDirCreate(const char *path, mode_t mode,
         /* 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:
@@ -1530,20 +1532,20 @@ 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);
@@ -1553,13 +1555,13 @@ parenterror:
     /* 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);
-- 
1.7.1.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]