[Libvirt-cim] [PATCH] [CU][RFC] Correct a few API issues

Dan Smith danms at us.ibm.com
Mon Nov 26 23:54:38 UTC 2007


# HG changeset patch
# User Dan Smith <danms at us.ibm.com>
# Date 1196124861 28800
# Node ID 96b651213e50f02586988064a014bbe746dc9c59
# Parent  05f120725e3dc562dc8fa251da698faa474f0b55
[CU][RFC] Correct a few API issues
that I think need to be resolved before we snap a release.

Some of the cu_get_* functions returned values instead of a CMPIrc's
like everything else, so this changes that.  Also, return const char *'s
from the broker memory so that the callers don't need to free the result.

This will require some work in libvirt-cim to make sure the new API is
adhered to properly.  If this is okay with people, I'll follow up with a
patch to libvirt-cim to make those changes.  Any other discussion about
things that should change in the libcmpiutil API is welcomed.

Signed-off-by: Dan Smith <danms at us.ibm.com>

diff -r 05f120725e3d -r 96b651213e50 args_util.c
--- a/args_util.c	Mon Nov 26 16:51:12 2007 -0800
+++ b/args_util.c	Mon Nov 26 16:54:21 2007 -0800
@@ -32,28 +32,32 @@
 
 #define CU_WEAK_TYPES 1
 
-char *cu_get_str_path(const CMPIObjectPath *reference, const char *key)
+CMPIrc cu_get_str_path(const CMPIObjectPath *ref,
+                       const char *key,
+                       const char **val)
 {
         CMPIData data;
         CMPIStatus s;
         const char *value;
         
-        data = CMGetKey(reference, key, &s);
+        data = CMGetKey(ref, key, &s);
         if ((s.rc != CMPI_RC_OK) || 
             CMIsNullValue(data) || 
             CMIsNullObject(data.value.string))
-                return NULL;
+                return CMPI_RC_ERR_FAILED;
 
         value = CMGetCharPtr(data.value.string);
         if ((value == NULL) || (*value == '\0'))
-                return NULL;
-
-        return strdup(value);
-}
-
-int cu_get_u16_path(const CMPIObjectPath *reference,
-                    const char *key,
-                    uint16_t *target)
+                return CMPI_RC_ERR_TYPE_MISMATCH;
+
+        *val = value;
+
+        return CMPI_RC_OK;
+}
+
+CMPIrc cu_get_u16_path(const CMPIObjectPath *reference,
+                       const char *key,
+                       uint16_t *target)
 {
         CMPIData data;
         CMPIStatus s;
@@ -61,11 +65,11 @@ int cu_get_u16_path(const CMPIObjectPath
         data = CMGetKey(reference, key, &s);
         if ((s.rc != CMPI_RC_OK) ||
             CMIsNullValue(data))
-                return 0;
+                return CMPI_RC_ERR_FAILED;
 
         *target = data.value.uint16;
 
-        return 1;
+        return CMPI_RC_OK;
 }
 
 const char *cu_check_args(const CMPIArgs *args, const char **names)
@@ -85,92 +89,101 @@ const char *cu_check_args(const CMPIArgs
         return NULL;
 }
 
-char *cu_get_str_arg(const CMPIArgs *args, const char *name)
-{
-        CMPIData argdata;
-        char *argval;
+CMPIrc cu_get_str_arg(const CMPIArgs *args, const char *name, const char **val)
+{
+        CMPIData argdata;
         CMPIStatus s;
 
         argdata = CMGetArg(args, name, &s);
         if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata)))
-                return NULL;
+                return CMPI_RC_ERR_INVALID_PARAMETER;
 
         if ((argdata.type != CMPI_string) || 
             CMIsNullObject(argdata.value.string))
