2001-10-03 16:41:29 +04:00
/*
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
*
2001-10-31 15:47:01 +03:00
* This file is released under the LGPL .
2001-10-03 16:41:29 +04:00
*/
# include "dev-cache.h"
# include "log.h"
# include "pool.h"
# include "hash.h"
# include "list.h"
2001-10-25 15:34:55 +04:00
# include "lvm-types.h"
# include "btree.h"
2001-10-03 16:41:29 +04:00
# 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 {
2001-10-25 15:34:55 +04:00
struct btree_iter * current ;
2001-10-03 16:41:29 +04:00
struct dev_filter * filter ;
} ;
struct dir_list {
2001-10-31 15:47:01 +03:00
struct list list ;
2001-10-03 16:41:29 +04:00
char dir [ 0 ] ;
} ;
static struct {
struct pool * mem ;
2001-10-25 15:34:55 +04:00
struct hash_table * names ;
struct btree * devices ;
2001-10-03 16:41:29 +04:00
int has_scanned ;
2001-10-31 15:47:01 +03:00
struct list dirs ;
2001-10-03 16:41:29 +04:00
} _cache ;
# define _alloc(x) pool_alloc(_cache.mem, (x))
# define _free(x) pool_free(_cache.mem, (x))
2001-10-25 15:34:55 +04:00
static int _insert ( const char * path , int rec ) ;
2001-10-25 18:04:18 +04:00
static struct device * _create_dev ( dev_t d )
2001-10-25 15:34:55 +04:00
{
struct device * dev ;
if ( ! ( dev = _alloc ( sizeof ( * dev ) ) ) ) {
stack ;
2001-10-25 18:04:18 +04:00
return NULL ;
2001-10-25 15:34:55 +04:00
}
2001-10-31 15:47:01 +03:00
list_init ( & dev - > aliases ) ;
2001-10-25 15:34:55 +04:00
dev - > dev = d ;
2001-11-14 13:01:52 +03:00
dev - > fd = - 1 ;
2001-10-25 15:34:55 +04:00
return dev ;
}
static int _add_alias ( struct device * dev , const char * path )
{
struct str_list * sl = _alloc ( sizeof ( * sl ) ) ;
if ( ! sl ) {
stack ;
return 0 ;
}
if ( ! ( sl - > str = pool_strdup ( _cache . mem , path ) ) ) {
stack ;
return 0 ;
}
2001-10-31 15:47:01 +03:00
list_add ( & dev - > aliases , & sl - > list ) ;
2001-10-25 15:34:55 +04:00
return 1 ;
}
/*
* Either creates a new dev , or adds an alias to
* an existing dev .
*/
static int _insert_dev ( const char * path , dev_t d )
{
struct device * dev ;
/* is this device already registered ? */
2001-10-25 18:04:18 +04:00
if ( ! ( dev = ( struct device * ) btree_lookup ( _cache . devices , d ) ) ) {
2001-10-25 15:34:55 +04:00
/* create new device */
2001-10-25 18:04:18 +04:00
if ( ! ( dev = _create_dev ( d ) ) ) {
2001-10-25 15:34:55 +04:00
stack ;
return 0 ;
}
if ( ! ( btree_insert ( _cache . devices , d , dev ) ) ) {
log_err ( " Couldn't insert device into binary tree. " ) ;
_free ( dev ) ;
return 0 ;
}
}
2001-10-25 18:04:18 +04:00
if ( ! _add_alias ( dev , path ) ) {
log_err ( " Couldn't add alias to dir cache. " ) ;
2001-10-25 15:34:55 +04:00
return 0 ;
}
2001-10-25 18:19:39 +04:00
if ( ! hash_insert ( _cache . names , path , dev ) ) {
log_err ( " Couldn't add name to hash in dir cache. " ) ;
return 0 ;
}
2001-10-25 15:34:55 +04:00
return 1 ;
}
2001-10-08 17:58:52 +04:00
2001-10-03 16:41:29 +04:00
/*
2001-10-25 15:34:55 +04:00
* return a new path for the destination of the link .
2001-10-03 16:41:29 +04:00
*/
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 ) ;
}
2001-10-25 15:34:55 +04:00
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 ;
}
2001-10-03 16:41:29 +04:00
/*
* 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-25 15:34:55 +04:00
static int _insert_dir ( const char * dir )
2001-10-03 16:41:29 +04:00
{
2001-10-25 15:34:55 +04:00
int n , dirent_count , r = 1 ;
struct dirent * * dirent ;
char * path ;
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
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 ;
}
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
if ( ! ( path = _join ( dir , dirent [ n ] - > d_name ) ) ) {
stack ;
return 0 ;
}
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
_collapse_slashes ( path ) ;
r & = _insert ( path , 1 ) ;
dbg_free ( path ) ;
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
free ( dirent [ n ] ) ;
}
free ( dirent ) ;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
return r ;
2001-10-08 17:58:52 +04:00
}
2001-10-25 15:34:55 +04:00
static int _insert ( const char * path , int rec )
2001-10-08 17:58:52 +04:00
{
struct stat info ;
2001-10-25 15:34:55 +04:00
char * actual_path ;
int r = 0 ;
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 ;
}
2001-10-25 15:34:55 +04:00
/* follow symlinks if neccessary. */
2001-10-03 16:41:29 +04:00
if ( S_ISLNK ( info . st_mode ) ) {
2001-10-23 22:20:27 +04:00
log_debug ( " %s: Following symbolic link " , path ) ;
2001-10-25 15:34:55 +04:00
if ( ! ( actual_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
}
2001-10-25 15:34:55 +04:00
if ( stat ( actual_path , & info ) < 0 ) {
log_sys_very_verbose ( " stat " , actual_path ) ;
return 0 ;
}
2001-10-03 16:41:29 +04:00
2001-10-25 15:34:55 +04:00
dbg_free ( actual_path ) ;
2001-10-03 16:41:29 +04:00
}
2001-10-25 15:34:55 +04:00
if ( S_ISDIR ( info . st_mode ) ) { /* add a directory */
if ( rec )
r = _insert_dir ( path ) ;
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
} else { /* add a device */
if ( ! S_ISBLK ( info . st_mode ) ) {
2001-11-13 17:17:50 +03:00
log_debug ( " %s: Not a block device " , path ) ;
2001-10-25 15:34:55 +04:00
return 0 ;
}
2001-10-08 17:58:52 +04:00
2001-10-25 15:34:55 +04:00
if ( ! _insert_dev ( path , info . st_rdev ) ) {
stack ;
return 0 ;
2001-10-03 16:41:29 +04:00
}
2001-10-25 15:34:55 +04:00
r = 1 ;
2001-10-03 16:41:29 +04:00
}
2001-10-25 15:34:55 +04:00
return r ;
2001-10-03 16:41:29 +04:00
}
static void _full_scan ( void )
{
2001-10-31 15:47:01 +03:00
struct list * dh ;
2001-10-03 16:41:29 +04:00
if ( _cache . has_scanned )
return ;
2001-10-31 15:47:01 +03:00
list_iterate ( dh , & _cache . dirs ) {
struct dir_list * dl = list_item ( dh , struct dir_list ) ;
2001-10-25 15:34:55 +04:00
_insert_dir ( dl - > dir ) ;
2001-10-31 15:47:01 +03:00
} ;
2001-10-03 16:41:29 +04:00
_cache . has_scanned = 1 ;
}
int dev_cache_init ( void )
{
2001-10-25 15:34:55 +04:00
_cache . names = NULL ;
2001-10-03 16:41:29 +04:00
if ( ! ( _cache . mem = pool_create ( 10 * 1024 ) ) ) {
stack ;
return 0 ;
}
2001-10-25 15:34:55 +04:00
if ( ! ( _cache . names = hash_create ( 128 ) ) ) {
2001-10-03 16:41:29 +04:00
stack ;
pool_destroy ( _cache . mem ) ;
_cache . mem = 0 ;
return 0 ;
}
2001-10-25 15:34:55 +04:00
if ( ! ( _cache . devices = btree_create ( _cache . mem ) ) ) {
log_err ( " Couldn't create binary tree for dev-cache. " ) ;
goto bad ;
}
2001-10-31 15:47:01 +03:00
list_init ( & _cache . dirs ) ;
2001-10-08 16:11:33 +04:00
2001-10-03 16:41:29 +04:00
return 1 ;
2001-10-25 15:34:55 +04:00
bad :
dev_cache_exit ( ) ;
return 0 ;
2001-10-03 16:41:29 +04:00
}
2001-11-14 13:01:52 +03:00
static inline void _check_for_open_devices ( void )
{
# ifndef NDEBUG
struct dev_iter * i = dev_iter_create ( NULL ) ;
struct device * dev ;
if ( ! i ) {
stack ;
return ;
}
while ( ( dev = dev_iter_get ( i ) ) ) {
if ( dev - > fd > = 0 )
log_err ( " Device '%s' has been left open. " ,
dev_name ( dev ) ) ;
}
dev_iter_destroy ( i ) ;
# endif
}
2001-10-03 16:41:29 +04:00
void dev_cache_exit ( void )
{
2001-11-14 13:01:52 +03:00
_check_for_open_devices ( ) ;
2001-10-03 16:41:29 +04:00
pool_destroy ( _cache . mem ) ;
2001-10-25 15:34:55 +04:00
if ( _cache . names )
hash_destroy ( _cache . names ) ;
2001-10-03 16:41:29 +04:00
}
int dev_cache_add_dir ( const char * path )
{
struct dir_list * dl ;
2001-10-23 15:50:49 +04:00
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 ;
}
2001-10-03 16:41:29 +04:00
if ( ! ( dl = _alloc ( sizeof ( * dl ) + strlen ( path ) + 1 ) ) )
return 0 ;
strcpy ( dl - > dir , path ) ;
2001-10-31 15:47:01 +03:00
list_add ( & _cache . dirs , & dl - > list ) ;
2001-10-03 16:41:29 +04:00
return 1 ;
}
struct device * dev_cache_get ( const char * name , struct dev_filter * f )
{
2001-10-25 15:34:55 +04:00
struct device * d = ( struct device * ) hash_lookup ( _cache . names , name ) ;
2001-10-08 16:11:33 +04:00
2001-10-08 17:58:52 +04:00
if ( ! d ) {
_insert ( name , 0 ) ;
2001-10-25 15:34:55 +04:00
d = ( struct device * ) hash_lookup ( _cache . names , name ) ;
2001-10-08 17:58:52 +04:00
}
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 ( ) ;
2001-10-25 15:34:55 +04:00
di - > current = btree_first ( _cache . devices ) ;
2001-10-03 16:41:29 +04:00
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 )
{
2001-10-25 15:34:55 +04:00
struct device * d = btree_get_data ( iter - > current ) ;
iter - > current = btree_next ( iter - > current ) ;
2001-10-03 16:41:29 +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 | |
iter - > filter - > passes_filter ( iter - > filter , d ) )
return d ;
}
return NULL ;
}