From 6b3c6527436a2ff28c3c3344113aeaabfbfaca7b Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Sun, 24 Dec 2023 14:24:21 +0100 Subject: [PATCH 1/5] nut-scanner: fix nutscan_scan_nut_simulation prototype Please CI and add void args Signed-off-by: Arnaud Quette --- tools/nut-scanner/nut-scan.h | 2 +- tools/nut-scanner/scan_nut_simulation.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/nut-scanner/nut-scan.h b/tools/nut-scanner/nut-scan.h index e638a454f4..c2df0c4701 100644 --- a/tools/nut-scanner/nut-scan.h +++ b/tools/nut-scanner/nut-scan.h @@ -155,7 +155,7 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char *start_ip, const char nutscan_device_t * nutscan_scan_nut(const char * startIP, const char * stopIP, const char * port, useconds_t usec_timeout); -nutscan_device_t * nutscan_scan_nut_simulation(); +nutscan_device_t * nutscan_scan_nut_simulation(void); nutscan_device_t * nutscan_scan_avahi(useconds_t usec_timeout); diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index 0d325f4ad5..8bf85e33f8 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -57,7 +57,7 @@ static int filter_ext(const struct dirent *dir) return 0; } -nutscan_device_t * nutscan_scan_nut_simulation() +nutscan_device_t * nutscan_scan_nut_simulation(void) { nutscan_device_t * dev = NULL; struct dirent **namelist; From c04f82b017131e05b209bf75ef7aab3feef1082d Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Sun, 24 Dec 2023 14:47:17 +0100 Subject: [PATCH 2/5] nut-scanner: don't require alphasort Sorting is not needed, and despite alphasort being supported since: * Since glibc 2.10: (_POSIX_C_SOURCE >= 200809L) * || Glibc versions <= 2.19: (_BSD_SOURCE || _SVID_SOURCE) it seems still not supported on some platforms. Signed-off-by: Arnaud Quette --- tools/nut-scanner/scan_nut_simulation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index 8bf85e33f8..d77b0554da 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -69,7 +69,7 @@ nutscan_device_t * nutscan_scan_nut_simulation(void) upsdebugx(1,"Scanning: %s", CONFPATH); - n = scandir(CONFPATH, &namelist, filter_ext, alphasort); + n = scandir(CONFPATH, &namelist, filter_ext, NULL); if (n < 0) { fatal_with_errno(EXIT_FAILURE, "Failed to scandir"); return NULL; From c1a658b1c8910298b9c245630302a2460ca4b709 Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Sun, 24 Dec 2023 14:55:07 +0100 Subject: [PATCH 3/5] nut-scanner: simplify simulation scan code Remove non-portable directory-filtering code, since there is no actual need for it, and very few chances of false positive results. Signed-off-by: Arnaud Quette --- tools/nut-scanner/scan_nut_simulation.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index d77b0554da..756c8998e3 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -44,14 +44,12 @@ static int filter_ext(const struct dirent *dir) if(!dir) return 0; - if(dir->d_type == DT_REG) { /* only deal with regular file */ - const char *ext = strrchr(dir->d_name,'.'); - if((!ext) || (ext == dir->d_name)) - return 0; - else { - if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { - return 1; - } + const char *ext = strrchr(dir->d_name,'.'); + if((!ext) || (ext == dir->d_name)) + return 0; + else { + if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { + return 1; } } return 0; From d4872d324d6f61a08bf610c2de8476b86000f7f8 Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Sun, 24 Dec 2023 16:03:24 +0100 Subject: [PATCH 4/5] nut-scanner: actually simplify simulation scan code Reimplement scan algorithm, and remove non-portable scandir code. Get back to my early implemention, using opendir/readdir, as in common/common.c. This works and is portable! Signed-off-by: Arnaud Quette --- tools/nut-scanner/scan_nut_simulation.c | 50 +++++++++---------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index 756c8998e3..144301b0ab 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Arnaud Quette + * Copyright (C) 2023-2024 Arnaud Quette * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,9 +25,6 @@ #include "nut-scan.h" #include "nut_stdint.h" #include -#if !HAVE_DECL_REALPATH -# include -#endif #define SCAN_NUT_SIMULATION_DRIVERNAME "dummy-ups" @@ -38,28 +35,11 @@ static nutscan_device_t * dev_ret = NULL; static pthread_mutex_t dev_mutex; #endif -/* return 1 when filter is ok (.dev or .seq) */ -static int filter_ext(const struct dirent *dir) -{ - if(!dir) - return 0; - - const char *ext = strrchr(dir->d_name,'.'); - if((!ext) || (ext == dir->d_name)) - return 0; - else { - if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { - return 1; - } - } - return 0; -} - nutscan_device_t * nutscan_scan_nut_simulation(void) { + DIR *dp; + struct dirent *dirp; nutscan_device_t * dev = NULL; - struct dirent **namelist; - int n; #if HAVE_PTHREAD pthread_mutex_init(&dev_mutex, NULL); @@ -67,19 +47,26 @@ nutscan_device_t * nutscan_scan_nut_simulation(void) upsdebugx(1,"Scanning: %s", CONFPATH); - n = scandir(CONFPATH, &namelist, filter_ext, NULL); - if (n < 0) { - fatal_with_errno(EXIT_FAILURE, "Failed to scandir"); + if ((dp = opendir(CONFPATH)) == NULL) { + fatal_with_errno(EXIT_FAILURE, "Failed to open %s", CONFPATH); return NULL; } - else { - while (n--) { - upsdebugx(1,"Found simulation file: %s", namelist[n]->d_name); + + while ((dirp = readdir(dp)) != NULL) + { + upsdebugx(5,"Comparing file %s with simulation file extensions", dirp->d_name); + const char *ext = strrchr(dirp->d_name,'.'); + if((!ext) || (ext == dirp->d_name)) + continue; + + /* Filter on '.dev' and '.seq' extensions' */ + if ((strcmp(ext, ".dev") == 0) || (strcmp(ext, ".seq") == 0)) { + upsdebugx(1,"Found simulation file: %s", dirp->d_name); dev = nutscan_new_device(); dev->type = TYPE_NUT_SIMULATION; dev->driver = strdup(SCAN_NUT_SIMULATION_DRIVERNAME); - dev->port = strdup(namelist[n]->d_name); + dev->port = strdup(dirp->d_name); #ifdef HAVE_PTHREAD pthread_mutex_lock(&dev_mutex); @@ -88,10 +75,9 @@ nutscan_device_t * nutscan_scan_nut_simulation(void) #ifdef HAVE_PTHREAD pthread_mutex_unlock(&dev_mutex); #endif - free(namelist[n]); } - free(namelist); } + closedir(dp); #if HAVE_PTHREAD pthread_mutex_destroy(&dev_mutex); From 6577bb77aa957f98b9f8d62c65efd848b7dc7226 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sun, 24 Dec 2023 22:25:38 +0100 Subject: [PATCH 5/5] Fix declaration-after-code in scan_nut_simulation.c Signed-off-by: Jim Klimov --- tools/nut-scanner/scan_nut_simulation.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/nut-scanner/scan_nut_simulation.c b/tools/nut-scanner/scan_nut_simulation.c index 144301b0ab..402293ad47 100644 --- a/tools/nut-scanner/scan_nut_simulation.c +++ b/tools/nut-scanner/scan_nut_simulation.c @@ -54,8 +54,10 @@ nutscan_device_t * nutscan_scan_nut_simulation(void) while ((dirp = readdir(dp)) != NULL) { - upsdebugx(5,"Comparing file %s with simulation file extensions", dirp->d_name); - const char *ext = strrchr(dirp->d_name,'.'); + const char *ext; + + upsdebugx(5, "Comparing file %s with simulation file extensions", dirp->d_name); + ext = strrchr(dirp->d_name, '.'); if((!ext) || (ext == dirp->d_name)) continue;