deblistparser.cc 32 KB

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