-                return NULL;
-
-        argval = strdup(CMGetCharPtr(argdata.value.string));
-
-        return argval;
-}
-
-CMPIObjectPath *cu_get_ref_arg(const CMPIArgs *args, const char *name)
+                return CMPI_RC_ERR_TYPE_MISMATCH;
+
+        *val = CMGetCharPtr(argdata.value.string);
+
+        return CMPI_RC_OK;
+}
+
+CMPIrc cu_get_ref_arg(const CMPIArgs *args,
+                      const char *name,
+                      CMPIObjectPath **op)
 {
         CMPIData argdata;
         CMPIStatus s;
 
         argdata = CMGetArg(args, name, &s);
         if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata)))
-                return NULL;
+                return CMPI_RC_ERR_INVALID_PARAMETER;
 
         if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref))
-                return NULL;
-
-        return argdata.value.ref;
-}
-
-CMPIInstance *cu_get_inst_arg(const CMPIArgs *args, const char *name)
-{
-        CMPIData argdata;
-        CMPIStatus s;
-
-        argdata = CMGetArg(args, name, &s);
-        if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata))) {
-                return NULL;
-        }
+                return CMPI_RC_ERR_TYPE_MISMATCH;
+
+        *op = argdata.value.ref;
+
+        return CMPI_RC_OK;
+}
+
+CMPIrc cu_get_inst_arg(const CMPIArgs *args,
+                       const char *name,
+                       CMPIInstance **inst)
+{
+        CMPIData argdata;
+        CMPIStatus s;
+
+        argdata = CMGetArg(args, name, &s);
+        if ((s.rc != CMPI_RC_OK) || (CMIsNullValue(argdata)))
+                return CMPI_RC_ERR_INVALID_PARAMETER;
 
         if ((argdata.type != CMPI_instance) ||
-            CMIsNullObject(argdata.value.inst)) {
-                return NULL;
-        }
-
-        return argdata.value.inst;
-}
-
-CMPIArray *cu_get_array_arg(const CMPIArgs *args, const char *name)
+            CMIsNullObject(argdata.value.inst))
+                CMPI_RC_ERR_TYPE_MISMATCH;
+
+        *inst = argdata.value.inst;
+
+        return CMPI_RC_OK;
+}
+
+CMPIrc cu_get_array_arg(const CMPIArgs *args,
+                        const char *name,
+                        CMPIArray **array)
 {
         CMPIData argdata;
         CMPIStatus s;
         
         argdata = CMGetArg(args, name, &s);
         if ((s.rc != CMPI_RC_OK) || CMIsNullValue(argdata))
-                return NULL;
+                return CMPI_RC_ERR_INVALID_PARAMETER;
 
         if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array))
-                return NULL;
-
-        return argdata.value.array;
-}
-
-int cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target)
+                return CMPI_RC_ERR_TYPE_MISMATCH;
+
+        *array = argdata.value.array;
+
+        return CMPI_RC_OK;
+}
+
+CMPIrc cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target)
 {
         CMPIData argdata;
         CMPIStatus s;
 
         argdata = CMGetArg(args, name, &s);
         if ((s.rc != CMPI_RC_OK) || CMIsNullValue(argdata))
-                return 0;
+                return CMPI_RC_ERR_INVALID_PARAMETER;
 
 #ifdef CU_WEAK_TYPES
         if (!(argdata.type & CMPI_INTEGER))
 #else
         if (argdata.type != CMPI_uint16)
 #endif
-                return 0;
+                return CMPI_RC_ERR_TYPE_MISMATCH;
 
         *target = argdata.value.uint16;
 
-        return 1;
+        return CMPI_RC_OK;
 }
 
 #define REQUIRE_PROPERTY_DEFINED(i, p, pv, s)                           \
