deblistparser.cc 28 KB

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