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