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 virtual bool equalContent( const Container& rhs ) const;
00047 virtual string getDiffString( const Container& c ) const;
00048 virtual void logDifference( const Container& c ) const;
00049
00050 virtual void getCommitActions( std::list<storage::commitAction*>& l ) const;
00051 virtual int getToCommit( storage::CommitStage stage,
00052 std::list<Container*>& col,
00053 std::list<Volume*>& vol );
00054
00055 virtual int commitChanges( storage::CommitStage stage );
00056 virtual int commitChanges( storage::CommitStage stage, Volume* vol );
00057 virtual void changeDeviceName( const string& old, const string& nw ) {}
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 * 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
00145 void clearUsedBy() { uby.clear(); }
00146 void setUsedBy(storage::UsedByType ub_type, const string& ub_name) { uby.set(ub_type, ub_name); }
00147 const storage::usedBy& getUsedBy() const { return uby; }
00148 storage::UsedByType getUsedByType() const { return uby.type(); }
00149
00150 bool readonly() const { return ronly; }
00151 unsigned long minorNr() const { return mnr; }
00152 unsigned long majorNr() const { return mjr; }
00153 unsigned long long sizeK() const { return size_k; }
00154 virtual string removeText(bool doing=true) const;
00155 virtual string createText(bool doing=true) const;
00156 virtual int resizeVolume( Volume* v, unsigned long long newSize );
00157 virtual int removeVolume( Volume* v );
00158 static storage::CType staticType() { return storage::CUNKNOWN; }
00159 friend std::ostream& operator<< (std::ostream& s, const Container &c );
00160 virtual Container* getCopy() const { return( new Container( *this ) ); }
00161 bool compareContainer( const Container* c, bool verbose ) const;
00162 void setExtError( const string& txt ) const;
00163 void setExtError( const SystemCmd& cmd, bool serr=true ) const;
00164
00165 protected:
00166 typedef CVIter ConstPlainIterator;
00167 ConstPlainIterator begin() const { return vols.begin(); }
00168 ConstPlainIterator end() const { return vols.end(); }
00169
00170 typedef VIter PlainIterator;
00171 PlainIterator begin() { return vols.begin(); }
00172 PlainIterator end() { return vols.end(); }
00173
00174 virtual void print( std::ostream& s ) const { s << *this; }
00175 void addToList( Volume* e )
00176 { pointerIntoSortedList<Volume>( vols, e ); }
00177 bool removeFromList( Volume* e );
00178 virtual int doCreate( Volume * v );
00179 virtual int doRemove( Volume * v );
00180 virtual int doResize( Volume * v );
00181 virtual void logData( const string& Dir ) {}
00182 Container& operator=( const Container& );
00183 static bool stageDecrease( const Volume& v )
00184 { return( v.deleted()||v.needShrink()); }
00185 static bool stageCreate( const Volume& v )
00186 { return( v.created()||v.needExtend()); }
00187
00188 static const string type_names[COTYPE_LAST_ENTRY];
00189
00190 Storage * const sto;
00191 storage::CType typ;
00192 string nm;
00193 string dev;
00194 bool del;
00195 bool create;
00196 bool silent;
00197 bool ronly;
00198 storage::usedBy uby;
00199 unsigned long long size_k;
00200 unsigned long mnr;
00201 unsigned long mjr;
00202 VCont vols;
00203 mutable storage::ContainerInfo info;
00204 };
00205
00206 }
00207
00208 #endif