summaryrefslogtreecommitdiffstats
path: root/lib/device/dev-cache.c
blob: 4e6ed3ecfff604139322eabed3e1814d52b7d0af (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2001 Sistina Software (UK) Limited.
 *
 * This file is released under the GPL.
 */

#include "dev-cache.h"
#include "log.h"
#include "pool.h"
#include "hash.h"
#include "list.h"
#include "dbg_malloc.h"

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>

/*
 * FIXME: really need to seperate names from the devices since
 * multiple names can point to the same device.
 */

struct dev_iter {
	struct hash_node *current;
	struct dev_filter *filter;
};

struct dir_list {
	struct list_head list;
	char dir[0];
};

static struct {
	struct pool *mem;
	struct hash_table *devices;

	int has_scanned;
	struct list_head dirs;

} _cache;


#define _alloc(x) pool_alloc(_cache.mem, (x))
#define _free(x) pool_free(_cache.mem, (x))

static int _dir_scan(const char *dir);

/*
 * return a new path for the destination of the path.
 */
static char *_follow_link(const char *path, struct stat *info)
{
	char buffer[PATH_MAX + 1];
	int n;
	n = readlink(path, buffer, sizeof(buffer) - 1);

	if (n <= 0)
		return NULL;

	buffer[n] = '\0';

	if (stat(buffer, info) < 0) {
		log_sys_very_verbose("stat", buffer);
		return NULL;
	}

	return pool_strdup(_cache.mem, buffer);
}

/*
 * Get rid of extra slashes in the path string.
 */
static void _collapse_slashes(char *str)
{
	char *ptr;
	int was_slash = 0;

	for (ptr = str; *ptr; ptr++) {
		if (*ptr == '/') {
			if (was_slash)
				continue;

			was_slash = 1;
		} else
			was_slash = 0;
		*str++ = *ptr;
	}

	*str = *ptr;
}

static struct device *_create_dev(const char *path, struct stat *info)
{
	struct device *dev;
	char *name = pool_strdup(_cache.mem, path);

	if (!name) {
		stack;
		return NULL;
	}

	_collapse_slashes(name);

	if (!(dev = _alloc(sizeof(*dev)))) {
		stack;
		goto bad;
	}

	dev->name = name;
	dev->dev = info->st_rdev;
	return dev;

 bad:
	_free(name);
	return NULL;
}

static int _insert(const char *path, int recurse)
{
	struct stat info;
	struct device *dev;

	/* If entry already exists, replace it */
        if ((dev = (struct device *)hash_lookup(_cache.devices, path))) {
		log_debug("dev-cache: removing %s", path);
		hash_remove(_cache.devices, path);
	}

	if (stat(path, &info) < 0) {
		log_sys_very_verbose("stat", path);
		return 0;
	}

	if (S_ISDIR(info.st_mode)) {
		if (recurse)
			return _dir_scan(path);

		return 0;
	}

	if (S_ISLNK(info.st_mode)) {
		log_debug("%s: Following symbolic link", path);
		if (!(path = _follow_link(path, &info))) {
			stack;
			return 0;
		}
	}

	if (!S_ISBLK(info.st_mode)) {
		log_debug("%s: Not a block device", path);
		return 0;
	}

	if (!(dev = _create_dev(path, &info))) {
		log_debug("%s: Creation of device cache entry failed!", path);
		return 0;
	}

	log_debug("dev-cache: adding %s", path);
	hash_insert(_cache.devices, path, dev);

	return 1;
}

static char *_join(const char *dir, const char *name)
{
	int len = strlen(dir) + strlen(name) + 2;
	char *r = dbg_malloc(len);
	if (r)
		snprintf(r, len, "%s/%s", dir, name);

	return r;
}

static int _dir_scan(const char *dir)
{
	int n, dirent_count;
	struct dirent **dirent;
	char *path;

	dirent_count = scandir(dir, &dirent, NULL, alphasort);
	if (dirent_count > 0) {
		for (n = 0; n < dirent_count; n++) {
			if (dirent[n]->d_name[0] == '.') {
				free(dirent[n]);
				continue;
			}

			if ((path = _join(dir, dirent[n]->d_name)))
				_insert(path, 1);

			dbg_free(path);
			free(dirent[n]);
		}
		free(dirent);
	}

	return 1;
}

static void _full_scan(void)
{
	struct list_head *tmp;

	if (_cache.has_scanned)
		return;

	list_for_each(tmp, &_cache.dirs) {
		struct dir_list *dl = list_entry(tmp, struct dir_list, list);
		_dir_scan(dl->dir);
	}

	_cache.has_scanned = 1;
}

int dev_cache_init(void)
{
	if (!(_cache.mem = pool_create(10 * 1024))) {
		stack;
		return 0;
	}

	if (!(_cache.devices = hash_create(128))) {
		stack;
		pool_destroy(_cache.mem);
		_cache.mem = 0;
		return 0;
	}

	INIT_LIST_HEAD(&_cache.dirs);

	return 1;
}

void dev_cache_exit(void)
{
	pool_destroy(_cache.mem);
	hash_destroy(_cache.devices);
}

int dev_cache_add_dir(const char *path)
{
	struct dir_list *dl;
	struct stat st;

	if (stat(path, &st)) {
		log_error("Ignoring %s: %s", path, strerror(errno));
		/* But don't fail */
		return 1;
	}

	if (!S_ISDIR(st.st_mode)) {
		log_error("Ignoring %s: Not a directory", path);
		return 1;
	}

	if (!(dl = _alloc(sizeof(*dl) + strlen(path) + 1)))
		return 0;

	strcpy(dl->dir, path);
	list_add(&dl->list, &_cache.dirs);
	return 1;
}

struct device *dev_cache_get(const char *name, struct dev_filter *f)
{
	struct device *d = (struct device *) hash_lookup(_cache.devices, name);

	if (!d) {
		_insert(name, 0);
		d = (struct device *) hash_lookup(_cache.devices, name);
	}

	return (d && (!f || f->passes_filter(f, d))) ? d : NULL;
}

struct dev_iter *dev_iter_create(struct dev_filter *f)
{
	struct dev_iter *di = dbg_malloc(sizeof(*di));

	if (!di)
		return NULL;

	_full_scan();
	di->current = hash_get_first(_cache.devices);
	di->filter = f;

	return di;
}

void dev_iter_destroy(struct dev_iter *iter)
{
	dbg_free(iter);
}

static inline struct device *_iter_next(struct dev_iter *iter)
{
	struct device *d = hash_get_data(_cache.devices, iter->current);
	iter->current = hash_get_next(_cache.devices, iter->current);
	return d;
}

struct device *dev_iter_get(struct dev_iter *iter)
{
	while (iter->current) {
		struct device *d = _iter_next(iter);
		if (!iter->filter ||
		    iter->filter->passes_filter(iter->filter, d))
			return d;
	}

	return NULL;
}