pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools
Log | Files | Refs | README | LICENSE

reject.c (1592B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "pkg.h"
      3 
      4 void
      5 rej_free(struct db *db)
      6 {
      7 	struct rejrule *rule, *tmp;
      8 
      9 	for (rule = TAILQ_FIRST(&db->rejrule_head); rule; rule = tmp) {
     10 		tmp = TAILQ_NEXT(rule, entry);
     11 		regfree(&rule->preg);
     12 		free(rule);
     13 	}
     14 }
     15 
     16 /* Parse reject.conf and pre-compute regexes */
     17 int
     18 rej_load(struct db *db)
     19 {
     20 	struct rejrule *rule;
     21 	char rejpath[PATH_MAX];
     22 	FILE *fp;
     23 	char *buf = NULL;
     24 	size_t sz = 0;
     25 	ssize_t len;
     26 	int r;
     27 
     28 	estrlcpy(rejpath, db->root, sizeof(rejpath));
     29 	estrlcat(rejpath, DBPATHREJECT, sizeof(rejpath));
     30 
     31 	if (!(fp = fopen(rejpath, "r")))
     32 		return -1;
     33 
     34 	while ((len = getline(&buf, &sz, fp)) != -1) {
     35 		/* skip empty lines and comments. */
     36 		if (!len || buf[0] == '#' || buf[0] == '\n')
     37 			continue;
     38 		if (len > 0 && buf[len - 1] == '\n')
     39 			buf[len - 1] = '\0';
     40 
     41 		/* copy and add regex */
     42 		rule = emalloc(sizeof(*rule));
     43 
     44 		r = regcomp(&(rule->preg), buf, REG_NOSUB | REG_EXTENDED);
     45 		if (r != 0) {
     46 			regerror(r, &(rule->preg), buf, len);
     47 			weprintf("invalid pattern: %s\n", buf);
     48 			free(buf);
     49 			fclose(fp);
     50 			rej_free(db);
     51 			return -1;
     52 		}
     53 
     54 		TAILQ_INSERT_TAIL(&db->rejrule_head, rule, entry);
     55 	}
     56 
     57 	if (ferror(fp)) {
     58 		weprintf("%s: read error:", rejpath);
     59 		free(buf);
     60 		fclose(fp);
     61 		rej_free(db);
     62 		return -1;
     63 	}
     64 
     65 	free(buf);
     66 	fclose(fp);
     67 
     68 	return 0;
     69 }
     70 
     71 /* Match pre-computed regexes against the file */
     72 int
     73 rej_match(struct db *db, const char *file)
     74 {
     75 	struct rejrule *rule;
     76 
     77 	TAILQ_FOREACH(rule, &db->rejrule_head, entry)
     78 		if (regexec(&rule->preg, file, 0, NULL, 0) != REG_NOMATCH)
     79 			return 1;
     80 	return 0;
     81 }