1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-10-11 03:33:14 +03:00

o dev_cache_t program works

This commit is contained in:
Joe Thornber
2001-10-08 13:58:52 +00:00
parent 721128e86d
commit 43b7b8cf69
6 changed files with 140 additions and 45 deletions

6
configure vendored
View File

@@ -1317,7 +1317,7 @@ else
int main() { int main() {
/* Ultrix mips cc rejects this. */ /* Ultrix mips cc rejects this. */
typedef int charset[2]; const charset x = {0,0}; typedef int charset[2]; const charset x;
/* SunOS 4.1.1 cc rejects this. */ /* SunOS 4.1.1 cc rejects this. */
char const *const *ccp; char const *const *ccp;
char **p; char **p;
@@ -1392,7 +1392,7 @@ for ac_kw in inline __inline__ __inline; do
#include "confdefs.h" #include "confdefs.h"
int main() { int main() {
} int $ac_kw foo() { } $ac_kw foo() {
; return 0; } ; return 0; }
EOF EOF
if { (eval echo configure:1399: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then if { (eval echo configure:1399: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
@@ -2160,6 +2160,7 @@ lib/Makefile \
man/Makefile \ man/Makefile \
tools/Makefile \ tools/Makefile \
test/mm/Makefile \ test/mm/Makefile \
test/device/Makefile \
test/format1/Makefile \ test/format1/Makefile \
" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15 " | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
EOF EOF
@@ -2257,6 +2258,7 @@ lib/Makefile \
man/Makefile \ man/Makefile \
tools/Makefile \ tools/Makefile \
test/mm/Makefile \ test/mm/Makefile \
test/device/Makefile \
test/format1/Makefile \ test/format1/Makefile \
"} "}
EOF EOF

View File

@@ -136,5 +136,6 @@ lib/Makefile \
man/Makefile \ man/Makefile \
tools/Makefile \ tools/Makefile \
test/mm/Makefile \ test/mm/Makefile \
test/device/Makefile \
test/format1/Makefile \ test/format1/Makefile \
) )

View File

@@ -188,7 +188,7 @@ void *hash_get_data(struct hash_table *t, struct hash_node *n)
static struct hash_node *_next_slot(struct hash_table *t, unsigned int s) static struct hash_node *_next_slot(struct hash_table *t, unsigned int s)
{ {
struct hash_node *c = 0; struct hash_node *c = NULL;
int i; int i;
for (i = s; i < t->num_slots && !c; i++) for (i = s; i < t->num_slots && !c; i++)
@@ -204,6 +204,7 @@ struct hash_node *hash_get_first(struct hash_table *t)
struct hash_node *hash_get_next(struct hash_table *t, struct hash_node *n) struct hash_node *hash_get_next(struct hash_table *t, struct hash_node *n)
{ {
return n->next ? n->next : _next_slot(t, _hash(n->key) + 1); unsigned int h = _hash(n->key) & (t->num_slots - 1);
return n->next ? n->next : _next_slot(t, h + 1);
} }

View File

@@ -46,6 +46,8 @@ static struct {
#define _alloc(x) pool_alloc(_cache.mem, (x)) #define _alloc(x) pool_alloc(_cache.mem, (x))
#define _free(x) pool_free(_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. * return a new path for the destination of the path.
*/ */
@@ -90,9 +92,8 @@ static void _collapse_slashes(char *str)
*str = *ptr; *str = *ptr;
} }
static struct device *_create_dev(const char *path) static struct device *_create_dev(const char *path, struct stat *info)
{ {
struct stat info;
struct device *dev; struct device *dev;
char *name = pool_strdup(_cache.mem, path); char *name = pool_strdup(_cache.mem, path);
@@ -103,31 +104,13 @@ static struct device *_create_dev(const char *path)
_collapse_slashes(name); _collapse_slashes(name);
if (stat(name, &info) < 0) {
log_sys_err("stat");
goto bad;
}
if (S_ISLNK(info.st_mode)) {
log_debug("%s is a symbolic link, following\n", name);
if (!(name = _follow_link(name, &info))) {
stack;
goto bad;
}
}
if (!S_ISBLK(info.st_mode)) {
log_debug("%s is not a block device\n", name);
goto bad;
}
if (!(dev = _alloc(sizeof(*dev)))) { if (!(dev = _alloc(sizeof(*dev)))) {
stack; stack;
goto bad; goto bad;
} }
dev->name = name; dev->name = name;
dev->dev = info.st_rdev; dev->dev = info->st_rdev;
return dev; return dev;
bad: bad:
@@ -135,28 +118,75 @@ static struct device *_create_dev(const char *path)
return NULL; return NULL;
} }
static struct device *_add(const char *dir, const char *path) static int _insert(const char *path, int recurse)
{ {
struct device *d; struct stat info;
int len = strlen(dir) + strlen(path) + 2; struct device *dev;
char *buffer = dbg_malloc(len);
snprintf(buffer, len, "%s/%s", dir, path); log_debug("dev-cache adding %s", path);
d = dev_cache_get(buffer, NULL);
dbg_free(buffer);
return d; if (stat(path, &info) < 0) {
log_sys_err("stat");
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 is a symbolic link, following", path);
if (!(path = _follow_link(path, &info))) {
stack;
return 0;
}
}
if (!S_ISBLK(info.st_mode)) {
log_debug("%s is not a block device", path);
return 0;
}
if (!(dev = _create_dev(path, &info))) {
stack;
return 0;
}
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) static int _dir_scan(const char *dir)
{ {
int n, dirent_count; int n, dirent_count;
struct dirent **dirent; struct dirent **dirent;
char *path;
dirent_count = scandir(dir, &dirent, NULL, alphasort); dirent_count = scandir(dir, &dirent, NULL, alphasort);
if (dirent_count > 0) { if (dirent_count > 0) {
for (n = 0; n < dirent_count; n++) { for (n = 0; n < dirent_count; n++) {
_add(dir, dirent[n]->d_name); 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[n]);
} }
free(dirent); free(dirent);
@@ -217,21 +247,14 @@ int dev_cache_add_dir(const char *path)
return 1; return 1;
} }
struct device *_insert_new(const char *name)
{
struct device *d = _create_dev(name);
if (!d || !hash_insert(_cache.devices, name, d))
return NULL;
return d;
}
struct device *dev_cache_get(const char *name, struct dev_filter *f) struct device *dev_cache_get(const char *name, struct dev_filter *f)
{ {
struct device *d = (struct device *) hash_lookup(_cache.devices, name); struct device *d = (struct device *) hash_lookup(_cache.devices, name);
if (!d && (d = _create_dev(name))) if (!d) {
hash_insert(_cache.devices, name, d); _insert(name, 0);
d = (struct device *) hash_lookup(_cache.devices, name);
}
return (d && (!f || f->passes_filter(f, d))) ? d : NULL; return (d && (!f || f->passes_filter(f, d))) ? d : NULL;
} }

View File

@@ -0,0 +1,20 @@
#
# Copyright (C) 2001 Sistina Software (UK) Limited
#
# This file is released under the GPL.
#
srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
SOURCES=\
dev_cache_t.c
TARGETS=dev_cache_t
include ../../make.tmpl
dev_cache_t: dev_cache_t.o $(top_srcdir)/lib/liblvm.a
$(CC) -o dev_cache_t dev_cache_t.o -L$(top_srcdir)/lib -llvm

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2001 Sistina Software (UK) Limited
*
* This file is released under the GPL.
*/
#include "dbg_malloc.h"
#include "dev-cache.h"
#include "log.h"
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
struct device *dev;
struct dev_iter *iter;
init_log(stderr);
init_debug(_LOG_DEBUG);
if (!dev_cache_init()) {
log_error("couldn't initialise dev_cache_init failed\n");
exit(1);
}
for (i = 1; i < argc; i++) {
if (!dev_cache_add_dir(argv[i])) {
log_error("couldn't add '%s' to dev_cache\n", argv[i]);
exit(1);
}
}
if (!(iter = dev_iter_create(NULL))) {
log_error("couldn't create iterator\n");
exit(1);
}
while ((dev = dev_iter_get(iter)))
printf("%s\n", dev->name);
dev_iter_destroy(iter);
dev_cache_exit();
dump_memory();
fin_log();
return 0;
}