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