2001-11-21 12:20:05 +03:00
/*
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
*
* This file is released under the LGPL .
*/
# include "format-text.h"
2002-01-07 12:16:20 +03:00
# include "import-export.h"
2001-11-21 12:20:05 +03:00
# include "log.h"
# include "pool.h"
# include "config.h"
# include "hash.h"
/*
2002-01-07 12:05:31 +03:00
* NOTE : Currently there can be only one vg per file .
2001-11-21 12:20:05 +03:00
*/
2002-01-07 12:05:31 +03:00
static void _not_written ( const char * cmd )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
log_err ( " The text format is lacking an implementation for '%s' " , cmd ) ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static struct list * _get_vgs ( struct format_instance * fi )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
return NULL ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static struct list * _get_pvs ( struct format_instance * fi )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
return NULL ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static struct physical_volume * _pv_read ( struct format_instance * fi ,
const char * pv_name )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
return NULL ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static int _pv_setup ( struct format_instance * fi , struct physical_volume * pv ,
struct volume_group * vg )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
2002-01-07 12:16:20 +03:00
return 0 ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static int _pv_write ( struct format_instance * fi , struct physical_volume * pv )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
2002-01-07 12:16:20 +03:00
return 0 ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static int _vg_setup ( struct format_instance * fi , struct volume_group * vg )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
2002-01-07 12:16:20 +03:00
return 0 ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static struct volume_group * _vg_read ( struct format_instance * fi ,
const char * vg_name )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
_not_written ( " _get_vgs " ) ;
return NULL ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static int _vg_write ( struct format_instance * fi , struct volume_group * vg )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
FILE * fp ;
char * file = ( char * ) fi - > private ;
2001-11-21 12:20:05 +03:00
2002-01-07 12:05:31 +03:00
/* FIXME: should be opened exclusively */
if ( ! ( fp = fopen ( file , " w " ) ) ) {
2002-01-09 16:07:03 +03:00
log_sys_error ( " fopen " , file ) ;
2002-01-07 12:05:31 +03:00
return 0 ;
}
2001-11-21 12:20:05 +03:00
2002-01-07 12:05:31 +03:00
if ( ! text_vg_export ( fp , vg ) ) {
2002-01-09 16:07:03 +03:00
log_error ( " Failed to write metadata to %s. " , file ) ;
fclose ( fp ) ;
return 0 ;
}
if ( ! fclose ( fp ) ) {
log_sys_error ( " fclose " , file ) ;
2002-01-07 12:05:31 +03:00
return 0 ;
}
2001-11-21 12:20:05 +03:00
2002-01-07 12:05:31 +03:00
return 1 ;
2001-11-21 12:20:05 +03:00
}
2002-01-07 14:12:11 +03:00
static void _destroy ( struct format_instance * fi )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:16:20 +03:00
pool_free ( fi - > cmd - > mem , fi ) ;
2001-11-21 12:20:05 +03:00
}
static struct format_handler _text_handler = {
get_vgs : _get_vgs ,
get_pvs : _get_pvs ,
pv_read : _pv_read ,
pv_setup : _pv_setup ,
pv_write : _pv_write ,
vg_setup : _vg_setup ,
vg_read : _vg_read ,
vg_write : _vg_write ,
destroy : _destroy
} ;
2002-01-07 12:05:31 +03:00
struct format_instance * text_format_create ( struct cmd_context * cmd ,
const char * file )
2001-11-21 12:20:05 +03:00
{
2002-01-07 12:05:31 +03:00
const char * no_alloc = " Couldn't allocate text format object. " ;
2001-11-21 12:20:05 +03:00
struct format_instance * fi ;
2002-01-07 12:16:20 +03:00
char * path ;
2001-11-21 12:20:05 +03:00
2002-01-07 12:05:31 +03:00
if ( ! ( fi = pool_alloc ( cmd - > mem , sizeof ( * fi ) ) ) ) {
log_err ( no_alloc ) ;
2001-11-21 12:20:05 +03:00
return NULL ;
}
2002-01-07 12:16:20 +03:00
if ( ! ( path = pool_strdup ( cmd - > mem , file ) ) ) {
pool_free ( fi - > cmd - > mem , fi ) ;
2002-01-07 12:05:31 +03:00
log_err ( no_alloc ) ;
return NULL ;
2001-11-21 12:20:05 +03:00
}
fi - > cmd = cmd ;
2002-01-07 12:16:20 +03:00
fi - > ops = & _text_handler ;
fi - > private = path ;
2001-11-21 12:20:05 +03:00
return fi ;
}