00001 #ifndef CONTAINER_H
00002 #define CONTAINER_H
00003
00004 #include <list>
00005
00006 #include "y2storage/Volume.h"
00007 #include "y2storage/StorageTypes.h"
00008 #include "y2storage/StorageTmpl.h"
00009
00010 namespace storage
00011 {
00012
00013 class Container
00014 {
00015 friend class Storage;
00016 protected:
00017 template<typename L1, typename L2> friend class ListListIterator;
00018
00019 typedef std::list<Volume*> VCont;
00020 typedef VCont::iterator VIter;
00021 typedef VCont::const_iterator CVIter;
00022 typedef VCont::reverse_iterator RVIter;
00023 typedef VCont::const_reverse_iterator CRVIter;
00024
00025
00026 public:
00027 bool operator== ( const Container& rhs ) const
00028 { return( typ == rhs.typ && nm == rhs.nm && del == rhs.del ); }
00029 bool operator!= ( const Container& rhs ) const
00030 { return( !(*this==rhs) ); }
00031 bool operator< ( const Container& rhs ) const
00032 {
00033 if( typ != rhs.typ )
00034 return( typ<rhs.typ );
00035 else if( nm != rhs.nm )
00036 return( nm<rhs.nm );
00037 else
00038 return( !del );
00039 }
00040 bool operator<= ( const Container& rhs ) const
00041 { return( *this<rhs || *this==rhs ); }
00042 bool operator>= ( const Container& rhs ) const
00043 { return( !(*this<rhs) ); }
00044 bool operator> ( const Container& rhs ) const
00045 { return( !(*this<=rhs) ); }
00046 bool equalContent( const Container& rhs ) const;
00047 string logDifference( const Container& c ) const;
00048
00049 virtual void getCommitActions( std::list<storage::commitAction*>& l ) const;
00050 virtual int getToCommit( storage::CommitStage stage,
00051 std::list<Container*>& col,
00052 std::list<Volume*>& vol );
00053
00054 virtual int commitChanges( storage::CommitStage stage );
00055 virtual int commitChanges( storage::CommitStage stage, Volume* vol );
00056 unsigned numVolumes() const;
00057 bool isEmpty() const;
00058 void getInfo( storage::ContainerInfo& info ) const;
00059 bool findVolume( const string& device, Volume*& vol );
00060
00061
00062 protected:
00063
00064 template< class Pred >
00065 struct ConstVolumePI { typedef ContainerIter<Pred, CVIter> type; };
00066 template< class Pred >
00067 struct VolumePI { typedef ContainerIter<Pred, VIter> type; };
00068 template< class Pred >
00069 struct VolumeI { typedef ContainerDerIter<Pred, typename VolumePI<Pred>::type, Volume> type; };
00070 typedef CheckFnc<const Volume> CheckFncVol;
00071 typedef CheckerIterator< CheckFncVol, ConstVolumePI<CheckFncVol>::type,
00072 CVIter, Volume> ConstVolPIterator;
00073 typedef CheckerIterator< CheckFncVol, VolumePI<CheckFncVol>::type,
00074 VIter, Volume> VolPIterator;
00075 typedef DerefIterator<VolPIterator,Volume> VolIterator;
00076 typedef IterPair<VolIterator> VolPair;
00077
00078 public:
00079
00080 template< class Pred >
00081 struct ConstVolumeI { typedef ContainerDerIter<Pred, typename ConstVolumePI<Pred>::type, const Volume> type; };
00082 template< class Pred >
00083 struct VolCondIPair { typedef MakeCondIterPair<Pred, typename ConstVolumeI<Pred>::type> type;};
00084 typedef DerefIterator<ConstVolPIterator,const Volume> ConstVolIterator;
00085 typedef IterPair<ConstVolIterator> ConstVolPair;
00086
00087
00088 ConstVolPair volPair( bool (* CheckFnc)( const Volume& )=NULL ) const
00089 {
00090 return( ConstVolPair( volBegin( CheckFnc ), volEnd( CheckFnc ) ));
00091 }
00092 ConstVolIterator volBegin( bool (* CheckFnc)( const Volume& )=NULL ) const
00093 {
00094 return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00095 }
00096 ConstVolIterator volEnd( bool (* CheckFnc)( const Volume& )=NULL ) const
00097 {
00098 return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00099 }
00100
00101 template< class Pred > typename VolCondIPair<Pred>::type volCondPair( const Pred& p ) const
00102 {
00103 return( VolCondIPair<Pred>::type( volCondBegin( p ), volCondEnd( p ) ) );
00104 }
00105 template< class Pred > typename ConstVolumeI<Pred>::type volCondBegin( const Pred& p ) const
00106 {
00107 return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p ) );
00108 }
00109 template< class Pred > typename ConstVolumeI<Pred>::type volCondEnd( const Pred& p ) const
00110 {
00111 return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p, true ) );
00112 }
00113
00114 protected:
00115
00116 VolPair volPair( bool (* CheckFnc)( const Volume& )=NULL )
00117 {
00118 return( VolPair( vBegin( CheckFnc ), vEnd( CheckFnc ) ));
00119 }
00120 VolIterator vBegin( bool (* CheckFnc)( const Volume& )=NULL )
00121 {
00122 return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00123 }
00124 VolIterator vEnd( bool (* CheckFnc)( const Volume& )=NULL )
00125 {
00126 return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00127 }
00128
00129 public:
00130 Container( Storage * const, const string& Name, storage::CType typ );
00131 Container( const Container& );
00132 Storage * const getStorage() const { return sto; }
00133 virtual ~Container();
00134 const string& name() const { return nm; }
00135 const string& device() const { return dev; }
00136 storage::CType type() const { return typ; }
00137 bool deleted() const { return del; }
00138 bool created() const { return create; }
00139 void setDeleted( bool val=true ) { del=val; }
00140 void setCreated( bool val=true ) { create=val; }
00141 void setSilent( bool val=true ) { silent=val; }
00142 void setUsedBy( storage::UsedByType t, const string& name ) { uby.set( t, name );}
00143 const storage::usedBy& getUsedBy() const { return( uby ); }
00144 storage::UsedByType getUsedByType() const { return( uby.type() ); }
00145 const string& usedByName() const { return( uby.name() ); }
00146 bool readonly() const { return ronly; }
00147 virtual string removeText(bool doing=true) const;
00148 virtual string createText(bool doing=true) const;
00149 virtual int resizeVolume( Volume* v, unsigned long long newSize );
00150 virtual int removeVolume( Volume* v );
00151 static storage::CType const staticType() { return storage::CUNKNOWN; }
00152 friend std::ostream& operator<< (std::ostream& s, const Container &c );
00153 virtual Container* getCopy() const { return( new Container( *this ) ); }
00154 bool compareContainer( const Container* c, bool verbose ) const;
00155 void setExtError( const string& txt ) const;
00156 void setExtError( const SystemCmd& cmd, bool serr=true ) const;
00157
00158 protected:
00159 typedef CVIter ConstPlainIterator;
00160 ConstPlainIterator begin() const { return vols.begin(); }
00161 ConstPlainIterator end() const { return vols.end(); }
00162
00163 typedef VIter PlainIterator;
00164 PlainIterator begin() { return vols.begin(); }
00165 PlainIterator end() { return vols.end(); }
00166
00167 virtual void print( std::ostream& s ) const { s << *this; }
00168 void addToList( Volume* e )
00169 { pointerIntoSortedList<Volume>( vols, e ); }
00170 bool removeFromList( Volume* e );
00171 virtual int doCreate( Volume * v );
00172 virtual int doRemove( Volume * v );
00173 virtual int doResize( Volume * v );
00174 virtual void logData( const string& Dir ) {;}
00175 Container& operator=( const Container& );
00176 static bool stageDecrease( const Volume& v )
00177 { return( v.deleted()||v.needShrink()); }
00178 static bool stageCreate( const Volume& v )
00179 { return( v.created()||v.needExtend()); }
00180
00181 static string type_names[EVMS+1];
00182 static unsigned order[EVMS+1];
00183
00184 Storage * const sto;
00185 storage::CType typ;
00186 string nm;
00187 string dev;
00188 bool del;
00189 bool create;
00190 bool silent;
00191 bool ronly;
00192 storage::usedBy uby;
00193 VCont vols;
00194 mutable storage::ContainerInfo info;
00195 };
00196
00197 }
00198
00199 #endif