You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

210 lines
5.2 KiB

  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 <apt-pkg/configuration.h>
  11. #include <apti18n.h>
  12. #include <sys/stat.h>
  13. #include <clocale>
  14. /*}}}*/
  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. time_t indexRecords::GetValidUntil() const
  29. {
  30. return this->ValidUntil;
  31. }
  32. const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
  33. {
  34. return Entries[MetaKey];
  35. }
  36. bool indexRecords::Exists(string const &MetaKey) const
  37. {
  38. return Entries.count(MetaKey) == 1;
  39. }
  40. bool indexRecords::Load(const string Filename) /*{{{*/
  41. {
  42. FileFd Fd(Filename, FileFd::ReadOnly);
  43. pkgTagFile TagFile(&Fd, Fd.Size() + 256); // XXX
  44. if (_error->PendingError() == true)
  45. {
  46. strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  47. return false;
  48. }
  49. pkgTagSection Section;
  50. if (TagFile.Step(Section) == false)
  51. {
  52. strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
  53. return false;
  54. }
  55. const char *Start, *End;
  56. Section.Get (Start, End, 0);
  57. Suite = Section.FindS("Suite");
  58. Dist = Section.FindS("Codename");
  59. int i;
  60. for (i=0;HashString::SupportedHashes()[i] != NULL; i++)
  61. {
  62. if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
  63. continue;
  64. string Name;
  65. string Hash;
  66. size_t Size;
  67. while (Start < End)
  68. {
  69. if (!parseSumData(Start, End, Name, Hash, Size))
  70. return false;
  71. indexRecords::checkSum *Sum = new indexRecords::checkSum;
  72. Sum->MetaKeyFilename = Name;
  73. Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);
  74. Sum->Size = Size;
  75. Entries[Name] = Sum;
  76. }
  77. break;
  78. }
  79. if(HashString::SupportedHashes()[i] == NULL)
  80. {
  81. strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
  82. return false;
  83. }
  84. string Label = Section.FindS("Label");
  85. string StrDate = Section.FindS("Date");
  86. string StrValidUntil = Section.FindS("Valid-Until");
  87. // if we have a Valid-Until header in the Release file, use it as default
  88. if (StrValidUntil.empty() == false)
  89. {
  90. if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
  91. {
  92. strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
  93. return false;
  94. }
  95. }
  96. // get the user settings for this archive and use what expires earlier
  97. int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
  98. if (Label.empty() == true)
  99. MaxAge = _config->FindI(string("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
  100. if(MaxAge == 0) // No user settings, use the one from the Release file
  101. return true;
  102. time_t date;
  103. if (RFC1123StrToTime(StrDate.c_str(), date) == false)
  104. {
  105. strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
  106. return false;
  107. }
  108. date += 24*60*60*MaxAge;
  109. if (ValidUntil == 0 || ValidUntil > date)
  110. ValidUntil = date;
  111. return true;
  112. }
  113. /*}}}*/
  114. vector<string> indexRecords::MetaKeys() /*{{{*/
  115. {
  116. std::vector<std::string> keys;
  117. std::map<string,checkSum *>::iterator I = Entries.begin();
  118. while(I != Entries.end()) {
  119. keys.push_back((*I).first);
  120. ++I;
  121. }
  122. return keys;
  123. }
  124. /*}}}*/
  125. bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
  126. string &Name, string &Hash, size_t &Size)
  127. {
  128. Name = "";
  129. Hash = "";
  130. Size = 0;
  131. /* Skip over the first blank */
  132. while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
  133. && Start < End)
  134. Start++;
  135. if (Start >= End)
  136. return false;
  137. /* Move EntryEnd to the end of the first entry (the hash) */
  138. const char *EntryEnd = Start;
  139. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  140. && EntryEnd < End)
  141. EntryEnd++;
  142. if (EntryEnd == End)
  143. return false;
  144. Hash.append(Start, EntryEnd-Start);
  145. /* Skip over intermediate blanks */
  146. Start = EntryEnd;
  147. while (*Start == '\t' || *Start == ' ')
  148. Start++;
  149. if (Start >= End)
  150. return false;
  151. EntryEnd = Start;
  152. /* Find the end of the second entry (the size) */
  153. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  154. && EntryEnd < End)
  155. EntryEnd++;
  156. if (EntryEnd == End)
  157. return false;
  158. Size = strtol (Start, NULL, 10);
  159. /* Skip over intermediate blanks */
  160. Start = EntryEnd;
  161. while (*Start == '\t' || *Start == ' ')
  162. Start++;
  163. if (Start >= End)
  164. return false;
  165. EntryEnd = Start;
  166. /* Find the end of the third entry (the filename) */
  167. while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
  168. && EntryEnd < End)
  169. EntryEnd++;
  170. Name.append(Start, EntryEnd-Start);
  171. Start = EntryEnd; //prepare for the next round
  172. return true;
  173. }
  174. /*}}}*/
  175. indexRecords::indexRecords()
  176. {
  177. }
  178. indexRecords::indexRecords(const string ExpectedDist) :
  179. ExpectedDist(ExpectedDist), ValidUntil(0)
  180. {
  181. }