2009-10-16 21:41:49 +04:00
/*
* Copyright ( C ) 2004 - 2007 Red Hat , Inc . All rights reserved .
*
* 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
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2009-10-16 21:41:49 +04:00
*/
2018-06-08 15:40:53 +03:00
# include "base/memory/zalloc.h"
2018-05-14 12:30:20 +03:00
# include "lib/misc/lib.h"
# include "lib/commands/toolcontext.h"
# include "lib/metadata/segtype.h"
# include "lib/format_text/text_export.h"
# include "lib/config/config.h"
2009-10-16 21:41:49 +04:00
2024-10-24 17:12:18 +03:00
static int _unknown_text_import ( struct lv_segment * seg , const struct dm_config_node * sn )
2009-10-16 21:41:49 +04:00
{
2011-08-30 18:55:15 +04:00
struct dm_config_node * new , * last = NULL , * head = NULL ;
const struct dm_config_node * current ;
2009-10-16 21:41:49 +04:00
log_verbose ( " importing unknown segment " ) ;
for ( current = sn ; current ! = NULL ; current = current - > sib ) {
if ( ! strcmp ( current - > key , " type " ) | | ! strcmp ( current - > key , " start_extent " ) | |
! strcmp ( current - > key , " tags " ) | | ! strcmp ( current - > key , " extent_count " ) )
continue ;
2011-08-30 18:55:15 +04:00
new = dm_config_clone_node_with_mem ( seg - > lv - > vg - > vgmem , current , 0 ) ;
2009-10-16 21:41:49 +04:00
if ( ! new )
return_0 ;
if ( last )
last - > sib = new ;
if ( ! head )
head = new ;
last = new ;
}
seg - > segtype_private = head ;
return 1 ;
}
static int _unknown_text_export ( const struct lv_segment * seg , struct formatter * f )
{
2011-08-30 18:55:15 +04:00
struct dm_config_node * cn = seg - > segtype_private ;
2009-10-16 21:41:49 +04:00
return out_config_node ( f , cn ) ;
}
2010-12-20 16:32:49 +03:00
static void _unknown_destroy ( struct segment_type * segtype )
2009-10-16 21:41:49 +04:00
{
2018-06-08 15:40:53 +03:00
free ( ( void * ) segtype - > name ) ;
free ( segtype ) ;
2009-10-16 21:41:49 +04:00
}
2024-05-03 12:59:05 +03:00
static const struct segtype_handler _unknown_ops = {
2009-10-16 21:41:49 +04:00
. text_import = _unknown_text_import ,
. text_export = _unknown_text_export ,
. destroy = _unknown_destroy ,
} ;
struct segment_type * init_unknown_segtype ( struct cmd_context * cmd , const char * name )
{
2018-06-08 15:40:53 +03:00
struct segment_type * segtype = zalloc ( sizeof ( * segtype ) ) ;
2009-10-16 21:41:49 +04:00
2011-09-25 01:19:30 +04:00
if ( ! segtype ) {
log_error ( " Failed to allocate memory for unknown segtype " ) ;
return NULL ;
}
2009-10-16 21:41:49 +04:00
segtype - > ops = & _unknown_ops ;
2018-06-08 15:40:53 +03:00
if ( ! ( segtype - > name = strdup ( name ) ) ) {
2018-02-13 21:06:35 +03:00
log_error ( " Failed to allocate name. " ) ;
2018-06-08 15:40:53 +03:00
free ( segtype ) ;
2018-02-13 21:06:35 +03:00
return NULL ;
}
2009-10-16 21:41:49 +04:00
segtype - > flags = SEG_UNKNOWN | SEG_VIRTUAL | SEG_CANNOT_BE_ZEROED ;
log_very_verbose ( " Initialised segtype: %s " , segtype - > name ) ;
return segtype ;
}