summaryrefslogtreecommitdiffstats
path: root/lib/label/label.c
blob: be2bcefa8e1c99e84fbeec6fcfadaeda1a4c6eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2002 Sistina Software
 *
 * This file is released under the LGPL.
 */

#include "label.h"
#include "list.h"
#include "dbg_malloc.h"
#include "log.h"

/*
 * Internal labeller struct.
 */
struct labeller_i {
	struct list list;

	struct labeller *l;
	char name[0];
};

static struct list _labellers;


static struct labeller_i *_alloc_li(const char *name, struct labeller *l)
{
	struct labeller_i *li;
	size_t len;

	len = sizeof(*li) + strlen(name) + 1;

	if (!(li = dbg_malloc(len))) {
		log_error("Couldn't allocate memory for labeller list object.");
		return NULL;
	}

	li->l = l;
	strcpy(li->name, name);

	return li;
}

static void _free_li(struct labeller_i *li)
{
	dbg_free(li);
}


int label_init(void)
{
	list_init(&_labellers);
	return 1;
}

void label_exit(void)
{
	struct list *c, *n;
	struct labeller_i *li;

	for (c = _labellers.n; c != &_labellers; c = n) {
		n = c->n;
		li = list_item(c, struct labeller_i);
		_free_li(li);
	}
}

int label_register_handler(const char *name, struct labeller *handler)
{
	struct labeller_i *li;

	if (!(li = _alloc_li(name, handler))) {
		stack;
		return 0;
	}

	list_add(&_labellers, &li->list);
	return 1;
}

struct labeller *label_get_handler(const char *name)
{
	struct list *lih;
	struct labeller_i *li;

	list_iterate (lih, &_labellers) {
		li = list_item(lih, struct labeller_i);
		if (!strcmp(li->name, name))
			return li->l;
	}

	return NULL;
}

static struct labeller *_find_labeller(struct device *dev)
{
	struct list *lih;
	struct labeller_i *li;

	list_iterate (lih, &_labellers) {
		li = list_item(lih, struct labeller_i);
		if (li->l->ops->can_handle(li->l, dev))
			return li->l;
	}

	log_debug("No label on device '%s'.", dev_name(dev));
	return NULL;
}

int label_remove(struct device *dev)
{
	struct labeller *l;

	if (!(l = _find_labeller(dev))) {
		stack;
		return 0;
	}

	return l->ops->remove(l, dev);
}

int label_read(struct device *dev, struct label **result)
{
	int r;
	struct list *lih;
	struct labeller_i *li;

	list_iterate (lih, &_labellers) {
		li = list_item(lih, struct labeller_i);
		if ((r = li->l->ops->read(li->l, dev, result))) {
			(*result)->labeller = li->l;
			return r;
		}
	}

	log_debug("No label on device '%s'.", dev_name(dev));
	return 0;
}

int label_verify(struct device *dev)
{
	struct labeller *l;

	if (!(l = _find_labeller(dev))) {
		stack;
		return 0;
	}

	return l->ops->verify(l, dev);
}

void label_destroy(struct label *lab)
{
	lab->labeller->ops->destroy_label(lab->labeller, lab);
}