Skip to contentSkip to conversation

use process_directory() to read dir

PATCH In review Sergey Senozhatsky 1 participant 9 messages Sep 19, 2013

lore
Share & tools
lore

Sergey Senozhatsky

author
#0 lore raw

cover use process_directory() to read dir

Hello,

This patch series removes some code duplication in trivial cases and replaces explicit opendir()/readdir() handling with existing common lib process_directory() helper.

 src/calibrate/calibrate.cpp | 151 ++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------
 src/devices/alsa.cpp        |  46 +++++++++++++++++-----------------------------
 src/devices/alsa.h          |   4 ++--
 src/devices/network.cpp     |   8 ++++----
 src/devices/rfkill.cpp      |  46 +++++++++++++++++-----------------------------
 src/devices/usb.cpp         |  79 +++++++++++++++++++++++++++++++------------------------------------------------
 src/lib.cpp                 |   4 ----
 src/tuning/ethernet.cpp     |  10 +++++-----
 src/tuning/tuningusb.cpp    |  46 ++++++++++++++--------------------------------
 9 files changed, 138 insertions(+), 256 deletions(-)
author
#1 lore raw

Re: [Powertop] [PATCH 1/8] lib: unify process_directory()

remove lo' check from process_directory() and force net callers to check for passed d_name. since process_directory() called from different sites lo' check is overhead.

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/lib.cpp | 4 ----
 1 file changed, 4 deletions(-)
src/lib.cpp +0 -4
index 881e52f..f27ec2f 100644
@@ -448,12 +448,8 @@ void process_directory(const char *d_name, callback fn)
 			break;
 		if (entry->d_name[0] == '.')
 			continue;
-		if (strcmp(entry->d_name, "lo")==0)
-			continue;
-
 		fn(entry->d_name);
 	}
-
 	closedir(dir);
 }
 
author
#2 lore raw

PATCH 2/8 alsa: use process_directory()

In reply to Sergey Senozhatsky

use process_directory() helper

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/devices/alsa.cpp | 46 +++++++++++++++++-----------------------------
 src/devices/alsa.h   |  4 ++--
 2 files changed, 19 insertions(+), 31 deletions(-)
src/devices/alsa.cpp +17 -29
index 33a52f5..5dee334 100644
@@ -27,10 +27,8 @@
 
 #include <stdio.h>
 #include <sys/types.h>
-#include <dirent.h>
 #include <unistd.h>
 
-
 using namespace std;
 
 #include "device.h"
@@ -42,7 +40,7 @@ using namespace std;
 #include <string.h>
 #include <unistd.h>
 
-alsa::alsa(char *_name, char *path): device()
+alsa::alsa(const char *_name, const char *path): device()
 {
 	ifstream file;
 
@@ -152,39 +150,29 @@ const char * alsa::device_name(void)
 	return name;
 }
 
-void create_all_alsa(void)
+static void create_all_alsa_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
+	class alsa *bl;
+	ofstream file;
 
-	dir = opendir("/sys/class/sound/card0/");
-	if (!dir)
+	if (strncmp(d_name, "hwC", 3) != 0)
 		return;
-	while (1) {
-		class alsa *bl;
-		ofstream file;
-		entry = readdir(dir);
-		if (!entry)
-			break;
-		if (strncmp(entry->d_name, "hwC", 3) != 0)
-			continue;
-		sprintf(filename, "/sys/class/sound/card0/%s/power_on_acct", entry->d_name);
-
-		if (access(filename, R_OK) != 0)
-			continue;
-
-		sprintf(filename, "/sys/class/sound/card0/%s", entry->d_name);
-
-		bl = new class alsa(entry->d_name, filename);
-		all_devices.push_back(bl);
-		register_parameter("alsa-codec-power", 0.5);
-	}
-	closedir(dir);
 
-}
+	sprintf(filename, "/sys/class/sound/card0/%s/power_on_acct", d_name);
+	if (access(filename, R_OK) != 0)
+		return;
 
