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.
 
 
 
 
 
 

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