#include "Sources.hpp" #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace net::lliurex::apt; using namespace boost; string SourceLine::ToString() { stringstream ss; ss.str(""); ss<< ((source_type==SourceType::Deb) ? "deb" : "deb-src"); ss<<" "; switch(uri_type) { case UriType::Http: ss<<"http://"; break; case UriType::Https: ss<<"https://"; break; case UriType::Ftp: ss<<"ftp://"; break; case UriType::File: ss<<"file://"; break; case UriType::Cdrom: ss<<"cdrom:"; break; } ss<deb|deb\\-src)\\s+(?http://|https://|file://|ftp://|cdrom:)(?\\[.+\\]/|\\S+)\\s+(?\\S+)(?(\\s+\\S+)+)"); regex Sources::str_option("^#(?\\w+)(\\[(?\\S+)\\])?\\s*=\\s*(?.*)"); const string Sources::SourcesList = "/etc/apt/sources.list"; Sources::Sources() { } Sources::Sources(string filename) { ifstream file(filename); optional=false; name["C"]="noname"; while(!file.eof()) { string line; getline(file,line);//read a single line from the file match_results what; //have we got a match? if (regex_search(line,what,Sources::str_line)) { string type; string uri_type; string uri; string dist; string components; vector vcmp; type=what["deb_type"]; uri_type=what["uri_type"]; uri=what["uri"]; dist = what["dist"]; components=what["components"]; //split components string buf; stringstream ss(components.substr(1)); while(ss >> buf) vcmp.push_back(buf); //fill SourceLine object SourceLine sl; if(type=="deb")sl.source_type=SourceType::Deb; if(type=="deb-src")sl.source_type=SourceType::Source; if(uri_type=="http://")sl.uri_type=UriType::Http; if(uri_type=="https://")sl.uri_type=UriType::Https; if(uri_type=="file://")sl.uri_type=UriType::File; if(uri_type=="ftp://")sl.uri_type=UriType::Ftp; if(uri_type=="cdrom:")sl.uri_type=UriType::Cdrom; //remove all ending slashs while(uri[uri.length()-1]=='/') { uri=uri.substr(0,uri.length()-1); } sl.uri=uri; sl.dist=dist; sl.components = vcmp; //and Push it up Push(sl); } else { if (regex_search(line,what,Sources::str_option)) { string key=what["key"]; string value=what["value"]; string locale=what["locale"]; if(key=="optional") { optional=(value=="true"); } if(key=="name") { locale=(locale=="") ? "C" : locale; name[locale]=value; } } } } file.close(); } void Sources::Add(string line) { match_results what; if (regex_search(line,what,Sources::str_line)) { string type; string uri_type; string uri; string dist; string components; vector vcmp; type=what["deb_type"]; uri_type=what["uri_type"]; uri=what["uri"]; dist = what["dist"]; components=what["components"]; //split components string buf; stringstream ss(components.substr(1)); while(ss >> buf) vcmp.push_back(buf); //fill SourceLine object SourceLine sl; if(type=="deb")sl.source_type=SourceType::Deb; if(type=="deb-src")sl.source_type=SourceType::Source; if(uri_type=="http://")sl.uri_type=UriType::Http; if(uri_type=="https://")sl.uri_type=UriType::Https; if(uri_type=="file://")sl.uri_type=UriType::File; if(uri_type=="ftp://")sl.uri_type=UriType::Ftp; if(uri_type=="cdrom:")sl.uri_type=UriType::Cdrom; sl.uri=uri; sl.dist=dist; sl.components = vcmp; //and Push it up Push(sl); } else { throw runtime_error("Failed to parse source line"); } } void Sources::Push(SourceLine line) { bool found = false; for(int n = 0;n0) { ret=FindLineStatus::Found; } else { if(p>0) { ret=FindLineStatus::Partial; } else { ret=FindLineStatus::NotFound; } } return ret; } Sources net::lliurex::apt::operator+(Sources & A,Sources & B) { Sources R; R.lines = A.lines; for(SourceLine line : B.lines) { R.Push(line); } return R; } void Sources::Print() { for(int n=0;n