+	sprintf(filename, "/sys/class/sound/card0/%s", d_name);
+	bl = new class alsa(d_name, filename);
+	all_devices.push_back(bl);
+	register_parameter("alsa-codec-power", 0.5);
+}
 
+void create_all_alsa(void)
+{
+	process_directory("/sys/class/sound/card0/", create_all_alsa_callback);
+}
 
 double alsa::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
 {
src/devices/alsa.h +2 -2
index 5aebfd7..f7b4b97 100644
@@ -41,7 +41,7 @@ class alsa: public device {
 	int rindex;
 public:
 
-	alsa(char *_name, char *path);
+	alsa(const char *_name, const char *path);
 
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
@@ -63,4 +63,4 @@ public:
 extern void create_all_alsa(void);
 
 
-#endif
\ No newline at end of file
+#endif
author
#3 lore raw

PATCH 3/8 calibrate: use process_directory()

In reply to Sergey Senozhatsky

use process_directory() helper

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/calibrate/calibrate.cpp | 151 ++++++++++++++------------------------------
 1 file changed, 48 insertions(+), 103 deletions(-)
src/calibrate/calibrate.cpp +48 -103
index 5457cf8..db368e0 100644
@@ -33,7 +33,6 @@
 #include <pthread.h>
 #include <math.h>
 #include <sys/types.h>
-#include <dirent.h>
 
 #include "../parameters/parameters.h"
 extern "C" {
@@ -85,45 +84,32 @@ static void restore_all_sysfs(void)
 	set_wifi_power_saving("wlan0", wireless_PS);
 }
 
-static void find_all_usb(void)
+static void find_all_usb_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
+	ifstream file;
 
-	dir = opendir("/sys/bus/usb/devices/");
-	if (!dir)
+	sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name);
+	if (access(filename, R_OK) != 0)
 		return;
-	while (1) {
-		ifstream file;
-
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", entry->d_name);
-		if (access(filename, R_OK)!=0)
-			continue;
-
-		sprintf(filename, "/sys/bus/usb/devices/%s/power/idVendor", entry->d_name);
-		file.open(filename, ios::in);
-		if (file) {
-			file.getline(filename, 4096);
-			file.close();
-			if (strcmp(filename, "1d6b")==0)
-				continue;
-		}
 
-		sprintf(filename, "/sys/bus/usb/devices/%s/power/control", entry->d_name);
+	sprintf(filename, "/sys/bus/usb/devices/%s/power/idVendor", d_name);
+	file.open(filename, ios::in);
+	if (file) {
+		file.getline(filename, 4096);
+		file.close();
+		if (strcmp(filename, "1d6b") == 0)
+			return;
+	}
 
-		save_sysfs(filename);
+	sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name);
+	save_sysfs(filename);
+	usb_devices.push_back(filename);
+}
 
-		usb_devices.push_back(filename);
-	}
-	closedir(dir);
+static void find_all_usb(void)
+{
+	process_directory("/sys/bus/usb/devices/", find_all_usb_callback);
 }
 
 static void suspend_all_usb_devices(void)
@@ -134,33 +120,19 @@ static void suspend_all_usb_devices(void)
 		write_sysfs(usb_devices[i], "auto\n");
 }
 
-
-static void find_all_rfkill(void)
+static void find_all_rfkill_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
-
-	dir = opendir("/sys/class/rfkill/");
-	if (!dir)
+	sprintf(filename, "/sys/class/rfkill/%s/soft", d_name);
+	if (access(filename, R_OK) != 0)
 		return;
-	while (1) {
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/class/rfkill/%s/soft", entry->d_name);
-		if (access(filename, R_OK)!=0)
-			continue;
-
-		save_sysfs(filename);
+	save_sysfs(filename);
+	rfkill_devices.push_back(filename);
+}
 
-		rfkill_devices.push_back(filename);
-	}
-	closedir(dir);
+static void find_all_rfkill(void)
+{
+	process_directory("/sys/class/rfkill/", find_all_rfkill_callback);
 }
 
 static void rfkill_all_radios(void)
