1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
12 Author: Michael Andres <ma@suse.de>
13 Maintainer: Michael Andres <ma@suse.de>
15 Purpose: Manage vendor attributes
24 #include "zypp/base/LogTools.h"
25 #include "zypp/base/IOStream.h"
26 #include "zypp/base/String.h"
28 #include "zypp/PathInfo.h"
29 #include "zypp/VendorAttr.h"
30 #include "zypp/ZYppFactory.h"
32 #include "zypp/ZConfig.h"
36 #undef ZYPP_BASE_LOGGER_LOGGROUP
37 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::VendorAttr"
39 ///////////////////////////////////////////////////////////////////
41 { /////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////
45 { /////////////////////////////////////////////////////////////////
47 typedef std::map<Vendor,bool> TrustMap;
50 typedef std::set<std::string> VendorList;
51 VendorList _trustedVendors;
53 bool addTrustedVendor( const std::string & str_r )
55 std::string line( str::trim( str_r ) );
56 if ( ! line.empty() && line[0] != '#')
58 _trustedVendors.insert( str::toLower( line ) );
63 bool trusted( const Vendor & vendor_r )
65 TrustMap::value_type val( vendor_r, false );
66 pair<TrustMap::iterator, bool> res = _trustMap.insert( val );
70 // check the new vendor in map
71 for ( VendorList::const_iterator it = _trustedVendors.begin();
72 it != _trustedVendors.end(); ++it )
74 if ( str::toLower( res.first->first.substr( 0, it->size() ) )
75 == str::toLower( *it ) )
78 res.first->second = true;
83 return res.first->second;
85 /////////////////////////////////////////////////////////////////
87 ///////////////////////////////////////////////////////////////////
89 const VendorAttr & VendorAttr::instance()
91 static VendorAttr _val;
95 VendorAttr::VendorAttr ()
97 const char * vendors[] = {
104 "ati technologies inc.",
107 _trustedVendors.insert( vendors, vendors+(sizeof(vendors)/sizeof(const char *)) );
109 Pathname vendorrcPath( "/etc/zypp/trustedVendors" );
112 Target_Ptr trg( getZYpp()->target() );
114 vendorrcPath = trg->root() / vendorrcPath;
118 // noop: Someone decided to let target() throw if the ptr is NULL ;(
121 PathInfo vendorrc( vendorrcPath );
122 if ( vendorrc.isFile() )
124 MIL << "Reading " << vendorrc << endl;
125 ifstream inp( vendorrc.asString().c_str() );
126 iostr::forEachLine( inp, addTrustedVendor );
128 MIL << "Trusted Vendors: " << _trustedVendors << endl;
131 void VendorAttr::enableAutoProtect()
133 MIL << "FIXME: Not implemented." << endl;
137 void VendorAttr::disableAutoProtect()
139 MIL << "FIXME: Not implemented." << endl;
143 bool VendorAttr::isKnown( const Vendor & vendor_r ) const
144 { return trusted( vendor_r ); }
147 bool VendorAttr::autoProtect( const Vendor & vendor_r ) const
148 { return( ZConfig::instance().autolock_untrustedvendor() && ! trusted( vendor_r ) ); }
150 /** Helper: Lowercase prefix */
151 inline bool hasLcPrefix( const std::string & str_r, const std::string & pref_r )
152 { return str::toLower( str_r.substr( 0, pref_r.size() ) ) == pref_r; }
154 /** Helper: SuSE and equivalent vendors */
155 inline bool isSUSE( const Vendor & vnd_r )
157 static const std::string defSUSE ( "suse" );
158 static const std::string defopenSUSE( "opensuse" );
160 return( hasLcPrefix( vnd_r, defSUSE )
161 || hasLcPrefix( vnd_r, defopenSUSE ) );
164 bool VendorAttr::equivalent( const Vendor & lhs, const Vendor & rhs ) const
169 // By now handcrafted equivalence definition:
170 return( isSUSE( lhs ) && isSUSE( rhs ) );
173 /////////////////////////////////////////////////////////////////
175 ///////////////////////////////////////////////////////////////////