indexrecords.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // if (Dist.empty())
  49. // {
  50. // ErrorText = _(("No Codename entry in Release file " + Filename).c_str());
  51. // return false;
  52. // }
  53. if (!Section.Find("MD5Sum", Start, End))
  54. {
  55. ErrorText = _(("No MD5Sum entry in Release file " + Filename).c_str());
  56. return false;
  57. }
  58. string Name;
  59. string MD5Hash;
  60. size_t Size;
  61. while (Start < End)
  62. {
  63. if (!parseSumData(Start, End, Name, MD5Hash, Size))
  64. return false;
  65. indexRecords::checkSum *Sum = new indexRecords::checkSum;
  66. Sum->MetaKeyFilename = Name;
  67. Sum->MD5Hash = MD5Hash;
  68. Sum->Size = Size;
  69. Entries[Name] = Sum;
  70. }
  71. string Strdate = Section.FindS("Date"); // FIXME: verify this somehow?
  72. return true;
  73. }
  74. vector<string> indexRecords::MetaKeys()
  75. {
  76. std::vector<std::string> keys;
  77. std::map<string,checkSum *>::iterator I = Entries.begin();
  78. while(I != Entries.end()) {
  79. keys.push_back((*I).first);
  80. ++I;
  81. }
  82. return keys;
  83. }
  84. bool indexRecords::parseSumData(const char *&Start, const char *End,
  85. string &Name, string &Hash, size_t &Size)
  86. {
  87. Name = "";
  88. Hash = "";
  89. Size = 0;
  90. /* Skip over the first blank */
  91. while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
  92. && Start < End)
  93. Start++;
  94. if (Start >= End)
  95. return false;
  96. /* Move EntryEnd to the end of the first entry (the hash) */
  97. const char *EntryEnd = Start;
  98. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  99. && EntryEnd < End)
  100. EntryEnd++;
  101. if (EntryEnd == End)
  102. return false;
  103. Hash.append(Start, EntryEnd-Start);
  104. /* Skip over intermediate blanks */
  105. Start = EntryEnd;
  106. while (*Start == '\t' || *Start == ' ')
  107. Start++;
  108. if (Start >= End)
  109. return false;
  110. EntryEnd = Start;
  111. /* Find the end of the second entry (the size) */
  112. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  113. && EntryEnd < End)
  114. EntryEnd++;
  115. if (EntryEnd == End)
  116. return false;
  117. Size = strtol (Start, NULL, 10);
  118. /* Skip over intermediate blanks */
  119. Start = EntryEnd;
  120. while (*Start == '\t' || *Start == ' ')
  121. Start++;
  122. if (Start >= End)
  123. return false;
  124. EntryEnd = Start;
  125. /* Find the end of the third entry (the filename) */
  126. while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
  127. && EntryEnd < End)
  128. EntryEnd++;
  129. Name.append(Start, EntryEnd-Start);
  130. Start = EntryEnd; //prepare for the next round
  131. return true;
  132. }
  133. indexRecords::indexRecords()
  134. {
  135. }
  136. indexRecords::indexRecords(const string ExpectedDist) :
  137. ExpectedDist(ExpectedDist)
  138. {
  139. }