@@ -178,35 +150,22 @@ static void unrfkill_all_radios(void)
 		write_sysfs(rfkill_devices[i], "0\n");
 }
 
-static void find_backlight(void)
+static void find_backlight_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
-
-	dir = opendir("/sys/class/backlight/");
-	if (!dir)
+	sprintf(filename, "/sys/class/backlight/%s/brightness", d_name);
+	if (access(filename, R_OK) != 0)
 		return;
-	while (1) {
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/class/backlight/%s/brightness", entry->d_name);
-		if (access(filename, R_OK)!=0)
-			continue;
 
-		save_sysfs(filename);
-
-		backlight_devices.push_back(filename);
+	save_sysfs(filename);
+	backlight_devices.push_back(filename);
+	sprintf(filename, "/sys/class/backlight/%s/max_brightness", d_name);
+	blmax = read_sysfs(filename);
+}
 
-		sprintf(filename, "/sys/class/backlight/%s/max_brightness", entry->d_name);
-		blmax = read_sysfs(filename);
-	}
-	closedir(dir);
+static void find_backlight(void)
+{
+	process_directory("/sys/class/backlight/", find_backlight_callback);
 }
 
 static void lower_backlight(void)
@@ -217,34 +176,20 @@ static void lower_backlight(void)
 		write_sysfs(backlight_devices[i], "0\n");
 }
 
-
-static void find_scsi_link(void)
+static void find_scsi_link_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
-
-	dir = opendir("/sys/class/scsi_host/");
-	if (!dir)
+	sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", d_name);
+	if (access(filename, R_OK)!=0)
 		return;
-	while (1) {
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", entry->d_name);
-		if (access(filename, R_OK)!=0)
-			continue;
 
-		save_sysfs(filename);
-
-		scsi_link_devices.push_back(filename);
+	save_sysfs(filename);
+	scsi_link_devices.push_back(filename);
+}
 
-	}
-	closedir(dir);
+static void find_scsi_link(void)
+{
+	process_directory("/sys/class/scsi_host/", find_scsi_link_callback);
 }
 
 static void set_scsi_link(const char *state)
author
#4 lore raw

PATCH 4/8 network: use updated process_directory()

In reply to Sergey Senozhatsky

