[libvirt] [PATCH 2/6] util: file: use VIR_AUTOCLOSE instead of VIR_FORCE_CLOSE

Shi Lei shi_lei at massclouds.com
Mon Sep 10 03:47:51 UTC 2018


By making use of GNU C's cleanup attribute handled by the VIR_AUTOCLOSE macro,
many of the VIR_FORCE_CLOSE calls can be dropped, which in turn leads to
getting rid of many of our cleanup sections.
    
Signed-off-by: Shi Lei <shi_lei at massclouds.com>
---
 src/util/virfile.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 01ebdb6..e63fada 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1969,29 +1969,22 @@ int
 virFileIsCDROM(const char *path)
 {
     struct stat st;
-    int fd;
-    int ret = -1;
+    VIR_AUTOCLOSE(fd);
 
     if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
-        goto cleanup;
+        return -1;
 
     if (fstat(fd, &st) < 0)
-        goto cleanup;
+        return -1;
 
-    if (!S_ISBLK(st.st_mode)) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (!S_ISBLK(st.st_mode))
+        return 0;
 
     /* Attempt to detect via a CDROM specific ioctl */
     if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) >= 0)
-        ret = 1;
-    else
-        ret = 0;
+        return 1;
 
- cleanup:
-    VIR_FORCE_CLOSE(fd);
-    return ret;
+    return 0;
 }
 
 #else
-- 
2.17.1





More information about the libvir-list mailing list