VendorAttr.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00009 /*
00010   File:       VendorAttr.cc
00011 
00012   Author:     Michael Andres <ma@suse.de>
00013   Maintainer: Michael Andres <ma@suse.de>
00014 
00015   Purpose: Manage vendor attributes
00016 
00017 /-*/
00018 
00019 #include <iostream>
00020 #include <fstream>
00021 #include <set>
00022 #include <map>
00023 
00024 #include "zypp/base/LogTools.h"
00025 #include "zypp/base/IOStream.h"
00026 #include "zypp/base/String.h"
00027 
00028 #include "zypp/PathInfo.h"
00029 #include "zypp/VendorAttr.h"
00030 #include "zypp/ZYppFactory.h"
00031 
00032 #include "zypp/ZConfig.h"
00033 
00034 using namespace std;
00035 
00036 #undef  ZYPP_BASE_LOGGER_LOGGROUP
00037 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::VendorAttr"
00038 
00040 namespace zypp
00041 { 
00042 
00044   namespace
00045   { 
00046 
00047     typedef std::map<Vendor,bool> TrustMap;
00048     TrustMap _trustMap;
00049 
00050     typedef std::set<std::string> VendorList;
00051     VendorList _trustedVendors;
00052 
00053     bool addTrustedVendor( const std::string & str_r )
00054     {
00055       std::string line( str::trim( str_r ) );
00056       if ( ! line.empty() && line[0] != '#')
00057         {
00058           _trustedVendors.insert( str::toLower( line ) );
00059         }
00060       return true;
00061     }
00062 
00063     bool trusted( const Vendor & vendor_r )
00064     {
00065       TrustMap::value_type val( vendor_r, false );
00066       pair<TrustMap::iterator, bool> res = _trustMap.insert( val );
00067 
00068       if ( res.second )
00069         {
00070           // check the new vendor in map
00071           for ( VendorList::const_iterator it = _trustedVendors.begin();
00072                 it != _trustedVendors.end(); ++it )
00073             {
00074               if ( str::toLower( res.first->first.substr( 0, it->size() ) )
00075                    == str::toLower( *it ) )
00076                 {
00077                   // match
00078                   res.first->second = true;
00079                   break;
00080                 }
00081             }
00082         }
00083       return res.first->second;
00084     }
00086   } // namespace
00088 
00089   const VendorAttr & VendorAttr::instance()
00090   {
00091     static VendorAttr _val;
00092     return _val;
00093   }
00094 
00095   VendorAttr::VendorAttr ()
00096   {
00097     const char * vendors[] = {
00098       "jpackage project",
00099       "novell",
00100       "opensuse",
00101       "sgi",
00102       "silicon graphics",
00103       "suse",
00104       "ati technologies inc.",
00105       "nvidia"
00106     };
00107     _trustedVendors.insert( vendors, vendors+(sizeof(vendors)/sizeof(const char *)) );
00108 
00109     Pathname vendorrcPath( "/etc/zypp/trustedVendors" );
00110     try
00111       {
00112         Target_Ptr trg( getZYpp()->target() );
00113         if ( trg )
00114           vendorrcPath = trg->root() / vendorrcPath;
00115       }
00116     catch ( ... )
00117       {
00118         // noop: Someone decided to let target() throw if the ptr is NULL ;(
00119       }
00120 
00121     PathInfo vendorrc( vendorrcPath );
00122     if ( vendorrc.isFile() )
00123       {
00124         MIL << "Reading " << vendorrc << endl;
00125         ifstream inp( vendorrc.asString().c_str() );
00126         iostr::forEachLine( inp, addTrustedVendor );
00127       }
00128     MIL << "Trusted Vendors: " << _trustedVendors << endl;
00129   }
00130 
00131   void VendorAttr::enableAutoProtect()
00132   {
00133     MIL << "FIXME: Not implemented." << endl;
00134     // FIXME use ZConfig
00135   }
00136 
00137   void VendorAttr::disableAutoProtect()
00138   {
00139     MIL << "FIXME: Not implemented." << endl;
00140     // FIXME use ZConfig
00141   }
00142 
00143   bool VendorAttr::isKnown( const Vendor & vendor_r ) const
00144   { return trusted( vendor_r ); }
00145 
00146 
00147   bool VendorAttr::autoProtect( const Vendor & vendor_r ) const
00148   { return( ZConfig::instance().autolock_untrustedvendor() && ! trusted( vendor_r ) ); }
00149 
00151   inline bool hasLcPrefix( const std::string & str_r, const std::string & pref_r )
00152   { return str::toLower( str_r.substr( 0, pref_r.size() ) ) == pref_r; }
00153 
00155   inline bool isSUSE( const Vendor & vnd_r )
00156   {
00157     static const std::string defSUSE    ( "suse" );
00158     static const std::string defopenSUSE( "opensuse" );
00159 
00160     return(    hasLcPrefix( vnd_r, defSUSE )
00161             || hasLcPrefix( vnd_r, defopenSUSE ) );
00162   }
00163 
00164   bool VendorAttr::equivalent( const Vendor & lhs, const Vendor & rhs ) const
00165   {
00166    if ( lhs == rhs )
00167       return true;
00168 
00169     // By now handcrafted equivalence definition:
00170     return( isSUSE( lhs ) && isSUSE( rhs ) );
00171   }
00172 
00174 } // namespace zypp
00176 

Generated on Tue Sep 25 19:23:11 2007 for libzypp by  doxygen 1.5.3