yast2-storage

Container.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) [2004-2010] Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
00020  */
00021 
00022 
00023 #ifndef CONTAINER_H
00024 #define CONTAINER_H
00025 
00026 #include <list>
00027 
00028 #include "storage/Volume.h"
00029 #include "storage/StorageTypes.h"
00030 #include "storage/StorageTmpl.h"
00031 
00032 
00033 namespace storage
00034 {
00035     using std::list;
00036 
00037 
00038     class Container : public Device
00039     {
00040     friend class Storage;
00041     protected:
00042         template<typename L1, typename L2> friend class ListListIterator;
00043 
00044         typedef std::list<Volume*> VCont;
00045         typedef VCont::iterator VIter;
00046         typedef VCont::const_iterator CVIter;
00047         typedef VCont::reverse_iterator RVIter;
00048         typedef VCont::const_reverse_iterator CRVIter;
00049 
00050 
00051     public:
00052         bool operator== ( const Container& rhs ) const
00053             { return( typ == rhs.typ && nm == rhs.nm && del == rhs.del ); }
00054         bool operator!= ( const Container& rhs ) const
00055             { return( !(*this==rhs) ); }
00056         bool operator< ( const Container& rhs ) const
00057             {
00058             if( typ != rhs.typ )
00059                 return( typ<rhs.typ );
00060             else if( nm != rhs.nm )
00061                 return( nm<rhs.nm );
00062             else
00063                 return( !del );
00064             }
00065         bool operator<= ( const Container& rhs ) const
00066             { return( *this<rhs || *this==rhs ); }
00067         bool operator>= ( const Container& rhs ) const
00068             { return( !(*this<rhs) ); }
00069         bool operator> ( const Container& rhs ) const
00070             { return( !(*this<=rhs) ); }
00071         virtual bool equalContent( const Container& rhs ) const;
00072 
00073         void logDifference(std::ostream& log, const Container& rhs) const;
00074         virtual void logDifferenceWithVolumes(std::ostream& log, const Container& rhs) const = 0;
00075 
00076         virtual void getCommitActions(list<commitAction>& l) const;
00077         virtual void getToCommit(storage::CommitStage stage, list<const Container*>& col,
00078                                  list<const Volume*>& vol) const;
00079 
00080         virtual int commitChanges( storage::CommitStage stage );
00081         virtual int commitChanges( storage::CommitStage stage, Volume* vol );
00082         virtual void changeDeviceName( const string& old, const string& nw ) {}
00083         bool isEmpty() const;
00084         void getInfo( storage::ContainerInfo& info ) const;
00085         bool findVolume( const string& device, Volume*& vol );
00086 
00087         static bool notDeleted(const Container& c) { return !c.deleted(); }
00088 
00089 // iterators over volumes of a container
00090     protected:
00091         // protected typedefs for iterators over volumes
00092         template< class Pred >
00093             struct ConstVolumePI { typedef ContainerIter<Pred, CVIter> type; };
00094         template< class Pred >
00095             struct VolumePI { typedef ContainerIter<Pred, VIter> type; };
00096         template< class Pred >
00097             struct VolumeI { typedef ContainerDerIter<Pred, typename VolumePI<Pred>::type, Volume> type; };
00098         typedef CheckFnc<const Volume> CheckFncVol;
00099         typedef CheckerIterator< CheckFncVol, ConstVolumePI<CheckFncVol>::type,
00100                                  CVIter, Volume> ConstVolPIterator;
00101         typedef CheckerIterator< CheckFncVol, VolumePI<CheckFncVol>::type,
00102                                  VIter, Volume> VolPIterator;
00103         typedef DerefIterator<VolPIterator,Volume> VolIterator;
00104         typedef IterPair<VolIterator> VolPair;
00105 
00106     public:
00107         // public typedefs for iterators over volumes
00108         template< class Pred >
00109             struct ConstVolumeI { typedef ContainerDerIter<Pred, typename ConstVolumePI<Pred>::type, const Volume> type; };
00110         template< class Pred >
00111             struct VolCondIPair { typedef MakeCondIterPair<Pred, typename ConstVolumeI<Pred>::type> type;};
00112         typedef DerefIterator<ConstVolPIterator,const Volume> ConstVolIterator;
00113         typedef IterPair<ConstVolIterator> ConstVolPair;
00114 
00115         // public member functions for iterators over volumes
00116         ConstVolPair volPair( bool (* CheckFnc)( const Volume& )=NULL ) const
00117             {
00118             return( ConstVolPair( volBegin( CheckFnc ), volEnd( CheckFnc ) ));
00119             }
00120         ConstVolIterator volBegin( bool (* CheckFnc)( const Volume& )=NULL ) const
00121             {
00122             return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00123             }
00124         ConstVolIterator volEnd( bool (* CheckFnc)( const Volume& )=NULL ) const
00125             {
00126             return( ConstVolIterator( ConstVolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00127             }
00128 
00129         template< class Pred > typename VolCondIPair<Pred>::type volCondPair( const Pred& p ) const
00130             {
00131             return( VolCondIPair<Pred>::type( volCondBegin( p ), volCondEnd( p ) ) );
00132             }
00133         template< class Pred > typename ConstVolumeI<Pred>::type volCondBegin( const Pred& p ) const
00134             {
00135             return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p ) );
00136             }
00137         template< class Pred > typename ConstVolumeI<Pred>::type volCondEnd( const Pred& p ) const
00138             {
00139             return( ConstVolumeI<Pred>::type( vols.begin(), vols.end(), p, true ) );
00140             }
00141 
00142     protected:
00143         // protected member functions for iterators over volumes
00144         VolPair volPair( bool (* CheckFnc)( const Volume& )=NULL )
00145             {
00146             return( VolPair( vBegin( CheckFnc ), vEnd( CheckFnc ) ));
00147             }
00148         VolIterator vBegin( bool (* CheckFnc)( const Volume& )=NULL )
00149             {
00150             return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc )) );
00151             }
00152         VolIterator vEnd( bool (* CheckFnc)( const Volume& )=NULL )
00153             {
00154             return( VolIterator( VolPIterator(vols.begin(), vols.end(), CheckFnc, true )) );
00155             }
00156 
00157     public:
00158 
00159         Container(Storage* s, const string& name, const string& device, CType typ);
00160         Container(Storage* s, const string& name, const string& device, CType typ,
00161                   SystemInfo& systemInfo);
00162         Container(Storage* s, CType typ, const xmlNode* node);
00163         Container(const Container& c);
00164         virtual ~Container();
00165 
00166         void saveData(xmlNode* node) const;
00167 
00168         Storage* getStorage() const { return sto; }
00169         const Storage* getStorageConst() const { return sto; }
00170         CType type() const { return typ; }
00171         bool isPartitionable() const;
00172         static bool Partitionable( const Container&d ) 
00173             { return( d.isPartitionable() ); }
00174         bool isDeviceUsable() const;
00175         static bool DeviceUsable( const Container&d ) 
00176             { return( d.isDeviceUsable() ); }
00177 
00178         bool readonly() const { return ronly; }
00179 
00180         virtual Text removeText(bool doing) const;
00181         virtual Text createText(bool doing) const;
00182         virtual int resizeVolume( Volume* v, unsigned long long newSize );
00183         virtual int removeVolume( Volume* v );
00184         static storage::CType staticType() { return storage::CUNKNOWN; }
00185         friend std::ostream& operator<< (std::ostream& s, const Container &c );
00186 
00187         virtual Container* getCopy() const = 0; // Container is always derived
00188 
00189         bool compareContainer(const Container& rhs, bool verbose) const;
00190         void setExtError( const string& txt ) const;
00191         void setExtError( const SystemCmd& cmd, bool serr=true ) const;
00192 
00193     protected:
00194         typedef CVIter ConstPlainIterator;
00195         ConstPlainIterator begin() const { return vols.begin(); }
00196         ConstPlainIterator end() const { return vols.end(); }
00197 
00198         typedef VIter PlainIterator;
00199         PlainIterator begin() { return vols.begin(); }
00200         PlainIterator end() { return vols.end(); }
00201 
00202         virtual void print( std::ostream& s ) const { s << *this; }
00203         void addToList(Volume* e);
00204         bool removeFromList( Volume* e );
00205         virtual int doCreate( Volume * v );
00206         virtual int doRemove( Volume * v );
00207         virtual int doResize( Volume * v );
00208 
00209         virtual void logData(const string& Dir) const {}
00210 
00211         static bool stageDecrease(const Volume& v);
00212         static bool stageIncrease(const Volume& v);
00213         static bool stageFormat(const Volume& v);
00214         static bool stageMount(const Volume& v);
00215 
00216         Storage* const sto;
00217         const CType typ;
00218         bool ronly;
00219 
00220         VCont vols;
00221 
00222         mutable storage::ContainerInfo info; // workaround for broken ycp bindings
00223 
00224     private:
00225 
00226         Container& operator=(const Container&); // disallow
00227 
00228     };
00229 
00230 }
00231 
00232 #endif