00001 #ifndef ETC_FSTAB_H
00002 #define ETC_FSTAB_H
00003
00004 #include <string>
00005 #include <list>
00006 #include <map>
00007
00008 #include "y2storage/StorageInterface.h"
00009
00010 namespace storage
00011 {
00012
00013 class AsciiFile;
00014 struct FstabChange;
00015
00016 struct FstabEntry
00017 {
00018 FstabEntry() { freq=passno=0;
00019 crypto=cryptt=loop=noauto=dmcrypt=tmpcrypt=false;
00020 encr=storage::ENC_NONE; mount_by=storage::MOUNTBY_DEVICE; }
00021 FstabEntry& operator=( const FstabChange& rhs );
00022 friend std::ostream& operator<< (std::ostream& s, const FstabEntry &v );
00023
00024 string device;
00025 string dentry;
00026 string mount;
00027 string fs;
00028 std::list<string> opts;
00029 int freq;
00030 int passno;
00031 bool loop;
00032 bool dmcrypt;
00033 bool noauto;
00034 bool crypto;
00035 bool cryptt;
00036 bool tmpcrypt;
00037 string loop_dev;
00038 string cr_opts;
00039 string cr_key;
00040 storage::EncryptType encr;
00041 storage::MountByType mount_by;
00042
00043 void calcDependent();
00044 };
00045
00046 inline std::ostream& operator<< (std::ostream& s, const FstabEntry &v )
00047 {
00048 s << "device:" << v.device
00049 << " dentry:" << v.dentry << " mount:" << v.mount
00050 << " fs:" << v.fs << " opts:" << mergeString( v.opts, "," )
00051 << " freq:" << v.freq << " passno:" << v.passno;
00052 if( v.noauto )
00053 s << " noauto";
00054 if( v.crypto )
00055 s << " crypto";
00056 if( v.cryptt )
00057 s << " cryptt";
00058 if( v.tmpcrypt )
00059 s << " tmpcrypt";
00060 if( v.loop )
00061 s << " loop";
00062 if( v.dmcrypt )
00063 s << " dmcrypt";
00064 if( !v.loop_dev.empty() )
00065 s << " loop_dev:" << v.loop_dev;
00066 if( !v.cr_key.empty() )
00067 s << " cr_key:" << v.cr_key;
00068 if( !v.cr_opts.empty() )
00069 s << " cr_opts:" << v.cr_opts;
00070 if( v.encr != storage::ENC_NONE )
00071 s << " encr:" << v.encr;
00072 return( s );
00073 }
00074
00075 struct FstabChange
00076 {
00077 FstabChange() { freq=passno=0; encr=storage::ENC_NONE; tmpcrypt=false; }
00078 FstabChange( const FstabEntry& e ) { *this = e; }
00079 FstabChange& operator=( const FstabEntry& rhs )
00080 {
00081 device = rhs.device;
00082 dentry = rhs.dentry; mount = rhs.mount; fs = rhs.fs;
00083 opts = rhs.opts; freq = rhs.freq; passno = rhs.passno;
00084 loop_dev = rhs.loop_dev; encr = rhs.encr;
00085 tmpcrypt = rhs.tmpcrypt;
00086 return( *this );
00087 }
00088 friend std::ostream& operator<< (std::ostream& s, const FstabChange &v );
00089 string device;
00090 string dentry;
00091 string mount;
00092 string fs;
00093 std::list<string> opts;
00094 int freq;
00095 int passno;
00096 string loop_dev;
00097 storage::EncryptType encr;
00098 bool tmpcrypt;
00099 };
00100
00101 inline FstabEntry& FstabEntry::operator=( const FstabChange& rhs )
00102 {
00103 device = rhs.device;
00104 dentry = rhs.dentry; mount = rhs.mount; fs = rhs.fs;
00105 opts = rhs.opts; freq = rhs.freq; passno = rhs.passno;
00106 loop_dev = rhs.loop_dev; encr = rhs.encr;
00107 tmpcrypt = rhs.tmpcrypt;
00108 calcDependent();
00109 return( *this );
00110 }
00111
00112 inline std::ostream& operator<< (std::ostream& s, const FstabChange &v )
00113 {
00114 s << "device:" << v.device
00115 << " dentry:" << v.dentry << " mount:" << v.mount
00116 << " fs:" << v.fs << " opts:" << mergeString( v.opts, "," )
00117 << " freq:" << v.freq << " passno:" << v.passno;
00118 if( !v.loop_dev.empty() )
00119 s << " loop_dev:" << v.loop_dev;
00120 if( v.encr != storage::ENC_NONE )
00121 s << " encr:" << v.encr;
00122 if( v.tmpcrypt )
00123 s << " tmpcrypt";
00124 return( s );
00125 }
00126
00127 class EtcFstab
00128 {
00129 public:
00130 EtcFstab( const string& prefix = "", bool rootMounted=true );
00131 bool findDevice( const string& dev, FstabEntry& entry ) const;
00132 bool findDevice( const std::list<string>& dl, FstabEntry& entry ) const;
00133 bool findMount( const string& mount, FstabEntry& entry ) const;
00134 bool findUuidLabel( const string& uuid, const string& label,
00135 FstabEntry& entry ) const;
00136 bool findIdPath( const std::list<string>& id, const string& path,
00137 FstabEntry& entry ) const;
00138 void setDevice( const FstabEntry& entry, const string& device );
00139 int updateEntry( const string& dev, const string& mount,
00140 const string& fs, const string& opts="defaults" );
00141 int updateEntry( const FstabChange& entry );
00142 int addEntry( const FstabChange& entry );
00143 int removeEntry( const FstabEntry& entry );
00144 int changeRootPrefix( const string& prfix );
00145 void getFileBasedLoops( const string& prefix, std::list<FstabEntry>& l );
00146 void getEntries( std::list<FstabEntry>& l );
00147 string addText( bool doing, bool crypto, const string& mp );
00148 string updateText( bool doing, bool crypto, const string& mp );
00149 string removeText( bool doing, bool crypto, const string& mp );
00150 int flush();
00151 int findPrefix( const AsciiFile& tab, const string& mount );
00152
00153 protected:
00154 struct Entry
00155 {
00156 enum operation { NONE, ADD, REMOVE, UPDATE };
00157 Entry() { op=NONE; }
00158 operation op;
00159 FstabEntry nnew;
00160 FstabEntry old;
00161 };
00162
00163 void readFiles();
00164 AsciiFile* findFile( const FstabEntry& e, AsciiFile*& fstab,
00165 AsciiFile*& cryptotab, int& lineno );
00166 bool findCrtab( const FstabEntry& e, const AsciiFile& crtab,
00167 int& lineno );
00168 bool findCrtab( const string& device, const AsciiFile& crtab,
00169 int& lineno );
00170 void makeStringList( const FstabEntry& e, std::list<string>& ls );
00171 void makeCrtabStringList( const FstabEntry& e, std::list<string>& ls );
00172 string updateLine( const std::list<string>& ol,
00173 const std::list<string>& nl, const string& line );
00174 string createLine( const std::list<string>& ls, unsigned fields,
00175 unsigned* flen );
00176 string createTabLine( const FstabEntry& e );
00177 void makeCrStringList( const FstabEntry& e, std::list<string>& ls );
00178 string createCrtabLine( const FstabEntry& e );
00179
00180 static unsigned fstabFields[6];
00181 static unsigned cryptotabFields[6];
00182 static unsigned crypttabFields[6];
00183
00184 string prefix;
00185 std::list<Entry> co;
00186 };
00187
00188 }
00189
00190 #endif