From 12eabe30319346dfd168c607122d49ecf2a1c76c Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 22 Oct 2001 14:39:12 +0000 Subject: [PATCH] o composite filter that allows us to merge filters. Think of it as &&'ing filters in order. eg, f = composite_filter_create(2, regex_filter, persistent_filter); ownership of the filters passes, they will be destroyed when f's destroy method is called. --- lib/Makefile.in | 3 +- lib/filters/filter-composite.c | 70 +++++++++++++++++++++++++++++++++ lib/filters/filter-composite.h | 14 +++++++ lib/filters/filter-persistent.c | 14 +++---- lib/filters/filter-persistent.h | 2 +- 5 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 lib/filters/filter-composite.c create mode 100644 lib/filters/filter-composite.h diff --git a/lib/Makefile.in b/lib/Makefile.in index 58af73cc8..2e55bd08b 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -16,9 +16,10 @@ SOURCES=\ device/dev-cache.c \ device/dev-io.c \ device/device.c \ + display/display.c \ + filters/filter-composite.c \ filters/filter-persistent.c \ filters/filter-regex.c \ - display/display.c \ filters/filter.c \ format1/disk-rep.c \ format1/format1.c \ diff --git a/lib/filters/filter-composite.c b/lib/filters/filter-composite.c new file mode 100644 index 000000000..df6aa4c6a --- /dev/null +++ b/lib/filters/filter-composite.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the GPL. + */ + +#include "filter-composite.h" +#include "dbg_malloc.h" +#include "log.h" + +#include + +static int _and_p(struct dev_filter *f, struct device *dev) +{ + struct dev_filter **filters = (struct dev_filter **) f->private; + + while (*filters) { + if (!(*filters)->passes_filter(*filters, dev)) + return 0; + filters++; + } + + return 1; +} + +void _destroy(struct dev_filter *f) +{ + struct dev_filter **filters = (struct dev_filter **) f->private; + + while (*filters) { + (*filters)->destroy(*filters); + filters++; + } + + dbg_free(f->private); +} + + +struct dev_filter *composite_filter_create(int n, ...) +{ + struct dev_filter **filters = dbg_malloc(sizeof(*filters) * (n + 1)); + struct dev_filter *cf; + va_list ap; + int i; + + if (!filters) { + stack; + return NULL; + } + + if (!(cf = dbg_malloc(sizeof(*cf)))) { + stack; + dbg_free(filters); + return NULL; + } + + va_start(ap, n); + for (i = 0; i < n; i++) { + struct dev_filter *f = va_arg(ap, struct dev_filter *); + filters[i] = f; + } + filters[i] = NULL; + va_end(ap); + + cf->passes_filter = _and_p; + cf->destroy = _destroy; + cf->private = filters; + + return cf; +} diff --git a/lib/filters/filter-composite.h b/lib/filters/filter-composite.h new file mode 100644 index 000000000..7462e4e22 --- /dev/null +++ b/lib/filters/filter-composite.h @@ -0,0 +1,14 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the GPL. + */ + +#ifndef _LVM_FILTER_COMPOSITE_H +#define _LVM_FILTER_COMPOSITE_H + +#include "dev-cache.h" + +struct dev_filter *composite_filter_create(int n, ...); + +#endif diff --git a/lib/filters/filter-persistent.c b/lib/filters/filter-persistent.c index b236e3be9..457c2cf21 100644 --- a/lib/filters/filter-persistent.c +++ b/lib/filters/filter-persistent.c @@ -28,7 +28,7 @@ struct pfilter { #define PF_UNCHECKED ((void *) 1) #define PF_CHECKED ((void *) 2) -int _init_hash(struct pfilter *pf) +static int _init_hash(struct pfilter *pf) { if (pf->devices) hash_destroy(pf->devices); @@ -37,7 +37,7 @@ int _init_hash(struct pfilter *pf) return pf ? 1 : 0; } -int _load(struct pfilter *pf) +static int _load(struct pfilter *pf) { int r = 0; struct config_file *cf; @@ -82,7 +82,7 @@ int _load(struct pfilter *pf) return r; } -int _dump(struct pfilter *pf) +static int _dump(struct pfilter *pf) { int first = 1; struct hash_node *n; @@ -112,7 +112,7 @@ int _dump(struct pfilter *pf) return 1; } -int _check(const char *path) +static int _check(const char *path) { int fd = open(path, O_RDONLY), r = 0; @@ -123,7 +123,7 @@ int _check(const char *path) return r; } -int _init_valid_p(struct dev_filter *f, struct device *dev) +static int _init_valid_p(struct dev_filter *f, struct device *dev) { struct pfilter *pf = (struct pfilter *) f->private; void *l = hash_lookup(pf->devices, dev->name); @@ -139,7 +139,7 @@ int _init_valid_p(struct dev_filter *f, struct device *dev) return 0; } -int _valid_p(struct dev_filter *f, struct device *dev) +static int _valid_p(struct dev_filter *f, struct device *dev) { struct pfilter *pf = (struct pfilter *) f->private; void *l = hash_lookup(pf->devices, dev->name); @@ -155,7 +155,7 @@ int _valid_p(struct dev_filter *f, struct device *dev) return 1; } -void _destroy(struct dev_filter *f) +static void _destroy(struct dev_filter *f) { struct pfilter *pf = (struct pfilter *) f->private; diff --git a/lib/filters/filter-persistent.h b/lib/filters/filter-persistent.h index 8f445762b..6d68e1c4d 100644 --- a/lib/filters/filter-persistent.h +++ b/lib/filters/filter-persistent.h @@ -5,7 +5,7 @@ */ #ifndef _LVM_FILTER_PERSISTENT_H -#define _LVM_FILTER_PERSISTENT_h +#define _LVM_FILTER_PERSISTENT_H #include "dev-cache.h" -- 2.43.5