YUMFileListParser.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00013 #include <sys/types.h>
00014 #include <regex.h>
00015 
00016 #include <zypp/parser/yum/YUMFileListParser.h>
00017 #include <istream>
00018 #include <string>
00019 #include "zypp/parser/xml_parser_assert.h"
00020 #include <libxml/xmlstring.h>
00021 #include <libxml/xmlreader.h>
00022 #include <libxml/tree.h>
00023 #include <zypp/parser/LibXMLHelper.h>
00024 #include <zypp/parser/yum/schemanames.h>
00025 #include <iostream>
00026 #include <zypp/base/Logger.h>
00027 #include <zypp/ZYppFactory.h>
00028 #include <boost/regex.hpp>
00029 #include <boost/algorithm/string.hpp>
00030 
00031 using namespace std;
00032 namespace zypp {
00033   namespace parser {
00034     namespace yum {
00035 
00036 static boost::regex filenameRegex("^.*(/bin/|/sbin/|/lib/|/lib64/|/etc/|/usr/share/dict/words/|/usr/games/|/usr/share/magic\\.mime|/opt/gnome/games).*$"); 
00037         static regex_t filenameRegexT;
00038         static bool filenameRegexOk = false;
00039         static bool useboost;
00040 
00041       YUMFileListParser::YUMFileListParser(istream &is, const string& baseUrl)
00042       : XMLNodeIterator<YUMFileListData_Ptr>(is, baseUrl,FILELISTSCHEMA)
00043         , _zypp_architecture( getZYpp()->architecture() )
00044       {
00045         fetchNext();
00046       }
00047 
00048       YUMFileListParser::YUMFileListParser()
00049         : _zypp_architecture( getZYpp()->architecture() )
00050       { }
00051 
00052       YUMFileListParser::YUMFileListParser(YUMFileListData_Ptr& entry)
00053       : XMLNodeIterator<YUMFileListData_Ptr>(entry)
00054         , _zypp_architecture( getZYpp()->architecture() )
00055       { }
00056 
00057 
00058 
00059       YUMFileListParser::~YUMFileListParser()
00060       {
00061       }
00062 
00063 
00064 
00065 
00066       // select for which elements process() will be called
00067       bool
00068       YUMFileListParser::isInterested(const xmlNodePtr nodePtr)
00069       {
00070         bool result = (_helper.isElement(nodePtr)
00071                        && _helper.name(nodePtr) == "package");
00072         return result;
00073       }
00074 
00075 
00076       // do the actual processing
00077       YUMFileListData_Ptr
00078       YUMFileListParser::process(const xmlTextReaderPtr reader)
00079       {
00080         xml_assert(reader);
00081         YUMFileListData_Ptr dataPtr = new YUMFileListData;
00082         xmlNodePtr dataNode = xmlTextReaderExpand(reader);
00083         xml_assert(dataNode);
00084 
00085         dataPtr->pkgId = _helper.attribute(dataNode,"pkgid");
00086         dataPtr->name = _helper.attribute(dataNode,"name");
00087         dataPtr->arch = _helper.attribute(dataNode,"arch");
00088         try {
00089             if (!Arch(dataPtr->arch).compatibleWith( _zypp_architecture )) {
00090                 dataPtr = NULL;                 // skip <package>, incompatible architecture
00091                 return dataPtr;
00092             }
00093         }
00094         catch( const Exception & excpt_r ) {
00095             ZYPP_CAUGHT( excpt_r );
00096             DBG << "Skipping malformed " << dataPtr->arch << endl;
00097             dataPtr = NULL;
00098             return dataPtr;
00099         }
00100 
00101         for (xmlNodePtr child = dataNode->children;
00102             child != 0;
00103             child = child->next)
00104         {
00105             if (_helper.isElement(child)) {
00106                 string name = _helper.name(child);
00107                 if (name == "version") {
00108                     dataPtr->epoch = _helper.attribute(child,"epoch");
00109                     dataPtr->ver = _helper.attribute(child,"ver");
00110                     dataPtr->rel = _helper.attribute(child,"rel");
00111                 }
00112                 else if (name == "file") {
00113                     string filename = _helper.content( child );
00114                     if (!filenameRegexOk)
00115                     {
00116                         const char * filenameRegexPattern = "/(s?bin|lib(64)?|etc)/|^/usr/(games/|share/(dict/words|magic\\.mime)$)|^/opt/gnome/games/";
00117                         int r = regcomp (&filenameRegexT, filenameRegexPattern, REG_EXTENDED | REG_NOSUB);
00118                         //MIL << "regcomp " << r;
00119                         filenameRegexOk = true;
00120                     }
00121 
00122                     bool match;
00123                     match = !regexec (&filenameRegexT, filename.c_str(), 0 /*nmatch*/, NULL /*pmatch*/, 0 /*flags*/);
00124                     
00125                     if (match)
00126                         dataPtr->files.push_back( FileData( filename, _helper.attribute( child, "type" ) ) );
00127                 }
00128                 else {
00129                    WAR << "YUM <filelists> contains the unknown element <" << name << "> "
00130                      << _helper.positionInfo(child) << ", skipping" << endl;
00131                 }
00132             }
00133         }
00134         //std::cout << dataPtr->files.size() << std::endl;
00135         return dataPtr;
00136       }
00137 
00138 
00139     } // namespace yum
00140   } // namespace parser
00141 } // namespace zypp

Generated on Wed Sep 27 01:16:33 2006 for zypp by  doxygen 1.4.6