indexrecords.cc 4.1 KB

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