deblistparser.cc 32 KB

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