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.
 
 
 
 
 
 

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