]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
interface: Clean up virInterfaceObjListFindByMACString
authorJohn Ferlan <jferlan@redhat.com>
Sat, 15 Apr 2017 11:08:01 +0000 (07:08 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Fri, 2 Jun 2017 19:50:14 +0000 (15:50 -0400)
Alter the algorithm to return a list of matching names rather than a
list of match virInterfaceObjPtr which are then just dereferenced
extracting the def->name and def->mac. Since the def->mac would be
the same as the passed @mac, just return a list of names and as long
as there's only one, extract the [0] entry from the passed list.
Also alter the error message on failure to include the mac that wasn't
found.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/conf/virinterfaceobj.c
src/conf/virinterfaceobj.h
src/test/test_driver.c

index 9470b4a6a391e87ca7ebf3d8cbc9a39211b22c9e..8bd8094c58c5dd4467f50336eca788426938d6dd 100644 (file)
@@ -109,11 +109,11 @@ virInterfaceObjListNew(void)
 int
 virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces,
                                    const char *mac,
-                                   virInterfaceObjPtr *matches,
+                                   char **const matches,
                                    int maxmatches)
 {
     size_t i;
-    unsigned int matchct = 0;
+    int matchct = 0;
 
     for (i = 0; i < interfaces->count; i++) {
         virInterfaceObjPtr obj = interfaces->objs[i];
@@ -122,18 +122,23 @@ virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces,
         virInterfaceObjLock(obj);
         def = obj->def;
         if (STRCASEEQ(def->mac, mac)) {
-            matchct++;
-            if (matchct <= maxmatches) {
-                matches[matchct - 1] = obj;
-                /* keep the lock if we're returning object to caller */
-                /* it is the caller's responsibility to unlock *all* matches */
-                continue;
+            if (matchct < maxmatches) {
+                if (VIR_STRDUP(matches[matchct], def->name) < 0) {
+                    virInterfaceObjUnlock(obj);
+                    goto error;
+                }
+                matchct++;
             }
         }
         virInterfaceObjUnlock(obj);
-
     }
     return matchct;
+
+ error:
+    while (--matchct >= 0)
+        VIR_FREE(matches[matchct]);
+
+    return -1;
 }
 
 
index f1bcab931ff0c1411964658dfa97afdc314a8493..3934e6395e8cdee67ed7c7d5365a3b8277acf76e 100644 (file)
@@ -44,7 +44,7 @@ virInterfaceObjListNew(void);
 int
 virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces,
                                    const char *mac,
-                                   virInterfaceObjPtr *matches,
+                                   char **const matches,
                                    int maxmatches);
 
 virInterfaceObjPtr
index 8444e91d059342b77ae7e4f6b74b46e447044f62..511d65fbe453476a127de61a1ce00007586a3c15 100644 (file)
@@ -3728,17 +3728,18 @@ testInterfaceLookupByMACString(virConnectPtr conn,
                                const char *mac)
 {
     testDriverPtr privconn = conn->privateData;
-    virInterfaceObjPtr obj;
-    virInterfaceDefPtr def;
     int ifacect;
+    char *ifacenames[] = { NULL, NULL };
     virInterfacePtr ret = NULL;
 
     testDriverLock(privconn);
-    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac, &obj, 1);
+    ifacect = virInterfaceObjListFindByMACString(privconn->ifaces, mac,
+                                                 ifacenames, 2);
     testDriverUnlock(privconn);
 
     if (ifacect == 0) {
-        virReportError(VIR_ERR_NO_INTERFACE, NULL);
+        virReportError(VIR_ERR_NO_INTERFACE,
+                       _("no interface with matching mac '%s'"), mac);
         goto cleanup;
     }
 
@@ -3747,12 +3748,11 @@ testInterfaceLookupByMACString(virConnectPtr conn,
         goto cleanup;
     }
 
-    def = virInterfaceObjGetDef(obj);
-    ret = virGetInterface(conn, def->name, def->mac);
+    ret = virGetInterface(conn, ifacenames[0], mac);
 
  cleanup:
-    if (obj)
-        virInterfaceObjUnlock(obj);
+    VIR_FREE(ifacenames[0]);
+    VIR_FREE(ifacenames[1]);
     return ret;
 }