indexrecords.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: indexrecords.cc,v 1.1.2.4 2003/12/30 02:11:43 mdz Exp $
  4. /*}}}*/
  5. // Include Files /*{{{*/
  6. #ifdef __GNUG__
  7. #pragma implementation "apt-pkg/indexrecords.h"
  8. #endif
  9. #include <apt-pkg/indexrecords.h>
  10. #include <apt-pkg/tagfile.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apti18n.h>
  14. #include <sys/stat.h>
  15. string indexRecords::GetDist() const
  16. {
  17. return this->Dist;
  18. }
  19. bool indexRecords::CheckDist(const string MaybeDist) const
  20. {
  21. return (this->Dist == MaybeDist
  22. || this->Suite == MaybeDist);
  23. }
  24. string indexRecords::GetExpectedDist() const
  25. {
  26. return this->ExpectedDist;
  27. }
  28. const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
  29. {
  30. return Entries[MetaKey];
  31. }
  32. bool indexRecords::Load(const string Filename)
  33. {
  34. FileFd Fd(Filename, FileFd::ReadOnly);
  35. pkgTagFile TagFile(&Fd, Fd.Size() + 256); // XXX
  36. if (_error->PendingError() == true)
  37. {
  38. ErrorText = _(("Unable to parse Release file " + Filename).c_str());
  39. return false;
  40. }
  41. pkgTagSection Section;
  42. if (TagFile.Step(Section) == false)
  43. {
  44. ErrorText = _(("No sections in Release file " + Filename).c_str());
  45. return false;
  46. }
  47. const char *Start, *End;
  48. Section.Get (Start, End, 0);
  49. Suite = Section.FindS("Suite");
  50. Dist = Section.FindS("Codename");
  51. // if (Dist.empty())
  52. // {
  53. // ErrorText = _(("No Codename entry in Release file " + Filename).c_str());
  54. // return false;
  55. // }
  56. if (!Section.Find("MD5Sum", Start, End))
  57. {
  58. ErrorText = _(("No MD5Sum entry in Release file " + Filename).c_str());
  59. return false;
  60. }
  61. string Name;
  62. string MD5Hash;
  63. size_t Size;
  64. while (Start < End)
  65. {
  66. if (!parseSumData(Start, End, Name, MD5Hash, Size))
  67. return false;
  68. indexRecords::checkSum *Sum = new indexRecords::checkSum;
  69. Sum->MetaKeyFilename = Name;
  70. Sum->MD5Hash = MD5Hash;
  71. Sum->Size = Size;
  72. Entries[Name] = Sum;
  73. }
  74. string Strdate = Section.FindS("Date"); // FIXME: verify this somehow?
  75. return true;
  76. }
  77. vector<string> indexRecords::MetaKeys()
  78. {
  79. std::vector<std::string> keys;
  80. std::map<string,checkSum *>::iterator I = Entries.begin();
  81. while(I != Entries.end()) {
  82. keys.push_back((*I).first);
  83. ++I;
  84. }
  85. return keys;
  86. }
  87. bool indexRecords::parseSumData(const char *&Start, const char *End,
  88. string &Name, string &Hash, size_t &Size)
  89. {
  90. Name = "";
  91. Hash = "";
  92. Size = 0;
  93. /* Skip over the first blank */
  94. while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
  95. && Start < End)
  96. Start++;
  97. if (Start >= End)
  98. return false;
  99. /* Move EntryEnd to the end of the first entry (the hash) */
  100. const char *EntryEnd = Start;
  101. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  102. && EntryEnd < End)
  103. EntryEnd++;
  104. if (EntryEnd == End)
  105. return false;
  106. Hash.append(Start, EntryEnd-Start);
  107. /* Skip over intermediate blanks */
  108. Start = EntryEnd;
  109. while (*Start == '\t' || *Start == ' ')
  110. Start++;
  111. if (Start >= End)
  112. return false;
  113. EntryEnd = Start;
  114. /* Find the end of the second entry (the size) */
  115. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  116. && EntryEnd < End)
  117. EntryEnd++;
  118. if (EntryEnd == End)
  119. return false;
  120. Size = strtol (Start, NULL, 10);
  121. /* Skip over intermediate blanks */
  122. Start = EntryEnd;
  123. while (*Start == '\t' || *Start == ' ')
  124. Start++;
  125. if (Start >= End)
  126. return false;
  127. EntryEnd = Start;
  128. /* Find the end of the third entry (the filename) */
  129. while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
  130. && EntryEnd < End)
  131. EntryEnd++;
  132. Name.append(Start, EntryEnd-Start);
  133. Start = EntryEnd; //prepare for the next round
  134. return true;
  135. }
  136. indexRecords::indexRecords()
  137. {
  138. }
  139. indexRecords::indexRecords(const string ExpectedDist) :
  140. ExpectedDist(ExpectedDist)
  141. {
  142. }