You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1042 lines
33 KiB

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