2002-12-20 02:25:55 +03:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2007 Red Hat , Inc . All rights reserved .
2002-12-20 02:25:55 +03:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2002-12-20 02:25:55 +03:00
*/
# include "lib.h"
# include "lvm-string.h"
2006-08-21 16:54:53 +04:00
# include <ctype.h>
2003-09-18 00:35:57 +04:00
int emit_to_buffer ( char * * buffer , size_t * size , const char * fmt , . . . )
{
int n ;
va_list ap ;
va_start ( ap , fmt ) ;
n = vsnprintf ( * buffer , * size , fmt , ap ) ;
va_end ( ap ) ;
2007-08-22 18:38:18 +04:00
if ( n < 0 | | ( ( size_t ) n = = * size ) )
2003-09-18 00:35:57 +04:00
return 0 ;
* buffer + = n ;
* size - = n ;
return 1 ;
}
2004-02-13 17:43:35 +03:00
2004-09-15 19:02:36 +04:00
/*
2007-04-25 22:24:19 +04:00
* Count occurences of ' c ' in ' str ' until we reach a null char .
*
* Returns :
2008-03-12 19:03:22 +03:00
* len - incremented for each char we encounter .
* count - number of occurrences of ' c ' and ' c2 ' .
2004-09-15 19:02:36 +04:00
*/
2008-03-12 19:03:22 +03:00
static void _count_chars ( const char * str , size_t * len , int * count ,
const int c1 , const int c2 )
2004-09-15 19:02:36 +04:00
{
const char * ptr ;
for ( ptr = str ; * ptr ; ptr + + , ( * len ) + + )
2008-03-12 19:03:22 +03:00
if ( * ptr = = c1 | | * ptr = = c2 )
2007-04-25 22:24:19 +04:00
( * count ) + + ;
}
/*
* Count occurences of ' c ' in ' str ' of length ' size ' .
*
* Returns :
2007-08-22 18:38:18 +04:00
* Number of occurrences of ' c '
2007-04-25 22:24:19 +04:00
*/
2008-03-12 19:03:22 +03:00
unsigned count_chars ( const char * str , size_t len , const int c )
2007-04-25 22:24:19 +04:00
{
2007-08-22 18:38:18 +04:00
size_t i ;
unsigned count = 0 ;
2007-04-25 22:24:19 +04:00
2007-08-22 18:38:18 +04:00
for ( i = 0 ; i < len ; i + + )
2007-04-25 22:24:19 +04:00
if ( str [ i ] = = c )
count + + ;
2007-08-22 18:38:18 +04:00
return count ;
2004-09-15 19:02:36 +04:00
}
/*
2008-03-12 19:03:22 +03:00
* Length of string after escaping double quotes and backslashes .
2004-09-15 19:02:36 +04:00
*/
2008-03-12 19:03:22 +03:00
size_t escaped_len ( const char * str )
{
size_t len = 1 ;
int count = 0 ;
_count_chars ( str , & len , & count , ' \" ' , ' \\ ' ) ;
return count + len ;
}
/*
* Copies a string , quoting orig_char with quote_char .
* Optionally also quote quote_char .
*/
static void _quote_characters ( char * * out , const char * src ,
const int orig_char , const int quote_char ,
int quote_quote_char )
2004-09-15 19:02:36 +04:00
{
while ( * src ) {
2008-03-12 19:03:22 +03:00
if ( * src = = orig_char | |
( * src = = quote_char & & quote_quote_char ) )
* ( * out ) + + = quote_char ;
2004-09-15 19:02:36 +04:00
* ( * out ) + + = * src + + ;
}
}
2008-03-12 19:03:22 +03:00
/*
* Unquote orig_char in string .
* Also unquote quote_char .
*/
static void _unquote_characters ( char * src , const int orig_char ,
const int quote_char )
{
char * out = src ;
while ( * src ) {
if ( * src = = quote_char & &
( * ( src + 1 ) = = orig_char | | * ( src + 1 ) = = quote_char ) )
src + + ;
* out + + = * src + + ;
}
* out = ' \0 ' ;
}
/*
* Copies a string , quoting hyphens with hyphens .
*/
static void _quote_hyphens ( char * * out , const char * src )
{
return _quote_characters ( out , src , ' - ' , ' - ' , 0 ) ;
}
2004-09-15 19:02:36 +04:00
/*
* < vg > - < lv > - < layer > or if ! layer just < vg > - < lv > .
*/
2005-11-09 01:52:26 +03:00
char * build_dm_name ( struct dm_pool * mem , const char * vgname ,
const char * lvname , const char * layer )
2004-09-15 19:02:36 +04:00
{
2004-10-11 19:59:23 +04:00
size_t len = 1 ;
int hyphens = 1 ;
2004-09-15 19:02:36 +04:00
char * r , * out ;
2008-03-12 19:03:22 +03:00
_count_chars ( vgname , & len , & hyphens , ' - ' , 0 ) ;
_count_chars ( lvname , & len , & hyphens , ' - ' , 0 ) ;
2004-09-15 19:02:36 +04:00
2004-10-11 19:59:23 +04:00
if ( layer & & * layer ) {
2008-03-12 19:03:22 +03:00
_count_chars ( layer , & len , & hyphens , ' - ' , 0 ) ;
2004-10-11 19:59:23 +04:00
hyphens + + ;
}
2004-09-15 19:02:36 +04:00
2004-10-11 19:59:23 +04:00
len + = hyphens ;
2004-09-15 19:02:36 +04:00
2005-10-17 03:03:59 +04:00
if ( ! ( r = dm_pool_alloc ( mem , len ) ) ) {
2005-11-09 01:52:26 +03:00
log_error ( " build_dm_name: Allocation failed for % " PRIsize_t
" for %s %s %s. " , len , vgname , lvname , layer ) ;
2004-09-15 19:02:36 +04:00
return NULL ;
}
out = r ;
2005-11-09 01:52:26 +03:00
_quote_hyphens ( & out , vgname ) ;
2004-09-15 19:02:36 +04:00
* out + + = ' - ' ;
2005-11-09 01:52:26 +03:00
_quote_hyphens ( & out , lvname ) ;
2004-09-15 19:02:36 +04:00
if ( layer & & * layer ) {
2006-04-28 21:01:07 +04:00
/* No hyphen if the layer begins with _ e.g. _mlog */
if ( * layer ! = ' _ ' )
* out + + = ' - ' ;
2004-09-15 19:02:36 +04:00
_quote_hyphens ( & out , layer ) ;
}
* out = ' \0 ' ;
return r ;
}
2008-03-12 19:03:22 +03:00
/*
* Copies a string , quoting double quotes with backslashes .
*/
char * escape_double_quotes ( char * out , const char * src )
{
char * buf = out ;
_quote_characters ( & buf , src , ' \" ' , ' \\ ' , 1 ) ;
* buf = ' \0 ' ;
return out ;
}
/*
* Undo quoting in situ .
*/
void unescape_double_quotes ( char * src )
{
_unquote_characters ( src , ' \" ' , ' \\ ' ) ;
}
2007-04-25 22:24:19 +04:00
/*
* Device layer names are all of the form < vg > - < lv > - < layer > , any
* other hyphens that appear in these names are quoted with yet
* another hyphen . The top layer of any device has no layer
* name . eg , vg0 - lvol0 .
*/
2006-04-19 19:33:07 +04:00
int validate_name ( const char * n )
{
register char c ;
register int len = 0 ;
if ( ! n | | ! * n )
return 0 ;
/* Hyphen used as VG-LV separator - ambiguity if LV starts with it */
if ( * n = = ' - ' )
return 0 ;
if ( ! strcmp ( n , " . " ) | | ! strcmp ( n , " .. " ) )
return 0 ;
while ( ( len + + , c = * n + + ) )
if ( ! isalnum ( c ) & & c ! = ' . ' & & c ! = ' _ ' & & c ! = ' - ' & & c ! = ' + ' )
return 0 ;
if ( len > NAME_LEN )
return 0 ;
return 1 ;
}