diff mbox

[ovs-dev,03/27] uuid: New function uuid_is_partial_match().

Message ID 20170430232231.15151-4-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff April 30, 2017, 11:22 p.m. UTC
This will have another caller in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/db-ctl-base.c | 10 +---------
 lib/uuid.c        | 11 +++++++++++
 lib/uuid.h        |  3 ++-
 3 files changed, 14 insertions(+), 10 deletions(-)

Comments

Russell Bryant May 2, 2017, 8:13 p.m. UTC | #1
On Sun, Apr 30, 2017 at 7:22 PM, Ben Pfaff <blp@ovn.org> wrote:
> This will have another caller in an upcoming commit.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>

Acked-by: Russell Bryant <russell@ovn.org>

with a minor comment inline ...

> diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
> index ab617f9e065d..18109691b1d6 100644
> --- a/lib/db-ctl-base.c
> +++ b/lib/db-ctl-base.c
> @@ -300,14 +300,6 @@ get_row_by_id(struct ctl_context *ctx,
>      return final;
>  }
>
> -static bool
> -is_partial_uuid_match(const struct uuid *uuid, const char *match)
> -{
> -    char uuid_s[UUID_LEN + 1];
> -    snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
> -    return !strncmp(uuid_s, match, strlen(match));
> -}
> -
>  static const struct ovsdb_idl_row *
>  get_row(struct ctl_context *ctx,
>          const struct ovsdb_idl_table_class *table, const char *record_id,
> @@ -343,7 +335,7 @@ get_row(struct ctl_context *ctx,
>                                                                   table);
>               r != NULL;
>               r = ovsdb_idl_next_row(r)) {
> -            if (is_partial_uuid_match(&r->uuid, record_id)) {
> +            if (uuid_is_partial_match(&r->uuid, record_id) >= 4) {
>                  if (!row) {
>                      row = r;
>                  } else {

I'm not sure if the ">= 4" is valuable since there is an outer
conditional that ensures "record_id" is at least a 4 character long
partial UUID.  It doesn't seem harmful, either though ...
Ben Pfaff May 3, 2017, 3:19 p.m. UTC | #2
On Tue, May 02, 2017 at 04:13:58PM -0400, Russell Bryant wrote:
> On Sun, Apr 30, 2017 at 7:22 PM, Ben Pfaff <blp@ovn.org> wrote:
> > This will have another caller in an upcoming commit.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> 
> Acked-by: Russell Bryant <russell@ovn.org>

Thanks!

> > diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
> > index ab617f9e065d..18109691b1d6 100644
> > --- a/lib/db-ctl-base.c
> > +++ b/lib/db-ctl-base.c
> > @@ -300,14 +300,6 @@ get_row_by_id(struct ctl_context *ctx,
> >      return final;
> >  }
> >
> > -static bool
> > -is_partial_uuid_match(const struct uuid *uuid, const char *match)
> > -{
> > -    char uuid_s[UUID_LEN + 1];
> > -    snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
> > -    return !strncmp(uuid_s, match, strlen(match));
> > -}
> > -
> >  static const struct ovsdb_idl_row *
> >  get_row(struct ctl_context *ctx,
> >          const struct ovsdb_idl_table_class *table, const char *record_id,
> > @@ -343,7 +335,7 @@ get_row(struct ctl_context *ctx,
> >                                                                   table);
> >               r != NULL;
> >               r = ovsdb_idl_next_row(r)) {
> > -            if (is_partial_uuid_match(&r->uuid, record_id)) {
> > +            if (uuid_is_partial_match(&r->uuid, record_id) >= 4) {
> >                  if (!row) {
> >                      row = r;
> >                  } else {
> 
> I'm not sure if the ">= 4" is valuable since there is an outer
> conditional that ensures "record_id" is at least a 4 character long
> partial UUID.  It doesn't seem harmful, either though ...

That's a good point.  I dropped the ">= 4".
diff mbox

Patch

diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
index ab617f9e065d..18109691b1d6 100644
--- a/lib/db-ctl-base.c
+++ b/lib/db-ctl-base.c
@@ -300,14 +300,6 @@  get_row_by_id(struct ctl_context *ctx,
     return final;
 }
 
-static bool
-is_partial_uuid_match(const struct uuid *uuid, const char *match)
-{
-    char uuid_s[UUID_LEN + 1];
-    snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
-    return !strncmp(uuid_s, match, strlen(match));
-}
-
 static const struct ovsdb_idl_row *
 get_row(struct ctl_context *ctx,
         const struct ovsdb_idl_table_class *table, const char *record_id,
@@ -343,7 +335,7 @@  get_row(struct ctl_context *ctx,
                                                                  table);
              r != NULL;
              r = ovsdb_idl_next_row(r)) {
-            if (is_partial_uuid_match(&r->uuid, record_id)) {
+            if (uuid_is_partial_match(&r->uuid, record_id) >= 4) {
                 if (!row) {
                     row = r;
                 } else {
diff --git a/lib/uuid.c b/lib/uuid.c
index 636492bcbe5b..0b20c24faecd 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -242,6 +242,17 @@  uuid_is_partial_string(const char *s)
     return i;
 }
 
+/* Compares 'match' to the string representation of 'uuid'.  If 'match' equals
+ * or is a prefix of this string representation, returns strlen(match);
+ * otherwise, returns 0.  (Thus, the caller can  */
+int
+uuid_is_partial_match(const struct uuid *uuid, const char *match)
+{
+    char uuid_s[UUID_LEN + 1];
+    snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
+    size_t match_len = strlen(match);
+    return !strncmp(uuid_s, match, match_len) ? match_len : 0;
+}
 
 static void
 sha1_update_int(struct sha1_ctx *sha1_ctx, uintmax_t x)
diff --git a/lib/uuid.h b/lib/uuid.h
index 605ec17b7455..10bc8b541bac 100644
--- a/lib/uuid.h
+++ b/lib/uuid.h
@@ -1,4 +1,4 @@ 
-/* Copyright (c) 2008, 2009, 2010, 2016 Nicira, Inc.
+/* Copyright (c) 2008, 2009, 2010, 2016, 2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@  int uuid_compare_3way(const struct uuid *, const struct uuid *);
 bool uuid_from_string(struct uuid *, const char *);
 bool uuid_from_string_prefix(struct uuid *, const char *);
 int uuid_is_partial_string(const char *);
+int uuid_is_partial_match(const struct uuid *, const char *match);
 void uuid_set_bits_v4(struct uuid *);
 
 #endif /* uuid.h */