00001 #ifndef STORAGE_TYPES_H
00002 #define STORAGE_TYPES_H
00003
00004 #include <iostream>
00005 #include <ext/stdio_filebuf.h>
00006
00007 #include "y2storage/Regex.h"
00008 #include "y2storage/AppUtil.h"
00009 #include "y2storage/StorageInterface.h"
00010
00011 namespace storage
00012 {
00013
00014 inline bool operator<( CType a, CType b )
00015 {
00016 static int order[COTYPE_LAST_ENTRY] = { 0, 1, 3, 7, 5, 4, 6, 2, 8 };
00017 bool ret = order[a] < order[b];
00018 y2mil( "a:" << a << " o(a):" << order[a] << " b:" << b << " o(b):" << order[b] << " ret:" << ret );
00019 return( ret );
00020 }
00021
00022 inline bool operator<=( CType a, CType b )
00023 {
00024 return( a==b || a<b );
00025 }
00026
00027 inline bool operator>=( CType a, CType b )
00028 {
00029 return( !(a<b) );
00030 }
00031
00032 inline bool operator>( CType a, CType b )
00033 {
00034 return( a!=b && !(a<b) );
00035 }
00036
00037 struct contOrder
00038 {
00039 contOrder(CType t) : order(0)
00040 {
00041 if( t==LOOP )
00042 order=1;
00043 }
00044 operator unsigned() const { return( order ); }
00045 protected:
00046 unsigned order;
00047 };
00048
00049 typedef enum { DECREASE, INCREASE, FORMAT, MOUNT } CommitStage;
00050
00051 class Volume;
00052 class Container;
00053
00054 struct commitAction
00055 {
00056 commitAction( CommitStage s, CType t, const string& d, const Volume* v,
00057 bool destr=false )
00058 { stage=s; type=t; descr=d; destructive=destr; container=false;
00059 u.vol=v; }
00060 commitAction( CommitStage s, CType t, const string& d, const Container* co,
00061 bool destr=false )
00062 { stage=s; type=t; descr=d; destructive=destr; container=true;
00063 u.co=co; }
00064 commitAction( CommitStage s, CType t, Volume* v )
00065 { stage=s; type=t; destructive=false; container=false; u.vol=v; }
00066 commitAction( CommitStage s, CType t, Container* c )
00067 { stage=s; type=t; destructive=false; container=true; u.co=c; }
00068 CommitStage stage;
00069 CType type;
00070 string descr;
00071 bool destructive;
00072 bool container;
00073 union
00074 {
00075 const Volume* vol;
00076 const Container* co;
00077 } u;
00078 const Container* co() const { return( container?u.co:NULL ); }
00079 const Volume* vol() const { return( container?NULL:u.vol ); }
00080 bool operator==( const commitAction& rhs ) const
00081 { return( stage==rhs.stage && type==rhs.type ); }
00082 bool operator<( const commitAction& rhs ) const
00083 {
00084 contOrder l(type);
00085 contOrder r(rhs.type);
00086
00087 if( stage==rhs.stage && stage==MOUNT )
00088 return( false );
00089 else if( unsigned(r)==unsigned(l) )
00090 {
00091 if( stage==rhs.stage )
00092 {
00093 if( stage==DECREASE )
00094 {
00095 if( type!=rhs.type )
00096 return( type>rhs.type );
00097 else
00098 return( container<rhs.container );
00099 }
00100 else
00101 {
00102 if( type!=rhs.type )
00103 return( type<rhs.type );
00104 else
00105 return( container>rhs.container );
00106 }
00107 }
00108 else
00109 return( stage<rhs.stage );
00110 }
00111 else
00112 return( unsigned(l)<unsigned(r) );
00113 }
00114 bool operator<=( const commitAction& rhs ) const
00115 { return( *this < rhs || *this == rhs ); }
00116 bool operator>=( const commitAction& rhs ) const
00117 { return( ! (*this < rhs) ); }
00118 bool operator>( const commitAction& rhs ) const
00119 { return( !(*this < rhs && *this == rhs) ); }
00120 };
00121
00122 struct usedBy
00123 {
00124 usedBy() : t(storage::UB_NONE) {;}
00125 usedBy( storage::UsedByType typ, const string& n ) : t(typ), nm(n) {;}
00126 void clear() { t=storage::UB_NONE; nm.erase(); }
00127 void set( storage::UsedByType type, const string& n )
00128 { t=type; (t==storage::UB_NONE)?nm.erase():nm=n; }
00129 bool operator==( const usedBy& rhs ) const
00130 { return( t==rhs.t && nm==rhs.nm ); }
00131 bool operator!=( const usedBy& rhs ) const
00132 { return( !(*this==rhs)); }
00133 inline operator string() const;
00134
00135 storage::UsedByType type() const { return( t ); }
00136 const string& name() const { return( nm ); }
00137 friend inline std::ostream& operator<< (std::ostream&, const usedBy& );
00138
00139 storage::UsedByType t;
00140 string nm;
00141 };
00142
00143 inline usedBy::operator string() const
00144 {
00145 string st;
00146 if( t!=storage::UB_NONE )
00147 {
00148 switch( t )
00149 {
00150 case storage::UB_LVM:
00151 st = "lvm";
00152 break;
00153 case storage::UB_MD:
00154 st = "md";
00155 break;
00156 case storage::UB_EVMS:
00157 st = "evms";
00158 break;
00159 case storage::UB_DM:
00160 st = "dm";
00161 break;
00162 case storage::UB_DMRAID:
00163 st = "dmraid";
00164 break;
00165 default:
00166 st = "UNKNOWN";
00167 break;
00168 }
00169 st += "[" + nm + "]";
00170 }
00171 return( st );
00172 }
00173
00174 inline std::ostream& operator<< (std::ostream& s, const usedBy& d )
00175 {
00176 if( d.t!=storage::UB_NONE )
00177 {
00178 s << " UsedBy:" << string(d);
00179 }
00180 return( s );
00181 }
00182
00183 struct match_string
00184 {
00185 match_string( const string& t ) : r(t) {};
00186 bool operator()(const string&s) { return( r.match( s )); }
00187 Regex r;
00188 };
00189
00190 struct find_begin
00191 {
00192 find_begin( const string& t ) : val(t) {};
00193 bool operator()(const string&s) { return( s.find(val)==0 ); }
00194 const string& val;
00195 };
00196
00197 struct find_any
00198 {
00199 find_any( const string& t ) : val(t) {};
00200 bool operator()(const string&s) { return( s.find(val)!=string::npos ); }
00201 const string& val;
00202 };
00203
00204 }
00205
00206 #endif