From d3e71563db3f9127a36390b557ef714c638bdaf5 Mon Sep 17 00:00:00 2001 From: Marc Stern Date: Thu, 15 Dec 2022 12:09:03 +0100 Subject: [PATCH 1/5] Option -q (quiet): only display needed information --- modsec-sdbm-util.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/modsec-sdbm-util.c b/modsec-sdbm-util.c index 420a493..ed54ad0 100644 --- a/modsec-sdbm-util.c +++ b/modsec-sdbm-util.c @@ -40,6 +40,7 @@ #define p printf #define v if (verbose) printf #endif +#define p2 if (!quiet) printf #define IS_EXPIRED 128 @@ -51,7 +52,7 @@ #define EXTRACT 64 -int verbose = 0; +int verbose = 0, quiet = 0; static char progress_feedback[] = {'|', '/', '-', '\\'}; @@ -231,6 +232,12 @@ static int dump_database(apr_pool_t *pool, apr_sdbm_t *db, int action, char *new char *file_name = "/new_db"; int full_len = 1 + strlen(new_db_path) + strlen(file_name); char *full_path = (char *) malloc(full_len); + if (!full_path) { + v("Failed to retrieve the first key of the database.\n"); + fret = -1; + goto end; + } + strcpy(full_path, new_db_path); strcat(full_path, file_name); full_path[full_len] = '\0'; @@ -343,13 +350,13 @@ static int dump_database(apr_pool_t *pool, apr_sdbm_t *db, int action, char *new } if (action & SHRINK || action & STATUS) { - printf("\n"); - printf("Total of %.0f elements processed.\n", elements); - printf("%d elements removed.\n", removed); - printf("Expired elements: %d, inconsistent items: %d\n", expired_datum, + p2("\n"); + p2("Total of %.0f elements processed.\n", elements); + p2("%d elements removed.\n", removed); + p2("Expired elements: %d, inconsistent items: %d\n", expired_datum, bad_datum); if (expired_datum+bad_datum != 0 && elements !=0) - printf("Fragmentation rate: %2.2f%% of the database is/was dirty " \ + p2("Fragmentation rate: %2.2f%% of the database is/was dirty " \ "data.\n", 100*(expired_datum+bad_datum)/elements); } @@ -397,6 +404,7 @@ void help (void) { p(" -r, remove: Expects to receive a key as a paramter to be removed;\n"); p(" -V. version: Print version information.\n"); p(" -v, verbose: Some extra information about what this utility is doing.\n"); + p(" -q, quiet: Only display needed information.\n"); p(" -h, help: this message.\n\n"); } @@ -447,6 +455,9 @@ int main (int argc, char **argv) case 'v': verbose = 1; break; + case 'q': + quiet = 1; + break; case 'V': version(); return 0; @@ -485,7 +496,7 @@ int main (int argc, char **argv) apr_dir_t *db_dest_dir; // test to see if the target directory exists - printf ("Checking target directory: %s\n", new_db_path); + p2("Checking target directory: %s\n", new_db_path); ret = apr_dir_open(&db_dest_dir, new_db_path, pool); if (ret != APR_SUCCESS) { char errmsg[120]; @@ -493,19 +504,19 @@ int main (int argc, char **argv) goto that_is_all_folks; } apr_dir_close(db_dest_dir); - printf("Target directory exists.\n"); + p2("Target directory exists.\n"); - printf ("Opening file: %s\n", file); + p2("Opening file: %s\n", file); ret = open_sdbm(pool, &db, argv[index]); if (ret < 0) { printf("Failed to open sdbm: %s\n", file); goto that_is_all_folks; } - printf("Database ready to be used.\n"); + p2("Database ready to be used.\n"); if (to_remove) { - printf("Removing key: %s\n", to_remove); + p2("Removing key: %s\n", to_remove); remove_key(pool, db, to_remove); continue; } From af639d97b9ecabe3861657d2226914ad1c5f296a Mon Sep 17 00:00:00 2001 From: Marc Stern Date: Thu, 15 Dec 2022 12:16:59 +0100 Subject: [PATCH 2/5] minor syntax change --- modsec-sdbm-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modsec-sdbm-util.c b/modsec-sdbm-util.c index ed54ad0..d7c4089 100644 --- a/modsec-sdbm-util.c +++ b/modsec-sdbm-util.c @@ -40,7 +40,7 @@ #define p printf #define v if (verbose) printf #endif -#define p2 if (!quiet) printf +#define p2 if (!quiet) p #define IS_EXPIRED 128 From f8bf89b159fabdedb9f2eab96ac075758ad1332f Mon Sep 17 00:00:00 2001 From: Marc Stern Date: Thu, 15 Dec 2022 12:23:42 +0100 Subject: [PATCH 3/5] revert last change --- modsec-sdbm-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modsec-sdbm-util.c b/modsec-sdbm-util.c index d7c4089..ed54ad0 100644 --- a/modsec-sdbm-util.c +++ b/modsec-sdbm-util.c @@ -40,7 +40,7 @@ #define p printf #define v if (verbose) printf #endif -#define p2 if (!quiet) p +#define p2 if (!quiet) printf #define IS_EXPIRED 128 From 845e9b1fcf4ffcdc531435dcf0b842bc4ca3d5a7 Mon Sep 17 00:00:00 2001 From: Marc Stern Date: Thu, 2 Feb 2023 11:22:34 +0100 Subject: [PATCH 4/5] Fixed error message --- modsec-sdbm-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modsec-sdbm-util.c b/modsec-sdbm-util.c index ed54ad0..0339d46 100644 --- a/modsec-sdbm-util.c +++ b/modsec-sdbm-util.c @@ -233,7 +233,7 @@ static int dump_database(apr_pool_t *pool, apr_sdbm_t *db, int action, char *new int full_len = 1 + strlen(new_db_path) + strlen(file_name); char *full_path = (char *) malloc(full_len); if (!full_path) { - v("Failed to retrieve the first key of the database.\n"); + v("Cannot allocate memory.\n"); fret = -1; goto end; } From 006ed746e09b0f07c7ab97cd1f077fc65e28b659 Mon Sep 17 00:00:00 2001 From: Marc Stern Date: Sat, 18 Mar 2023 18:17:35 +0100 Subject: [PATCH 5/5] Missing 'q' in options check --- modsec-sdbm-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modsec-sdbm-util.c b/modsec-sdbm-util.c index 0339d46..123782d 100644 --- a/modsec-sdbm-util.c +++ b/modsec-sdbm-util.c @@ -424,7 +424,7 @@ int main (int argc, char **argv) return 0; } - while ((c = getopt (argc, argv, "nkxsdahVvur:D:")) != -1) + while ((c = getopt (argc, argv, "qnkxsdahVvur:D:")) != -1) switch (c) { case 'd':