deblistparser.cc 16 KB

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