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 virtual void changeDeviceName( const string& old, const string& nw )
00057 {};
00058 unsigned numVolumes() const;
00059 bool isEmpty() const;
00060 void getInfo( storage::ContainerInfo& info ) const;
00061 bool findVolume( const string& device, Volume*& vol );
00062
00063
00064 protected:
00065
00066 template< class Pred >
00067 struct ConstVolumePI { typedef ContainerIter<Pred, CVIter> type; };
00068 template< class Pred >
00069 struct VolumePI { typedef ContainerIter<Pred, VIter> type; };
00070 template< class Pred >
00071 struct VolumeI { typedef ContainerDerIter<Pred, typename VolumePI<Pred>::type, Volume> type; };
00072 typedef CheckFnc<const Volume> CheckFncVol;
00073 typedef CheckerIterator< CheckFncVol, ConstVolumePI<CheckFncVol>::type,
00074 CVIter, Volume> ConstVolPIterator;
00075 typedef CheckerIterator< CheckFncVol, VolumePI<CheckFncVol>::type,
00076 VIter, Volume> VolPIterator;
00077 typedef DerefIterator<VolPIterator,Volume> VolIterator;
00078 typedef IterPair<VolIterator> VolPair;
00079
00080 public:
00081
00082 template< class Pred >
00083 struct ConstVolumeI { typedef ContainerDerIter<Pred, typename ConstVolumePI<Pred>::type, const Volume> type; };
00084 template< class Pred >
00085 struct VolCondIPair { typedef MakeCondIterPair<Pred, typename ConstVolumeI<Pred>::type> type;};
00086 typedef DerefIterator<ConstVolPIterator,const Volume> ConstVolIterator;
00087 typedef IterPair<ConstVolIterator> ConstVolPair;
00088
00089
00090 ConstVolPair volPair( bool (* CheckFnc)( const Volume& )=NULL ) const
00091 {
00092 return( ConstVolPair( volBegin( CheckFnc ), volEnd( CheckFnc ) ));
00093 }
00094 ConstVolIterator volBegin( bool (* CheckFnc)( const Volume& )=NULL ) const
00095 {
00096 return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00097 }
00098 ConstVolIterator volEnd( bool (* CheckFnc)( const Volume& )=NULL ) const
00099 {
00100 return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00101 }
00102
00103 template< class Pred > typename VolCondIPair<Pred>::type volCondPair( const Pred& p ) const
00104 {
00105 return( VolCondIPair<Pred>::type( volCondBegin( p ), volCondEnd( p ) ) );
00106 }
00107 template< class Pred > typename ConstVolumeI<Pred>::type volCondBegin( const Pred& p ) const
00108 {
00109 return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p ) );
00110 }
00111 template< class Pred > typename ConstVolumeI<Pred>::type volCondEnd( const Pred& p ) const
00112 {
00113 return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p, true ) );
00114 }
00115
00116 protected:
00117
00118 VolPair volPair( bool (* CheckFnc)( const Volume& )=NULL )
00119 {
00120 return( VolPair( vBegin( CheckFnc ), vEnd( CheckFnc ) ));
00121 }
00122 VolIterator vBegin( bool (* CheckFnc)( const Volume& )=NULL )
00123 {
00124 return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00125 }
00126 VolIterator vEnd( bool (* CheckFnc)( const Volume& )=NULL )
00127 {
00128 return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00129 }
00130
00131 public:
00132 Container( Storage * const, const string& Name, storage::CType typ );
00133 Container( const Container& );
00134 Storage * const getStorage() const { return sto; }
00135 virtual ~Container();
00136 const string& name() const { return nm; }
00137 const string& device() const { return dev; }
00138 storage::CType type() const { return typ; }
00139 bool deleted() const { return del; }
00140 bool created() const { return create; }
00141 void setDeleted( bool val=true ) { del=val; }
00142 void setCreated( bool val=true ) { create=val; }
00143 void setSilent( bool val=true ) { silent=val; }
00144 void setUsedBy( storage::UsedByType t, const string& name ) { uby.set( t, name );}
00145 const storage::usedBy& getUsedBy() const { return( uby ); }
00146 storage::UsedByType getUsedByType() const { return( uby.type() ); }
00147 const string& usedByName() const { return( uby.name() ); }
00148 bool readonly() const { return ronly; }
00149 unsigned long long sizeK() const { return size_k; }
00150 unsigned long minorNr() const { return mnr; }
00151 unsigned long majorNr() const { return mjr; }
00152 virtual string removeText(bool doing=true) const;
00153 virtual string createText(bool doing=true) const;
00154 virtual int resizeVolume( Volume* v, unsigned long long newSize );
00155 virtual int removeVolume( Volume* v );
00156 static storage::CType const staticType() { return storage::CUNKNOWN; }
00157 friend std::ostream& operator<< (std::ostream& s, const Container &c );
00158 virtual Container* getCopy() const { return( new Container( *this ) ); }
00159 bool compareContainer( const Container* c, bool verbose ) const;
00160 void setExtError( const string& txt ) const;
00161 void setExtError( const SystemCmd& cmd, bool serr=true ) const;
00162
00163 protected:
00164 typedef CVIter ConstPlainIterator;
00165 ConstPlainIterator begin() const { return vols.begin(); }
00166 ConstPlainIterator end() const { return vols.end(); }
00167
00168 typedef VIter PlainIterator;
00169 PlainIterator begin() { return vols.begin(); }
00170 PlainIterator end() { return vols.end(); }
00171
00172 virtual void print( std::ostream& s ) const { s << *this; }
00173 void addToList( Volume* e )
00174 { pointerIntoSortedList<Volume>( vols, e ); }
00175 bool removeFromList( Volume* e );
00176 virtual int doCreate( Volume * v );
00177 virtual int doRemove( Volume * v );
00178 virtual int doResize( Volume * v );
00179 virtual void logData( const string& Dir ) {;}
00180 Container& operator=( const Container& );
00181 static bool stageDecrease( const Volume& v )
00182 { return( v.deleted()||v.needShrink()); }
00183 static bool stageCreate( const Volume& v )
00184 { return( v.created()||v.needExtend()); }
00185
00186 static string type_names[EVMS+1];
00187 static unsigned order[EVMS+1];
00188
00189 Storage * const sto;
00190 storage::CType typ;
00191 string nm;
00192 string dev;
00193 bool del;
00194 bool create;
00195 bool silent;
00196 bool ronly;
00197 storage::usedBy uby;
00198 unsigned long long size_k;
00199 unsigned long mnr;
00200 unsigned long mjr;
00201 VCont vols;
00202 mutable storage::ContainerInfo info;
00203 };
00204
00205 }
00206
00207 #endif