deblistparser.cc 18 KB

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