deblistparser.cc 33 KB

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