deblistparser.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 <apt-pkg/deblistparser.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/crc-16.h>
  15. #include <apt-pkg/md5.h>
  16. #include <ctype.h>
  17. #include <system.h>
  18. /*}}}*/
  19. static debListParser::WordList PrioList[] = {{"important",pkgCache::State::Important},
  20. {"required",pkgCache::State::Required},
  21. {"standard",pkgCache::State::Standard},
  22. {"optional",pkgCache::State::Optional},
  23. {"extra",pkgCache::State::Extra},
  24. {}};
  25. // ListParser::debListParser - Constructor /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. debListParser::debListParser(FileFd *File) : Tags(File)
  29. {
  30. Arch = _config->Find("APT::architecture");
  31. }
  32. /*}}}*/
  33. // ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. unsigned long debListParser::UniqFindTagWrite(const char *Tag)
  37. {
  38. const char *Start;
  39. const char *Stop;
  40. if (Section.Find(Tag,Start,Stop) == false)
  41. return 0;
  42. return WriteUniqString(Start,Stop - Start);
  43. }
  44. /*}}}*/
  45. // ListParser::Package - Return the package name /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* This is to return the name of the package this section describes */
  48. string debListParser::Package()
  49. {
  50. string Result = Section.FindS("Package");
  51. if (Result.empty() == true)
  52. _error->Error("Encountered a section with no Package: header");
  53. return Result;
  54. }
  55. /*}}}*/
  56. // ListParser::Version - Return the version string /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* This is to return the string describing the version in debian form,
  59. epoch:upstream-release. If this returns the blank string then the
  60. entry is assumed to only describe package properties */
  61. string debListParser::Version()
  62. {
  63. return Section.FindS("Version");
  64. }
  65. /*}}}*/
  66. // ListParser::NewVersion - Fill in the version structure /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. bool debListParser::NewVersion(pkgCache::VerIterator Ver)
  70. {
  71. // Parse the section
  72. Ver->Section = UniqFindTagWrite("Section");
  73. Ver->Arch = UniqFindTagWrite("Architecture");
  74. // Archive Size
  75. Ver->Size = (unsigned)Section.FindI("Size");
  76. // Unpacked Size (in K)
  77. Ver->InstalledSize = (unsigned)Section.FindI("Installed-Size");
  78. Ver->InstalledSize *= 1024;
  79. // Priority
  80. const char *Start;
  81. const char *Stop;
  82. if (Section.Find("Priority",Start,Stop) == true)
  83. {
  84. if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false)
  85. Ver->Priority = pkgCache::State::Extra;
  86. }
  87. if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
  88. return false;
  89. if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
  90. return false;
  91. if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
  92. return false;
  93. if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
  94. return false;
  95. if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
  96. return false;
  97. if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
  98. return false;
  99. // Obsolete.
  100. if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
  101. return false;
  102. if (ParseProvides(Ver) == false)
  103. return false;
  104. return true;
  105. }
  106. /*}}}*/
  107. // ListParser::Description - Return the description string /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This is to return the string describing the package in debian
  110. form. If this returns the blank string then the entry is assumed to
  111. only describe package properties */
  112. string debListParser::Description()
  113. {
  114. if (DescriptionLanguage().empty())
  115. return Section.FindS("Description");
  116. else
  117. return Section.FindS(("Description-" + pkgIndexFile::LanguageCode()).c_str());
  118. }
  119. /*}}}*/
  120. // ListParser::DescriptionLanguage - Return the description lang string /*{{{*/
  121. // ---------------------------------------------------------------------
  122. /* This is to return the string describing the language of
  123. description. If this returns the blank string then the entry is
  124. assumed to describe original description. */
  125. string debListParser::DescriptionLanguage()
  126. {
  127. return Section.FindS("Description").empty() ? pkgIndexFile::LanguageCode() : "";
  128. }
  129. /*}}}*/
  130. // ListParser::Description - Return the description_md5 MD5SumValue /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* This is to return the md5 string to allow the check if is the right
  133. description. If thisreturns a blank string then calculate the md5
  134. value. */
  135. MD5SumValue debListParser::Description_md5()
  136. {
  137. string value = Section.FindS("Description-md5");
  138. if (value.empty()) {
  139. MD5Summation md5;
  140. md5.Add((Description() + "\n").c_str());
  141. return md5.Result();
  142. } else
  143. return MD5SumValue(value);
  144. }
  145. /*}}}*/
  146. // ListParser::UsePackage - Update a package structure /*{{{*/
  147. // ---------------------------------------------------------------------
  148. /* This is called to update the package with any new information
  149. that might be found in the section */
  150. bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
  151. pkgCache::VerIterator Ver)
  152. {
  153. if (Pkg->Section == 0)
  154. Pkg->Section = UniqFindTagWrite("Section");
  155. if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
  156. return false;
  157. if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
  158. return false;
  159. if (strcmp(Pkg.Name(),"apt") == 0)
  160. Pkg->Flags |= pkgCache::Flag::Important;
  161. if (ParseStatus(Pkg,Ver) == false)
  162. return false;
  163. return true;
  164. }
  165. /*}}}*/
  166. // ListParser::VersionHash - Compute a unique hash for this version /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* */
  169. unsigned short debListParser::VersionHash()
  170. {
  171. const char *Sections[] ={"Installed-Size",
  172. "Depends",
  173. "Pre-Depends",
  174. // "Suggests",
  175. // "Recommends",
  176. "Conflicts",
  177. "Replaces",0};
  178. unsigned long Result = INIT_FCS;
  179. char S[1024];
  180. for (const char **I = Sections; *I != 0; I++)
  181. {
  182. const char *Start;
  183. const char *End;
  184. if (Section.Find(*I,Start,End) == false || End - Start >= (signed)sizeof(S))
  185. continue;
  186. /* Strip out any spaces from the text, this undoes dpkgs reformatting
  187. of certain fields. dpkg also has the rather interesting notion of
  188. reformatting depends operators < -> <= */
  189. char *I = S;
  190. for (; Start != End; Start++)
  191. {
  192. if (isspace(*Start) == 0)
  193. *I++ = tolower(*Start);
  194. if (*Start == '<' && Start[1] != '<' && Start[1] != '=')
  195. *I++ = '=';
  196. if (*Start == '>' && Start[1] != '>' && Start[1] != '=')
  197. *I++ = '=';
  198. }
  199. Result = AddCRC16(Result,S,I - S);
  200. }
  201. return Result;
  202. }
  203. /*}}}*/
  204. // ListParser::ParseStatus - Parse the status field /*{{{*/
  205. // ---------------------------------------------------------------------
  206. /* Status lines are of the form,
  207. Status: want flag status
  208. want = unknown, install, hold, deinstall, purge
  209. flag = ok, reinstreq, hold, hold-reinstreq
  210. status = not-installed, unpacked, half-configured,
  211. half-installed, config-files, post-inst-failed,
  212. removal-failed, installed
  213. Some of the above are obsolete (I think?) flag = hold-* and
  214. status = post-inst-failed, removal-failed at least.
  215. */
  216. bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
  217. pkgCache::VerIterator Ver)
  218. {
  219. const char *Start;
  220. const char *Stop;
  221. if (Section.Find("Status",Start,Stop) == false)
  222. return true;
  223. // Isolate the first word
  224. const char *I = Start;
  225. for(; I < Stop && *I != ' '; I++);
  226. if (I >= Stop || *I != ' ')
  227. return _error->Error("Malformed Status line");
  228. // Process the want field
  229. WordList WantList[] = {{"unknown",pkgCache::State::Unknown},
  230. {"install",pkgCache::State::Install},
  231. {"hold",pkgCache::State::Hold},
  232. {"deinstall",pkgCache::State::DeInstall},
  233. {"purge",pkgCache::State::Purge},
  234. {}};
  235. if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false)
  236. return _error->Error("Malformed 1st word in the Status line");
  237. // Isloate the next word
  238. I++;
  239. Start = I;
  240. for(; I < Stop && *I != ' '; I++);
  241. if (I >= Stop || *I != ' ')
  242. return _error->Error("Malformed status line, no 2nd word");
  243. // Process the flag field
  244. WordList FlagList[] = {{"ok",pkgCache::State::Ok},
  245. {"reinstreq",pkgCache::State::ReInstReq},
  246. {"hold",pkgCache::State::HoldInst},
  247. {"hold-reinstreq",pkgCache::State::HoldReInstReq},
  248. {}};
  249. if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false)
  250. return _error->Error("Malformed 2nd word in the Status line");
  251. // Isloate the last word
  252. I++;
  253. Start = I;
  254. for(; I < Stop && *I != ' '; I++);
  255. if (I != Stop)
  256. return _error->Error("Malformed Status line, no 3rd word");
  257. // Process the flag field
  258. WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled},
  259. {"unpacked",pkgCache::State::UnPacked},
  260. {"half-configured",pkgCache::State::HalfConfigured},
  261. {"installed",pkgCache::State::Installed},
  262. {"half-installed",pkgCache::State::HalfInstalled},
  263. {"config-files",pkgCache::State::ConfigFiles},
  264. {"post-inst-failed",pkgCache::State::HalfConfigured},
  265. {"removal-failed",pkgCache::State::HalfInstalled},
  266. {}};
  267. if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false)
  268. return _error->Error("Malformed 3rd word in the Status line");
  269. /* A Status line marks the package as indicating the current
  270. version as well. Only if it is actually installed.. Otherwise
  271. the interesting dpkg handling of the status file creates bogus
  272. entries. */
  273. if (!(Pkg->CurrentState == pkgCache::State::NotInstalled ||
  274. Pkg->CurrentState == pkgCache::State::ConfigFiles))
  275. {
  276. if (Ver.end() == true)
  277. _error->Warning("Encountered status field in a non-version description");
  278. else
  279. Pkg->CurrentVer = Ver.Index();
  280. }
  281. return true;
  282. }
  283. const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
  284. {
  285. // Determine the operator
  286. switch (*I)
  287. {
  288. case '<':
  289. I++;
  290. if (*I == '=')
  291. {
  292. I++;
  293. Op = pkgCache::Dep::LessEq;
  294. break;
  295. }
  296. if (*I == '<')
  297. {
  298. I++;
  299. Op = pkgCache::Dep::Less;
  300. break;
  301. }
  302. // < is the same as <= and << is really Cs < for some reason
  303. Op = pkgCache::Dep::LessEq;
  304. break;
  305. case '>':
  306. I++;
  307. if (*I == '=')
  308. {
  309. I++;
  310. Op = pkgCache::Dep::GreaterEq;
  311. break;
  312. }
  313. if (*I == '>')
  314. {
  315. I++;
  316. Op = pkgCache::Dep::Greater;
  317. break;
  318. }
  319. // > is the same as >= and >> is really Cs > for some reason
  320. Op = pkgCache::Dep::GreaterEq;
  321. break;
  322. case '=':
  323. Op = pkgCache::Dep::Equals;
  324. I++;
  325. break;
  326. // HACK around bad package definitions
  327. default:
  328. Op = pkgCache::Dep::Equals;
  329. break;
  330. }
  331. return I;
  332. }
  333. /*}}}*/
  334. // ListParser::ParseDepends - Parse a dependency element /*{{{*/
  335. // ---------------------------------------------------------------------
  336. /* This parses the dependency elements out of a standard string in place,
  337. bit by bit. */
  338. const char *debListParser::ParseDepends(const char *Start,const char *Stop,
  339. string &Package,string &Ver,
  340. unsigned int &Op, bool ParseArchFlags)
  341. {
  342. // Strip off leading space
  343. for (;Start != Stop && isspace(*Start) != 0; Start++);
  344. // Parse off the package name
  345. const char *I = Start;
  346. for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
  347. *I != ',' && *I != '|'; I++);
  348. // Malformed, no '('
  349. if (I != Stop && *I == ')')
  350. return 0;
  351. if (I == Start)
  352. return 0;
  353. // Stash the package name
  354. Package.assign(Start,I - Start);
  355. // Skip white space to the '('
  356. for (;I != Stop && isspace(*I) != 0 ; I++);
  357. // Parse a version
  358. if (I != Stop && *I == '(')
  359. {
  360. // Skip the '('
  361. for (I++; I != Stop && isspace(*I) != 0 ; I++);
  362. if (I + 3 >= Stop)
  363. return 0;
  364. I = ConvertRelation(I,Op);
  365. // Skip whitespace
  366. for (;I != Stop && isspace(*I) != 0; I++);
  367. Start = I;
  368. for (;I != Stop && *I != ')'; I++);
  369. if (I == Stop || Start == I)
  370. return 0;
  371. // Skip trailing whitespace
  372. const char *End = I;
  373. for (; End > Start && isspace(End[-1]); End--);
  374. Ver = string(Start,End-Start);
  375. I++;
  376. }
  377. else
  378. {
  379. Ver = string();
  380. Op = pkgCache::Dep::NoOp;
  381. }
  382. // Skip whitespace
  383. for (;I != Stop && isspace(*I) != 0; I++);
  384. if (ParseArchFlags == true)
  385. {
  386. string arch = _config->Find("APT::Architecture");
  387. // Parse an architecture
  388. if (I != Stop && *I == '[')
  389. {
  390. // malformed
  391. I++;
  392. if (I == Stop)
  393. return 0;
  394. const char *End = I;
  395. bool Found = false;
  396. bool NegArch = false;
  397. while (I != Stop)
  398. {
  399. // look for whitespace or ending ']'
  400. while (End != Stop && !isspace(*End) && *End != ']')
  401. End++;
  402. if (End == Stop)
  403. return 0;
  404. if (*I == '!')
  405. {
  406. NegArch = true;
  407. I++;
  408. }
  409. if (stringcmp(arch,I,End) == 0)
  410. Found = true;
  411. if (*End++ == ']') {
  412. I = End;
  413. break;
  414. }
  415. I = End;
  416. for (;I != Stop && isspace(*I) != 0; I++);
  417. }
  418. if (NegArch)
  419. Found = !Found;
  420. if (Found == false)
  421. Package = ""; /* not for this arch */
  422. }
  423. // Skip whitespace
  424. for (;I != Stop && isspace(*I) != 0; I++);
  425. }
  426. if (I != Stop && *I == '|')
  427. Op |= pkgCache::Dep::Or;
  428. if (I == Stop || *I == ',' || *I == '|')
  429. {
  430. if (I != Stop)
  431. for (I++; I != Stop && isspace(*I) != 0; I++);
  432. return I;
  433. }
  434. return 0;
  435. }
  436. /*}}}*/
  437. // ListParser::ParseDepends - Parse a dependency list /*{{{*/
  438. // ---------------------------------------------------------------------
  439. /* This is the higher level depends parser. It takes a tag and generates
  440. a complete depends tree for the given version. */
  441. bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
  442. const char *Tag,unsigned int Type)
  443. {
  444. const char *Start;
  445. const char *Stop;
  446. if (Section.Find(Tag,Start,Stop) == false)
  447. return true;
  448. string Package;
  449. string Version;
  450. unsigned int Op;
  451. while (1)
  452. {
  453. Start = ParseDepends(Start,Stop,Package,Version,Op);
  454. if (Start == 0)
  455. return _error->Error("Problem parsing dependency %s",Tag);
  456. if (NewDepends(Ver,Package,Version,Op,Type) == false)
  457. return false;
  458. if (Start == Stop)
  459. break;
  460. }
  461. return true;
  462. }
  463. /*}}}*/
  464. // ListParser::ParseProvides - Parse the provides list /*{{{*/
  465. // ---------------------------------------------------------------------
  466. /* */
  467. bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
  468. {
  469. const char *Start;
  470. const char *Stop;
  471. if (Section.Find("Provides",Start,Stop) == false)
  472. return true;
  473. string Package;
  474. string Version;
  475. unsigned int Op;
  476. while (1)
  477. {
  478. Start = ParseDepends(Start,Stop,Package,Version,Op);
  479. if (Start == 0)
  480. return _error->Error("Problem parsing Provides line");
  481. if (Op != pkgCache::Dep::NoOp)
  482. return _error->Error("Malformed provides line");
  483. if (NewProvides(Ver,Package,Version) == false)
  484. return false;
  485. if (Start == Stop)
  486. break;
  487. }
  488. return true;
  489. }
  490. /*}}}*/
  491. // ListParser::GrabWord - Matches a word and returns /*{{{*/
  492. // ---------------------------------------------------------------------
  493. /* Looks for a word in a list of words - for ParseStatus */
  494. bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out)
  495. {
  496. for (unsigned int C = 0; List[C].Str != 0; C++)
  497. {
  498. if (strcasecmp(Word.c_str(),List[C].Str) == 0)
  499. {
  500. Out = List[C].Val;
  501. return true;
  502. }
  503. }
  504. return false;
  505. }
  506. /*}}}*/
  507. // ListParser::Step - Move to the next section in the file /*{{{*/
  508. // ---------------------------------------------------------------------
  509. /* This has to be carefull to only process the correct architecture */
  510. bool debListParser::Step()
  511. {
  512. iOffset = Tags.Offset();
  513. while (Tags.Step(Section) == true)
  514. {
  515. /* See if this is the correct Architecture, if it isn't then we
  516. drop the whole section. A missing arch tag only happens (in theory)
  517. inside the Status file, so that is a positive return */
  518. const char *Start;
  519. const char *Stop;
  520. if (Section.Find("Architecture",Start,Stop) == false)
  521. return true;
  522. if (stringcmp(Arch,Start,Stop) == 0)
  523. return true;
  524. if (stringcmp(Start,Stop,"all") == 0)
  525. return true;
  526. iOffset = Tags.Offset();
  527. }
  528. return false;
  529. }
  530. /*}}}*/
  531. // ListParser::LoadReleaseInfo - Load the release information /*{{{*/
  532. // ---------------------------------------------------------------------
  533. /* */
  534. bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI,
  535. FileFd &File)
  536. {
  537. pkgTagFile Tags(&File, File.Size() + 256); // XXX
  538. pkgTagSection Section;
  539. if (Tags.Step(Section) == false)
  540. return false;
  541. const char *Start;
  542. const char *Stop;
  543. if (Section.Find("Suite",Start,Stop) == true)
  544. FileI->Archive = WriteUniqString(Start,Stop - Start);
  545. if (Section.Find("Component",Start,Stop) == true)
  546. FileI->Component = WriteUniqString(Start,Stop - Start);
  547. if (Section.Find("Version",Start,Stop) == true)
  548. FileI->Version = WriteUniqString(Start,Stop - Start);
  549. if (Section.Find("Origin",Start,Stop) == true)
  550. FileI->Origin = WriteUniqString(Start,Stop - Start);
  551. if (Section.Find("Label",Start,Stop) == true)
  552. FileI->Label = WriteUniqString(Start,Stop - Start);
  553. if (Section.Find("Architecture",Start,Stop) == true)
  554. FileI->Architecture = WriteUniqString(Start,Stop - Start);
  555. if (Section.FindFlag("NotAutomatic",FileI->Flags,
  556. pkgCache::Flag::NotAutomatic) == false)
  557. _error->Warning("Bad NotAutomatic flag");
  558. return !_error->PendingError();
  559. }
  560. /*}}}*/
  561. // ListParser::GetPrio - Convert the priority from a string /*{{{*/
  562. // ---------------------------------------------------------------------
  563. /* */
  564. unsigned char debListParser::GetPrio(string Str)
  565. {
  566. unsigned char Out;
  567. if (GrabWord(Str,PrioList,Out) == false)
  568. Out = pkgCache::State::Extra;
  569. return Out;
  570. }
  571. /*}}}*/