deblistparser.cc 35 KB

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