deblistparser.cc 35 KB

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