deblistparser.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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/fileutl.h>
  18. #include <apt-pkg/crc-16.h>
  19. #include <apt-pkg/md5.h>
  20. #include <apt-pkg/mmap.h>
  21. #include <apt-pkg/pkgcache.h>
  22. #include <apt-pkg/cacheiterators.h>
  23. #include <apt-pkg/tagfile.h>
  24. #include <apt-pkg/macros.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. #include <algorithm>
  28. #include <string>
  29. #include <vector>
  30. #include <ctype.h>
  31. /*}}}*/
  32. using std::string;
  33. static 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. {NULL, 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, string const &Arch) : Tags(File),
  46. Arch(Arch) {
  47. if (Arch == "native")
  48. this->Arch = _config->Find("APT::Architecture");
  49. Architectures = APT::Configuration::getArchitectures();
  50. MultiArchEnabled = Architectures.size() > 1;
  51. }
  52. /*}}}*/
  53. // ListParser::Package - Return the package name /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* This is to return the name of the package this section describes */
  56. string debListParser::Package() {
  57. string const Result = Section.FindS("Package");
  58. if(unlikely(Result.empty() == true))
  59. _error->Error("Encountered a section with no Package: header");
  60. return Result;
  61. }
  62. /*}}}*/
  63. // ListParser::Architecture - Return the package arch /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* This will return the Architecture of the package this section describes */
  66. string debListParser::Architecture() {
  67. return Section.FindS("Architecture");
  68. }
  69. /*}}}*/
  70. // ListParser::ArchitectureAll /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* */
  73. bool debListParser::ArchitectureAll() {
  74. return Section.FindS("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. string debListParser::Version()
  83. {
  84. return Section.FindS("Version");
  85. }
  86. /*}}}*/
  87. unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/
  88. {
  89. unsigned char MA;
  90. string const MultiArch = Section.FindS("Multi-Arch");
  91. if (MultiArch.empty() == true || MultiArch == "no")
  92. MA = pkgCache::Version::None;
  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::None;
  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.c_str(), Section.FindS("Package").c_str());
  113. MA = pkgCache::Version::None;
  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 const 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. Stop = Space;
  144. const char * const Open = (const char * const) memchr(Space, '(', Stop - Space);
  145. if (likely(Open != NULL))
  146. {
  147. const char * const Close = (const char * const) memchr(Open, ')', Stop - Open);
  148. if (likely(Close != NULL))
  149. {
  150. std::string const version(Open + 1, (Close - Open) - 1);
  151. if (version != Ver.VerStr())
  152. {
  153. map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version);
  154. Ver->SourceVerStr = idx;
  155. }
  156. }
  157. }
  158. }
  159. std::string const pkgname(Start, Stop - Start);
  160. if (pkgname != G.Name())
  161. {
  162. for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P))
  163. {
  164. for (V = P.VersionList(); V.end() == false; ++V)
  165. {
  166. if (pkgname == V.SourcePkgName())
  167. {
  168. Ver->SourcePkgName = V->SourcePkgName;
  169. break;
  170. }
  171. }
  172. if (V.end() == false)
  173. break;
  174. }
  175. if (V.end() == true)
  176. {
  177. map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname);
  178. Ver->SourcePkgName = idx;
  179. }
  180. }
  181. }
  182. Ver->MultiArch = ParseMultiArch(true);
  183. // Archive Size
  184. Ver->Size = Section.FindULL("Size");
  185. // Unpacked Size (in K)
  186. Ver->InstalledSize = Section.FindULL("Installed-Size");
  187. Ver->InstalledSize *= 1024;
  188. // Priority
  189. if (Section.Find("Priority",Start,Stop) == true)
  190. {
  191. if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
  192. Ver->Priority = pkgCache::State::Extra;
  193. }
  194. if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
  195. return false;
  196. if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
  197. return false;
  198. if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
  199. return false;
  200. if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
  201. return false;
  202. if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
  203. return false;
  204. if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
  205. return false;
  206. if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
  207. return false;
  208. if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
  209. return false;
  210. // Obsolete.
  211. if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
  212. return false;
  213. if (ParseProvides(Ver) == false)
  214. return false;
  215. return true;
  216. }
  217. /*}}}*/
  218. // ListParser::Description - Return the description string /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* This is to return the string describing the package in debian
  221. form. If this returns the blank string then the entry is assumed to
  222. only describe package properties */
  223. string debListParser::Description(std::string const &lang)
  224. {
  225. if (lang.empty())
  226. return Section.FindS("Description");
  227. else
  228. return Section.FindS(string("Description-").append(lang).c_str());
  229. }
  230. /*}}}*/
  231. // ListParser::AvailableDescriptionLanguages /*{{{*/
  232. std::vector<std::string> debListParser::AvailableDescriptionLanguages()
  233. {
  234. std::vector<std::string> const understood = APT::Configuration::getLanguages();
  235. std::vector<std::string> avail;
  236. if (Section.Exists("Description") == true)
  237. avail.push_back("");
  238. for (std::vector<std::string>::const_iterator lang = understood.begin(); lang != understood.end(); ++lang)
  239. {
  240. std::string const tagname = "Description-" + *lang;
  241. if (Section.Exists(tagname.c_str()) == true)
  242. avail.push_back(*lang);
  243. }
  244. return avail;
  245. }
  246. /*}}}*/
  247. // ListParser::Description_md5 - Return the description_md5 MD5SumValue /*{{{*/
  248. // ---------------------------------------------------------------------
  249. /* This is to return the md5 string to allow the check if it is the right
  250. description. If no Description-md5 is found in the section it will be
  251. calculated.
  252. */
  253. MD5SumValue debListParser::Description_md5()
  254. {
  255. string const value = Section.FindS("Description-md5");
  256. if (value.empty() == true)
  257. {
  258. std::string const desc = Description("") + "\n";
  259. if (desc == "\n")
  260. return MD5SumValue();
  261. MD5Summation md5;
  262. md5.Add(desc.c_str());
  263. return md5.Result();
  264. }
  265. else if (likely(value.size() == 32))
  266. {
  267. if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos))
  268. return MD5SumValue(value);
  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(*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. // ListParser::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, hold, hold-reinstreq
  350. status = not-installed, unpacked, half-configured,
  351. half-installed, config-files, post-inst-failed,
  352. removal-failed, installed
  353. Some of the above are obsolete (I think?) flag = hold-* and
  354. status = post-inst-failed, removal-failed at least.
  355. */
  356. bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
  357. pkgCache::VerIterator &Ver)
  358. {
  359. const char *Start;
  360. const char *Stop;
  361. if (Section.Find("Status",Start,Stop) == false)
  362. return true;
  363. // UsePackage() is responsible for setting the flag in the default case
  364. bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
  365. if (essential == true &&
  366. Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  367. return false;
  368. // Isolate the first word
  369. const char *I = Start;
  370. for(; I < Stop && *I != ' '; I++);
  371. if (I >= Stop || *I != ' ')
  372. return _error->Error("Malformed Status line");
  373. // Process the want field
  374. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  375. {"install",pkgCache::State::Install},
  376. {"hold",pkgCache::State::Hold},
  377. {"deinstall",pkgCache::State::DeInstall},
  378. {"purge",pkgCache::State::Purge},
  379. {NULL, 0}};
  380. if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
  381. return _error->Error("Malformed 1st word in the Status line");
  382. // Isloate the next word
  383. I++;
  384. Start = I;
  385. for(; I < Stop && *I != ' '; I++);
  386. if (I >= Stop || *I != ' ')
  387. return _error->Error("Malformed status line, no 2nd word");
  388. // Process the flag field
  389. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  390. {"reinstreq",pkgCache::State::ReInstReq},
  391. {"hold",pkgCache::State::HoldInst},
  392. {"hold-reinstreq",pkgCache::State::HoldReInstReq},
  393. {NULL, 0}};
  394. if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
  395. return _error->Error("Malformed 2nd word in the Status line");
  396. // Isloate the last word
  397. I++;
  398. Start = I;
  399. for(; I < Stop && *I != ' '; I++);
  400. if (I != Stop)
  401. return _error->Error("Malformed Status line, no 3rd word");
  402. // Process the flag field
  403. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  404. {"unpacked",pkgCache::State::UnPacked},
  405. {"half-configured",pkgCache::State::HalfConfigured},
  406. {"installed",pkgCache::State::Installed},
  407. {"half-installed",pkgCache::State::HalfInstalled},
  408. {"config-files",pkgCache::State::ConfigFiles},
  409. {"triggers-awaited",pkgCache::State::TriggersAwaited},
  410. {"triggers-pending",pkgCache::State::TriggersPending},
  411. {"post-inst-failed",pkgCache::State::HalfConfigured},
  412. {"removal-failed",pkgCache::State::HalfInstalled},
  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(*Start) != 0; ++Start);
  504. // Parse off the package name
  505. const char *I = Start;
  506. for (;I != Stop && isspace(*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. string const arch = _config->Find("APT::Architecture");
  518. if (StripMultiArch == true) {
  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(*I) != 0 ; I++);
  528. // Parse a version
  529. if (I != Stop && *I == '(')
  530. {
  531. // Skip the '('
  532. for (I++; I != Stop && isspace(*I) != 0 ; I++);
  533. if (I + 3 >= Stop)
  534. return 0;
  535. I = ConvertRelation(I,Op);
  536. // Skip whitespace
  537. for (;I != Stop && isspace(*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(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(*I) != 0; I++);
  555. if (ParseArchFlags == true)
  556. {
  557. APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false);
  558. // Parse an architecture
  559. if (I != Stop && *I == '[')
  560. {
  561. ++I;
  562. // malformed
  563. if (unlikely(I == Stop))
  564. return 0;
  565. const char *End = I;
  566. bool Found = false;
  567. bool NegArch = false;
  568. while (I != Stop)
  569. {
  570. // look for whitespace or ending ']'
  571. for (;End != Stop && !isspace(*End) && *End != ']'; ++End);
  572. if (unlikely(End == Stop))
  573. return 0;
  574. if (*I == '!')
  575. {
  576. NegArch = true;
  577. ++I;
  578. }
  579. std::string arch(I, End);
  580. if (arch.empty() == false && matchesArch(arch.c_str()) == true)
  581. {
  582. Found = true;
  583. if (I[-1] != '!')
  584. NegArch = false;
  585. // we found a match, so fast-forward to the end of the wildcards
  586. for (; End != Stop && *End != ']'; ++End);
  587. }
  588. if (*End++ == ']') {
  589. I = End;
  590. break;
  591. }
  592. I = End;
  593. for (;I != Stop && isspace(*I) != 0; I++);
  594. }
  595. if (NegArch == true)
  596. Found = !Found;
  597. if (Found == false)
  598. Package = ""; /* not for this arch */
  599. }
  600. // Skip whitespace
  601. for (;I != Stop && isspace(*I) != 0; I++);
  602. }
  603. if (ParseRestrictionsList == true)
  604. {
  605. // Parse a restrictions list
  606. if (I != Stop && *I == '<')
  607. {
  608. ++I;
  609. // malformed
  610. if (unlikely(I == Stop))
  611. return 0;
  612. std::vector<string> const profiles = APT::Configuration::getBuildProfiles();
  613. const char *End = I;
  614. bool Found = false;
  615. bool NegRestriction = false;
  616. while (I != Stop)
  617. {
  618. // look for whitespace or ending '>'
  619. for (;End != Stop && !isspace(*End) && *End != '>'; ++End);
  620. if (unlikely(End == Stop))
  621. return 0;
  622. if (*I == '!')
  623. {
  624. NegRestriction = true;
  625. ++I;
  626. }
  627. std::string restriction(I, End);
  628. std::string prefix = "profile.";
  629. // only support for "profile" prefix, ignore others
  630. if (restriction.size() > prefix.size() &&
  631. restriction.substr(0, prefix.size()) == prefix)
  632. {
  633. // get the name of the profile
  634. restriction = restriction.substr(prefix.size());
  635. if (restriction.empty() == false && profiles.empty() == false &&
  636. std::find(profiles.begin(), profiles.end(), restriction) != profiles.end())
  637. {
  638. Found = true;
  639. if (I[-1] != '!')
  640. NegRestriction = false;
  641. // we found a match, so fast-forward to the end of the wildcards
  642. for (; End != Stop && *End != '>'; ++End);
  643. }
  644. }
  645. if (*End++ == '>') {
  646. I = End;
  647. break;
  648. }
  649. I = End;
  650. for (;I != Stop && isspace(*I) != 0; I++);
  651. }
  652. if (NegRestriction == true)
  653. Found = !Found;
  654. if (Found == false)
  655. Package = ""; /* not for this restriction */
  656. }
  657. // Skip whitespace
  658. for (;I != Stop && isspace(*I) != 0; I++);
  659. }
  660. if (I != Stop && *I == '|')
  661. Op |= pkgCache::Dep::Or;
  662. if (I == Stop || *I == ',' || *I == '|')
  663. {
  664. if (I != Stop)
  665. for (I++; I != Stop && isspace(*I) != 0; I++);
  666. return I;
  667. }
  668. return 0;
  669. }
  670. /*}}}*/
  671. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  672. // ---------------------------------------------------------------------
  673. /* This is the higher level depends parser. It takes a tag and generates
  674. a complete depends tree for the given version. */
  675. bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
  676. const char *Tag,unsigned int Type)
  677. {
  678. const char *Start;
  679. const char *Stop;
  680. if (Section.Find(Tag,Start,Stop) == false)
  681. return true;
  682. string const pkgArch = Ver.Arch();
  683. while (1)
  684. {
  685. string Package;
  686. string Version;
  687. unsigned int Op;
  688. Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
  689. if (Start == 0)
  690. return _error->Error("Problem parsing dependency %s",Tag);
  691. size_t const found = Package.rfind(':');
  692. // If negative is unspecific it needs to apply on all architectures
  693. if (MultiArchEnabled == true && found == string::npos &&
  694. (Type == pkgCache::Dep::Conflicts ||
  695. Type == pkgCache::Dep::DpkgBreaks ||
  696. Type == pkgCache::Dep::Replaces))
  697. {
  698. for (std::vector<std::string>::const_iterator a = Architectures.begin();
  699. a != Architectures.end(); ++a)
  700. if (NewDepends(Ver,Package,*a,Version,Op,Type) == false)
  701. return false;
  702. if (NewDepends(Ver,Package,"none",Version,Op,Type) == false)
  703. return false;
  704. }
  705. else if (MultiArchEnabled == true && found != string::npos &&
  706. strcmp(Package.c_str() + found, ":any") != 0)
  707. {
  708. string Arch = Package.substr(found+1, string::npos);
  709. Package = Package.substr(0, found);
  710. // Such dependencies are not supposed to be accepted …
  711. // … but this is probably the best thing to do.
  712. if (Arch == "native")
  713. Arch = _config->Find("APT::Architecture");
  714. if (NewDepends(Ver,Package,Arch,Version,Op,Type) == false)
  715. return false;
  716. }
  717. else
  718. {
  719. if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
  720. return false;
  721. if ((Type == pkgCache::Dep::Conflicts ||
  722. Type == pkgCache::Dep::DpkgBreaks ||
  723. Type == pkgCache::Dep::Replaces) &&
  724. NewDepends(Ver, Package,
  725. (pkgArch != "none") ? "none" : _config->Find("APT::Architecture"),
  726. Version,Op,Type) == false)
  727. return false;
  728. }
  729. if (Start == Stop)
  730. break;
  731. }
  732. return true;
  733. }
  734. /*}}}*/
  735. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  736. // ---------------------------------------------------------------------
  737. /* */
  738. bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
  739. {
  740. const char *Start;
  741. const char *Stop;
  742. if (Section.Find("Provides",Start,Stop) == true)
  743. {
  744. string Package;
  745. string Version;
  746. string const Arch = Ver.Arch();
  747. unsigned int Op;
  748. while (1)
  749. {
  750. Start = ParseDepends(Start,Stop,Package,Version,Op);
  751. if (Start == 0)
  752. return _error->Error("Problem parsing Provides line");
  753. if (Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals) {
  754. _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.c_str());
  755. } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) {
  756. if (NewProvidesAllArch(Ver, Package, Version) == false)
  757. return false;
  758. } else {
  759. if (NewProvides(Ver, Package, Arch, Version) == false)
  760. return false;
  761. }
  762. if (Start == Stop)
  763. break;
  764. }
  765. }
  766. if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
  767. {
  768. string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
  769. return NewProvidesAllArch(Ver, Package, Ver.VerStr());
  770. }
  771. else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
  772. return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr());
  773. return true;
  774. }
  775. /*}}}*/
  776. // ListParser::NewProvides - add provides for all architectures /*{{{*/
  777. bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
  778. string const &Version) {
  779. for (std::vector<string>::const_iterator a = Architectures.begin();
  780. a != Architectures.end(); ++a)
  781. {
  782. if (NewProvides(Ver, Package, *a, Version) == false)
  783. return false;
  784. }
  785. return true;
  786. }
  787. /*}}}*/
  788. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  789. // ---------------------------------------------------------------------
  790. /* Looks for a word in a list of words - for ParseStatus */
  791. bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
  792. {
  793. for (unsigned int C = 0; List[C].Str != 0; C++)
  794. {
  795. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  796. {
  797. Out = List[C].Val;
  798. return true;
  799. }
  800. }
  801. return false;
  802. }
  803. /*}}}*/
  804. // ListParser::Step - Move to the next section in the file /*{{{*/
  805. // ---------------------------------------------------------------------
  806. /* This has to be careful to only process the correct architecture */
  807. bool debListParser::Step()
  808. {
  809. iOffset = Tags.Offset();
  810. while (Tags.Step(Section) == true)
  811. {
  812. /* See if this is the correct Architecture, if it isn't then we
  813. drop the whole section. A missing arch tag only happens (in theory)
  814. inside the Status file, so that is a positive return */
  815. string const Architecture = Section.FindS("Architecture");
  816. if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false)
  817. {
  818. if (APT::Configuration::checkArchitecture(Architecture) == true)
  819. return true;
  820. /* parse version stanzas without an architecture only in the status file
  821. (and as misfortune bycatch flat-archives) */
  822. if ((Arch.empty() == true || Arch == "any") && Architecture.empty() == true)
  823. return true;
  824. }
  825. else
  826. {
  827. if (Architecture == Arch)
  828. return true;
  829. if (Architecture == "all" && Arch == _config->Find("APT::Architecture"))
  830. return true;
  831. }
  832. iOffset = Tags.Offset();
  833. }
  834. return false;
  835. }
  836. /*}}}*/
  837. // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
  838. // ---------------------------------------------------------------------
  839. /* */
  840. bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI,
  841. FileFd &File, string component)
  842. {
  843. // apt-secure does no longer download individual (per-section) Release
  844. // file. to provide Component pinning we use the section name now
  845. map_stringitem_t const storage = StoreString(pkgCacheGenerator::MIXED, component);
  846. FileI->Component = storage;
  847. pkgTagFile TagFile(&File, File.Size());
  848. pkgTagSection Section;
  849. if (_error->PendingError() == true || TagFile.Step(Section) == false)
  850. return false;
  851. std::string data;
  852. #define APT_INRELEASE(TYPE, TAG, STORE) \
  853. data = Section.FindS(TAG); \
  854. if (data.empty() == false) \
  855. { \
  856. map_stringitem_t const storage = StoreString(pkgCacheGenerator::TYPE, data); \
  857. STORE = storage; \
  858. }
  859. APT_INRELEASE(MIXED, "Suite", FileI->Archive)
  860. APT_INRELEASE(MIXED, "Component", FileI->Component)
  861. APT_INRELEASE(VERSIONNUMBER, "Version", FileI->Version)
  862. APT_INRELEASE(MIXED, "Origin", FileI->Origin)
  863. APT_INRELEASE(MIXED, "Codename", FileI->Codename)
  864. APT_INRELEASE(MIXED, "Label", FileI->Label)
  865. #undef APT_INRELEASE
  866. Section.FindFlag("NotAutomatic", FileI->Flags, pkgCache::Flag::NotAutomatic);
  867. Section.FindFlag("ButAutomaticUpgrades", FileI->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  868. return !_error->PendingError();
  869. }
  870. /*}}}*/
  871. // ListParser::GetPrio - Convert the priority from a string /*{{{*/
  872. // ---------------------------------------------------------------------
  873. /* */
  874. unsigned char debListParser::GetPrio(string Str)
  875. {
  876. unsigned char Out;
  877. if (GrabWord(Str,PrioList,Out) == false)
  878. Out = pkgCache::State::Extra;
  879. return Out;
  880. }
  881. /*}}}*/
  882. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  883. bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/
  884. pkgCache::VerIterator const &Ver)
  885. {
  886. if (pkgCacheGenerator::ListParser::SameVersion(Hash, Ver) == false)
  887. return false;
  888. // status file has no (Download)Size, but all others are fair game
  889. // status file is parsed last, so the first version we encounter is
  890. // probably also the version we have downloaded
  891. unsigned long long const Size = Section.FindULL("Size");
  892. if (Size != 0 && Size != Ver->Size)
  893. return false;
  894. // available everywhere, but easier to check here than to include in VersionHash
  895. unsigned char MultiArch = ParseMultiArch(false);
  896. if (MultiArch != Ver->MultiArch)
  897. return false;
  898. // for all practical proposes (we can check): same version
  899. return true;
  900. }
  901. /*}}}*/
  902. #endif
  903. debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile)
  904. : debListParser(File, ""), DebFile(DebFile)
  905. {
  906. }
  907. bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg,
  908. pkgCache::VerIterator &Ver)
  909. {
  910. bool res = debListParser::UsePackage(Pkg, Ver);
  911. // we use the full file path as a provides so that the file is found
  912. // by its name
  913. if(NewProvidesAllArch(Ver, DebFile, Ver.VerStr()) == false)
  914. return false;
  915. return res;
  916. }