static int do_regex_checks(struct _info *info, char *rr_type, char *key, char *rr_rdata) { char *pattern; unsigned keylen = strlen(key), patlen, vpos; int nchecks = 0; if (cdb_findinit(&info->cdbf, &info->cdb, key, keylen) <= 0) { return (-1); } while (cdb_findnext(&info->cdbf) > 0) { int m; vpos = cdb_datapos(&info->cdb); patlen = cdb_datalen(&info->cdb); pattern = malloc(patlen + 1); cdb_read(&info->cdb, pattern, patlen, vpos); pattern[patlen] = '\0'; if (*pattern == '@') { /* * Handle special function for data check */ if (!strcmp(pattern, "@IP")) { m = func_ip(info, rr_type, rr_rdata); if (m) { ++nchecks; } else { return (0); } } else if (!strcmp(pattern, "@SOA")) { m = func_soa(info, rr_type, rr_rdata); if (m) { ++nchecks; } else { return (0); } } } else { m = match(pattern, rr_rdata); // fprintf(stderr, "*** match %s ", m ? "TRUE " : "false"); // fprintf(stderr, "%s\n", pattern); if (m) { ++nchecks; // FIXME: can return now } else { sprintf(info->reason, "%s [%s] fails regexp [%s]", rr_type, rr_rdata, pattern); } } free(pattern); } return (nchecks); }
static int __cdb_findnext(Dictionary *dic, void *key, unsigned int keylen) { #if defined(USE_TINYCDB) if (dic->first) { cdb_findinit(&dic->cdb_find, &dic->cdb, key, keylen); dic->first = 0; } return cdb_findnext(&dic->cdb_find); #else return cdb_findnext(&dic->cdb, key, keylen); #endif }
VALUE mCDB_Reader_each_for_key(VALUE self,VALUE key) { struct cdb db; VALUE value; struct cdb_find find; StringValue(key); int fd = open_cdb_fd(self); size_t vlen; cdb_init(&db,fd); cdb_findinit(&find,&db,RSTRING_PTR(key),RSTRING_LEN(key)); while(cdb_findnext(&find) > 0) { vlen = cdb_datalen(&db); value = rb_str_buf_new(vlen); cdb_read(&db,RSTRING_PTR(value),vlen,cdb_datapos(&db)); rb_str_set_len(value,vlen); rb_yield(value); } close(fd); return Qnil; }
int checks(char *key, char *buf) { struct cdb cdb; struct cdb_find cdbf; int fd; char *data; unsigned keylen = strlen(key), datalen, vpos; if (((fd = open(DB, O_RDONLY)) == -1) || (cdb_init(&cdb, fd) != 0)) { printf("Can't open database: %s\n", strerror(errno)); return (-1); } if (cdb_findinit(&cdbf, &cdb, key, keylen) <= 0) { return (-1); } while (cdb_findnext(&cdbf) > 0) { char *pattern, *subject; int m; vpos = cdb_datapos(&cdb); datalen = cdb_datalen(&cdb); data = malloc(datalen + 1); cdb_read(&cdb, data, datalen, vpos); data[datalen] = '\0'; pattern = data; subject = buf; m = match(pattern, subject); printf("*** match %s ", m ? "TRUE " : "false"); printf("%s\n", data); free(data); } cdb_free(&cdb); close(fd); return (0); }
/// Traverses the roadmap file, attempting to find build-avoidance /// opportunities for the current command. Any files eligible for /// recycling are compared to the current state and downloaded /// as required. /// @param[in] ca the CA being shopped for /// @param[in] cmdkey the roadmap index to the command /// @param[in] getfiles boolean - really get new files? /// @return an enum indicating what happened shop_e shop(ca_o ca, CCS cmdkey, int getfiles) { shop_e rc; shopping_state_s shop_state, *ssp = &shop_state; struct cdb_find cdbf; CCS line; char cmdix[64]; if (!ShopCDB) { return SHOP_OFF; } // Expect the worst, hope for the best ... rc = SHOP_NOMATCH; memset(ssp, 0, sizeof(*ssp)); ssp->getfiles = getfiles; ssp->cdbp = ShopCDB; ssp->ca = ca; ssp->ptx_dict = _shop_ptx_init(); ssp->ignore_path_re = re_init_prop__(P_SHOP_IGNORE_PATH_RE); // First, grab the set of PTXes and store them away. if (cdb_findinit(&cdbf, ssp->cdbp, PTX_PREFIX, strlen(PTX_PREFIX)) >= 0) { while (cdb_findnext(&cdbf) > 0) { unsigned len; char xn[64]; CS id; len = cdb_datalen(ssp->cdbp); cdb_read(ssp->cdbp, xn, len, cdb_datapos(ssp->cdbp)); xn[len] = '\0'; if ((id = strchr(xn, '='))) { *id++ = '\0'; _shop_ptx_insert(ssp, xn, id); } else { putil_int("bad PTX line in roadmap: %s", xn); } } } else { putil_die("cdb_findinit (%s)", PTX_PREFIX); } // This is basically a debugging mode where the cmd key is // provided directly. We then look up the cmdline from that, // and rejoin the main logic. if (cmdkey) { if ((line = _shop_find_cmdline(ssp, cmdkey))) { ca_set_line(ssp->ca, line); putil_free(line); } else { putil_int("no line found for cmd key '%s'", cmdkey); rc = SHOP_ERR; } } line = ca_get_line(ssp->ca); // Tell CDB what command line we're looking for. if (cdb_findinit(&cdbf, ssp->cdbp, line, strlen(line)) < 0) { putil_die("cdb_findinit (%s)", line); } // It's possible there would be more than one instance of a given // command line so we look for the full match in a loop. In most // cases this loop will be traversed only once. for (cmdix[0] = '\0'; rc != SHOP_RECYCLED && cdb_findnext(&cdbf) > 0;) { unsigned len; len = cdb_datalen(ssp->cdbp); cdb_read(ssp->cdbp, cmdix, len, cdb_datapos(ssp->cdbp)); cmdix[len] = '\0'; rc = _shop_for_cmd(ssp, cmdix); } // If rc equals SHOP_RECYCLED we've matched a command from a given PTX. // All we need to do is determine all the target files which // go with it and process them. if (rc == SHOP_RECYCLED) { vb_printf(VB_SHOP, "WINNER is %s (%s)", ssp->winner, ssp->wix); if (_shop_collect_targets(ssp, ssp->wincmd) == SHOP_RECYCLED) { if (_shop_process_targets(ssp)) { rc = SHOP_ERR; } else { // Mark the passed-in CA as recycled. ca_set_recycled(ssp->ca, ssp->winner); } } } // All the rest is cleanup ... ca_clear_pa(ssp->ca); _shop_ptx_destroy(ssp->ptx_dict); return rc; }
static shop_e _shop_collect_targets(shopping_state_s *ssp, CCS cmdix) { char key[64]; struct cdb_find cdbf_tgt; unsigned vlen; shop_e rc; rc = SHOP_RECYCLED; vb_printf(VB_SHOP, "COLLECTING: [%s]", cmdix); // First we collect all targets of the winning command in the // "real" CA object .... snprintf(key, charlen(key), ">%s", cmdix); if (cdb_findinit(&cdbf_tgt, ssp->cdbp, key, strlen(key)) < 0) { putil_die("cdb_findinit: %s (%s)", key, ca_get_line(ssp->ca)); } while (cdb_findnext(&cdbf_tgt) > 0) { CS buf, pskeys, csv, ptxes, ixstr; CCS pskey; vlen = cdb_datalen(ssp->cdbp); buf = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, buf, vlen, cdb_datapos(ssp->cdbp)); buf[vlen] = '\0'; if (!(ptxes = strstr(buf, FS1))) { putil_int("bad format in roadmap: %s", buf); rc = SHOP_ERR; continue; } *ptxes++ = '\0'; pskeys = buf; // Make sure to skip all targets not associated with the winning PTX. for (ixstr = util_strsep(&ptxes, FS2); ixstr; ixstr = util_strsep(&ptxes, FS2)) { if (!strcmp(ixstr, ssp->wix)) { ps_o tgt_ps; pa_o dummy_pa; for (pskey = util_strsep(&pskeys, FS2); pskey && rc == SHOP_RECYCLED; pskey = util_strsep(&pskeys, FS2)) { if (cdb_find(ssp->cdbp, pskey, strlen(pskey)) <= 0) { putil_int("bad key in roadmap: %s", pskey); continue; } vlen = cdb_datalen(ssp->cdbp); csv = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, csv, vlen, cdb_datapos(ssp->cdbp)); csv[vlen] = '\0'; vb_printf(VB_SHOP, "COLLECTED [%s] %s", pskey, csv); tgt_ps = ps_newFromCSVString(csv); // Unfortunately, as noted elsewhere, a CA object // doesn't hold PS objects, it holds PA objects. // So we need a dummy PA to wrap the target PS in. dummy_pa = pa_new(); pa_set_ps(dummy_pa, tgt_ps); if (ps_is_link(tgt_ps)) { pa_set_op(dummy_pa, OP_LINK); } else if (ps_is_symlink(tgt_ps)) { pa_set_op(dummy_pa, OP_SYMLINK); } else if (ps_is_unlink(tgt_ps)) { pa_set_op(dummy_pa, OP_UNLINK); } else { pa_set_op(dummy_pa, OP_CREAT); } pa_set_call(dummy_pa, "dummy"); pa_set_uploadable(dummy_pa, 1); ca_record_pa(ssp->ca, dummy_pa); } // Once we've reached the winner, we're done. break; } } } return rc; }
static void _shop_compare_prereqs(shopping_state_s *ssp, CCS cmdix) { char key[64]; struct cdb_find cdbf_prq; unsigned vlen; snprintf(key, charlen(key), "<%s", cmdix); if (cdb_findinit(&cdbf_prq, ssp->cdbp, key, strlen(key)) < 0) { putil_die("cdb_findinit: %s (%s)", key, ca_get_line(ssp->ca)); } while (cdb_findnext(&cdbf_prq) > 0 && _shop_ptx_count(ssp)) { CCS pskey; CS pskeys, prqline, ptxes1, ptxbuf, ixstr; int ptxesleft; // Read a prereq line from the roadmap. vlen = cdb_datalen(ssp->cdbp); prqline = (CS)alloca(vlen + 1); cdb_read(ssp->cdbp, prqline, vlen, cdb_datapos(ssp->cdbp)); prqline[vlen] = '\0'; // Split the line into a list of path states and a list of ptxes. if (!(ptxes1 = strstr(prqline, FS1))) { putil_int("bad format in roadmap: %s", prqline); break; } *ptxes1++ = '\0'; pskeys = prqline; // Here we make a temporary copy of the ptx list and take a // quick run through it. If none of the listed ptxes remain // eligible, we can skip evaluating this set of path states. // On the other hand, if we do go on to evaluate any of these // ptxes, mark them as having been evaluated. In order to // be a winner you must not only survive the war but also // show evidence of having fought. No one gets a medal for // hiding in a foxhole, so to speak. ptxbuf = (CS)alloca(strlen(ptxes1) + CHARSIZE); strcpy(ptxbuf, ptxes1); for (ptxesleft = 0, ixstr = util_strsep(&ptxbuf, FS2); ixstr; ixstr = util_strsep(&ptxbuf, FS2)) { if (_shop_ptx_contains(ssp, ixstr)) { ptxesleft = 1; _shop_ptx_mark_as_seen(ssp, ixstr); } } if (!ptxesleft) { continue; } /* * Evaluate individual path states. The format allows for * pathstate keys to be enumerated, like S1+S2+S3+S4, or * for ranges in the form "S1-4". */ for (pskey = util_strsep(&pskeys, FS2); pskey && _shop_ptx_count(ssp); pskey = util_strsep(&pskeys, FS2)) { CS end; if ((end = strchr(pskey, '-'))) { uint64_t i, first, last; char nkey[64], *p; // The following relies on the fact that Integer.toString // is guaranteed to use lower-case characters. *end++ = '\0'; for (p = nkey; ISALPHA(*pskey) && ISUPPER(*pskey); ) { *p++ = *pskey++; } first = strtol(pskey, NULL, RMAP_RADIX); last = strtol(end, NULL, RMAP_RADIX); for (i = first; i <= last; i++) { util_format_to_radix(RMAP_RADIX, p, charlen(nkey), i); _shop_cmp_pathstate(ssp, nkey, ptxes1); } } else { _shop_cmp_pathstate(ssp, pskey, ptxes1); } } } ps_destroy(CurrentPS); CurrentPS = NULL; }