log: actually do substring matches with fnmatch

Historically we matched log filters with strstr(), and when switching to
fnmatch in cbb0fd3cfd, it was stated that
we would continue to match substrings, with "foo" being equivalent to
"*foo*". Unfortuntely I forget to provide the code to actually make that
happen. This fixes it to prepend and append "*". We don't bother to
check if the pattern already has a leading/trailing '*', because
"**foo**" will match the same as "*foo*".

Reviewed-by: Erik Skultety <eskultet@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2018-05-14 14:51:14 +01:00
parent fe8a06798d
commit 8fe30b2167

View File

@ -1409,6 +1409,7 @@ virLogFilterNew(const char *match,
{ {
virLogFilterPtr ret = NULL; virLogFilterPtr ret = NULL;
char *mdup = NULL; char *mdup = NULL;
size_t mlen = strlen(match);
virCheckFlags(VIR_LOG_STACK_TRACE, NULL); virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
@ -1418,9 +1419,16 @@ virLogFilterNew(const char *match,
return NULL; return NULL;
} }
if (VIR_STRDUP_QUIET(mdup, match) < 0) /* We must treat 'foo' as equiv to '*foo*' for fnmatch
* todo substring matches, so add 2 extra bytes
*/
if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)
return NULL; return NULL;
mdup[0] = '*';
memcpy(mdup + 1, match, mlen);
mdup[mlen + 1] = '*';
if (VIR_ALLOC_QUIET(ret) < 0) { if (VIR_ALLOC_QUIET(ret) < 0) {
VIR_FREE(mdup); VIR_FREE(mdup);
return NULL; return NULL;