2001-09-28 19:42:25 +04:00
/*
2001-10-03 15:06:31 +04:00
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
2001-09-28 19:42:25 +04:00
*
* This file is released under the GPL .
*/
# include "dev-cache.h"
# include "log.h"
# include "pool.h"
# include "hash.h"
2001-10-04 14:13:07 +04:00
# include "list.h"
# include "dbg_malloc.h"
2001-09-28 19:42:25 +04:00
# include <stdlib.h>
2001-10-04 14:13:07 +04:00
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include <sys/param.h>
# include <dirent.h>
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
/*
* FIXME : really need to seperate names from the devices since
* multiple names can point to the same device .
*/
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
struct dev_iter {
struct hash_node * current ;
struct dev_filter * filter ;
} ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
struct dir_list {
2001-10-04 14:13:07 +04:00
struct list_head list ;
char dir [ 0 ] ;
2001-09-28 19:42:25 +04:00
} ;
2001-10-03 15:06:31 +04:00
static struct {
struct pool * mem ;
2001-09-28 19:42:25 +04:00
struct hash_table * devices ;
2001-10-03 15:06:31 +04:00
int has_scanned ;
struct list_head dirs ;
2001-09-28 19:42:25 +04:00
} _cache ;
2001-10-03 15:06:31 +04:00
# define _alloc(x) pool_alloc(_cache.mem, (x))
# define _free(x) pool_free(_cache.mem, (x))
/*
* return a new path for the destination of the path .
*/
2001-10-04 14:13:07 +04:00
static char * _follow_link ( const char * path , struct stat * info )
2001-10-03 15:06:31 +04:00
{
2001-10-04 14:13:07 +04:00
char buffer [ PATH_MAX + 1 ] ;
2001-10-03 15:06:31 +04:00
int n ;
2001-10-04 14:13:07 +04:00
n = readlink ( path , buffer , sizeof ( buffer ) - 1 ) ;
2001-10-03 15:06:31 +04:00
if ( n < = 0 )
return NULL ;
buffer [ n ] = ' \0 ' ;
if ( stat ( buffer , info ) < 0 ) {
log_sys_err ( " stat " ) ;
return NULL ;
}
return pool_strdup ( _cache . mem , buffer ) ;
}
/*
* Get rid of extra slashes in the path string .
*/
static void _collapse_slashes ( char * str )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
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 ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
static struct device * _create_dev ( const char * path )
2001-09-28 19:42:25 +04:00
{
struct stat info ;
2001-10-03 15:06:31 +04:00
struct device * dev ;
2001-10-04 14:13:07 +04:00
char * name = pool_strdup ( _cache . mem , path ) ;
2001-09-28 19:42:25 +04:00
2001-10-04 14:13:07 +04:00
if ( ! name ) {
2001-10-03 15:06:31 +04:00
stack ;
return NULL ;
}
2001-10-04 14:13:07 +04:00
_collapse_slashes ( name ) ;
if ( stat ( name , & info ) < 0 ) {
log_sys_err ( " stat " ) ;
goto bad ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
if ( S_ISLNK ( info . st_mode ) ) {
2001-10-04 14:13:07 +04:00
log_debug ( " %s is a symbolic link, following \n " , name ) ;
if ( ! ( name = _follow_link ( name , & info ) ) ) {
stack ;
goto bad ;
}
2001-10-03 15:06:31 +04:00
}
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
if ( ! S_ISBLK ( info . st_mode ) ) {
2001-10-04 14:13:07 +04:00
log_debug ( " %s is not a block device \n " , name ) ;
goto bad ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
if ( ! ( dev = _alloc ( sizeof ( * dev ) ) ) ) {
stack ;
2001-10-04 14:13:07 +04:00
goto bad ;
2001-10-03 15:06:31 +04:00
}
2001-09-28 19:42:25 +04:00
2001-10-04 14:13:07 +04:00
dev - > name = name ;
2001-10-03 15:06:31 +04:00
dev - > dev = info . st_rdev ;
return dev ;
2001-10-04 14:13:07 +04:00
bad :
_free ( name ) ;
return NULL ;
2001-10-03 15:06:31 +04:00
}
static struct device * _add ( const char * dir , const char * path )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
struct device * d ;
int len = strlen ( dir ) + strlen ( path ) + 2 ;
2001-10-04 14:13:07 +04:00
char * buffer = dbg_malloc ( len ) ;
2001-10-03 15:06:31 +04:00
2001-10-04 14:13:07 +04:00
snprintf ( buffer , len , " %s/%s " , dir , path ) ;
d = dev_cache_get ( buffer , NULL ) ;
dbg_free ( buffer ) ;
2001-10-03 15:06:31 +04:00
return d ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
static int _dir_scan ( const char * dir )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
int n , dirent_count ;
struct dirent * * dirent ;
dirent_count = scandir ( dir , & dirent , NULL , alphasort ) ;
if ( dirent_count > 0 ) {
for ( n = 0 ; n < dirent_count ; n + + ) {
_add ( dir , dirent [ n ] - > d_name ) ;
free ( dirent [ n ] ) ;
}
free ( dirent ) ;
}
return 1 ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
static void _full_scan ( void )
2001-09-28 19:42:25 +04:00
{
2001-10-04 14:13:07 +04:00
struct list_head * tmp ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
if ( _cache . has_scanned )
return ;
2001-09-28 19:42:25 +04:00
2001-10-04 14:13:07 +04:00
list_for_each ( tmp , & _cache . dirs ) {
struct dir_list * dl = list_entry ( tmp , struct dir_list , list ) ;
_dir_scan ( dl - > dir ) ;
}
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
_cache . has_scanned = 1 ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
int dev_cache_init ( void )
2001-09-28 19:42:25 +04:00
{
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 ;
}
return 1 ;
}
2001-10-03 15:06:31 +04:00
void dev_cache_exit ( void )
2001-09-28 19:42:25 +04:00
{
pool_destroy ( _cache . mem ) ;
hash_destroy ( _cache . devices ) ;
}
2001-10-03 15:06:31 +04:00
int dev_cache_add_dir ( const char * path )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
struct dir_list * dl ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
if ( ! ( dl = _alloc ( sizeof ( * dl ) + strlen ( path ) + 1 ) ) )
return 0 ;
strcpy ( dl - > dir , path ) ;
2001-10-04 14:13:07 +04:00
list_add ( & dl - > list , & _cache . dirs ) ;
2001-10-03 15:06:31 +04:00
return 1 ;
2001-09-28 19:42:25 +04:00
}
2001-10-04 14:13:07 +04:00
struct device * _insert_new ( const char * name )
2001-09-28 19:42:25 +04:00
{
2001-10-04 14:13:07 +04:00
struct device * d = _create_dev ( name ) ;
if ( ! d | | ! hash_insert ( _cache . devices , name , d ) )
return NULL ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
return d ;
2001-09-28 19:42:25 +04:00
}
2001-10-04 14:13:07 +04:00
struct device * dev_cache_get ( const char * name , struct dev_filter * f )
{
struct device * d = ( struct device * ) hash_lookup ( _cache . devices , name ) ;
return ( d & & ( ! f | | f - > passes_filter ( f , d ) ) ) ? d : NULL ;
}
2001-10-03 15:06:31 +04:00
struct dev_iter * dev_iter_create ( struct dev_filter * f )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
struct dev_iter * di = dbg_malloc ( sizeof ( * di ) ) ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
if ( ! di )
return NULL ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
_full_scan ( ) ;
2001-10-04 14:13:07 +04:00
di - > current = hash_get_first ( _cache . devices ) ;
2001-10-03 15:06:31 +04:00
di - > filter = f ;
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
return di ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
void dev_iter_destroy ( struct dev_iter * iter )
2001-09-28 19:42:25 +04:00
{
2001-10-03 15:06:31 +04:00
dbg_free ( iter ) ;
}
2001-09-28 19:42:25 +04:00
2001-10-03 15:06:31 +04:00
static inline struct device * _iter_next ( struct dev_iter * iter )
{
struct device * d = hash_get_data ( _cache . devices , iter - > current ) ;
2001-10-04 14:13:07 +04:00
iter - > current = hash_get_next ( _cache . devices , iter - > current ) ;
2001-10-03 15:06:31 +04:00
return d ;
}
struct device * dev_iter_get ( struct dev_iter * iter )
{
while ( iter - > current ) {
struct device * d = _iter_next ( iter ) ;
if ( ! iter - > filter | |
2001-10-04 14:13:07 +04:00
iter - > filter - > passes_filter ( iter - > filter , d ) )
2001-10-03 15:06:31 +04:00
return d ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00
return NULL ;
2001-09-28 19:42:25 +04:00
}
2001-10-03 15:06:31 +04:00