]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add replace_value() and tweak API
authorAlan T. DeKok <aland@freeradius.org>
Sat, 13 Nov 2021 17:53:53 +0000 (12:53 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 13 Nov 2021 17:53:53 +0000 (12:53 -0500)
src/lib/util/edit.c
src/lib/util/edit.h
src/lib/util/edit_tests.c

index 4dbda60ff2c6a80d2bd2d08869651e4d20a941f6..fbc8eb795452e4c0fab9346d9615d5c2ee1d7cae 100644 (file)
@@ -43,7 +43,7 @@ typedef enum {
 static const char *edit_names[4] = {
        "invalid",
        "delete",
-       "record_value",
+       "value",
        "insert",
 };
 #endif
@@ -368,7 +368,7 @@ int fr_edit_list_delete(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *vp)
  *
  *  After this function returns, it's safe to edit the pair.
  */
-int fr_edit_list_record_value(fr_edit_list_t *el, fr_pair_t *vp)
+int fr_edit_list_save_value(fr_edit_list_t *el, fr_pair_t *vp)
 {
        if (!el) return 0;
 
@@ -377,6 +377,23 @@ int fr_edit_list_record_value(fr_edit_list_t *el, fr_pair_t *vp)
        return edit_record(el, FR_EDIT_VALUE, vp, NULL, NULL);
 }
 
+/** Write a new value to the #fr_value_box_t
+ *
+ *  After this function returns, the value has been updated.
+ */
+int fr_edit_list_replace_value(fr_edit_list_t *el, fr_pair_t *vp, fr_value_box_t *box)
+{
+       if (!el) return 0;
+
+       if (!fr_type_is_leaf(vp->vp_type)) return -1;
+
+       if (edit_record(el, FR_EDIT_VALUE, vp, NULL, NULL) < 0) return -1;
+
+       if (!fr_type_is_fixed_size(vp->vp_type)) fr_value_box_clear(&vp->data);
+       fr_value_box_copy_shallow(NULL, &vp->data, box);
+       return 0;
+}
+
 /** Replace a pair with another one.
  *
  *  This function mirrors fr_pair_replace().
@@ -498,5 +515,5 @@ fr_edit_list_t *fr_edit_list_alloc(TALLOC_CTX *ctx, int hint)
  *             edit VPs, recording edits
  *
  *     free temporary map
- *     talloc_free(edit list)
+ *     commit(edit list)
  */
index 7246fb45e93b1e431680813906ad3a5c8a6d5876..e9fc463a8bd6b0d54f2eef26c17e7816474ffcbd 100644 (file)
@@ -43,7 +43,9 @@ int fr_edit_list_insert_after(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_
 
 int fr_edit_list_delete(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *vp) CC_HINT(nonnull(2,3));
 
-int fr_edit_list_record_value(fr_edit_list_t *el, fr_pair_t *vp) CC_HINT(nonnull(2));
+int fr_edit_list_save_value(fr_edit_list_t *el, fr_pair_t *vp) CC_HINT(nonnull(2));
+
+int fr_edit_list_replace_value(fr_edit_list_t *el, fr_pair_t *vp, fr_value_box_t *box) CC_HINT(nonnull(2,3));
 
 int fr_edit_list_replace(fr_edit_list_t *el, fr_pair_list_t *list, fr_pair_t *to_replace, fr_pair_t *vp) CC_HINT(nonnull(2,3,4));
 
index f16d827b0cb7463bf7ad2893c64c3a12aab8fe64..c0b78029cee7187296fefccf95fbcff9cdf5b136 100644 (file)
@@ -366,7 +366,7 @@ static void test_pair_edit_value(void)
        el = fr_edit_list_alloc(NULL, 5);
        fr_assert(el != NULL);
 
-       rcode = fr_edit_list_record_value(el, vp);
+       rcode = fr_edit_list_save_value(el, vp);
        TEST_CHECK(rcode == 0);
 
        TEST_CHECK(vp->vp_uint32 == 0);
@@ -400,7 +400,7 @@ static void test_pair_edit_value_abort(void)
        el = fr_edit_list_alloc(NULL, 5);
        fr_assert(el != NULL);
 
-       rcode = fr_edit_list_record_value(el, vp);
+       rcode = fr_edit_list_save_value(el, vp);
        TEST_CHECK(rcode == 0);
 
        TEST_CHECK(vp->vp_uint32 == 0);