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