2001-10-03 16:41:29 +04:00
/*
* 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))
2001-10-08 17:58:52 +04:00
static int _dir_scan ( const char * dir ) ;
2001-10-03 16:41:29 +04:00
/*
* 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 ) {
2001-10-10 20:36:32 +04:00
log_sys_very_verbose ( " stat " , buffer ) ;
2001-10-03 16:41:29 +04:00
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 ;
}
2001-10-08 17:58:52 +04:00
static struct device * _create_dev ( const char * path , struct stat * info )
2001-10-03 16:41:29 +04:00
{
struct device * dev ;
char * name = pool_strdup ( _cache . mem , path ) ;
if ( ! name ) {
stack ;
return NULL ;
}
_collapse_slashes ( name ) ;
2001-10-08 17:58:52 +04:00
if ( ! ( dev = _alloc ( sizeof ( * dev ) ) ) ) {
stack ;
2001-10-03 16:41:29 +04:00
goto bad ;
}
2001-10-08 17:58:52 +04:00
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 ;
2001-10-10 20:36:32 +04:00
log_very_verbose ( " dev-cache adding %s " , path ) ;
2001-10-08 17:58:52 +04:00
if ( stat ( path , & info ) < 0 ) {
2001-10-10 20:36:32 +04:00
log_sys_very_verbose ( " stat " , path ) ;
2001-10-08 17:58:52 +04:00
return 0 ;
}
if ( S_ISDIR ( info . st_mode ) ) {
if ( recurse )
return _dir_scan ( path ) ;
return 0 ;
}
2001-10-03 16:41:29 +04:00
if ( S_ISLNK ( info . st_mode ) ) {
2001-10-08 17:58:52 +04:00
log_debug ( " %s is a symbolic link, following " , path ) ;
if ( ! ( path = _follow_link ( path , & info ) ) ) {
2001-10-03 16:41:29 +04:00
stack ;
2001-10-08 17:58:52 +04:00
return 0 ;
2001-10-03 16:41:29 +04:00
}
}
if ( ! S_ISBLK ( info . st_mode ) ) {
2001-10-08 17:58:52 +04:00
log_debug ( " %s is not a block device " , path ) ;
return 0 ;
2001-10-03 16:41:29 +04:00
}
2001-10-08 17:58:52 +04:00
if ( ! ( dev = _create_dev ( path , & info ) ) ) {
2001-10-03 16:41:29 +04:00
stack ;
2001-10-08 17:58:52 +04:00
return 0 ;
2001-10-03 16:41:29 +04:00
}
2001-10-08 17:58:52 +04:00
hash_insert ( _cache . devices , path , dev ) ;
return 1 ;
2001-10-03 16:41:29 +04:00
}
2001-10-08 17:58:52 +04:00
static char * _join ( const char * dir , const char * name )
2001-10-03 16:41:29 +04:00
{
2001-10-08 17:58:52 +04:00
int len = strlen ( dir ) + strlen ( name ) + 2 ;
char * r = dbg_malloc ( len ) ;
if ( r )
snprintf ( r , len , " %s/%s " , dir , name ) ;
2001-10-03 16:41:29 +04:00
2001-10-08 17:58:52 +04:00
return r ;
2001-10-03 16:41:29 +04:00
}
static int _dir_scan ( const char * dir )
{
int n , dirent_count ;
struct dirent * * dirent ;
2001-10-08 17:58:52 +04:00
char * path ;
2001-10-03 16:41:29 +04:00
dirent_count = scandir ( dir , & dirent , NULL , alphasort ) ;
if ( dirent_count > 0 ) {
for ( n = 0 ; n < dirent_count ; n + + ) {
2001-10-08 17:58:52 +04:00
if ( dirent [ n ] - > d_name [ 0 ] = = ' . ' ) {
free ( dirent [ n ] ) ;
continue ;
}
if ( ( path = _join ( dir , dirent [ n ] - > d_name ) ) )
_insert ( path , 1 ) ;
dbg_free ( path ) ;
2001-10-03 16:41:29 +04:00
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 ;
}
2001-10-08 16:11:33 +04:00
INIT_LIST_HEAD ( & _cache . dirs ) ;
2001-10-03 16:41:29 +04:00
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 ;
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 ) ;
2001-10-08 16:11:33 +04:00
2001-10-08 17:58:52 +04:00
if ( ! d ) {
_insert ( name , 0 ) ;
d = ( struct device * ) hash_lookup ( _cache . devices , name ) ;
}
2001-10-08 16:11:33 +04:00
2001-10-03 16:41:29 +04:00
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 ;
}