deblistparser.cc 33 KB

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