indexrecords.cc 3.9 KB

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