check for passed `lo' d_name

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/devices/network.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/devices/network.cpp +4 -4
index 3b67610..d0827ef 100644
@@ -30,7 +30,6 @@
 
 #include <stdio.h>
 #include <sys/types.h>
-#include <dirent.h>
 #include <libgen.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -344,11 +343,13 @@ const char * network::device_name(void)
 	return name;
 }
 
-void netdev_callback(const char *d_name)
+static void netdev_callback(const char *d_name)
 {
 	std::string f_name("/sys/class/net/");
-	f_name.append(d_name);
+	if (strcmp(d_name, "lo") == 0)
+		return;
 
+	f_name.append(d_name);
 	network *bl = new(std::nothrow) class network(d_name, f_name.c_str());
 	if (bl) {
 		all_devices.push_back(bl);
@@ -356,7 +357,6 @@ void netdev_callback(const char *d_name)
 	}
 }
 
-
 void create_all_nics(callback fn)
 {
 	if (!fn)
author
#5 lore raw

PATCH 5/8 rfkill: use process_directory()

In reply to Sergey Senozhatsky

use process_directory() helper

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/devices/rfkill.cpp | 46 +++++++++++++++++-----------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)
src/devices/rfkill.cpp +17 -29
index 2404432..01aec23 100644
@@ -27,7 +27,6 @@
 
 #include <stdio.h>
 #include <sys/types.h>
-#include <dirent.h>
 #include <libgen.h>
 #include <unistd.h>
 
@@ -136,41 +135,30 @@ const char * rfkill::device_name(void)
 	return name;
 }
 
-void create_all_rfkills(void)
+static void create_all_rfkills_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
 	char name[4096];
+	class rfkill *bl;
+	ifstream file;
 
-	dir = opendir("/sys/class/rfkill/");
-	if (!dir)
-		return;
-	while (1) {
-		class rfkill *bl;
-		ifstream file;
-		entry = readdir(dir);
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-		sprintf(filename, "/sys/class/rfkill/%s/name", entry->d_name);
-		strcpy(name, entry->d_name);
-		file.open(filename, ios::in);
-		if (file) {
-			file.getline(name, 100);
-			file.close();
-		}
-
-		sprintf(filename, "/sys/class/rfkill/%s", entry->d_name);
-		bl = new class rfkill(name, filename);
-		all_devices.push_back(bl);
+	sprintf(filename, "/sys/class/rfkill/%s/name", d_name);
+	strcpy(name, d_name);
+	file.open(filename, ios::in);
+	if (file) {
+		file.getline(name, 100);
+		file.close();
 	}
-	closedir(dir);
 
+	sprintf(filename, "/sys/class/rfkill/%s", d_name);
+	bl = new class rfkill(name, filename);
+	all_devices.push_back(bl);
 }
 
-
+void create_all_rfkills(void)
+{
+	process_directory("/sys/class/rfkill/", create_all_rfkills_callback);
+}
 
 double rfkill::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
 {
@@ -185,4 +173,4 @@ double rfkill::power_usage(struct result_bundle *result, struct parameter_bundle
 	power += util * factor / 100.0;
 
 	return power;
-}
\ No newline at end of file
+}
author
#6 lore raw

PATCH 6/8 usb: use process_directory()

In reply to Sergey Senozhatsky

use process_directory() helper

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/devices/usb.cpp | 79 +++++++++++++++++++++--------------------------------
 1 file changed, 31 insertions(+), 48 deletions(-)
src/devices/usb.cpp +31 -48
index e7542a8..2c5d38b 100644
@@ -25,11 +25,9 @@
 #include "usb.h"
 
 #include <string.h>
-
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
-#include <dirent.h>
 
 #include "../lib.h"
 #include "../parameters/parameters.h"
@@ -186,57 +184,42 @@ double usbdevice::power_usage(struct result_bundle *result, struct parameter_bun
 	return power;
 }
 
-
-void create_all_usb_devices(void)
+static void create_all_usb_devices_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
 	char filename[4096];
-
-	dir = opendir("/sys/bus/usb/devices/");
-	if (!dir)
+	ifstream file;
+	class usbdevice *usb;
+	char device_name[4096];
+	char vendorid[64], devid[64];
+	char devid_name[4096];
+
+	sprintf(filename, "/sys/bus/usb/devices/%s", d_name);
+	sprintf(device_name, "%s/power/active_duration", filename);
+	if (access(device_name, R_OK) != 0)
 		return;
-	while (1) {
-		ifstream file;
-		class usbdevice *usb;
-		char device_name[4096];
-		char vendorid[64], devid[64];
-		char devid_name[4096];
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/bus/usb/devices/%s", entry->d_name);
-
-		sprintf(device_name, "%s/power/active_duration", filename);
-		if (access(device_name, R_OK)!=0)
-			continue;
-
-		sprintf(device_name, "%s/idVendor", filename);
-		file.open(device_name, ios::in);
-		if (file)
-			file.getline(vendorid, 64);
-		file.close();
-		sprintf(device_name, "%s/idProduct", filename);
-		file.open(device_name, ios::in);
-		if (file)
-			file.getline(devid, 64);
-		file.close();
-
-		sprintf(devid_name, "usb-device-%s-%s", vendorid, devid);
 
-		sprintf(device_name, "usb-device-%s-%s-%s", entry->d_name, vendorid, devid);
+	sprintf(device_name, "%s/idVendor", filename);
+	file.open(device_name, ios::in);
+	if (file)
+		file.getline(vendorid, 64);
+	file.close();
+	sprintf(device_name, "%s/idProduct", filename);
+	file.open(device_name, ios::in);
+	if (file)
+		file.getline(devid, 64);
+	file.close();
 
-		if (result_device_exists(device_name))
-			continue;
+	sprintf(devid_name, "usb-device-%s-%s", vendorid, devid);
+	sprintf(device_name, "usb-device-%s-%s-%s", d_name, vendorid, devid);
+	if (result_device_exists(device_name))
+		return;
 
-		usb = new class usbdevice(device_name, filename, devid_name);
-		all_devices.push_back(usb);
+	usb = new class usbdevice(device_name, filename, devid_name);
+	all_devices.push_back(usb);
+	register_parameter(devid_name, 0.1);
+}
 
-		register_parameter(devid_name, 0.1);
-	}
-	closedir(dir);
+void create_all_usb_devices(void)
+{
+	process_directory("/sys/bus/usb/devices/", create_all_usb_devices_callback);
 }
author
#7 lore raw

PATCH 7/8 tuning ethernet: use updated process_directory()

In reply to Sergey Senozhatsky

check for `lo' d_name in ethtunable_callback()

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/tuning/ethernet.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
src/tuning/ethernet.cpp +5 -5
index 3164b31..3d36112 100644
@@ -25,6 +25,7 @@
 
 #include "tuning.h"
 #include "tunable.h"
