deblistparser.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: deblistparser.cc,v 1.29.2.5 2004/01/06 01:43:44 mdz Exp $
  4. /* ######################################################################
  5. Package Cache Generator - Generator for the cache structure.
  6. This builds the cache structure from the abstract package list parser.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/deblistparser.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/cachefilter.h>
  15. #include <apt-pkg/aptconfiguration.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/crc-16.h>
  18. #include <apt-pkg/md5.h>
  19. #include <apt-pkg/pkgcache.h>
  20. #include <apt-pkg/cacheiterators.h>
  21. #include <apt-pkg/tagfile.h>
  22. #include <apt-pkg/macros.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <algorithm>
  26. #include <string>
  27. #include <vector>
  28. #include <ctype.h>
  29. /*}}}*/
  30. using std::string;
  31. static debListParser::WordList PrioList[] = {
  32. {"required",pkgCache::State::Required},
  33. {"important",pkgCache::State::Important},
  34. {"standard",pkgCache::State::Standard},
  35. {"optional",pkgCache::State::Optional},
  36. {"extra",pkgCache::State::Extra},
  37. {NULL, 0}};
  38. // ListParser::debListParser - Constructor /*{{{*/
  39. // ---------------------------------------------------------------------
  40. /* Provide an architecture and only this one and "all" will be accepted
  41. in Step(), if no Architecture is given we will accept every arch
  42. we would accept in general with checkArchitecture() */
  43. debListParser::debListParser(FileFd *File) :
  44. pkgCacheListParser(), d(NULL), Tags(File)
  45. {
  46. }
  47. /*}}}*/
  48. // ListParser::Package - Return the package name /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* This is to return the name of the package this section describes */
  51. string debListParser::Package() {
  52. string const Result = Section.FindS("Package");
  53. if(unlikely(Result.empty() == true))
  54. _error->Error("Encountered a section with no Package: header");
  55. return Result;
  56. }
  57. /*}}}*/
  58. // ListParser::Architecture - Return the package arch /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* This will return the Architecture of the package this section describes */
  61. string debListParser::Architecture() {
  62. std::string const Arch = Section.FindS("Architecture");
  63. return Arch.empty() ? "none" : Arch;
  64. }
  65. /*}}}*/
  66. // ListParser::ArchitectureAll /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. bool debListParser::ArchitectureAll() {
  70. return Section.FindS("Architecture") == "all";
  71. }
  72. /*}}}*/
  73. // ListParser::Version - Return the version string /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* This is to return the string describing the version in debian form,
  76. epoch:upstream-release. If this returns the blank string then the
  77. entry is assumed to only describe package properties */
  78. string debListParser::Version()
  79. {
  80. return Section.FindS("Version");
  81. }
  82. /*}}}*/
  83. unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/
  84. {
  85. unsigned char MA;
  86. string const MultiArch = Section.FindS("Multi-Arch");
  87. if (MultiArch.empty() == true || MultiArch == "no")
  88. MA = pkgCache::Version::No;
  89. else if (MultiArch == "same") {
  90. if (ArchitectureAll() == true)
  91. {
  92. if (showErrors == true)
  93. _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
  94. Section.FindS("Package").c_str());
  95. MA = pkgCache::Version::No;
  96. }
  97. else
  98. MA = pkgCache::Version::Same;
  99. }
  100. else if (MultiArch == "foreign")
  101. MA = pkgCache::Version::Foreign;
  102. else if (MultiArch == "allowed")
  103. MA = pkgCache::Version::Allowed;
  104. else
  105. {
  106. if (showErrors == true)
  107. _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
  108. MultiArch.c_str(), Section.FindS("Package").c_str());
  109. MA = pkgCache::Version::No;
  110. }
  111. if (ArchitectureAll() == true)
  112. MA |= pkgCache::Version::All;
  113. return MA;
  114. }
  115. /*}}}*/
  116. // ListParser::NewVersion - Fill in the version structure /*{{{*/
  117. // ---------------------------------------------------------------------
  118. /* */
  119. bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
  120. {
  121. const char *Start;
  122. const char *Stop;
  123. // Parse the section
  124. if (Section.Find("Section",Start,Stop) == true)
  125. {
  126. map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start);
  127. Ver->Section = idx;
  128. }
  129. // Parse the source package name
  130. pkgCache::GrpIterator const G = Ver.ParentPkg().Group();
  131. Ver->SourcePkgName = G->Name;
  132. Ver->SourceVerStr = Ver->VerStr;
  133. if (Section.Find("Source",Start,Stop) == true)
  134. {
  135. const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start);
  136. pkgCache::VerIterator V;
  137. if (Space != NULL)
  138. {
  139. Stop = Space;
  140. const char * const Open = (const char * const) memchr(Space, '(', Stop - Space);
  141. if (likely(Open != NULL))
  142. {
  143. const char * const Close = (const char * const) memchr(Open, ')', Stop - Open);
  144. if (likely(Close != NULL))
  145. {
  146. std::string const version(Open + 1, (Close - Open) - 1);
  147. if (version != Ver.VerStr())
  148. {
  149. map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version);
  150. Ver->SourceVerStr = idx;
  151. }
  152. }
  153. }
  154. }
  155. std::string const pkgname(Start, Stop - Start);
  156. if (pkgname != G.Name())
  157. {
  158. for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
  159. {
  160. for (V = P.VersionList(); V.end() == false; ++V)
  161. {
  162. if (pkgname == V.SourcePkgName())
  163. {
  164. Ver->SourcePkgName = V->SourcePkgName;
  165. break;
  166. }
  167. }
  168. if (V.end() == false)
  169. break;
  170. }
  171. if (V.end() == true)
  172. {
  173. map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname);
  174. Ver->SourcePkgName = idx;
  175. }
  176. }
  177. }
  178. Ver->MultiArch = ParseMultiArch(true);
  179. // Archive Size
  180. Ver->Size = Section.FindULL("Size");
  181. // Unpacked Size (in K)
  182. Ver->InstalledSize = Section.FindULL("Installed-Size");
  183. Ver->InstalledSize *= 1024;
  184. // Priority
  185. if (Section.Find("Priority",Start,Stop) == true)
  186. {
  187. if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
  188. Ver->Priority = pkgCache::State::Extra;
  189. }
  190. if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
  191. return false;
  192. if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
  193. return false;
  194. if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
  195. return false;
  196. if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
  197. return false;
  198. if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
  199. return false;
  200. if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
  201. return false;
  202. if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
  203. return false;
  204. if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
  205. return false;
  206. // Obsolete.
  207. if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
  208. return false;
  209. if (ParseProvides(Ver) == false)
  210. return false;
  211. return true;
  212. }
  213. /*}}}*/
  214. // ListParser::Description - Return the description string /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* This is to return the string describing the package in debian
  217. form. If this returns the blank string then the entry is assumed to
  218. only describe package properties */
  219. string debListParser::Description(std::string const &lang)
  220. {
  221. if (lang.empty())
  222. return Section.FindS("Description");
  223. else
  224. return Section.FindS(string("Description-").append(lang).c_str());
  225. }
  226. /*}}}*/
  227. // ListParser::AvailableDescriptionLanguages /*{{{*/
  228. std::vector<std::string> debListParser::AvailableDescriptionLanguages()
  229. {
  230. std::vector<std::string> const understood = APT::Configuration::getLanguages();
  231. std::vector<std::string> avail;
  232. if (Section.Exists("Description") == true)
  233. avail.push_back("");
  234. for (std::vector<std::string>::const_iterator lang = understood.begin(); lang != understood.end(); ++lang)
  235. {
  236. std::string const tagname = "Description-" + *lang;
  237. if (Section.Exists(tagname.c_str()) == true)
  238. avail.push_back(*lang);
  239. }
  240. return avail;
  241. }
  242. /*}}}*/
  243. // ListParser::Description_md5 - Return the description_md5 MD5SumValue /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* This is to return the md5 string to allow the check if it is the right
  246. description. If no Description-md5 is found in the section it will be
  247. calculated.
  248. */
  249. MD5SumValue debListParser::Description_md5()
  250. {
  251. string const value = Section.FindS("Description-md5");
  252. if (value.empty() == true)
  253. {
  254. std::string const desc = Description("") + "\n";
  255. if (desc == "\n")
  256. return MD5SumValue();
  257. MD5Summation md5;
  258. md5.Add(desc.c_str());
  259. return md5.Result();
  260. }
  261. else if (likely(value.size() == 32))
  262. {
  263. if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos))
  264. return MD5SumValue(value);
  265. _error->Error("Malformed Description-md5 line; includes invalid character '%s'", value.c_str());
  266. return MD5SumValue();
  267. }
  268. _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%s'", (int)value.size(), value.c_str());
  269. return MD5SumValue();
  270. }
  271. /*}}}*/
  272. // ListParser::UsePackage - Update a package structure /*{{{*/
  273. // ---------------------------------------------------------------------
  274. /* This is called to update the package with any new information
  275. that might be found in the section */
  276. bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
  277. pkgCache::VerIterator &Ver)
  278. {
  279. string const static myArch = _config->Find("APT::Architecture");
  280. // Possible values are: "all", "native", "installed" and "none"
  281. // The "installed" mode is handled by ParseStatus(), See #544481 and friends.
  282. string const static essential = _config->Find("pkgCacheGen::Essential", "all");
  283. if (essential == "all" ||
  284. (essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()))
  285. if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  286. return false;
  287. if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
  288. return false;
  289. if (strcmp(Pkg.Name(),"apt") == 0)
  290. {
  291. if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) ||
  292. essential == "all")
  293. Pkg->Flags |= pkgCache::Flag::Essential | pkgCache::Flag::Important;
  294. else
  295. Pkg->Flags |= pkgCache::Flag::Important;
  296. }
  297. if (ParseStatus(Pkg,Ver) == false)
  298. return false;
  299. return true;
  300. }
  301. /*}}}*/
  302. // ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* */
  305. unsigned short debListParser::VersionHash()
  306. {
  307. const char *Sections[] ={"Installed-Size",
  308. "Depends",
  309. "Pre-Depends",
  310. // "Suggests",
  311. // "Recommends",
  312. "Conflicts",
  313. "Breaks",
  314. "Replaces",0};
  315. unsigned long Result = INIT_FCS;
  316. char S[1024];
  317. for (const char * const *I = Sections; *I != 0; ++I)
  318. {
  319. const char *Start;
  320. const char *End;
  321. if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S))
  322. continue;
  323. /* Strip out any spaces from the text, this undoes dpkgs reformatting
  324. of certain fields. dpkg also has the rather interesting notion of
  325. reformatting depends operators < -> <= */
  326. char *J = S;
  327. for (; Start != End; ++Start)
  328. {
  329. if (isspace(*Start) != 0)
  330. continue;
  331. *J++ = tolower_ascii(*Start);
  332. if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=')
  333. *J++ = '=';
  334. }
  335. Result = AddCRC16(Result,S,J - S);
  336. }
  337. return Result;
  338. }
  339. /*}}}*/
  340. // ListParser::ParseStatus - Parse the status field /*{{{*/
  341. // ---------------------------------------------------------------------
  342. /* Status lines are of the form,
  343. Status: want flag status
  344. want = unknown, install, hold, deinstall, purge
  345. flag = ok, reinstreq
  346. status = not-installed, config-files, half-installed, unpacked,
  347. half-configured, triggers-awaited, triggers-pending, installed
  348. */
  349. bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
  350. pkgCache::VerIterator &Ver)
  351. {
  352. const char *Start;
  353. const char *Stop;
  354. if (Section.Find("Status",Start,Stop) == false)
  355. return true;
  356. // UsePackage() is responsible for setting the flag in the default case
  357. bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
  358. if (essential == true &&
  359. Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  360. return false;
  361. // Isolate the first word
  362. const char *I = Start;
  363. for(; I < Stop && *I != ' '; I++);
  364. if (I >= Stop || *I != ' ')
  365. return _error->Error("Malformed Status line");
  366. // Process the want field
  367. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  368. {"install",pkgCache::State::Install},
  369. {"hold",pkgCache::State::Hold},
  370. {"deinstall",pkgCache::State::DeInstall},
  371. {"purge",pkgCache::State::Purge},
  372. {NULL, 0}};
  373. if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
  374. return _error->Error("Malformed 1st word in the Status line");
  375. // Isloate the next word
  376. I++;
  377. Start = I;
  378. for(; I < Stop && *I != ' '; I++);
  379. if (I >= Stop || *I != ' ')
  380. return _error->Error("Malformed status line, no 2nd word");
  381. // Process the flag field
  382. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  383. {"reinstreq",pkgCache::State::ReInstReq},
  384. {"hold",pkgCache::State::HoldInst},
  385. {"hold-reinstreq",pkgCache::State::HoldReInstReq},
  386. {NULL, 0}};
  387. if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
  388. return _error->Error("Malformed 2nd word in the Status line");
  389. // Isloate the last word
  390. I++;
  391. Start = I;
  392. for(; I < Stop && *I != ' '; I++);
  393. if (I != Stop)
  394. return _error->Error("Malformed Status line, no 3rd word");
  395. // Process the flag field
  396. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  397. {"config-files",pkgCache::State::ConfigFiles},
  398. {"half-installed",pkgCache::State::HalfInstalled},
  399. {"unpacked",pkgCache::State::UnPacked},
  400. {"half-configured",pkgCache::State::HalfConfigured},
  401. {"triggers-awaited",pkgCache::State::TriggersAwaited},
  402. {"triggers-pending",pkgCache::State::TriggersPending},
  403. {"installed",pkgCache::State::Installed},
  404. {NULL, 0}};
  405. if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
  406. return _error->Error("Malformed 3rd word in the Status line");
  407. /* A Status line marks the package as indicating the current
  408. version as well. Only if it is actually installed.. Otherwise
  409. the interesting dpkg handling of the status file creates bogus
  410. entries. */
  411. if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
  412. Pkg->CurrentState == pkgCache::State::ConfigFiles))
  413. {
  414. if (Ver.end() == true)
  415. _error->Warning("Encountered status field in a non-version description");
  416. else
  417. Pkg->CurrentVer = Ver.Index();
  418. }
  419. return true;
  420. }
  421. const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
  422. {
  423. // Determine the operator
  424. switch (*I)
  425. {
  426. case '<':
  427. I++;
  428. if (*I == '=')
  429. {
  430. I++;
  431. Op = pkgCache::Dep::LessEq;
  432. break;
  433. }
  434. if (*I == '<')
  435. {
  436. I++;
  437. Op = pkgCache::Dep::Less;
  438. break;
  439. }
  440. // < is the same as <= and << is really Cs < for some reason
  441. Op = pkgCache::Dep::LessEq;
  442. break;
  443. case '>':
  444. I++;
  445. if (*I == '=')
  446. {
  447. I++;
  448. Op = pkgCache::Dep::GreaterEq;
  449. break;
  450. }
  451. if (*I == '>')
  452. {
  453. I++;
  454. Op = pkgCache::Dep::Greater;
  455. break;
  456. }
  457. // > is the same as >= and >> is really Cs > for some reason
  458. Op = pkgCache::Dep::GreaterEq;
  459. break;
  460. case '=':
  461. Op = pkgCache::Dep::Equals;
  462. I++;
  463. break;
  464. // HACK around bad package definitions
  465. default:
  466. Op = pkgCache::Dep::Equals;
  467. break;
  468. }
  469. return I;
  470. }
  471. /*}}}*/
  472. // ListParser::ParseDepends - Parse a dependency element /*{{{*/
  473. // ---------------------------------------------------------------------
  474. /* This parses the dependency elements out of a standard string in place,
  475. bit by bit. */
  476. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  477. std::string &Package,std::string &Ver,unsigned int &Op)
  478. { return ParseDepends(Start, Stop, Package, Ver, Op, false, true, false); }
  479. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  480. std::string &Package,std::string &Ver,unsigned int &Op,
  481. bool const &ParseArchFlags)
  482. { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, true, false); }
  483. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  484. std::string &Package,std::string &Ver,unsigned int &Op,
  485. bool const &ParseArchFlags, bool const &StripMultiArch)
  486. { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, StripMultiArch, false); }
  487. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  488. string &Package,string &Ver,
  489. unsigned int &Op, bool const &ParseArchFlags,
  490. bool const &StripMultiArch,
  491. bool const &ParseRestrictionsList)
  492. {
  493. // Strip off leading space
  494. for (;Start != Stop && isspace(*Start) != 0; ++Start);
  495. // Parse off the package name
  496. const char *I = Start;
  497. for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
  498. *I != ',' && *I != '|' && *I != '[' && *I != ']' &&
  499. *I != '<' && *I != '>'; ++I);
  500. // Malformed, no '('
  501. if (I != Stop && *I == ')')
  502. return 0;
  503. if (I == Start)
  504. return 0;
  505. // Stash the package name
  506. Package.assign(Start,I - Start);
  507. // We don't want to confuse library users which can't handle MultiArch
  508. string const arch = _config->Find("APT::Architecture");
  509. if (StripMultiArch == true) {
  510. size_t const found = Package.rfind(':');
  511. if (found != string::npos &&
  512. (strcmp(Package.c_str() + found, ":any") == 0 ||
  513. strcmp(Package.c_str() + found, ":native") == 0 ||
  514. strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
  515. Package = Package.substr(0,found);
  516. }
  517. // Skip white space to the '('
  518. for (;I != Stop && isspace(*I) != 0 ; I++);
  519. // Parse a version
  520. if (I != Stop && *I == '(')
  521. {
  522. // Skip the '('
  523. for (I++; I != Stop && isspace(*I) != 0 ; I++);
  524. if (I + 3 >= Stop)
  525. return 0;
  526. I = ConvertRelation(I,Op);
  527. // Skip whitespace
  528. for (;I != Stop && isspace(*I) != 0; I++);
  529. Start = I;
  530. I = (const char*) memchr(I, ')', Stop - I);
  531. if (I == NULL || Start == I)
  532. return 0;
  533. // Skip trailing whitespace
  534. const char *End = I;
  535. for (; End > Start && isspace(End[-1]); End--);
  536. Ver.assign(Start,End-Start);
  537. I++;
  538. }
  539. else
  540. {
  541. Ver.clear();
  542. Op = pkgCache::Dep::NoOp;
  543. }
  544. // Skip whitespace
  545. for (;I != Stop && isspace(*I) != 0; I++);
  546. if (ParseArchFlags == true)
  547. {
  548. APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false);
  549. // Parse an architecture
  550. if (I != Stop && *I == '[')
  551. {
  552. ++I;
  553. // malformed
  554. if (unlikely(I == Stop))
  555. return 0;
  556. const char *End = I;
  557. bool Found = false;
  558. bool NegArch = false;
  559. while (I != Stop)
  560. {
  561. // look for whitespace or ending ']'
  562. for (;End != Stop && !isspace(*End) && *End != ']'; ++End);
  563. if (unlikely(End == Stop))
  564. return 0;
  565. if (*I == '!')
  566. {
  567. NegArch = true;
  568. ++I;
  569. }
  570. std::string arch(I, End);
  571. if (arch.empty() == false && matchesArch(arch.c_str()) == true)
  572. {
  573. Found = true;
  574. if (I[-1] != '!')
  575. NegArch = false;
  576. // we found a match, so fast-forward to the end of the wildcards
  577. for (; End != Stop && *End != ']'; ++End);
  578. }
  579. if (*End++ == ']') {
  580. I = End;
  581. break;
  582. }
  583. I = End;
  584. for (;I != Stop && isspace(*I) != 0; I++);
  585. }
  586. if (NegArch == true)
  587. Found = !Found;
  588. if (Found == false)
  589. Package = ""; /* not for this arch */
  590. }
  591. // Skip whitespace
  592. for (;I != Stop && isspace(*I) != 0; I++);
  593. }
  594. if (ParseRestrictionsList == true)
  595. {
  596. // Parse a restrictions formula which is in disjunctive normal form:
  597. // (foo AND bar) OR (blub AND bla)
  598. std::vector<string> const profiles = APT::Configuration::getBuildProfiles();
  599. // if the next character is a restriction list, then by default the
  600. // dependency does not apply and the conditions have to be checked
  601. // if the next character is not a restriction list, then by default the
  602. // dependency applies
  603. bool applies1 = (*I != '<');
  604. while (I != Stop)
  605. {
  606. if (*I != '<')
  607. break;
  608. ++I;
  609. // malformed
  610. if (unlikely(I == Stop))
  611. return 0;
  612. const char *End = I;
  613. // if of the prior restriction list is already fulfilled, then
  614. // we can just skip to the end of the current list
  615. if (applies1) {
  616. for (;End != Stop && *End != '>'; ++End);
  617. I = ++End;
  618. // skip whitespace
  619. for (;I != Stop && isspace(*I) != 0; I++);
  620. } else {
  621. bool applies2 = true;
  622. // all the conditions inside a restriction list have to be
  623. // met so once we find one that is not met, we can skip to
  624. // the end of this list
  625. while (I != Stop)
  626. {
  627. // look for whitespace or ending '>'
  628. // End now points to the character after the current term
  629. for (;End != Stop && !isspace(*End) && *End != '>'; ++End);
  630. if (unlikely(End == Stop))
  631. return 0;
  632. bool NegRestriction = false;
  633. if (*I == '!')
  634. {
  635. NegRestriction = true;
  636. ++I;
  637. }
  638. std::string restriction(I, End);
  639. if (restriction.empty() == false && profiles.empty() == false &&
  640. std::find(profiles.begin(), profiles.end(), restriction) != profiles.end())
  641. {
  642. if (NegRestriction) {
  643. applies2 = false;
  644. // since one of the terms does not apply we don't have to check the others
  645. for (; End != Stop && *End != '>'; ++End);
  646. }
  647. } else {
  648. if (!NegRestriction) {
  649. applies2 = false;
  650. // since one of the terms does not apply we don't have to check the others
  651. for (; End != Stop && *End != '>'; ++End);
  652. }
  653. }
  654. if (*End++ == '>') {
  655. I = End;
  656. // skip whitespace
  657. for (;I != Stop && isspace(*I) != 0; I++);
  658. break;
  659. }
  660. I = End;
  661. // skip whitespace
  662. for (;I != Stop && isspace(*I) != 0; I++);
  663. }
  664. if (applies2) {
  665. applies1 = true;
  666. }
  667. }
  668. }
  669. if (applies1 == false) {
  670. Package = ""; //not for this restriction
  671. }
  672. }
  673. if (I != Stop && *I == '|')
  674. Op |= pkgCache::Dep::Or;
  675. if (I == Stop || *I == ',' || *I == '|')
  676. {
  677. if (I != Stop)
  678. for (I++; I != Stop && isspace(*I) != 0; I++);
  679. return I;
  680. }
  681. return 0;
  682. }
  683. /*}}}*/
  684. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  685. // ---------------------------------------------------------------------
  686. /* This is the higher level depends parser. It takes a tag and generates
  687. a complete depends tree for the given version. */
  688. bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
  689. const char *Tag,unsigned int Type)
  690. {
  691. const char *Start;
  692. const char *Stop;
  693. if (Section.Find(Tag,Start,Stop) == false)
  694. return true;
  695. string const pkgArch = Ver.Arch();
  696. while (1)
  697. {
  698. string Package;
  699. string Version;
  700. unsigned int Op;
  701. Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
  702. if (Start == 0)
  703. return _error->Error("Problem parsing dependency %s",Tag);
  704. size_t const found = Package.rfind(':');
  705. if (found == string::npos || strcmp(Package.c_str() + found, ":any") == 0)
  706. {
  707. if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
  708. return false;
  709. }
  710. else
  711. {
  712. string Arch = Package.substr(found+1, string::npos);
  713. Package = Package.substr(0, found);
  714. // Such dependencies are not supposed to be accepted …
  715. // … but this is probably the best thing to do anyway
  716. if (Arch == "native")
  717. Arch = _config->Find("APT::Architecture");
  718. if (NewDepends(Ver,Package,Arch,Version,Op | pkgCache::Dep::ArchSpecific,Type) == false)
  719. return false;
  720. }
  721. if (Start == Stop)
  722. break;
  723. }
  724. return true;
  725. }
  726. /*}}}*/
  727. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  728. // ---------------------------------------------------------------------
  729. /* */
  730. bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
  731. {
  732. const char *Start;
  733. const char *Stop;
  734. if (Section.Find("Provides",Start,Stop) == true)
  735. {
  736. string Package;
  737. string Version;
  738. string const Arch = Ver.Arch();
  739. unsigned int Op;
  740. while (1)
  741. {
  742. Start = ParseDepends(Start,Stop,Package,Version,Op);
  743. const size_t archfound = Package.rfind(':');
  744. if (Start == 0)
  745. return _error->Error("Problem parsing Provides line");
  746. if (Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals) {
  747. _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.c_str());
  748. } else if (archfound != string::npos) {
  749. string OtherArch = Package.substr(archfound+1, string::npos);
  750. Package = Package.substr(0, archfound);
  751. if (NewProvides(Ver, Package, OtherArch, Version, pkgCache::Flag::ArchSpecific) == false)
  752. return false;
  753. } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
  754. if (NewProvidesAllArch(Ver, Package, Version, 0) == false)
  755. return false;
  756. } else {
  757. if (NewProvides(Ver, Package, Arch, Version, 0) == false)
  758. return false;
  759. }
  760. if (Start == Stop)
  761. break;
  762. }
  763. }
  764. if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  765. {
  766. string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
  767. return NewProvidesAllArch(Ver, Package, Ver.VerStr(), pkgCache::Flag::MultiArchImplicit);
  768. }
  769. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  770. return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit);
  771. return true;
  772. }
  773. /*}}}*/
  774. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  775. // ---------------------------------------------------------------------
  776. /* Looks for a word in a list of words - for ParseStatus */
  777. bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
  778. {
  779. for (unsigned int C = 0; List[C].Str != 0; C++)
  780. {
  781. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  782. {
  783. Out = List[C].Val;
  784. return true;
  785. }
  786. }
  787. return false;
  788. }
  789. /*}}}*/
  790. // ListParser::Step - Move to the next section in the file /*{{{*/
  791. // ---------------------------------------------------------------------
  792. /* This has to be careful to only process the correct architecture */
  793. bool debListParser::Step()
  794. {
  795. iOffset = Tags.Offset();
  796. return Tags.Step(Section);
  797. }
  798. /*}}}*/
  799. // ListParser::GetPrio - Convert the priority from a string /*{{{*/
  800. // ---------------------------------------------------------------------
  801. /* */
  802. unsigned char debListParser::GetPrio(string Str)
  803. {
  804. unsigned char Out;
  805. if (GrabWord(Str,PrioList,Out) == false)
  806. Out = pkgCache::State::Extra;
  807. return Out;
  808. }
  809. /*}}}*/
  810. bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/
  811. pkgCache::VerIterator const &Ver)
  812. {
  813. if (pkgCacheListParser::SameVersion(Hash, Ver) == false)
  814. return false;
  815. // status file has no (Download)Size, but all others are fair game
  816. // status file is parsed last, so the first version we encounter is
  817. // probably also the version we have downloaded
  818. unsigned long long const Size = Section.FindULL("Size");
  819. if (Size != 0 && Size != Ver->Size)
  820. return false;
  821. // available everywhere, but easier to check here than to include in VersionHash
  822. unsigned char MultiArch = ParseMultiArch(false);
  823. if (MultiArch != Ver->MultiArch)
  824. return false;
  825. // for all practical proposes (we can check): same version
  826. return true;
  827. }
  828. /*}}}*/
  829. debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile)
  830. : debListParser(File), DebFile(DebFile)
  831. {
  832. }
  833. bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg,
  834. pkgCache::VerIterator &Ver)
  835. {
  836. bool res = debListParser::UsePackage(Pkg, Ver);
  837. // we use the full file path as a provides so that the file is found
  838. // by its name
  839. if(NewProvides(Ver, DebFile, Pkg.Cache()->NativeArch(), Ver.VerStr(), 0) == false)
  840. return false;
  841. return res;
  842. }
  843. debListParser::~debListParser() {}