1 /* $NetBSD: filter-composite.c,v 1.1.1.1 2008/12/22 00:17:58 haad Exp $ */
2
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
6 *
7 * This file is part of LVM2.
8 *
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "lib.h"
19 #include "filter-composite.h"
20
21 #include <stdarg.h>
22
_and_p(struct dev_filter * f,struct device * dev)23 static int _and_p(struct dev_filter *f, struct device *dev)
24 {
25 struct dev_filter **filters = (struct dev_filter **) f->private;
26
27 while (*filters) {
28 if (!(*filters)->passes_filter(*filters, dev))
29 return 0;
30 filters++;
31 }
32
33 log_debug("Using %s", dev_name(dev));
34
35 return 1;
36 }
37
_composite_destroy(struct dev_filter * f)38 static void _composite_destroy(struct dev_filter *f)
39 {
40 struct dev_filter **filters = (struct dev_filter **) f->private;
41
42 while (*filters) {
43 (*filters)->destroy(*filters);
44 filters++;
45 }
46
47 dm_free(f->private);
48 dm_free(f);
49 }
50
composite_filter_create(int n,struct dev_filter ** filters)51 struct dev_filter *composite_filter_create(int n, struct dev_filter **filters)
52 {
53 struct dev_filter **filters_copy, *cft;
54
55 if (!filters)
56 return_NULL;
57
58 if (!(filters_copy = dm_malloc(sizeof(*filters) * (n + 1)))) {
59 log_error("composite filters allocation failed");
60 return NULL;
61 }
62
63 memcpy(filters_copy, filters, sizeof(*filters) * n);
64 filters_copy[n] = NULL;
65
66 if (!(cft = dm_malloc(sizeof(*cft)))) {
67 log_error("compsoite filters allocation failed");
68 dm_free(filters_copy);
69 return NULL;
70 }
71
72 cft->passes_filter = _and_p;
73 cft->destroy = _composite_destroy;
74 cft->private = filters_copy;
75
76 return cft;
77 }
78