tagfile.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: tagfile.cc,v 1.37.2.2 2003/12/31 16:02:30 mdz Exp $
  4. /* ######################################################################
  5. Fast scanner for RFC-822 type header information
  6. This uses a rotating buffer to load the package information into.
  7. The scanner runs over it and isolates and indexes a single section.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include<config.h>
  12. #include <apt-pkg/tagfile.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <string>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <apti18n.h>
  22. /*}}}*/
  23. using std::string;
  24. class pkgTagFilePrivate
  25. {
  26. public:
  27. pkgTagFilePrivate(FileFd *pFd, unsigned long long Size) : Fd(*pFd), Buffer(NULL),
  28. Start(NULL), End(NULL),
  29. Done(false), iOffset(0),
  30. Size(Size)
  31. {
  32. }
  33. FileFd &Fd;
  34. char *Buffer;
  35. char *Start;
  36. char *End;
  37. bool Done;
  38. unsigned long long iOffset;
  39. unsigned long long Size;
  40. };
  41. class pkgTagSectionPrivate
  42. {
  43. public:
  44. pkgTagSectionPrivate()
  45. {
  46. }
  47. struct TagData {
  48. unsigned int StartTag;
  49. unsigned int EndTag;
  50. unsigned int StartValue;
  51. unsigned int NextInBucket;
  52. explicit TagData(unsigned int const StartTag) : StartTag(StartTag), EndTag(0), StartValue(0), NextInBucket(0) {}
  53. };
  54. std::vector<TagData> Tags;
  55. };
  56. static unsigned long AlphaHash(const char *Text, size_t Length) /*{{{*/
  57. {
  58. /* This very simple hash function for the last 8 letters gives
  59. very good performance on the debian package files */
  60. if (Length > 8)
  61. {
  62. Text += (Length - 8);
  63. Length = 8;
  64. }
  65. unsigned long Res = 0;
  66. for (size_t i = 0; i < Length; ++i)
  67. Res = ((unsigned long)(Text[i]) & 0xDF) ^ (Res << 1);
  68. return Res & 0xFF;
  69. }
  70. /*}}}*/
  71. // TagFile::pkgTagFile - Constructor /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* */
  74. pkgTagFile::pkgTagFile(FileFd *pFd,unsigned long long Size)
  75. : d(NULL)
  76. {
  77. Init(pFd, Size);
  78. }
  79. void pkgTagFile::Init(FileFd *pFd,unsigned long long Size)
  80. {
  81. /* The size is increased by 4 because if we start with the Size of the
  82. filename we need to try to read 1 char more to see an EOF faster, 1
  83. char the end-pointer can be on and maybe 2 newlines need to be added
  84. to the end of the file -> 4 extra chars */
  85. Size += 4;
  86. if(d != NULL)
  87. {
  88. free(d->Buffer);
  89. delete d;
  90. }
  91. d = new pkgTagFilePrivate(pFd, Size);
  92. if (d->Fd.IsOpen() == false)
  93. d->Start = d->End = d->Buffer = 0;
  94. else
  95. d->Buffer = (char*)malloc(sizeof(char) * Size);
  96. if (d->Buffer == NULL)
  97. d->Done = true;
  98. else
  99. d->Done = false;
  100. d->Start = d->End = d->Buffer;
  101. d->iOffset = 0;
  102. if (d->Done == false)
  103. Fill();
  104. }
  105. /*}}}*/
  106. // TagFile::~pkgTagFile - Destructor /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. pkgTagFile::~pkgTagFile()
  110. {
  111. free(d->Buffer);
  112. delete d;
  113. }
  114. /*}}}*/
  115. // TagFile::Offset - Return the current offset in the buffer /*{{{*/
  116. APT_PURE unsigned long pkgTagFile::Offset()
  117. {
  118. return d->iOffset;
  119. }
  120. /*}}}*/
  121. // TagFile::Resize - Resize the internal buffer /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* Resize the internal buffer (double it in size). Fail if a maximum size
  124. * size is reached.
  125. */
  126. bool pkgTagFile::Resize()
  127. {
  128. // fail is the buffer grows too big
  129. if(d->Size > 1024*1024+1)
  130. return false;
  131. return Resize(d->Size * 2);
  132. }
  133. bool pkgTagFile::Resize(unsigned long long const newSize)
  134. {
  135. unsigned long long const EndSize = d->End - d->Start;
  136. // get new buffer and use it
  137. char* newBuffer = (char*)realloc(d->Buffer, sizeof(char) * newSize);
  138. if (newBuffer == NULL)
  139. return false;
  140. d->Buffer = newBuffer;
  141. d->Size = newSize;
  142. // update the start/end pointers to the new buffer
  143. d->Start = d->Buffer;
  144. d->End = d->Start + EndSize;
  145. return true;
  146. }
  147. /*}}}*/
  148. // TagFile::Step - Advance to the next section /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* If the Section Scanner fails we refill the buffer and try again.
  151. * If that fails too, double the buffer size and try again until a
  152. * maximum buffer is reached.
  153. */
  154. bool pkgTagFile::Step(pkgTagSection &Tag)
  155. {
  156. if(Tag.Scan(d->Start,d->End - d->Start) == false)
  157. {
  158. do
  159. {
  160. if (Fill() == false)
  161. return false;
  162. if(Tag.Scan(d->Start,d->End - d->Start, false))
  163. break;
  164. if (Resize() == false)
  165. return _error->Error(_("Unable to parse package file %s (%d)"),
  166. d->Fd.Name().c_str(), 1);
  167. } while (Tag.Scan(d->Start,d->End - d->Start, false) == false);
  168. }
  169. d->Start += Tag.size();
  170. d->iOffset += Tag.size();
  171. Tag.Trim();
  172. return true;
  173. }
  174. /*}}}*/
  175. // TagFile::Fill - Top up the buffer /*{{{*/
  176. // ---------------------------------------------------------------------
  177. /* This takes the bit at the end of the buffer and puts it at the start
  178. then fills the rest from the file */
  179. bool pkgTagFile::Fill()
  180. {
  181. unsigned long long EndSize = d->End - d->Start;
  182. unsigned long long Actual = 0;
  183. memmove(d->Buffer,d->Start,EndSize);
  184. d->Start = d->Buffer;
  185. d->End = d->Buffer + EndSize;
  186. if (d->Done == false)
  187. {
  188. // See if only a bit of the file is left
  189. unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1);
  190. if (d->Fd.Read(d->End, dataSize, &Actual) == false)
  191. return false;
  192. if (Actual != dataSize)
  193. d->Done = true;
  194. d->End += Actual;
  195. }
  196. if (d->Done == true)
  197. {
  198. if (EndSize <= 3 && Actual == 0)
  199. return false;
  200. if (d->Size - (d->End - d->Buffer) < 4)
  201. return true;
  202. // Append a double new line if one does not exist
  203. unsigned int LineCount = 0;
  204. for (const char *E = d->End - 1; E - d->End < 6 && (*E == '\n' || *E == '\r'); E--)
  205. if (*E == '\n')
  206. LineCount++;
  207. if (LineCount < 2)
  208. {
  209. if ((unsigned)(d->End - d->Buffer) >= d->Size)
  210. Resize(d->Size + 3);
  211. for (; LineCount < 2; LineCount++)
  212. *d->End++ = '\n';
  213. }
  214. return true;
  215. }
  216. return true;
  217. }
  218. /*}}}*/
  219. // TagFile::Jump - Jump to a pre-recorded location in the file /*{{{*/
  220. // ---------------------------------------------------------------------
  221. /* This jumps to a pre-recorded file location and reads the record
  222. that is there */
  223. bool pkgTagFile::Jump(pkgTagSection &Tag,unsigned long long Offset)
  224. {
  225. // We are within a buffer space of the next hit..
  226. if (Offset >= d->iOffset && d->iOffset + (d->End - d->Start) > Offset)
  227. {
  228. unsigned long long Dist = Offset - d->iOffset;
  229. d->Start += Dist;
  230. d->iOffset += Dist;
  231. // if we have seen the end, don't ask for more
  232. if (d->Done == true)
  233. return Tag.Scan(d->Start, d->End - d->Start);
  234. else
  235. return Step(Tag);
  236. }
  237. // Reposition and reload..
  238. d->iOffset = Offset;
  239. d->Done = false;
  240. if (d->Fd.Seek(Offset) == false)
  241. return false;
  242. d->End = d->Start = d->Buffer;
  243. if (Fill() == false)
  244. return false;
  245. if (Tag.Scan(d->Start, d->End - d->Start) == true)
  246. return true;
  247. // This appends a double new line (for the real eof handling)
  248. if (Fill() == false)
  249. return false;
  250. if (Tag.Scan(d->Start, d->End - d->Start, false) == false)
  251. return _error->Error(_("Unable to parse package file %s (%d)"),d->Fd.Name().c_str(), 2);
  252. return true;
  253. }
  254. /*}}}*/
  255. // pkgTagSection::pkgTagSection - Constructor /*{{{*/
  256. // ---------------------------------------------------------------------
  257. /* */
  258. APT_IGNORE_DEPRECATED_PUSH
  259. pkgTagSection::pkgTagSection()
  260. : Section(0), d(NULL), Stop(0)
  261. {
  262. d = new pkgTagSectionPrivate();
  263. #if APT_PKG_ABI < 413
  264. TagCount = 0;
  265. memset(&Indexes, 0, sizeof(Indexes));
  266. #endif
  267. memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
  268. }
  269. APT_IGNORE_DEPRECATED_POP
  270. /*}}}*/
  271. // TagSection::Scan - Scan for the end of the header information /*{{{*/
  272. #if APT_PKG_ABI < 413
  273. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength)
  274. {
  275. return Scan(Start, MaxLength, true);
  276. }
  277. #endif
  278. bool pkgTagSection::Scan(const char *Start,unsigned long MaxLength, bool const Restart)
  279. {
  280. Section = Start;
  281. const char *End = Start + MaxLength;
  282. if (Restart == false && d->Tags.empty() == false)
  283. {
  284. Stop = Section + d->Tags.back().StartTag;
  285. if (End <= Stop)
  286. return false;
  287. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  288. if (Stop == NULL)
  289. return false;
  290. ++Stop;
  291. }
  292. else
  293. {
  294. Stop = Section;
  295. if (d->Tags.empty() == false)
  296. {
  297. memset(&AlphaIndexes, 0, sizeof(AlphaIndexes));
  298. d->Tags.clear();
  299. }
  300. d->Tags.reserve(0x100);
  301. }
  302. #if APT_PKG_ABI >= 413
  303. unsigned int TagCount = d->Tags.size();
  304. #else
  305. APT_IGNORE_DEPRECATED(TagCount = d->Tags.size();)
  306. #endif
  307. if (Stop == 0)
  308. return false;
  309. pkgTagSectionPrivate::TagData lastTagData(0);
  310. lastTagData.EndTag = 0;
  311. unsigned long lastTagHash = 0;
  312. while (Stop < End)
  313. {
  314. TrimRecord(true,End);
  315. // this can happen when TrimRecord trims away the entire Record
  316. // (e.g. because it just contains comments)
  317. if(Stop == End)
  318. return true;
  319. // Start a new index and add it to the hash
  320. if (isspace(Stop[0]) == 0)
  321. {
  322. // store the last found tag
  323. if (lastTagData.EndTag != 0)
  324. {
  325. if (AlphaIndexes[lastTagHash] != 0)
  326. lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
  327. APT_IGNORE_DEPRECATED_PUSH
  328. AlphaIndexes[lastTagHash] = TagCount;
  329. #if APT_PKG_ABI < 413
  330. if (d->Tags.size() < sizeof(Indexes)/sizeof(Indexes[0]))
  331. Indexes[d->Tags.size()] = lastTagData.StartTag;
  332. #endif
  333. APT_IGNORE_DEPRECATED_POP
  334. d->Tags.push_back(lastTagData);
  335. }
  336. APT_IGNORE_DEPRECATED(++TagCount;)
  337. lastTagData = pkgTagSectionPrivate::TagData(Stop - Section);
  338. // find the colon separating tag and value
  339. char const * Colon = (char const *) memchr(Stop, ':', End - Stop);
  340. if (Colon == NULL)
  341. return false;
  342. // find the end of the tag (which might or might not be the colon)
  343. char const * EndTag = Colon;
  344. --EndTag;
  345. for (; EndTag > Stop && isspace(*EndTag) != 0; --EndTag)
  346. ;
  347. ++EndTag;
  348. lastTagData.EndTag = EndTag - Section;
  349. lastTagHash = AlphaHash(Stop, EndTag - Stop);
  350. // find the beginning of the value
  351. Stop = Colon + 1;
  352. for (; isspace(*Stop) != 0; ++Stop);
  353. if (Stop >= End)
  354. return false;
  355. lastTagData.StartValue = Stop - Section;
  356. }
  357. Stop = (const char *)memchr(Stop,'\n',End - Stop);
  358. if (Stop == 0)
  359. return false;
  360. for (; Stop+1 < End && Stop[1] == '\r'; Stop++)
  361. /* nothing */
  362. ;
  363. // Double newline marks the end of the record
  364. if (Stop+1 < End && Stop[1] == '\n')
  365. {
  366. if (lastTagData.EndTag != 0)
  367. {
  368. if (AlphaIndexes[lastTagHash] != 0)
  369. lastTagData.NextInBucket = AlphaIndexes[lastTagHash];
  370. APT_IGNORE_DEPRECATED(AlphaIndexes[lastTagHash] = TagCount;)
  371. #if APT_PKG_ABI < 413
  372. APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = lastTagData.StartTag;)
  373. #endif
  374. d->Tags.push_back(lastTagData);
  375. }
  376. pkgTagSectionPrivate::TagData const td(Stop - Section);
  377. #if APT_PKG_ABI < 413
  378. APT_IGNORE_DEPRECATED(Indexes[d->Tags.size()] = td.StartTag;)
  379. #endif
  380. d->Tags.push_back(td);
  381. TrimRecord(false,End);
  382. return true;
  383. }
  384. Stop++;
  385. }
  386. return false;
  387. }
  388. /*}}}*/
  389. // TagSection::TrimRecord - Trim off any garbage before/after a record /*{{{*/
  390. // ---------------------------------------------------------------------
  391. /* There should be exactly 2 newline at the end of the record, no more. */
  392. void pkgTagSection::TrimRecord(bool BeforeRecord, const char*& End)
  393. {
  394. if (BeforeRecord == true)
  395. return;
  396. for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r'); Stop++);
  397. }
  398. /*}}}*/
  399. // TagSection::Trim - Trim off any trailing garbage /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* There should be exactly 1 newline at the end of the buffer, no more. */
  402. void pkgTagSection::Trim()
  403. {
  404. for (; Stop > Section + 2 && (Stop[-2] == '\n' || Stop[-2] == '\r'); Stop--);
  405. }
  406. /*}}}*/
  407. // TagSection::Exists - return True if a tag exists /*{{{*/
  408. #if APT_PKG_ABI >= 413
  409. bool pkgTagSection::Exists(const char* const Tag) const
  410. #else
  411. bool pkgTagSection::Exists(const char* const Tag)
  412. #endif
  413. {
  414. unsigned int tmp;
  415. return Find(Tag, tmp);
  416. }
  417. /*}}}*/
  418. // TagSection::Find - Locate a tag /*{{{*/
  419. // ---------------------------------------------------------------------
  420. /* This searches the section for a tag that matches the given string. */
  421. bool pkgTagSection::Find(const char *Tag,unsigned int &Pos) const
  422. {
  423. size_t const Length = strlen(Tag);
  424. unsigned int Bucket = AlphaIndexes[AlphaHash(Tag, Length)];
  425. if (Bucket == 0)
  426. return false;
  427. for (; Bucket != 0; Bucket = d->Tags[Bucket - 1].NextInBucket)
  428. {
  429. if ((d->Tags[Bucket - 1].EndTag - d->Tags[Bucket - 1].StartTag) != Length)
  430. continue;
  431. char const * const St = Section + d->Tags[Bucket - 1].StartTag;
  432. if (strncasecmp(Tag,St,Length) != 0)
  433. continue;
  434. Pos = Bucket - 1;
  435. return true;
  436. }
  437. Pos = 0;
  438. return false;
  439. }
  440. bool pkgTagSection::Find(const char *Tag,const char *&Start,
  441. const char *&End) const
  442. {
  443. unsigned int Pos;
  444. if (Find(Tag, Pos) == false)
  445. return false;
  446. Start = Section + d->Tags[Pos].StartValue;
  447. // Strip off the gunk from the end
  448. End = Section + d->Tags[Pos + 1].StartTag;
  449. if (unlikely(Start > End))
  450. return _error->Error("Internal parsing error");
  451. for (; isspace(End[-1]) != 0 && End > Start; --End);
  452. return true;
  453. }
  454. /*}}}*/
  455. // TagSection::FindS - Find a string /*{{{*/
  456. string pkgTagSection::FindS(const char *Tag) const
  457. {
  458. const char *Start;
  459. const char *End;
  460. if (Find(Tag,Start,End) == false)
  461. return string();
  462. return string(Start,End);
  463. }
  464. /*}}}*/
  465. // TagSection::FindRawS - Find a string /*{{{*/
  466. string pkgTagSection::FindRawS(const char *Tag) const
  467. {
  468. unsigned int Pos;
  469. if (Find(Tag, Pos) == false)
  470. return "";
  471. char const *Start = (char const *) memchr(Section + d->Tags[Pos].EndTag, ':', d->Tags[Pos].StartValue - d->Tags[Pos].EndTag);
  472. ++Start;
  473. char const *End = Section + d->Tags[Pos + 1].StartTag;
  474. if (unlikely(Start > End))
  475. return "";
  476. for (; isspace(End[-1]) != 0 && End > Start; --End);
  477. return std::string(Start, End - Start);
  478. }
  479. /*}}}*/
  480. // TagSection::FindI - Find an integer /*{{{*/
  481. // ---------------------------------------------------------------------
  482. /* */
  483. signed int pkgTagSection::FindI(const char *Tag,signed long Default) const
  484. {
  485. const char *Start;
  486. const char *Stop;
  487. if (Find(Tag,Start,Stop) == false)
  488. return Default;
  489. // Copy it into a temp buffer so we can use strtol
  490. char S[300];
  491. if ((unsigned)(Stop - Start) >= sizeof(S))
  492. return Default;
  493. strncpy(S,Start,Stop-Start);
  494. S[Stop - Start] = 0;
  495. char *End;
  496. signed long Result = strtol(S,&End,10);
  497. if (S == End)
  498. return Default;
  499. return Result;
  500. }
  501. /*}}}*/
  502. // TagSection::FindULL - Find an unsigned long long integer /*{{{*/
  503. // ---------------------------------------------------------------------
  504. /* */
  505. unsigned long long pkgTagSection::FindULL(const char *Tag, unsigned long long const &Default) const
  506. {
  507. const char *Start;
  508. const char *Stop;
  509. if (Find(Tag,Start,Stop) == false)
  510. return Default;
  511. // Copy it into a temp buffer so we can use strtoull
  512. char S[100];
  513. if ((unsigned)(Stop - Start) >= sizeof(S))
  514. return Default;
  515. strncpy(S,Start,Stop-Start);
  516. S[Stop - Start] = 0;
  517. char *End;
  518. unsigned long long Result = strtoull(S,&End,10);
  519. if (S == End)
  520. return Default;
  521. return Result;
  522. }
  523. /*}}}*/
  524. // TagSection::FindB - Find boolean value /*{{{*/
  525. // ---------------------------------------------------------------------
  526. /* */
  527. bool pkgTagSection::FindB(const char *Tag, bool const &Default) const
  528. {
  529. const char *Start, *Stop;
  530. if (Find(Tag, Start, Stop) == false)
  531. return Default;
  532. return StringToBool(string(Start, Stop));
  533. }
  534. /*}}}*/
  535. // TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
  536. // ---------------------------------------------------------------------
  537. /* The bits marked in Flag are masked on/off in Flags */
  538. bool pkgTagSection::FindFlag(const char *Tag,unsigned long &Flags,
  539. unsigned long Flag) const
  540. {
  541. const char *Start;
  542. const char *Stop;
  543. if (Find(Tag,Start,Stop) == false)
  544. return true;
  545. return FindFlag(Flags, Flag, Start, Stop);
  546. }
  547. bool pkgTagSection::FindFlag(unsigned long &Flags, unsigned long Flag,
  548. char const* Start, char const* Stop)
  549. {
  550. switch (StringToBool(string(Start, Stop)))
  551. {
  552. case 0:
  553. Flags &= ~Flag;
  554. return true;
  555. case 1:
  556. Flags |= Flag;
  557. return true;
  558. default:
  559. _error->Warning("Unknown flag value: %s",string(Start,Stop).c_str());
  560. return true;
  561. }
  562. return true;
  563. }
  564. /*}}}*/
  565. void pkgTagSection::Get(const char *&Start,const char *&Stop,unsigned int I) const
  566. {
  567. Start = Section + d->Tags[I].StartTag;
  568. Stop = Section + d->Tags[I+1].StartTag;
  569. }
  570. APT_PURE unsigned int pkgTagSection::Count() const { /*{{{*/
  571. if (d->Tags.empty() == true)
  572. return 0;
  573. // the last element is just marking the end and isn't a real one
  574. return d->Tags.size() - 1;
  575. }
  576. /*}}}*/
  577. // TagSection::Write - Ordered (re)writing of fields /*{{{*/
  578. pkgTagSection::Tag pkgTagSection::Tag::Remove(std::string const &Name)
  579. {
  580. return Tag(REMOVE, Name, "");
  581. }
  582. pkgTagSection::Tag pkgTagSection::Tag::Rename(std::string const &OldName, std::string const &NewName)
  583. {
  584. return Tag(RENAME, OldName, NewName);
  585. }
  586. pkgTagSection::Tag pkgTagSection::Tag::Rewrite(std::string const &Name, std::string const &Data)
  587. {
  588. if (Data.empty() == true)
  589. return Tag(REMOVE, Name, "");
  590. else
  591. return Tag(REWRITE, Name, Data);
  592. }
  593. static bool WriteTag(FileFd &File, std::string Tag, std::string const &Value)
  594. {
  595. if (Value.empty() || isspace(Value[0]) != 0)
  596. Tag.append(":");
  597. else
  598. Tag.append(": ");
  599. Tag.append(Value);
  600. Tag.append("\n");
  601. return File.Write(Tag.c_str(), Tag.length());
  602. }
  603. static bool RewriteTags(FileFd &File, pkgTagSection const * const This, char const * const Tag,
  604. std::vector<pkgTagSection::Tag>::const_iterator &R,
  605. std::vector<pkgTagSection::Tag>::const_iterator const &REnd)
  606. {
  607. size_t const TagLen = strlen(Tag);
  608. for (; R != REnd; ++R)
  609. {
  610. std::string data;
  611. if (R->Name.length() == TagLen && strncasecmp(R->Name.c_str(), Tag, R->Name.length()) == 0)
  612. {
  613. if (R->Action != pkgTagSection::Tag::REWRITE)
  614. break;
  615. data = R->Data;
  616. }
  617. else if(R->Action == pkgTagSection::Tag::RENAME && R->Data.length() == TagLen &&
  618. strncasecmp(R->Data.c_str(), Tag, R->Data.length()) == 0)
  619. data = This->FindRawS(R->Name.c_str());
  620. else
  621. continue;
  622. return WriteTag(File, Tag, data);
  623. }
  624. return true;
  625. }
  626. bool pkgTagSection::Write(FileFd &File, char const * const * const Order, std::vector<Tag> const &Rewrite) const
  627. {
  628. // first pass: Write everything we have an order for
  629. if (Order != NULL)
  630. {
  631. for (unsigned int I = 0; Order[I] != 0; ++I)
  632. {
  633. std::vector<Tag>::const_iterator R = Rewrite.begin();
  634. if (RewriteTags(File, this, Order[I], R, Rewrite.end()) == false)
  635. return false;
  636. if (R != Rewrite.end())
  637. continue;
  638. if (Exists(Order[I]) == false)
  639. continue;
  640. if (WriteTag(File, Order[I], FindRawS(Order[I])) == false)
  641. return false;
  642. }
  643. }
  644. // second pass: See if we have tags which aren't ordered
  645. if (d->Tags.empty() == false)
  646. {
  647. for (std::vector<pkgTagSectionPrivate::TagData>::const_iterator T = d->Tags.begin(); T != d->Tags.end() - 1; ++T)
  648. {
  649. char const * const fieldname = Section + T->StartTag;
  650. size_t fieldnamelen = T->EndTag - T->StartTag;
  651. if (Order != NULL)
  652. {
  653. unsigned int I = 0;
  654. for (; Order[I] != 0; ++I)
  655. {
  656. if (fieldnamelen == strlen(Order[I]) && strncasecmp(fieldname, Order[I], fieldnamelen) == 0)
  657. break;
  658. }
  659. if (Order[I] != 0)
  660. continue;
  661. }
  662. std::string const name(fieldname, fieldnamelen);
  663. std::vector<Tag>::const_iterator R = Rewrite.begin();
  664. if (RewriteTags(File, this, name.c_str(), R, Rewrite.end()) == false)
  665. return false;
  666. if (R != Rewrite.end())
  667. continue;
  668. if (WriteTag(File, name, FindRawS(name.c_str())) == false)
  669. return false;
  670. }
  671. }
  672. // last pass: see if there are any rewrites remaining we haven't done yet
  673. for (std::vector<Tag>::const_iterator R = Rewrite.begin(); R != Rewrite.end(); ++R)
  674. {
  675. if (R->Action == Tag::REMOVE)
  676. continue;
  677. std::string const name = ((R->Action == Tag::RENAME) ? R->Data : R->Name);
  678. if (Exists(name.c_str()))
  679. continue;
  680. if (Order != NULL)
  681. {
  682. unsigned int I = 0;
  683. for (; Order[I] != 0; ++I)
  684. {
  685. if (strncasecmp(name.c_str(), Order[I], name.length()) == 0 && name.length() == strlen(Order[I]))
  686. break;
  687. }
  688. if (Order[I] != 0)
  689. continue;
  690. }
  691. if (WriteTag(File, name, ((R->Action == Tag::RENAME) ? FindRawS(R->Name.c_str()) : R->Data)) == false)
  692. return false;
  693. }
  694. return true;
  695. }
  696. /*}}}*/
  697. #include "tagfile-order.c"
  698. // TFRewrite - Rewrite a control record /*{{{*/
  699. // ---------------------------------------------------------------------
  700. /* This writes the control record to stdout rewriting it as necessary. The
  701. override map item specificies the rewriting rules to follow. This also
  702. takes the time to sort the feild list. */
  703. APT_IGNORE_DEPRECATED_PUSH
  704. bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
  705. TFRewriteData *Rewrite)
  706. {
  707. unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
  708. for (unsigned I = 0; I != 256; I++)
  709. Visited[I] = 0;
  710. // Set new tag up as necessary.
  711. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  712. {
  713. if (Rewrite[J].NewTag == 0)
  714. Rewrite[J].NewTag = Rewrite[J].Tag;
  715. }
  716. // Write all all of the tags, in order.
  717. if (Order != NULL)
  718. {
  719. for (unsigned int I = 0; Order[I] != 0; I++)
  720. {
  721. bool Rewritten = false;
  722. // See if this is a field that needs to be rewritten
  723. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  724. {
  725. if (strcasecmp(Rewrite[J].Tag,Order[I]) == 0)
  726. {
  727. Visited[J] |= 2;
  728. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  729. {
  730. if (isspace(Rewrite[J].Rewrite[0]))
  731. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  732. else
  733. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  734. }
  735. Rewritten = true;
  736. break;
  737. }
  738. }
  739. // See if it is in the fragment
  740. unsigned Pos;
  741. if (Tags.Find(Order[I],Pos) == false)
  742. continue;
  743. Visited[Pos] |= 1;
  744. if (Rewritten == true)
  745. continue;
  746. /* Write out this element, taking a moment to rewrite the tag
  747. in case of changes of case. */
  748. const char *Start;
  749. const char *Stop;
  750. Tags.Get(Start,Stop,Pos);
  751. if (fputs(Order[I],Output) < 0)
  752. return _error->Errno("fputs","IO Error to output");
  753. Start += strlen(Order[I]);
  754. if (fwrite(Start,Stop - Start,1,Output) != 1)
  755. return _error->Errno("fwrite","IO Error to output");
  756. if (Stop[-1] != '\n')
  757. fprintf(Output,"\n");
  758. }
  759. }
  760. // Now write all the old tags that were missed.
  761. for (unsigned int I = 0; I != Tags.Count(); I++)
  762. {
  763. if ((Visited[I] & 1) == 1)
  764. continue;
  765. const char *Start;
  766. const char *Stop;
  767. Tags.Get(Start,Stop,I);
  768. const char *End = Start;
  769. for (; End < Stop && *End != ':'; End++);
  770. // See if this is a field that needs to be rewritten
  771. bool Rewritten = false;
  772. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  773. {
  774. if (stringcasecmp(Start,End,Rewrite[J].Tag) == 0)
  775. {
  776. Visited[J] |= 2;
  777. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  778. {
  779. if (isspace(Rewrite[J].Rewrite[0]))
  780. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  781. else
  782. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  783. }
  784. Rewritten = true;
  785. break;
  786. }
  787. }
  788. if (Rewritten == true)
  789. continue;
  790. // Write out this element
  791. if (fwrite(Start,Stop - Start,1,Output) != 1)
  792. return _error->Errno("fwrite","IO Error to output");
  793. if (Stop[-1] != '\n')
  794. fprintf(Output,"\n");
  795. }
  796. // Now write all the rewrites that were missed
  797. for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)
  798. {
  799. if ((Visited[J] & 2) == 2)
  800. continue;
  801. if (Rewrite[J].Rewrite != 0 && Rewrite[J].Rewrite[0] != 0)
  802. {
  803. if (isspace(Rewrite[J].Rewrite[0]))
  804. fprintf(Output,"%s:%s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  805. else
  806. fprintf(Output,"%s: %s\n",Rewrite[J].NewTag,Rewrite[J].Rewrite);
  807. }
  808. }
  809. return true;
  810. }
  811. APT_IGNORE_DEPRECATED_POP
  812. /*}}}*/
  813. pkgTagSection::~pkgTagSection() { delete d; }