[PATCH 3/3] storage/util: replace unnecessary while loop by if

Jiang Jiacheng jiangjiacheng at huawei.com
Thu Jan 5 11:51:08 UTC 2023


These while loops exit directly due to break after entering.
Use if instead of these while loops.

Signed-off-by: Jiang Jiacheng <jiangjiacheng at huawei.com>
---
 src/storage/storage_util.c | 4 +---
 src/util/virutil.c         | 6 +-----
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
index 3871718b09..520fdd03d0 100644
--- a/src/storage/storage_util.c
+++ b/src/storage/storage_util.c
@@ -3784,12 +3784,10 @@ getNewStyleBlockDevice(const char *lun_path,
     if (virDirOpen(&block_dir, block_path) < 0)
         return -1;
 
-    while ((direrr = virDirRead(block_dir, &block_dirent, block_path)) > 0) {
+    if ((direrr = virDirRead(block_dir, &block_dirent, block_path)) > 0) {
         *block_device = g_strdup(block_dirent->d_name);
 
         VIR_DEBUG("Block device is '%s'", *block_device);
-
-        break;
     }
 
     if (direrr < 0)
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 7e246d22d1..ddc66001cb 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -1432,15 +1432,11 @@ virHostHasIOMMU(void)
 {
     g_autoptr(DIR) iommuDir = NULL;
     struct dirent *iommuGroup = NULL;
-    int direrr;
 
     if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0)
         return false;
 
-    while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0)
-        break;
-
-    if (direrr < 0 || !iommuGroup)
+    if (virDirRead(iommuDir, &iommuGroup, NULL) < 0 || !iommuGroup)
         return false;
 
     return true;
-- 
2.33.0



More information about the libvir-list mailing list