diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-09-24 13:33:54 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-09-24 13:33:54 +0100 |
commit | ccc6d4ac6c2b3fdc4bfca20184ae7a2ef03cd1d6 (patch) | |
tree | a7e3ea9170ee9bb89c50fa821bf00d518cc7d32a | |
parent | 50a508f71363db1ab9db5463b754a54387d2fcf2 (diff) | |
download | rspamd-ccc6d4ac6c2b3fdc4bfca20184ae7a2ef03cd1d6.tar.gz rspamd-ccc6d4ac6c2b3fdc4bfca20184ae7a2ef03cd1d6.zip |
[Minor] Improve handling of directories for rspamc
-rw-r--r-- | src/client/rspamc.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/src/client/rspamc.c b/src/client/rspamc.c index c29808435..ec715abb5 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -1416,39 +1416,59 @@ rspamc_process_dir (struct event_base *ev_base, struct rspamc_command *cmd, const gchar *name, GQueue *attrs) { DIR *d; - struct dirent entry, *pentry = NULL; + struct dirent *entry, **pentry = NULL; gint cur_req = 0; gchar fpath[PATH_MAX]; FILE *in; struct stat st; + gboolean is_reg, is_dir; + gsize name_max, len; - memset (&entry, 0, sizeof (entry)); d = opendir (name); if (d != NULL) { - while (readdir_r (d, &entry, &pentry) == 0) { + /* Portably allocate struct direntry */ + name_max = pathconf (name, _PC_NAME_MAX); + + if (name_max == -1) { + name_max = PATH_MAX + 1; + } + + len = offsetof(struct dirent, d_name) + name_max + 1; + entry = g_malloc0 (len); + + while (readdir_r (d, entry, pentry) == 0) { if (pentry == NULL) { break; } - if (pentry->d_name[0] == '.') { + if (entry->d_name[0] == '.') { continue; } - rspamd_snprintf (fpath, sizeof (fpath), "%s%c%s", - name, G_DIR_SEPARATOR, pentry->d_name); + rspamd_snprintf (fpath, sizeof (fpath), "%s%c%*s", + name, G_DIR_SEPARATOR, + (gint)entry->d_namlen, entry->d_name); + is_reg = FALSE; + is_dir = FALSE; + +#ifdef _DIRENT_HAVE_D_TYPE +#else if (lstat (fpath, &st) == -1) { rspamd_fprintf (stderr, "cannot stat file %s: %s\n", fpath, strerror (errno)); continue; } - if (S_ISDIR (st.st_mode)) { + is_dir = S_ISDIR (st.st_mode); + is_reg = S_ISREG (st.st_mode); +#endif + if (is_dir) { rspamc_process_dir (ev_base, cmd, fpath, attrs); continue; } - else if (S_ISREG (st.st_mode)) { + else if (is_reg) { in = fopen (fpath, "r"); if (in == NULL) { rspamd_fprintf (stderr, "cannot open file %s: %s\n", @@ -1467,6 +1487,8 @@ rspamc_process_dir (struct event_base *ev_base, struct rspamc_command *cmd, } } } + + g_free (entry); } else { fprintf (stderr, "cannot open directory %s: %s\n", name, strerror (errno)); |