From f2fe10e3918bcab97a5948c3d88ca91a35b0ede2 Mon Sep 17 00:00:00 2001
From: lixiaokeng <lixiaokeng@huawei.com>
Date: Wed, 1 Jul 2020 11:07:06 +0000
Subject: [PATCH] lvm: code reduce cyclomatic complexity

enhance code and reduce cyclomatic complexity

Fix issue: https://gitee.com/src-openeuler/lvm2/issues/I4RZQQ

Signed-off-by:lixiaokeng <lixiaokeng@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
 lib/device/dev-cache.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
index 94e39fa..868a218 100644
--- a/lib/device/dev-cache.c
+++ b/lib/device/dev-cache.c
@@ -1059,6 +1059,19 @@ static int _device_in_udev_db(const dev_t d)
 	return 0;
 }
 
+bool time_out(long* time_use, struct timeval start)
+{
+	struct timeval end;
+
+	if (!gettimeofday(&end, NULL)) {
+		*time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
+		if (*time_use > udev_getdev_timemax) {
+			return true;
+		}
+	}
+	return false;
+}
+
 static int _insert_udev_dir(struct udev *udev, const char *dir)
 {
 	struct udev_enumerate *udev_enum = NULL;
@@ -1067,16 +1080,13 @@ static int _insert_udev_dir(struct udev *udev, const char *dir)
 	struct udev_device *device;
 	int r = 1;
 	int ret = -1;
-	struct timeval start, end;
+	struct timeval start;
 	long time_use = 0;
 
 	ret = gettimeofday(&start,NULL);
 	udev_enum = udev_enumerate_new(udev);
-	if (!ret && !gettimeofday(&end,NULL)) {
-		time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
-		if (time_use > udev_getdev_timemax) {
-			log_print_unless_silent("Call udev_enumerate_new use %ld usec", time_use);
-		}
+	if (!ret && time_out(&time_use, start)) {
+		log_print_unless_silent("Call udev_enumerate_new use %ld usec", time_use);
 	}
 	if (!udev_enum) {
 		log_error("Failed to udev_enumerate_new.");
@@ -1110,14 +1120,12 @@ static int _insert_udev_dir(struct udev *udev, const char *dir)
 					 entry_name);
 			continue;
 		}
+
 		ret = gettimeofday(&start,NULL);
 		node_name = udev_device_get_devnode(device);
-		if (!ret && !gettimeofday(&end,NULL)) {
-			time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
-			if (time_use > udev_getdev_timemax) {
-				log_print_unless_silent("Call udev_device_get_devnode use %ld usec, device:%s",
-							time_use, entry_name);
-			}
+		if (!ret && time_out(&time_use, start)) {
+			log_print_unless_silent("Call udev_device_get_devnode use %ld usec, device:%s",
+					time_use, entry_name);
 		}
 
 		if (!node_name)
@@ -1128,12 +1136,9 @@ static int _insert_udev_dir(struct udev *udev, const char *dir)
 
 		ret = gettimeofday(&start,NULL);
 		first_entry = udev_device_get_devlinks_list_entry(device);
-		if (!ret && !gettimeofday(&end,NULL)) {
-			time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
-			if (time_use > udev_getdev_timemax) {
-				log_print_unless_silent("Call udev_device_get_devlinks_list_entry use %ld usec, dev:%s",
-							time_use, entry_name);
-			}
+		if (!ret && time_out(&time_use, start)) {
+			log_print_unless_silent("Call udev_device_get_devlinks_list_entry use %ld usec, dev:%s",
+					time_use, entry_name);
 		}
 		udev_list_entry_foreach(symlink_entry, first_entry) {
 			if (!(symlink_name = udev_list_entry_get_name(symlink_entry)))
-- 
1.8.3.1

