deblistparser.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 || End - Start >= (signed)sizeof(S))
  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; ++Start)
  329. {
  330. if (isspace_ascii(*Start) != 0)
  331. continue;
  332. *J++ = tolower_ascii(*Start);
  333. if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=')
  334. *J++ = '=';
  335. }
  336. Result = AddCRC16(Result,S,J - S);
  337. }
  338. return Result;
  339. }
  340. /*}}}*/
  341. // StatusListParser::ParseStatus - Parse the status field /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* Status lines are of the form,
  344. Status: want flag status
  345. want = unknown, install, hold, deinstall, purge
  346. flag = ok, reinstreq
  347. status = not-installed, config-files, half-installed, unpacked,
  348. half-configured, triggers-awaited, triggers-pending, installed
  349. */
  350. bool debListParser::ParseStatus(pkgCache::PkgIterator &,
  351. pkgCache::VerIterator &)
  352. {
  353. return true;
  354. }
  355. bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
  356. pkgCache::VerIterator &Ver)
  357. {
  358. const char *Start;
  359. const char *Stop;
  360. if (Section.Find("Status",Start,Stop) == false)
  361. return true;
  362. // UsePackage() is responsible for setting the flag in the default case
  363. bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
  364. if (essential == true &&
  365. Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  366. return false;
  367. // Isolate the first word
  368. const char *I = Start;
  369. for(; I < Stop && *I != ' '; I++);
  370. if (I >= Stop || *I != ' ')
  371. return _error->Error("Malformed Status line");
  372. // Process the want field
  373. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  374. {"install",pkgCache::State::Install},
  375. {"hold",pkgCache::State::Hold},
  376. {"deinstall",pkgCache::State::DeInstall},
  377. {"purge",pkgCache::State::Purge},
  378. {"", 0}};
  379. if (GrabWord(StringView(Start,I-Start),WantList,Pkg->SelectedState) == false)
  380. return _error->Error("Malformed 1st word in the Status line");
  381. // Isloate the next word
  382. I++;
  383. Start = I;
  384. for(; I < Stop && *I != ' '; I++);
  385. if (I >= Stop || *I != ' ')
  386. return _error->Error("Malformed status line, no 2nd word");
  387. // Process the flag field
  388. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  389. {"reinstreq",pkgCache::State::ReInstReq},
  390. {"hold",pkgCache::State::HoldInst},
  391. {"hold-reinstreq",pkgCache::State::HoldReInstReq},
  392. {"", 0}};
  393. if (GrabWord(StringView(Start,I-Start),FlagList,Pkg->InstState) == false)
  394. return _error->Error("Malformed 2nd word in the Status line");
  395. // Isloate the last word
  396. I++;
  397. Start = I;
  398. for(; I < Stop && *I != ' '; I++);
  399. if (I != Stop)
  400. return _error->Error("Malformed Status line, no 3rd word");
  401. // Process the flag field
  402. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  403. {"config-files",pkgCache::State::ConfigFiles},
  404. {"half-installed",pkgCache::State::HalfInstalled},
  405. {"unpacked",pkgCache::State::UnPacked},
  406. {"half-configured",pkgCache::State::HalfConfigured},
  407. {"triggers-awaited",pkgCache::State::TriggersAwaited},
  408. {"triggers-pending",pkgCache::State::TriggersPending},
  409. {"installed",pkgCache::State::Installed},
  410. {"", 0}};
  411. if (GrabWord(StringView(Start,I-Start),StatusList,Pkg->CurrentState) == false)
  412. return _error->Error("Malformed 3rd word in the Status line");
  413. /* A Status line marks the package as indicating the current
  414. version as well. Only if it is actually installed.. Otherwise
  415. the interesting dpkg handling of the status file creates bogus
  416. entries. */
  417. if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
  418. Pkg->CurrentState == pkgCache::State::ConfigFiles))
  419. {
  420. if (Ver.end() == true)
  421. _error->Warning("Encountered status field in a non-version description");
  422. else
  423. Pkg->CurrentVer = Ver.Index();
  424. }
  425. return true;
  426. }
  427. const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
  428. {
  429. // Determine the operator
  430. switch (*I)
  431. {
  432. case '<':
  433. I++;
  434. if (*I == '=')
  435. {
  436. I++;
  437. Op = pkgCache::Dep::LessEq;
  438. break;
  439. }
  440. if (*I == '<')
  441. {
  442. I++;
  443. Op = pkgCache::Dep::Less;
  444. break;
  445. }
  446. // < is the same as <= and << is really Cs < for some reason
  447. Op = pkgCache::Dep::LessEq;
  448. break;
  449. case '>':
  450. I++;
  451. if (*I == '=')
  452. {
  453. I++;
  454. Op = pkgCache::Dep::GreaterEq;
  455. break;
  456. }
  457. if (*I == '>')
  458. {
  459. I++;
  460. Op = pkgCache::Dep::Greater;
  461. break;
  462. }
  463. // > is the same as >= and >> is really Cs > for some reason
  464. Op = pkgCache::Dep::GreaterEq;
  465. break;
  466. case '=':
  467. Op = pkgCache::Dep::Equals;
  468. I++;
  469. break;
  470. // HACK around bad package definitions
  471. default:
  472. Op = pkgCache::Dep::Equals;
  473. break;
  474. }
  475. return I;
  476. }
  477. /*}}}*/
  478. // ListParser::ParseDepends - Parse a dependency element /*{{{*/
  479. // ---------------------------------------------------------------------
  480. /* This parses the dependency elements out of a standard string in place,
  481. bit by bit. */
  482. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  483. std::string &Package,std::string &Ver,unsigned int &Op)
  484. { return ParseDepends(Start, Stop, Package, Ver, Op, false, true, false); }
  485. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  486. std::string &Package,std::string &Ver,unsigned int &Op,
  487. bool const &ParseArchFlags)
  488. { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, 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, bool const &StripMultiArch)
  492. { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, StripMultiArch, false); }
  493. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  494. string &Package,string &Ver,
  495. unsigned int &Op, bool const &ParseArchFlags,
  496. bool const &StripMultiArch,
  497. bool const &ParseRestrictionsList)
  498. {
  499. StringView PackageView;
  500. StringView VerView;
  501. auto res = ParseDepends(Start, Stop, PackageView, VerView, Op, (bool)ParseArchFlags,
  502. (bool) StripMultiArch, (bool) ParseRestrictionsList);
  503. Package = PackageView.to_string();
  504. Ver = VerView.to_string();
  505. return res;
  506. }
  507. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  508. StringView &Package,StringView &Ver,
  509. unsigned int &Op, bool ParseArchFlags,
  510. bool StripMultiArch,
  511. bool ParseRestrictionsList)
  512. {
  513. // Strip off leading space
  514. for (;Start != Stop && isspace_ascii(*Start) != 0; ++Start);
  515. // Parse off the package name
  516. const char *I = Start;
  517. for (;I != Stop && isspace_ascii(*I) == 0 && *I != '(' && *I != ')' &&
  518. *I != ',' && *I != '|' && *I != '[' && *I != ']' &&
  519. *I != '<' && *I != '>'; ++I);
  520. // Malformed, no '('
  521. if (I != Stop && *I == ')')
  522. return 0;
  523. if (I == Start)
  524. return 0;
  525. // Stash the package name
  526. Package = StringView(Start, I - Start);
  527. // We don't want to confuse library users which can't handle MultiArch
  528. if (StripMultiArch == true) {
  529. string const arch = _config->Find("APT::Architecture");
  530. size_t const found = Package.rfind(':');
  531. if (found != StringView::npos &&
  532. (Package.substr(found) == ":any" ||
  533. Package.substr(found) == ":native" ||
  534. Package.substr(found +1) == arch))
  535. Package = Package.substr(0,found);
  536. }
  537. // Skip white space to the '('
  538. for (;I != Stop && isspace_ascii(*I) != 0 ; I++);
  539. // Parse a version
  540. if (I != Stop && *I == '(')
  541. {
  542. // Skip the '('
  543. for (I++; I != Stop && isspace_ascii(*I) != 0 ; I++);
  544. if (I + 3 >= Stop)
  545. return 0;
  546. I = ConvertRelation(I,Op);
  547. // Skip whitespace
  548. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  549. Start = I;
  550. I = (const char*) memchr(I, ')', Stop - I);
  551. if (I == NULL || Start == I)
  552. return 0;
  553. // Skip trailing whitespace
  554. const char *End = I;
  555. for (; End > Start && isspace_ascii(End[-1]); End--);
  556. Ver = StringView(Start,End-Start);
  557. I++;
  558. }
  559. else
  560. {
  561. Ver = StringView();
  562. Op = pkgCache::Dep::NoOp;
  563. }
  564. // Skip whitespace
  565. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  566. if (unlikely(ParseArchFlags == true))
  567. {
  568. string const arch = _config->Find("APT::Architecture");
  569. APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false);
  570. // Parse an architecture
  571. if (I != Stop && *I == '[')
  572. {
  573. ++I;
  574. // malformed
  575. if (unlikely(I == Stop))
  576. return 0;
  577. const char *End = I;
  578. bool Found = false;
  579. bool NegArch = false;
  580. while (I != Stop)
  581. {
  582. // look for whitespace or ending ']'
  583. for (;End != Stop && !isspace_ascii(*End) && *End != ']'; ++End);
  584. if (unlikely(End == Stop))
  585. return 0;
  586. if (*I == '!')
  587. {
  588. NegArch = true;
  589. ++I;
  590. }
  591. std::string const arch(I, End);
  592. if (arch.empty() == false && matchesArch(arch.c_str()) == true)
  593. {
  594. Found = true;
  595. if (I[-1] != '!')
  596. NegArch = false;
  597. // we found a match, so fast-forward to the end of the wildcards
  598. for (; End != Stop && *End != ']'; ++End);
  599. }
  600. if (*End++ == ']') {
  601. I = End;
  602. break;
  603. }
  604. I = End;
  605. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  606. }
  607. if (NegArch == true)
  608. Found = !Found;
  609. if (Found == false)
  610. Package = ""; /* not for this arch */
  611. }
  612. // Skip whitespace
  613. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  614. }
  615. if (unlikely(ParseRestrictionsList == true))
  616. {
  617. // Parse a restrictions formula which is in disjunctive normal form:
  618. // (foo AND bar) OR (blub AND bla)
  619. std::vector<string> const profiles = APT::Configuration::getBuildProfiles();
  620. // if the next character is a restriction list, then by default the
  621. // dependency does not apply and the conditions have to be checked
  622. // if the next character is not a restriction list, then by default the
  623. // dependency applies
  624. bool applies1 = (*I != '<');
  625. while (I != Stop)
  626. {
  627. if (*I != '<')
  628. break;
  629. ++I;
  630. // malformed
  631. if (unlikely(I == Stop))
  632. return 0;
  633. const char *End = I;
  634. // if of the prior restriction list is already fulfilled, then
  635. // we can just skip to the end of the current list
  636. if (applies1) {
  637. for (;End != Stop && *End != '>'; ++End);
  638. I = ++End;
  639. // skip whitespace
  640. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  641. } else {
  642. bool applies2 = true;
  643. // all the conditions inside a restriction list have to be
  644. // met so once we find one that is not met, we can skip to
  645. // the end of this list
  646. while (I != Stop)
  647. {
  648. // look for whitespace or ending '>'
  649. // End now points to the character after the current term
  650. for (;End != Stop && !isspace_ascii(*End) && *End != '>'; ++End);
  651. if (unlikely(End == Stop))
  652. return 0;
  653. bool NegRestriction = false;
  654. if (*I == '!')
  655. {
  656. NegRestriction = true;
  657. ++I;
  658. }
  659. std::string const restriction(I, End);
  660. if (restriction.empty() == false && profiles.empty() == false &&
  661. std::find(profiles.begin(), profiles.end(), restriction) != profiles.end())
  662. {
  663. if (NegRestriction) {
  664. applies2 = false;
  665. // since one of the terms does not apply we don't have to check the others
  666. for (; End != Stop && *End != '>'; ++End);
  667. }
  668. } else {
  669. if (!NegRestriction) {
  670. applies2 = false;
  671. // since one of the terms does not apply we don't have to check the others
  672. for (; End != Stop && *End != '>'; ++End);
  673. }
  674. }
  675. if (*End++ == '>') {
  676. I = End;
  677. // skip whitespace
  678. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  679. break;
  680. }
  681. I = End;
  682. // skip whitespace
  683. for (;I != Stop && isspace_ascii(*I) != 0; I++);
  684. }
  685. if (applies2) {
  686. applies1 = true;
  687. }
  688. }
  689. }
  690. if (applies1 == false) {
  691. Package = ""; //not for this restriction
  692. }
  693. }
  694. if (I != Stop && *I == '|')
  695. Op |= pkgCache::Dep::Or;
  696. if (I == Stop || *I == ',' || *I == '|')
  697. {
  698. if (I != Stop)
  699. for (I++; I != Stop && isspace_ascii(*I) != 0; I++);
  700. return I;
  701. }
  702. return 0;
  703. }
  704. /*}}}*/
  705. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  706. // ---------------------------------------------------------------------
  707. /* This is the higher level depends parser. It takes a tag and generates
  708. a complete depends tree for the given version. */
  709. bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
  710. StringView Tag,unsigned int Type)
  711. {
  712. const char *Start;
  713. const char *Stop;
  714. if (Section.Find(Tag,Start,Stop) == false || Start == Stop)
  715. return true;
  716. string const pkgArch = Ver.Arch();
  717. while (1)
  718. {
  719. StringView Package;
  720. StringView Version;
  721. unsigned int Op;
  722. Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
  723. if (Start == 0)
  724. return _error->Error("Problem parsing dependency %.*s",(int)Tag.length(), Tag.data());
  725. size_t const found = Package.rfind(':');
  726. if (found == string::npos)
  727. {
  728. if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
  729. return false;
  730. }
  731. else if (Package.substr(found) == ":any")
  732. {
  733. if (NewDepends(Ver,Package,"any",Version,Op,Type) == false)
  734. return false;
  735. }
  736. else
  737. {
  738. // Such dependencies are not supposed to be accepted …
  739. // … but this is probably the best thing to do anyway
  740. if (Package.substr(found + 1) == "native")
  741. {
  742. std::string const Pkg = Package.substr(0, found).to_string() + ':' + Ver.Cache()->NativeArch();
  743. if (NewDepends(Ver, Pkg, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false)
  744. return false;
  745. }
  746. else if (NewDepends(Ver, Package, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false)
  747. return false;
  748. }
  749. if (Start == Stop)
  750. break;
  751. }
  752. return true;
  753. }
  754. /*}}}*/
  755. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  756. // ---------------------------------------------------------------------
  757. /* */
  758. bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
  759. {
  760. /* it is unlikely, but while parsing dependencies, we might have already
  761. picked up multi-arch implicit provides which we do not want to duplicate here */
  762. bool hasProvidesAlready = false;
  763. std::string const spzName = Ver.ParentPkg().FullName(false);
  764. {
  765. for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
  766. {
  767. if (Prv.IsMultiArchImplicit() == false || (Prv->Flags & pkgCache::Flag::ArchSpecific) == 0)
  768. continue;
  769. if (spzName != Prv.OwnerPkg().FullName(false))
  770. continue;
  771. hasProvidesAlready = true;
  772. break;
  773. }
  774. }
  775. string const Arch = Ver.Arch();
  776. const char *Start;
  777. const char *Stop;
  778. if (Section.Find("Provides",Start,Stop) == true)
  779. {
  780. StringView Package;
  781. StringView Version;
  782. unsigned int Op;
  783. do
  784. {
  785. Start = ParseDepends(Start,Stop,Package,Version,Op, false, false, false);
  786. const size_t archfound = Package.rfind(':');
  787. if (Start == 0)
  788. return _error->Error("Problem parsing Provides line");
  789. if (unlikely(Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals)) {
  790. _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.to_string().c_str());
  791. } else if (archfound != string::npos) {
  792. StringView spzArch = Package.substr(archfound + 1);
  793. if (spzArch != "any")
  794. {
  795. if (NewProvides(Ver, Package.substr(0, archfound), spzArch, Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
  796. return false;
  797. }
  798. if (NewProvides(Ver, Package, "any", Version, pkgCache::Flag::ArchSpecific) == false)
  799. return false;
  800. } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
  801. if (APT::Configuration::checkArchitecture(Arch))
  802. {
  803. if (NewProvidesAllArch(Ver, Package, Version, 0) == false)
  804. return false;
  805. }
  806. else if (NewProvides(Ver, Package, Arch, Version, 0) == false)
  807. return false;
  808. } else {
  809. if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  810. {
  811. if (NewProvides(Ver, Package.to_string().append(":any"), "any", Version, pkgCache::Flag::MultiArchImplicit) == false)
  812. return false;
  813. }
  814. if (NewProvides(Ver, Package, Arch, Version, 0) == false)
  815. return false;
  816. }
  817. if (archfound == std::string::npos)
  818. {
  819. string spzName = Package.to_string();
  820. spzName.push_back(':');
  821. spzName.append(Ver.ParentPkg().Arch());
  822. pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any");
  823. if (spzPkg.end() == false)
  824. {
  825. if (NewProvides(Ver, spzName, "any", Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
  826. return false;
  827. }
  828. }
  829. } while (Start != Stop);
  830. }
  831. if (APT::Configuration::checkArchitecture(Arch))
  832. {
  833. if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  834. {
  835. string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
  836. if (NewProvides(Ver, Package, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false)
  837. return false;
  838. }
  839. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  840. {
  841. if (NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false)
  842. return false;
  843. }
  844. }
  845. if (hasProvidesAlready == false)
  846. {
  847. pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any");
  848. if (spzPkg.end() == false)
  849. {
  850. if (NewProvides(Ver, spzName, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
  851. return false;
  852. }
  853. }
  854. return true;
  855. }
  856. /*}}}*/
  857. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  858. // ---------------------------------------------------------------------
  859. /* Looks for a word in a list of words - for ParseStatus */
  860. bool debListParser::GrabWord(StringView Word, WordList const *List, unsigned char &Out)
  861. {
  862. for (unsigned int C = 0; List[C].Str.empty() == false; C++)
  863. {
  864. if (Word.length() == List[C].Str.length() &&
  865. strncasecmp(Word.data(), List[C].Str.data(), Word.length()) == 0)
  866. {
  867. Out = List[C].Val;
  868. return true;
  869. }
  870. }
  871. return false;
  872. }
  873. /*}}}*/
  874. // ListParser::Step - Move to the next section in the file /*{{{*/
  875. // ---------------------------------------------------------------------
  876. /* This has to be careful to only process the correct architecture */
  877. bool debListParser::Step()
  878. {
  879. iOffset = Tags.Offset();
  880. return Tags.Step(Section);
  881. }
  882. /*}}}*/
  883. // ListParser::GetPrio - Convert the priority from a string /*{{{*/
  884. // ---------------------------------------------------------------------
  885. /* */
  886. unsigned char debListParser::GetPrio(string Str)
  887. {
  888. unsigned char Out;
  889. if (GrabWord(Str,PrioList,Out) == false)
  890. Out = pkgCache::State::Extra;
  891. return Out;
  892. }
  893. /*}}}*/
  894. bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/
  895. pkgCache::VerIterator const &Ver)
  896. {
  897. if (pkgCacheListParser::SameVersion(Hash, Ver) == false)
  898. return false;
  899. // status file has no (Download)Size, but all others are fair game
  900. // status file is parsed last, so the first version we encounter is
  901. // probably also the version we have downloaded
  902. unsigned long long const Size = Section.FindULL("Size");
  903. if (Size != 0 && Size != Ver->Size)
  904. return false;
  905. // available everywhere, but easier to check here than to include in VersionHash
  906. unsigned char MultiArch = ParseMultiArch(false);
  907. if (MultiArch != Ver->MultiArch)
  908. return false;
  909. // for all practical proposes (we can check): same version
  910. return true;
  911. }
  912. /*}}}*/
  913. debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile)
  914. : debListParser(File), DebFile(DebFile)
  915. {
  916. }
  917. bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg,
  918. pkgCache::VerIterator &Ver)
  919. {
  920. bool res = debListParser::UsePackage(Pkg, Ver);
  921. // we use the full file path as a provides so that the file is found
  922. // by its name
  923. if(NewProvides(Ver, DebFile, Pkg.Cache()->NativeArch(), Ver.VerStr(), 0) == false)
  924. return false;
  925. return res;
  926. }
  927. debListParser::~debListParser() {}