diff -r 05f120725e3d -r 96b651213e50 libcmpiutil.h
--- a/libcmpiutil.h	Mon Nov 26 16:51:12 2007 -0800
+++ b/libcmpiutil.h	Mon Nov 26 16:54:21 2007 -0800
@@ -91,18 +91,22 @@ const char *cu_check_args(const CMPIArgs
  *
  * @param args The argument list to search
  * @param name The name of the argument
- * @returns The string argument's value, or NULL if error (must be free()'d)
- */
-char *cu_get_str_arg(const CMPIArgs *args, const char *name);
+ * @param val The value of the argument (must be free()'d)
+ * @returns CMPI_RC_OK if successful
+ */
+CMPIrc cu_get_str_arg(const CMPIArgs *args, const char *name, const char **val);
 
 /**
  * Get a reference argument
  *
  * @param args The argument list to search
  * @param name The name of the argument
- * @returns The reference argument's value, or NULL if error
- */
-CMPIObjectPath *cu_get_ref_arg(const CMPIArgs *args, const char *name);
+ * @param op The reference argument's value
+ * @returns CMPI_RC_OK if successful
+ */
+CMPIrc cu_get_ref_arg(const CMPIArgs *args,
+                      const char *name,
+                      CMPIObjectPath **op);
 
 /**
  * Get an instance argument
@@ -111,7 +115,9 @@ CMPIObjectPath *cu_get_ref_arg(const CMP
  * @param name The name of the argument
  * @returns The instance argument's value, or NULL if error
  */
-CMPIInstance *cu_get_inst_arg(const CMPIArgs *args, const char *name);
+CMPIrc cu_get_inst_arg(const CMPIArgs *args,
+                       const char *name,
+                       CMPIInstance **inst);
 
 /**
  * Get an array argument
@@ -120,7 +126,9 @@ CMPIInstance *cu_get_inst_arg(const CMPI
  * @param name The name of the argument
  * @returns The array argument's value, or NULL if error
  */
-CMPIArray *cu_get_array_arg(const CMPIArgs *args, const char *name);
+CMPIrc cu_get_array_arg(const CMPIArgs *args,
+                        const char *name,
+                        CMPIArray **array);
 
 /**
  * Get a uint16 argument
@@ -128,18 +136,22 @@ CMPIArray *cu_get_array_arg(const CMPIAr
  * @param args The argument list to search
  * @param name The name of the argument
  * @param target The uint16_t to reflect the argument value
- * @returns nonzero on success, zero otherwise
- */
-int cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target);
+ * @returns CMPI_RC_OK if successful
+ */
+CMPIrc cu_get_u16_arg(const CMPIArgs *args, const char *name, uint16_t *target);
 
 /**
  * Get a string component of an object path
  *
- * @param reference The object path
+ * @param ref The object path
  * @param key The name of the component to return
- * @returns The value of the component, or NULL if error (must be free()'d)
- */
-char *cu_get_str_path(const CMPIObjectPath *reference, const char *key);
+ * @param val The value of the component, or NULL if error (must be
+ *            free()'d)
+ * @returns CMPI_RC_OK on success
+ */
+CMPIrc cu_get_str_path(const CMPIObjectPath *ref,
+                       const char *key,
+                       const char **val);
 
 /**
  * Get a uint16 component of an object path
@@ -147,11 +159,11 @@ char *cu_get_str_path(const CMPIObjectPa
  * @param reference The object path
  * @param key The name of the component to return
  * @param target A pointer to the uint16 to set
- * @returns nonzero on success, zero otherwise
- */
-int cu_get_u16_path(const CMPIObjectPath *reference,
-                    const char *key,
-                    uint16_t *target);
+ * @returns CMPI_RC_OK if successful
+ */
+CMPIrc cu_get_u16_path(const CMPIObjectPath *reference,
+                       const char *key,
+                       uint16_t *target);
 
 /* Forward declaration */
 struct inst_list;
diff -r 05f120725e3d -r 96b651213e50 std_indication.c
--- a/std_indication.c	Mon Nov 26 16:51:12 2007 -0800
+++ b/std_indication.c	Mon Nov 26 16:54:21 2007 -0800
@@ -49,7 +49,8 @@ static CMPIStatus raise(struct std_indic
         if (ctx->handler->raise_fn == NULL)
                 return (CMPIStatus){CMPI_RC_OK, NULL};
 
-        inst = cu_get_inst_arg(argsin, "Indication");
+        if (cu_get_inst_arg(argsin, "Indication", &inst) != CMPI_RC_OK)
+                return (CMPIStatus){CMPI_RC_ERR_FAILED, NULL};
 
         return ctx->handler->raise_fn(context, inst);
 }




More information about the Libvirt-cim mailing list