deblistparser.cc 33 KB

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