int dns_add_search_domain(const char* domain, int index, char** msg) { int ret; char* path; assert(domain); assert(index >= 1); if (domain == NULL || index < 1) { asprintf(msg, "NULL arguments."); return EXIT_FAILURE; } switch (ret = aug_match(sysaugeas, "/files/"AUGEAS_DNS_CONF"/search/domain", NULL)) { case -1: asprintf(msg, "Augeas match for \"%s\" failed: %s", "/files/"AUGEAS_DNS_CONF"/search/domain", aug_error_message(sysaugeas)); return EXIT_FAILURE; case 0: /* First domain to be added */ if (index != 1) { asprintf(msg, "Configuration data (dns-resolver search domains) are inconsistent with system configuration file (code 1)."); return EXIT_FAILURE; } break; default: /* Some domains already in the config file */ if ((index - ret) > 1) { asprintf(msg, "Configuration data (dns-resolver search domains) are inconsistent with system configuration file (code 2)."); return EXIT_FAILURE; } /* insert new (empty) node */ if (index == 1) { if (aug_insert(sysaugeas, "/files/"AUGEAS_DNS_CONF"/search/domain[1]", "domain", 1) == -1) { asprintf(msg, "Inserting DNS search domain configuration before \"%s\" failed (%s)", "/files/"AUGEAS_DNS_CONF"/search/domain[1]", aug_error_message(sysaugeas)); return (EXIT_FAILURE); } } else { asprintf(&path, "/files/%s/search/domain[%d]", AUGEAS_DNS_CONF, index - 1); if (aug_insert(sysaugeas, path, "domain", 0) == -1) { asprintf(msg, "Inserting DNS search domain configuration after \"%s\" failed (%s)", path, aug_error_message(sysaugeas)); free(path); return (EXIT_FAILURE); } free(path); path = NULL; } } /* Set the value of the newly inserted node (or possibly create it, too) */ asprintf(&path, "/files/%s/search/domain[%d]", AUGEAS_DNS_CONF, index); if (aug_set(sysaugeas, path, domain) == -1) { aug_rm(sysaugeas, path); /* previously inserted, do rollback */ asprintf(msg, "Unable to set DNS search domain \"%s\" (%s).", domain, aug_error_message(sysaugeas)); free(path); return EXIT_FAILURE; } free(path); return EXIT_SUCCESS; }
/* * call-seq: * insert(PATH, LABEL, BEFORE) -> int * * Make LABEL a sibling of PATH by inserting it directly before or after PATH. * The boolean BEFORE determines if LABEL is inserted before or after PATH. */ VALUE augeas_insert(VALUE s, VALUE path, VALUE label, VALUE before) { augeas *aug = aug_handle(s); const char *cpath = StringValueCStr(path) ; const char *clabel = StringValueCStr(label) ; int callValue = aug_insert(aug, cpath, clabel, RTEST(before)); return INT2FIX(callValue) ; }
static int Paug_insert(lua_State *L) { augeas *a = Paug_checkarg(L, 1); const char *path = luaL_checkstring(L, 2); const char *label = luaL_checkstring(L, 3); int before = lua_toboolean(L, 4); return pushresult(L, aug_insert(a, path, label, before), a, path); }
int do_aug_insert (const char *path, const char *label, int before) { #ifdef HAVE_AUGEAS int r; NEED_AUG (-1); r = aug_insert (aug, path, label, before); if (r == -1) { reply_with_error ("Augeas insert failed"); return -1; } return 0; #else NOT_AVAILABLE (-1); #endif }