+#include <sys/types.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,12 +33,8 @@
 #include <utility>
 #include <iostream>
 #include <fstream>
-#include <unistd.h>
-#include <dirent.h>
 #include <sys/socket.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <dirent.h>
 #include <linux/types.h>
 #include <net/if.h>
 #include <linux/sockios.h>
@@ -146,7 +143,10 @@ const char *ethernet_tunable::toggle_script(void)
 
 void ethtunable_callback(const char *d_name)
 {
-	class ethernet_tunable *eth = new(std::nothrow) class ethernet_tunable(d_name);
+	class ethernet_tunable *eth;
+	if (strcmp(d_name, "lo") == 0)
+		return;
+	eth = new(std::nothrow) class ethernet_tunable(d_name);
 	if (eth)
 		all_tunables.push_back(eth);
 }
author
#8 lore raw

PATCH 8/8 tuningusb: use process_directory()

In reply to Sergey Senozhatsky

use process_directory() helper

Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/tuning/tuningusb.cpp | 46 ++++++++++++++--------------------------------
 1 file changed, 14 insertions(+), 32 deletions(-)
src/tuning/tuningusb.cpp +14 -32
index 74bbacf..27a6dca 100644
@@ -31,9 +31,6 @@
 #include <utility>
 #include <iostream>
 #include <fstream>
-#include <unistd.h>
-#include <sys/types.h>
-#include <dirent.h>
 
 #include "../lib.h"
 
@@ -116,40 +113,25 @@ const char *usb_tunable::toggle_script(void)
 	return toggle_good;
 }
 
-void add_usb_tunables(void)
+static void add_usb_callback(const char *d_name)
 {
-	struct dirent *entry;
-	DIR *dir;
+	class usb_tunable *usb;
 	char filename[4096];
 
-	dir = opendir("/sys/bus/usb/devices/");
-	if (!dir)
+	sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name);
+	if (access(filename, R_OK) != 0)
 		return;
-	while (1) {
-		class usb_tunable *usb;
-
-		entry = readdir(dir);
-
-		if (!entry)
-			break;
-		if (entry->d_name[0] == '.')
-			continue;
-
-		sprintf(filename, "/sys/bus/usb/devices/%s/power/control", entry->d_name);
-
-		if (access(filename, R_OK) != 0)
-			continue;
-
-		sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", entry->d_name);
-		if (access(filename, R_OK)!=0)
-			continue;
 
-		sprintf(filename, "/sys/bus/usb/devices/%s", entry->d_name);
-
-		usb = new class usb_tunable(filename, entry->d_name);
+	sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name);
+	if (access(filename, R_OK)!=0)
+		return;
 
-		all_tunables.push_back(usb);
+	sprintf(filename, "/sys/bus/usb/devices/%s", d_name);
+	usb = new class usb_tunable(filename, d_name);
+	all_tunables.push_back(usb);
+}
 
-	}
-	closedir(dir);
+void add_usb_tunables(void)
+{
+	process_directory("/sys/bus/usb/devices/", add_usb_callback);
 }