deblistparser.cc 32 KB

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