deblistparser.cc 16 KB

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