deblistparser.cc 29 KB

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