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.
 
 
 
 
 
 

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