From: Vsevolod Stakhov Date: Wed, 10 Sep 2014 12:59:23 +0000 (+0100) Subject: Fix directories processing in rspamc. X-Git-Tag: 0.7.0~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=719752929adf89a5ddbaf4542475cde1af9ab676;p=thirdparty%2Frspamd.git Fix directories processing in rspamc. --- diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 9db789005b..3a29f3c1d7 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -25,6 +25,9 @@ #include "config.h" #include "util.h" #include "http.h" +#include +#include +#include #include "rspamdclient.h" #include "utlist.h" @@ -714,6 +717,50 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd, } } +static void +rspamc_process_dir (struct event_base *ev_base, struct rspamc_command *cmd, + const gchar *name, GHashTable *attrs) +{ + DIR *d; + gint cur_req = 0; + struct dirent *ent; + FILE *in; + char filebuf[PATH_MAX]; + + d = opendir (name); + + if (d != NULL) { + while ((ent = readdir (d))) { + if (ent->d_type == DT_REG || ent->d_type == DT_UNKNOWN) { + rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%s", + name, G_DIR_SEPARATOR, ent->d_name); + if (access (filebuf, R_OK) != -1) { + in = fopen (filebuf, "r"); + if (in == NULL) { + fprintf (stderr, "cannot open file %s\n", filebuf); + exit (EXIT_FAILURE); + } + rspamc_process_input (ev_base, cmd, in, filebuf, attrs); + cur_req++; + fclose (in); + if (cur_req >= max_requests) { + cur_req = 0; + /* Wait for completion */ + event_base_loop (ev_base, 0); + } + } + } + } + } + else { + fprintf (stderr, "cannot open directory %s\n", name); + exit (EXIT_FAILURE); + } + + closedir (d); + event_base_loop (ev_base, 0); +} + gint main (gint argc, gchar **argv, gchar **env) { @@ -722,6 +769,7 @@ main (gint argc, gchar **argv, gchar **env) struct rspamc_command *cmd; FILE *in = NULL; struct event_base *ev_base; + struct stat st; kwattrs = g_hash_table_new (rspamd_str_hash, rspamd_str_equal); @@ -751,11 +799,6 @@ main (gint argc, gchar **argv, gchar **env) else { cmd = check_rspamc_command ("symbols"); /* Symbols command */ start_argc = 1; - in = fopen (argv[1], "r"); - if (in == NULL) { - fprintf (stderr, "cannot open file %s\n", argv[1]); - exit (EXIT_FAILURE); - } } } else { @@ -796,14 +839,25 @@ main (gint argc, gchar **argv, gchar **env) } else { for (i = start_argc; i < argc; i++) { - in = fopen (argv[i], "r"); - if (in == NULL) { - fprintf (stderr, "cannot open file %s\n", argv[i]); + if (stat (argv[i], &st) == -1) { + fprintf (stderr, "cannot stat file %s\n", argv[i]); exit (EXIT_FAILURE); } - rspamc_process_input (ev_base, cmd, in, argv[i], kwattrs); - cur_req++; - fclose (in); + if (S_ISDIR (st.st_mode)) { + /* Directories are processed with a separate limit */ + rspamc_process_dir (ev_base, cmd, argv[i], kwattrs); + cur_req = 0; + } + else { + in = fopen (argv[i], "r"); + if (in == NULL) { + fprintf (stderr, "cannot open file %s\n", argv[i]); + exit (EXIT_FAILURE); + } + rspamc_process_input (ev_base, cmd, in, argv[i], kwattrs); + cur_req++; + fclose (in); + } if (cur_req >= max_requests) { cur_req = 0; /* Wait for completion */