You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

664 lines
20 KiB

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