]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
audit_logging: add method to replace the object for a given key with a new object
authorJule Anger <janger@samba.org>
Tue, 22 Mar 2022 15:06:37 +0000 (16:06 +0100)
committerJule Anger <janger@samba.org>
Mon, 8 Aug 2022 12:56:28 +0000 (12:56 +0000)
Signed-off-by: Jule Anger <janger@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/audit_logging/audit_logging.c
lib/audit_logging/audit_logging.h

index 87378e1bb95367858ab356056fea5d1797a95ce4..43acf9512c98424e580e16cfedf83127a4c16e90 100644 (file)
@@ -905,6 +905,52 @@ int json_add_guid(struct json_object *object,
        return ret;
 }
 
+/*
+ * @brief Replaces the object for a given key with a given json object.
+ *
+ * If key already exists, the value will be replaced. Otherwise the given
+ * value will be added under the given key.
+ *
+ * @param object the JSON object to be updated.
+ * @param key the key which will be updated.
+ * @param new_obj the new value object to be inserted.
+ *
+ * @return 0 the operation was successful
+ *        -1 the operation failed (e.j. if one of the paramters is invalid)
+ */
+int json_update_object(struct json_object *object,
+                      const char *key,
+                      struct json_object *new_obj)
+{
+       int ret = 0;
+
+       if (json_is_invalid(object)) {
+               DBG_ERR("Unable to update key [%s], "
+                       "target object is invalid\n",
+                       key);
+               return JSON_ERROR;
+       }
+       if (json_is_invalid(new_obj)) {
+               DBG_ERR("Unable to update key [%s], "
+                       "new object is invalid\n",
+                       key);
+               return JSON_ERROR;
+       }
+
+       if (key == NULL) {
+               DBG_ERR("Unable to add null String as key\n");
+               return JSON_ERROR;
+       }
+
+       ret = json_object_set(object->root, key, new_obj->root);
+       if (ret != 0) {
+               DBG_ERR("Unable to update object\n");
+               return ret;
+       }
+
+       return ret;
+}
+
 /*
  * @brief Convert a JSON object into a string
  *
index 86e9134a86a0df79720cd0f85e7a37bc2e0a23e6..49576ece68df2090401e3d94c70e3a58e51a91ab 100644 (file)
@@ -87,6 +87,10 @@ _WARN_UNUSED_RESULT_ int json_add_guid(struct json_object *object,
                                       const char *name,
                                       const struct GUID *guid);
 
+_WARN_UNUSED_RESULT_ int json_update_object(struct json_object *object,
+                                           const char *key,
+                                           struct json_object *new_obj);
+
 _WARN_UNUSED_RESULT_ struct json_object json_get_array(
     struct json_object *object, const char *name);
 _WARN_UNUSED_RESULT_ struct json_object json_get_object(