Changeset
src/util/virlog.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
Git apply log
Switched to a new branch '20180516111716.5879-1-berrange@redhat.com'
Applying: log: actually do substring matches with fnmatch
To https://github.com/patchew-project/libvirt
 + d3a0666...4484a93 patchew/20180516111716.5879-1-berrange@redhat.com -> patchew/20180516111716.5879-1-berrange@redhat.com (forced update)
Test passed: syntax-check

loading

[libvirt] [PATCH v2] log: actually do substring matches with fnmatch
Posted by Daniel P. Berrangé, 6 days ago
Historically we matched log filters with strstr(), and when switching to
fnmatch in cbb0fd3cfdc287f6f4653ef1f04a7cfb2ea51b27, 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*".

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---

Changed in v2:

 - Simplify to always append/prepend '*'
 - Other fixes from Erik

 src/util/virlog.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/util/virlog.c b/src/util/virlog.c
index be9fc0cf78..9b63b8e7cd 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -1409,6 +1409,7 @@ virLogFilterNew(const char *match,
 {
     virLogFilterPtr ret = NULL;
     char *mdup = NULL;
+    size_t mlen = strlen(match);
 
     virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
 
@@ -1418,9 +1419,16 @@ virLogFilterNew(const char *match,
         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;
 
+    mdup[0] = '*';
+    memcpy(mdup + 1, match, mlen);
+    mdup[mlen + 1] = '*';
+
     if (VIR_ALLOC_QUIET(ret) < 0) {
         VIR_FREE(mdup);
         return NULL;
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] log: actually do substring matches with fnmatch
Posted by Erik Skultety, 6 days ago
On Wed, May 16, 2018 at 12:17:16PM +0100, Daniel P. Berrangé wrote:
> Historically we matched log filters with strstr(), and when switching to
> fnmatch in cbb0fd3cfdc287f6f4653ef1f04a7cfb2ea51b27, 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*".
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>
> Changed in v2:
>
>  - Simplify to always append/prepend '*'
>  - Other fixes from Erik
>
>  src/util/virlog.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/src/util/virlog.c b/src/util/virlog.c
> index be9fc0cf78..9b63b8e7cd 100644
> --- a/src/util/virlog.c
> +++ b/src/util/virlog.c
> @@ -1409,6 +1409,7 @@ virLogFilterNew(const char *match,
>  {
>      virLogFilterPtr ret = NULL;
>      char *mdup = NULL;
> +    size_t mlen = strlen(match);
>
>      virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
>
> @@ -1418,9 +1419,16 @@ virLogFilterNew(const char *match,
>          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 */

An extra comment block closing sequence at the ^end of the line, or one extra
below, I don't care as long as one is dropped to pass compilation...

> +     */
> +    if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)
>          return NULL;
>
> +    mdup[0] = '*';
> +    memcpy(mdup + 1, match, mlen);
> +    mdup[mlen + 1] = '*';
> +

Oh, nice simplification indeed.

Reviewed-by: Erik Skultety <eskultet@redhat.com>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list