mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 06:25:31 +00:00
exists policy
This commit is contained in:
@@ -15,6 +15,8 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -41,7 +43,7 @@ mem_strdup(isc_mem_t *mctx, const char *s) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
undefine_action(char *key, unsigned int type, isc_symvalue_t value) {
|
undefine_action(char *key, unsigned int type, isc_symvalue_t value) {
|
||||||
INSIST(type == 0);
|
INSIST(type == 1);
|
||||||
isc_mem_free(mctx, key);
|
isc_mem_free(mctx, key);
|
||||||
isc_mem_free(mctx, value.as_pointer);
|
isc_mem_free(mctx, value.as_pointer);
|
||||||
}
|
}
|
||||||
@@ -54,16 +56,23 @@ main(int argc, char *argv[]) {
|
|||||||
isc_symvalue_t value;
|
isc_symvalue_t value;
|
||||||
int trace = 0;
|
int trace = 0;
|
||||||
int c;
|
int c;
|
||||||
|
isc_symexists_t exists_policy = isc_symexists_reject;
|
||||||
|
|
||||||
INSIST(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
INSIST(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
||||||
INSIST(isc_symtab_create(mctx, 691, undefine_action, &st) ==
|
INSIST(isc_symtab_create(mctx, 691, undefine_action, &st) ==
|
||||||
ISC_R_SUCCESS);
|
ISC_R_SUCCESS);
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "t")) != -1) {
|
while ((c = getopt(argc, argv, "tar")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 't':
|
case 't':
|
||||||
trace = 1;
|
trace = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'a':
|
||||||
|
exists_policy = isc_symexists_add;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
exists_policy = isc_symexists_replace;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +83,7 @@ main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if (cp[0] == '!') {
|
if (cp[0] == '!') {
|
||||||
cp++;
|
cp++;
|
||||||
result = isc_symtab_undefine(st, cp, 0);
|
result = isc_symtab_undefine(st, cp, 1);
|
||||||
if (trace || result != ISC_R_SUCCESS)
|
if (trace || result != ISC_R_SUCCESS)
|
||||||
printf("undefine('%s'): %s\n", cp,
|
printf("undefine('%s'): %s\n", cp,
|
||||||
isc_result_totext(result));
|
isc_result_totext(result));
|
||||||
@@ -97,7 +106,8 @@ main(int argc, char *argv[]) {
|
|||||||
*cp++ = '\0';
|
*cp++ = '\0';
|
||||||
key = mem_strdup(mctx, key);
|
key = mem_strdup(mctx, key);
|
||||||
value.as_pointer = mem_strdup(mctx, cp);
|
value.as_pointer = mem_strdup(mctx, cp);
|
||||||
result = isc_symtab_define(st, key, 0, value);
|
result = isc_symtab_define(st, key, 1, value,
|
||||||
|
exists_policy);
|
||||||
if (trace || result != ISC_R_SUCCESS)
|
if (trace || result != ISC_R_SUCCESS)
|
||||||
printf("define('%s', '%s'): %s\n",
|
printf("define('%s', '%s'): %s\n",
|
||||||
key, cp,
|
key, cp,
|
||||||
|
@@ -168,7 +168,7 @@ isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
|
|||||||
|
|
||||||
isc_result_t
|
isc_result_t
|
||||||
isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
|
isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
|
||||||
isc_symvalue_t value)
|
isc_symvalue_t value, isc_symexists_t exists_policy)
|
||||||
{
|
{
|
||||||
unsigned int bucket;
|
unsigned int bucket;
|
||||||
elt_t *elt;
|
elt_t *elt;
|
||||||
@@ -178,17 +178,29 @@ isc_symtab_define(isc_symtab_t *symtab, char *key, unsigned int type,
|
|||||||
|
|
||||||
FIND(symtab, key, type, bucket, elt);
|
FIND(symtab, key, type, bucket, elt);
|
||||||
|
|
||||||
if (elt != NULL)
|
if (exists_policy != isc_symexists_add && elt != NULL) {
|
||||||
return (ISC_R_EXISTS);
|
if (exists_policy == isc_symexists_reject)
|
||||||
|
return (ISC_R_EXISTS);
|
||||||
elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof *elt);
|
INSIST(exists_policy == isc_symexists_replace);
|
||||||
if (elt == NULL)
|
UNLINK(symtab->table[bucket], elt, link);
|
||||||
return (ISC_R_NOMEMORY);
|
if (symtab->undefine_action != NULL)
|
||||||
|
(symtab->undefine_action)(elt->key, elt->type,
|
||||||
|
elt->value);
|
||||||
|
} else {
|
||||||
|
elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof *elt);
|
||||||
|
if (elt == NULL)
|
||||||
|
return (ISC_R_NOMEMORY);
|
||||||
|
}
|
||||||
elt->key = key;
|
elt->key = key;
|
||||||
elt->type = type;
|
elt->type = type;
|
||||||
elt->value = value;
|
elt->value = value;
|
||||||
|
|
||||||
APPEND(symtab->table[bucket], elt, link);
|
/*
|
||||||
|
* We prepend so that a 'type 0' lookup will return the most
|
||||||
|
* recent definition, and a 'type 0' undefine will undefine the
|
||||||
|
* most recent definition.
|
||||||
|
*/
|
||||||
|
PREPEND(symtab->table[bucket], elt, link);
|
||||||
|
|
||||||
return (ISC_R_SUCCESS);
|
return (ISC_R_SUCCESS);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user