From: Alan T. DeKok Date: Sat, 13 Nov 2021 17:53:53 +0000 (-0500) Subject: add replace_value() and tweak API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=192197be507cadf5e10c4a2e96beb60bf2eea43c;p=thirdparty%2Ffreeradius-server.git add replace_value() and tweak API --- diff --git a/src/lib/util/edit.c b/src/lib/util/edit.c index 4dbda60ff2..fbc8eb7954 100644 --- a/src/lib/util/edit.c +++ b/src/lib/util/edit.c @@ -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) */ diff --git a/src/lib/util/edit.h b/src/lib/util/edit.h index 7246fb45e9..e9fc463a8b 100644 --- a/src/lib/util/edit.h +++ b/src/lib/util/edit.h @@ -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)); diff --git a/src/lib/util/edit_tests.c b/src/lib/util/edit_tests.c index f16d827b0c..c0b78029ce 100644 --- a/src/lib/util/edit_tests.c +++ b/src/lib/util/edit_tests.c @@ -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);